X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=applications%2Fcodec%2Fconvert.c;h=73ed9d09524760e97aa867d96607763ec37e620a;hb=c9ae2a5fbcdf8eb2325fe7fdcffbc64879e5c105;hp=d9a45b9aa370b931025cccd3a2f6f2536a0acd16;hpb=6aaaa3eae28d82e3a2c34e0bd57d027be0685869;p=openjpeg.git diff --git a/applications/codec/convert.c b/applications/codec/convert.c index d9a45b9a..73ed9d09 100644 --- a/applications/codec/convert.c +++ b/applications/codec/convert.c @@ -67,9 +67,9 @@ static int int_floorlog2(int a) { <<-- <<-- <<-- <<-- */ -// TGA header definition. -#pragma pack(push,1) // Pack structure byte aligned -typedef struct tga_header +#ifdef INFORMATION_ONLY +/* TGA header definition. */ +struct tga_header { unsigned char id_length; /* Image id field length */ unsigned char colour_map_type; /* Colour map type */ @@ -89,47 +89,87 @@ typedef struct tga_header unsigned short image_height; /* Image height */ unsigned char pixel_depth; /* Pixel depth */ unsigned char image_desc; /* Image descriptor */ -} tga_header; -#pragma pack(pop) // Return to normal structure packing alignment. +}; +#endif /* INFORMATION_ONLY */ + +static unsigned short get_ushort(unsigned short val) { + +#ifdef ORDER_BIGENDIAN + return( ((val & 0xff) << 8) + (val >> 8) ); +#else + return( val ); +#endif + +} + +#define TGA_HEADER_SIZE 18 int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, unsigned int *width, unsigned int *height, int *flip_image) { int palette_size; - tga_header tga ; + unsigned char *tga ; + unsigned char id_len, cmap_type, image_type; + unsigned char pixel_depth, image_desc; + unsigned short cmap_index, cmap_len, cmap_entry_size; + unsigned short x_origin, y_origin, image_w, image_h; if (!bits_per_pixel || !width || !height || !flip_image) return 0; - - // Read TGA header - fread((unsigned char*)&tga, sizeof(tga_header), 1, fp); + tga = (unsigned char*)malloc(18); - *bits_per_pixel = tga.pixel_depth; - - *width = tga.image_width; - *height = tga.image_height ; + if ( fread(tga, TGA_HEADER_SIZE, 1, fp) != 1 ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + return 0 ; + } + id_len = (unsigned char)tga[0]; + cmap_type = (unsigned char)tga[1]; + image_type = (unsigned char)tga[2]; + cmap_index = get_ushort(*(unsigned short*)(&tga[3])); + cmap_len = get_ushort(*(unsigned short*)(&tga[5])); + cmap_entry_size = (unsigned char)tga[7]; + + + x_origin = get_ushort(*(unsigned short*)(&tga[8])); + y_origin = get_ushort(*(unsigned short*)(&tga[10])); + image_w = get_ushort(*(unsigned short*)(&tga[12])); + image_h = get_ushort(*(unsigned short*)(&tga[14])); + pixel_depth = (unsigned char)tga[16]; + image_desc = (unsigned char)tga[17]; + + free(tga); + + *bits_per_pixel = (unsigned int)pixel_depth; + *width = (unsigned int)image_w; + *height = (unsigned int)image_h; - // Ignore tga identifier, if present ... - if (tga.id_length) + /* Ignore tga identifier, if present ... */ + if (id_len) { - unsigned char *id = (unsigned char *) malloc(tga.id_length); - fread(id, tga.id_length, 1, fp); + unsigned char *id = (unsigned char *) malloc(id_len); + if ( !fread(id, id_len, 1, fp) ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + free(id); + return 0 ; + } free(id); } - // Test for compressed formats ... not yet supported ... + /* Test for compressed formats ... not yet supported ... // Note :- 9 - RLE encoded palettized. - // 10 - RLE encoded RGB. - if (tga.image_type > 8) + // 10 - RLE encoded RGB. */ + if (image_type > 8) { fprintf(stderr, "Sorry, compressed tga files are not currently supported.\n"); return 0 ; } - *flip_image = !(tga.image_desc & 32); + *flip_image = !(image_desc & 32); - // Palettized formats are not yet supported, skip over the palette, if present ... - palette_size = tga.colour_map_length * (tga.colour_map_entry_size/8); + /* Palettized formats are not yet supported, skip over the palette, if present ... */ + palette_size = cmap_len * (cmap_entry_size/8); if (palette_size>0) { @@ -140,28 +180,58 @@ int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, } int tga_writeheader(FILE *fp, int bits_per_pixel, int width, int height, - bool flip_image) + opj_bool flip_image) { - tga_header tga; + unsigned short image_w, image_h, us0; + unsigned char uc0, image_type; + unsigned char pixel_depth, image_desc; if (!bits_per_pixel || !width || !height) return 0; - memset(&tga, 0, sizeof(tga_header)); + pixel_depth = 0; - tga.pixel_depth = bits_per_pixel; - tga.image_width = width; - tga.image_height = height; - tga.image_type = 2; // Uncompressed. - tga.image_desc = 8; // 8 bits per component. + if ( bits_per_pixel < 256 ) + pixel_depth = (unsigned char)bits_per_pixel; + else{ + fprintf(stderr,"ERROR: Wrong bits per pixel inside tga_header"); + return 0; + } + uc0 = 0; - if (flip_image) - tga.image_desc |= 32; + if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* id_length */ + if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_type */ + + image_type = 2; /* Uncompressed. */ + if(fwrite(&image_type, 1, 1, fp) != 1) goto fails; + + us0 = 0; + if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_index */ + if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* colour_map_length */ + if(fwrite(&uc0, 1, 1, fp) != 1) goto fails; /* colour_map_entry_size */ - // Write TGA header - fwrite((unsigned char*)&tga, sizeof(tga_header), 1, fp); + if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* x_origin */ + if(fwrite(&us0, 2, 1, fp) != 1) goto fails; /* y_origin */ + + image_w = (unsigned short)width; + image_h = (unsigned short) height; + + if(fwrite(&image_w, 2, 1, fp) != 1) goto fails; + if(fwrite(&image_h, 2, 1, fp) != 1) goto fails; + + if(fwrite(&pixel_depth, 1, 1, fp) != 1) goto fails; + + image_desc = 8; /* 8 bits per component. */ + + if (flip_image) + image_desc |= 32; + if(fwrite(&image_desc, 1, 1, fp) != 1) goto fails; return 1; + +fails: + fputs("\nwrite_tgaheader: write ERROR\n", stderr); + return 0; } opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) { @@ -173,8 +243,8 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) { opj_image_cmptparm_t cmptparm[4]; /* maximum 4 components */ int numcomps; OPJ_COLOR_SPACE color_space; - bool mono ; - bool save_alpha; + opj_bool mono ; + opj_bool save_alpha; int subsampling_dx, subsampling_dy; int i; @@ -187,15 +257,15 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) { if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image)) return NULL; - // We currently only support 24 & 32 bit tga's ... + /* We currently only support 24 & 32 bit tga's ... */ if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32))) return NULL; /* initialize image components */ memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t)); - mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16); // Mono with & without alpha. - save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); // Mono with alpha, or RGB with alpha + mono = (pixel_bit_depth == 8) || (pixel_bit_depth == 16); /* Mono with & without alpha. */ + save_alpha = (pixel_bit_depth == 16) || (pixel_bit_depth == 32); /* Mono with alpha, or RGB with alpha */ if (mono) { color_space = CLRSPC_GRAY; @@ -246,9 +316,25 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) { for (x=0;xcomps[0].data[index]=r; image->comps[1].data[index]=g; @@ -261,10 +347,30 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) { for (x=0;xcomps[0].data[index]=r; image->comps[1].data[index]=g; @@ -282,13 +388,14 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) { int imagetotga(opj_image_t * image, const char *outfile) { int width, height, bpp, x, y; - bool write_alpha; - int i; + opj_bool write_alpha; + int i, adjustR, adjustG, adjustB; unsigned int alpha_channel; float r,g,b,a; unsigned char value; float scale; FILE *fdest; + size_t res; fdest = fopen(outfile, "wb"); if (!fdest) { @@ -308,47 +415,67 @@ int imagetotga(opj_image_t * image, const char *outfile) { width = image->comps[0].w; height = image->comps[0].h; - // Mono with alpha, or RGB with alpha. + /* Mono with alpha, or RGB with alpha. */ write_alpha = (image->numcomps==2) || (image->numcomps==4); - // Write TGA header + /* Write TGA header */ bpp = write_alpha ? 32 : 24; - if (!tga_writeheader(fdest, bpp, width , height, true)) + if (!tga_writeheader(fdest, bpp, width , height, OPJ_TRUE)) return 1; alpha_channel = image->numcomps-1; scale = 255.0f / (float)((1<comps[0].prec)-1); + adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); + adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); + adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); + for (y=0; y < height; y++) { unsigned int index=y*width; for (x=0; x < width; x++, index++) { - r = (float)(image->comps[0].data[index]); + r = (float)(image->comps[0].data[index] + adjustR); if (image->numcomps>2) { - g = (float)(image->comps[1].data[index]); - b = (float)(image->comps[2].data[index]); + g = (float)(image->comps[1].data[index] + adjustG); + b = (float)(image->comps[2].data[index] + adjustB); } - else {// Greyscale ... + else {/* Greyscale ... */ g = r; b = r; } - // TGA format writes BGR ... + /* TGA format writes BGR ... */ value = (unsigned char)(b*scale); - fwrite(&value,1,1,fdest); + res = fwrite(&value,1,1,fdest); + if( res < 1 ) { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + return 1; + } value = (unsigned char)(g*scale); - fwrite(&value,1,1,fdest); + res = fwrite(&value,1,1,fdest); + if( res < 1 ) { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + return 1; + } value = (unsigned char)(r*scale); - fwrite(&value,1,1,fdest); + res = fwrite(&value,1,1,fdest); + if( res < 1 ) { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + return 1; + } if (write_alpha) { a = (float)(image->comps[alpha_channel].data[index]); value = (unsigned char)(a*scale); - fwrite(&value,1,1,fdest); + res = fwrite(&value,1,1,fdest); + if( res < 1 ) { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + return 1; + } } } } @@ -390,7 +517,8 @@ typedef struct { DWORD biClrImportant; /* Number of important color (0: ALL) */ } BITMAPINFOHEADER_t; -opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) { +opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) +{ int subsampling_dx = parameters->subsampling_dx; int subsampling_dy = parameters->subsampling_dy; @@ -407,355 +535,456 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) { unsigned int j, PAD = 0; int x, y, index; - int gray_scale = 1, not_end_file = 1; - - unsigned int line = 0, col = 0; - unsigned char v, v2; + int gray_scale = 1; + int has_color; DWORD W, H; IN = fopen(filename, "rb"); - if (!IN) { - fprintf(stderr, "Failed to open %s for reading !!\n", filename); - return 0; - } + if (!IN) + { + fprintf(stderr, "Failed to open %s for reading !!\n", filename); + return NULL; + } 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; - } else { + if (File_h.bfType != 19778) + { + fprintf(stderr,"Error, not a BMP file!\n"); + fclose(IN); + return NULL; + } /* FILE HEADER */ /* ------------- */ - File_h.bfSize = getc(IN); - File_h.bfSize = (getc(IN) << 8) + File_h.bfSize; - File_h.bfSize = (getc(IN) << 16) + File_h.bfSize; - File_h.bfSize = (getc(IN) << 24) + File_h.bfSize; + File_h.bfSize = getc(IN); + File_h.bfSize = (getc(IN) << 8) + File_h.bfSize; + File_h.bfSize = (getc(IN) << 16) + File_h.bfSize; + File_h.bfSize = (getc(IN) << 24) + File_h.bfSize; - File_h.bfReserved1 = getc(IN); - File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1; + File_h.bfReserved1 = getc(IN); + File_h.bfReserved1 = (getc(IN) << 8) + File_h.bfReserved1; - File_h.bfReserved2 = getc(IN); - File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2; + File_h.bfReserved2 = getc(IN); + File_h.bfReserved2 = (getc(IN) << 8) + File_h.bfReserved2; - File_h.bfOffBits = getc(IN); - File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits; - File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits; - File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits; + File_h.bfOffBits = getc(IN); + File_h.bfOffBits = (getc(IN) << 8) + File_h.bfOffBits; + File_h.bfOffBits = (getc(IN) << 16) + File_h.bfOffBits; + File_h.bfOffBits = (getc(IN) << 24) + File_h.bfOffBits; /* INFO HEADER */ /* ------------- */ - Info_h.biSize = getc(IN); - Info_h.biSize = (getc(IN) << 8) + Info_h.biSize; - Info_h.biSize = (getc(IN) << 16) + Info_h.biSize; - Info_h.biSize = (getc(IN) << 24) + Info_h.biSize; - - Info_h.biWidth = getc(IN); - Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth; - Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth; - Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth; - w = Info_h.biWidth; - - Info_h.biHeight = getc(IN); - Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight; - Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight; - Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight; - h = Info_h.biHeight; - - Info_h.biPlanes = getc(IN); - Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes; - - Info_h.biBitCount = getc(IN); - Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount; - - Info_h.biCompression = getc(IN); - Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression; - Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression; - Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression; - - Info_h.biSizeImage = getc(IN); - Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage; - Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage; - Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage; - - Info_h.biXpelsPerMeter = getc(IN); - Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter; - Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter; - Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter; - - Info_h.biYpelsPerMeter = getc(IN); - Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter; - Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter; - Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter; - - Info_h.biClrUsed = getc(IN); - Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed; - Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed; - Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed; - - Info_h.biClrImportant = getc(IN); - Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant; - Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant; - Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant; + Info_h.biSize = getc(IN); + Info_h.biSize = (getc(IN) << 8) + Info_h.biSize; + Info_h.biSize = (getc(IN) << 16) + Info_h.biSize; + Info_h.biSize = (getc(IN) << 24) + Info_h.biSize; + + if(Info_h.biSize != 40) + { + fprintf(stderr,"Error, unknown BMP header size %ld\n", Info_h.biSize); + fclose(IN); + return NULL; + } + Info_h.biWidth = getc(IN); + Info_h.biWidth = (getc(IN) << 8) + Info_h.biWidth; + Info_h.biWidth = (getc(IN) << 16) + Info_h.biWidth; + Info_h.biWidth = (getc(IN) << 24) + Info_h.biWidth; + w = Info_h.biWidth; + + Info_h.biHeight = getc(IN); + Info_h.biHeight = (getc(IN) << 8) + Info_h.biHeight; + Info_h.biHeight = (getc(IN) << 16) + Info_h.biHeight; + Info_h.biHeight = (getc(IN) << 24) + Info_h.biHeight; + h = Info_h.biHeight; + + Info_h.biPlanes = getc(IN); + Info_h.biPlanes = (getc(IN) << 8) + Info_h.biPlanes; + + Info_h.biBitCount = getc(IN); + Info_h.biBitCount = (getc(IN) << 8) + Info_h.biBitCount; + + Info_h.biCompression = getc(IN); + Info_h.biCompression = (getc(IN) << 8) + Info_h.biCompression; + Info_h.biCompression = (getc(IN) << 16) + Info_h.biCompression; + Info_h.biCompression = (getc(IN) << 24) + Info_h.biCompression; + + Info_h.biSizeImage = getc(IN); + Info_h.biSizeImage = (getc(IN) << 8) + Info_h.biSizeImage; + Info_h.biSizeImage = (getc(IN) << 16) + Info_h.biSizeImage; + Info_h.biSizeImage = (getc(IN) << 24) + Info_h.biSizeImage; + + Info_h.biXpelsPerMeter = getc(IN); + Info_h.biXpelsPerMeter = (getc(IN) << 8) + Info_h.biXpelsPerMeter; + Info_h.biXpelsPerMeter = (getc(IN) << 16) + Info_h.biXpelsPerMeter; + Info_h.biXpelsPerMeter = (getc(IN) << 24) + Info_h.biXpelsPerMeter; + + Info_h.biYpelsPerMeter = getc(IN); + Info_h.biYpelsPerMeter = (getc(IN) << 8) + Info_h.biYpelsPerMeter; + Info_h.biYpelsPerMeter = (getc(IN) << 16) + Info_h.biYpelsPerMeter; + Info_h.biYpelsPerMeter = (getc(IN) << 24) + Info_h.biYpelsPerMeter; + + Info_h.biClrUsed = getc(IN); + Info_h.biClrUsed = (getc(IN) << 8) + Info_h.biClrUsed; + Info_h.biClrUsed = (getc(IN) << 16) + Info_h.biClrUsed; + Info_h.biClrUsed = (getc(IN) << 24) + Info_h.biClrUsed; + + Info_h.biClrImportant = getc(IN); + Info_h.biClrImportant = (getc(IN) << 8) + Info_h.biClrImportant; + Info_h.biClrImportant = (getc(IN) << 16) + Info_h.biClrImportant; + Info_h.biClrImportant = (getc(IN) << 24) + Info_h.biClrImportant; /* Read the data and store them in the OUT file */ - - if (Info_h.biBitCount == 24) { - 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; - } - /* 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; + if (Info_h.biBitCount == 24) + { + 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; + } + /* create the image */ + image = opj_image_create(numcomps, &cmptparm[0], color_space); + if(!image) + { + fclose(IN); + return NULL; + } - /* set image data */ + /* 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; - /* Place the cursor at the beginning of the image information */ - fseek(IN, 0, SEEK_SET); - fseek(IN, File_h.bfOffBits, SEEK_SET); + /* 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; + W = Info_h.biWidth; + H = Info_h.biHeight; - /* PAD = 4 - (3 * W) % 4; */ - /* PAD = (PAD == 4) ? 0 : PAD; */ - PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0; + /* 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); + 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 < (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++; - } - } + index = 0; - free(RGB); + 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++; + } + } + 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; - } 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]) - 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); + 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, File_h.bfOffBits, SEEK_SET); - W = Info_h.biWidth; - H = Info_h.biHeight; - if (Info_h.biWidth % 2) - W++; + 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; - } + 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); + free(table_R); free(table_G); free(table_B); + 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 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 */ + /* set image data */ - RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char)); + RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char)); - fread(RGB, sizeof(unsigned char), W * H, IN); - 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++; - } - } + 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); - } 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]) - gray_scale = 0; - } + } + 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 x, y, max; + int i, 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]); + } - 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; - } + if (has_color) + gray_scale = 0; + + 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); + free(table_R); + free(table_G); + free(table_B); + 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 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) { - v2 = getc(IN); - for (i = 0; i < (int) v; i++) { - RGB[line * Info_h.biWidth + col] = v2; - col++; - } - } 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"); - 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; - } - } + /* 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; + 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; } - if (gray_scale) { - 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++; - } + else + { + c = getc(IN); + + if (c == 0x00) /* EOL */ + { + x = 0; + ++y; + pix = RGB + x + (H - y - 1) * W; } - } 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++; + else if (c == 0x01) /* EOP */ + break; + else if (c == 0x02) /* MOVE by dxdy */ + { + c = getc(IN); + x += c; + c = getc(IN); + y += 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); } } - free(RGB); - free(table_R); - free(table_G); - free(table_B); - } else { - fprintf(stderr, - "Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n", Info_h.biBitCount); - } + }/* 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; + return image; } int imagetobmp(opj_image_t * image, const char *outfile) { @@ -925,7 +1154,6 @@ int imagetobmp(opj_image_t * image, const char *outfile) { } for (i = 0; i < w * h; i++) { - unsigned char rc; int r; r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; @@ -956,15 +1184,27 @@ PGX IMAGE FORMAT unsigned char readuchar(FILE * f) { unsigned char c1; - fread(&c1, 1, 1, f); + if ( !fread(&c1, 1, 1, f) ) + { + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); + return 0; + } return c1; } unsigned short readushort(FILE * f, int bigendian) { unsigned char c1, c2; - fread(&c1, 1, 1, f); - fread(&c2, 1, 1, f); + 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 (c1 << 8) + c2; else @@ -974,10 +1214,26 @@ unsigned short readushort(FILE * f, int bigendian) unsigned int readuint(FILE * f, int bigendian) { unsigned char c1, c2, c3, c4; - fread(&c1, 1, 1, f); - fread(&c2, 1, 1, f); - fread(&c3, 1, 1, f); - fread(&c4, 1, 1, f); + 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 (c1 << 24) + (c2 << 16) + (c3 << 8) + c4; else @@ -1013,8 +1269,11 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) { } 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); - + if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9){ + fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n"); + return NULL; + } + i=0; sign='+'; while (signtmp[i]!='\0') { @@ -1106,6 +1365,7 @@ int imagetopgx(opj_image_t * image, const char *outfile) { char bname[256]; /* buffer for name */ char *name = bname; /* pointer */ int nbytes = 0; + size_t res; const size_t olen = strlen(outfile); const size_t dotpos = olen - 4; const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */ @@ -1118,11 +1378,11 @@ int imagetopgx(opj_image_t * image, const char *outfile) { name = (char*)malloc(total+1); } strncpy(name, outfile, dotpos); - if (image->numcomps > 1) { - sprintf(name+dotpos, "-%d.pgx", compno); - } else { + /*if (image->numcomps > 1) {*/ + sprintf(name+dotpos, "_%d.pgx", compno); + /*} else { strcpy(name+dotpos, ".pgx"); - } + }*/ fdest = fopen(name, "wb"); if (!fdest) { fprintf(stderr, "ERROR -> failed to open %s for writing\n", name); @@ -1148,7 +1408,11 @@ int imagetopgx(opj_image_t * image, const char *outfile) { int v = image->comps[compno].data[i]; for (j = nbytes - 1; j >= 0; j--) { char byte = (char) (v >> (j * 8)); - fwrite(&byte, 1, 1, fdest); + res = fwrite(&byte, 1, 1, fdest); + if( res < 1 ) { + fprintf(stderr, "failed to write 1 byte for %s\n", name); + return 1; + } } } fclose(fdest); @@ -1226,7 +1490,11 @@ static void read_pnm_header(FILE *reader, struct pnm_header *ph) char idf[256], type[256]; char line[256]; - fgets(line, 250, reader); + if (fgets(line, 250, reader) == NULL) + { + fprintf(stderr,"\nWARNING: fgets return a NULL value"); + return; + } if(line[0] != 'P') { @@ -1481,7 +1749,8 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) { for(compno = 0; compno < numcomps; compno++) { index = 0; - fscanf(fp, "%u", &index); + if (fscanf(fp, "%u", &index) != 1) + fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n"); image->comps[compno].data[i] = (index * 255)/header_info.maxval; } @@ -1502,14 +1771,16 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) { { for(compno = 0; compno < numcomps; compno++) { - fread(&c0, 1, 1, fp); + if ( !fread(&c0, 1, 1, fp) ) + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); if(one) { image->comps[compno].data[i] = c0; } else { - fread(&c1, 1, 1, fp); + if ( !fread(&c1, 1, 1, fp) ) + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); /* netpbm: */ image->comps[compno].data[i] = ((c0<<8) | c1); } @@ -1523,7 +1794,8 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) { { unsigned int index; - fscanf(fp, "%u", &index); + if ( fscanf(fp, "%u", &index) != 1) + fprintf(stderr, "\nWARNING: fscanf return a number of element different from the expected.\n"); image->comps[0].data[i] = (index?0:255); } @@ -1552,13 +1824,14 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) { } } else - if((format == 7 && header_info.bw)) //MONO + if((format == 7 && header_info.bw)) /*MONO*/ { unsigned char uc; for(i = 0; i < w * h; ++i) { - fread(&uc, 1, 1, fp); + if ( !fread(&uc, 1, 1, fp) ) + fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); image->comps[0].data[i] = (uc & 1)?0:255; } } @@ -1578,7 +1851,7 @@ int imagetopnm(opj_image_t * image, const char *outfile) FILE *fdest = NULL; const char *tmp = outfile; char *destname; - + alpha = NULL; if((prec = image->comps[0].prec) > 16) { fprintf(stderr,"%s:%d:imagetopnm\n\tprecision %d is larger than 16" @@ -1640,7 +1913,7 @@ int imagetopnm(opj_image_t * image, const char *outfile) { fprintf(fdest, "P6\n# OpenJPEG-%s\n%d %d\n%d\n", opj_version(), wr, hr, max); - alpha = NULL; adjustA = 0; + adjustA = 0; } adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); @@ -1767,13 +2040,13 @@ int imagetopnm(opj_image_t * image, const char *outfile) <<-- <<-- <<-- <<-- */ typedef struct tiff_infoheader{ - DWORD tiWidth; // Width of Image in pixel - DWORD tiHeight; // Height of Image in pixel - DWORD tiPhoto; // Photometric - WORD tiBps; // Bits per sample - WORD tiSf; // Sample Format - WORD tiSpp; // Sample per pixel 1-bilevel,gray scale , 2- RGB - WORD tiPC; // Planar config (1-Interleaved, 2-Planarcomp) + DWORD tiWidth; /* Width of Image in pixel*/ + DWORD tiHeight; /* Height of Image in pixel */ + DWORD tiPhoto; /* Photometric */ + WORD tiBps; /* Bits per sample */ + WORD tiSf; /* Sample Format */ + WORD tiSpp; /* Sample per pixel 1-bilevel,gray scale , 2- RGB */ + WORD tiPC; /* Planar config (1-Interleaved, 2-Planarcomp) */ }tiff_infoheader_t; int imagetotif(opj_image_t * image, const char *outfile) @@ -1781,7 +2054,6 @@ int imagetotif(opj_image_t * image, const char *outfile) int width, height, imgsize; int bps,index,adjust, sgnd; int ushift, dshift, has_alpha, force16; - unsigned int last_i=0; TIFF *tif; tdata_t buf; tstrip_t strip; @@ -1842,10 +2114,10 @@ int imagetotif(opj_image_t * image, const char *outfile) for(strip = 0; strip < TIFFNumberOfStrips(tif); strip++) { unsigned char *dat8; - tsize_t i, ssize; + tsize_t i, ssize, last_i = 0; + int step, restx; ssize = TIFFStripSize(tif); dat8 = (unsigned char*)buf; - int step, restx; if(bps == 8) { @@ -1880,7 +2152,7 @@ int imagetotif(opj_image_t * image, const char *outfile) } else break; - }//for(i = 0;) + }/*for(i = 0;)*/ if(last_i < ssize) { @@ -1913,10 +2185,10 @@ int imagetotif(opj_image_t * image, const char *outfile) } else break; - }//for(i) - }//if(last_i < ssize) + }/*for(i)*/ + }/*if(last_i < ssize)*/ - } //if(bps == 8) + } /*if(bps == 8)*/ else if(bps == 16) { @@ -1948,8 +2220,8 @@ int imagetotif(opj_image_t * image, const char *outfile) b = (b<>dshift); if(has_alpha) a = (a<>dshift); } - dat8[i+0] = r;//LSB - dat8[i+1] = (r >> 8);//MSB + dat8[i+0] = r;/*LSB*/ + dat8[i+1] = (r >> 8);/*MSB*/ dat8[i+2] = g; dat8[i+3] = (g >> 8); dat8[i+4] = b; @@ -1964,7 +2236,7 @@ int imagetotif(opj_image_t * image, const char *outfile) } else break; - }//for(i = 0;) + }/*for(i = 0;)*/ if(last_i < ssize) { @@ -1993,8 +2265,8 @@ int imagetotif(opj_image_t * image, const char *outfile) b = (b<>dshift); if(has_alpha) a = (a<>dshift); } - dat8[i+0] = r;//LSB - if(i+1 < ssize) dat8[i+1] = (r >> 8);else break;//MSB + dat8[i+0] = r;/*LSB*/ + if(i+1 < ssize) dat8[i+1] = (r >> 8);else break;/*MSB*/ if(i+2 < ssize) dat8[i+2] = g; else break; if(i+3 < ssize) dat8[i+3] = (g >> 8);else break; if(i+4 < ssize) dat8[i+4] = b; else break; @@ -2009,18 +2281,18 @@ int imagetotif(opj_image_t * image, const char *outfile) } else break; - }//for(i) - }//if(last_i < ssize) + }/*for(i)*/ + }/*if(last_i < ssize)*/ - }//if(bps == 16) + }/*if(bps == 16)*/ (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size); - }//for(strip = 0; ) + }/*for(strip = 0; )*/ _TIFFfree((void*)buf); TIFFClose(tif); return 0; - }//RGB(A) + }/*RGB(A)*/ if(image->numcomps == 1 /* GRAY */ || ( image->numcomps == 2 /* GRAY_ALPHA */ @@ -2081,8 +2353,8 @@ int imagetotif(opj_image_t * image, const char *outfile) } else break; - }//for(i ) - }//if(bps == 8 + }/*for(i )*/ + }/*if(bps == 8*/ else if(bps == 16) { @@ -2107,21 +2379,21 @@ int imagetotif(opj_image_t * image, const char *outfile) r = (r<>dshift); if(has_alpha) a = (a<>dshift); } - dat8[i+0] = r;//LSB - dat8[i+1] = r >> 8;//MSB + dat8[i+0] = r;/*LSB*/ + dat8[i+1] = r >> 8;/*MSB*/ if(has_alpha) { dat8[i+2] = a; dat8[i+3] = a >> 8; } index++; - }//if(index < imgsize) + }/*if(index < imgsize)*/ else break; - }//for(i ) + }/*for(i )*/ } (void)TIFFWriteEncodedStrip(tif, strip, (void*)buf, strip_size); - }//for(strip + }/*for(strip*/ _TIFFfree(buf); TIFFClose(tif); @@ -2291,9 +2563,9 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) { if(index < imgsize) { - image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R - image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; // G - image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B + image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; /* R */ + image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; /* G */ + image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; /* B */ if(has_alpha) image->comps[3].data[index] = ( dat8[i+7] << 8 ) | dat8[i+6]; @@ -2315,8 +2587,8 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) } else break; - }//for(i = 0) - }//if(Info.tiBps == 16) + }/*for(i = 0)*/ + }/*if(Info.tiBps == 16)*/ else if(Info.tiBps == 8) { @@ -2326,9 +2598,9 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) { if(index < imgsize) { - image->comps[0].data[index] = dat8[i+0];// R - image->comps[1].data[index] = dat8[i+1];// G - image->comps[2].data[index] = dat8[i+2];// B + image->comps[0].data[index] = dat8[i+0];/* R */ + image->comps[1].data[index] = dat8[i+1];/* G */ + image->comps[2].data[index] = dat8[i+2];/* B */ if(has_alpha) image->comps[3].data[index] = dat8[i+3]; @@ -2343,11 +2615,11 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) image->comps[3].data[index] = image->comps[3].data[index] << 4 ; } index++; - }//if(index + }/*if(index*/ else break; - }//for(i ) - }//if( Info.tiBps == 8) + }/*for(i )*/ + }/*if( Info.tiBps == 8)*/ else if(Info.tiBps == 12)/* CINEMA file */ { @@ -2370,15 +2642,15 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) } else break; - }//for(i ) + }/*for(i )*/ } - }//for(strip = 0; ) + }/*for(strip = 0; )*/ _TIFFfree(buf); TIFFClose(tif); return image; - }//RGB(A) + }/*RGB(A)*/ if(Info.tiPhoto == PHOTOMETRIC_MINISBLACK) /* GRAY(A) */ { @@ -2441,7 +2713,7 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) } else break; - }//for(i ) + }/*for(i )*/ } else if(Info.tiBps == 8) @@ -2459,14 +2731,14 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) } else break; - }//for(i ) + }/*for(i )*/ } - }//for(strip = 0; + }/*for(strip = 0;*/ _TIFFfree(buf); TIFFClose(tif); - }//GRAY(A) + }/*GRAY(A)*/ return image; @@ -2585,6 +2857,7 @@ opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw int imagetoraw(opj_image_t * image, const char *outfile) { FILE *rawFile = NULL; + size_t res; int compno; int w, h; int line, row; @@ -2622,7 +2895,11 @@ int imagetoraw(opj_image_t * image, const char *outfile) for (line = 0; line < h; line++) { for(row = 0; row < w; row++) { curr = (signed char) (*ptr & mask); - fwrite(&curr, sizeof(signed char), 1, rawFile); + res = fwrite(&curr, sizeof(signed char), 1, rawFile); + if( res < 1 ) { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + return 1; + } ptr++; } } @@ -2635,7 +2912,11 @@ int imagetoraw(opj_image_t * image, const char *outfile) for (line = 0; line < h; line++) { for(row = 0; row < w; row++) { curr = (unsigned char) (*ptr & mask); - fwrite(&curr, sizeof(unsigned char), 1, rawFile); + res = fwrite(&curr, sizeof(unsigned char), 1, rawFile); + if( res < 1 ) { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + return 1; + } ptr++; } } @@ -2653,9 +2934,17 @@ int imagetoraw(opj_image_t * image, const char *outfile) unsigned char temp; curr = (signed short int) (*ptr & mask); temp = (unsigned char) (curr >> 8); - fwrite(&temp, 1, 1, rawFile); + res = fwrite(&temp, 1, 1, rawFile); + if( res < 1 ) { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + return 1; + } temp = (unsigned char) curr; - fwrite(&temp, 1, 1, rawFile); + res = fwrite(&temp, 1, 1, rawFile); + if( res < 1 ) { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + return 1; + } ptr++; } } @@ -2670,9 +2959,17 @@ int imagetoraw(opj_image_t * image, const char *outfile) unsigned char temp; curr = (unsigned short int) (*ptr & mask); temp = (unsigned char) (curr >> 8); - fwrite(&temp, 1, 1, rawFile); + res = fwrite(&temp, 1, 1, rawFile); + if( res < 1 ) { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + return 1; + } temp = (unsigned char) curr; - fwrite(&temp, 1, 1, rawFile); + res = fwrite(&temp, 1, 1, rawFile); + if( res < 1 ) { + fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + return 1; + } ptr++; } }