From 3ac04d32cd29c535144fbd1f97eea3fafd5c21d6 Mon Sep 17 00:00:00 2001 From: Ian C Date: Fri, 15 May 2020 21:42:38 +0000 Subject: Now builds cleanly. --- img.c | 264 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 264 insertions(+) create mode 100644 img.c (limited to 'img.c') diff --git a/img.c b/img.c new file mode 100644 index 0000000..4971712 --- /dev/null +++ b/img.c @@ -0,0 +1,264 @@ +#include +#include +#include +#include + +#include +#include +#include + +typedef unsigned long ulong; + +static int MAGFAC =1; + +/* The size of source when magnifying - size of destination when shrinking. +*/ +#define SIZEX 256 +#define SIZEY 256 + +static int AVGMODE =False; + +/* Source box size +*/ +#define SRCWIDTH ((shrink) ? (SIZEX*MAGFAC) : (SIZEX)) +#define SRCHEIGHT ((shrink) ? (SIZEY*MAGFAC) : (SIZEY)) + +/* Destination box size +*/ +#define DESTWIDTH ((shrink) ? (SIZEX) : (SIZEX*MAGFAC)) +#define DESTHEIGHT ((shrink) ? (SIZEY) : (SIZEY*MAGFAC)) + +static Display *display; +static Window window,root; +static XImage *src,*dest; +static XEvent ev; +static ulong black,white; +static int width,height,cx,cy; +static int shrink=False; +static GC gc; + +int main(int argc, char *argv[]) +{ + void ShrinkImg(XImage *src, XImage *dest); + void ScaleImg(XImage *src, XImage *dest); + char str[180]; + Window rw,cw; + int rx,ry,cx,cy,px,py,bmask; + int f; + + if (!(display=XOpenDisplay(NULL))) + { + fprintf(stderr,"%s: couldn't open display\n",argv[0]); + exit(1); + } + + if (argc>1) + { + if (!strcmp(argv[1],"-help")) + { + fprintf(stderr,"Keys :\n"); + fprintf(stderr,"LMB - Increase mag factor\n"); + fprintf(stderr,"MMB - Decrease mag factor\n"); + fprintf(stderr,"A - Toggle average mode\n"); + fprintf(stderr,"RMB - Quit\n"); + } + } + + root=DefaultRootWindow(display); + width=DisplayWidth(display,0); + height=DisplayHeight(display,0); + + window=XCreateSimpleWindow (display,root,0,800,DESTWIDTH,DESTHEIGHT,0,0,0); + + gc=XCreateGC(display,window,0L,NULL); + + white=WhitePixel(display,0); + black=BlackPixel(display,0); + XSetBackground(display,gc,white); + XSetForeground(display,gc,white); + XSetPlaneMask(display,gc,AllPlanes); + + XSelectInput (display,window,ButtonPressMask|KeyPressMask); + XMapWindow(display,window); + + while(True) + { + if (XPending(display)) + { + XNextEvent (display,&ev); + + switch(ev.type) + { + case ButtonPress: + switch(ev.xbutton.button) + { + case 1: + if (shrink) + { + MAGFAC=MAGFAC>>1; + + if (MAGFAC<=1) + { + shrink=False; + MAGFAC=1; + } + } + else + MAGFAC++; + + XResizeWindow(display,window,DESTWIDTH,DESTHEIGHT); + break; + + case 2: + if (shrink) + { + MAGFAC=MAGFAC<<1; + if (((width/MAGFAC)=width) + rx=width-SRCWIDTH; + + if ((ry+SRCHEIGHT)>=height) + ry=height-SRCHEIGHT; + + src=XGetImage(display,root,rx,ry,SRCWIDTH,SRCHEIGHT,AllPlanes,ZPixmap); + + /* Cheat to create an image we know will be the same as the source... + */ + dest=XGetImage(display,root,0,0,DESTWIDTH,DESTHEIGHT,AllPlanes,ZPixmap); + + if (shrink) + ShrinkImg(src,dest); + else + ScaleImg(src,dest); + + XPutImage(display,window,gc,dest,0,0,0,0,DESTWIDTH,DESTHEIGHT); + + px=SRCWIDTH/2; + py=SRCHEIGHT/2; + + if (!shrink) + { + sprintf(str,"Pix: %lu (%lx)",XGetPixel(src,px,py),XGetPixel(src,px,py)); + XDrawString(display,window,gc,0,10,str,strlen(str)); + XDrawRectangle(display,window,gc,px*MAGFAC,py*MAGFAC,MAGFAC,MAGFAC); + } + + XDestroyImage (src); + XDestroyImage (dest); + /* XSync(display,False); */ + } + + XCloseDisplay(display); +} + + +/* Note, assumes 1 byte per pixel... +*/ +void ScaleImg(XImage *src, XImage *dest) +{ + int x,y,sx,sy,bx,by; + ulong b; + + for(x=0;x