[trunk] [trunk] remove old opj_tcp and rename opj_tcp_v2 to opj_tcp
[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-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
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
64   for (i=0;i<2;i++){
65   
66     if(i)
67       opj_stream_seek(cio,lenp,p_manager);
68
69
70     lenp = opj_stream_tell (cio);
71
72     opj_stream_skip(cio, 4, p_manager); /* L [at the end] */
73
74     opj_write_bytes(l_data_header,JPIP_CIDX,4); /* CIDX */
75     opj_stream_write_data(cio,l_data_header,4,p_manager);
76
77     opj_write_cptr( offset, cstr_info.codestream_size, cio,p_manager);
78
79     opj_write_manf( i, num_box, box, cio,p_manager);
80     
81     num_box = 0;
82     box[num_box].length = opj_write_mainmhix( offset, cstr_info, cio,p_manager);
83     box[num_box].type = JPIP_MHIX;
84     num_box++;
85
86     box[num_box].length = opj_write_tpix( offset, cstr_info, j2klen, cio,p_manager);
87     box[num_box].type = JPIP_TPIX;
88     num_box++;
89       
90     box[num_box].length = opj_write_thix( offset, cstr_info, cio, p_manager);
91     box[num_box].type = JPIP_THIX;
92     num_box++;
93
94     EPHused = opj_check_EPHuse( offset, cstr_info.marker, cstr_info.marknum, cio,p_manager);
95       
96     box[num_box].length = opj_write_ppix( offset, cstr_info, EPHused, j2klen, cio,p_manager);
97     box[num_box].type = JPIP_PPIX;
98     num_box++;
99     
100     box[num_box].length = opj_write_phix( offset, cstr_info, EPHused, j2klen, cio,p_manager);
101     box[num_box].type = JPIP_PHIX;
102     num_box++;
103       
104     len = (OPJ_UINT32) (opj_stream_tell(cio)-lenp);
105     opj_stream_seek(cio, lenp,p_manager);
106     opj_write_bytes(l_data_header,len,4);/* L  */
107     opj_stream_write_data(cio,l_data_header,4,p_manager);
108     opj_stream_seek(cio, lenp+len,p_manager);
109   }
110
111   opj_free( box);
112   
113   return len;
114 }
115
116
117
118 void opj_write_cptr(int coff, int clen, opj_stream_private_t *cio,
119               opj_event_mgr_t * p_manager )
120 {
121   OPJ_BYTE l_data_header [3*8];
122   OPJ_UINT32 len;
123   OPJ_OFF_T lenp;
124
125
126   lenp = opj_stream_tell(cio);
127   opj_stream_skip( cio, 4, p_manager);               /* L [at the end]     */
128   opj_write_bytes( l_data_header, JPIP_CPTR, 4);   /* T                  */
129   opj_write_bytes( l_data_header+4, 0, 2);           /* DR  A PRECISER !!  */
130   opj_write_bytes( l_data_header+6, 0, 2);           /* CONT               */
131   opj_write_bytes( l_data_header+8, coff, 8);    /* COFF A PRECISER !! */
132   opj_write_bytes( l_data_header+16, clen, 8);    /* CLEN               */
133   opj_stream_write_data(cio,l_data_header,3*8,p_manager);
134
135   len = (OPJ_UINT32) (opj_stream_tell(cio) - lenp);
136   opj_stream_seek(cio,lenp,p_manager);
137   opj_write_bytes(l_data_header, len, 4);         /* L                  */
138   opj_stream_write_data(cio,l_data_header,4,p_manager);
139   opj_stream_seek(cio, lenp+len,p_manager);
140
141 }
142
143
144
145 void opj_write_manf(int second, 
146                     int v, 
147                     opj_jp2_box_t *box, 
148                     opj_stream_private_t *cio,
149                     opj_event_mgr_t * p_manager )
150 {
151   OPJ_BYTE l_data_header [4];
152   int i;
153   OPJ_UINT32 len;
154   OPJ_OFF_T lenp;
155   
156   lenp = opj_stream_tell(cio);
157   opj_stream_skip( cio, 4, p_manager);             /* L [at the end]     */
158   opj_write_bytes( l_data_header, JPIP_MANF, 4);   /* T                  */
159   opj_stream_write_data(cio,l_data_header,4,p_manager);
160
161   if (second){                          /* Write only during the second pass */
162     for( i=0; i<v; i++){
163       opj_write_bytes( l_data_header, box[i].length, 4); /* Box length                     */
164       opj_stream_write_data(cio,l_data_header,4,p_manager);
165       opj_write_bytes( l_data_header, box[i].type, 4); /* Box type                       */
166       opj_stream_write_data(cio,l_data_header,4,p_manager);
167     }
168   }
169
170   len = (OPJ_UINT32) (opj_stream_tell(cio) - lenp);
171   opj_stream_seek(cio,lenp,p_manager);
172   opj_write_bytes(l_data_header, len, 4);/* L                                 */
173   opj_stream_write_data(cio,l_data_header,4,p_manager);
174   opj_stream_seek(cio,lenp+len,p_manager);
175 }
176
177
178 int opj_write_mainmhix( int coff, opj_codestream_info_t cstr_info, opj_stream_private_t *cio,
179               opj_event_mgr_t * p_manager )
180 {
181   OPJ_BYTE l_data_header [8];
182   int i;
183   OPJ_UINT32 len;
184   OPJ_OFF_T lenp;
185   
186   lenp = opj_stream_tell (cio);
187   opj_stream_skip(cio, 4, p_manager);               /* L [at the end]                    */
188   opj_write_bytes(l_data_header,JPIP_MHIX,4);       /* MHIX                              */
189   opj_stream_write_data(cio,l_data_header,4,p_manager);
190
191   opj_write_bytes(l_data_header, cstr_info.main_head_end-cstr_info.main_head_start+1, 8);        /* TLEN                              */
192   opj_stream_write_data(cio,l_data_header,8,p_manager);
193
194   for(i = 1; i < cstr_info.marknum; i++){    /* Marker restricted to 1 apparition, skip SOC marker */
195     opj_write_bytes( l_data_header, cstr_info.marker[i].type, 2);
196     opj_write_bytes( l_data_header+2, 0, 2);
197     opj_stream_write_data(cio,l_data_header,4,p_manager);
198     opj_write_bytes( l_data_header,(OPJ_UINT32) (cstr_info.marker[i].pos-coff), 8);
199     opj_stream_write_data(cio,l_data_header,8,p_manager);
200     opj_write_bytes( l_data_header, cstr_info.marker[i].len, 2);
201     opj_stream_write_data(cio,l_data_header,2,p_manager);
202   }
203
204   len = (OPJ_UINT32) (opj_stream_tell(cio)-lenp);
205   opj_stream_seek(cio, lenp,p_manager);
206   opj_write_bytes(l_data_header,len,4);/* L  */
207   opj_stream_write_data(cio,l_data_header,4,p_manager);
208   opj_stream_seek(cio, lenp+len,p_manager);
209   
210   return len;
211 }
212
213 opj_bool opj_check_EPHuse( int coff, opj_marker_info_t *markers, int marknum, opj_stream_private_t *cio,
214               opj_event_mgr_t * p_manager )
215 {
216   OPJ_BYTE l_data_header [4];
217   opj_bool EPHused = OPJ_FALSE;
218   int i=0;
219   OPJ_OFF_T org_pos;
220   unsigned int Scod;
221
222   for(i = 0; i < marknum; i++)
223     {
224     if( markers[i].type == J2K_MS_COD)
225       {
226       org_pos = opj_stream_tell(cio);
227       opj_stream_seek(cio, coff+markers[i].pos+2,p_manager);
228
229       opj_stream_read_data(cio,l_data_header,1,p_manager);
230       opj_read_bytes(l_data_header,&Scod,1);
231       if( ((Scod >> 2) & 1))
232         EPHused = OPJ_TRUE;
233       opj_stream_seek( cio, org_pos, p_manager);
234
235       break;
236       }
237     }    
238   return EPHused;
239 }