diff options
-rw-r--r-- | Makefile | 10 | ||||
-rw-r--r-- | README | 1 | ||||
-rw-r--r-- | img.c | 264 | ||||
-rw-r--r-- | img3d.c | 152 |
4 files changed, 427 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..48374da --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +all: img img3d + +img: img.c + cc -I/usr/openwin/include -L/usr/openwin/lib -O -o img img.c -lm -lX11 + +img3d: img3d.c + cc -I/usr/openwin/include -L/usr/openwin/lib -O -o img3d img3d.c -lm -lX11 + +clean: + rm -f img img3d core @@ -0,0 +1 @@ +Do 'img -help' or 'img3d -help' to see key and mouse button usage. @@ -0,0 +1,264 @@ +#include <X11/X.h> +#include <X11/Xlib.h> +#include <X11/keysym.h> +#include <X11/Xutil.h> + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +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)<SIZEX)||((height/MAGFAC)<SIZEY)) + do + { + MAGFAC--; + } while (((width/MAGFAC)<SIZEX)|| + ((height/MAGFAC)<SIZEY)); + } + else + { + MAGFAC--; + + if (MAGFAC==0) + { + MAGFAC=2; + shrink=True; + } + } + + XResizeWindow(display,window,DESTWIDTH,DESTHEIGHT); + break; + + case 3: + XCloseDisplay(display); + exit(0); + break; + } + break; + + case KeyPress: + switch (XLookupKeysym ((XKeyEvent*)&ev,ShiftMapIndex)) + { + case XK_A: + case XK_a: + AVGMODE=!AVGMODE; + break; + } + break; + + default: + break; + } + } + + XQueryPointer(display,window,&rw,&cw,&rx,&ry,&cx,&cy,&bmask); + + rx-=SRCWIDTH/2; + ry-=SRCHEIGHT/2; + + if (rx<0) + rx=0; + + if (ry<0) + ry=0; + + if ((rx+SRCWIDTH)>=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<SRCWIDTH;x++) + for(y=0;y<SRCHEIGHT;y++) + { + b=XGetPixel(src,x,y); + bx=x*MAGFAC; + by=y*MAGFAC; + + for(sx=0;sx<MAGFAC;sx++) + for(sy=0;sy<MAGFAC;sy++) + XPutPixel(dest,bx+sx,by+sy,b); + } +} + + +void ShrinkImg(XImage *src, XImage *dest) +{ + int w; + int sx,sy,x,y,bx,by; + ulong b,prev; + + w=MAGFAC*MAGFAC; + + if (!AVGMODE) + { + for(x=0,bx=0;x<SRCWIDTH;x+=MAGFAC,bx++) + for(y=0,by=0;y<SRCHEIGHT;y+=MAGFAC,by++) + XPutPixel(dest,bx,by,XGetPixel(src,x,y)); + } + else + { + prev=white; + + for(y=0;y<DESTHEIGHT;y++) + for(x=0;x<DESTWIDTH;x++) + { + bx=x*MAGFAC; + by=y*MAGFAC; + b=prev; + + for(sx=0;sx<MAGFAC;sx++) + for(sy=0;sy<MAGFAC;sy++) + if (XGetPixel(src,bx+sx,by+sy)!=prev) + b=XGetPixel(src,bx+sx,by+sy); + + XPutPixel(dest,x,y,b); + + prev=b; + } + } +} @@ -0,0 +1,152 @@ +#include <X11/X.h> +#include <X11/Xlib.h> +#include <X11/Xutil.h> + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +typedef unsigned long ulong; + +#define SIZEX 400 +#define SIZEX2 (SIZEX/2) +#define SIZEY 400 +#define SIZEY2 (SIZEY/2) + +static int SCANSCALE =64; + +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 ScaleImg(XImage *dest, XImage *src); + char str[180]; + Window rw,cw; + int rx,ry,cx,cy,bmask; + int f; + + if (argc>1) + if (!strcmp(argv[1],"-help")) + { + fprintf(stderr,"Keys :\n"); + fprintf(stderr,"LMB - Lower viewing angle\n"); + fprintf(stderr,"MMB - Raise viewing angle\n"); + fprintf(stderr,"RMB - Quit\n"); + } + + if (!(display=XOpenDisplay(NULL))) + { + fprintf(stderr,"%s: couldn't open display\n",argv[0]); + exit(1); + } + + root=DefaultRootWindow(display); + root=RootWindow(display,DefaultScreen(display)); + width=DisplayWidth(display,DefaultScreen(display)); + height=DisplayHeight(display,DefaultScreen(display)); + + window=XCreateSimpleWindow (display,root,0,800,SIZEX,SIZEY,0,0,0); + + gc=XCreateGC(display,window,0L,NULL); + + XSetBackground(display,gc,white=WhitePixel(display,0)); + XSetForeground(display,gc,black=BlackPixel(display,0)); + XSetPlaneMask(display,gc,AllPlanes); + + XSelectInput (display,window,ButtonPressMask); + XMapWindow(display,window); + + while(True) + { + if (XCheckTypedWindowEvent(display,window,ButtonPress,&ev)) + switch(ev.xbutton.button) + { + case 1: + SCANSCALE=SCANSCALE>>1; + + if (SCANSCALE<1) + SCANSCALE=1; + break; + + case 2: + SCANSCALE=SCANSCALE<<1; + break; + + case 3: + XCloseDisplay(display); + exit(0); + break; + } + + XQueryPointer(display,window,&rw,&cw,&rx,&ry,&cx,&cy,&bmask); + + rx-=SIZEX/2; + ry-=SIZEY/2; + + if (rx<0) + rx=0; + + if (ry<0) + ry=0; + + if ((rx+SIZEX)>=width) + rx=width-SIZEX; + + if ((ry+SIZEY)>=height) + ry=height-SIZEY; + + src=XGetImage(display,root,rx,ry,SIZEX,SIZEY,AllPlanes,ZPixmap); + + /* Cheat to create an image we know will be the same as the source... + */ + dest=XGetImage(display,root,0,0,SIZEX,SIZEY,AllPlanes,ZPixmap); + + ScaleImg(src,dest); + + XPutImage(display,window,gc,dest,0,0,0,0,SIZEX,SIZEY); + + XDestroyImage (src); + XDestroyImage (dest); + /* XSync(display,False); */ + } +} + + +int ScaleX (double n, double sc) +{ + return SIZEX2-(int)((SIZEX2-n)/sc); +} + + +int ScaleY (double n, double sc) +{ + return SIZEY2-(int)((SIZEY2-n)/sc); +} + + +void ScaleImg(XImage *src, XImage *dest) +{ + int x,y; + double sc; + + sc=1.0; + + for(y=0;y<SIZEY;y++) + { + for(x=0;x<SIZEX;x++) + if (sc==1) + XPutPixel(dest,x,y,XGetPixel(src,x,y)); + else + XPutPixel(dest,x,y,XGetPixel(src,ScaleX((double)x,sc), + ScaleY((double)y,sc))); + + sc+=(1.0/SCANSCALE); + } +} |