[trunk] fix name
[openjpeg.git] / applications / jpip / tools / indexer / ext_libopenjpeg / ext_jp2.c
1 /*
2  * $Id$
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) 2010-2011, Kaori Hagihara
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <stdio.h>
32
33 #include "ext_openjpeg.h"
34 #include "cio.h"
35 #include "cio_ext.h"
36 #include "cidx_manager.h"
37 #include "indexbox_manager.h"
38
39 /* 
40  * Write JP box
41  *    copy from jp2.c
42  *
43  * @param[in] cio file output handler
44  */
45 void jp2_write_jp(opj_cio_t *cio);
46
47 /* 
48  * Write FTYP box - File type box
49  *    copy from jp2.c
50  *
51  * @param[in] jp2 JP2 handle
52  * @param[in] cio file output handle
53  */
54 void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
55
56 /* 
57  * Write file Index (superbox)
58  *
59  * @param[in] offset_jp2c offset of jp2c box
60  * @param[in] length_jp2c length of jp2c box
61  * @param[in] offset_idx  offset of cidx box
62  * @param[in] length_idx  length of cidx box
63  * @param[in] cio         file output handle
64  * @return                length of fidx box
65  */
66 int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
67
68 /* 
69  * Write index Finder box
70  *
71  * @param[in] offset offset of fidx box
72  * @param[in] length length of fidx box
73  * @param[in] cio         file output handle
74  */
75 void write_iptr( int offset, int length, opj_cio_t *cio);
76
77 opj_bool idxjp2_encode( opj_cinfo_t *cinfo, opj_cio_t *cio, unsigned char *j2kstream, int j2klen, opj_codestream_info_t cstr_info, opj_image_t *image)
78 {  
79   opj_jp2_t *jp2;
80   int pos_iptr, pos_cidx, pos_jp2c, end_pos, pos_fidx, len_fidx;
81   int i;
82   
83   jp2 = cinfo->jp2_handle;
84
85   /* JP2 encoding */
86  
87   /* JPEG 2000 Signature box */
88   jp2_write_jp(cio);
89   /* File Type box */
90   jp2_write_ftyp(jp2, cio);
91   /* JP2 Header box */
92   jp2_write_jp2h(jp2, cio);
93   
94   pos_iptr = cio_tell( cio);
95   cio_skip( cio, 24); /* IPTR further ! */
96   
97   pos_jp2c = cio_tell( cio);
98
99   cio_write( cio, j2klen+8, 4); // L NOTICE: modify for the extended box
100   cio_write( cio, JP2_JP2C, 4);  // JP2C
101
102   for( i=0; i<j2klen; i++)
103     cio_write( cio, j2kstream[i], 1);
104
105   pos_cidx = cio_tell( cio);
106
107   write_cidx( pos_jp2c+8, cio, image, cstr_info, j2klen);
108
109   pos_fidx = cio_tell( cio);
110   len_fidx = write_fidx( pos_jp2c, pos_cidx-pos_jp2c, pos_cidx, cio_tell(cio), cio);
111
112   end_pos = cio_tell( cio);
113
114   cio_seek( cio, pos_iptr);
115   write_iptr( pos_fidx, len_fidx, cio);
116   
117   cio_seek( cio, end_pos);
118
119   return OPJ_TRUE;
120 }
121
122 void jp2_write_jp( opj_cio_t *cio)
123 {
124   opj_jp2_box_t box;
125
126   box.init_pos = cio_tell(cio);
127   cio_skip(cio, 4);
128   cio_write(cio, JP2_JP, 4);            /* JP2 signature */
129   cio_write(cio, 0x0d0a870a, 4);
130
131   box.length = cio_tell(cio) - box.init_pos;
132   cio_seek(cio, box.init_pos);
133   cio_write(cio, box.length, 4);        /* L */
134   cio_seek(cio, box.init_pos + box.length);
135 }
136
137 void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio)
138 {
139   unsigned int i;
140   opj_jp2_box_t box;
141
142   box.init_pos = cio_tell(cio);
143   cio_skip(cio, 4);
144   cio_write(cio, JP2_FTYP, 4);          /* FTYP */
145
146   cio_write(cio, jp2->brand, 4);                /* BR */
147   cio_write(cio, jp2->minversion, 4);   /* MinV */
148
149   for (i = 0; i < jp2->numcl; i++) {
150     cio_write(cio, jp2->cl[i], 4);      /* CL */
151   }
152
153   box.length = cio_tell(cio) - box.init_pos;
154   cio_seek(cio, box.init_pos);
155   cio_write(cio, box.length, 4);        /* L */
156   cio_seek(cio, box.init_pos + box.length);
157 }
158
159
160 /* 
161  * Write proxy box
162  *
163  * @param[in] offset_jp2c offset of jp2c box
164  * @param[in] length_jp2c length of jp2c box
165  * @param[in] offset_idx  offset of cidx box
166  * @param[in] length_idx  length of cidx box
167  * @param[in] cio         file output handle
168  */
169 void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
170
171 int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
172 {  
173   int len, lenp;
174   
175   lenp = cio_tell( cio);
176   cio_skip( cio, 4);              /* L [at the end] */
177   cio_write( cio, JPIP_FIDX, 4);  /* IPTR           */
178   
179   write_prxy( offset_jp2c, length_jp2c, offset_idx, offset_jp2c, cio);
180
181   len = cio_tell( cio)-lenp;
182   cio_seek( cio, lenp);
183   cio_write( cio, len, 4);        /* L              */
184   cio_seek( cio, lenp+len);  
185
186   return len;
187 }
188
189 void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
190 {
191   int len, lenp;
192
193   lenp = cio_tell( cio);
194   cio_skip( cio, 4);              /* L [at the end] */
195   cio_write( cio, JPIP_PRXY, 4);  /* IPTR           */
196   
197   cio_ext_write( cio, offset_jp2c, 8); /* OOFF           */
198   cio_write( cio, length_jp2c, 4); /* OBH part 1     */
199   cio_write( cio, JP2_JP2C, 4);        /* OBH part 2     */
200   
201   cio_write( cio, 1,1);           /* NI             */
202
203   cio_ext_write( cio, offset_idx, 8);  /* IOFF           */
204   cio_write( cio, length_idx, 4);  /* IBH part 1     */
205   cio_write( cio, JPIP_CIDX, 4);   /* IBH part 2     */
206
207   len = cio_tell( cio)-lenp;
208   cio_seek( cio, lenp);
209   cio_write( cio, len, 4);        /* L              */
210   cio_seek( cio, lenp+len);
211 }
212
213 void write_iptr( int offset, int length, opj_cio_t *cio)
214 {
215   int len, lenp;
216   
217   lenp = cio_tell( cio);
218   cio_skip( cio, 4);              /* L [at the end] */
219   cio_write( cio, JPIP_IPTR, 4);  /* IPTR           */
220   
221   cio_ext_write( cio, offset, 8);
222   cio_ext_write( cio, length, 8);
223
224   len = cio_tell( cio)-lenp;
225   cio_seek( cio, lenp);
226   cio_write( cio, len, 4);        /* L             */
227   cio_seek( cio, lenp+len);
228 }