2 * $Id: tpix_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.
33 * \brief Modification of jpip.c from 2KAN indexer
36 #include "opj_includes.h"
38 #define MAX(a,b) ((a)>(b)?(a):(b))
41 * Get number of maximum tile parts per tile
43 * @param[in] cstr_info codestream information
44 * @return number of maximum tile parts per tile
46 int get_num_max_tile_parts(opj_codestream_info_t cstr_info);
50 * Write faix box of tpix
52 * @param[in] coff offset of j2k codestream
53 * @param[in] compno component number
54 * @param[in] cstr_info codestream information
55 * @param[in] j2klen length of j2k codestream
56 * @param[in] cio file output handle
57 * @return length of faix box
60 int opj_write_tpix(int coff,
61 opj_codestream_info_t cstr_info,
62 int j2klen, opj_stream_private_t *cio,
63 opj_event_mgr_t * p_manager)
65 OPJ_BYTE l_data_header [4];
69 lenp = opj_stream_tell(cio);
70 opj_stream_skip(cio, 4, p_manager);
71 opj_write_bytes(l_data_header, JPIP_TPIX, 4); /* TPIX */
72 opj_stream_write_data(cio, l_data_header, 4, p_manager);
74 opj_write_tpixfaix(coff, 0, cstr_info, j2klen, cio, p_manager);
76 len = (OPJ_UINT32)(opj_stream_tell(cio) - lenp);
78 opj_stream_skip(cio, lenp, p_manager);
79 opj_write_bytes(l_data_header, len, 4); /* L */
80 opj_stream_write_data(cio, l_data_header, 4, p_manager);
81 opj_stream_seek(cio, lenp + len, p_manager);
86 int opj_write_tpixfaix(int coff,
88 opj_codestream_info_t cstr_info,
90 opj_stream_private_t *cio,
91 opj_event_mgr_t * p_manager)
97 OPJ_UINT32 num_max_tile_parts;
98 OPJ_UINT32 size_of_coding; /* 4 or 8 */
100 OPJ_BYTE l_data_header [8];
103 num_max_tile_parts = (OPJ_UINT32)get_num_max_tile_parts(cstr_info);
105 if (j2klen > pow(2, 32)) {
107 version = num_max_tile_parts == 1 ? 1 : 3;
110 version = num_max_tile_parts == 1 ? 0 : 2;
113 lenp = opj_stream_tell(cio);
114 opj_stream_skip(cio, 4, p_manager); /* L [at the end] */
115 opj_write_bytes(l_data_header, JPIP_FAIX, 4); /* FAIX */
116 opj_stream_write_data(cio, l_data_header, 4, p_manager);
117 opj_write_bytes(l_data_header, version, 1); /* Version 0 = 4 bytes */
118 opj_stream_write_data(cio, l_data_header, 1, p_manager);
120 opj_write_bytes(l_data_header, num_max_tile_parts,
121 size_of_coding); /* NMAX */
122 opj_stream_write_data(cio, l_data_header, size_of_coding, p_manager);
123 opj_write_bytes(l_data_header, (OPJ_UINT32)(cstr_info.tw * cstr_info.th),
124 size_of_coding); /* M */
125 opj_stream_write_data(cio, l_data_header, size_of_coding, p_manager);
127 for (i = 0; i < (OPJ_UINT32)(cstr_info.tw * cstr_info.th); i++) {
128 for (j = 0; j < (OPJ_UINT32)cstr_info.tile[i].num_tps; j++) {
129 tp = cstr_info.tile[i].tp[j];
131 opj_write_bytes(l_data_header, (OPJ_UINT32)(tp.tp_start_pos - coff),
132 size_of_coding); /* start position */
133 opj_stream_write_data(cio, l_data_header, size_of_coding, p_manager);
134 opj_write_bytes(l_data_header,
135 (OPJ_UINT32)(tp.tp_end_pos - tp.tp_start_pos + 1),
136 size_of_coding); /* length */
137 opj_stream_write_data(cio, l_data_header, size_of_coding, p_manager);
139 if (version & 0x02) {
140 if (cstr_info.tile[i].num_tps == 1 && cstr_info.numdecompos[compno] > 1) {
141 Aux = (OPJ_UINT32)(cstr_info.numdecompos[compno] + 1);
146 opj_write_bytes(l_data_header, Aux, 4);
147 opj_stream_write_data(cio, l_data_header, 4, p_manager);
149 /*cio_write(img.tile[i].tile_parts[j].num_reso_AUX,4);*/ /* Aux_i,j : Auxiliary value */
150 /* fprintf(stderr,"AUX value %d\n",Aux);*/
155 while (j < num_max_tile_parts) {
157 opj_write_bytes(l_data_header, 0,
158 size_of_coding); /* start position */
159 opj_stream_write_data(cio, l_data_header, size_of_coding, p_manager);
160 opj_write_bytes(l_data_header, 0,
161 size_of_coding); /* length */
162 opj_stream_write_data(cio, l_data_header, size_of_coding, p_manager);
164 if (version & 0x02) {
165 opj_write_bytes(l_data_header, 0, 4); /* Aux_i,j : Auxiliary value */
167 opj_stream_write_data(cio, l_data_header, 4, p_manager);
172 len = (OPJ_UINT32)(opj_stream_tell(cio) - lenp);
173 opj_stream_seek(cio, lenp, p_manager);
174 opj_write_bytes(l_data_header, len, 4); /* L */
175 opj_stream_write_data(cio, l_data_header, 4, p_manager);
176 opj_stream_seek(cio, lenp + len, p_manager);
181 int get_num_max_tile_parts(opj_codestream_info_t cstr_info)
183 int num_max_tp = 0, i;
185 for (i = 0; i < cstr_info.tw * cstr_info.th; i++) {
186 num_max_tp = MAX(cstr_info.tile[i].num_tps, num_max_tp);