diff options
Diffstat (limited to 'graphic.c')
-rw-r--r-- | graphic.c | 149 |
1 files changed, 96 insertions, 53 deletions
@@ -17,11 +17,6 @@ #include <X11/StringDefs.h> #endif -#ifdef OPENGL -#include <GL/gl.h> -#include <GL/glx.h> -#endif /* OPENGL */ - #ifdef TCL_WRAPPER #include <tk.h> #endif @@ -110,12 +105,13 @@ short *collect_graphics(short *pagelist) /* Otherwise, return TRUE. */ /*----------------------------------------------------------------------*/ +#ifndef HAVE_CAIRO Boolean transform_graphic(graphicptr gp) { - int width, height, twidth, theight, rotation; - float scale, tscale; + int width, height, twidth, theight; + float scale, tscale, rotation, crot; double cosr, sinr; - int x, y, c, s, hw, hh, thw, thh, xorig, yorig, crot, xc, yc; + int x, y, c, s, hw, hh, thw, thh, xorig, yorig, xc, yc; int screen = DefaultScreen(dpy); static GC cmgc = (GC)NULL; @@ -123,8 +119,8 @@ Boolean transform_graphic(graphicptr gp) scale = gp->scale * tscale; rotation = gp->rotation + UTopRotation(); - if (rotation >= 360) rotation -= 360; - else if (rotation < 0) rotation += 360; + if (rotation >= 360.0) rotation -= 360.0; + else if (rotation < 0.0) rotation += 360.0; /* Check if the top-level rotation and scale match the */ /* saved target image. If so, then we're done. */ @@ -139,8 +135,8 @@ Boolean transform_graphic(graphicptr gp) /* that fits the rotated and scaled image. */ crot = rotation; - if (crot > 90 && crot < 180) crot = 180 - crot; - if (crot > 270 && crot < 360) crot = 360 - crot; + if (crot > 90.0 && crot < 180.0) crot = 180.0 - crot; + if (crot > 270.0 && crot < 360.0) crot = 360.0 - crot; cosr = cos(RADFAC * crot); sinr = sin(RADFAC * crot); width = gp->source->width * scale; @@ -187,7 +183,7 @@ Boolean transform_graphic(graphicptr gp) return FALSE; } - if (rotation != 0) { + if (rotation != 0.0) { gp->clipmask = XCreatePixmap(dpy, areawin->window, twidth, theight, 1); if (cmgc == (GC)NULL) { XGCValues values; @@ -223,12 +219,14 @@ Boolean transform_graphic(graphicptr gp) gp->trot = rotation; return TRUE; } +#endif /* HAVE_CAIRO */ /*----------------------------------------------------------------------*/ /* Draw a graphic image by copying from the image to the window. */ /* Image is centered on the center point of the graphic image. */ /*----------------------------------------------------------------------*/ +#ifndef HAVE_CAIRO void UDrawGraphic(graphicptr gp) { XPoint ppt; @@ -271,6 +269,7 @@ void UDrawGraphic(graphicptr gp) } } } +#endif /* HAVE_CAIRO */ /*----------------------------------------------------------------------*/ /* Allocate space for a new graphic source image of size width x height */ @@ -297,10 +296,7 @@ Imagedata *addnewimage(char *name, int width, int height) else iptr->filename = NULL; /* must be filled in later! */ iptr->refcount = 0; /* no calls yet */ - iptr->image = XCreateImage(dpy, DefaultVisual(dpy, screen), - DefaultDepth(dpy, screen), ZPixmap, 0, NULL, - width, height, 8, 0); - iptr->image->data = (char *)malloc(height * iptr->image->bytes_per_line); + iptr->image = xcImageCreate(width, height); return iptr; } @@ -323,10 +319,6 @@ graphicptr new_graphic(objinstptr destinst, char *filename, int px, int py) FILE *fg; int nr, width, height, imax, x, y, i; /* nf, (jdk) */ char id[5], c, buf[128]; - union { - u_char b[4]; - u_long i; - } pixel; locdestinst = (destinst == NULL) ? areawin->topinstance : destinst; destobject = locdestinst->thisobject; @@ -404,14 +396,13 @@ graphicptr new_graphic(objinstptr destinst, char *filename, int px, int py) iptr = addnewimage(filename, width, height); /* Read the image data from the PPM file */ - - pixel.b[3] = 0; for (y = 0; y < height; y++) for (x = 0; x < width; x++) { - fread(&pixel.b[2], 1, 1, fg); - fread(&pixel.b[1], 1, 1, fg); - fread(&pixel.b[0], 1, 1, fg); - XPutPixel(iptr->image, x, y, pixel.i); + u_char r, g, b; + fread(&r, 1, 1, fg); + fread(&g, 1, 1, fg); + fread(&b, 1, 1, fg); + xcImagePutPixel(iptr->image, x, y, r, g, b); } } @@ -421,14 +412,16 @@ graphicptr new_graphic(objinstptr destinst, char *filename, int px, int py) (*gp)->scale = 1.0; (*gp)->position.x = px; (*gp)->position.y = py; - (*gp)->rotation = 0; + (*gp)->rotation = 0.0; (*gp)->color = DEFAULTCOLOR; (*gp)->passed = NULL; - (*gp)->clipmask = (Pixmap)NULL; (*gp)->source = iptr->image; +#ifndef HAVE_CAIRO (*gp)->target = NULL; - (*gp)->trot = 0; + (*gp)->trot = 0.0; (*gp)->tscale = 0; + (*gp)->clipmask = (Pixmap)NULL; +#endif /* HAVE_CAIRO */ calcbboxvalues(locdestinst, (genericptr *)gp); updatepagebounds(destobject); @@ -453,10 +446,6 @@ graphicptr gradient_field(objinstptr destinst, int px, int py, int c1, int c2) int width, height, imax, x, y, i; int r, g, b, rd, gd, bd; char id[11]; - union { - u_char b[4]; - u_long i; - } pixel; locdestinst = (destinst == NULL) ? areawin->topinstance : destinst; destobject = locdestinst->thisobject; @@ -491,15 +480,13 @@ graphicptr gradient_field(objinstptr destinst, int px, int py, int c1, int c2) gd = (colorlist[c2].color.green >> 8) - g; bd = (colorlist[c2].color.blue >> 8) - b; - pixel.b[3] = 0; - for (y = 0; y < height; y++) { - pixel.b[2] = r + ((y * rd) / (height - 1)); - pixel.b[1] = g + ((y * gd) / (height - 1)); - pixel.b[0] = b + ((y * bd) / (height - 1)); - for (x = 0; x < width; x++) { - XPutPixel(iptr->image, x, y, pixel.i); - } - } + for (y = 0; y < height; y++) + for (x = 0; x < width; x++) { + xcImagePutPixel(iptr->image, x, y, + r + ((y * rd) / (height - 1)), + g + ((y * gd) / (height - 1)), + b + ((y * bd) / (height - 1))); + } iptr->refcount++; NEW_GRAPHIC(gp, destobject); @@ -507,14 +494,16 @@ graphicptr gradient_field(objinstptr destinst, int px, int py, int c1, int c2) (*gp)->scale = 1.0; (*gp)->position.x = px; (*gp)->position.y = py; - (*gp)->rotation = 0; + (*gp)->rotation = 0.0; (*gp)->color = DEFAULTCOLOR; (*gp)->passed = NULL; - (*gp)->clipmask = (Pixmap)NULL; (*gp)->source = iptr->image; +#ifndef HAVE_CAIRO (*gp)->target = NULL; - (*gp)->trot = 0; + (*gp)->trot = 0.0; (*gp)->tscale = 0; + (*gp)->clipmask = (Pixmap)NULL; +#endif /* HAVE_CAIRO */ calcbboxvalues(locdestinst, (genericptr *)gp); updatepagebounds(destobject); @@ -530,7 +519,7 @@ graphicptr gradient_field(objinstptr destinst, int px, int py, int c1, int c2) /* Free memory associated with the XImage structure for a graphic. */ /*----------------------------------------------------------------------*/ -void freeimage(XImage *source) +void freeimage(xcImage *source) { int i, j; Imagedata *iptr; @@ -540,12 +529,7 @@ void freeimage(XImage *source) if (iptr->image == source) { iptr->refcount--; if (iptr->refcount <= 0) { - if (iptr->image->data != NULL) { - /* Free data first, because we used our own malloc() */ - free(iptr->image->data); - iptr->image->data = NULL; - } - XDestroyImage(iptr->image); + xcImageDestroy(iptr->image); free(iptr->filename); /* Remove this from the list of images */ @@ -565,6 +549,7 @@ void freeimage(XImage *source) void freegraphic(graphicptr gp) { +#ifndef HAVE_CAIRO if (gp->target != NULL) { if (gp->target->data != NULL) { /* Free data first, because we used our own malloc() */ @@ -574,7 +559,65 @@ void freegraphic(graphicptr gp) XDestroyImage(gp->target); } if (gp->clipmask != (Pixmap)NULL) XFreePixmap(dpy, gp->clipmask); +#endif /* HAVE_CAIRO */ freeimage(gp->source); } /*----------------------------------------------------------------------*/ +/* A light wrapper around XImage, to a generalized xcImage */ +/*----------------------------------------------------------------------*/ + +#ifndef HAVE_CAIRO +xcImage *xcImageCreate(int width, int height) +{ + int screen = DefaultScreen(dpy); + xcImage *img = XCreateImage(dpy, DefaultVisual(dpy, screen), + DefaultDepth(dpy, screen), ZPixmap, 0, NULL, width, height, 8, 0); + img->data = (char *) malloc(height * img->bytes_per_line); + return img; +} + +void xcImageDestroy(xcImage *img) +{ + free(img->data); + img->data = NULL; + XDestroyImage(img); +} + +int xcImageGetWidth(xcImage *img) +{ + return img->width; +} + +int xcImageGetHeight(xcImage *img) +{ + return img->height; +} + +void xcImagePutPixel(xcImage *img, int x, int y, u_char r, u_char g, u_char b) +{ + union { + u_char b[4]; + u_long i; + } pixel; + pixel.b[3] = 0; + pixel.b[2] = r; + pixel.b[1] = g; + pixel.b[0] = b; + XPutPixel(img, x, y, pixel.i); +} + +void xcImageGetPixel(xcImage *img, int x, int y, u_char *r, u_char *g, + u_char *b) +{ + union { + u_char b[4]; + u_long i; + } pixel; + pixel.i = XGetPixel(img, x, y); + *r = pixel.b[2]; + *g = pixel.b[1]; + *b = pixel.b[0]; +} +#endif /* HAVE_CAIRO */ + |