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