31794deed4c06a5b5619857517b5359671245f57
[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   }
69   return 0;
70 }
71
72 /*
73
74 * Initialisation of a Standard JP2 structure
75 */
76
77 int jp2_init_stdjp2(jp2_struct_t * jp2_struct, j2k_image_t * img)
78 {
79   int depth_0, sign, depth, i;
80
81   jp2_struct->h = img->y1 - img->y0;    // HEIGHT
82   jp2_struct->w = img->x1 - img->x0;    // WIDTH
83   jp2_struct->numcomps = img->numcomps; // NC
84   jp2_struct->comps = (jp2_comps_t *) malloc(jp2_struct->numcomps * sizeof(jp2_comps_t));
85
86   depth_0 = img->comps[0].prec - 1;
87   sign = img->comps[0].sgnd;
88   jp2_struct->bpc = depth_0 + (sign << 7);
89
90   for (i = 1; i < img->numcomps; i++) {
91     depth = img->comps[i].prec - 1;
92     sign = img->comps[i].sgnd;
93     if (depth_0 != depth)
94       jp2_struct->bpc = 255;
95   }
96
97
98
99   jp2_struct->C = 7;            // C : Always 7
100   jp2_struct->UnkC = 0;         // UnkC, colorspace specified in colr box
101   jp2_struct->IPR = 0;          // IPR, no intellectual property
102
103   for (i = 0; i < img->numcomps; i++)
104     jp2_struct->comps[i].bpcc =
105       img->comps[i].prec - 1 + (img->comps[i].sgnd << 7);
106
107   jp2_struct->precedence = 0;   // PRECEDENCE
108   jp2_struct->approx = 0;       // APPROX
109
110   if ((img->numcomps == 1 || img->numcomps == 3)
111       && (jp2_struct->bpc != 255))
112     jp2_struct->meth = 1;
113   else
114     jp2_struct->meth = 2;
115
116   if (jp2_struct->meth == 1) {
117     if (img->color_space == 1)
118       jp2_struct->enumcs = 16;
119     else if (img->color_space == 2)
120       jp2_struct->enumcs = 17;
121     else if (img->color_space == 3)
122       jp2_struct->enumcs = 18;  // YUV                          
123   } else
124     jp2_struct->enumcs = 0;     // PROFILE (??)
125
126   jp2_struct->brand = JP2_JP2;  /* BR         */
127   jp2_struct->minversion = 0;   /* MinV       */
128   jp2_struct->numcl = 1;
129   jp2_struct->cl = (int *) malloc(jp2_struct->numcl * sizeof(int));
130   jp2_struct->cl[0] = JP2_JP2;  /* CL0 : JP2  */
131   return 0;
132 }
133
134
135 void jp2_write_url(char *Idx_file)
136 {
137   unsigned int i;
138   char str[256];
139   jp2_box_t box;
140
141   sprintf(str, "%s", Idx_file);
142
143
144   box.init_pos = cio_tell();
145   cio_skip(4);
146   cio_write(JP2_URL, 4);        // DBTL
147   cio_write(0, 1);              // VERS
148   cio_write(0, 3);              // FLAG
149
150   for (i = 0; i < strlen(str); i++) {
151     cio_write(str[i], 1);
152   }
153
154   box.length = cio_tell() - box.init_pos;
155   cio_seek(box.init_pos);
156   cio_write(box.length, 4);     /*    L       */
157   cio_seek(box.init_pos + box.length);
158 }
159
160 /*
161 * Read the IHDR box
162 *
163 * Image Header box
164 *
165 */
166 int jp2_read_ihdr(jp2_struct_t * jp2_struct)
167 {
168   jp2_box_t box;
169
170   jp2_read_boxhdr(&box);
171   if (JP2_IHDR != box.type) {
172     fprintf(stderr, "Error: Expected IHDR Marker\n");
173     return 1;
174   }
175
176   jp2_struct->h = cio_read(4);  // HEIGHT
177   jp2_struct->w = cio_read(4);  // WIDTH
178   jp2_struct->numcomps = cio_read(2);   // NC
179
180   jp2_struct->bpc = cio_read(1);        // BPC
181
182   jp2_struct->C = cio_read(1);  // C 
183   jp2_struct->UnkC = cio_read(1);       // UnkC
184   jp2_struct->IPR = cio_read(1);        // IPR
185
186   if (cio_tell() - box.init_pos != box.length) {
187     fprintf(stderr, "Error with IHDR Box\n");
188     return 1;
189   }
190   return 0;
191 }
192
193 void jp2_write_ihdr(jp2_struct_t * jp2_struct)
194 {
195   jp2_box_t box;
196
197   box.init_pos = cio_tell();
198   cio_skip(4);
199   cio_write(JP2_IHDR, 4);       // IHDR
200
201   cio_write(jp2_struct->h, 4);  // HEIGHT
202   cio_write(jp2_struct->w, 4);  // WIDTH
203   cio_write(jp2_struct->numcomps, 2);   // NC
204
205   cio_write(jp2_struct->bpc, 1);        // BPC  
206
207   cio_write(jp2_struct->C, 1);  // C : Always 7
208   cio_write(jp2_struct->UnkC, 1);       // UnkC, colorspace unknow
209   cio_write(jp2_struct->IPR, 1);        // IPR, no intellectual property
210
211   box.length = cio_tell() - box.init_pos;
212   cio_seek(box.init_pos);
213   cio_write(box.length, 4);     /*    L       */
214   cio_seek(box.init_pos + box.length);
215 }
216
217
218 void jp2_write_bpcc(jp2_struct_t * jp2_struct)
219 {
220   unsigned int i;
221   jp2_box_t box;
222
223   box.init_pos = cio_tell();
224   cio_skip(4);
225   cio_write(JP2_BPCC, 4);       // BPCC
226
227   for (i = 0; i < jp2_struct->numcomps; i++)
228     cio_write(jp2_struct->comps[i].bpcc, 1);
229
230   box.length = cio_tell() - box.init_pos;
231   cio_seek(box.init_pos);
232   cio_write(box.length, 4);     /*    L       */
233   cio_seek(box.init_pos + box.length);
234 }
235
236
237 int jp2_read_bpcc(jp2_struct_t * jp2_struct)
238 {
239   unsigned int i;
240   jp2_box_t box;
241
242   jp2_read_boxhdr(&box);
243   if (JP2_BPCC != box.type) {
244     fprintf(stderr, "Error: Expected BPCC Marker\n");
245     return 1;
246   }
247
248   for (i = 0; i < jp2_struct->numcomps; i++)
249     jp2_struct->comps[i].bpcc = cio_read(1);
250
251   if (cio_tell() - box.init_pos != box.length) {
252     fprintf(stderr, "Error with BPCC Box\n");
253     return 1;
254   }
255   return 0;
256 }
257
258 void jp2_write_colr(jp2_struct_t * jp2_struct)
259 {
260   jp2_box_t box;
261
262   box.init_pos = cio_tell();
263   cio_skip(4);
264   cio_write(JP2_COLR, 4);       // COLR
265
266   cio_write(jp2_struct->meth, 1);       // METH
267   cio_write(jp2_struct->precedence, 1); // PRECEDENCE
268   cio_write(jp2_struct->approx, 1);     // APPROX
269
270   if (jp2_struct->meth == 1)
271     cio_write(jp2_struct->enumcs, 4);   // EnumCS
272   else
273     cio_write(0, 1);            // PROFILE (??)
274
275   box.length = cio_tell() - box.init_pos;
276   cio_seek(box.init_pos);
277   cio_write(box.length, 4);     /*    L       */
278   cio_seek(box.init_pos + box.length);
279 }
280
281 int jp2_read_colr(jp2_struct_t * jp2_struct)
282 {
283   jp2_box_t box;
284
285   jp2_read_boxhdr(&box);
286   if (JP2_COLR != box.type) {
287     fprintf(stderr, "Error: Expected COLR Marker\n");
288     return 1;
289   }
290
291   jp2_struct->meth = cio_read(1);       // METH
292   jp2_struct->precedence = cio_read(1); // PRECEDENCE
293   jp2_struct->approx = cio_read(1);     // APPROX
294
295   if (jp2_struct->meth == 1)
296     jp2_struct->enumcs = cio_read(4);   // EnumCS
297   else
298     cio_read(1);                // PROFILE 
299
300   if (cio_tell() - box.init_pos != box.length) {
301     fprintf(stderr, "Error with BPCC Box\n");
302     return 1;
303   }
304   return 0;
305 }
306
307 /*
308 * Write the JP2H box
309 *
310 * JP2 Header box
311 *
312 */
313 void jp2_write_jp2h(jp2_struct_t * jp2_struct)
314 {
315   jp2_box_t box;
316
317   box.init_pos = cio_tell();
318   cio_skip(4);;
319   cio_write(JP2_JP2H, 4);       /* JP2H */
320
321   jp2_write_ihdr(jp2_struct);
322
323   if (jp2_struct->bpc == 255)
324     jp2_write_bpcc(jp2_struct);
325   jp2_write_colr(jp2_struct);
326
327   box.length = cio_tell() - box.init_pos;
328   cio_seek(box.init_pos);
329   cio_write(box.length, 4);     /*    L       */
330   cio_seek(box.init_pos + box.length);
331 }
332
333
334 /*
335 * Read the JP2H box
336 *
337 * JP2 Header box
338 *
339 */
340 int jp2_read_jp2h(jp2_struct_t * jp2_struct)
341 {
342   jp2_box_t box;
343
344   jp2_read_boxhdr(&box);
345   if (JP2_JP2H != box.type) {
346     fprintf(stderr, "Error: Expected JP2H Marker\n");
347     return 1;
348   }
349
350   if (jp2_read_ihdr(jp2_struct))
351     return 1;
352
353   if (jp2_struct->bpc == 255)
354     if (jp2_read_bpcc(jp2_struct))
355       return 1;
356   if (jp2_read_colr(jp2_struct))
357     return 1;
358
359   if (cio_tell() - box.init_pos != box.length) {
360     fprintf(stderr, "Error with JP2H Box\n");
361     return 1;
362   }
363   return 0;
364 }
365
366 /*
367 * Write the FTYP box
368 *
369 * File type box
370 *
371 */
372 void jp2_write_ftyp(jp2_struct_t * jp2_struct)
373 {
374   unsigned int i;
375   jp2_box_t box;
376
377   box.init_pos = cio_tell();
378   cio_skip(4);
379   cio_write(JP2_FTYP, 4);       /* FTYP       */
380
381   cio_write(jp2_struct->brand, 4);      /* BR         */
382   cio_write(jp2_struct->minversion, 4); /* MinV       */
383
384   for (i = 0; i < jp2_struct->numcl; i++)
385     cio_write(jp2_struct->cl[i], 4);    /* CL           */
386
387   box.length = cio_tell() - box.init_pos;
388   cio_seek(box.init_pos);
389   cio_write(box.length, 4);     /*    L       */
390   cio_seek(box.init_pos + box.length);
391 }
392
393 /*
394 * Read the FTYP box
395 *
396 * File type box
397 *
398 */
399 int jp2_read_ftyp(jp2_struct_t * jp2_struct)
400 {
401   int i;
402   jp2_box_t box;
403
404   jp2_read_boxhdr(&box);
405
406   if (JP2_FTYP != box.type) {
407     fprintf(stderr, "Error: Excpected FTYP Marker\n");
408     return 1;
409   }
410
411   jp2_struct->brand = cio_read(4);      /* BR              */
412   jp2_struct->minversion = cio_read(4); /* MinV            */
413   jp2_struct->numcl = (box.length - 16) / 4;
414   jp2_struct->cl =
415     (unsigned int *) malloc(jp2_struct->numcl * sizeof(unsigned int));
416
417   for (i = 0; i < (int)jp2_struct->numcl; i++)
418     jp2_struct->cl[i] = cio_read(4);    /* CLi */
419
420   if (cio_tell() - box.init_pos != box.length) {
421     fprintf(stderr, "Error with FTYP Box\n");
422     return 1;
423   }
424   return 0;
425 }
426
427 int jp2_write_jp2c(j2k_image_t * img, j2k_cp_t * cp, char *jp2_buffer,
428                    char *index)
429 {
430   int len;
431   jp2_box_t box;
432
433   box.init_pos = cio_tell();
434   cio_skip(4);
435   cio_write(JP2_JP2C, 4);       // JP2C
436
437   len = j2k_encode(img, cp, jp2_buffer, cp->tdx * cp->tdy * cp->th * cp->tw * 2, index);
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   return box.length;
444 }
445
446
447 int jp2_read_jp2c(unsigned char *src, int len, jp2_struct_t * jp2_struct,
448                   j2k_cp_t * cp)
449 {
450   jp2_box_t box;
451
452   jp2_read_boxhdr(&box);
453   if (JP2_JP2C != box.type) {
454     fprintf(stderr, "Error: Expected JP2C Marker\n");
455     return 1;
456   }
457
458   src += cio_tell();
459
460   if (j2k_decode(src, len, jp2_struct->image, cp) == 0) {
461     fprintf(stderr, "JP2F box: failed to decode J2K bitstream image!\n");
462     return 1;
463   }
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 int jp2_decode(unsigned char *src, int len, jp2_struct_t * jp2_struct,
512                j2k_cp_t * cp)
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, len, jp2_struct, cp))
523     return 1;
524   return 0;
525 }
526
527 int jp2_encode(jp2_struct_t * jp2_struct, j2k_cp_t * cp, char *output,
528                char *index)
529 {
530   int len;
531
532   jp2_write_jp();
533   jp2_write_ftyp(jp2_struct);
534   jp2_write_jp2h(jp2_struct);
535   len = jp2_write_jp2c(jp2_struct->image, cp, output, index);
536
537   return cio_tell();
538 }