From 333896e44bae70515b2d82877dd14e7e28338977 Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Tue, 10 Nov 1998 23:47:08 +0100 Subject: [PATCH] rnd-19981110-2 --- src/Makefile | 2 + src/gifload.c | 2 +- src/image.c | 8 +++ src/init.c | 6 +- src/pcx.c | 177 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 191 insertions(+), 4 deletions(-) create mode 100644 src/pcx.c diff --git a/src/Makefile b/src/Makefile index 9cc555fc..553a3e9c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -94,6 +94,7 @@ SRCS = main.c \ random.c \ gifload.c \ gif.c \ + pcx.c \ image.c \ network.c \ netserv.c @@ -115,6 +116,7 @@ OBJS = main.o \ random.o \ gifload.o \ gif.o \ + pcx.o \ image.o \ network.o \ netserv.o diff --git a/src/gifload.c b/src/gifload.c index 977678cd..360cd0e3 100644 --- a/src/gifload.c +++ b/src/gifload.c @@ -42,7 +42,7 @@ int Read_GIF_to_Pixmaps(Display *display, Window window, char *filename, #endif /* load GIF file */ - if (!(image = Read_GIF_to_Image(filename))) + if (!(image = Read_PCX_to_Image(filename))) { printf("Loading GIF image failed -- maybe no GIF...\n"); exit(1); diff --git a/src/image.c b/src/image.c index d75de855..85861dcc 100644 --- a/src/image.c +++ b/src/image.c @@ -18,7 +18,11 @@ Image *monochrome(Image *cimage) int bitmap_pixel; if (BITMAPP(cimage)) + { + printf("-->ERROR(monochrome)\n"); + return(NULL); + } image = newBitImage(cimage->width, cimage->height); @@ -319,6 +323,8 @@ void compress(Image *image) free(map); free(used); + + #if 0 if (badcount) printf("%d out-of-range pixels, ", badcount); @@ -336,6 +342,8 @@ void compress(Image *image) } #endif + + image->rgb.compressed= TRUE; /* don't do it again */ } diff --git a/src/init.c b/src/init.c index 8e60cfeb..2a6c363d 100644 --- a/src/init.c +++ b/src/init.c @@ -26,9 +26,9 @@ #include "netserv.h" #ifdef DEBUG -/* + #define DEBUG_TIMING -*/ + #endif struct PictureFileInfo @@ -642,7 +642,7 @@ void LoadGfx(int pos, struct PictureFileInfo *pic) char *picturemask_ext = "Mask.xbm"; #else int gif_err; - char *picture_ext = ".gif"; + char *picture_ext = ".pcx"; #endif #ifdef DEBUG_TIMING diff --git a/src/pcx.c b/src/pcx.c new file mode 100644 index 00000000..9e5a7217 --- /dev/null +++ b/src/pcx.c @@ -0,0 +1,177 @@ + +/* pcx.c */ + +#include "image.h" + +#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 */ +#define PCX_256COLORS_MAGIC 0x0c /* first byte of a PCX 256 color map */ +#define PCX_MAXDEPTH 8 /* supports up to 8 bits per pixel */ +#define PCX_MAXCOLORS 256 /* maximum number of colors */ +#define PCX_HEADER_SIZE 128 +#define PCX_PALETTE_SIZE (3 * PCX_MAXCOLORS) + +struct PCX_Header +{ + unsigned char signature; /* PCX file identifier */ + unsigned char version; /* version compatibility level */ + unsigned char encoding; /* encoding method */ + unsigned char bits_per_pixel; /* bits per pixel, or depth */ + unsigned short xmin; /* X position of left edge */ + unsigned short ymin; /* Y position of top edge */ + unsigned short xmax; /* X position of right edge */ + unsigned short ymax; /* Y position of bottom edge */ + unsigned short hres; /* X screen resolution of source image */ + unsigned short vres; /* Y screen resolution of source image */ + unsigned char palette[16][3]; /* PCX color map */ + unsigned char reserved; /* should be 0, 1 if std res fax */ + unsigned char color_planes; /* bit planes in image */ + unsigned short bytes_per_line;/* byte delta between scanlines */ + unsigned short palette_type; /* 0 = undef, 1 = color, 2 = grayscale */ + unsigned char filler[58]; /* fill to struct size of 128 */ +}; + +static boolean PCX_LoadImage(); /* Routine to load PCX bitmap */ +static boolean PCX_LoadColormap(); /* Routine to load PCX colormap */ + +Image *Read_PCX_to_Image(char *filename) +{ + FILE *file; + unsigned char buffer[PCX_HEADER_SIZE]; + struct PCX_Header pcx; + Image *image; + int width, height, depth; + + if (!(file = fopen(filename, "r"))) + return NULL; + + /* read PCX header */ + if (fread(buffer, 1, PCX_HEADER_SIZE, file) != PCX_HEADER_SIZE) + { + fclose(file); + return NULL; + } + + pcx.signature = buffer[0]; + pcx.version = buffer[1]; + pcx.encoding = buffer[2]; + pcx.bits_per_pixel = buffer[3]; + pcx.xmin = buffer[4] + 256 * buffer[5]; + pcx.ymin = buffer[6] + 256 * buffer[7]; + pcx.xmax = buffer[8] + 256 * buffer[9]; + pcx.ymax = buffer[10] + 256 * buffer[11]; + pcx.color_planes = buffer[65]; + pcx.bytes_per_line = buffer[66] + 256 * buffer[67]; + pcx.palette_type = buffer[68] + 256 * buffer[69]; + + width = pcx.xmax - pcx.xmin + 1; + height = pcx.ymax - pcx.ymin + 1; + depth = pcx.bits_per_pixel; + + if (pcx.signature != PCX_MAGIC || pcx.version > PCX_LAST_VERSION || + pcx.encoding != PCX_ENCODING || pcx.color_planes > PCX_MAXDEPTH || + width < 0 || height < 0) + { + fclose(file); + return NULL; + } + + 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("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")); + } + + /* allocate new image structure */ + image = newRGBImage(width, height, depth); + + /* read compressed bitmap data */ + if (!PCX_LoadImage(file, image)) + { + fclose(file); + return image; + } + + /* read colormap data */ + if (!PCX_LoadColormap(file, image)) + { + fclose(file); + return image; + } + + fclose(file); + return(image); +} + +static boolean PCX_LoadImage(FILE *file, Image *image) +{ + /* Run Length Encoding: If the two high bits are set, + * then the low 6 bits contain a repeat count, and the byte to + * repeat is the next byte in the file. If the two high bits are + * not set, then this is the byte to write. + */ + + register unsigned char *ptr, *ptr_last; + int value, count; + + ptr = image->data; + ptr_last = ptr + (image->width * image->height * image->pixlen); + + while (ptr < ptr_last) + { + if ((value = fgetc(file)) == EOF) + return FALSE; + + if ((value & 0xc0) == 0xc0) + { + count = value & 0x3f; + + if ((value = fgetc(file)) == EOF) + return FALSE; + + for ( ; count && (ptr < ptr_last); count--) + *ptr++ = (unsigned char)value; + + if (count) + printf("Repeat count spans end of image!\n"); + } + else + *ptr++ = (unsigned char)value; + } + + return TRUE; +} + +static boolean PCX_LoadColormap(FILE *file, Image *image) +{ + unsigned char buffer[PCX_PALETTE_SIZE]; + int i, result, magic; + + /* read colormap magic byte */ + if ((magic = fgetc(file)) == EOF) + return FALSE; + + if (magic != PCX_256COLORS_MAGIC) + return FALSE; + + /* read PCX 256 colors colormap */ + if ((result = fread(buffer, 1, PCX_PALETTE_SIZE, file)) != PCX_PALETTE_SIZE) + return FALSE; + + for (i=0; irgb.red[i] = buffer[i*3 + 0] << 8; + image->rgb.green[i] = buffer[i*3 + 1] << 8; + image->rgb.blue[i] = buffer[i*3 + 2] << 8; + } + image->rgb.used = PCX_MAXCOLORS; + + return TRUE; +} -- 2.34.1