From d46a473059b29c03d44430dfc75f170e3eb7cf63 Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Mon, 9 Nov 1998 22:33:47 +0100 Subject: [PATCH] rnd-19981109-2 --- src/Makefile | 4 +- src/gif.c | 5 +-- src/gifload.c | 13 +++---- src/{send.c => image.c} | 82 ++++++++++++++--------------------------- src/{xli.h => image.h} | 57 ++++------------------------ src/misc.c | 12 ++++++ src/misc.h | 1 + src/new.c | 70 +++++------------------------------ 8 files changed, 67 insertions(+), 177 deletions(-) rename src/{send.c => image.c} (91%) rename src/{xli.h => image.h} (77%) diff --git a/src/Makefile b/src/Makefile index 50502fa7..ca65fea5 100644 --- a/src/Makefile +++ b/src/Makefile @@ -94,7 +94,7 @@ SRCS = main.c \ random.c \ gifload.c \ gif.c \ - send.c \ + image.c \ new.c \ network.c \ netserv.c @@ -116,7 +116,7 @@ OBJS = main.o \ random.o \ gifload.o \ gif.o \ - send.o \ + image.o \ new.o \ network.o \ netserv.o diff --git a/src/gif.c b/src/gif.c index d2fdda81..bf3f5fc7 100644 --- a/src/gif.c +++ b/src/gif.c @@ -3,8 +3,8 @@ #define GIF_C -#include "xli.h" #include "gif.h" +#include "image.h" #define PUSH_PIXEL(p) \ { \ @@ -468,7 +468,7 @@ static int gifin_add_string(int p, int e) return GIFIN_SUCCESS; } -Image *gifLoad(char *fullname) +Image *Read_GIF_to_Image(char *fullname) { FILE *f; Image *image; @@ -508,7 +508,6 @@ Image *gifLoad(char *fullname) image = newRGBImage(gifin_img_width, gifin_img_height, (gifin_l_cmap_flag ? gifin_l_pixel_bits : gifin_g_pixel_bits)); - image->title = dupString(fullname); /* if image has a local colormap, override global colormap */ diff --git a/src/gifload.c b/src/gifload.c index 1e1aebcd..977678cd 100644 --- a/src/gifload.c +++ b/src/gifload.c @@ -14,7 +14,7 @@ #ifndef MSDOS #include "gifload.h" -#include "xli.h" +#include "image.h" #ifdef DEBUG /* @@ -27,7 +27,6 @@ extern long Counter(void); - int Read_GIF_to_Pixmaps(Display *display, Window window, char *filename, Pixmap *pixmap, Pixmap *pixmap_mask) { @@ -43,7 +42,7 @@ int Read_GIF_to_Pixmaps(Display *display, Window window, char *filename, #endif /* load GIF file */ - if (!(image = gifLoad(filename))) + if (!(image = Read_GIF_to_Image(filename))) { printf("Loading GIF image failed -- maybe no GIF...\n"); exit(1); @@ -77,7 +76,7 @@ int Read_GIF_to_Pixmaps(Display *display, Window window, char *filename, depth = DefaultDepth(display, screen); /* convert internal image structure to X11 XImage */ - if (!(ximageinfo = imageToXImage(display, screen, visual, depth, image))) + if (!(ximageinfo = Image_to_XImage(display, screen, visual, depth, image))) { fprintf(stderr, "Cannot convert Image to XImage.\n"); exit(1); @@ -94,7 +93,7 @@ int Read_GIF_to_Pixmaps(Display *display, Window window, char *filename, XSetWindowColormap(display, window, ximageinfo->cmap); /* convert XImage to Pixmap */ - if ((*pixmap = ximageToPixmap(display, window, ximageinfo)) == None) + if ((*pixmap = XImage_to_Pixmap(display, window, ximageinfo)) == None) { fprintf(stderr, "Cannot convert XImage to Pixmap.\n"); exit(1); @@ -118,7 +117,7 @@ int Read_GIF_to_Pixmaps(Display *display, Window window, char *filename, #endif /* convert internal image structure to X11 XImage */ - if (!(ximageinfo_mask = imageToXImage(display, screen, visual, depth, + if (!(ximageinfo_mask = Image_to_XImage(display, screen, visual, depth, image_mask))) { fprintf(stderr, "Cannot convert Image to XImage.\n"); @@ -133,7 +132,7 @@ int Read_GIF_to_Pixmaps(Display *display, Window window, char *filename, #endif /* convert XImage to Pixmap */ - if ((*pixmap_mask = ximageToPixmap(display, window, ximageinfo_mask)) == None) + if ((*pixmap_mask = XImage_to_Pixmap(display, window, ximageinfo_mask)) == None) { fprintf(stderr, "Cannot convert XImage to Pixmap.\n"); exit(1); diff --git a/src/send.c b/src/image.c similarity index 91% rename from src/send.c rename to src/image.c index f0aeee1a..f72ea230 100644 --- a/src/send.c +++ b/src/image.c @@ -1,7 +1,8 @@ -/* send.c */ +/* image.c */ -#include "xli.h" +#include "image.h" +#include "misc.h" /* extra colors to try allocating in private color maps to minimise flashing */ #define NOFLASH_COLORS 256 @@ -19,17 +20,7 @@ Image *monochrome(Image *cimage) if (BITMAPP(cimage)) return(NULL); - /* - printf(" Converting to monochrome..."); - fflush(stdout); - */ - - image= newBitImage(cimage->width, cimage->height); - if (cimage->title) - { - image->title= (char *)lmalloc(strlen(cimage->title) + 13); - sprintf(image->title, "%s (monochrome)", cimage->title); - } + image = newBitImage(cimage->width, cimage->height); spl = cimage->pixlen; dll = (image->width / 8) + (image->width % 8 ? 1 : 0); @@ -58,10 +49,6 @@ Image *monochrome(Image *cimage) dp += dll; /* next row */ } - /* - printf("done\n"); - */ - return(image); } @@ -83,7 +70,7 @@ static unsigned int *buildIndex(unsigned int width, fzoom= (float)zoom / 100.0; *rwidth= (unsigned int)(fzoom * width + 0.5); } - index= (unsigned int *)lmalloc(sizeof(unsigned int) * *rwidth); + index= (unsigned int *)checked_malloc(sizeof(unsigned int) * *rwidth); for (a= 0; a < *rwidth; a++) { if (zoom) @@ -230,7 +217,6 @@ Image *zoom(Image *oimage, unsigned int xzoom, unsigned int yzoom) break; } - image->title = dupString(oimage->title); free((byte *)xindex); free((byte *)yindex); @@ -255,14 +241,9 @@ void compress(Image *image) if (!RGBP(image) || image->rgb.compressed) return; - /* - printf(" Compressing colormap..."); - fflush(stdout); - */ - - used = (unsigned char *)lcalloc(sizeof(unsigned char) * depthToColors(image->depth)); + used = (unsigned char *)checked_calloc(sizeof(unsigned char) * depthToColors(image->depth)); dmask = (1 << image->depth) -1; /* Mask any illegal bits for that depth */ - map = (Pixel *)lcalloc(sizeof(Pixel) * depthToColors(image->depth)); + map = (Pixel *)checked_calloc(sizeof(Pixel) * depthToColors(image->depth)); /* init fast duplicate check table */ for(r=0;r<32;r++) @@ -338,7 +319,7 @@ void compress(Image *image) free(map); free(used); - /* +#if 0 if (badcount) printf("%d out-of-range pixels, ", badcount); @@ -353,7 +334,7 @@ void compress(Image *image) printf("%d unique color%s\n", next_index, (next_index == 1 ? "" : "s")); } - */ +#endif image->rgb.compressed= TRUE; /* don't do it again */ } @@ -361,7 +342,7 @@ void compress(Image *image) -Pixmap ximageToPixmap(Display *disp, Window parent, XImageInfo *ximageinfo) +Pixmap XImage_to_Pixmap(Display *disp, Window parent, XImageInfo *ximageinfo) { Pixmap pixmap; @@ -371,8 +352,8 @@ Pixmap ximageToPixmap(Display *disp, Window parent, XImageInfo *ximageinfo) ximageinfo->drawable = pixmap; - sendXImage(ximageinfo, 0, 0, 0, 0, - ximageinfo->ximage->width, ximageinfo->ximage->height); + XImage_to_Drawable(ximageinfo, 0, 0, 0, 0, + ximageinfo->ximage->width, ximageinfo->ximage->height); return(pixmap); } @@ -386,8 +367,6 @@ Pixmap ximageToPixmap(Display *disp, Window parent, XImageInfo *ximageinfo) static unsigned int bitsPerPixelAtDepth(Display *disp, int scrn, unsigned int depth) { -#if defined(XlibSpecificationRelease) && (XlibSpecificationRelease >= 4) - /* the way things are */ XPixmapFormatValues *xf; int nxf, a; @@ -403,13 +382,6 @@ static unsigned int bitsPerPixelAtDepth(Display *disp, int scrn, } } XFree(xf); -#else /* the way things were (X11R3) */ - unsigned int a; - - for (a= 0; a < disp->nformats; a++) - if (disp->pixmap_format[a].depth == depth) - return(disp->pixmap_format[a].bits_per_pixel); -#endif /* this should never happen; if it does, we're in trouble */ @@ -418,8 +390,8 @@ static unsigned int bitsPerPixelAtDepth(Display *disp, int scrn, exit(1); } -XImageInfo *imageToXImage(Display *disp, int scrn, Visual *visual, - unsigned int ddepth, Image *image) +XImageInfo *Image_to_XImage(Display *disp, int scrn, Visual *visual, + unsigned int ddepth, Image *image) { static XColor xcolor_private[NOFLASH_COLORS]; static int colorcell_used[NOFLASH_COLORS]; @@ -447,7 +419,7 @@ XImageInfo *imageToXImage(Display *disp, int scrn, Visual *visual, xcolor.flags = DoRed | DoGreen | DoBlue; redvalue = greenvalue = bluevalue = NULL; - ximageinfo = (XImageInfo *)lmalloc(sizeof(XImageInfo)); + ximageinfo = (XImageInfo *)checked_malloc(sizeof(XImageInfo)); ximageinfo->disp = disp; ximageinfo->scrn = scrn; ximageinfo->depth = 0; @@ -469,9 +441,9 @@ XImageInfo *imageToXImage(Display *disp, int scrn, Visual *visual, unsigned int redbottom, greenbottom, bluebottom; unsigned int redtop, greentop, bluetop; - redvalue = (Pixel *)lmalloc(sizeof(Pixel) * 256); - greenvalue = (Pixel *)lmalloc(sizeof(Pixel) * 256); - bluevalue = (Pixel *)lmalloc(sizeof(Pixel) * 256); + redvalue = (Pixel *)checked_malloc(sizeof(Pixel) * 256); + greenvalue = (Pixel *)checked_malloc(sizeof(Pixel) * 256); + bluevalue = (Pixel *)checked_malloc(sizeof(Pixel) * 256); ximageinfo->cmap = global_cmap; @@ -562,7 +534,8 @@ XImageInfo *imageToXImage(Display *disp, int scrn, Visual *visual, case PseudoColor: ximageinfo->cmap = global_cmap; - ximageinfo->index = (Pixel *)lmalloc(sizeof(Pixel) * image->rgb.used); + ximageinfo->index = + (Pixel *)checked_malloc(sizeof(Pixel) * image->rgb.used); for (a=0; argb.used; a++) { @@ -593,7 +566,7 @@ XImageInfo *imageToXImage(Display *disp, int scrn, Visual *visual, /* allocate the rest of the color cells read/write */ global_cmap_index = - (Pixel *)lmalloc(sizeof(Pixel) * NOFLASH_COLORS); + (Pixel *)checked_malloc(sizeof(Pixel) * NOFLASH_COLORS); for (i=0; iwidth + 7) / 8); - data= lmalloc(linelen * image->height); + data= checked_malloc(linelen * image->height); memcpy((char *)data, (char *)image->data, linelen * image->height); @@ -758,7 +731,7 @@ XImageInfo *imageToXImage(Display *disp, int scrn, Visual *visual, NULL, image->width, image->height, 8, image->width * dpixlen); - data = (byte *)lmalloc(image->width * image->height * dpixlen); + data = (byte *)checked_malloc(image->width * image->height * dpixlen); ximageinfo->depth = ddepth; ximageinfo->ximage->data = (char *)data; ximageinfo->ximage->byte_order = MSBFirst; @@ -840,9 +813,9 @@ XImageInfo *imageToXImage(Display *disp, int scrn, Visual *visual, * to the drawable. */ -void sendXImage(XImageInfo *ximageinfo, - int src_x, int src_y, int dst_x, int dst_y, - unsigned int w, unsigned int h) +void XImage_to_Drawable(XImageInfo *ximageinfo, + int src_x, int src_y, int dst_x, int dst_y, + unsigned int w, unsigned int h) { XGCValues gcv; @@ -877,7 +850,8 @@ void freeXImage(Image *image, XImageInfo *ximageinfo) if (ximageinfo->index != NULL) /* if we allocated colors */ { if (ximageinfo->no > 0 && !ximageinfo->rootimage) /* don't free root colors */ - XFreeColors(ximageinfo->disp, ximageinfo->cmap, ximageinfo->index, ximageinfo->no, 0); + XFreeColors(ximageinfo->disp, ximageinfo->cmap, ximageinfo->index, + ximageinfo->no, 0); free(ximageinfo->index); } if (ximageinfo->gc) diff --git a/src/xli.h b/src/image.h similarity index 77% rename from src/xli.h rename to src/image.h index 50826558..b6c6dfbd 100644 --- a/src/xli.h +++ b/src/image.h @@ -1,41 +1,9 @@ -/* xli.h: - * - * jim frost 06.21.89 - * - * Copyright 1989 Jim Frost. See included file "copyright.h" for complete - * copyright information. - */ - -#include -#include -#include -#include -#include +/* image.h */ -#include -#include -#include -#include -#include +#include "main.h" -typedef unsigned long Pixel; /* what X thinks a pixel is */ typedef unsigned short Intensity; /* what X thinks an RGB intensity is */ -typedef unsigned char byte; /* unsigned byte type */ - -#define FALSE 0 -#define TRUE (!FALSE) - -/* Display device dependent Information structure */ -typedef struct -{ - int width; /* Display width and height */ - int height; - - Display *disp; - int scrn; - Colormap colormap; -} DisplayInfo; /* This struct holds the X-client side bits for a rendered image. */ typedef struct @@ -61,7 +29,8 @@ Pixmap ximageToPixmap(); void freeXImage(); -typedef struct rgbmap { +typedef struct rgbmap +{ unsigned int size; /* size of RGB map */ unsigned int used; /* number of colors used in RGB map */ int compressed; /* image uses colormap fully */ @@ -74,7 +43,6 @@ typedef struct rgbmap { */ typedef struct { - char *title; /* name of image */ unsigned int type; /* type of image */ RGBMap rgb; /* RGB map of image if IRGB type */ unsigned int width; /* width of image in pixels */ @@ -125,14 +93,7 @@ typedef struct { /* functions */ -void cleanUpWindow(); /* window.c */ -char imageInWindow(); - -int visualClassFromName(); -char *nameOfVisualClass(); - -extern unsigned long DepthToColorsTable[]; /* new.c */ -char *dupString(); +extern unsigned long DepthToColorsTable[]; Image *newBitImage(); Image *newRGBImage(); void freeImage(); @@ -142,12 +103,8 @@ void freeRGBMapData(); byte *lcalloc(); byte *lmalloc(); -Image *gifLoad(); +Image *Read_GIF_to_Image(); Image *monochrome(); Image *zoom(); -void compress(); /* compress.c */ - -int xliOpenDisplay(); -void tellAboutDisplay(DisplayInfo *); -void xliCloseDisplay(DisplayInfo *); +void compress(); diff --git a/src/misc.c b/src/misc.c index c560ae4b..a7e52dcc 100644 --- a/src/misc.c +++ b/src/misc.c @@ -446,6 +446,18 @@ void *checked_malloc(unsigned long size) return ptr; } +void *checked_calloc(unsigned long size) +{ + void *ptr; + + ptr = calloc(1, size); + + if (ptr == NULL) + Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size); + + return ptr; +} + #define TRANSLATE_KEYSYM_TO_KEYNAME 0 #define TRANSLATE_KEYSYM_TO_X11KEYNAME 1 #define TRANSLATE_X11KEYNAME_TO_KEYSYM 2 diff --git a/src/misc.h b/src/misc.h index 798e61ef..c0109175 100644 --- a/src/misc.h +++ b/src/misc.h @@ -45,6 +45,7 @@ void MarkTileDirty(int, int); void GetOptions(char **); void Error(int, char *, ...); void *checked_malloc(unsigned long); +void *checked_calloc(unsigned long); char *getKeyNameFromKeySym(KeySym); char *getX11KeyNameFromKeySym(KeySym); KeySym getKeySymFromX11KeyName(char *); diff --git a/src/new.c b/src/new.c index 508f7393..d1744d5e 100644 --- a/src/new.c +++ b/src/new.c @@ -1,7 +1,8 @@ /* new.c */ -#include "xli.h" +#include "image.h" +#include "misc.h" /* this table is useful for quick conversions between depth and ncolors */ @@ -35,26 +36,14 @@ unsigned long DepthToColorsTable[] = /* 24 */ 16777216 }; -char *dupString(char *s) -{ - char *d; - - if (!s) - return(NULL); - - d = (char *)lmalloc(strlen(s) + 1); - strcpy(d, s); - return(d); -} - void newRGBMapData(RGBMap *rgb, unsigned int size) { rgb->used = 0; rgb->size = size; rgb->compressed = FALSE; - rgb->red = (Intensity *)lmalloc(sizeof(Intensity) * size); - rgb->green = (Intensity *)lmalloc(sizeof(Intensity) * size); - rgb->blue = (Intensity *)lmalloc(sizeof(Intensity) * size); + rgb->red = (Intensity *)checked_malloc(sizeof(Intensity) * size); + rgb->green = (Intensity *)checked_malloc(sizeof(Intensity) * size); + rgb->blue = (Intensity *)checked_malloc(sizeof(Intensity) * size); } void freeRGBMapData(RGBMap *rgb) @@ -69,9 +58,8 @@ Image *newBitImage(unsigned int width, unsigned int height) Image *image; unsigned int linelen; - image = (Image *)lmalloc(sizeof(Image)); + image = (Image *)checked_malloc(sizeof(Image)); image->type = IBITMAP; - image->title = NULL; newRGBMapData(&(image->rgb), (unsigned int)2); *(image->rgb.red)= *(image->rgb.green) = *(image->rgb.blue)= 65535; *(image->rgb.red + 1)= *(image->rgb.green + 1) = *(image->rgb.blue + 1)= 0; @@ -80,7 +68,7 @@ Image *newBitImage(unsigned int width, unsigned int height) image->height = height; image->depth = 1; linelen = ((width + 7) / 8); - image->data = (unsigned char *)lcalloc(linelen * height); + image->data = (unsigned char *)checked_calloc(linelen * height); return(image); } @@ -93,25 +81,19 @@ Image *newRGBImage(unsigned int width, unsigned int height, unsigned int depth) depth = 1; /* sometimes interpreted as `one color' */ pixlen = ((depth+7) / 8); numcolors = depthToColors(depth); - image = (Image *)lmalloc(sizeof(Image)); + image = (Image *)checked_malloc(sizeof(Image)); image->type = IRGB; - image->title = NULL; newRGBMapData(&(image->rgb), numcolors); image->width = width; image->height = height; image->depth = depth; image->pixlen = pixlen; - image->data = (unsigned char *)lmalloc(width * height * pixlen); + image->data = (unsigned char *)checked_malloc(width * height * pixlen); return(image); } void freeImageData(Image *image) { - if (image->title) - { - free((byte *)image->title); - image->title= NULL; - } freeRGBMapData(&(image->rgb)); free(image->data); } @@ -121,37 +103,3 @@ void freeImage(Image *image) freeImageData(image); free((byte *)image); } - -byte *lmalloc(unsigned int size) -{ - byte *area; - - if (size == 0) - { - size = 1; - } - if (!(area = (byte *)malloc(size))) - { - fprintf(stderr, "Out of memory!\n"); - exit(1); - } - - return(area); -} - -byte *lcalloc(unsigned int size) -{ - byte *area; - - if (size == 0) - { - size = 1; - } - if (!(area = (byte *)calloc(1, size))) - { - fprintf(stderr, "Out of memory!\n"); - exit(1); - } - - return(area); -} -- 2.34.1