1 /* Copyright (c) 2001 David Janssens
2 * Copyright (c) 2002-2003 Yannick Verschueren
3 * Copyright (c) 2002-2003 Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
34 #define _CRTDBG_MAP_ALLOC
36 #include <stdlib.h> // Must be included first
50 #ifndef DONT_HAVE_GETOPT
53 #include "compat/getopt.h"
56 void usage_display(char *prgm)
58 fprintf(stdout,"Usage:\n");
59 fprintf(stdout," %s...\n",prgm);
60 fprintf(stdout," -i <compressed file>\n");
61 fprintf(stdout," REQUIRED\n");
62 fprintf(stdout," Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
63 fprintf(stdout," is identified based on its suffix.\n");
64 fprintf(stdout," -o <decompressed file>\n");
65 fprintf(stdout," REQUIRED\n");
66 fprintf(stdout," Currently accepts PGM-files, PPM-files, PNM-files, PGX-files and\n");
67 fprintf(stdout," BMP-files. Binary data is written to the file (not ascii). If a PGX\n");
68 fprintf(stdout," filename is given, there will be as many output files as there are\n");
69 fprintf(stdout," components: an indice starting from 0 will then be appended to the\n");
70 fprintf(stdout," output filename, just before the \"pgx\" extension. If a PGM filename\n");
71 fprintf(stdout," is given and there are more than one component, only the first component\n");
72 fprintf(stdout," will be written to the file.\n");
73 fprintf(stdout," -r <reduce factor>\n");
74 fprintf(stdout," Set the number of highest resolution levels to be discarded. The\n");
75 fprintf(stdout," image resolution is effectively divided by 2 to the power of the\n");
76 fprintf(stdout," number of discarded levels. The reduce factor is limited by the\n");
77 fprintf(stdout," smallest total number of decomposition levels among tiles.\n");
78 //fprintf(stdout," -l <number of quality layers to decode>\n");
79 //fprintf(stdout," Set the maximum number of quality layers to decode. If there are\n");
80 //fprintf(stdout," less quality layers than the specified number, all the quality layers\n");
81 //fprintf(stdout," are decoded.\n");
82 fprintf(stdout," -u\n");
83 fprintf(stdout," print an usage statement\n");
87 int main(int argc, char **argv)
103 jp2_struct_t *jp2_struct=NULL;
105 int w, wr, wrr, h, hr, hrr, max;
106 int i, compno, pad, j;
115 int c = getopt(argc, argv,"i:o:r:l:u");
135 if ((S1 == 'j' && S2 == '2' && S3 == 'k')
136 || (S1 == 'J' && S2 == '2' && S3 == 'K')
137 || (S1 == 'j' && S2 == '2' && S3 == 'c')
138 || (S1 == 'J' && S2 == '2' && S3 == 'C')) {
139 cp.cod_format=J2K_CFMT;
144 if ((S1 == 'j' && S2 == 'p' && S3 == '2')
145 || (S1 == 'J' && S2 == 'P' && S3 == '2')) {
146 cp.cod_format=JP2_CFMT;
151 if ((S1 == 'j' && S2 == 'p' && S3 == 't')
152 || (S1 == 'J' && S2 == 'P' && S3 == 'T')) {
153 cp.cod_format=JPT_CFMT;
158 "j2k_to_image : Unknown input image format *.%c%c%c [only *.j2k, *.jp2, *.jpc or *.jpt]!! \n",
163 /* ----------------------------------------------------- */
180 if ((S1 == 'p' && S2 == 'g' && S3 == 'x')
181 || (S1 == 'P' && S2 == 'G' && S3 == 'X')) {
182 cp.decod_format = PGX_DFMT;
187 if ((S1 == 'p' && S2 == 'n' && S3 == 'm')
188 || (S1 == 'P' && S2 == 'N' && S3 == 'M')
189 || (S1 == 'p' && S2 == 'g' && S3 == 'm')
190 || (S1 == 'P' && S2 == 'G' && S3 == 'M')
191 || (S1 == 'P' && S2 == 'P' && S3 == 'M')
192 || (S1 == 'p' && S2 == 'p' && S3 == 'm')) {
193 cp.decod_format = PXM_DFMT;
198 if ((S1 == 'b' && S2 == 'm' && S3 == 'p')
199 || (S1 == 'B' && S2 == 'M' && S3 == 'P')) {
200 cp.decod_format = BMP_DFMT;
206 "!! Unrecognized output image format *.%c%c%c [only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n",
212 /* ----------------------------------------------------- */
217 sscanf(tmp, "%d", &cp.reduce);
220 /* ----------------------------------------------------- */
225 sscanf(tmp, "%d", &cp.layer);
228 /* ----------------------------------------------------- */
231 usage_display(argv[0]);
234 /* ----------------------------------------------------- */
237 fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c,optarg);
243 //Check required arguments
244 //------------------------
245 if (!infile || !outfile) {
246 fprintf(stderr,"ERROR -> At least one required argument is missing\nCheck j2k_to_image -u for usage information\n");
250 //Read the input file and put it in memory
251 //----------------------------------------
252 fsrc = fopen(infile, "rb");
254 fprintf(stderr, "ERROR -> failed to open %s for reading\n", infile);
257 fseek(fsrc, 0, SEEK_END);
259 fseek(fsrc, 0, SEEK_SET);
260 src = (char *) malloc(len);
261 fread(src, 1, len, fsrc);
264 //Decode the code-stream
265 //----------------------
266 switch(cp.cod_format) {
269 if (!j2k_decode(src, len, &img, &cp)) {
270 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
276 jp2_struct = (jp2_struct_t *) malloc(sizeof(jp2_struct_t));
277 jp2_struct->image = &img;
279 if (jp2_read_struct(src, jp2_struct, len)) {
280 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode jp2 structure!\n");
284 if (!j2k_decode(src + jp2_struct->j2k_codestream_offset, jp2_struct->j2k_codestream_len, &img, &cp)) {
285 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
289 /* Insert code here if you want to create actions on jp2_struct before deleting it */
295 if (!j2k_decode_jpt_stream(src, len, &img, &cp)) {
296 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode JPT-file!\n");
303 "ERROR -> j2k_to_image : Unknown input image format\n");
308 //Free the memory containing the code-stream
309 //------------------------------------------
314 //Create output image
315 //-------------------
317 /* ---------------------------- / */
319 /* / FORMAT : PNM, PGM or PPM / */
321 /* ---------------------------- / */
323 switch (cp.decod_format) {
324 case PXM_DFMT: /* PNM PGM PPM */
334 if (img.numcomps == 3 && img.comps[0].dx == img.comps[1].dx
335 && img.comps[1].dx == img.comps[2].dx
336 && img.comps[0].dy == img.comps[1].dy
337 && img.comps[1].dy == img.comps[2].dy
338 && img.comps[0].prec == img.comps[1].prec
339 && img.comps[1].prec == img.comps[2].prec
340 && S2 !='g' && S2 !='G') {
342 fdest = fopen(outfile, "wb");
344 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
348 w = int_ceildiv(img.x1 - img.x0, img.comps[0].dx);
349 // wr = int_ceildiv(int_ceildivpow2(img.x1 - img.x0,img.factor),img.comps[0].dx);
351 wrr = int_ceildivpow2(img.comps[0].w, img.comps[0].factor);
353 h = int_ceildiv(img.y1 - img.y0, img.comps[0].dy);
354 // hr = int_ceildiv(int_ceildivpow2(img.y1 - img.y0,img.factor), img.comps[0].dy);
356 hrr = int_ceildivpow2(img.comps[0].h, img.comps[0].factor);
358 max = img.comps[0].prec > 8 ? 255 : (1 << img.comps[0].prec) - 1;
361 int_ceildivpow2(img.comps[0].x0 -
362 int_ceildiv(img.x0, img.comps[0].dx),
363 img.comps[0].factor);
365 int_ceildivpow2(img.comps[0].y0 -
366 int_ceildiv(img.y0, img.comps[0].dy),
367 img.comps[0].factor);
370 fprintf(fdest, "P6\n# %d %d %d %d %d\n%d %d\n%d\n",
371 cp.tcps[cp.tileno[0]].tccps[0].numresolutions, w, h,
372 img.comps[0].x0, img.comps[0].y0, wrr, hrr, max);
373 adjust = img.comps[0].prec > 8 ? img.comps[0].prec - 8 : 0;
374 for (i = 0; i < wrr * hrr; i++) {
376 r = img.comps[0].data[i / wrr * wr + i % wrr];
377 r += (img.comps[0].sgnd ? 1 << (img.comps[0].prec - 1) : 0);
380 g = img.comps[1].data[i / wrr * wr + i % wrr];
381 g += (img.comps[1].sgnd ? 1 << (img.comps[1].prec - 1) : 0);
384 b = img.comps[2].data[i / wrr * wr + i % wrr];
385 b += (img.comps[2].sgnd ? 1 << (img.comps[2].prec - 1) : 0);
388 fprintf(fdest, "%c%c%c", r, g, b);
390 free(img.comps[0].data);
391 free(img.comps[1].data);
392 free(img.comps[2].data);
396 int ncomp=(S2=='g' || S2=='G')?1:img.numcomps;
397 if (img.numcomps>ncomp) {
398 fprintf(stderr,"WARNING -> [PGM files] Only the first component\n");
399 fprintf(stderr," is written to the file\n");
401 for (compno = 0; compno < ncomp; compno++) {
404 sprintf(name, "%d.%s", compno, outfile);
406 sprintf(name, "%s", outfile);
409 fdest = fopen(name, "wb");
411 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
415 w = int_ceildiv(img.x1 - img.x0, img.comps[compno].dx);
416 // wr = int_ceildiv(int_ceildivpow2(img.x1 - img.x0,img.factor),img.comps[compno].dx);
417 wr = img.comps[compno].w;
419 int_ceildivpow2(img.comps[compno].w, img.comps[compno].factor);
421 h = int_ceildiv(img.y1 - img.y0, img.comps[compno].dy);
422 // hr = int_ceildiv(int_ceildivpow2(img.y1 - img.y0,img.factor), img.comps[compno].dy);
423 hr = img.comps[compno].h;
425 int_ceildivpow2(img.comps[compno].h, img.comps[compno].factor);
428 img.comps[compno].prec >
429 8 ? 255 : (1 << img.comps[compno].prec) - 1;
431 img.comps[compno].x0 =
432 int_ceildivpow2(img.comps[compno].x0 -
434 img.comps[compno].dx),
435 img.comps[compno].factor);
436 img.comps[compno].y0 =
437 int_ceildivpow2(img.comps[compno].y0 -
439 img.comps[compno].dy),
440 img.comps[compno].factor);
442 fprintf(fdest, "P5\n# %d %d %d %d %d\n%d %d\n%d\n",
443 cp.tcps[cp.tileno[0]].tccps[compno].
444 numresolutions, w, h, img.comps[compno].x0,
445 img.comps[compno].y0, wrr, hrr, max);
447 img.comps[compno].prec > 8 ? img.comps[compno].prec - 8 : 0;
448 for (i = 0; i < wrr * hrr; i++) {
450 l = img.comps[compno].data[i / wrr * wr + i % wrr];
451 l += (img.comps[compno].
452 sgnd ? 1 << (img.comps[compno].prec - 1) : 0);
454 fprintf(fdest, "%c", l);
457 free(img.comps[compno].data);
462 /* ------------------------ / */
464 /* / FORMAT : PGX / */
466 /* /----------------------- / */
467 case PGX_DFMT: /* PGX */
468 for (compno = 0; compno < img.numcomps; compno++) {
469 j2k_comp_t *comp = &img.comps[compno];
480 //if (img.numcomps > 1)
481 sprintf(name, "%s-%d.pgx", outfile, compno);
485 //sprintf(name, "%s.pgx", outfile);
487 fdest = fopen(name, "wb");
489 fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
493 // w = int_ceildiv(img.x1 - img.x0, comp->dx);
494 // wr = int_ceildiv(int_ceildivpow2(img.x1 - img.x0,img.factor), comp->dx);
495 w = img.comps[compno].w;
496 wr = int_ceildivpow2(img.comps[compno].w, img.comps[compno].factor);
498 // h = int_ceildiv(img.y1 - img.y0, comp->dy);
499 // hr = int_ceildiv(int_ceildivpow2(img.y1 - img.y0,img.factor), comp->dy);
500 h = img.comps[compno].h;
501 hr = int_ceildivpow2(img.comps[compno].h, img.comps[compno].factor);
503 fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+',
509 else if (comp->prec <= 16)
514 for (i = 0; i < wr * hr; i++) {
515 int v = img.comps[compno].data[i / wr * w + i % wr];
517 for (j = nbytes - 1; j >= 0; j--) {
519 char byte = (char) (v >> (j * 8));
521 fwrite(&byte, 1, 1, fdest);
525 free(img.comps[compno].data);
530 /* ------------------------ / */
532 /* / FORMAT : BMP / */
534 /* /----------------------- / */
536 case BMP_DFMT: /* BMP */
537 if (img.numcomps == 3 && img.comps[0].dx == img.comps[1].dx
538 && img.comps[1].dx == img.comps[2].dx
539 && img.comps[0].dy == img.comps[1].dy
540 && img.comps[1].dy == img.comps[2].dy
541 && img.comps[0].prec == img.comps[1].prec
542 && img.comps[1].prec == img.comps[2].prec) {
543 /* -->> -->> -->> -->>
547 <<-- <<-- <<-- <<-- */
549 fdest = fopen(outfile, "wb");
551 fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
555 // w = int_ceildiv(img.x1 - img.x0, img.comps[0].dx);
556 // wr = int_ceildiv(int_ceildivpow2(img.x1 - img.x0,img.factor), img.comps[0].dx);
558 wr = int_ceildivpow2(img.comps[0].w, img.comps[0].factor);
560 // h = int_ceildiv(img.y1 - img.y0, img.comps[0].dy);
561 // hr = int_ceildiv(int_ceildivpow2(img.y1 - img.y0,img.factor), img.comps[0].dy);
563 hr = int_ceildivpow2(img.comps[0].h, img.comps[0].factor);
565 fprintf(fdest, "BM");
569 fprintf(fdest, "%c%c%c%c",
570 (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) +
572 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)
574 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)
576 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)
578 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
579 ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
580 fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,
581 ((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
585 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,
586 ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
587 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
588 (unsigned char) ((wr) >> 8) & 0xff,
589 (unsigned char) ((wr) >> 16) & 0xff,
590 (unsigned char) ((wr) >> 24) & 0xff);
591 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
592 (unsigned char) ((hr) >> 8) & 0xff,
593 (unsigned char) ((hr) >> 16) & 0xff,
594 (unsigned char) ((hr) >> 24) & 0xff);
595 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
596 fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
597 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
598 ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
599 fprintf(fdest, "%c%c%c%c",
600 (unsigned char) (3 * hr * wr +
601 3 * hr * (wr % 2)) & 0xff,
602 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >>
604 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >>
606 (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >>
608 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
609 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
610 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
611 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
612 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
613 ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
614 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
615 ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
617 for (i = 0; i < wr * hr; i++) {
618 unsigned char R, G, B;
620 // R = img.comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
621 R = img.comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
622 // G = img.comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
623 G = img.comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
624 // B = img.comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
625 B = img.comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
626 fprintf(fdest, "%c%c%c", B, G, R);
628 if ((i + 1) % wr == 0) {
629 for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */
630 fprintf(fdest, "%c", 0);
634 free(img.comps[1].data);
635 free(img.comps[2].data);
636 } else { /* Gray-scale */
638 /* -->> -->> -->> -->>
640 8 bits non code (Gray scale)
642 <<-- <<-- <<-- <<-- */
643 fdest = fopen(argv[2], "wb");
644 // w = int_ceildiv(img.x1 - img.x0, img.comps[0].dx);
645 // wr = int_ceildiv(int_ceildivpow2(img.x1 - img.x0,img.factor), img.comps[0].dx);
647 wr = int_ceildivpow2(img.comps[0].w, img.comps[0].factor);
649 // h = int_ceildiv(img.y1 - img.y0, img.comps[0].dy);
650 // hr = int_ceildiv(int_ceildivpow2(img.y1 - img.y0,img.factor), img.comps[0].dy);
652 hr = int_ceildivpow2(img.comps[0].h, img.comps[0].factor);
654 fprintf(fdest, "BM");
658 fprintf(fdest, "%c%c%c%c",
659 (unsigned char) (hr * wr + 54 + 1024 +
660 hr * (wr % 2)) & 0xff,
661 (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2))
663 (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2))
665 (unsigned char) ((hr * wr + 54 + 1024 + wr * (wr % 2))
667 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
668 ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
669 fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff,
670 ((54 + 1024) >> 8) & 0xff, ((54 + 1024) >> 16) & 0xff,
671 ((54 + 1024) >> 24) & 0xff);
675 fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,
676 ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
677 fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
678 (unsigned char) ((wr) >> 8) & 0xff,
679 (unsigned char) ((wr) >> 16) & 0xff,
680 (unsigned char) ((wr) >> 24) & 0xff);
681 fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
682 (unsigned char) ((hr) >> 8) & 0xff,
683 (unsigned char) ((hr) >> 16) & 0xff,
684 (unsigned char) ((hr) >> 24) & 0xff);
685 fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
686 fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
687 fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
688 ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
689 fprintf(fdest, "%c%c%c%c",
690 (unsigned char) (hr * wr + hr * (wr % 2)) & 0xff,
691 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 8) &
693 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 16) &
695 (unsigned char) ((hr * wr + hr * (wr % 2)) >> 24) & 0xff);
696 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
697 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
698 fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
699 ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
700 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
701 ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
702 fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
703 ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
706 for (i = 0; i < 256; i++) {
707 fprintf(fdest, "%c%c%c%c", i, i, i, 0);
710 for (i = 0; i < wr * hr; i++) {
712 // fprintf(fdest, "%c", img.comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]);
714 img.comps[0].data[w * hr - ((i) / (wr) + 1) * w +
716 /*if (((i + 1) % w == 0 && w % 2))
717 fprintf(fdest, "%c", 0); */
718 if ((i + 1) % wr == 0) {
719 for (pad = wr % 4 ? 4 - wr % 4 : 0; pad > 0; pad--) /* ADD */
720 fprintf(fdest, "%c", 0);
724 free(img.comps[0].data);
729 "ERROR -> j2k_to_image : Unknown output image format\n");
735 // Free remaining structures
736 //--------------------------
741 // Check memory leaks if debug mode
742 //---------------------------------
746 _CrtDumpMemoryLeaks();