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