4 * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5 * Copyright (c) 2002-2011, Professor Benoit Macq
6 * Copyright (c) 2010-2011, Kaori Hagihara
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
33 #include "ext_openjpeg.h"
36 #include "cidx_manager.h"
37 #include "indexbox_manager.h"
43 * @param[in] cio file output handler
45 void jp2_write_jp(opj_cio_t *cio);
48 * Write FTYP box - File type box
51 * @param[in] jp2 JP2 handle
52 * @param[in] cio file output handle
54 void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
57 * Write file Index (superbox)
59 * @param[in] offset_jp2c offset of jp2c box
60 * @param[in] length_jp2c length of jp2c box
61 * @param[in] offset_idx offset of cidx box
62 * @param[in] length_idx length of cidx box
63 * @param[in] cio file output handle
64 * @return length of fidx box
66 int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
69 * Write index Finder box
71 * @param[in] offset offset of fidx box
72 * @param[in] length length of fidx box
73 * @param[in] cio file output handle
75 void write_iptr( int offset, int length, opj_cio_t *cio);
77 opj_bool idxjp2_encode( opj_cinfo_t *cinfo, opj_cio_t *cio, unsigned char *j2kstream, int j2klen, opj_codestream_info_t cstr_info, opj_image_t *image)
80 int pos_iptr, pos_cidx, pos_jp2c, end_pos, pos_fidx, len_fidx;
83 jp2 = cinfo->jp2_handle;
87 /* JPEG 2000 Signature box */
90 jp2_write_ftyp(jp2, cio);
92 jp2_write_jp2h(jp2, cio);
94 pos_iptr = cio_tell( cio);
95 cio_skip( cio, 24); /* IPTR further ! */
97 pos_jp2c = cio_tell( cio);
99 cio_write( cio, j2klen+8, 4); // L NOTICE: modify for the extended box
100 cio_write( cio, JP2_JP2C, 4); // JP2C
102 for( i=0; i<j2klen; i++)
103 cio_write( cio, j2kstream[i], 1);
105 pos_cidx = cio_tell( cio);
107 write_cidx( pos_jp2c+8, cio, image, cstr_info, j2klen);
109 pos_fidx = cio_tell( cio);
110 len_fidx = write_fidx( pos_jp2c, pos_cidx-pos_jp2c, pos_cidx, cio_tell(cio), cio);
112 end_pos = cio_tell( cio);
114 cio_seek( cio, pos_iptr);
115 write_iptr( pos_fidx, len_fidx, cio);
117 cio_seek( cio, end_pos);
122 void jp2_write_jp( opj_cio_t *cio)
126 box.init_pos = cio_tell(cio);
128 cio_write(cio, JP2_JP, 4); /* JP2 signature */
129 cio_write(cio, 0x0d0a870a, 4);
131 box.length = cio_tell(cio) - box.init_pos;
132 cio_seek(cio, box.init_pos);
133 cio_write(cio, box.length, 4); /* L */
134 cio_seek(cio, box.init_pos + box.length);
137 void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio)
142 box.init_pos = cio_tell(cio);
144 cio_write(cio, JP2_FTYP, 4); /* FTYP */
146 cio_write(cio, jp2->brand, 4); /* BR */
147 cio_write(cio, jp2->minversion, 4); /* MinV */
149 for (i = 0; i < jp2->numcl; i++) {
150 cio_write(cio, jp2->cl[i], 4); /* CL */
153 box.length = cio_tell(cio) - box.init_pos;
154 cio_seek(cio, box.init_pos);
155 cio_write(cio, box.length, 4); /* L */
156 cio_seek(cio, box.init_pos + box.length);
163 * @param[in] offset_jp2c offset of jp2c box
164 * @param[in] length_jp2c length of jp2c box
165 * @param[in] offset_idx offset of cidx box
166 * @param[in] length_idx length of cidx box
167 * @param[in] cio file output handle
169 void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
171 int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
175 lenp = cio_tell( cio);
176 cio_skip( cio, 4); /* L [at the end] */
177 cio_write( cio, JPIP_FIDX, 4); /* IPTR */
179 write_prxy( offset_jp2c, length_jp2c, offset_idx, offset_jp2c, cio);
181 len = cio_tell( cio)-lenp;
182 cio_seek( cio, lenp);
183 cio_write( cio, len, 4); /* L */
184 cio_seek( cio, lenp+len);
189 void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
193 lenp = cio_tell( cio);
194 cio_skip( cio, 4); /* L [at the end] */
195 cio_write( cio, JPIP_PRXY, 4); /* IPTR */
197 cio_ext_write( cio, offset_jp2c, 8); /* OOFF */
198 cio_write( cio, length_jp2c, 4); /* OBH part 1 */
199 cio_write( cio, JP2_JP2C, 4); /* OBH part 2 */
201 cio_write( cio, 1,1); /* NI */
203 cio_ext_write( cio, offset_idx, 8); /* IOFF */
204 cio_write( cio, length_idx, 4); /* IBH part 1 */
205 cio_write( cio, JPIP_CIDX, 4); /* IBH part 2 */
207 len = cio_tell( cio)-lenp;
208 cio_seek( cio, lenp);
209 cio_write( cio, len, 4); /* L */
210 cio_seek( cio, lenp+len);
213 void write_iptr( int offset, int length, opj_cio_t *cio)
217 lenp = cio_tell( cio);
218 cio_skip( cio, 4); /* L [at the end] */
219 cio_write( cio, JPIP_IPTR, 4); /* IPTR */
221 cio_ext_write( cio, offset, 8);
222 cio_ext_write( cio, length, 8);
224 len = cio_tell( cio)-lenp;
225 cio_seek( cio, lenp);
226 cio_write( cio, len, 4); /* L */
227 cio_seek( cio, lenp+len);