2 * Copyright (c) 2001-2003, David Janssens
3 * Copyright (c) 2002-2003, Yannick Verschueren
4 * Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
34 #ifndef DONT_HAVE_GETOPT
37 #include "compat/getopt.h"
44 printf ("HELP\n----\n\n");
45 printf ("- the option -help displays the readme.txt file on screen\n\n");
48 printf ("List of parameters for the coder JPEG 2000 :\n");
51 ("- The markers COD and QCD are writed both of two in the main_header and never appear in the tile_header. The markers in the main header are : SOC SIZ COD QCD COM.\n");
54 ("- This coder can encode mega image, a test was made on a 24000x24000 pixels color image. You need enough disk space memory (twice the original) to encode the image. (i.e. for a 1.5 Gb image you need a minimum of 3Gb of disk memory)\n");
56 printf ("REMARKS :\n");
57 printf ("---------\n");
60 ("* the value of rate enter in the code line is the compression factor !\n");
61 printf ("exemple :\n");
64 ("-r 20,10,1 means quality 1 : compress 20x, quality 2 : compress 10x and quality 3 : compress 1x = lossless\n");
66 printf ("By default :\n");
67 printf ("------------\n");
69 printf (" * lossless\n");
70 printf (" * 1 tile\n");
71 printf (" * size of precinct 2^15 x 2^15 (means 1 precinct)\n");
72 printf (" * size of code-block 64 x 64\n");
73 printf (" * Number of resolution : 6\n");
74 printf (" * No SOP marker in the codestream\n");
75 printf (" * No EPH marker in the codestream\n");
76 printf (" * No sub-sampling in x and y direction\n");
77 printf (" * No mode switch activated\n");
78 printf (" * progression order : LRCP\n");
79 printf (" * No index file\n");
80 printf (" * No ROI upshifted\n");
81 printf (" * No offset of the origin of the image\n");
82 printf (" * No offset of the origin of the tiles\n");
83 printf (" * Reversible DWT 5-3\n");
85 printf ("Parameters :\n");
86 printf ("------------\n");
89 ("-i : source file (-i source.pnm also *.pgm, *.ppm) required\n");
92 ("-o : destination file (-o dest.j2k or .jp2) required\n");
94 printf ("-help : Display the help information optional\n ");
96 printf ("-r : different rates (-r 20,10,5) optional\n ");
98 printf ("-n : Number of resolution (-n 3) optional\n");
100 printf ("-b : size of code block (-b 32,32) optional\n");
102 printf ("-c : size of precinct (-c 128,128) optional\n");
104 printf ("-t : size of tile (-t 512,512) optional\n");
107 ("-p : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] optional\n");
109 printf ("-s : subsampling factor (-s 2,2) [-s X,Y] optional\n");
111 printf ("-SOP : write SOP marker before each packet optional\n");
114 ("-EPH : write EPH marker after each header packet optional\n");
117 ("-M : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL) 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] optional\n");
119 (" for several mode switch you have to add the value of each mode you want\n");
121 (" ex : RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
124 ("-x : Create an index file *.Idx (-x index_name.Idx) optional\n");
127 ("-ROI:c=%%d,U=%%d : quantization indices upshifted for component c=%%d [%%d = 0,1,2]\n");
129 (" with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI:c=0,U=25) optional\n");
132 ("-d : offset of the origin of the image (-d 150,300) optional\n");
135 ("-T : offset of the origin of the tiles (-T 100,75) optional\n");
137 printf ("-I : Use the irreversible DWT 9-7 (-I) optional\n");
139 printf ("IMPORTANT :\n");
140 printf ("-----------\n");
142 printf ("* subsampling bigger than 2 can produce error\n");
144 printf ("The index file respect the structure below :\n");
145 printf ("---------------------------------------------\n");
147 printf ("Image_height Image_width\n");
148 printf ("progression order\n");
149 printf ("Tiles_size_X Tiles_size_Y\n");
150 printf ("Components_nb\n");
151 printf ("Layers_nb\n");
152 printf ("decomposition_levels\n");
153 printf ("Precincts_size_X Precincts_size_Y\n");
154 printf ("Main_header_end_position\n");
155 printf ("Codestream_size\n");
156 printf ("Tile0 start_pos end_Theader end_pos\n");
157 printf ("Tile1 '' '' ''\n");
159 printf ("TileN '' '' ''\n");
160 printf ("Tpacket_0 Tile layer res. comp. prec. start_pos end_pos\n");
162 printf ("Tpacket_M '' '' '' '' '' '' ''\n");
166 give_progression (char progression[4])
168 if (progression[0] == 'L' && progression[1] == 'R'
169 && progression[2] == 'C' && progression[3] == 'P')
175 if (progression[0] == 'R' && progression[1] == 'L'
176 && progression[2] == 'C' && progression[3] == 'P')
182 if (progression[0] == 'R' && progression[1] == 'P'
183 && progression[2] == 'C' && progression[3] == 'L')
189 if (progression[0] == 'P' && progression[1] == 'C'
190 && progression[2] == 'R' && progression[3] == 'L')
196 if (progression[0] == 'C' && progression[1] == 'P'
197 && progression[2] == 'R' && progression[3] == 'L')
211 double dwt_norms_97[4][10] = {
212 {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
213 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
214 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
215 {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
222 for (l = 0; a > 1; l++)
230 encode_stepsize (int stepsize, int numbps, int *expn, int *mant)
233 p = floorlog2 (stepsize) - 13;
234 n = 11 - floorlog2 (stepsize);
235 *mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
240 calc_explicit_stepsizes (j2k_tccp_t * tccp, int prec)
242 int numbands, bandno;
243 numbands = 3 * tccp->numresolutions - 2;
244 for (bandno = 0; bandno < numbands; bandno++)
248 int resno, level, orient, gain;
249 resno = bandno == 0 ? 0 : (bandno - 1) / 3 + 1;
250 orient = bandno == 0 ? 0 : (bandno - 1) % 3 + 1;
251 level = tccp->numresolutions - 1 - resno;
253 tccp->qmfbid == 0 ? 0 : (orient ==
255 || orient == 2 ? 1 : 2));
256 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT)
262 double norm = dwt_norms_97[orient][level];
263 stepsize = (1 << (gain + 1)) / norm;
265 encode_stepsize ((int) floor (stepsize * 8192.0), prec + gain,
266 &tccp->stepsizes[bandno].expn,
267 &tccp->stepsizes[bandno].mant);
272 main (int argc, char **argv)
275 int NumResolution, numD_min; /* NumResolution : number of resolution */
276 int Tile_arg; /* Tile_arg = 0 (not in argument) ou = 1 (in argument) */
277 int CSty; /* CSty : coding style */
278 int Prog_order; /* progression order (default LRCP) */
280 int numpocs, numpocs_tile; /* Number of progression order change (POC) default 0 */
281 int prcw_init[J2K_MAXRLVLS]; /* Initialisation Precinct width */
282 int prch_init[J2K_MAXRLVLS]; /* Initialisation Precinct height */
283 //int prcw_init, prch_init; /* Initialisation precincts' size */
284 int cblockw_init, cblockh_init; /* Initialisation codeblocks' size */
285 int mode, value; /* Mode switch (cblk_style) */
286 int subsampling_dx, subsampling_dy; /* subsampling value for dx and dy */
287 int ROI_compno, ROI_shift; /* region of interrest */
288 int Dim[2]; /* portion of the image coded */
289 int TX0, TY0; /* tile off-set */
291 j2k_cp_t cp, cp_init; /* cp_init is used to initialise in multiple tiles */
292 j2k_tcp_t *tcp, *tcp_init; /* tcp_init is used to initialise in multiple tile */
293 j2k_poc_t POC[32]; /* POC : used in case of Progression order change */
302 int res_spec = 0; /* For various precinct sizes specification */
322 ROI_compno = -1; /* no ROI */
328 cp.comment = "Created by OpenJPEG version 0.9";
331 cp.fixed_quality = 0; //add fixed_quality
335 cp_init.tcps = (j2k_tcp_t *) malloc (sizeof (j2k_tcp_t)); /* initialisation if only one tile */
336 tcp_init = &cp_init.tcps[0];
337 tcp_init->numlayers = 0;
339 cp.intermed_file = 1;
343 int c = getopt (argc, argv,
344 "i:o:r:q:f:t:n:c:b:x:p:s:d:h:P:S:E:M:R:T:C:I");
349 case 'i': /* IN fill */
363 if ((S1 == 'p' && S2 == 'g' && S3 == 'x')
364 || (S1 == 'P' && S2 == 'G' && S3 == 'X'))
370 if ((S1 == 'p' && S2 == 'n' && S3 == 'm')
371 || (S1 == 'P' && S2 == 'N' && S3 == 'M')
372 || (S1 == 'p' && S2 == 'g' && S3 == 'm') || (S1 == 'P'
375 || (S1 == 'P' && S2 == 'P' && S3 == 'M') || (S1 == 'p'
383 if ((S1 == 'b' && S2 == 'm' && S3 == 'p')
384 || (S1 == 'B' && S2 == 'M' && S3 == 'P'))
390 "!! Unrecognized format for infile : %c%c%c [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n\n",
394 /* ----------------------------------------------------- */
395 case 'o': /* OUT fill */
410 if ((S1 == 'j' && S2 == '2' && S3 == 'k')
411 || (S1 == 'J' && S2 == '2' && S3 == 'K'))
412 cp.JPEG2000_format = 0;
413 else if ((S1 == 'j' && S2 == 'p' && S3 == '2')
414 || (S1 == 'J' && S2 == 'P' && S3 == '2'))
415 cp.JPEG2000_format = 1;
419 "Unknown output format image *.%c%c%c [only *.j2k, *.jp2]!! \n",
427 /* ----------------------------------------------------- */
428 case 'r': /* rates rates/distorsion */
430 while (sscanf (s, "%d", &tcp_init->rates[tcp_init->numlayers]) == 1)
432 tcp_init->numlayers++;
433 while (*s && *s != ',')
444 /* ----------------------------------------------------- */
445 case 'q': /* add fixed_quality */
447 while (sscanf (s, "%f", &tcp_init->distoratio[tcp_init->numlayers])
450 tcp_init->numlayers++;
451 while (*s && *s != ',')
459 cp.fixed_quality = 1;
463 /* ----------------------------------------------------- */
464 case 'f': /* mod fixed_quality (before : -q) */
466 sscanf (s, "%d", &tcp_init->numlayers);
468 if (tcp_init->numlayers > 9)
471 (int *) malloc (tcp_init->numlayers * NumResolution * 3 *
474 for (i = 0; i < tcp_init->numlayers; i++)
476 tcp_init->rates[i] = 1;
477 sscanf (s, "%d,", &cp.matrice[i * NumResolution * 3]);
479 if (cp.matrice[i * NumResolution * 3] > 9)
481 cp.matrice[i * NumResolution * 3 + 1] = 0;
482 cp.matrice[i * NumResolution * 3 + 2] = 0;
483 for (j = 1; j < NumResolution; j++)
485 sscanf (s, "%d,%d,%d",
486 &cp.matrice[i * NumResolution * 3 + j * 3 + 0],
487 &cp.matrice[i * NumResolution * 3 + j * 3 + 1],
488 &cp.matrice[i * NumResolution * 3 + j * 3 + 2]);
490 if (cp.matrice[i * NumResolution * 3 + j * 3] > 9)
492 if (cp.matrice[i * NumResolution * 3 + j * 3 + 1] > 9)
494 if (cp.matrice[i * NumResolution * 3 + j * 3 + 2] > 9)
497 if (i < tcp_init->numlayers - 1)
502 /* ----------------------------------------------------- */
503 case 't': /* tiles */
504 sscanf (optarg, "%d,%d", &cp.tdx, &cp.tdy);
507 /* ----------------------------------------------------- */
508 case 'n': /* resolution */
509 sscanf (optarg, "%d", &NumResolution);
511 /* ----------------------------------------------------- */
512 case 'c': /* precinct dimension */
517 sscanf (s, "[%d,%d]%c", &prcw_init[res_spec],
518 &prch_init[res_spec], &sep);
521 s = strpbrk (s, "]") + 2;
525 /* ----------------------------------------------------- */
526 case 'b': /* code-block dimension */
527 sscanf (optarg, "%d,%d", &cblockw_init, &cblockh_init);
528 if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
529 || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4)
532 "!! Size of code_block error (option -b) !!\n\nRestriction :\n * width*height<=4096\n * 4<=width,height<= 1024\n\n");
536 /* ----------------------------------------------------- */
537 case 'x': /* creation of index file */
541 /* ----------------------------------------------------- */
542 case 'p': /* progression order */
544 for (i = 0; i < 4; i++)
549 Prog_order = give_progression (progression);
551 if (Prog_order == -1)
554 "Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
558 /* ----------------------------------------------------- */
559 case 's': /* subsampling factor */
560 if (sscanf (optarg, "%d,%d", &subsampling_dx, &subsampling_dy) != 2)
563 "'-s' sub-sampling argument error ! [-s dx,dy]\n");
567 /* ----------------------------------------------------- */
568 case 'd': /* coordonnate of the reference grid */
569 if (sscanf (optarg, "%d,%d", &Dim[0], &Dim[1]) != 2)
572 "-d 'coordonnate of the reference grid' argument error !! [-d x0,y0]\n");
576 /* ----------------------------------------------------- */
577 case 'h': /* Display an help description */
581 /* ----------------------------------------------------- */
583 fprintf (stderr, "/----------------------------------\\\n");
584 fprintf (stderr, "| POC option not fully tested !! |\n");
585 fprintf (stderr, "\\----------------------------------/\n");
588 while (sscanf (s, "T%d=%d,%d,%d,%d,%d,%s", &POC[numpocs].tile,
589 &POC[numpocs].resno0, &POC[numpocs].compno0,
590 &POC[numpocs].layno1, &POC[numpocs].resno1,
591 &POC[numpocs].compno1, POC[numpocs].progorder) == 7)
593 POC[numpocs].prg = give_progression (POC[numpocs].progorder);
594 /* POC[numpocs].tile; */
596 while (*s && *s != '/')
605 /* ------------------------------------------------------ */
606 case 'S': /* SOP marker */
609 /* ------------------------------------------------------ */
610 case 'E': /* EPH marker */
613 /* ------------------------------------------------------ */
614 case 'M': /* Mode switch pas tous au point !! */
615 if (sscanf (optarg, "%d", &value) == 1)
617 for (i = 0; i <= 5; i++)
619 int cache = value & (1 << i);
625 /* ------------------------------------------------------ */
627 if (sscanf (optarg, "OI:c=%d,U=%d", &ROI_compno, &ROI_shift) != 2)
629 fprintf (stderr, "ROI error !! [-ROI:c='compno',U='shift']\n");
633 /* ------------------------------------------------------ */
634 case 'T': /* Tile offset */
635 if (sscanf (optarg, "%d,%d", &TX0, &TY0) != 2)
638 "-T 'tile offset' argument error !! [-T X0,Y0]");
642 /* ------------------------------------------------------ */
643 case 'C': /* Add a comment */
646 /* ------------------------------------------------------ */
647 case 'I': /* reversible or not */
650 /* ------------------------------------------------------ */
661 if (!infile || !outfile)
664 "usage: image_to_j2k -i image-file -o j2k/jp2-file (+ options)\n");
668 if ((cp.disto_alloc || cp.fixed_alloc || cp.fixed_quality)
669 && (!(cp.disto_alloc ^ cp.fixed_alloc ^ cp.fixed_quality)))
672 "Error: options -r -q and -f can not be used together !!\n");
674 } // mod fixed_quality
676 /* if no rate entered, lossless by default */
677 if (tcp_init->numlayers == 0)
679 tcp_init->rates[tcp_init->numlayers] = 0; //MOD antonin : losslessbug
680 tcp_init->numlayers++;
684 if (TX0 > Dim[0] || TY0 > Dim[1])
687 "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
688 TX0, Dim[0], TY0, Dim[1]);
692 for (i = 0; i < numpocs; i++)
694 if (POC[i].prg == -1)
697 "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
702 switch (cp.image_type)
708 (infile, &img, cp.tdy, subsampling_dx, subsampling_dy, Dim, cp))
710 fprintf (stderr, "not a pgx file\n");
717 (infile, &img, -1, subsampling_dx, subsampling_dy, Dim, cp))
719 fprintf (stderr, " not a pgx file\n");
726 if (!pnmtoimage (infile, &img, subsampling_dx, subsampling_dy, Dim))
728 fprintf (stderr, " not a pnm file\n");
734 if (!bmptoimage (infile, &img, subsampling_dx, subsampling_dy, Dim))
736 fprintf (stderr, " not a bmp file\n");
741 /* to respect profile - 0 */
742 /* ---------------------- */
744 /* while (int_ceildiv(img.x1,(1<<numD_min))-int_ceildiv(img.x0,(1<<numD_min))>120 || int_ceildiv(img.y1,(1<<numD_min))-int_ceildiv(img.y0,(1<<numD_min))>160) numD_min++;
745 if ((numD_min+1)>NumResolution)
747 fprintf(stderr,"\n********************************************************************************\n\n");
748 fprintf(stderr, "In view to respect Profile-0, the number of resolution used is %d in place of %d\n\n",numD_min+1,NumResolution);
749 fprintf(stderr, "********************************************************************************\n\n");
750 NumResolution=numD_min+1;
755 cp.tw = int_ceildiv (img.x1 - cp.tx0, cp.tdx);
756 cp.th = int_ceildiv (img.y1 - cp.ty0, cp.tdy);
760 cp.tdx = img.x1 - cp.tx0;
761 cp.tdy = img.y1 - cp.ty0;
764 /* Initialization for PPM marker */
770 /* Init the mutiple tiles */
771 /* ---------------------- */
772 cp.tcps = (j2k_tcp_t *) malloc (cp.tw * cp.th * sizeof (j2k_tcp_t));
774 for (tileno = 0; tileno < cp.tw * cp.th; tileno++)
776 tcp = &cp.tcps[tileno];
777 tcp->numlayers = tcp_init->numlayers;
778 for (j = 0; j < tcp->numlayers; j++)
780 if (cp.fixed_quality) // add fixed_quality
781 tcp->distoratio[j] = tcp_init->distoratio[j];
783 tcp->rates[j] = tcp_init->rates[j];
786 tcp->prg = Prog_order;
787 tcp->mct = img.numcomps == 3 ? 1 : 0;
789 tcp->ppt_data = NULL;
796 /* intialisation of POC */
798 for (i = 0; i < numpocs; i++)
800 if (tileno == POC[i].tile - 1 || POC[i].tile == -1)
802 tcp_poc = &tcp->pocs[numpocs_tile];
803 tcp_poc->resno0 = POC[numpocs_tile].resno0;
804 tcp_poc->compno0 = POC[numpocs_tile].compno0;
805 tcp_poc->layno1 = POC[numpocs_tile].layno1;
806 tcp_poc->resno1 = POC[numpocs_tile].resno1;
807 tcp_poc->compno1 = POC[numpocs_tile].compno1;
808 tcp_poc->prg = POC[numpocs_tile].prg;
809 tcp_poc->tile = POC[numpocs_tile].tile;
814 tcp->numpocs = numpocs_tile;
815 tcp->tccps = (j2k_tccp_t *) malloc (img.numcomps * sizeof (j2k_tccp_t));
817 for (i = 0; i < img.numcomps; i++)
819 tccp = &tcp->tccps[i];
820 tccp->csty = CSty & 0x01; /* 0 => one precinct || 1 => custom precinct */
821 tccp->numresolutions = NumResolution;
822 tccp->cblkw = int_floorlog2 (cblockw_init);
823 tccp->cblkh = int_floorlog2 (cblockh_init);
824 tccp->cblksty = mode;
825 tccp->qmfbid = ir ? 0 : 1;
826 tccp->qntsty = ir ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
829 tccp->roishift = ROI_shift;
832 if (CSty & J2K_CCP_CSTY_PRT)
835 for (j = tccp->numresolutions - 1; j >= 0; j--)
839 if (prcw_init[p] < 1)
842 tccp->prcw[j] = int_floorlog2 (prcw_init[p]);
844 if (prch_init[p] < 1)
847 tccp->prch[j] = int_floorlog2 (prch_init[p]);
851 int size_prcw, size_prch;
853 prcw_init[res_spec - 1] >> (p - (res_spec - 1));
855 prch_init[res_spec - 1] >> (p - (res_spec - 1));
859 tccp->prcw[j] = int_floorlog2 (size_prcw);
863 tccp->prch[j] = int_floorlog2 (size_prch);
866 /*printf("\nsize precinct pour level %d : %d,%d\n", j,
867 tccp->prcw[j], tccp->prch[j]); */
872 for (j = 0; j < tccp->numresolutions; j++)
878 calc_explicit_stepsizes (tccp, img.comps[i].prec);
884 if (cp.JPEG2000_format == 0)
885 { /* J2K format output */
886 if (cp.intermed_file == 1)
887 { /* After the encoding of each tile, j2k_encode
888 stores the data in the file */
889 len = j2k_encode (&img, &cp, outfile, cp.tdx * cp.tdy * 2, index);
892 fprintf (stderr, "failed to encode image\n");
898 outbuf = (char *) malloc (cp.tdx * cp.tdy * cp.tw * cp.th * 2); /* Allocate memory for all tiles */
899 cio_init (outbuf, cp.tdx * cp.tdy * cp.tw * cp.th * 2);
901 j2k_encode (&img, &cp, outbuf,
902 cp.tdx * cp.tdy * cp.tw * cp.th * 2, index);
905 fprintf (stderr, "failed to encode image\n");
908 f = fopen (outfile, "wb");
911 fprintf (stderr, "failed to open %s for writing\n", outfile);
914 fwrite (outbuf, 1, len, f);
919 else /* JP2 format output */
921 jp2_struct_t *jp2_struct;
922 jp2_struct = (jp2_struct_t *) malloc (sizeof (jp2_struct_t));
923 jp2_struct->image = &img;
925 /* Initialising the standard JP2 box content */
926 /* If you wish to modify those boxes, you have to modify the jp2_struct content */
927 if (jp2_init_stdjp2 (jp2_struct, &img))
929 fprintf (stderr, "Error with jp2 initialization");
933 if (cp.intermed_file == 1)
935 /*For the moment, JP2 format does not use intermediary files for each tile */
936 cp.intermed_file = 0;
938 outbuf = (char *) malloc (cp.tdx * cp.tdy * cp.tw * cp.th * 2);
939 cio_init (outbuf, cp.tdx * cp.tdy * cp.tw * cp.th * 2);
940 len = jp2_encode (jp2_struct, &cp, outbuf, index);
943 fprintf (stderr, "failed to encode image\n");
946 f = fopen (outfile, "wb");
949 fprintf (stderr, "failed to open %s for writing\n", outfile);
952 fwrite (outbuf, 1, len, f);
957 /* Remove the temporary files */
958 /* -------------------------- */
961 for (i = 0; i < img.numcomps; i++)
964 sprintf (&tmp, "Compo%d", i);
965 if (remove (&tmp) == -1)
967 fprintf (stderr, "failed to kill %s file !\n", &tmp);
973 for (i = 0; i < cp.th; i++)
976 sprintf (&tmp, "bandtile%d", i + 1);
978 if (remove (&tmp) == -1)
980 fprintf (stderr, "failed to kill %s file !\n", &tmp);
988 if (tcp_init->numlayers > 9)
990 for (tileno = 0; tileno < cp.tw * cp.th; tileno++)
991 free (cp.tcps[tileno].tccps);