ab69212d567a04bc8b4a04af7c7664c3f0c5186d
[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 #include "tcd.h"
36 #include "int.h"
37
38 #define JPIP_JPIP 0x6a706970
39
40 #define JP2_JP   0x6a502020
41 #define JP2_FTYP 0x66747970
42 #define JP2_JP2H 0x6a703268
43 #define JP2_IHDR 0x69686472
44 #define JP2_COLR 0x636f6c72
45 #define JP2_JP2C 0x6a703263
46 #define JP2_URL  0x75726c20
47 #define JP2_DBTL 0x6474626c
48 #define JP2_BPCC 0x62706363
49 #define JP2_JP2  0x6a703220
50
51 /*
52
53 * Read box headers
54 *
55 */
56
57 int jp2_read_boxhdr(jp2_box_t * box)
58 {
59   box->init_pos = cio_tell();
60   box->length = cio_read(4);
61   box->type = cio_read(4);
62   if (box->length == 1) {
63     if (cio_read(4) != 0) {
64       fprintf(stderr, "Error: Cannot handle box sizes higher than 2^32\n");
65       return 1;
66     };
67     box->length = cio_read(4);
68     if (box->length == 0) 
69       box->length = cio_numbytesleft() + 12;
70   }
71   else if (box->length == 0) {
72     box->length = cio_numbytesleft() + 8;
73   }
74   return 0;
75 }
76
77 /*
78
79 * Initialisation of a Standard JP2 structure
80 */
81
82 int jp2_init_stdjp2(jp2_struct_t * jp2_struct, j2k_image_t * img)
83 {
84   int depth_0, sign, depth, i;
85
86   jp2_struct->h = img->y1 - img->y0;    // HEIGHT
87   jp2_struct->w = img->x1 - img->x0;    // WIDTH
88   jp2_struct->numcomps = img->numcomps; // NC
89   jp2_struct->comps = (jp2_comps_t *) malloc(jp2_struct->numcomps * sizeof(jp2_comps_t));
90
91   depth_0 = img->comps[0].prec - 1;
92   sign = img->comps[0].sgnd;
93   jp2_struct->bpc = depth_0 + (sign << 7);
94
95   for (i = 1; i < img->numcomps; i++) {
96     depth = img->comps[i].prec - 1;
97     sign = img->comps[i].sgnd;
98     if (depth_0 != depth)
99       jp2_struct->bpc = 255;
100   }
101
102
103
104   jp2_struct->C = 7;            // C : Always 7
105   jp2_struct->UnkC = 0;         // UnkC, colorspace specified in colr box
106   jp2_struct->IPR = 0;          // IPR, no intellectual property
107
108   for (i = 0; i < img->numcomps; i++)
109     jp2_struct->comps[i].bpcc =
110       img->comps[i].prec - 1 + (img->comps[i].sgnd << 7);
111
112   jp2_struct->precedence = 0;   // PRECEDENCE
113   jp2_struct->approx = 0;       // APPROX
114
115   if ((img->numcomps == 1 || img->numcomps == 3)
116       && (jp2_struct->bpc != 255))
117     jp2_struct->meth = 1;
118   else
119     jp2_struct->meth = 2;
120
121   if (jp2_struct->meth == 1) {
122     if (img->color_space == 1)
123       jp2_struct->enumcs = 16;
124     else if (img->color_space == 2)
125       jp2_struct->enumcs = 17;
126     else if (img->color_space == 3)
127       jp2_struct->enumcs = 18;  // YUV                          
128   } else
129     jp2_struct->enumcs = 0;     // PROFILE (??)
130
131   jp2_struct->brand = JP2_JP2;  /* BR         */
132   jp2_struct->minversion = 0;   /* MinV       */
133   jp2_struct->numcl = 1;
134   jp2_struct->cl = (int *) malloc(jp2_struct->numcl * sizeof(int));
135   jp2_struct->cl[0] = JP2_JP2;  /* CL0 : JP2  */
136   return 0;
137 }
138
139
140 void jp2_write_url(char *Idx_file)
141 {
142   unsigned int i;
143   char str[256];
144   jp2_box_t box;
145
146   sprintf(str, "%s", Idx_file);
147
148
149   box.init_pos = cio_tell();
150   cio_skip(4);
151   cio_write(JP2_URL, 4);        // DBTL
152   cio_write(0, 1);              // VERS
153   cio_write(0, 3);              // FLAG
154
155   for (i = 0; i < strlen(str); i++) {
156     cio_write(str[i], 1);
157   }
158
159   box.length = cio_tell() - box.init_pos;
160   cio_seek(box.init_pos);
161   cio_write(box.length, 4);     /*    L       */
162   cio_seek(box.init_pos + box.length);
163 }
164
165 /*
166 * Read the IHDR box
167 *
168 * Image Header box
169 *
170 */
171 int jp2_read_ihdr(jp2_struct_t * jp2_struct)
172 {
173   jp2_box_t box;
174
175   jp2_read_boxhdr(&box);
176   if (JP2_IHDR != box.type) {
177     fprintf(stderr, "Error: Expected IHDR Marker\n");
178     return 1;
179   }
180
181   jp2_struct->h = cio_read(4);  // HEIGHT
182   jp2_struct->w = cio_read(4);  // WIDTH
183   jp2_struct->numcomps = cio_read(2);   // NC
184
185   jp2_struct->bpc = cio_read(1);        // BPC
186
187   jp2_struct->C = cio_read(1);  // C 
188   jp2_struct->UnkC = cio_read(1);       // UnkC
189   jp2_struct->IPR = cio_read(1);        // IPR
190
191   if (cio_tell() - box.init_pos != box.length) {
192     fprintf(stderr, "Error with IHDR Box\n");
193     return 1;
194   }
195   return 0;
196 }
197
198 void jp2_write_ihdr(jp2_struct_t * jp2_struct)
199 {
200   jp2_box_t box;
201
202   box.init_pos = cio_tell();
203   cio_skip(4);
204   cio_write(JP2_IHDR, 4);       // IHDR
205
206   cio_write(jp2_struct->h, 4);  // HEIGHT
207   cio_write(jp2_struct->w, 4);  // WIDTH
208   cio_write(jp2_struct->numcomps, 2);   // NC
209
210   cio_write(jp2_struct->bpc, 1);        // BPC  
211
212   cio_write(jp2_struct->C, 1);  // C : Always 7
213   cio_write(jp2_struct->UnkC, 1);       // UnkC, colorspace unknow
214   cio_write(jp2_struct->IPR, 1);        // IPR, no intellectual property
215
216   box.length = cio_tell() - box.init_pos;
217   cio_seek(box.init_pos);
218   cio_write(box.length, 4);     /*    L       */
219   cio_seek(box.init_pos + box.length);
220 }
221
222
223 void jp2_write_bpcc(jp2_struct_t * jp2_struct)
224 {
225   unsigned int i;
226   jp2_box_t box;
227
228   box.init_pos = cio_tell();
229   cio_skip(4);
230   cio_write(JP2_BPCC, 4);       // BPCC
231
232   for (i = 0; i < jp2_struct->numcomps; i++)
233     cio_write(jp2_struct->comps[i].bpcc, 1);
234
235   box.length = cio_tell() - box.init_pos;
236   cio_seek(box.init_pos);
237   cio_write(box.length, 4);     /*    L       */
238   cio_seek(box.init_pos + box.length);
239 }
240
241
242 int jp2_read_bpcc(jp2_struct_t * jp2_struct)
243 {
244   unsigned int i;
245   jp2_box_t box;
246
247   jp2_read_boxhdr(&box);
248   if (JP2_BPCC != box.type) {
249     fprintf(stderr, "Error: Expected BPCC Marker\n");
250     return 1;
251   }
252
253   for (i = 0; i < jp2_struct->numcomps; i++)
254     jp2_struct->comps[i].bpcc = cio_read(1);
255
256   if (cio_tell() - box.init_pos != box.length) {
257     fprintf(stderr, "Error with BPCC Box\n");
258     return 1;
259   }
260   return 0;
261 }
262
263 void jp2_write_colr(jp2_struct_t * jp2_struct)
264 {
265   jp2_box_t box;
266
267   box.init_pos = cio_tell();
268   cio_skip(4);
269   cio_write(JP2_COLR, 4);       // COLR
270
271   cio_write(jp2_struct->meth, 1);       // METH
272   cio_write(jp2_struct->precedence, 1); // PRECEDENCE
273   cio_write(jp2_struct->approx, 1);     // APPROX
274
275   if (jp2_struct->meth == 1)
276     cio_write(jp2_struct->enumcs, 4);   // EnumCS
277   else
278     cio_write(0, 1);            // PROFILE (??)
279
280   box.length = cio_tell() - box.init_pos;
281   cio_seek(box.init_pos);
282   cio_write(box.length, 4);     /*    L       */
283   cio_seek(box.init_pos + box.length);
284 }
285
286 int jp2_read_colr(jp2_struct_t * jp2_struct)
287 {
288   jp2_box_t box;
289
290   jp2_read_boxhdr(&box);
291   if (JP2_COLR != box.type) {
292     fprintf(stderr, "Error: Expected COLR Marker\n");
293     return 1;
294   }
295
296   jp2_struct->meth = cio_read(1);       // METH
297   jp2_struct->precedence = cio_read(1); // PRECEDENCE
298   jp2_struct->approx = cio_read(1);     // APPROX
299
300   if (jp2_struct->meth == 1)
301     jp2_struct->enumcs = cio_read(4);   // EnumCS
302   else
303     cio_read(1);                // PROFILE 
304
305   if (cio_tell() - box.init_pos != box.length) {
306     fprintf(stderr, "Error with BPCC Box\n");
307     return 1;
308   }
309   return 0;
310 }
311
312 /*
313 * Write the JP2H box
314 *
315 * JP2 Header box
316 *
317 */
318 void jp2_write_jp2h(jp2_struct_t * jp2_struct)
319 {
320   jp2_box_t box;
321
322   box.init_pos = cio_tell();
323   cio_skip(4);;
324   cio_write(JP2_JP2H, 4);       /* JP2H */
325
326   jp2_write_ihdr(jp2_struct);
327
328   if (jp2_struct->bpc == 255)
329     jp2_write_bpcc(jp2_struct);
330   jp2_write_colr(jp2_struct);
331
332   box.length = cio_tell() - box.init_pos;
333   cio_seek(box.init_pos);
334   cio_write(box.length, 4);     /*    L       */
335   cio_seek(box.init_pos + box.length);
336 }
337
338
339 /*
340 * Read the JP2H box
341 *
342 * JP2 Header box
343 *
344 */
345 int jp2_read_jp2h(jp2_struct_t * jp2_struct)
346 {
347   jp2_box_t box;
348
349   jp2_read_boxhdr(&box);
350   if (JP2_JP2H != box.type) {
351     fprintf(stderr, "Error: Expected JP2H Marker\n");
352     return 1;
353   }
354
355   if (jp2_read_ihdr(jp2_struct))
356     return 1;
357
358   if (jp2_struct->bpc == 255)
359     if (jp2_read_bpcc(jp2_struct))
360       return 1;
361   if (jp2_read_colr(jp2_struct))
362     return 1;
363
364   if (cio_tell() - box.init_pos != box.length) {
365     fprintf(stderr, "Error with JP2H Box\n");
366     return 1;
367   }
368   return 0;
369 }
370
371 /*
372 * Write the FTYP box
373 *
374 * File type box
375 *
376 */
377 void jp2_write_ftyp(jp2_struct_t * jp2_struct)
378 {
379   unsigned int i;
380   jp2_box_t box;
381
382   box.init_pos = cio_tell();
383   cio_skip(4);
384   cio_write(JP2_FTYP, 4);       /* FTYP       */
385
386   cio_write(jp2_struct->brand, 4);      /* BR         */
387   cio_write(jp2_struct->minversion, 4); /* MinV       */
388
389   for (i = 0; i < jp2_struct->numcl; i++)
390     cio_write(jp2_struct->cl[i], 4);    /* CL           */
391
392   box.length = cio_tell() - box.init_pos;
393   cio_seek(box.init_pos);
394   cio_write(box.length, 4);     /*    L       */
395   cio_seek(box.init_pos + box.length);
396 }
397
398 /*
399 * Read the FTYP box
400 *
401 * File type box
402 *
403 */
404 int jp2_read_ftyp(jp2_struct_t * jp2_struct)
405 {
406   int i;
407   jp2_box_t box;
408
409   jp2_read_boxhdr(&box);
410
411   if (JP2_FTYP != box.type) {
412     fprintf(stderr, "Error: Excpected FTYP Marker\n");
413     return 1;
414   }
415
416   jp2_struct->brand = cio_read(4);      /* BR              */
417   jp2_struct->minversion = cio_read(4); /* MinV            */
418   jp2_struct->numcl = (box.length - 16) / 4;
419   jp2_struct->cl =
420     (unsigned int *) malloc(jp2_struct->numcl * sizeof(unsigned int));
421
422   for (i = 0; i < (int)jp2_struct->numcl; i++)
423     jp2_struct->cl[i] = cio_read(4);    /* CLi */
424
425   if (cio_tell() - box.init_pos != box.length) {
426     fprintf(stderr, "Error with FTYP Box\n"); 
427     return 1;
428   }
429   return 0;
430 }
431
432 int jp2_write_jp2c(jp2_struct_t * jp2_struct, char *j2k_codestream)
433 {
434   jp2_box_t box;
435
436   box.init_pos = cio_tell();
437   cio_skip(4);
438   cio_write(JP2_JP2C, 4);       // JP2C
439
440   jp2_struct->j2k_codestream_offset = cio_tell();
441   memcpy(cio_getbp(),j2k_codestream, jp2_struct->j2k_codestream_len);
442
443   box.length = 8 + jp2_struct->j2k_codestream_len;
444   cio_seek(box.init_pos);
445   cio_write(box.length, 4);     /*    L       */
446   cio_seek(box.init_pos + box.length);
447
448   return box.length;
449 }
450
451
452 int jp2_read_jp2c(unsigned char *src, jp2_struct_t * jp2_struct)
453 {
454   jp2_box_t box;
455
456   jp2_read_boxhdr(&box);
457   if (JP2_JP2C != box.type) {
458     fprintf(stderr, "Error: Expected JP2C Marker\n");
459     return 1;
460   }
461
462   jp2_struct->j2k_codestream_offset = cio_tell();
463   jp2_struct->j2k_codestream_len = box.length - 8;
464
465   return 0;
466 }
467
468 void jp2_write_jp()
469 {
470   jp2_box_t box;
471
472   box.init_pos = cio_tell();
473   cio_skip(4);
474   cio_write(JP2_JP, 4);         // JP
475   cio_write(0x0d0a870a, 4);
476
477   box.length = cio_tell() - box.init_pos;
478   cio_seek(box.init_pos);
479   cio_write(box.length, 4);     /*    L       */
480   cio_seek(box.init_pos + box.length);
481 }
482
483 /*
484 * Read the JP box
485 *
486 * JPEG 2000 signature
487 *
488 * return 1 if error else 0
489 */
490 int jp2_read_jp()
491 {
492   jp2_box_t box;
493
494   jp2_read_boxhdr(&box);
495   if (JP2_JP != box.type) {
496     fprintf(stderr, "Error: Expected JP Marker\n");
497     return 1;
498   }
499   if (0x0d0a870a != cio_read(4)) {
500     fprintf(stderr, "Error with JP Marker\n");
501     return 1;
502   }
503   if (cio_tell() - box.init_pos != box.length) {
504     fprintf(stderr, "Error with JP Box size\n");
505     return 1;
506   }
507   return 0;
508
509 }
510
511
512 int jp2_read_struct(unsigned char *src, jp2_struct_t * jp2_struct, int len)
513 {
514   cio_init(src, len);
515
516   if (jp2_read_jp())
517     return 1;
518   if (jp2_read_ftyp(jp2_struct))
519     return 1;
520   if (jp2_read_jp2h(jp2_struct))
521     return 1;
522   if (jp2_read_jp2c(src, jp2_struct))
523     return 1;
524   return 0;
525 }
526
527 int jp2_wrap_j2k(jp2_struct_t * jp2_struct, char *j2k_codestream, char *output)
528 {
529   jp2_write_jp();
530   jp2_write_ftyp(jp2_struct);
531   jp2_write_jp2h(jp2_struct);
532   jp2_write_jp2c(jp2_struct, j2k_codestream);
533
534   return cio_tell();
535 }