2 * $Id: cidx_manager.c 897 2011-08-28 21:43:57Z Kaori.Hagihara@gmail.com $
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) 2003-2004, Yannick Verschueren
7 * Copyright (c) 2010-2011, Kaori Hagihara
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include "opj_includes.h"
36 * Write CPTR Codestream finder box
38 * @param[in] coff offset of j2k codestream
39 * @param[in] clen length of j2k codestream
40 * @param[in] cio file output handle
42 void write_cptr(int coff, int clen, opj_cio_t *cio);
46 * Write main header index table (box)
48 * @param[in] coff offset of j2k codestream
49 * @param[in] cstr_info codestream information
50 * @param[in] cio file output handle
51 * @return length of mainmhix box
53 int write_mainmhix( int coff, opj_codestream_info_t cstr_info, opj_cio_t *cio);
57 * Check if EPH option is used
59 * @param[in] coff offset of j2k codestream
60 * @param[in] markers marker information
61 * @param[in] marknum number of markers
62 * @param[in] cio file output handle
63 * @return true if EPH is used
65 opj_bool check_EPHuse( int coff, opj_marker_info_t *markers, int marknum, opj_cio_t *cio);
68 int write_cidx( int offset, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t cstr_info, int j2klen)
74 (void)image; /* unused ? */
77 box = (opj_jp2_box_t *)opj_calloc( 32, sizeof(opj_jp2_box_t));
84 lenp = cio_tell( cio);
86 cio_skip( cio, 4); /* L [at the end] */
87 cio_write( cio, JPIP_CIDX, 4); /* CIDX */
88 write_cptr( offset, cstr_info.codestream_size, cio);
90 write_manf( i, num_box, box, cio);
93 box[num_box].length = write_mainmhix( offset, cstr_info, cio);
94 box[num_box].type = JPIP_MHIX;
97 box[num_box].length = write_tpix( offset, cstr_info, j2klen, cio);
98 box[num_box].type = JPIP_TPIX;
101 box[num_box].length = write_thix( offset, cstr_info, cio);
102 box[num_box].type = JPIP_THIX;
105 EPHused = check_EPHuse( offset, cstr_info.marker, cstr_info.marknum, cio);
107 box[num_box].length = write_ppix( offset, cstr_info, EPHused, j2klen, cio);
108 box[num_box].type = JPIP_PPIX;
111 box[num_box].length = write_phix( offset, cstr_info, EPHused, j2klen, cio);
112 box[num_box].type = JPIP_PHIX;
115 len = cio_tell( cio)-lenp;
116 cio_seek( cio, lenp);
117 cio_write( cio, len, 4); /* L */
118 cio_seek( cio, lenp+len);
126 void write_cptr(int coff, int clen, opj_cio_t *cio)
130 lenp = cio_tell( cio);
131 cio_skip( cio, 4); /* L [at the end] */
132 cio_write( cio, JPIP_CPTR, 4); /* T */
133 cio_write( cio, 0, 2); /* DR A PRECISER !! */
134 cio_write( cio, 0, 2); /* CONT */
135 cio_write( cio, coff, 8); /* COFF A PRECISER !! */
136 cio_write( cio, clen, 8); /* CLEN */
137 len = cio_tell( cio) - lenp;
138 cio_seek( cio, lenp);
139 cio_write( cio, len, 4); /* L */
140 cio_seek( cio, lenp+len);
143 void write_manf(int second, int v, opj_jp2_box_t *box, opj_cio_t *cio)
147 lenp = cio_tell( cio);
148 cio_skip( cio, 4); /* L [at the end] */
149 cio_write( cio, JPIP_MANF,4); /* T */
151 if (second){ /* Write only during the second pass */
153 cio_write( cio, box[i].length, 4); /* Box length */
154 cio_write( cio, box[i].type, 4); /* Box type */
158 len = cio_tell( cio) - lenp;
159 cio_seek( cio, lenp);
160 cio_write( cio, len, 4); /* L */
161 cio_seek( cio, lenp+len);
164 int write_mainmhix( int coff, opj_codestream_info_t cstr_info, opj_cio_t *cio)
169 lenp = cio_tell( cio);
170 cio_skip( cio, 4); /* L [at the end] */
171 cio_write( cio, JPIP_MHIX, 4); /* MHIX */
173 cio_write( cio, cstr_info.main_head_end-cstr_info.main_head_start+1, 8); /* TLEN */
175 for(i = 1; i < cstr_info.marknum; i++){ /* Marker restricted to 1 apparition, skip SOC marker */
176 cio_write( cio, cstr_info.marker[i].type, 2);
177 cio_write( cio, 0, 2);
178 cio_write( cio, cstr_info.marker[i].pos-coff, 8);
179 cio_write( cio, cstr_info.marker[i].len, 2);
182 len = cio_tell( cio) - lenp;
183 cio_seek( cio, lenp);
184 cio_write( cio, len, 4); /* L */
185 cio_seek( cio, lenp+len);
190 opj_bool check_EPHuse( int coff, opj_marker_info_t *markers, int marknum, opj_cio_t *cio)
192 opj_bool EPHused = OPJ_FALSE;
197 for(i = 0; i < marknum; i++){
198 if( markers[i].type == J2K_MS_COD){
199 org_pos = cio_tell( cio);
200 cio_seek( cio, coff+markers[i].pos+2);
202 Scod = cio_read( cio, 1);
203 if( ((Scod >> 2) & 1))
205 cio_seek( cio, org_pos);