Errors with some BMP file solved

This commit is contained in:
Yannick Verschueren 2004-03-01 14:44:53 +00:00
parent e798d3f348
commit 994f0d518e
1 changed files with 83 additions and 136 deletions

View File

@ -67,26 +67,22 @@ typedef struct {
UINT4 biClrImportant; /* Number of important color (0: ALL) */ UINT4 biClrImportant; /* Number of important color (0: ALL) */
} BITMAPINFOHEADER_t; } BITMAPINFOHEADER_t;
int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx, int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx, int subsampling_dy, int Dim[2])
int subsampling_dy, int Dim[2])
{ {
FILE *IN; FILE *IN;
FILE *Compo0 = NULL, *Compo1 = NULL, *Compo2 = NULL; FILE *Compo0 = NULL, *Compo1 = NULL, *Compo2 = NULL;
BITMAPFILEHEADER_t File_h; BITMAPFILEHEADER_t File_h;
BITMAPINFOHEADER_t Info_h; BITMAPINFOHEADER_t Info_h;
unsigned char *RGB; unsigned char *RGB;
unsigned char *table_R, *table_G, *table_B; unsigned char *table_R, *table_G, *table_B;
int i, w, h; int i, w, h, PAD, type = 0;
int gray_scale = 1, not_end_file = 1, line = 0, col = 0; int gray_scale = 1, not_end_file = 1, line = 0, col = 0;
unsigned char v, v2; unsigned char v, v2;
UINT4 W, H; UINT4 W, H;
IN = fopen(filename, "rb"); IN = fopen(filename, "rb");
if (!IN) { if (!IN) {
fprintf(stderr, fprintf(stderr, "\033[0;33mFailed to open %s for reading !!\033[0;39m\n", filename);
"\033[0;33mFailed to open %s for reading !!\033[0;39m\n",
filename);
return 0; return 0;
} }
@ -176,17 +172,10 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
if (Info_h.biBitCount == 24) { if (Info_h.biBitCount == 24) {
img->x0 = Dim[0]; img->x0 = Dim[0];
img->y0 = Dim[1]; img->y0 = Dim[1];
img->x1 = img->x1 = !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) * subsampling_dx + 1;
!Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - img->y1 = !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) * subsampling_dy + 1;
1) *
subsampling_dx + 1;
img->y1 =
!Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
1) *
subsampling_dy + 1;
img->numcomps = 3; img->numcomps = 3;
img->comps = img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
(j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
for (i = 0; i < img->numcomps; i++) { for (i = 0; i < img->numcomps; i++) {
img->comps[i].prec = 8; img->comps[i].prec = 8;
img->comps[i].bpp = 8; img->comps[i].bpp = 8;
@ -196,18 +185,15 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
} }
Compo0 = fopen("Compo0", "wb"); Compo0 = fopen("Compo0", "wb");
if (!Compo0) { if (!Compo0) {
fprintf(stderr, fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
"\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
} }
Compo1 = fopen("Compo1", "wb"); Compo1 = fopen("Compo1", "wb");
if (!Compo1) { if (!Compo1) {
fprintf(stderr, fprintf(stderr, "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
"\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
} }
Compo2 = fopen("Compo2", "wb"); Compo2 = fopen("Compo2", "wb");
if (!Compo2) { if (!Compo2) {
fprintf(stderr, fprintf(stderr, "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
"\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
} }
/* Place the cursor at the beginning of the image information */ /* Place the cursor at the beginning of the image information */
@ -216,30 +202,39 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
W = Info_h.biWidth; W = Info_h.biWidth;
H = Info_h.biHeight; H = Info_h.biHeight;
if (Info_h.biWidth % 2)
W++;
RGB = (unsigned char *) malloc(3 * W * H * sizeof(unsigned char)); PAD = 4 - (3 * W) % 4;
fread(RGB, sizeof(unsigned char), 3 * W * H, IN); RGB = (unsigned char *) malloc((3 * W + PAD) * H * sizeof(unsigned char));
for (i = 0; i < W * H; i++) {
unsigned char R, G, B;
B = fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
RGB[3 * W * H - ((i * 3) / (3 * W) + 1) * 3 * W +
(i * 3) % (W * 3)]; for (i = 0; i < (3 * W + PAD) * H; i++)
G = {
RGB[3 * W * H - ((i * 3 + 1) / (3 * W) + 1) * 3 * W + unsigned char elmt;
(i * 3 + 1) % (W * 3)]; int Wp = 3 * W + PAD;
R =
RGB[3 * W * H - ((i * 3 + 2) / (3 * W) + 1) * 3 * W + elmt = RGB[(H - (i/Wp + 1)) * Wp + i % Wp];
(i * 3 + 2) % (W * 3)]; if ((i % Wp) < (3 * W))
if ((i % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) { {
fprintf(Compo0, "%c", R); switch (type)
fprintf(Compo1, "%c", G); {
fprintf(Compo2, "%c", B); 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;
} }
} }
}
fclose(Compo0); fclose(Compo0);
fclose(Compo1); fclose(Compo1);
fclose(Compo2); fclose(Compo2);
@ -247,14 +242,8 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
} else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) { } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 0) {
img->x0 = Dim[0]; img->x0 = Dim[0];
img->y0 = Dim[1]; img->y0 = Dim[1];
img->x1 = img->x1 = !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) * subsampling_dx + 1;
!Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - img->y1 = !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) * subsampling_dy + 1;
1) *
subsampling_dx + 1;
img->y1 =
!Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
1) *
subsampling_dy + 1;
table_R = (unsigned char *) malloc(256 * sizeof(unsigned char)); table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
table_G = (unsigned char *) malloc(256 * sizeof(unsigned char)); table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
@ -265,8 +254,7 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
table_G[i] = getc(IN); table_G[i] = getc(IN);
table_R[i] = getc(IN); table_R[i] = getc(IN);
getc(IN); getc(IN);
if (table_R[i] != table_G[i] && table_R[i] != table_B[i] if (table_R[i] != table_G[i] && table_R[i] != table_B[i] && table_G[i] != table_B[i])
&& table_G[i] != table_B[i])
gray_scale = 0; gray_scale = 0;
} }
@ -284,8 +272,7 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
fread(RGB, sizeof(unsigned char), W * H, IN); fread(RGB, sizeof(unsigned char), W * H, IN);
if (gray_scale) { if (gray_scale) {
img->numcomps = 1; img->numcomps = 1;
img->comps = img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
(j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
img->comps[0].prec = 8; img->comps[0].prec = 8;
img->comps[0].bpp = 8; img->comps[0].bpp = 8;
img->comps[0].sgnd = 0; img->comps[0].sgnd = 0;
@ -293,20 +280,16 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
img->comps[0].dy = subsampling_dy; img->comps[0].dy = subsampling_dy;
Compo0 = fopen("Compo0", "wb"); Compo0 = fopen("Compo0", "wb");
if (!Compo0) { if (!Compo0) {
fprintf(stderr, fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
"\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
} }
for (i = 0; i < W * H; i++) { for (i = 0; i < W * H; i++) {
if ((i % W < W - 1 && Info_h.biWidth % 2) if ((i % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2))
|| !(Info_h.biWidth % 2)) fprintf(Compo0, "%c", table_R[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]);
fprintf(Compo0, "%c",
table_R[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]);
} }
fclose(Compo0); fclose(Compo0);
} else { } else {
img->numcomps = 3; img->numcomps = 3;
img->comps = img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
(j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
for (i = 0; i < img->numcomps; i++) { for (i = 0; i < img->numcomps; i++) {
img->comps[i].prec = 8; img->comps[i].prec = 8;
img->comps[i].bpp = 8; img->comps[i].bpp = 8;
@ -317,29 +300,22 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
Compo0 = fopen("Compo0", "wb"); Compo0 = fopen("Compo0", "wb");
if (!Compo0) { if (!Compo0) {
fprintf(stderr, fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
"\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
} }
Compo1 = fopen("Compo1", "wb"); Compo1 = fopen("Compo1", "wb");
if (!Compo1) { if (!Compo1) {
fprintf(stderr, fprintf(stderr, "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
"\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
} }
Compo2 = fopen("Compo2", "wb"); Compo2 = fopen("Compo2", "wb");
if (!Compo2) { if (!Compo2) {
fprintf(stderr, fprintf(stderr, "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
"\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
} }
for (i = 0; i < W * H; i++) { for (i = 0; i < W * H; i++) {
if ((i % W < W - 1 && Info_h.biWidth % 2) if ((i % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
|| !(Info_h.biWidth % 2)) { fprintf(Compo0, "%c", table_R[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]);
fprintf(Compo0, "%c", fprintf(Compo1, "%c", table_G[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]);
table_R[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]); fprintf(Compo2, "%c", table_B[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]);
fprintf(Compo1, "%c",
table_G[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]);
fprintf(Compo2, "%c",
table_B[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]);
} }
} }
@ -352,16 +328,8 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
} else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) { } else if (Info_h.biBitCount == 8 && Info_h.biCompression == 1) {
img->x0 = Dim[0]; img->x0 = Dim[0];
img->y0 = Dim[1]; img->y0 = Dim[1];
img->x1 = img->x1 = !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - 1) * subsampling_dx + 1;
!Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - img->y1 = !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - 1) * subsampling_dy + 1;
1) *
subsampling_dx + 1;
img->y1 =
!Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
1) *
subsampling_dy + 1;
table_R = (unsigned char *) malloc(256 * sizeof(unsigned char)); table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
table_G = (unsigned char *) malloc(256 * sizeof(unsigned char)); table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
@ -372,8 +340,7 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
table_G[i] = getc(IN); table_G[i] = getc(IN);
table_R[i] = getc(IN); table_R[i] = getc(IN);
getc(IN); getc(IN);
if (table_R[i] != table_G[i] && table_R[i] != table_B[i] if (table_R[i] != table_G[i] && table_R[i] != table_B[i] && table_G[i] != table_B[i])
&& table_G[i] != table_B[i])
gray_scale = 0; gray_scale = 0;
} }
@ -391,13 +358,11 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
img->comps[0].dy = subsampling_dy; img->comps[0].dy = subsampling_dy;
Compo0 = fopen("Compo0", "wb"); Compo0 = fopen("Compo0", "wb");
if (!Compo0) { if (!Compo0) {
fprintf(stderr, fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
"\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
} }
} else { } else {
img->numcomps = 3; img->numcomps = 3;
img->comps = img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
(j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
for (i = 0; i < img->numcomps; i++) { for (i = 0; i < img->numcomps; i++) {
img->comps[i].prec = 8; img->comps[i].prec = 8;
img->comps[i].bpp = 8; img->comps[i].bpp = 8;
@ -407,24 +372,19 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
} }
Compo0 = fopen("Compo0", "wb"); Compo0 = fopen("Compo0", "wb");
if (!Compo0) { if (!Compo0) {
fprintf(stderr, fprintf(stderr, "\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
"\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
} }
Compo1 = fopen("Compo1", "wb"); Compo1 = fopen("Compo1", "wb");
if (!Compo1) { if (!Compo1) {
fprintf(stderr, fprintf(stderr, "\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
"\033[0;33mFailed to open Compo1 for writing !\033[0;39m\n");
} }
Compo2 = fopen("Compo2", "wb"); Compo2 = fopen("Compo2", "wb");
if (!Compo2) { if (!Compo2) {
fprintf(stderr, fprintf(stderr, "\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
"\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
} }
} }
RGB = RGB = (unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight * sizeof(unsigned char));
(unsigned char *) malloc(Info_h.biWidth * Info_h.biHeight *
sizeof(unsigned char));
while (not_end_file) { while (not_end_file) {
v = getc(IN); v = getc(IN);
@ -463,26 +423,14 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
if (gray_scale) { if (gray_scale) {
for (line = 0; line < Info_h.biHeight; line++) for (line = 0; line < Info_h.biHeight; line++)
for (col = 0; col < Info_h.biWidth; col++) for (col = 0; col < Info_h.biWidth; col++)
fprintf(Compo0, "%c", fprintf(Compo0, "%c", table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]]);
table_R[(int)
RGB[(Info_h.biHeight - line -
1) * Info_h.biWidth + col]]);
fclose(Compo0); fclose(Compo0);
} else { } else {
for (line = 0; line < Info_h.biHeight; line++) for (line = 0; line < Info_h.biHeight; line++)
for (col = 0; col < Info_h.biWidth; col++) { for (col = 0; col < Info_h.biWidth; col++) {
fprintf(Compo0, "%c", fprintf(Compo0, "%c", table_R[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]]);
table_R[(int) fprintf(Compo1, "%c", table_G[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]]);
RGB[(Info_h.biHeight - line - fprintf(Compo2, "%c", table_B[(int)RGB[(Info_h.biHeight - line - 1) * Info_h.biWidth + col]]);
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(Compo0);
fclose(Compo1); fclose(Compo1);
@ -490,8 +438,7 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
} }
free(RGB); free(RGB);
} else } else
printf fprintf(stderr,"Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n",
("Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n",
Info_h.biBitCount); Info_h.biBitCount);
fclose(IN); fclose(IN);