Enabled compression of TIF image format to j2k by tifftoimage() and decompression...
[openjpeg.git] / libopenjpeg / jp2.c
1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "opj_includes.h"
33
34 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
35 /*@{*/
36
37 /** @name Local static functions */
38 /*@{*/
39
40 /**
41 Read box headers
42 @param cinfo Codec context info
43 @param cio Input stream
44 @param box
45 @return Returns true if successful, returns false otherwise
46 */
47 static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box);
48 /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
49 /**
50 Read the IHDR box - Image Header box
51 @param jp2 JP2 handle
52 @param cio Input buffer stream
53 @return Returns true if successful, returns false otherwise
54 */
55 static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
56 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
57 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
58 static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
59 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio);
60 static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio);
61 /**
62 Write the JP2H box - JP2 Header box
63 @param jp2 JP2 handle
64 @param cio Output buffer stream
65 */
66 static void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio);
67 /**
68 Read the JP2H box - JP2 Header box
69 @param jp2 JP2 handle
70 @param cio Input buffer stream
71 @return Returns true if successful, returns false otherwise
72 */
73 static bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio);
74 /**
75 Write the FTYP box - File type box
76 @param jp2 JP2 handle
77 @param cio Output buffer stream
78 */
79 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
80 /**
81 Read the FTYP box - File type box
82 @param jp2 JP2 handle
83 @param cio Input buffer stream
84 @return Returns true if successful, returns false otherwise
85 */
86 static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
87 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, char *index);
88 static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset);
89 static void jp2_write_jp(opj_cio_t *cio);
90 /**
91 Read the JP box - JPEG 2000 signature
92 @param jp2 JP2 handle
93 @param cio Input buffer stream
94 @return Returns true if successful, returns false otherwise
95 */
96 static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio);
97 /**
98 Decode the structure of a JP2 file
99 @param jp2 JP2 handle
100 @param cio Input buffer stream
101 @return Returns true if successful, returns false otherwise
102 */
103 static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio);
104
105 /*@}*/
106
107 /*@}*/
108
109 /* ----------------------------------------------------------------------- */
110
111 static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
112         box->init_pos = cio_tell(cio);
113         box->length = cio_read(cio, 4);
114         box->type = cio_read(cio, 4);
115         if (box->length == 1) {
116                 if (cio_read(cio, 4) != 0) {
117                         opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
118                         return false;
119                 }
120                 box->length = cio_read(cio, 4);
121                 if (box->length == 0) 
122                         box->length = cio_numbytesleft(cio) + 12;
123         }
124         else if (box->length == 0) {
125                 box->length = cio_numbytesleft(cio) + 8;
126         }
127         
128         return true;
129 }
130
131 #if 0
132 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
133         unsigned int i;
134         opj_jp2_box_t box;
135
136         box.init_pos = cio_tell(cio);
137         cio_skip(cio, 4);
138         cio_write(cio, JP2_URL, 4);     /* DBTL */
139         cio_write(cio, 0, 1);           /* VERS */
140         cio_write(cio, 0, 3);           /* FLAG */
141
142         if(Idx_file) {
143                 for (i = 0; i < strlen(Idx_file); i++) {
144                         cio_write(cio, Idx_file[i], 1);
145                 }
146         }
147
148         box.length = cio_tell(cio) - box.init_pos;
149         cio_seek(cio, box.init_pos);
150         cio_write(cio, box.length, 4);  /* L */
151         cio_seek(cio, box.init_pos + box.length);
152 }
153 #endif
154
155 static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
156         opj_jp2_box_t box;
157
158         opj_common_ptr cinfo = jp2->cinfo;
159
160         jp2_read_boxhdr(cinfo, cio, &box);
161         if (JP2_IHDR != box.type) {
162                 opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
163                 return false;
164         }
165
166         jp2->h = cio_read(cio, 4);                      /* HEIGHT */
167         jp2->w = cio_read(cio, 4);                      /* WIDTH */
168         jp2->numcomps = cio_read(cio, 2);       /* NC */
169         jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
170
171         jp2->bpc = cio_read(cio, 1);            /* BPC */
172
173         jp2->C = cio_read(cio, 1);                      /* C */
174         jp2->UnkC = cio_read(cio, 1);           /* UnkC */
175         jp2->IPR = cio_read(cio, 1);            /* IPR */
176
177         if (cio_tell(cio) - box.init_pos != box.length) {
178                 opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
179                 return false;
180         }
181
182         return true;
183 }
184
185 static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
186         opj_jp2_box_t box;
187
188         box.init_pos = cio_tell(cio);
189         cio_skip(cio, 4);
190         cio_write(cio, JP2_IHDR, 4);            /* IHDR */
191
192         cio_write(cio, jp2->h, 4);                      /* HEIGHT */
193         cio_write(cio, jp2->w, 4);                      /* WIDTH */
194         cio_write(cio, jp2->numcomps, 2);       /* NC */
195
196         cio_write(cio, jp2->bpc, 1);            /* BPC */
197
198         cio_write(cio, jp2->C, 1);                      /* C : Always 7 */
199         cio_write(cio, jp2->UnkC, 1);           /* UnkC, colorspace unknown */
200         cio_write(cio, jp2->IPR, 1);            /* IPR, no intellectual property */
201
202         box.length = cio_tell(cio) - box.init_pos;
203         cio_seek(cio, box.init_pos);
204         cio_write(cio, box.length, 4);  /* L */
205         cio_seek(cio, box.init_pos + box.length);
206 }
207
208 static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
209         unsigned int i;
210         opj_jp2_box_t box;
211
212         box.init_pos = cio_tell(cio);
213         cio_skip(cio, 4);
214         cio_write(cio, JP2_BPCC, 4);    /* BPCC */
215
216         for (i = 0; i < jp2->numcomps; i++) {
217                 cio_write(cio, jp2->comps[i].bpcc, 1);
218         }
219
220         box.length = cio_tell(cio) - box.init_pos;
221         cio_seek(cio, box.init_pos);
222         cio_write(cio, box.length, 4);  /* L */
223         cio_seek(cio, box.init_pos + box.length);
224 }
225
226
227 static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
228         unsigned int i;
229         opj_jp2_box_t box;
230
231         opj_common_ptr cinfo = jp2->cinfo;
232
233         jp2_read_boxhdr(cinfo, cio, &box);
234         if (JP2_BPCC != box.type) {
235                 opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
236                 return false;
237         }
238
239         for (i = 0; i < jp2->numcomps; i++) {
240                 jp2->comps[i].bpcc = cio_read(cio, 1);
241         }
242
243         if (cio_tell(cio) - box.init_pos != box.length) {
244                 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
245                 return false;
246         }
247
248         return true;
249 }
250
251 static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
252         opj_jp2_box_t box;
253
254         box.init_pos = cio_tell(cio);
255         cio_skip(cio, 4);
256         cio_write(cio, JP2_COLR, 4);            /* COLR */
257
258         cio_write(cio, jp2->meth, 1);           /* METH */
259         cio_write(cio, jp2->precedence, 1);     /* PRECEDENCE */
260         cio_write(cio, jp2->approx, 1);         /* APPROX */
261
262         if (jp2->meth == 1) {
263                 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
264         } else {
265                 cio_write(cio, 0, 1);                   /* PROFILE (??) */
266         }
267
268         box.length = cio_tell(cio) - box.init_pos;
269         cio_seek(cio, box.init_pos);
270         cio_write(cio, box.length, 4);  /* L */
271         cio_seek(cio, box.init_pos + box.length);
272 }
273
274 static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
275         opj_jp2_box_t box;
276         int skip_len;
277
278         opj_common_ptr cinfo = jp2->cinfo;
279
280         jp2_read_boxhdr(cinfo, cio, &box);
281         do {
282                 if (JP2_COLR != box.type) {
283                         cio_skip(cio, box.length - 8);
284                         jp2_read_boxhdr(cinfo, cio, &box);
285                 }
286         } while(JP2_COLR != box.type);
287
288         jp2->meth = cio_read(cio, 1);           /* METH */
289         jp2->precedence = cio_read(cio, 1);     /* PRECEDENCE */
290         jp2->approx = cio_read(cio, 1);         /* APPROX */
291
292         if (jp2->meth == 1) {
293                 jp2->enumcs = cio_read(cio, 4); /* EnumCS */
294         } else {
295                 /* skip PROFILE */
296                 skip_len = box.init_pos + box.length - cio_tell(cio);
297                 if (skip_len < 0) {
298                         opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H box size\n");
299                         return false;
300                 }
301                 cio_skip(cio, box.init_pos + box.length - cio_tell(cio));
302         }
303
304         if (cio_tell(cio) - box.init_pos != box.length) {
305                 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
306                 return false;
307         }
308         return true;
309 }
310
311 static void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
312         opj_jp2_box_t box;
313
314         box.init_pos = cio_tell(cio);
315         cio_skip(cio, 4);
316         cio_write(cio, JP2_JP2H, 4);    /* JP2H */
317
318         jp2_write_ihdr(jp2, cio);
319
320         if (jp2->bpc == 255) {
321                 jp2_write_bpcc(jp2, cio);
322         }
323         jp2_write_colr(jp2, cio);
324
325         box.length = cio_tell(cio) - box.init_pos;
326         cio_seek(cio, box.init_pos);
327         cio_write(cio, box.length, 4);  /* L */
328         cio_seek(cio, box.init_pos + box.length);
329 }
330
331 static bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
332         opj_jp2_box_t box;
333         int skip_len;
334
335         opj_common_ptr cinfo = jp2->cinfo;
336
337         jp2_read_boxhdr(cinfo, cio, &box);
338         do {
339                 if (JP2_JP2H != box.type) {
340                         if (box.type == JP2_JP2C) {
341                                 opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
342                                 return false;
343                         }
344                         cio_skip(cio, box.length - 8);
345                         jp2_read_boxhdr(cinfo, cio, &box);
346                 }
347         } while(JP2_JP2H != box.type);
348
349         if (!jp2_read_ihdr(jp2, cio))
350                 return false;
351
352         if (jp2->bpc == 255) {
353                 if (!jp2_read_bpcc(jp2, cio))
354                         return false;
355         }
356         if (!jp2_read_colr(jp2, cio))
357                 return false;
358
359         skip_len = box.init_pos + box.length - cio_tell(cio);
360         if (skip_len < 0) {
361                 opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H Box\n");
362                 return false;
363         }
364         cio_skip(cio, box.init_pos + box.length - cio_tell(cio));
365
366         return true;
367 }
368
369 static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
370         unsigned int i;
371         opj_jp2_box_t box;
372
373         box.init_pos = cio_tell(cio);
374         cio_skip(cio, 4);
375         cio_write(cio, JP2_FTYP, 4);            /* FTYP */
376
377         cio_write(cio, jp2->brand, 4);          /* BR */
378         cio_write(cio, jp2->minversion, 4);     /* MinV */
379
380         for (i = 0; i < jp2->numcl; i++) {
381                 cio_write(cio, jp2->cl[i], 4);  /* CL */
382         }
383
384         box.length = cio_tell(cio) - box.init_pos;
385         cio_seek(cio, box.init_pos);
386         cio_write(cio, box.length, 4);  /* L */
387         cio_seek(cio, box.init_pos + box.length);
388 }
389
390 static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
391         int i;
392         opj_jp2_box_t box;
393
394         opj_common_ptr cinfo = jp2->cinfo;
395
396         jp2_read_boxhdr(cinfo, cio, &box);
397
398         if (JP2_FTYP != box.type) {
399                 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
400                 return false;
401         }
402
403         jp2->brand = cio_read(cio, 4);          /* BR */
404         jp2->minversion = cio_read(cio, 4);     /* MinV */
405         jp2->numcl = (box.length - 16) / 4;
406         jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
407
408         for (i = 0; i < (int)jp2->numcl; i++) {
409                 jp2->cl[i] = cio_read(cio, 4);  /* CLi */
410         }
411
412         if (cio_tell(cio) - box.init_pos != box.length) {
413                 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
414                 return false;
415         }
416
417         return true;
418 }
419
420 static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, char *index) {
421         unsigned int j2k_codestream_offset, j2k_codestream_length;
422         opj_jp2_box_t box;
423
424         opj_j2k_t *j2k = jp2->j2k;
425
426         box.init_pos = cio_tell(cio);
427         cio_skip(cio, 4);
428         cio_write(cio, JP2_JP2C, 4);    /* JP2C */
429
430         /* J2K encoding */
431         j2k_codestream_offset = cio_tell(cio);
432         if(!j2k_encode(j2k, cio, image, index)) {
433                 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
434                 return 0;
435         }
436         j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
437
438         jp2->j2k_codestream_offset = j2k_codestream_offset;
439         jp2->j2k_codestream_length = j2k_codestream_length;
440
441         box.length = 8 + jp2->j2k_codestream_length;
442         cio_seek(cio, box.init_pos);
443         cio_write(cio, box.length, 4);  /* L */
444         cio_seek(cio, box.init_pos + box.length);
445
446         return box.length;
447 }
448
449 static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
450         opj_jp2_box_t box;
451
452         opj_common_ptr cinfo = jp2->cinfo;
453
454         jp2_read_boxhdr(cinfo, cio, &box);
455         do {
456                 if(JP2_JP2C != box.type) {
457                         cio_skip(cio, box.length - 8);
458                         jp2_read_boxhdr(cinfo, cio, &box);
459                 }
460         } while(JP2_JP2C != box.type);
461
462         *j2k_codestream_offset = cio_tell(cio);
463         *j2k_codestream_length = box.length - 8;
464
465         return true;
466 }
467
468 static void jp2_write_jp(opj_cio_t *cio) {
469         opj_jp2_box_t box;
470
471         box.init_pos = cio_tell(cio);
472         cio_skip(cio, 4);
473         cio_write(cio, JP2_JP, 4);              /* JP2 signature */
474         cio_write(cio, 0x0d0a870a, 4);
475
476         box.length = cio_tell(cio) - box.init_pos;
477         cio_seek(cio, box.init_pos);
478         cio_write(cio, box.length, 4);  /* L */
479         cio_seek(cio, box.init_pos + box.length);
480 }
481
482 static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
483         opj_jp2_box_t box;
484
485         opj_common_ptr cinfo = jp2->cinfo;
486
487         jp2_read_boxhdr(cinfo, cio, &box);
488         if (JP2_JP != box.type) {
489                 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
490                 return false;
491         }
492         if (0x0d0a870a != cio_read(cio, 4)) {
493                 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
494                 return false;
495         }
496         if (cio_tell(cio) - box.init_pos != box.length) {
497                 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
498                 return false;
499         }
500
501         return true;
502 }
503
504
505 static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio) {
506         if (!jp2_read_jp(jp2, cio))
507                 return false;
508         if (!jp2_read_ftyp(jp2, cio))
509                 return false;
510         if (!jp2_read_jp2h(jp2, cio))
511                 return false;
512         if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
513                 return false;
514         
515         return true;
516 }
517
518 /* ----------------------------------------------------------------------- */
519 /* JP2 decoder interface                                             */
520 /* ----------------------------------------------------------------------- */
521
522 opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
523         opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
524         if(jp2) {
525                 jp2->cinfo = cinfo;
526                 /* create the J2K codec */
527                 jp2->j2k = j2k_create_decompress(cinfo);
528                 if(jp2->j2k == NULL) {
529                         jp2_destroy_decompress(jp2);
530                         return NULL;
531                 }
532         }
533         return jp2;
534 }
535
536 void jp2_destroy_decompress(opj_jp2_t *jp2) {
537         if(jp2) {
538                 /* destroy the J2K codec */
539                 j2k_destroy_decompress(jp2->j2k);
540
541                 if(jp2->comps) {
542                         opj_free(jp2->comps);
543                 }
544                 if(jp2->cl) {
545                         opj_free(jp2->cl);
546                 }
547                 opj_free(jp2);
548         }
549 }
550
551 void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
552         /* setup the J2K codec */
553         j2k_setup_decoder(jp2->j2k, parameters);
554         /* further JP2 initializations go here */
555 }
556
557 opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio) {
558         opj_common_ptr cinfo;
559         opj_image_t *image = NULL;
560
561         if(!jp2 || !cio) {
562                 return NULL;
563         }
564
565         cinfo = jp2->cinfo;
566
567         /* JP2 decoding */
568         if(!jp2_read_struct(jp2, cio)) {
569                 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
570                 return NULL;
571         }
572
573         /* J2K decoding */
574         image = j2k_decode(jp2->j2k, cio);
575         if(!image) {
576                 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
577         }
578
579         return image;
580 }
581
582 /* ----------------------------------------------------------------------- */
583 /* JP2 encoder interface                                             */
584 /* ----------------------------------------------------------------------- */
585
586 opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
587         opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
588         if(jp2) {
589                 jp2->cinfo = cinfo;
590                 /* create the J2K codec */
591                 jp2->j2k = j2k_create_compress(cinfo);
592                 if(jp2->j2k == NULL) {
593                         jp2_destroy_compress(jp2);
594                         return NULL;
595                 }
596         }
597         return jp2;
598 }
599
600 void jp2_destroy_compress(opj_jp2_t *jp2) {
601         if(jp2) {
602                 /* destroy the J2K codec */
603                 j2k_destroy_compress(jp2->j2k);
604
605                 if(jp2->comps) {
606                         opj_free(jp2->comps);
607                 }
608                 if(jp2->cl) {
609                         opj_free(jp2->cl);
610                 }
611                 opj_free(jp2);
612         }
613 }
614
615 void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
616         int i;
617         int depth_0, sign;
618
619         if(!jp2 || !parameters || !image)
620                 return;
621
622         /* setup the J2K codec */
623         /* ------------------- */
624
625         /* Check if number of components respects standard */
626         if (image->numcomps < 1 || image->numcomps > 16384) {
627                 opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
628                 return;
629         }
630
631         j2k_setup_encoder(jp2->j2k, parameters, image);
632
633         /* setup the JP2 codec */
634         /* ------------------- */
635         
636         /* Profile box */
637
638         jp2->brand = JP2_JP2;   /* BR */
639         jp2->minversion = 0;    /* MinV */
640         jp2->numcl = 1;
641         jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
642         jp2->cl[0] = JP2_JP2;   /* CL0 : JP2 */
643
644         /* Image Header box */
645
646         jp2->numcomps = image->numcomps;        /* NC */
647         jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
648         jp2->h = image->y1 - image->y0;         /* HEIGHT */
649         jp2->w = image->x1 - image->x0;         /* WIDTH */
650         /* BPC */
651         depth_0 = image->comps[0].prec - 1;
652         sign = image->comps[0].sgnd;
653         jp2->bpc = depth_0 + (sign << 7);
654         for (i = 1; i < image->numcomps; i++) {
655                 int depth = image->comps[i].prec - 1;
656                 sign = image->comps[i].sgnd;
657                 if (depth_0 != depth)
658                         jp2->bpc = 255;
659         }
660         jp2->C = 7;                     /* C : Always 7 */
661         jp2->UnkC = 0;          /* UnkC, colorspace specified in colr box */
662         jp2->IPR = 0;           /* IPR, no intellectual property */
663         
664         /* BitsPerComponent box */
665
666         for (i = 0; i < image->numcomps; i++) {
667                 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
668         }
669
670         /* Colour Specification box */
671
672         if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
673                 jp2->meth = 1;  /* METH: Enumerated colourspace */
674         } else {
675                 jp2->meth = 2;  /* METH: Restricted ICC profile */
676         }
677         if (jp2->meth == 1) {
678                 if (image->color_space == 1)
679                         jp2->enumcs = 16;       /* sRGB as defined by IEC 61966�2�1 */
680                 else if (image->color_space == 2)
681                         jp2->enumcs = 17;       /* greyscale */
682                 else if (image->color_space == 3)
683                         jp2->enumcs = 18;       /* YUV */
684         } else {
685                 jp2->enumcs = 0;                /* PROFILE (??) */
686         }
687         jp2->precedence = 0;    /* PRECEDENCE */
688         jp2->approx = 0;                /* APPROX */
689
690 }
691
692 bool jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, char *index) {
693
694         /* JP2 encoding */
695
696         /* JPEG 2000 Signature box */
697         jp2_write_jp(cio);
698         /* File Type box */
699         jp2_write_ftyp(jp2, cio);
700         /* JP2 Header box */
701         jp2_write_jp2h(jp2, cio);
702
703         /* J2K encoding */
704
705         if(!jp2_write_jp2c(jp2, cio, image, index)) {
706                 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
707                 return false;
708         }
709
710         return true;
711 }
712
713