X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=src%2Fgdcmopenjpeg%2Fcodec%2Fconvert.c;h=fc7a17fac785b7a1ed69232f1f1e094c1b5dc068;hb=a401c209a120a44b355e53180a5a9a8d1a3cdc4c;hp=2aeed36698aa3746ac6c905388a33544dbc43d10;hpb=d18b84b1727b1fe38b5c6b9ceea237fb445eefb3;p=gdcm.git diff --git a/src/gdcmopenjpeg/codec/convert.c b/src/gdcmopenjpeg/codec/convert.c index 2aeed366..fc7a17fa 100644 --- a/src/gdcmopenjpeg/codec/convert.c +++ b/src/gdcmopenjpeg/codec/convert.c @@ -1,7 +1,9 @@ /* * Copyright (c) 2001-2003, David Janssens * Copyright (c) 2002-2003, Yannick Verschueren - * Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium + * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe + * Copyright (c) 2005, Hervé Drolon, FreeImage Team + * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -25,12 +27,41 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ - -#include #include #include -#include #include +#include "openjpeg.h" + +/* + * Get logarithm of an integer and round downwards. + * + * log2(a) + */ +static int int_floorlog2(int a) { + int l; + for (l = 0; a > 1; l++) { + a >>= 1; + } + return l; +} + +/* + * Divide an integer by a power of 2 and round upwards. + * + * a divided by 2^b + */ +static int int_ceildivpow2(int a, int b) { + return (a + (1 << b) - 1) >> b; +} + +/* + * Divide an integer and round upwards. + * + * a divided by b + */ +static int int_ceildiv(int a, int b) { + return (a + b - 1) / b; +} /* -->> -->> -->> -->> @@ -38,63 +69,66 @@ <<-- <<-- <<-- <<-- */ -/* UINT2 defines a two byte word */ -typedef unsigned short int UINT2; +/* WORD defines a two byte word */ +typedef unsigned short int WORD; -/* UINT4 defines a four byte word */ -typedef unsigned long int UINT4; +/* DWORD defines a four byte word */ +typedef unsigned long int DWORD; typedef struct { - UINT2 bfType; /* 'BM' for Bitmap (19776) */ - UINT4 bfSize; /* Size of the file */ - UINT2 bfReserved1; /* Reserved : 0 */ - UINT2 bfReserved2; /* Reserved : 0 */ - UINT4 bfOffBits; /* Offset */ + WORD bfType; /* 'BM' for Bitmap (19776) */ + DWORD bfSize; /* Size of the file */ + WORD bfReserved1; /* Reserved : 0 */ + WORD bfReserved2; /* Reserved : 0 */ + DWORD bfOffBits; /* Offset */ } BITMAPFILEHEADER_t; typedef struct { - UINT4 biSize; /* Size of the structure in bytes */ - UINT4 biWidth; /* Width of the image in pixels */ - UINT4 biHeight; /* Heigth of the image in pixels */ - UINT2 biPlanes; /* 1 */ - UINT2 biBitCount; /* Number of color bits by pixels */ - UINT4 biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */ - UINT4 biSizeImage; /* Size of the image in bytes */ - UINT4 biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */ - UINT4 biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */ - UINT4 biClrUsed; /* Number of color used in the image (0: ALL) */ - UINT4 biClrImportant; /* Number of important color (0: ALL) */ + DWORD biSize; /* Size of the structure in bytes */ + DWORD biWidth; /* Width of the image in pixels */ + DWORD biHeight; /* Heigth of the image in pixels */ + WORD biPlanes; /* 1 */ + WORD biBitCount; /* Number of color bits by pixels */ + DWORD biCompression; /* Type of encoding 0: none 1: RLE8 2: RLE4 */ + DWORD biSizeImage; /* Size of the image in bytes */ + DWORD biXpelsPerMeter; /* Horizontal (X) resolution in pixels/meter */ + DWORD biYpelsPerMeter; /* Vertical (Y) resolution in pixels/meter */ + DWORD biClrUsed; /* Number of color used in the image (0: ALL) */ + DWORD biClrImportant; /* Number of important color (0: ALL) */ } BITMAPINFOHEADER_t; -int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx, - int subsampling_dy, int Dim[2]) -{ +opj_image_t* bmptoimage(char *filename, opj_cparameters_t *parameters) { + int subsampling_dx = parameters->subsampling_dx; + int subsampling_dy = parameters->subsampling_dy; + + int i, numcomps, w, h; + OPJ_COLOR_SPACE color_space; + opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */ + opj_image_t * image = NULL; + FILE *IN; - FILE *Compo0 = NULL, *Compo1 = NULL, *Compo2 = NULL; BITMAPFILEHEADER_t File_h; BITMAPINFOHEADER_t Info_h; unsigned char *RGB; unsigned char *table_R, *table_G, *table_B; - unsigned int j, w, h, PAD, type = 0; + unsigned int j, PAD = 0; - int i; + int x, y, index; int gray_scale = 1, not_end_file = 1; unsigned int line = 0, col = 0; unsigned char v, v2; - UINT4 W, H; - + DWORD W, H; + IN = fopen(filename, "rb"); if (!IN) { - fprintf(stderr, - "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", - filename); + fprintf(stderr, "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", filename); return 0; } - + File_h.bfType = getc(IN); File_h.bfType = (getc(IN) << 8) + File_h.bfType; - + if (File_h.bfType != 19778) { fprintf(stderr,"Error, not a BMP file!\n"); return 0; @@ -174,263 +208,191 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx, Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant; /* Read the data and store them in the OUT file */ - + if (Info_h.biBitCount == 24) { - img->x0 = Dim[0]; - img->y0 = Dim[1]; - img->x1 = - !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) * - subsampling_dx + 1; - img->y1 = - !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) * - subsampling_dy + 1; - img->numcomps = 3; - img->color_space = 1; - img->comps = - (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t)); - for (i = 0; i < img->numcomps; i++) { - img->comps[i].prec = 8; - img->comps[i].bpp = 8; - img->comps[i].sgnd = 0; - img->comps[i].dx = subsampling_dx; - img->comps[i].dy = subsampling_dy; - } - Compo0 = fopen("Compo0", "wb"); - if (!Compo0) { - fprintf(stderr, - "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); + numcomps = 3; + color_space = CLRSPC_SRGB; + /* initialize image components */ + memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t)); + for(i = 0; i < numcomps; i++) { + cmptparm[i].prec = 8; + cmptparm[i].bpp = 8; + cmptparm[i].sgnd = 0; + cmptparm[i].dx = subsampling_dx; + cmptparm[i].dy = subsampling_dy; + cmptparm[i].w = w; + cmptparm[i].h = h; } - Compo1 = fopen("Compo1", "wb"); - if (!Compo1) { - fprintf(stderr, - "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n"); - } - Compo2 = fopen("Compo2", "wb"); - if (!Compo2) { - fprintf(stderr, - "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n"); + /* create the image */ + image = opj_image_create(numcomps, &cmptparm[0], color_space); + if(!image) { + fclose(IN); + return NULL; } + /* set image offset and reference grid */ + image->x0 = parameters->image_offset_x0; + image->y0 = parameters->image_offset_y0; + image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1; + image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1; + + /* set image data */ + /* Place the cursor at the beginning of the image information */ fseek(IN, 0, SEEK_SET); fseek(IN, File_h.bfOffBits, SEEK_SET); - + W = Info_h.biWidth; H = Info_h.biHeight; - /* PAD = 4 - (3 * W) % 4;*/ + /* PAD = 4 - (3 * W) % 4; */ /* PAD = (PAD == 4) ? 0 : PAD; */ PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0; - - - RGB = - (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char)); - + + RGB = (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char)); + fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN); - - for (j = 0; j < (3 * W + PAD) * H; j++) { - unsigned char elmt; - int Wp = 3 * W + PAD; - - elmt = RGB[(H - (j / Wp + 1)) * Wp + j % Wp]; - if ((j % Wp) < (3 * W)) { - switch (type) { - case 0: - fprintf(Compo2, "%c", elmt); - type = 1; - break; - case 1: - fprintf(Compo1, "%c", elmt); - type = 2; - break; - case 2: - fprintf(Compo0, "%c", elmt); - type = 0; - break; - } + + index = 0; + + for(y = 0; y < (int)H; y++) { + unsigned char *scanline = RGB + (3 * W + PAD) * (H - 1 - y); + for(x = 0; x < (int)W; x++) { + unsigned char *pixel = &scanline[3 * x]; + image->comps[0].data[index] = pixel[2]; /* R */ + image->comps[1].data[index] = pixel[1]; /* G */ + image->comps[2].data[index] = pixel[0]; /* B */ + index++; } } - fclose(Compo0); - fclose(Compo1); - fclose(Compo2); free(RGB); - } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) { - img->x0 = Dim[0]; - img->y0 = Dim[1]; - img->x1 = - !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -1) * - subsampling_dx + 1; - img->y1 = - !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -1) * - subsampling_dy + 1; + } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) { table_R = (unsigned char *) malloc(256 * sizeof(unsigned char)); table_G = (unsigned char *) malloc(256 * sizeof(unsigned char)); table_B = (unsigned char *) malloc(256 * sizeof(unsigned char)); - + for (j = 0; j < Info_h.biClrUsed; j++) { table_B[j] = getc(IN); table_G[j] = getc(IN); table_R[j] = getc(IN); getc(IN); - if (table_R[j] != table_G[j] && table_R[j] != table_B[j] - && table_G[j] != table_B[j]) + if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j]) gray_scale = 0; } - + /* Place the cursor at the beginning of the image information */ fseek(IN, 0, SEEK_SET); fseek(IN, File_h.bfOffBits, SEEK_SET); - + W = Info_h.biWidth; H = Info_h.biHeight; if (Info_h.biWidth % 2) W++; + + numcomps = gray_scale ? 1 : 3; + color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB; + /* initialize image components */ + memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t)); + for(i = 0; i < numcomps; i++) { + cmptparm[i].prec = 8; + cmptparm[i].bpp = 8; + cmptparm[i].sgnd = 0; + cmptparm[i].dx = subsampling_dx; + cmptparm[i].dy = subsampling_dy; + cmptparm[i].w = w; + cmptparm[i].h = h; + } + /* create the image */ + image = opj_image_create(numcomps, &cmptparm[0], color_space); + if(!image) { + fclose(IN); + return NULL; + } - RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char)); + /* set image offset and reference grid */ + image->x0 = parameters->image_offset_x0; + image->y0 = parameters->image_offset_y0; + image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1; + image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1; + + /* set image data */ + RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char)); + fread(RGB, sizeof(unsigned char), W * H, IN); if (gray_scale) { - img->numcomps = 1; - img->comps = - (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t)); - img->comps[0].prec = 8; - img->comps[0].bpp = 8; - img->comps[0].sgnd = 0; - img->comps[0].dx = subsampling_dx; - img->comps[0].dy = subsampling_dy; - Compo0 = fopen("Compo0", "wb"); - if (!Compo0) { - fprintf(stderr, - "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); - } + index = 0; for (j = 0; j < W * H; j++) { - if ((j % W < W - 1 && Info_h.biWidth % 2) - || !(Info_h.biWidth % 2)) - fprintf(Compo0, "%c", - table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]); - } - fclose(Compo0); - } else { - img->numcomps = 3; - img->comps = - (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t)); - for (i = 0; i < img->numcomps; i++) { - img->comps[i].prec = 8; - img->comps[i].bpp = 8; - img->comps[i].sgnd = 0; - img->comps[i].dx = subsampling_dx; - img->comps[i].dy = subsampling_dy; - } - - Compo0 = fopen("Compo0", "wb"); - if (!Compo0) { - fprintf(stderr, - "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); - } - Compo1 = fopen("Compo1", "wb"); - if (!Compo1) { - fprintf(stderr, - "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n"); - } - Compo2 = fopen("Compo2", "wb"); - if (!Compo2) { - fprintf(stderr, - "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n"); + if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) { + image->comps[0].data[index] = table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]; + index++; + } } + } else { + index = 0; for (j = 0; j < W * H; j++) { - if ((j % W < W - 1 && Info_h.biWidth % 2) - || !(Info_h.biWidth % 2)) { - fprintf(Compo0, "%c", - table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]); - fprintf(Compo1, "%c", - table_G[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]); - fprintf(Compo2, "%c", - table_B[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]); + if ((j % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) { + unsigned char pixel_index = RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]; + image->comps[0].data[index] = table_R[pixel_index]; + image->comps[1].data[index] = table_G[pixel_index]; + image->comps[2].data[index] = table_B[pixel_index]; + index++; } - } - fclose(Compo0); - fclose(Compo1); - fclose(Compo2); } free(RGB); - - } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) { - img->x0 = Dim[0]; - img->y0 = Dim[1]; - img->x1 = - !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) * - subsampling_dx + 1; - img->y1 = - !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) * - subsampling_dy + 1; - + + } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) { table_R = (unsigned char *) malloc(256 * sizeof(unsigned char)); table_G = (unsigned char *) malloc(256 * sizeof(unsigned char)); table_B = (unsigned char *) malloc(256 * sizeof(unsigned char)); - + for (j = 0; j < Info_h.biClrUsed; j++) { table_B[j] = getc(IN); table_G[j] = getc(IN); table_R[j] = getc(IN); getc(IN); - if (table_R[j] != table_G[j] && table_R[j] != table_B[j] - && table_G[j] != table_B[j]) + if (table_R[j] != table_G[j] && table_R[j] != table_B[j] && table_G[j] != table_B[j]) gray_scale = 0; } - /* Place the cursor at the beginning of the image information */ - fseek(IN, 0, SEEK_SET); - fseek(IN, File_h.bfOffBits, SEEK_SET); - - if (gray_scale) { - img->numcomps = 1; - img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t)); - img->comps[0].prec = 8; - img->comps[0].bpp = 8; - img->comps[0].sgnd = 0; - img->comps[0].dx = subsampling_dx; - img->comps[0].dy = subsampling_dy; - Compo0 = fopen("Compo0", "wb"); - if (!Compo0) { - fprintf(stderr, - "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); - } - } else { - img->numcomps = 3; - img->comps = - (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t)); - for (i = 0; i < img->numcomps; i++) { - img->comps[i].prec = 8; - img->comps[i].bpp = 8; - img->comps[i].sgnd = 0; - img->comps[i].dx = subsampling_dx; - img->comps[i].dy = subsampling_dy; - } - Compo0 = fopen("Compo0", "wb"); - if (!Compo0) { - fprintf(stderr, - "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); - } - Compo1 = fopen("Compo1", "wb"); - if (!Compo1) { - fprintf(stderr, - "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n"); - } - Compo2 = fopen("Compo2", "wb"); - if (!Compo2) { - fprintf(stderr, - "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n"); - } + numcomps = gray_scale ? 1 : 3; + color_space = gray_scale ? CLRSPC_GRAY : CLRSPC_SRGB; + /* initialize image components */ + memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t)); + for(i = 0; i < numcomps; i++) { + cmptparm[i].prec = 8; + cmptparm[i].bpp = 8; + cmptparm[i].sgnd = 0; + cmptparm[i].dx = subsampling_dx; + cmptparm[i].dy = subsampling_dy; + cmptparm[i].w = w; + cmptparm[i].h = h; + } + /* create the image */ + image = opj_image_create(numcomps, &cmptparm[0], color_space); + if(!image) { + fclose(IN); + return NULL; } - RGB = - (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * - sizeof(unsigned char)); + /* set image offset and reference grid */ + image->x0 = parameters->image_offset_x0; + image->y0 = parameters->image_offset_y0; + image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1; + image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1; + /* set image data */ + + /* Place the cursor at the beginning of the image information */ + fseek(IN, 0, SEEK_SET); + fseek(IN, File_h.bfOffBits, SEEK_SET); + + RGB = (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * sizeof(unsigned char)); + while (not_end_file) { v = getc(IN); if (v) { @@ -442,70 +404,225 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx, } else { v = getc(IN); switch (v) { - case 0: - col = 0; - line++; - break; - case 1: - line++; - not_end_file = 0; - break; - case 2: - fprintf(stderr,"No Delta supported\n"); - return 1; - break; - default: - for (i = 0; i < v; i++) { - v2 = getc(IN); - RGB[line * Info_h.biWidth + col] = v2; - col++; - } - if (v % 2) - v2 = getc(IN); + case 0: + col = 0; + line++; + break; + case 1: + line++; + not_end_file = 0; + break; + case 2: + fprintf(stderr,"No Delta supported\n"); + opj_image_destroy(image); + fclose(IN); + return NULL; + default: + for (i = 0; i < v; i++) { + v2 = getc(IN); + RGB[line * Info_h.biWidth + col] = v2; + col++; + } + if (v % 2) + v2 = getc(IN); + break; } } } - if (gray_scale) { - for (line = 0; line < Info_h.biHeight; line++) - for (col = 0; col < Info_h.biWidth; col++) - fprintf(Compo0, "%c", table_R[(int) - RGB[(Info_h.biHeight - line - - 1) * Info_h.biWidth + col]]); - fclose(Compo0); - } else { - for (line = 0; line < Info_h.biHeight; line++) - for (col = 0; col < Info_h.biWidth; col++) { - fprintf(Compo0, "%c", table_R[(int) - RGB[(Info_h.biHeight - line - - 1) * Info_h.biWidth + col]]); - fprintf(Compo1, "%c", table_G[(int) - RGB[(Info_h.biHeight - line - - 1) * Info_h.biWidth + col]]); - fprintf(Compo2, "%c", table_B[(int) - RGB[(Info_h.biHeight - line - - 1) * Info_h.biWidth + col]]); - } - fclose(Compo0); - fclose(Compo1); - fclose(Compo2); + index = 0; + for (line = 0; line < Info_h.biHeight; line++) { + for (col = 0; col < Info_h.biWidth; col++) { + image->comps[0].data[index] = table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]]; + index++; + } } - free(RGB); - } else - fprintf(stderr, - "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", - Info_h.biBitCount); - - fclose(IN); - return 1; + } else { + index = 0; + for (line = 0; line < Info_h.biHeight; line++) { + for (col = 0; col < Info_h.biWidth; col++) { + unsigned char pixel_index = (int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]; + image->comps[0].data[index] = table_R[pixel_index]; + image->comps[1].data[index] = table_G[pixel_index]; + image->comps[2].data[index] = table_B[pixel_index]; + index++; + } + } + } + free(RGB); + } else { + fprintf(stderr, + "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount); + } + fclose(IN); + } + + return image; +} + +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); + } + } + fclose(fdest); } + + return 0; } - /* -->> -->> -->> -->> +/* -->> -->> -->> -->> - PGX IMAGE FORMAT +PGX IMAGE FORMAT - <<-- <<-- <<-- <<-- */ +<<-- <<-- <<-- <<-- */ unsigned char readuchar(FILE * f) @@ -539,160 +656,197 @@ unsigned int readuint(FILE * f, int bigendian) return (c4 << 24) + (c3 << 16) + (c2 << 8) + c1; } -int pgxtoimage(char *filename, j2k_image_t * img, int tdy, - int subsampling_dx, int subsampling_dy, int Dim[2], - j2k_cp_t cp) -{ - FILE *f; +opj_image_t* pgxtoimage(char *filename, opj_cparameters_t *parameters) { + FILE *f = NULL; int w, h, prec; - int i, compno, bandno; - char str[256]; + int i, numcomps, max; + OPJ_COLOR_SPACE color_space; + opj_image_cmptparm_t cmptparm; /* maximum of 1 component */ + opj_image_t * image = NULL; char endian1,endian2,sign; char signtmp[32]; char temp[32]; int bigendian; - j2k_comp_t *comp; - - img->numcomps = 1; - img->color_space = 2; - img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t)); - for (compno = 0; compno < img->numcomps; compno++) { - FILE *src; - char tmp[16]; - int max = 0; - int Y1; - - comp = &img->comps[compno]; - sprintf(str, "%s", filename); - + opj_image_comp_t *comp = NULL; - f = fopen(str, "rb"); - if (!f) { - fprintf(stderr, "Failed to open %s for reading !\n", str); - return 0; - } + numcomps = 1; + color_space = CLRSPC_GRAY; + + memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t)); - fseek(f, 0, SEEK_SET); - fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h); + max = 0; - i=0; + f = fopen(filename, "rb"); + if (!f) { + fprintf(stderr, "Failed to open %s for reading !\n", filename); + return NULL; + } + + fseek(f, 0, SEEK_SET); + fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h); + + i=0; + sign='+'; + while (signtmp[i]!='\0') { + if (signtmp[i]=='-') sign='-'; + i++; + } + + fgetc(f); + if (endian1=='M' && endian2=='L') { + bigendian = 1; + } else if (endian2=='M' && endian1=='L') { + bigendian = 0; + } else { + fprintf(stderr, "Bad pgx header, please check input file\n"); + return NULL; + } - sign='+'; + /* initialize image component */ - while (signtmp[i]!='\0') { - if (signtmp[i]=='-') sign='-'; - i++; + cmptparm.x0 = parameters->image_offset_x0; + cmptparm.y0 = parameters->image_offset_y0; + cmptparm.w = !cmptparm.x0 ? (w - 1) * parameters->subsampling_dx + 1 : cmptparm.x0 + (w - 1) * parameters->subsampling_dx + 1; + cmptparm.h = !cmptparm.y0 ? (h - 1) * parameters->subsampling_dy + 1 : cmptparm.y0 + (h - 1) * parameters->subsampling_dy + 1; + + if (sign == '-') { + cmptparm.sgnd = 1; + } else { + cmptparm.sgnd = 0; + } + cmptparm.prec = prec; + cmptparm.bpp = prec; + cmptparm.dx = parameters->subsampling_dx; + cmptparm.dy = parameters->subsampling_dy; + + /* create the image */ + image = opj_image_create(numcomps, &cmptparm, color_space); + if(!image) { + fclose(f); + return NULL; + } + /* set image offset and reference grid */ + image->x0 = cmptparm.x0; + image->y0 = cmptparm.x0; + image->x1 = cmptparm.w; + image->y1 = cmptparm.h; + + /* set image data */ + + comp = &image->comps[0]; + + for (i = 0; i < w * h; i++) { + int v; + if (comp->prec <= 8) { + if (!comp->sgnd) { + v = readuchar(f); + } else { + v = (char) readuchar(f); + } + } else if (comp->prec <= 16) { + if (!comp->sgnd) { + v = readushort(f, bigendian); + } else { + v = (short) readushort(f, bigendian); + } + } else { + if (!comp->sgnd) { + v = readuint(f, bigendian); + } else { + v = (int) readuint(f, bigendian); + } } + if (v > max) + max = v; + comp->data[i] = v; + } + fclose(f); + comp->bpp = int_floorlog2(max) + 1; - fgetc(f); - if (endian1=='M' && endian2=='L') - bigendian = 1; - else if (endian2=='M' && endian1=='L') - bigendian = 0; - else { - fprintf(stderr, "Bad pgx header, please check input file\n"); - return 0; + return image; +} + +int imagetopgx(opj_image_t * image, char *outfile) { + int w, wr, h, hr; + int i, j, compno; + FILE *fdest = NULL; + + for (compno = 0; compno < image->numcomps; compno++) { + opj_image_comp_t *comp = &image->comps[compno]; + char name[256]; + int nbytes = 0; + char *tmp = outfile; + while (*tmp) { + tmp++; } - if (compno == 0) { - img->x0 = Dim[0]; - img->y0 = Dim[1]; - img->x1 = - !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) * - subsampling_dx + 1; - img->y1 = - !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) * - subsampling_dy + 1; - } else { - if (w != img->x1 || h != img->y1) - return 0; + while (*tmp!='.') { + tmp--; } - - if (sign == '-') { - comp->sgnd = 1; + *tmp='\0'; + + if (image->numcomps > 1) { + sprintf(name, "%s-%d.pgx", outfile, compno); } else { - comp->sgnd = 0; + sprintf(name, "%s.pgx", outfile); } - comp->prec = prec; - comp->dx = subsampling_dx; - comp->dy = subsampling_dy; - bandno = 1; - - Y1 = cp.ty0 + bandno * cp.tdy < - img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1; - Y1 -= img->y0; - - sprintf(tmp, "bandtile%d", bandno); /* bandtile file */ - src = fopen(tmp, "wb"); - if (!src) { - fprintf(stderr, "failed to open %s for writing !\n", tmp); + fdest = fopen(name, "wb"); + if (!fdest) { + fprintf(stderr, "ERROR -> failed to open %s for writing\n", name); + return 1; } - for (i = 0; i < w * h; i++) { - int v; - if (i == Y1 * w / subsampling_dy && tdy != -1) { /* bandtile is full */ - fclose(src); - bandno++; - sprintf(tmp, "bandtile%d", bandno); - src = fopen(tmp, "wb"); - if (!src) { - fprintf(stderr, "failed to open %s for writing !\n", tmp); - } - Y1 = cp.ty0 + bandno * cp.tdy < - img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1; - Y1 -= img->y0; - } - if (comp->prec <= 8) { - if (!comp->sgnd) { - v = readuchar(f); - } else { - v = (char) readuchar(f); - } - } else if (comp->prec <= 16) { - if (!comp->sgnd) { - v = readushort(f, bigendian); - } else { - v = (short) readushort(f, bigendian); - } - } else { - if (!comp->sgnd) { - v = readuint(f, bigendian); - } else { - v = (int) readuint(f, bigendian); - } + /* w = int_ceildiv(image->x1 - image->x0, comp->dx); */ + /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor), comp->dx); */ + w = image->comps[compno].w; + wr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor); + + /* h = int_ceildiv(image->y1 - image->y0, comp->dy); */ + /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), comp->dy); */ + h = image->comps[compno].h; + hr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor); + + fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, wr, hr); + if (comp->prec <= 8) { + nbytes = 1; + } else if (comp->prec <= 16) { + nbytes = 2; + } else { + nbytes = 4; + } + for (i = 0; i < wr * hr; i++) { + int v = image->comps[compno].data[i / wr * w + i % wr]; + for (j = nbytes - 1; j >= 0; j--) { + char byte = (char) (v >> (j * 8)); + fwrite(&byte, 1, 1, fdest); } - if (v > max) - max = v; - fprintf(src, "%d ", v); } - fclose(f); - fclose(src); - comp->bpp = int_floorlog2(max) + 1; + fclose(fdest); } - return 1; + + return 0; } /* -->> -->> -->> -->> - PNM IMAGE FORMAT +PNM IMAGE FORMAT - <<-- <<-- <<-- <<-- */ +<<-- <<-- <<-- <<-- */ -int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx, - int subsampling_dy, int Dim[2]) -{ - FILE *f; - FILE *Compo0, *Compo1, *Compo2; - int w, h; - int i; +opj_image_t* pnmtoimage(char *filename, opj_cparameters_t *parameters) { + int subsampling_dx = parameters->subsampling_dx; + int subsampling_dy = parameters->subsampling_dy; + + FILE *f = NULL; + int i, compno, numcomps, w, h; + OPJ_COLOR_SPACE color_space; + opj_image_cmptparm_t cmptparm[3]; /* maximum of 3 components */ + opj_image_t * image = NULL; char value; - char comment[256]; f = fopen(filename, "rb"); if (!f) { - fprintf(stderr, - "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", - filename); + fprintf(stderr, "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", filename); return 0; } @@ -700,214 +854,197 @@ int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx, return 0; value = fgetc(f); - if (value == '2') { - fgetc(f); - if (fgetc(f) == '#') { - fseek(f, 0, SEEK_SET); - fscanf(f, "P2\n"); - fgets(comment, 256, f); - fscanf(f, "%d %d\n255", &w, &h); - } else { - fseek(f, 0, SEEK_SET); - fscanf(f, "P2\n%d %d\n255", &w, &h); - } - - fgetc(f); - img->x0 = Dim[0]; - img->y0 = Dim[1]; - img->x1 = - !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) * - subsampling_dx + 1; - img->y1 = - !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) * - subsampling_dy + 1; - - img->numcomps = 1; - img->color_space = 2; - img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t)); - img->comps[0].prec = 8; - img->comps[0].bpp = 8; - img->comps[0].sgnd = 0; - img->comps[0].dx = subsampling_dx; - img->comps[0].dy = subsampling_dy; - - Compo0 = fopen("Compo0", "wb"); - if (!Compo0) { - fprintf(stderr, - "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); - } - for (i = 0; i < w * h; i++) { - unsigned int l; - fscanf(f, "%d", &l); - fprintf(Compo0, "%c", l); - } - fclose(Compo0); - } else if (value == '5') { - fgetc(f); - if (fgetc(f) == '#') { - fseek(f, 0, SEEK_SET); - fscanf(f, "P5\n"); - fgets(comment, 256, f); - fscanf(f, "%d %d\n255", &w, &h); - } else { - fseek(f, 0, SEEK_SET); - fscanf(f, "P5\n%d %d\n255", &w, &h); - } - - fgetc(f); - img->x0 = Dim[0]; - img->y0 = Dim[1]; - img->x1 = - !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) * - subsampling_dx + 1; - img->y1 = - !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) * - subsampling_dy + 1; - - img->numcomps = 1; - img->color_space = 2; - img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t)); - img->comps[0].prec = 8; - img->comps[0].bpp = 8; - img->comps[0].sgnd = 0; - img->comps[0].dx = subsampling_dx; - img->comps[0].dy = subsampling_dy; - Compo0 = fopen("Compo0", "wb"); - if (!Compo0) { - fprintf(stderr, - "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); - } - for (i = 0; i < w * h; i++) { - unsigned char l; - fread(&l, 1, 1, f); - fwrite(&l, 1, 1, Compo0); - } - fclose(Compo0); - } else if (value == '3') { - fgetc(f); - if (fgetc(f) == '#') { - fseek(f, 0, SEEK_SET); - fscanf(f, "P3\n"); - fgets(comment, 256, f); - fscanf(f, "%d %d\n255", &w, &h); - } else { - fseek(f, 0, SEEK_SET); - fscanf(f, "P3\n%d %d\n255", &w, &h); - } + switch(value) { + case '2': /* greyscale image type */ + case '5': + numcomps = 1; + color_space = CLRSPC_GRAY; + break; + + case '3': /* RGB image type */ + case '6': + numcomps = 3; + color_space = CLRSPC_SRGB; + break; + + default: + fclose(f); + return NULL; + } fgetc(f); - img->x0 = Dim[0]; - img->y0 = Dim[1]; - img->x1 = - !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) * - subsampling_dx + 1; - img->y1 = - !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) * - subsampling_dy + 1; - img->numcomps = 3; - img->color_space = 1; - img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t)); - for (i = 0; i < img->numcomps; i++) { - img->comps[i].prec = 8; - img->comps[i].bpp = 8; - img->comps[i].sgnd = 0; - img->comps[i].dx = subsampling_dx; - img->comps[i].dy = subsampling_dy; - } - Compo0 = fopen("Compo0", "wb"); - if (!Compo0) { - fprintf(stderr, - "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); - } + + /* skip comments */ + while(fgetc(f) == '#') while(fgetc(f) != '\n'); + + fseek(f, -1, SEEK_CUR); + fscanf(f, "%d %d\n255", &w, &h); + fgetc(f); /* */ + + /* initialize image components */ + memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t)); + for(i = 0; i < numcomps; i++) { + cmptparm[i].prec = 8; + cmptparm[i].bpp = 8; + cmptparm[i].sgnd = 0; + cmptparm[i].dx = subsampling_dx; + cmptparm[i].dy = subsampling_dy; + cmptparm[i].w = w; + cmptparm[i].h = h; + } + /* create the image */ + image = opj_image_create(numcomps, &cmptparm[0], color_space); + if(!image) { + fclose(f); + return NULL; + } - Compo1 = fopen("Compo1", "wb"); - if (!Compo1) { - fprintf(stderr, - "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n"); - } + /* set image offset and reference grid */ + image->x0 = parameters->image_offset_x0; + image->y0 = parameters->image_offset_y0; + image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1; + image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1; - Compo2 = fopen("Compo2", "wb"); - if (!Compo2) { - fprintf(stderr, - "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n"); - } + /* set image data */ + if ((value == '2') || (value == '3')) { /* ASCII */ for (i = 0; i < w * h; i++) { - unsigned int r, g, b; - fscanf(f, "%d", &r); - fscanf(f, "%d", &g); - fscanf(f, "%d", &b); - fprintf(Compo0, "%c", r); - fprintf(Compo1, "%c", g); - fprintf(Compo2, "%c", b); + for(compno = 0; compno < numcomps; compno++) { + unsigned int index = 0; + fscanf(f, "%u", &index); + /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */ + image->comps[compno].data[i] = index; + } } - fclose(Compo0); - fclose(Compo1); - fclose(Compo2); - } else if (value == '6') { - fgetc(f); - if (fgetc(f) == '#') { - fseek(f, 0, SEEK_SET); - fscanf(f, "P6\n"); - fgets(comment, 256, f); - fscanf(f, "%d %d\n255", &w, &h); - } else { - fseek(f, 0, SEEK_SET); - fscanf(f, "P6\n%d %d\n255", &w, &h); + } else if ((value == '5') || (value == '6')) { /* BINARY */ + for (i = 0; i < w * h; i++) { + for(compno = 0; compno < numcomps; compno++) { + unsigned char index = 0; + fread(&index, 1, 1, f); + /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */ + image->comps[compno].data[i] = index; + } } + } - fgetc(f); - img->x0 = Dim[0]; - img->y0 = Dim[1]; - img->x1 = - !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) * - subsampling_dx + 1; - img->y1 = - !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) * - subsampling_dy + 1; - img->numcomps = 3; - img->color_space = 1; - img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t)); - for (i = 0; i < img->numcomps; i++) { - img->comps[i].prec = 8; - img->comps[i].bpp = 8; - img->comps[i].sgnd = 0; - img->comps[i].dx = subsampling_dx; - img->comps[i].dy = subsampling_dy; - } - Compo0 = fopen("Compo0", "wb"); - if (!Compo0) { - fprintf(stderr, - "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n"); - } + fclose(f); - Compo1 = fopen("Compo1", "wb"); - if (!Compo1) { - fprintf(stderr, - "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n"); - } + return image; +} + +int imagetopnm(opj_image_t * image, char *outfile) { + int w, wr, wrr, h, hr, hrr, max; + int i, compno; + int adjust; + FILE *fdest = NULL; + char S2; + char *tmp = outfile; - Compo2 = fopen("Compo2", "wb"); - if (!Compo2) { - fprintf(stderr, - "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n"); + while (*tmp) { + tmp++; + } + tmp--; + tmp--; + S2 = *tmp; + + 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 + && S2 !='g' && S2 !='G') { + + fdest = fopen(outfile, "wb"); + if (!fdest) { + fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile); + return 1; } - for (i = 0; i < w * h; i++) { - unsigned char r, g, b; - fread(&r, 1, 1, f); - fread(&g, 1, 1, f); - fread(&b, 1, 1, f); - fwrite(&r, 1, 1, Compo0); - fwrite(&g, 1, 1, Compo1); - fwrite(&b, 1, 1, Compo2); + 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); */ + wr = image->comps[0].w; + wrr = 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); */ + hr = image->comps[0].h; + hrr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor); + + max = image->comps[0].prec > 8 ? 255 : (1 << image->comps[0].prec) - 1; + + image->comps[0].x0 = int_ceildivpow2(image->comps[0].x0 - int_ceildiv(image->x0, image->comps[0].dx), image->comps[0].factor); + image->comps[0].y0 = int_ceildivpow2(image->comps[0].y0 - int_ceildiv(image->y0, image->comps[0].dy), image->comps[0].factor); + + fprintf(fdest, "P6\n%d %d\n%d\n", wrr, hrr, max); + adjust = image->comps[0].prec > 8 ? image->comps[0].prec - 8 : 0; + for (i = 0; i < wrr * hrr; i++) { + int r, g, b; + unsigned char rc,gc,bc; + r = image->comps[0].data[i / wrr * wr + i % wrr]; + r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); + rc = (unsigned char) ((r >> adjust)+((r >> (adjust-1))%2)); + + g = image->comps[1].data[i / wrr * wr + i % wrr]; + g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); + gc = (unsigned char) ((g >> adjust)+((g >> (adjust-1))%2)); + + b = image->comps[2].data[i / wrr * wr + i % wrr]; + b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); + bc = (unsigned char) ((b >> adjust)+((b >> (adjust-1))%2)); + + fprintf(fdest, "%c%c%c", rc, gc, bc); } - fclose(Compo0); - fclose(Compo1); - fclose(Compo2); + fclose(fdest); } else { - return 0; + int ncomp=(S2=='g' || S2=='G')?1:image->numcomps; + if (image->numcomps>ncomp) { + fprintf(stderr,"WARNING -> [PGM files] Only the first component\n"); + fprintf(stderr," is written to the file\n"); + } + for (compno = 0; compno < ncomp; compno++) { + char name[256]; + if (ncomp > 1) { + sprintf(name, "%d.%s", compno, outfile); + } else { + sprintf(name, "%s", outfile); + } + + fdest = fopen(name, "wb"); + if (!fdest) { + fprintf(stderr, "ERROR -> failed to open %s for writing\n", name); + return 1; + } + + w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx); + /* wr = int_ceildiv(int_ceildivpow2(image->x1 - image->x0,image->factor),image->comps[compno].dx); */ + wr = image->comps[compno].w; + wrr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor); + + h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy); + /* hr = int_ceildiv(int_ceildivpow2(image->y1 - image->y0,image->factor), image->comps[compno].dy); */ + hr = image->comps[compno].h; + hrr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor); + + max = image->comps[compno].prec > 8 ? 255 : (1 << image->comps[compno].prec) - 1; + + image->comps[compno].x0 = int_ceildivpow2(image->comps[compno].x0 - int_ceildiv(image->x0, image->comps[compno].dx), image->comps[compno].factor); + image->comps[compno].y0 = int_ceildivpow2(image->comps[compno].y0 - int_ceildiv(image->y0, image->comps[compno].dy), image->comps[compno].factor); + + fprintf(fdest, "P5\n%d %d\n%d\n", wrr, hrr, max); + adjust = image->comps[compno].prec > 8 ? image->comps[compno].prec - 8 : 0; + + for (i = 0; i < wrr * hrr; i++) { + int l; + unsigned char lc; + l = image->comps[compno].data[i / wrr * wr + i % wrr]; + l += (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0); + lc = (unsigned char) ((l >> adjust)+((l >> (adjust-1))%2)); + fprintf(fdest, "%c", lc); + } + fclose(fdest); + } } - fclose(f); - return 1; + + return 0; }