- /* 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));
-
- if ( fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN) != (3 * W + PAD) * H )
- {
- free(RGB);
- opj_image_destroy(image);
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
- return NULL;
- }
-
- index = 0;
-
- for(y = 0; y < H; y++)
- {
- unsigned char *scanline = RGB + (3 * (unsigned int)W + PAD) * ((unsigned int)H - 1 - (unsigned int)y);
- for(x = 0; x < 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++;
- }
- }
- free(RGB);
- }/* if (Info_h.biBitCount == 24) */
- else
- if (Info_h.biBitCount == 8 && Info_h.biCompression == 0)/*RGB */
- {
- if(Info_h.biClrUsed == 0) Info_h.biClrUsed = 256;
- else
- if(Info_h.biClrUsed > 256) Info_h.biClrUsed = 256;
-
- 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));
-
- has_color = 0;
- for (j = 0; j < Info_h.biClrUsed; j++)
- {
- table_B[j] = (unsigned char)getc(IN);
- table_G[j] = (unsigned char)getc(IN);
- table_R[j] = (unsigned char)getc(IN);
- getc(IN);
- has_color +=
- !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
- }
- if(has_color) gray_scale = 0;
-
- /* Place the cursor at the beginning of the image information */
- fseek(IN, 0, SEEK_SET);
- fseek(IN, (long)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 ? OPJ_CLRSPC_GRAY : OPJ_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 = (OPJ_UINT32)subsampling_dx;
- cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
- cmptparm[i].w = (OPJ_UINT32)w;
- cmptparm[i].h = (OPJ_UINT32)h;
- }
- /* create the image */
- image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
- if(!image)
- {
- fclose(IN);
- free(table_R); free(table_G); free(table_B);
- return NULL;
- }
-
- /* set image offset and reference grid */
- image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
- image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
- image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 : image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
- image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 : image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
-
- /* set image data */
-
- RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
-
- if ( fread(RGB, sizeof(unsigned char), W * H, IN) != W * H )
- {
- free(table_R);
- free(table_G);
- free(table_B);
- free(RGB);
- opj_image_destroy(image);
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
- return NULL;
- }
- if (gray_scale)
- {
- index = 0;
- for (j = 0; j < W * H; j++)
- {
- 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))
- {
- 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++;
- }
- }
- }
- free(RGB);
- free(table_R);
- free(table_G);
- free(table_B);
- }/* RGB8 */
- else
- if (Info_h.biBitCount == 8 && Info_h.biCompression == 1)/*RLE8*/
- {
- unsigned char *pix, *beyond;
- int *gray, *red, *green, *blue;
- unsigned int max;
- int c, c1;
- unsigned char uc;
-
- if (Info_h.biClrUsed == 0)
- Info_h.biClrUsed = 256;
- else if (Info_h.biClrUsed > 256)
- Info_h.biClrUsed = 256;
-
- 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));
-
- has_color = 0;
- for (j = 0; j < Info_h.biClrUsed; j++)
- {
- table_B[j] = (unsigned char)getc(IN);
- table_G[j] = (unsigned char)getc(IN);
- table_R[j] = (unsigned char)getc(IN);
- getc(IN);
- has_color += !(table_R[j] == table_G[j] && table_R[j] == table_B[j]);
- }
-
- if (has_color)
- gray_scale = 0;
-
- numcomps = gray_scale ? 1 : 3;
- color_space = gray_scale ? OPJ_CLRSPC_GRAY : OPJ_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 = (OPJ_UINT32)subsampling_dx;
- cmptparm[i].dy = (OPJ_UINT32)subsampling_dy;
- cmptparm[i].w = (OPJ_UINT32)w;
- cmptparm[i].h = (OPJ_UINT32)h;
- }
- /* create the image */
- image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
- if (!image)
- {
- fclose(IN);
- free(table_R);
- free(table_G);
- free(table_B);
- return NULL;
- }
-
- /* set image offset and reference grid */
- image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
- image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
- image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 : image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
- image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 : image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
-
- /* set image data */
-
- /* Place the cursor at the beginning of the image information */
- fseek(IN, 0, SEEK_SET);
- fseek(IN, (long)File_h.bfOffBits, SEEK_SET);
-
- W = Info_h.biWidth;
- H = Info_h.biHeight;
- RGB = (unsigned char *) calloc(1, W * H * sizeof(unsigned char));
- beyond = RGB + W * H;
- pix = beyond - W;
- x = y = 0;
-
- while (y < H)
- {
- c = getc(IN);
-
- if (c)
- {
- c1 = getc(IN);
-
- for (i = 0; i < c && x < W && pix < beyond; i++, x++, pix++)
- *pix = (unsigned char)c1;
- }
- else
- {
- c = getc(IN);
-
- if (c == 0x00) /* EOL */
- {
- x = 0;
- ++y;
- pix = RGB + x + (H - y - 1) * W;
- }
- else if (c == 0x01) /* EOP */
- break;
- else if (c == 0x02) /* MOVE by dxdy */
- {
- c = getc(IN);
- x += (unsigned int)c;
- c = getc(IN);
- y += (unsigned int)c;
- pix = RGB + (H - y - 1) * W + x;
- }
- else /* 03 .. 255 */
- {
- i = 0;
- for (; i < c && x < W && pix < beyond; i++, x++, pix++)
- {
- c1 = getc(IN);
- *pix = (unsigned char)c1;
- }
- if (c & 1) /* skip padding byte */
- getc(IN);
- }
- }
- }/* while() */
-
- if (gray_scale)
- {
- gray = image->comps[0].data;
- pix = RGB;
- max = W * H;
-
- while (max--)
- {
- uc = *pix++;
-
- *gray++ = table_R[uc];
- }
- }
- else
- {
- /*int *red, *green, *blue;*/
-
- red = image->comps[0].data;
- green = image->comps[1].data;
- blue = image->comps[2].data;
- pix = RGB;
- max = W * H;
-
- while (max--)
- {
- uc = *pix++;
-
- *red++ = table_R[uc];
- *green++ = table_G[uc];
- *blue++ = table_B[uc];
- }
- }
- free(RGB);
- free(table_R);
- free(table_G);
- free(table_B);
- }/* RLE8 */
- 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, const char *outfile) {
- int w, h;
- int i, pad;
- FILE *fdest = NULL;
- int adjustR, adjustG, adjustB;
-
- if (image->comps[0].prec < 8) {
- fprintf(stderr, "Unsupported number of components: %d\n", image->comps[0].prec);
- return 1;
- }
- 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)image->comps[0].w;
- h = (int)image->comps[0].h;
-
- fprintf(fdest, "BM");
-
- /* FILE HEADER */
- /* ------------- */
- fprintf(fdest, "%c%c%c%c",
- (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
- (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
- (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
- (unsigned char) ((h * w * 3 + 3 * h * (w % 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) ((w) & 0xff),
- (unsigned char) ((w) >> 8) & 0xff,
- (unsigned char) ((w) >> 16) & 0xff,
- (unsigned char) ((w) >> 24) & 0xff);
- fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
- (unsigned char) ((h) >> 8) & 0xff,
- (unsigned char) ((h) >> 16) & 0xff,
- (unsigned char) ((h) >> 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 * h * w + 3 * h * (w % 2)) & 0xff,
- (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
- (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
- (unsigned char) ((h * w * 3 + 3 * h * (w % 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);
-
- if (image->comps[0].prec > 8) {
- adjustR = (int)image->comps[0].prec - 8;
- printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
- }
- else
- adjustR = 0;
- if (image->comps[1].prec > 8) {
- adjustG = (int)image->comps[1].prec - 8;
- printf("BMP CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
- }
- else
- adjustG = 0;
- if (image->comps[2].prec > 8) {
- adjustB = (int)image->comps[2].prec - 8;
- printf("BMP CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
- }
- else
- adjustB = 0;
-
- for (i = 0; i < w * h; i++) {
- unsigned char rc, gc, bc;
- int r, g, b;
-
- r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
- r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
- r = ((r >> adjustR)+((r >> (adjustR-1))%2));
- if(r > 255) r = 255; else if(r < 0) r = 0;
- rc = (unsigned char)r;
-
- g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
- g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
- g = ((g >> adjustG)+((g >> (adjustG-1))%2));
- if(g > 255) g = 255; else if(g < 0) g = 0;
- gc = (unsigned char)g;
-
- b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
- b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
- b = ((b >> adjustB)+((b >> (adjustB-1))%2));
- if(b > 255) b = 255; else if(b < 0) b = 0;
- bc = (unsigned char)b;
-
- fprintf(fdest, "%c%c%c", bc, gc, rc);
-
- if ((i + 1) % w == 0) {
- for (pad = (3 * w) % 4 ? 4 - (3 * w) % 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)image->comps[0].w;
- h = (int)image->comps[0].h;
-
- fprintf(fdest, "BM");
-
- /* FILE HEADER */
- /* ------------- */
- fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
- (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
- (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
- (unsigned char) ((h * w + 54 + 1024 + w * (w % 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) ((w) & 0xff),
- (unsigned char) ((w) >> 8) & 0xff,
- (unsigned char) ((w) >> 16) & 0xff,
- (unsigned char) ((w) >> 24) & 0xff);
- fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
- (unsigned char) ((h) >> 8) & 0xff,
- (unsigned char) ((h) >> 16) & 0xff,
- (unsigned char) ((h) >> 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) (h * w + h * (w % 2)) & 0xff,
- (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
- (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
- (unsigned char) ((h * w + h * (w % 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);
-
- if (image->comps[0].prec > 8) {
- adjustR = (int)image->comps[0].prec - 8;
- printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
- }else
- adjustR = 0;
-
- for (i = 0; i < 256; i++) {
- fprintf(fdest, "%c%c%c%c", i, i, i, 0);
- }
-
- for (i = 0; i < w * h; i++) {
- int r;
-
- r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
- r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
- r = ((r >> adjustR)+((r >> (adjustR-1))%2));
- if(r > 255) r = 255; else if(r < 0) r = 0;
-
- fprintf(fdest, "%c", (unsigned char)r);
-
- if ((i + 1) % w == 0) {
- for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) /* ADD */
- fprintf(fdest, "%c", 0);
- }
- }
- fclose(fdest);
- }
-
- return 0;
-}
-
-/* -->> -->> -->> -->>
-
-PGX IMAGE FORMAT
-
-<<-- <<-- <<-- <<-- */
-
-
-static unsigned char readuchar(FILE * f)
-{
- unsigned char c1;
- if ( !fread(&c1, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
- return 0;
- }
- return c1;
-}
-
-static unsigned short readushort(FILE * f, int bigendian)
-{
- unsigned char c1, c2;
- if ( !fread(&c1, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
- return 0;
- }
- if ( !fread(&c2, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
- return 0;
- }
- if (bigendian)
- return (unsigned short)((c1 << 8) + c2);
- else
- return (unsigned short)((c2 << 8) + c1);
-}
-
-static unsigned int readuint(FILE * f, int bigendian)
-{
- unsigned char c1, c2, c3, c4;
- if ( !fread(&c1, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
- return 0;
- }
- if ( !fread(&c2, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
- return 0;
- }
- if ( !fread(&c3, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
- return 0;
- }
- if ( !fread(&c4, 1, 1, f) )
- {
- fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
- return 0;
- }
- if (bigendian)
- return (unsigned int)(c1 << 24) + (unsigned int)(c2 << 16) + (unsigned int)(c3 << 8) + c4;
- else
- return (unsigned int)(c4 << 24) + (unsigned int)(c3 << 16) + (unsigned int)(c2 << 8) + c1;
-}
-
-opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
- FILE *f = NULL;
- int w, h, prec;
- int i, numcomps, max;
- OPJ_COLOR_SPACE color_space;
- opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
- opj_image_t * image = NULL;
- int adjustS, ushift, dshift, force8;
-
- char endian1,endian2,sign;
- char signtmp[32];
-
- char temp[32];
- int bigendian;
- opj_image_comp_t *comp = NULL;
-
- numcomps = 1;
- color_space = OPJ_CLRSPC_GRAY;
-
- memset(&cmptparm, 0, sizeof(opj_image_cmptparm_t));