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