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.
34 #include "cidx_manager.h"
35 #include "indexbox_manager.h"
40 * Write CPTR Codestream finder box
42 * @param[in] coff offset of j2k codestream
43 * @param[in] clen length of j2k codestream
44 * @param[in] cio file output handle
46 void write_cptr(int coff, int clen, opj_cio_t *cio);
50 * Write main header index table (box)
52 * @param[in] coff offset of j2k codestream
53 * @param[in] cstr_info codestream information
54 * @param[in] cio file output handle
55 * @return length of mainmhix box
57 int write_mainmhix( int coff, opj_codestream_info_t cstr_info, opj_cio_t *cio);
61 * Check if EPH option is used
63 * @param[in] coff offset of j2k codestream
64 * @param[in] markers marker information
65 * @param[in] marknum number of markers
66 * @param[in] cio file output handle
67 * @return true if EPH is used
69 opj_bool check_EPHuse( int coff, opj_marker_info_t *markers, int marknum, opj_cio_t *cio);
72 int write_cidx( int offset, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t cstr_info, int j2klen)
79 box = (opj_jp2_box_t *)calloc( 32, sizeof(opj_jp2_box_t));
86 lenp = cio_tell( cio);
88 cio_skip( cio, 4); /* L [at the end] */
89 cio_write( cio, JPIP_CIDX, 4); /* CIDX */
90 write_cptr( offset, cstr_info.codestream_size, cio);
92 write_manf( i, num_box, box, cio);
95 box[num_box].length = write_mainmhix( offset, cstr_info, cio);
96 box[num_box].type = JPIP_MHIX;
99 box[num_box].length = write_tpix( offset, cstr_info, j2klen, cio);
100 box[num_box].type = JPIP_TPIX;
103 box[num_box].length = write_thix( offset, cstr_info, cio);
104 box[num_box].type = JPIP_THIX;
107 EPHused = check_EPHuse( offset, cstr_info.marker, cstr_info.marknum, cio);
109 box[num_box].length = write_ppix( offset, cstr_info, EPHused, j2klen, cio);
110 box[num_box].type = JPIP_PPIX;
113 box[num_box].length = write_phix( offset, cstr_info, EPHused, j2klen, cio);
114 box[num_box].type = JPIP_PHIX;
117 len = cio_tell( cio)-lenp;
118 cio_seek( cio, lenp);
119 cio_write( cio, len, 4); /* L */
120 cio_seek( cio, lenp+len);
128 void write_cptr(int coff, int clen, opj_cio_t *cio)
132 lenp = cio_tell( cio);
133 cio_skip( cio, 4); /* L [at the end] */
134 cio_write( cio, JPIP_CPTR, 4); /* T */
135 cio_write( cio, 0, 2); /* DR A PRECISER !! */
136 cio_write( cio, 0, 2); /* CONT */
137 cio_write( cio, coff, 8); /* COFF A PRECISER !! */
138 cio_write( cio, clen, 8); /* CLEN */
139 len = cio_tell( cio) - lenp;
140 cio_seek( cio, lenp);
141 cio_write( cio, len, 4); /* L */
142 cio_seek( cio, lenp+len);
145 void write_manf(int second, int v, opj_jp2_box_t *box, opj_cio_t *cio)
149 lenp = cio_tell( cio);
150 cio_skip( cio, 4); /* L [at the end] */
151 cio_write( cio, JPIP_MANF,4); /* T */
153 if (second){ /* Write only during the second pass */
155 cio_write( cio, box[i].length, 4); /* Box length */
156 cio_write( cio, box[i].type, 4); /* Box type */
160 len = cio_tell( cio) - lenp;
161 cio_seek( cio, lenp);
162 cio_write( cio, len, 4); /* L */
163 cio_seek( cio, lenp+len);
166 int write_mainmhix( int coff, opj_codestream_info_t cstr_info, opj_cio_t *cio)
171 lenp = cio_tell( cio);
172 cio_skip( cio, 4); /* L [at the end] */
173 cio_write( cio, JPIP_MHIX, 4); /* MHIX */
175 cio_write( cio, cstr_info.main_head_end-cstr_info.main_head_start+1, 8); /* TLEN */
177 for(i = 1; i < cstr_info.marknum; i++){ /* Marker restricted to 1 apparition, skip SOC marker */
178 cio_write( cio, cstr_info.marker[i].type, 2);
179 cio_write( cio, 0, 2);
180 cio_write( cio, cstr_info.marker[i].pos-coff, 8);
181 cio_write( cio, cstr_info.marker[i].len, 2);
184 len = cio_tell( cio) - lenp;
185 cio_seek( cio, lenp);
186 cio_write( cio, len, 4); /* L */
187 cio_seek( cio, lenp+len);
192 opj_bool check_EPHuse( int coff, opj_marker_info_t *markers, int marknum, opj_cio_t *cio)
194 opj_bool EPHused = OPJ_FALSE;
199 for(i = 0; i < marknum; i++){
200 if( markers[i].type == J2K_MS_COD){
201 org_pos = cio_tell( cio);
202 cio_seek( cio, coff+markers[i].pos+2);
204 Scod = cio_read( cio, 1);
205 if( ((Scod >> 2) & 1))
207 cio_seek( cio, org_pos);