summaryrefslogtreecommitdiff
path: root/img.c
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2020-05-15 21:42:38 +0000
committerIan C <ianc@noddybox.co.uk>2020-05-15 21:42:38 +0000
commit3ac04d32cd29c535144fbd1f97eea3fafd5c21d6 (patch)
treed6afe573a6bb5b77ab458450a326f0ba1684c311 /img.c
parent3c5ebd9b39c4d0ec7935a67811c4f96a23c49921 (diff)
Now builds cleanly.
Diffstat (limited to 'img.c')
-rw-r--r--img.c264
1 files changed, 264 insertions, 0 deletions
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 <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;
+ }
+ }
+}