Conversions from int to unsigned int to ensure correct execution of int_min at line...
[openjpeg.git] / libopenjpeg / jp2.c
1 /*
2 * Copyright (c) 2003-2004, Yannick Verschueren
3 * Copyright (c) 2003-2004,  Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "j2k.h"
33 #include "jp2.h"
34 #include "cio.h"
35
36 #define JPIP_JPIP 0x6a706970
37
38 #define JP2_JP   0x6a502020
39 #define JP2_FTYP 0x66747970
40 #define JP2_JP2H 0x6a703268
41 #define JP2_IHDR 0x69686472
42 #define JP2_COLR 0x636f6c72
43 #define JP2_JP2C 0x6a703263
44 #define JP2_URL  0x75726c20
45 #define JP2_DBTL 0x6474626c
46 #define JP2_BPCC 0x62706363
47 #define JP2_JP2  0x6a703220
48
49 /*
50
51 * Read box headers
52 *
53 */
54
55 int jp2_read_boxhdr(jp2_box_t * box)
56 {
57   box->init_pos = cio_tell();
58   box->length = cio_read(4);
59   box->type = cio_read(4);
60   if (box->length == 1) {
61     if (cio_read(4) != 0) {
62       fprintf(stderr, "Error: Cannot handle box sizes higher than 2^32\n");
63       return 1;
64     };
65     box->length = cio_read(4);
66     if (box->length == 0) 
67       box->length = cio_numbytesleft() + 12;
68   }
69   else if (box->length == 0) {
70     box->length = cio_numbytesleft() + 8;
71   }
72   return 0;
73 }
74
75 /*
76
77 * Initialisation of a Standard JP2 structure
78 */
79
80 int jp2_init_stdjp2(jp2_struct_t * jp2_struct)
81 {
82
83   jp2_struct->comps = (jp2_comps_t *) malloc(jp2_struct->numcomps * sizeof(jp2_comps_t));
84
85   jp2_struct->precedence = 0;   // PRECEDENCE
86   jp2_struct->approx = 0;       // APPROX
87
88   jp2_struct->brand = JP2_JP2;  /* BR         */
89   jp2_struct->minversion = 0;   /* MinV       */
90   jp2_struct->numcl = 1;
91   jp2_struct->cl = (int *) malloc(jp2_struct->numcl * sizeof(int));
92   jp2_struct->cl[0] = JP2_JP2;  /* CL0 : JP2  */
93
94   jp2_struct->C = 7;            // C : Always 7
95   jp2_struct->UnkC = 0;         // UnkC, colorspace specified in colr box
96   jp2_struct->IPR = 0;          // IPR, no intellectual property
97
98   return 0;
99 }
100
101
102 void jp2_write_url(char *Idx_file)
103 {
104   unsigned int i;
105   char str[256];
106   jp2_box_t box;
107
108   sprintf(str, "%s", Idx_file);
109
110
111   box.init_pos = cio_tell();
112   cio_skip(4);
113   cio_write(JP2_URL, 4);        // DBTL
114   cio_write(0, 1);              // VERS
115   cio_write(0, 3);              // FLAG
116
117   for (i = 0; i < strlen(str); i++) {
118     cio_write(str[i], 1);
119   }
120
121   box.length = cio_tell() - box.init_pos;
122   cio_seek(box.init_pos);
123   cio_write(box.length, 4);     /*    L       */
124   cio_seek(box.init_pos + box.length);
125 }
126
127 /*
128 * Read the IHDR box
129 *
130 * Image Header box
131 *
132 */
133 int jp2_read_ihdr(jp2_struct_t * jp2_struct)
134 {
135   jp2_box_t box;
136
137   jp2_read_boxhdr(&box);
138   if (JP2_IHDR != box.type) {
139     fprintf(stderr, "Error: Expected IHDR Marker\n");
140     return 1;
141   }
142
143   jp2_struct->h = cio_read(4);  // HEIGHT
144   jp2_struct->w = cio_read(4);  // WIDTH
145   jp2_struct->numcomps = cio_read(2);   // NC
146
147   jp2_struct->bpc = cio_read(1);        // BPC
148
149   jp2_struct->C = cio_read(1);  // C 
150   jp2_struct->UnkC = cio_read(1);       // UnkC
151   jp2_struct->IPR = cio_read(1);        // IPR
152
153   if (cio_tell() - box.init_pos != box.length) {
154     fprintf(stderr, "Error with IHDR Box\n");
155     return 1;
156   }
157   return 0;
158 }
159
160 void jp2_write_ihdr(jp2_struct_t * jp2_struct)
161 {
162   jp2_box_t box;
163
164   box.init_pos = cio_tell();
165   cio_skip(4);
166   cio_write(JP2_IHDR, 4);       // IHDR
167
168   cio_write(jp2_struct->h, 4);  // HEIGHT
169   cio_write(jp2_struct->w, 4);  // WIDTH
170   cio_write(jp2_struct->numcomps, 2);   // NC
171
172   cio_write(jp2_struct->bpc, 1);        // BPC  
173
174   cio_write(jp2_struct->C, 1);  // C : Always 7
175   cio_write(jp2_struct->UnkC, 1);       // UnkC, colorspace unknow
176   cio_write(jp2_struct->IPR, 1);        // IPR, no intellectual property
177
178   box.length = cio_tell() - box.init_pos;
179   cio_seek(box.init_pos);
180   cio_write(box.length, 4);     /*    L       */
181   cio_seek(box.init_pos + box.length);
182 }
183
184
185 void jp2_write_bpcc(jp2_struct_t * jp2_struct)
186 {
187   unsigned int i;
188   jp2_box_t box;
189
190   box.init_pos = cio_tell();
191   cio_skip(4);
192   cio_write(JP2_BPCC, 4);       // BPCC
193
194   for (i = 0; i < jp2_struct->numcomps; i++)
195     cio_write(jp2_struct->comps[i].bpcc, 1);
196
197   box.length = cio_tell() - box.init_pos;
198   cio_seek(box.init_pos);
199   cio_write(box.length, 4);     /*    L       */
200   cio_seek(box.init_pos + box.length);
201 }
202
203
204 int jp2_read_bpcc(jp2_struct_t * jp2_struct)
205 {
206   unsigned int i;
207   jp2_box_t box;
208
209   jp2_read_boxhdr(&box);
210   if (JP2_BPCC != box.type) {
211     fprintf(stderr, "Error: Expected BPCC Marker\n");
212     return 1;
213   }
214
215   for (i = 0; i < jp2_struct->numcomps; i++)
216     jp2_struct->comps[i].bpcc = cio_read(1);
217
218   if (cio_tell() - box.init_pos != box.length) {
219     fprintf(stderr, "Error with BPCC Box\n");
220     return 1;
221   }
222   return 0;
223 }
224
225 void jp2_write_colr(jp2_struct_t * jp2_struct)
226 {
227   jp2_box_t box;
228
229   box.init_pos = cio_tell();
230   cio_skip(4);
231   cio_write(JP2_COLR, 4);       // COLR
232
233   cio_write(jp2_struct->meth, 1);       // METH
234   cio_write(jp2_struct->precedence, 1); // PRECEDENCE
235   cio_write(jp2_struct->approx, 1);     // APPROX
236
237   if (jp2_struct->meth == 1)
238     cio_write(jp2_struct->enumcs, 4);   // EnumCS
239   else
240     cio_write(0, 1);            // PROFILE (??)
241
242   box.length = cio_tell() - box.init_pos;
243   cio_seek(box.init_pos);
244   cio_write(box.length, 4);     /*    L       */
245   cio_seek(box.init_pos + box.length);
246 }
247
248 int jp2_read_colr(jp2_struct_t * jp2_struct)
249 {
250   jp2_box_t box;
251
252   jp2_read_boxhdr(&box);
253   if (JP2_COLR != box.type) {
254     fprintf(stderr, "Error: Expected COLR Marker\n");
255     return 1;
256   }
257
258   jp2_struct->meth = cio_read(1);       // METH
259   jp2_struct->precedence = cio_read(1); // PRECEDENCE
260   jp2_struct->approx = cio_read(1);     // APPROX
261
262   if (jp2_struct->meth == 1)
263     jp2_struct->enumcs = cio_read(4);   // EnumCS
264   else
265     cio_read(1);                // PROFILE 
266
267   if (cio_tell() - box.init_pos != box.length) {
268     fprintf(stderr, "Error with BPCC Box\n");
269     return 1;
270   }
271   return 0;
272 }
273
274 /*
275 * Write the JP2H box
276 *
277 * JP2 Header box
278 *
279 */
280 void jp2_write_jp2h(jp2_struct_t * jp2_struct)
281 {
282   jp2_box_t box;
283
284   box.init_pos = cio_tell();
285   cio_skip(4);;
286   cio_write(JP2_JP2H, 4);       /* JP2H */
287
288   jp2_write_ihdr(jp2_struct);
289
290   if (jp2_struct->bpc == 255)
291     jp2_write_bpcc(jp2_struct);
292   jp2_write_colr(jp2_struct);
293
294   box.length = cio_tell() - box.init_pos;
295   cio_seek(box.init_pos);
296   cio_write(box.length, 4);     /*    L       */
297   cio_seek(box.init_pos + box.length);
298 }
299
300
301 /*
302 * Read the JP2H box
303 *
304 * JP2 Header box
305 *
306 */
307 int jp2_read_jp2h(jp2_struct_t * jp2_struct)
308 {
309   jp2_box_t box;
310
311   jp2_read_boxhdr(&box);
312   if (JP2_JP2H != box.type) {
313     fprintf(stderr, "Error: Expected JP2H Marker\n");
314     return 1;
315   }
316
317   if (jp2_read_ihdr(jp2_struct))
318     return 1;
319
320   if (jp2_struct->bpc == 255)
321     if (jp2_read_bpcc(jp2_struct))
322       return 1;
323   if (jp2_read_colr(jp2_struct))
324     return 1;
325
326   if (cio_tell() - box.init_pos != box.length) {
327     fprintf(stderr, "Error with JP2H Box\n");
328     return 1;
329   }
330   return 0;
331 }
332
333 /*
334 * Write the FTYP box
335 *
336 * File type box
337 *
338 */
339 void jp2_write_ftyp(jp2_struct_t * jp2_struct)
340 {
341   unsigned int i;
342   jp2_box_t box;
343
344   box.init_pos = cio_tell();
345   cio_skip(4);
346   cio_write(JP2_FTYP, 4);       /* FTYP       */
347
348   cio_write(jp2_struct->brand, 4);      /* BR         */
349   cio_write(jp2_struct->minversion, 4); /* MinV       */
350
351   for (i = 0; i < jp2_struct->numcl; i++)
352     cio_write(jp2_struct->cl[i], 4);    /* CL           */
353
354   box.length = cio_tell() - box.init_pos;
355   cio_seek(box.init_pos);
356   cio_write(box.length, 4);     /*    L       */
357   cio_seek(box.init_pos + box.length);
358 }
359
360 /*
361 * Read the FTYP box
362 *
363 * File type box
364 *
365 */
366 int jp2_read_ftyp(jp2_struct_t * jp2_struct)
367 {
368   int i;
369   jp2_box_t box;
370
371   jp2_read_boxhdr(&box);
372
373   if (JP2_FTYP != box.type) {
374     fprintf(stderr, "Error: Excpected FTYP Marker\n");
375     return 1;
376   }
377
378   jp2_struct->brand = cio_read(4);      /* BR              */
379   jp2_struct->minversion = cio_read(4); /* MinV            */
380   jp2_struct->numcl = (box.length - 16) / 4;
381   jp2_struct->cl =
382     (unsigned int *) malloc(jp2_struct->numcl * sizeof(unsigned int));
383
384   for (i = 0; i < (int)jp2_struct->numcl; i++)
385     jp2_struct->cl[i] = cio_read(4);    /* CLi */
386
387   if (cio_tell() - box.init_pos != box.length) {
388     fprintf(stderr, "Error with FTYP Box\n"); 
389     return 1;
390   }
391   return 0;
392 }
393
394 int jp2_write_jp2c(int j2k_codestream_len, int *j2k_codestream_offset, char *j2k_codestream)
395 {
396   jp2_box_t box;
397
398   box.init_pos = cio_tell();
399   cio_skip(4);
400   cio_write(JP2_JP2C, 4);       // JP2C
401
402   *j2k_codestream_offset = cio_tell();
403   memcpy(cio_getbp(),j2k_codestream, j2k_codestream_len);
404
405   box.length = 8 + j2k_codestream_len;
406   cio_seek(box.init_pos);
407   cio_write(box.length, 4);     /*    L       */
408   cio_seek(box.init_pos + box.length);
409
410   return box.length;
411 }
412
413
414 int jp2_read_jp2c(unsigned int *j2k_codestream_len, unsigned int *j2k_codestream_offset)
415 {
416   jp2_box_t box;
417
418   jp2_read_boxhdr(&box);
419   if (JP2_JP2C != box.type) {
420     fprintf(stderr, "Error: Expected JP2C Marker\n");
421     return 1;
422   }
423
424   *j2k_codestream_offset = cio_tell();
425   *j2k_codestream_len = box.length - 8;
426
427   return 0;
428 }
429
430 void jp2_write_jp()
431 {
432   jp2_box_t box;
433
434   box.init_pos = cio_tell();
435   cio_skip(4);
436   cio_write(JP2_JP, 4);         // JP
437   cio_write(0x0d0a870a, 4);
438
439   box.length = cio_tell() - box.init_pos;
440   cio_seek(box.init_pos);
441   cio_write(box.length, 4);     /*    L       */
442   cio_seek(box.init_pos + box.length);
443 }
444
445 /*
446 * Read the JP box
447 *
448 * JPEG 2000 signature
449 *
450 * return 1 if error else 0
451 */
452 int jp2_read_jp()
453 {
454   jp2_box_t box;
455
456   jp2_read_boxhdr(&box);
457   if (JP2_JP != box.type) {
458     fprintf(stderr, "Error: Expected JP Marker\n");
459     return 1;
460   }
461   if (0x0d0a870a != cio_read(4)) {
462     fprintf(stderr, "Error with JP Marker\n");
463     return 1;
464   }
465   if (cio_tell() - box.init_pos != box.length) {
466     fprintf(stderr, "Error with JP Box size\n");
467     return 1;
468   }
469   return 0;
470
471 }
472
473
474 int jp2_read_struct(unsigned char *src, jp2_struct_t * jp2_struct, int len)
475 {
476   cio_init(src, len);
477
478   if (jp2_read_jp())
479     return 1;
480   if (jp2_read_ftyp(jp2_struct))
481     return 1;
482   if (jp2_read_jp2h(jp2_struct))
483     return 1;
484   if (jp2_read_jp2c(&jp2_struct->j2k_codestream_len, &jp2_struct->j2k_codestream_offset))
485     return 1;
486   return 0;
487 }
488
489 int jp2_wrap_j2k(jp2_struct_t * jp2_struct, char *j2k_codestream, char *output)
490 {
491   jp2_write_jp();
492   jp2_write_ftyp(jp2_struct);
493   jp2_write_jp2h(jp2_struct);
494
495   jp2_write_jp2c(jp2_struct->j2k_codestream_len, &jp2_struct->j2k_codestream_offset, j2k_codestream);
496
497   return cio_tell();
498 }