+int imagetobmp(opj_image_t * image, char *outfile) {
+ int w, wr, h, hr;
+ int i, pad;
+ FILE *fdest = NULL;
+
+ if (image->numcomps == 3 && image->comps[0].dx == image->comps[1].dx
+ && image->comps[1].dx == image->comps[2].dx
+ && image->comps[0].dy == image->comps[1].dy
+ && image->comps[1].dy == image->comps[2].dy
+ && image->comps[0].prec == image->comps[1].prec
+ && image->comps[1].prec == image->comps[2].prec) {
+
+ /* -->> -->> -->> -->>
+ 24 bits color
+ <<-- <<-- <<-- <<-- */
+
+ fdest = fopen(outfile, "wb");
+ if (!fdest) {
+ fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
+ return 1;
+ }
+
+ /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */
+ /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */
+ w = image->comps[0].w;
+ wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
+
+ /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */
+ /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
+ h = image->comps[0].h;
+ hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
+
+ fprintf(fdest, "BM");
+
+ /* FILE HEADER */
+ /* ------------- */
+ fprintf(fdest, "%c%c%c%c",
+ (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) + 54) & 0xff,
+ (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 8) & 0xff,
+ (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 16) & 0xff,
+ (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
+
+ /* INFO HEADER */
+ /* ------------- */
+ fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
+ (unsigned char) ((wr) >> 8) & 0xff,
+ (unsigned char) ((wr) >> 16) & 0xff,
+ (unsigned char) ((wr) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
+ (unsigned char) ((hr) >> 8) & 0xff,
+ (unsigned char) ((hr) >> 16) & 0xff,
+ (unsigned char) ((hr) >> 24) & 0xff);
+ fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
+ fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * hr * wr + 3 * hr * (wr % 2)) & 0xff,
+ (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 8) & 0xff,
+ (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 16) & 0xff,
+ (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
+
+ for (i = 0; i < wr * hr; i++) {
+ unsigned char R, G, B;
+ /* a modifier */
+ /* R = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
+ R = image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
+ /* G = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
+ G = image->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
+ /* B = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; */
+ B = image->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
+ fprintf(fdest, "%c%c%c", B, G, R);
+
+ if ((i + 1) % wr == 0) {
+ for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */
+ fprintf(fdest, "%c", 0);
+ }
+ }
+ fclose(fdest);
+ } else { /* Gray-scale */
+
+ /* -->> -->> -->> -->>
+ 8 bits non code (Gray scale)
+ <<-- <<-- <<-- <<-- */
+
+ fdest = fopen(outfile, "wb");
+ /* w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx); */
+ /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), image->comps[0].dx); */
+ w = image->comps[0].w;
+ wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
+
+ /* h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy); */
+ /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[0].dy); */
+ h = image->comps[0].h;
+ hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
+
+ fprintf(fdest, "BM");
+
+ /* FILE HEADER */
+ /* ------------- */
+ fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + 54 + 1024 + hr * (wr % 2)) & 0xff,
+ (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 8) & 0xff,
+ (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 16) & 0xff,
+ (unsigned char) ((hr * wr + 54 + 1024 + wr * (wr % 2)) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
+ ((54 + 1024) >> 16) & 0xff,
+ ((54 + 1024) >> 24) & 0xff);
+
+ /* INFO HEADER */
+ /* ------------- */
+ fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
+ (unsigned char) ((wr) >> 8) & 0xff,
+ (unsigned char) ((wr) >> 16) & 0xff,
+ (unsigned char) ((wr) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
+ (unsigned char) ((hr) >> 8) & 0xff,
+ (unsigned char) ((hr) >> 16) & 0xff,
+ (unsigned char) ((hr) >> 24) & 0xff);
+ fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
+ fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + hr * (wr % 2)) & 0xff,
+ (unsigned char) ((hr * wr + hr * (wr % 2)) >> 8) & 0xff,
+ (unsigned char) ((hr * wr + hr * (wr % 2)) >> 16) & 0xff,
+ (unsigned char) ((hr * wr + hr * (wr % 2)) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
+
+ for (i = 0; i < 256; i++) {
+ fprintf(fdest, "%c%c%c%c", i, i, i, 0);
+ }
+
+ for (i = 0; i < wr * hr; i++) {
+ /* a modifier !! */
+ /* fprintf(fdest, "%c", image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]); */
+ fprintf(fdest, "%c", image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]);
+ /*if (((i + 1) % w == 0 && w % 2))
+ fprintf(fdest, "%c", 0); */
+ if ((i + 1) % wr == 0) {
+ for (pad = wr % 4 ? 4 - wr % 4 : 0; pad > 0; pad--) /* ADD */
+ fprintf(fdest, "%c", 0);
+ }