2 * $Id: cidx_manager.c 897 2011-08-28 21:43:57Z Kaori.Hagihara@gmail.com $
4 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
5 * Copyright (c) 2002-2014, 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
43 void opj_write_cptr(int coff, int clen, opj_stream_private_t *cio,
44 opj_event_mgr_t * p_manager);
50 int opj_write_cidx(int offset, opj_stream_private_t *cio,
51 opj_codestream_info_t cstr_info, int j2klen,
52 opj_event_mgr_t * p_manager)
60 OPJ_BYTE l_data_header [4];
63 box = (opj_jp2_box_t *)opj_calloc(32, sizeof(opj_jp2_box_t));
67 for (i = 0; i < 2; i++) {
70 opj_stream_seek(cio, lenp, p_manager);
74 lenp = opj_stream_tell(cio);
76 opj_stream_skip(cio, 4, p_manager); /* L [at the end] */
78 opj_write_bytes(l_data_header, JPIP_CIDX, 4); /* CIDX */
79 opj_stream_write_data(cio, l_data_header, 4, p_manager);
81 opj_write_cptr(offset, cstr_info.codestream_size, cio, p_manager);
83 opj_write_manf(i, num_box, box, cio, p_manager);
86 box[num_box].length = (OPJ_UINT32)opj_write_mainmhix(offset, cstr_info, cio,
88 box[num_box].type = JPIP_MHIX;
91 box[num_box].length = (OPJ_UINT32)opj_write_tpix(offset, cstr_info, j2klen, cio,
93 box[num_box].type = JPIP_TPIX;
96 box[num_box].length = (OPJ_UINT32)opj_write_thix(offset, cstr_info, cio,
98 box[num_box].type = JPIP_THIX;
101 EPHused = opj_check_EPHuse(offset, cstr_info.marker, cstr_info.marknum, cio,
104 box[num_box].length = (OPJ_UINT32)opj_write_ppix(offset, cstr_info, EPHused,
105 j2klen, cio, p_manager);
106 box[num_box].type = JPIP_PPIX;
109 box[num_box].length = (OPJ_UINT32)opj_write_phix(offset, cstr_info, EPHused,
110 j2klen, cio, p_manager);
111 box[num_box].type = JPIP_PHIX;
114 len = (OPJ_UINT32)(opj_stream_tell(cio) - lenp);
115 opj_stream_seek(cio, lenp, p_manager);
116 opj_write_bytes(l_data_header, len, 4); /* L */
117 opj_stream_write_data(cio, l_data_header, 4, p_manager);
118 opj_stream_seek(cio, lenp + len, p_manager);
128 void opj_write_cptr(int coff, int clen, opj_stream_private_t *cio,
129 opj_event_mgr_t * p_manager)
131 OPJ_BYTE l_data_header [3 * 8];
136 lenp = opj_stream_tell(cio);
137 opj_stream_skip(cio, 4, p_manager); /* L [at the end] */
138 opj_write_bytes(l_data_header, JPIP_CPTR, 4); /* T */
139 opj_write_bytes(l_data_header + 4, 0, 2); /* DR A PRECISER !! */
140 opj_write_bytes(l_data_header + 6, 0, 2); /* CONT */
141 opj_write_bytes(l_data_header + 8, (OPJ_UINT32)coff,
142 8); /* COFF A PRECISER !! */
143 opj_write_bytes(l_data_header + 16, (OPJ_UINT32)clen,
145 opj_stream_write_data(cio, l_data_header, 3 * 8, p_manager);
147 len = (OPJ_UINT32)(opj_stream_tell(cio) - lenp);
148 opj_stream_seek(cio, lenp, p_manager);
149 opj_write_bytes(l_data_header, len, 4); /* L */
150 opj_stream_write_data(cio, l_data_header, 4, p_manager);
151 opj_stream_seek(cio, lenp + len, p_manager);
157 void opj_write_manf(int second,
160 opj_stream_private_t *cio,
161 opj_event_mgr_t * p_manager)
163 OPJ_BYTE l_data_header [4];
168 lenp = opj_stream_tell(cio);
169 opj_stream_skip(cio, 4, p_manager); /* L [at the end] */
170 opj_write_bytes(l_data_header, JPIP_MANF, 4); /* T */
171 opj_stream_write_data(cio, l_data_header, 4, p_manager);
173 if (second) { /* Write only during the second pass */
174 for (i = 0; i < v; i++) {
175 opj_write_bytes(l_data_header, box[i].length,
177 opj_stream_write_data(cio, l_data_header, 4, p_manager);
178 opj_write_bytes(l_data_header, box[i].type,
180 opj_stream_write_data(cio, l_data_header, 4, p_manager);
184 len = (OPJ_UINT32)(opj_stream_tell(cio) - lenp);
185 opj_stream_seek(cio, lenp, p_manager);
186 opj_write_bytes(l_data_header, len, 4);/* L */
187 opj_stream_write_data(cio, l_data_header, 4, p_manager);
188 opj_stream_seek(cio, lenp + len, p_manager);
192 int opj_write_mainmhix(int coff, opj_codestream_info_t cstr_info,
193 opj_stream_private_t *cio,
194 opj_event_mgr_t * p_manager)
196 OPJ_BYTE l_data_header [8];
201 lenp = opj_stream_tell(cio);
202 opj_stream_skip(cio, 4,
203 p_manager); /* L [at the end] */
204 opj_write_bytes(l_data_header, JPIP_MHIX,
206 opj_stream_write_data(cio, l_data_header, 4, p_manager);
208 opj_write_bytes(l_data_header,
209 (OPJ_UINT32)(cstr_info.main_head_end - cstr_info.main_head_start + 1),
211 opj_stream_write_data(cio, l_data_header, 8, p_manager);
213 for (i = 1; i < (OPJ_UINT32)cstr_info.marknum;
214 i++) { /* Marker restricted to 1 apparition, skip SOC marker */
215 opj_write_bytes(l_data_header, cstr_info.marker[i].type, 2);
216 opj_write_bytes(l_data_header + 2, 0, 2);
217 opj_stream_write_data(cio, l_data_header, 4, p_manager);
218 opj_write_bytes(l_data_header, (OPJ_UINT32)(cstr_info.marker[i].pos - coff), 8);
219 opj_stream_write_data(cio, l_data_header, 8, p_manager);
220 opj_write_bytes(l_data_header, (OPJ_UINT32)cstr_info.marker[i].len, 2);
221 opj_stream_write_data(cio, l_data_header, 2, p_manager);
224 len = (OPJ_UINT32)(opj_stream_tell(cio) - lenp);
225 opj_stream_seek(cio, lenp, p_manager);
226 opj_write_bytes(l_data_header, len, 4); /* L */
227 opj_stream_write_data(cio, l_data_header, 4, p_manager);
228 opj_stream_seek(cio, lenp + len, p_manager);
233 OPJ_BOOL opj_check_EPHuse(int coff, opj_marker_info_t *markers, int marknum,
234 opj_stream_private_t *cio,
235 opj_event_mgr_t * p_manager)
237 OPJ_BYTE l_data_header [4];
238 OPJ_BOOL EPHused = OPJ_FALSE;
243 for (i = 0; i < marknum; i++) {
244 if (markers[i].type == J2K_MS_COD) {
245 org_pos = opj_stream_tell(cio);
246 opj_stream_seek(cio, coff + markers[i].pos + 2, p_manager);
248 opj_stream_read_data(cio, l_data_header, 1, p_manager);
249 opj_read_bytes(l_data_header, &Scod, 1);
250 if (((Scod >> 2) & 1)) {
253 opj_stream_seek(cio, org_pos, p_manager);