cppcheck fix for openjp2 (#740)
[openjpeg.git] / src / lib / openjp2 / cidx_manager.c
1 /*
2  * $Id: cidx_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 #include "opj_includes.h"
33
34
35 /* 
36  * Write CPTR Codestream finder box
37  *
38  * @param[in] coff offset of j2k codestream
39  * @param[in] clen length of j2k codestream
40  * @param[in] cio  file output handle
41  */
42
43 void opj_write_cptr(int coff, int clen, opj_stream_private_t *cio,
44               opj_event_mgr_t * p_manager );
45
46
47
48
49
50 int opj_write_cidx( int offset, opj_stream_private_t *cio, opj_codestream_info_t cstr_info, int j2klen,
51               opj_event_mgr_t * p_manager )
52 {
53   int i;
54   OPJ_OFF_T lenp;
55   OPJ_UINT32 len;
56   opj_jp2_box_t *box;
57   int num_box = 0;
58   OPJ_BOOL  EPHused;
59   OPJ_BYTE l_data_header [4];
60
61   lenp = -1;
62   box = (opj_jp2_box_t *)opj_calloc( 32, sizeof(opj_jp2_box_t));
63   if(box == NULL){
64         return 0;
65   }
66   for (i=0;i<2;i++){
67   
68     if(i)
69       opj_stream_seek(cio,lenp,p_manager);
70
71
72     lenp = opj_stream_tell (cio);
73
74     opj_stream_skip(cio, 4, p_manager); /* L [at the end] */
75
76     opj_write_bytes(l_data_header,JPIP_CIDX,4); /* CIDX */
77     opj_stream_write_data(cio,l_data_header,4,p_manager);
78
79     opj_write_cptr( offset, cstr_info.codestream_size, cio,p_manager);
80
81     opj_write_manf( i, num_box, box, cio,p_manager);
82     
83     num_box = 0;
84     box[num_box].length = (OPJ_UINT32)opj_write_mainmhix( offset, cstr_info, cio,p_manager);
85     box[num_box].type = JPIP_MHIX;
86     num_box++;
87
88     box[num_box].length = (OPJ_UINT32)opj_write_tpix( offset, cstr_info, j2klen, cio,p_manager);
89     box[num_box].type = JPIP_TPIX;
90     num_box++;
91       
92     box[num_box].length = (OPJ_UINT32)opj_write_thix( offset, cstr_info, cio, p_manager);
93     box[num_box].type = JPIP_THIX;
94     num_box++;
95
96     EPHused = opj_check_EPHuse( offset, cstr_info.marker, cstr_info.marknum, cio,p_manager);
97       
98     box[num_box].length = (OPJ_UINT32)opj_write_ppix( offset, cstr_info, EPHused, j2klen, cio,p_manager);
99     box[num_box].type = JPIP_PPIX;
100     num_box++;
101     
102     box[num_box].length = (OPJ_UINT32)opj_write_phix( offset, cstr_info, EPHused, j2klen, cio,p_manager);
103     box[num_box].type = JPIP_PHIX;
104     num_box++;
105       
106     len = (OPJ_UINT32) (opj_stream_tell(cio)-lenp);
107     opj_stream_seek(cio, lenp,p_manager);
108     opj_write_bytes(l_data_header,len,4);/* L  */
109     opj_stream_write_data(cio,l_data_header,4,p_manager);
110     opj_stream_seek(cio, lenp+len,p_manager);
111   }
112
113   opj_free( box);
114   
115   return (int)len;
116 }
117
118
119
120 void opj_write_cptr(int coff, int clen, opj_stream_private_t *cio,
121               opj_event_mgr_t * p_manager )
122 {
123   OPJ_BYTE l_data_header [3*8];
124   OPJ_UINT32 len;
125   OPJ_OFF_T lenp;
126
127
128   lenp = opj_stream_tell(cio);
129   opj_stream_skip( cio, 4, p_manager);               /* L [at the end]     */
130   opj_write_bytes( l_data_header, JPIP_CPTR, 4);   /* T                  */
131   opj_write_bytes( l_data_header+4, 0, 2);           /* DR  A PRECISER !!  */
132   opj_write_bytes( l_data_header+6, 0, 2);           /* CONT               */
133   opj_write_bytes( l_data_header+8, (OPJ_UINT32)coff, 8);    /* COFF A PRECISER !! */
134   opj_write_bytes( l_data_header+16, (OPJ_UINT32)clen, 8);    /* CLEN               */
135   opj_stream_write_data(cio,l_data_header,3*8,p_manager);
136
137   len = (OPJ_UINT32) (opj_stream_tell(cio) - lenp);
138   opj_stream_seek(cio,lenp,p_manager);
139   opj_write_bytes(l_data_header, len, 4);         /* L                  */
140   opj_stream_write_data(cio,l_data_header,4,p_manager);
141   opj_stream_seek(cio, lenp+len,p_manager);
142
143 }
144
145
146
147 void opj_write_manf(int second, 
148                     int v, 
149                     opj_jp2_box_t *box, 
150                     opj_stream_private_t *cio,
151                     opj_event_mgr_t * p_manager )
152 {
153   OPJ_BYTE l_data_header [4];
154   int i;
155   OPJ_UINT32 len;
156   OPJ_OFF_T lenp;
157   
158   lenp = opj_stream_tell(cio);
159   opj_stream_skip( cio, 4, p_manager);             /* L [at the end]     */
160   opj_write_bytes( l_data_header, JPIP_MANF, 4);   /* T                  */
161   opj_stream_write_data(cio,l_data_header,4,p_manager);
162
163   if (second){                          /* Write only during the second pass */
164     for( i=0; i<v; i++){
165       opj_write_bytes( l_data_header, box[i].length, 4); /* Box length                     */
166       opj_stream_write_data(cio,l_data_header,4,p_manager);
167       opj_write_bytes( l_data_header, box[i].type, 4); /* Box type                       */
168       opj_stream_write_data(cio,l_data_header,4,p_manager);
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
179
180 int opj_write_mainmhix( int coff, opj_codestream_info_t cstr_info, opj_stream_private_t *cio,
181               opj_event_mgr_t * p_manager )
182 {
183   OPJ_BYTE l_data_header [8];
184   OPJ_UINT32 i;
185   OPJ_UINT32 len;
186   OPJ_OFF_T lenp;
187   
188   lenp = opj_stream_tell (cio);
189   opj_stream_skip(cio, 4, p_manager);               /* L [at the end]                    */
190   opj_write_bytes(l_data_header,JPIP_MHIX,4);       /* MHIX                              */
191   opj_stream_write_data(cio,l_data_header,4,p_manager);
192
193   opj_write_bytes(l_data_header, (OPJ_UINT32)(cstr_info.main_head_end-cstr_info.main_head_start+1), 8);        /* TLEN                              */
194   opj_stream_write_data(cio,l_data_header,8,p_manager);
195
196   for(i = 1; i < (OPJ_UINT32)cstr_info.marknum; i++){    /* Marker restricted to 1 apparition, skip SOC marker */
197     opj_write_bytes( l_data_header, cstr_info.marker[i].type, 2);
198     opj_write_bytes( l_data_header+2, 0, 2);
199     opj_stream_write_data(cio,l_data_header,4,p_manager);
200     opj_write_bytes( l_data_header,(OPJ_UINT32) (cstr_info.marker[i].pos-coff), 8);
201     opj_stream_write_data(cio,l_data_header,8,p_manager);
202     opj_write_bytes( l_data_header, (OPJ_UINT32)cstr_info.marker[i].len, 2);
203     opj_stream_write_data(cio,l_data_header,2,p_manager);
204   }
205
206   len = (OPJ_UINT32) (opj_stream_tell(cio)-lenp);
207   opj_stream_seek(cio, lenp,p_manager);
208   opj_write_bytes(l_data_header,len,4);/* L  */
209   opj_stream_write_data(cio,l_data_header,4,p_manager);
210   opj_stream_seek(cio, lenp+len,p_manager);
211   
212   return (int)len;
213 }
214
215 OPJ_BOOL opj_check_EPHuse( int coff, opj_marker_info_t *markers, int marknum, opj_stream_private_t *cio,
216               opj_event_mgr_t * p_manager )
217 {
218   OPJ_BYTE l_data_header [4];
219   OPJ_BOOL EPHused = OPJ_FALSE;
220   int i=0;
221   OPJ_OFF_T org_pos;
222   unsigned int Scod;
223
224   for(i = 0; i < marknum; i++)
225     {
226     if( markers[i].type == J2K_MS_COD)
227       {
228       org_pos = opj_stream_tell(cio);
229       opj_stream_seek(cio, coff+markers[i].pos+2,p_manager);
230
231       opj_stream_read_data(cio,l_data_header,1,p_manager);
232       opj_read_bytes(l_data_header,&Scod,1);
233       if( ((Scod >> 2) & 1))
234         EPHused = OPJ_TRUE;
235       opj_stream_seek( cio, org_pos, p_manager);
236
237       break;
238       }
239     }    
240   return EPHused;
241 }