ad55464b1d79397baf64fefe790b0ecdcc72b6b6
[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   } else if (box->length == 0) {
69     box->length = cio_numbytesleft() + 8;
70   }
71   return 0;
72 }
73
74 /*
75
76 * Initialisation of a Standard JP2 structure
77 */
78
79 int jp2_init_stdjp2(jp2_struct_t * jp2_struct)
80 {
81
82   jp2_struct->comps =
83     (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   int skip_len;
252
253   jp2_read_boxhdr(&box);
254   do {
255     if (JP2_COLR != box.type) {
256       cio_skip(box.length - 8);
257       jp2_read_boxhdr(&box);
258     }
259   } while (JP2_COLR != box.type);
260
261   jp2_struct->meth = cio_read(1);   /* METH*/
262   jp2_struct->precedence = cio_read(1);   /* PRECEDENCE*/
263   jp2_struct->approx = cio_read(1);   /* APPROX*/
264
265   if (jp2_struct->meth == 1)
266     jp2_struct->enumcs = cio_read(4);   /* EnumCS*/
267   else {
268     /* SKIP PROFILE     */
269     skip_len = box.init_pos + box.length - cio_tell();
270     if (skip_len < 0) {
271       fprintf(stderr, "Error with JP2H box size\n");
272       return 1;
273     }
274     cio_skip(box.init_pos + box.length - cio_tell());
275   }
276
277   if (cio_tell() - box.init_pos != box.length) {
278     fprintf(stderr, "Error with BPCC Box\n");
279     return 1;
280   }
281   return 0;
282 }
283
284 /*
285 * Write the JP2H box
286 *
287 * JP2 Header box
288 *
289 */
290 void jp2_write_jp2h(jp2_struct_t * jp2_struct)
291 {
292   jp2_box_t box;
293
294   box.init_pos = cio_tell();
295   cio_skip(4);;
296   cio_write(JP2_JP2H, 4);       /* JP2H */
297
298   jp2_write_ihdr(jp2_struct);
299
300   if (jp2_struct->bpc == 255)
301     jp2_write_bpcc(jp2_struct);
302   jp2_write_colr(jp2_struct);
303
304   box.length = cio_tell() - box.init_pos;
305   cio_seek(box.init_pos);
306   cio_write(box.length, 4);     /*    L       */
307   cio_seek(box.init_pos + box.length);
308 }
309
310
311 /*
312 * Read the JP2H box
313 *
314 * JP2 Header box
315 *
316 */
317 int jp2_read_jp2h(jp2_struct_t * jp2_struct)
318 {
319   jp2_box_t box;
320   int skip_len;
321
322   jp2_read_boxhdr(&box);
323   do {
324     if (JP2_JP2H != box.type) {
325       if (box.type == JP2_JP2C) {
326         fprintf(stderr, "Error: Expected JP2H Marker\n");
327         return 1;
328       }
329       cio_skip(box.length - 8);
330       jp2_read_boxhdr(&box);
331     }
332   } while (JP2_JP2H != box.type);
333
334   if (jp2_read_ihdr(jp2_struct))
335     return 1;
336
337   if (jp2_struct->bpc == 255) {
338     if (jp2_read_bpcc(jp2_struct))
339       return 1;
340   }
341
342   if (jp2_read_colr(jp2_struct))
343     return 1;
344
345   skip_len = box.init_pos + box.length - cio_tell();
346   if (skip_len < 0) {
347     fprintf(stderr, "Error with JP2H box size\n");
348     return 1;
349   }
350   cio_skip(box.init_pos + box.length - cio_tell());
351
352   return 0;
353 }
354
355 /*
356 * Write the FTYP box
357 *
358 * File type box
359 *
360 */
361 void jp2_write_ftyp(jp2_struct_t * jp2_struct)
362 {
363   unsigned int i;
364   jp2_box_t box;
365
366   box.init_pos = cio_tell();
367   cio_skip(4);
368   cio_write(JP2_FTYP, 4);       /* FTYP       */
369
370   cio_write(jp2_struct->brand, 4);      /* BR         */
371   cio_write(jp2_struct->minversion, 4); /* MinV       */
372
373   for (i = 0; i < jp2_struct->numcl; i++)
374     cio_write(jp2_struct->cl[i], 4);    /* CL           */
375
376   box.length = cio_tell() - box.init_pos;
377   cio_seek(box.init_pos);
378   cio_write(box.length, 4);     /*    L       */
379   cio_seek(box.init_pos + box.length);
380 }
381
382 /*
383 * Read the FTYP box
384 *
385 * File type box
386 *
387 */
388 int jp2_read_ftyp(jp2_struct_t * jp2_struct)
389 {
390   int i;
391   jp2_box_t box;
392
393   jp2_read_boxhdr(&box);
394
395   if (JP2_FTYP != box.type) {
396     fprintf(stderr, "Error: Excpected FTYP Marker\n");
397     return 1;
398   }
399
400   jp2_struct->brand = cio_read(4);      /* BR              */
401   jp2_struct->minversion = cio_read(4); /* MinV            */
402   jp2_struct->numcl = (box.length - 16) / 4;
403   jp2_struct->cl =
404     (unsigned int *) malloc(jp2_struct->numcl * sizeof(unsigned int));
405
406   for (i = 0; i < (int) jp2_struct->numcl; i++)
407     jp2_struct->cl[i] = cio_read(4);    /* CLi */
408
409   if (cio_tell() - box.init_pos != box.length) {
410     fprintf(stderr, "Error with FTYP Box\n");
411     return 1;
412   }
413   return 0;
414 }
415
416 int jp2_write_jp2c(int j2k_codestream_len, int *j2k_codestream_offset,
417                    char *j2k_codestream)
418 {
419   jp2_box_t box;
420
421   box.init_pos = cio_tell();
422   cio_skip(4);
423   cio_write(JP2_JP2C, 4);   /* JP2C*/
424
425   *j2k_codestream_offset = cio_tell();
426   memcpy(cio_getbp(), j2k_codestream, j2k_codestream_len);
427
428   box.length = 8 + j2k_codestream_len;
429   cio_seek(box.init_pos);
430   cio_write(box.length, 4);     /*    L       */
431   cio_seek(box.init_pos + box.length);
432
433   return box.length;
434 }
435
436
437 int jp2_read_jp2c(unsigned int *j2k_codestream_len,
438                   unsigned int *j2k_codestream_offset)
439 {
440   jp2_box_t box;
441
442   jp2_read_boxhdr(&box);
443   do {
444     if (JP2_JP2C != box.type) {
445       cio_skip(box.length - 8);
446       jp2_read_boxhdr(&box);
447     }
448   } while (JP2_JP2C != box.type);
449
450   *j2k_codestream_offset = cio_tell();
451   *j2k_codestream_len = box.length - 8;
452
453   return 0;
454 }
455
456 void jp2_write_jp()
457 {
458   jp2_box_t box;
459
460   box.init_pos = cio_tell();
461   cio_skip(4);
462   cio_write(JP2_JP, 4);      /* JP*/
463   cio_write(0x0d0a870a, 4);
464
465   box.length = cio_tell() - box.init_pos;
466   cio_seek(box.init_pos);
467   cio_write(box.length, 4);     /*    L       */
468   cio_seek(box.init_pos + box.length);
469 }
470
471 /*
472 * Read the JP box
473 *
474 * JPEG 2000 signature
475 *
476 * return 1 if error else 0
477 */
478 int jp2_read_jp()
479 {
480   jp2_box_t box;
481
482   jp2_read_boxhdr(&box);
483   if (JP2_JP != box.type) {
484     fprintf(stderr, "Error: Expected JP Marker\n");
485     return 1;
486   }
487   if (0x0d0a870a != cio_read(4)) {
488     fprintf(stderr, "Error with JP Marker\n");
489     return 1;
490   }
491   if (cio_tell() - box.init_pos != box.length) {
492     fprintf(stderr, "Error with JP Box size\n");
493     return 1;
494   }
495   return 0;
496
497 }
498
499
500 int jp2_read_struct(unsigned char *src, jp2_struct_t * jp2_struct, int len)
501 {
502   cio_init(src, len);
503
504   if (jp2_read_jp())
505     return 1;
506   if (jp2_read_ftyp(jp2_struct))
507     return 1;
508   if (jp2_read_jp2h(jp2_struct))
509     return 1;
510   if (jp2_read_jp2c
511       (&jp2_struct->j2k_codestream_len,
512        &jp2_struct->j2k_codestream_offset))
513     return 1;
514   return 0;
515 }
516
517 int jp2_wrap_j2k(jp2_struct_t * jp2_struct, char *j2k_codestream,
518                  char *output)
519 {
520   (void)output;
521   jp2_write_jp();
522   jp2_write_ftyp(jp2_struct);
523   jp2_write_jp2h(jp2_struct);
524
525   jp2_write_jp2c(jp2_struct->j2k_codestream_len,
526                  &jp2_struct->j2k_codestream_offset, j2k_codestream);
527
528   return cio_tell();
529 }