4b1c242cfed5e5f69f88c0644b4cab5165be0e24
[openjpeg.git] / libopenjpeg / 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 void write_cptr(int coff, int clen, opj_cio_t *cio);
43
44
45 /* 
46  * Write main header index table (box)
47  *
48  * @param[in] coff offset of j2k codestream
49  * @param[in] cstr_info codestream information
50  * @param[in] cio  file output handle
51  * @return         length of mainmhix box
52  */
53 int write_mainmhix( int coff, opj_codestream_info_t cstr_info, opj_cio_t *cio);
54
55
56 /* 
57  * Check if EPH option is used
58  *
59  * @param[in] coff    offset of j2k codestream
60  * @param[in] markers marker information
61  * @param[in] marknum number of markers
62  * @param[in] cio     file output handle
63  * @return            true if EPH is used
64  */
65 opj_bool check_EPHuse( int coff, opj_marker_info_t *markers, int marknum, opj_cio_t *cio);
66
67
68 int write_cidx( int offset, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t cstr_info, int j2klen)
69 {
70   int len, i, lenp;
71   opj_jp2_box_t *box;
72   int num_box = 0;
73   opj_bool  EPHused;
74
75   box = (opj_jp2_box_t *)opj_calloc( 32, sizeof(opj_jp2_box_t));
76
77   for (i=0;i<2;i++){
78   
79     if(i)
80       cio_seek( cio, lenp);
81
82     lenp = cio_tell( cio);
83
84     cio_skip( cio, 4);              /* L [at the end] */
85     cio_write( cio, JPIP_CIDX, 4);  /* CIDX           */
86     write_cptr( offset, cstr_info.codestream_size, cio);
87
88     write_manf( i, num_box, box, cio);
89     
90     num_box = 0;
91     box[num_box].length = write_mainmhix( offset, cstr_info, cio);
92     box[num_box].type = JPIP_MHIX;
93     num_box++;
94
95     box[num_box].length = write_tpix( offset, cstr_info, j2klen, cio);
96     box[num_box].type = JPIP_TPIX;
97     num_box++;
98       
99     box[num_box].length = write_thix( offset, cstr_info, cio);
100     box[num_box].type = JPIP_THIX;
101     num_box++;
102
103     EPHused = check_EPHuse( offset, cstr_info.marker, cstr_info.marknum, cio);
104       
105     box[num_box].length = write_ppix( offset, cstr_info, EPHused, j2klen, cio);
106     box[num_box].type = JPIP_PPIX;
107     num_box++;
108     
109     box[num_box].length = write_phix( offset, cstr_info, EPHused, j2klen, cio);
110     box[num_box].type = JPIP_PHIX;
111     num_box++;
112       
113     len = cio_tell( cio)-lenp;
114     cio_seek( cio, lenp);
115     cio_write( cio, len, 4);        /* L             */
116     cio_seek( cio, lenp+len);
117   }
118
119   opj_free( box);
120   
121   return len;
122 }
123
124 void write_cptr(int coff, int clen, opj_cio_t *cio)
125 {
126   int len, lenp;
127
128   lenp = cio_tell( cio);
129   cio_skip( cio, 4);               /* L [at the end]     */
130   cio_write( cio, JPIP_CPTR, 4);   /* T                  */
131   cio_write( cio, 0, 2);           /* DR  A PRECISER !!  */
132   cio_write( cio, 0, 2);           /* CONT               */
133   cio_write( cio, coff, 8);    /* COFF A PRECISER !! */
134   cio_write( cio, clen, 8);    /* CLEN               */
135   len = cio_tell( cio) - lenp;
136   cio_seek( cio, lenp);
137   cio_write( cio, len, 4);         /* L                  */
138   cio_seek( cio, lenp+len);
139 }
140
141 void write_manf(int second, int v, opj_jp2_box_t *box, opj_cio_t *cio)
142 {
143   int len, lenp, i;
144   
145   lenp = cio_tell( cio); 
146   cio_skip( cio, 4);                         /* L [at the end]                    */
147   cio_write( cio, JPIP_MANF,4);              /* T                                 */
148
149   if (second){                          /* Write only during the second pass */
150     for( i=0; i<v; i++){
151       cio_write( cio, box[i].length, 4);  /* Box length                     */ 
152       cio_write( cio, box[i].type, 4); /* Box type                       */
153     }
154   }
155
156   len = cio_tell( cio) - lenp;
157   cio_seek( cio, lenp);
158   cio_write( cio, len, 4);                   /* L                                 */
159   cio_seek( cio, lenp+len);
160 }
161
162 int write_mainmhix( int coff, opj_codestream_info_t cstr_info, opj_cio_t *cio)
163 {
164   int i;
165   int len, lenp;
166   
167   lenp = cio_tell( cio);
168   cio_skip( cio, 4);                               /* L [at the end]                    */
169   cio_write( cio, JPIP_MHIX, 4);                   /* MHIX                              */
170
171   cio_write( cio, cstr_info.main_head_end-cstr_info.main_head_start+1, 8);        /* TLEN                              */
172
173   for(i = 1; i < cstr_info.marknum; i++){    /* Marker restricted to 1 apparition, skip SOC marker */
174     cio_write( cio, cstr_info.marker[i].type, 2);
175     cio_write( cio, 0, 2);
176     cio_write( cio, cstr_info.marker[i].pos-coff, 8);
177     cio_write( cio, cstr_info.marker[i].len, 2);
178   }
179
180   len = cio_tell( cio) - lenp;
181   cio_seek( cio, lenp);
182   cio_write( cio, len, 4);        /* L           */
183   cio_seek( cio, lenp+len);
184   
185   return len;
186 }
187
188 opj_bool check_EPHuse( int coff, opj_marker_info_t *markers, int marknum, opj_cio_t *cio)
189 {
190   opj_bool EPHused = OPJ_FALSE;
191   int i=0;
192   int org_pos;
193   unsigned int Scod;
194
195   for(i = 0; i < marknum; i++){
196     if( markers[i].type == J2K_MS_COD){
197       org_pos = cio_tell( cio);
198       cio_seek( cio, coff+markers[i].pos+2);
199       
200       Scod = cio_read( cio, 1);
201       if( ((Scod >> 2) & 1))
202         EPHused = OPJ_TRUE;
203       cio_seek( cio, org_pos);
204
205       break;
206     }
207   }    
208   return EPHused;
209 }