struct opj_j2k: remove unused fields, and add some documentation
[openjpeg.git] / src / lib / openjp2 / tpix_manager.c
1 /*
2  * $Id: tpix_manager.c 897 2011-08-28 21:43:57Z Kaori.Hagihara@gmail.com $
3  *
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
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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.
30  */
31
32 /*! \file
33  *  \brief Modification of jpip.c from 2KAN indexer
34  */
35
36 #include "opj_includes.h"
37
38 #define MAX(a,b) ((a)>(b)?(a):(b))
39
40 /*
41  * Get number of maximum tile parts per tile
42  *
43  * @param[in] cstr_info codestream information
44  * @return              number of maximum tile parts per tile
45  */
46 int get_num_max_tile_parts(opj_codestream_info_t cstr_info);
47
48
49 /*
50  * Write faix box of tpix
51  *
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
58  */
59
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)
64 {
65     OPJ_BYTE l_data_header [4];
66     OPJ_UINT32 len;
67     OPJ_OFF_T lenp;
68
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);
73
74     opj_write_tpixfaix(coff, 0, cstr_info, j2klen, cio, p_manager);
75
76     len = (OPJ_UINT32)(opj_stream_tell(cio) - lenp);
77
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);
82
83     return (int)len;
84 }
85
86 int opj_write_tpixfaix(int coff,
87                        int compno,
88                        opj_codestream_info_t cstr_info,
89                        int j2klen,
90                        opj_stream_private_t *cio,
91                        opj_event_mgr_t * p_manager)
92 {
93     OPJ_UINT32 len;
94     OPJ_OFF_T lenp;
95     OPJ_UINT32 i, j;
96     OPJ_UINT32 Aux;
97     OPJ_UINT32 num_max_tile_parts;
98     OPJ_UINT32 size_of_coding; /* 4 or 8 */
99     opj_tp_info_t tp;
100     OPJ_BYTE l_data_header [8];
101     OPJ_UINT32 version;
102
103     num_max_tile_parts = (OPJ_UINT32)get_num_max_tile_parts(cstr_info);
104
105     if (j2klen > pow(2, 32)) {
106         size_of_coding =  8;
107         version = num_max_tile_parts == 1 ? 1 : 3;
108     } else {
109         size_of_coding = 4;
110         version = num_max_tile_parts == 1 ? 0 : 2;
111     }
112
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);
119
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);
126
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];
130
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);
138
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);
142                 } else {
143                     Aux = j + 1;
144                 }
145
146                 opj_write_bytes(l_data_header, Aux, 4);
147                 opj_stream_write_data(cio, l_data_header, 4, p_manager);
148
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);*/
151             }
152             /*cio_write(0,4);*/
153         }
154         /* PADDING */
155         while (j < num_max_tile_parts) {
156
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);
163
164             if (version & 0x02) {
165                 opj_write_bytes(l_data_header, 0, 4);    /* Aux_i,j : Auxiliary value */
166             }
167             opj_stream_write_data(cio, l_data_header, 4, p_manager);
168             j++;
169         }
170     }
171
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);
177
178     return (int)len;
179 }
180
181 int get_num_max_tile_parts(opj_codestream_info_t cstr_info)
182 {
183     int num_max_tp = 0, i;
184
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);
187     }
188
189     return num_max_tp;
190 }