X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fpcx.c;h=cb8e54980fdc1490b8fb09d9c624bb3928367aaf;hb=eae2f3467caaaf64a9370c1acd8cecf11fd58328;hp=514d30de6041f7683a86e96cffed79e474767357;hpb=182260bf970d56e056a8c0c580ad61302d21ab0f;p=rocksndiamonds.git diff --git a/src/pcx.c b/src/pcx.c index 514d30de..cb8e5498 100644 --- a/src/pcx.c +++ b/src/pcx.c @@ -15,6 +15,8 @@ #include "image.h" #include "misc.h" +#define PCX_DEBUG FALSE + #define PCX_MAGIC 0x0a /* first byte in a PCX image file */ #define PCX_LAST_VERSION 5 /* last acceptable version number */ #define PCX_ENCODING 1 /* PCX encoding method */ @@ -45,6 +47,9 @@ struct PCX_Header unsigned char filler[58]; /* fill to struct size of 128 */ }; +/* global PCX error value */ +int errno_pcx = PCX_Success; + static byte *PCX_ReadBitmap(Image *image, byte *buffer_ptr, byte *buffer_last) { /* Run Length Encoding: If the two high bits are set, @@ -127,21 +132,29 @@ Image *Read_PCX_to_Image(char *filename) int width, height, depth; int i; + errno_pcx = PCX_Success; + if (!(file = fopen(filename, "r"))) + { + errno_pcx = PCX_OpenFailed; return NULL; + } if (fseek(file, 0, SEEK_END) == -1) { fclose(file); + errno_pcx = PCX_ReadFailed; return NULL; } file_length = ftell(file); rewind(file); - if (file_length < PCX_HEADER_SIZE + PCX_COLORMAP_SIZE) + if (file_length < PCX_HEADER_SIZE) { + /* PCX file is too short to contain a valid PCX header */ fclose(file); + errno_pcx = PCX_FileInvalid; return NULL; } @@ -150,6 +163,7 @@ Image *Read_PCX_to_Image(char *filename) if (fread(file_buffer, 1, file_length, file) != file_length) { fclose(file); + errno_pcx = PCX_ReadFailed; return NULL; } @@ -176,20 +190,24 @@ Image *Read_PCX_to_Image(char *filename) width < 0 || height < 0) { free(file_buffer); + errno_pcx = PCX_FileInvalid; return NULL; } +#if PCX_DEBUG if (options.verbose) { printf("%s is a %dx%d PC Paintbrush image with %d bitplanes\n", filename, pcx.xmax, pcx.ymax, pcx.color_planes); printf("depth: %d\n", pcx.bits_per_pixel); + printf("color_planes: %d\n", pcx.color_planes); printf("bytes_per_line: %d\n", pcx.bytes_per_line); printf("palette type: %s\n", (pcx.palette_type == 1 ? "color" : pcx.palette_type == 2 ? "grayscale" : "undefined")); } +#endif /* allocate new image structure */ image = newImage(width, height, depth); @@ -202,6 +220,15 @@ Image *Read_PCX_to_Image(char *filename) { free(file_buffer); freeImage(image); + errno_pcx = PCX_FileInvalid; + return NULL; + } + + if (file_length < PCX_HEADER_SIZE + PCX_COLORMAP_SIZE) + { + /* PCX file is too short to contain a valid 256 colors colormap */ + fclose(file); + errno_pcx = PCX_ColorFailed; return NULL; } @@ -210,6 +237,7 @@ Image *Read_PCX_to_Image(char *filename) { free(file_buffer); freeImage(image); + errno_pcx = PCX_ColorFailed; return NULL; } @@ -221,8 +249,10 @@ Image *Read_PCX_to_Image(char *filename) if (image->rgb.color_used[i]) image->rgb.used++; +#if PCX_DEBUG if (options.verbose) printf("Read_PCX_to_Image: %d colors found\n", image->rgb.used); +#endif return image; }