1 /* File: maid-x11.c */
4 * Copyright (c) 1997 Ben Harrison, and others
6 * This software may be copied and distributed for educational, research,
7 * and not for profit purposes provided that this copyright and statement
8 * are included in all such copies.
14 #include "main/x11-gamma-builder.h"
17 * This file defines some "XImage" manipulation functions for X11.
19 * Original code by Desvignes Sebastien (desvigne@solar12.eerie.fr).
21 * BMP format support by Denis Eropkin (denis@dream.homepage.ru).
23 * Major fixes and cleanup by Ben Harrison (benh@phial.com).
25 * This file is designed to be "included" by "main-x11.c" or "main-xaw.c",
26 * which will have already "included" several relevant header files.
34 * Keysym macros, used on Keysyms to test for classes of symbols
35 * These were stolen from one of the X11 header files
37 * Also appears in "main-x11.c".
40 #define IsKeypadKey(keysym) \
41 (((unsigned)(keysym) >= XK_KP_Space) && ((unsigned)(keysym) <= XK_KP_Equal))
43 #define IsCursorKey(keysym) \
44 (((unsigned)(keysym) >= XK_Home) && ((unsigned)(keysym) < XK_Select))
46 #define IsPFKey(keysym) \
47 (((unsigned)(keysym) >= XK_KP_F1) && ((unsigned)(keysym) <= XK_KP_F4))
49 #define IsFunctionKey(keysym) \
50 (((unsigned)(keysym) >= XK_F1) && ((unsigned)(keysym) <= XK_F35))
52 #define IsMiscFunctionKey(keysym) \
53 (((unsigned)(keysym) >= XK_Select) && ((unsigned)(keysym) < XK_KP_Space))
55 #define IsModifierKey(keysym) \
56 (((unsigned)(keysym) >= XK_Shift_L) && ((unsigned)(keysym) <= XK_Hyper_R))
58 #endif /* IsModifierKey */
62 * Checks if the keysym is a special key or a normal key
63 * Assume that XK_MISCELLANY keysyms are special
65 * Also appears in "main-x11.c".
67 #define IsSpecialKey(keysym) \
68 ((unsigned)(keysym) >= 0xFF00)
71 static bool gamma_table_ready = FALSE;
72 static int gamma_val = 0;
76 * Hack -- Convert an RGB value to an X11 Pixel, or die.
79 static XftColor create_pixel(Display *dpy, byte red, byte green, byte blue)
81 static unsigned long create_pixel(Display *dpy, byte red, byte green, byte blue)
84 Colormap cmap = DefaultColormapOfScreen(DefaultScreenOfDisplay(dpy));
86 if (!gamma_table_ready)
88 concptr str = getenv("ANGBAND_X11_GAMMA");
89 if (str != NULL) gamma_val = atoi(str);
91 gamma_table_ready = TRUE;
93 /* Only need to build the table if gamma exists */
94 if (gamma_val) build_gamma_table(gamma_val);
97 /* Hack -- Gamma Correction */
100 red = gamma_table[red];
101 green = gamma_table[green];
102 blue = gamma_table[blue];
105 /* Build the color */
107 xcolour.red = red * 255;
108 xcolour.green = green * 255;
109 xcolour.blue = blue * 255;
110 xcolour.flags = DoRed | DoGreen | DoBlue;
115 xcol.red = xcolour.red;
116 xcol.green = xcolour.green;
117 xcol.blue = xcolour.blue;
119 if (!XftColorAllocValue(dpy, DefaultVisual(dpy, 0), cmap, &xcol, &color))
121 quit_fmt("Couldn't allocate bitmap color '#%02x%02x%02x'\n",
127 /* Attempt to Allocate the Parsed color */
128 if (!(XAllocColor(dpy, cmap, &xcolour)))
130 quit_fmt("Couldn't allocate bitmap color '#%02x%02x%02x'\n",
134 return (xcolour.pixel);
142 * The Win32 "BITMAPFILEHEADER" type.
144 typedef struct BITMAPFILEHEADER
154 * The Win32 "BITMAPINFOHEADER" type.
156 typedef struct BITMAPINFOHEADER
165 u32b biXPelsPerMeter;
166 u32b biYPelsPerMeter;
172 * The Win32 "RGBQUAD" type.
174 typedef struct RGBQUAD
176 unsigned char b, g, r;
177 unsigned char filler;
181 /*** Helper functions for system independent file loading. ***/
183 static byte get_byte(FILE *fff)
185 /* Get a character, and return it */
186 return (getc(fff) & 0xFF);
189 static void rd_byte(FILE *fff, byte *ip)
194 static void rd_u16b(FILE *fff, u16b *ip)
196 (*ip) = get_byte(fff);
197 (*ip) |= ((u16b)(get_byte(fff)) << 8);
200 static void rd_u32b(FILE *fff, u32b *ip)
202 (*ip) = get_byte(fff);
203 (*ip) |= ((u32b)(get_byte(fff)) << 8);
204 (*ip) |= ((u32b)(get_byte(fff)) << 16);
205 (*ip) |= ((u32b)(get_byte(fff)) << 24);
210 * Read a Win32 BMP file.
212 * This function replaces the old ReadRaw and RemapColors functions.
214 * Assumes that the bitmap has a size such that no padding is needed in
215 * various places. Currently only handles bitmaps with 3 to 256 colors.
217 static XImage *ReadBMP(Display *dpy, char *Name)
219 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
221 int depth = DefaultDepth(dpy, DefaultScreen(dpy));
225 BITMAPFILEHEADER fileheader;
226 BITMAPINFOHEADER infoheader;
240 unsigned long clr_pixels[256];
243 /* Open the BMP file */
244 f = fopen(Name, "r");
252 /* Read the "BITMAPFILEHEADER" */
253 rd_u16b(f, &(fileheader.bfType));
254 rd_u32b(f, &(fileheader.bfSize));
255 rd_u16b(f, &(fileheader.bfReserved1));
256 rd_u16b(f, &(fileheader.bfReserved2));
257 rd_u32b(f, &(fileheader.bfOffBits));
259 /* Read the "BITMAPINFOHEADER" */
260 rd_u32b(f, &(infoheader.biSize));
261 rd_u32b(f, &(infoheader.biWidth));
262 rd_u32b(f, &(infoheader.biHeight));
263 rd_u16b(f, &(infoheader.biPlanes));
264 rd_u16b(f, &(infoheader.biBitCount));
265 rd_u32b(f, &(infoheader.biCompresion));
266 rd_u32b(f, &(infoheader.biSizeImage));
267 rd_u32b(f, &(infoheader.biXPelsPerMeter));
268 rd_u32b(f, &(infoheader.biYPelsPerMeter));
269 rd_u32b(f, &(infoheader.biClrUsed));
270 rd_u32b(f, &(infoheader.biClrImportand));
272 /* Verify the header */
274 (fileheader.bfType != 19778) ||
275 (infoheader.biSize != 40))
277 quit_fmt("Incorrect BMP file format %s", Name);
280 /* The two headers above occupy 54 bytes total */
281 /* The "bfOffBits" field says where the data starts */
282 /* The "biClrUsed" field does not seem to be reliable */
283 /* Compute number of colors recorded */
284 ncol = (fileheader.bfOffBits - 54) / 4;
286 for (i = 0; i < ncol; i++)
290 /* Read an "RGBQUAD" */
291 rd_byte(f, &(clrg.b));
292 rd_byte(f, &(clrg.g));
293 rd_byte(f, &(clrg.r));
294 rd_byte(f, &(clrg.filler));
296 /* Analyze the color */
297 clr_pixels[i] = create_pixel(dpy, clrg.r, clrg.g, clrg.b);
300 /* Determine total bytes needed for image */
302 j = (depth - 1) >> 2;
303 while (j >>= 1) i <<= 1;
304 total = infoheader.biWidth * infoheader.biHeight * i;
306 /* Allocate image memory */
307 C_MAKE(Data, total, char);
309 Res = XCreateImage(dpy, visual, depth, ZPixmap, 0 /*offset*/,
310 Data, infoheader.biWidth, infoheader.biHeight,
311 8 /*bitmap_pad*/, 0 /*bytes_per_line*/);
316 C_KILL(Data, total, char);
321 for (y = 0; y < infoheader.biHeight; y++)
323 int y2 = infoheader.biHeight - y - 1;
325 for (x = 0; x < infoheader.biWidth; x++)
329 /* Verify not at end of file XXX XXX */
330 if (feof(f)) quit_fmt("Unexpected end of file in %s", Name);
332 if (infoheader.biBitCount == 24)
337 /* Verify not at end of file XXX XXX */
338 if (feof(f)) quit_fmt("Unexpected end of file in %s", Name);
340 XPutPixel(Res, x, y2, create_pixel(dpy, ch, c2, c3));
342 else if (infoheader.biBitCount == 8)
344 XPutPixel(Res, x, y2, clr_pixels[ch]);
346 else if (infoheader.biBitCount == 4)
348 XPutPixel(Res, x, y2, clr_pixels[ch / 16]);
350 XPutPixel(Res, x, y2, clr_pixels[ch % 16]);
354 /* Technically 1 bit is legal too */
355 quit_fmt("Illegal biBitCount %d in %s",
356 infoheader.biBitCount, Name);
367 /* ========================================================*/
368 /* Code for smooth icon rescaling from Uwe Siems, Jan 2000 */
369 /* ========================================================*/
372 * to save ourselves some labour, define a maximum expected icon width here:
374 #define MAX_ICON_WIDTH 32
377 /* some static variables for composing and decomposing pixel values into
378 * red, green and blue values
380 static unsigned long redMask, greenMask, blueMask;
381 static int redShift, greenShift, blueShift;
385 * Use smooth rescaling?
387 static bool smoothRescaling = TRUE;
391 * GetScaledRow reads a scan from the given XImage, scales it smoothly
392 * and returns the red, green and blue values in arrays.
393 * The values in this arrays must be divided by a certain value that is
394 * calculated in ScaleIcon.
395 * x, y is the position, iw is the input width and ow the output width
396 * redScan, greenScan and blueScan must be sufficiently sized
398 static void GetScaledRow(XImage *Im, int x, int y, int iw, int ow,
399 unsigned long *redScan, unsigned long *greenScan,
400 unsigned long *blueScan)
402 int xi, si, sifrac, ci, cifrac, addWhole, addFrac;
404 int prevRed, prevGreen, prevBlue, nextRed, nextGreen, nextBlue;
410 for (xi = 0; xi < ow; xi++)
412 pix = XGetPixel(Im, x + xi, y);
413 redScan [xi] = (pix >> redShift) & redMask;
414 greenScan [xi] = (pix >> greenShift) & greenMask;
415 blueScan [xi] = (pix >> blueShift) & blueMask;
420 /* scaling by subsampling (grow) */
423 /* read first pixel: */
424 pix = XGetPixel(Im, x, y);
425 nextRed = (pix >> redShift) & redMask;
426 nextGreen = (pix >> greenShift) & greenMask;
427 nextBlue = (pix >> blueShift) & blueMask;
429 prevGreen = nextGreen;
431 /* si and sifrac give the subsampling position: */
434 /* getNextPix tells us, that we need the next pixel */
437 for (xi = 0; xi <= ow; xi++)
442 prevGreen = nextGreen;
446 /* only get next pixel if in same icon */
447 pix = XGetPixel(Im, si + 1, y);
448 nextRed = (pix >> redShift) & redMask;
449 nextGreen = (pix >> greenShift) & greenMask;
450 nextBlue = (pix >> blueShift) & blueMask;
454 /* calculate subsampled color values: */
455 /* division by ow occurs in ScaleIcon */
456 redScan [xi] = prevRed * (ow - sifrac) + nextRed * sifrac;
457 greenScan [xi] = prevGreen * (ow - sifrac) + nextGreen * sifrac;
458 blueScan [xi] = prevBlue * (ow - sifrac) + nextBlue * sifrac;
460 /* advance sampling position: */
477 /* scaling by averaging (shrink) */
478 /* width of an output pixel in input pixels: */
481 /* start position of the first output pixel: */
484 /* get first input pixel: */
485 pix = XGetPixel(Im, x, y);
486 nextRed = (pix >> redShift) & redMask;
487 nextGreen = (pix >> greenShift) & greenMask;
488 nextBlue = (pix >> blueShift) & blueMask;
489 for (xi = 0; xi < ow; xi++)
491 /* find endpoint of the current output pixel: */
493 cifrac = sifrac + addFrac;
499 /* take fraction of current input pixel (starting segment): */
500 redScan[xi] = nextRed * (ow - sifrac);
501 greenScan[xi] = nextGreen * (ow - sifrac);
502 blueScan[xi] = nextBlue * (ow - sifrac);
504 /* add values for whole pixels: */
507 pix = XGetPixel(Im, si, y);
508 redScan[xi] += ((pix >> redShift) & redMask) *ow;
509 greenScan[xi] += ((pix >> greenShift) & greenMask) *ow;
510 blueScan[xi] += ((pix >> blueShift) & blueMask) *ow;
513 /* add fraction of current input pixel (ending segment): */
516 /* only get next pixel if still in icon: */
517 pix = XGetPixel(Im, si, y);
518 nextRed = (pix >> redShift) & redMask;
519 nextGreen = (pix >> greenShift) & greenMask;
520 nextBlue = (pix >> blueShift) & blueMask;
525 redScan[xi] += nextRed * sifrac;
526 greenScan[xi] += nextGreen * sifrac;
527 blueScan[xi] += nextBlue * sifrac;
535 * PutRGBScan takes arrays for red, green and blue and writes pixel values
536 * according to this values in the XImage-structure. w is the number of
537 * pixels to write and div is the value by which all red/green/blue values
540 static void PutRGBScan(XImage *Im, int x, int y, int w, int div,
541 unsigned long *redScan, unsigned long *greenScan,
542 unsigned long *blueScan)
546 unsigned long adj = div / 2;
547 for (xi = 0; xi < w; xi++)
549 pix = (((((redScan[xi] + adj) / div) & redMask) << redShift) +
550 ((((greenScan[xi] + adj) / div) & greenMask) << greenShift) +
551 ((((blueScan[xi] + adj) / div) & blueMask) << blueShift));
552 XPutPixel(Im, x + xi, y, pix);
558 * ScaleIcon transfers an area from XImage ImIn, locate (x1,y1) to ImOut,
560 * Source size is (ix, iy) and destination size is (ox, oy).
561 * It does this by getting icon scan line from GetScaledScan and handling
562 * them the same way as pixels are handled in GetScaledScan.
563 * This even allows icons to be scaled differently in horizontal and
564 * vertical directions (eg. shrink horizontal, grow vertical).
566 static void ScaleIcon(XImage *ImIn, XImage *ImOut,
567 int x1, int y1, int x2, int y2,
568 int ix, int iy, int ox, int oy)
571 int xi, yi, si, sifrac, ci, cifrac, addWhole, addFrac;
573 /* buffers for pixel rows: */
574 unsigned long prevRed [MAX_ICON_WIDTH];
575 unsigned long prevGreen [MAX_ICON_WIDTH];
576 unsigned long prevBlue [MAX_ICON_WIDTH];
577 unsigned long nextRed [MAX_ICON_WIDTH];
578 unsigned long nextGreen [MAX_ICON_WIDTH];
579 unsigned long nextBlue [MAX_ICON_WIDTH];
580 unsigned long tempRed [MAX_ICON_WIDTH];
581 unsigned long tempGreen [MAX_ICON_WIDTH];
582 unsigned long tempBlue [MAX_ICON_WIDTH];
586 /* get divider value for the horizontal scaling: */
596 /* no scaling needed vertically: */
597 for (yi = 0; yi < oy; yi++)
599 GetScaledRow(ImIn, x1, y1 + yi, ix, ox,
600 tempRed, tempGreen, tempBlue);
601 PutRGBScan(ImOut, x2, y2 + yi, ox, div,
602 tempRed, tempGreen, tempBlue);
607 /* scaling by subsampling (grow): */
612 GetScaledRow(ImIn, x1, y1, ix, ox, nextRed, nextGreen, nextBlue);
613 /* si and sifrac give the subsampling position: */
616 /* getNextRow tells us, that we need the next row */
618 for (yi = 0; yi <= oy; yi++)
622 for (xi = 0; xi < ox; xi++)
624 prevRed[xi] = nextRed[xi];
625 prevGreen[xi] = nextGreen[xi];
626 prevBlue[xi] = nextBlue[xi];
630 /* only get next row if in same icon */
631 GetScaledRow(ImIn, x1, si + 1, ix, ox,
632 nextRed, nextGreen, nextBlue);
636 /* calculate subsampled color values: */
637 /* division by oy occurs in PutRGBScan */
638 for (xi = 0; xi < ox; xi++)
640 tempRed[xi] = (prevRed[xi] * (oy - sifrac) +
641 nextRed[xi] * sifrac);
642 tempGreen[xi] = (prevGreen[xi] * (oy - sifrac) +
643 nextGreen[xi] * sifrac);
644 tempBlue[xi] = (prevBlue[xi] * (oy - sifrac) +
645 nextBlue[xi] * sifrac);
648 /* write row to output image: */
649 PutRGBScan(ImOut, x2, y2 + yi, ox, div,
650 tempRed, tempGreen, tempBlue);
652 /* advance sampling position: */
669 /* scaling by averaging (shrink) */
671 /* height of a output row in input rows: */
674 /* start position of the first output row: */
677 /* get first input row: */
678 GetScaledRow(ImIn, x1, y1, ix, ox, nextRed, nextGreen, nextBlue);
679 for (yi = 0; yi < oy; yi++)
681 /* find endpoint of the current output row: */
683 cifrac = sifrac + addFrac;
689 /* take fraction of current input row (starting segment): */
690 for (xi = 0; xi < ox; xi++)
692 tempRed[xi] = nextRed[xi] * (oy - sifrac);
693 tempGreen[xi] = nextGreen[xi] * (oy - sifrac);
694 tempBlue[xi] = nextBlue[xi] * (oy - sifrac);
697 /* add values for whole pixels: */
700 GetScaledRow(ImIn, x1, si, ix, ox,
701 nextRed, nextGreen, nextBlue);
702 for (xi = 0; xi < ox; xi++)
704 tempRed[xi] += nextRed[xi] * oy;
705 tempGreen[xi] += nextGreen[xi] * oy;
706 tempBlue[xi] += nextBlue[xi] * oy;
710 /* add fraction of current input row (ending segment): */
713 /* only get next row if still in icon: */
714 GetScaledRow(ImIn, x1, si, ix, ox,
715 nextRed, nextGreen, nextBlue);
718 for (xi = 0; xi < ox; xi++)
720 tempRed[xi] += nextRed[xi] * sifrac;
721 tempGreen[xi] += nextGreen[xi] * sifrac;
722 tempBlue[xi] += nextBlue[xi] * sifrac;
724 /* write row to output image: */
725 PutRGBScan(ImOut, x2, y2 + yi, ox, div,
726 tempRed, tempGreen, tempBlue);
733 static XImage *ResizeImageSmooth(Display *dpy, XImage *Im,
734 int ix, int iy, int ox, int oy)
736 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
738 int width1, height1, width2, height2;
746 height1 = Im->height;
748 width2 = ox * width1 / ix;
749 height2 = oy * height1 / iy;
751 Data = (char *)malloc(width2 * height2 * Im->bits_per_pixel / 8);
753 Tmp = XCreateImage(dpy, visual,
754 Im->depth, ZPixmap, 0, Data, width2, height2,
757 /* compute values for decomposing pixel into color values: */
758 redMask = Im->red_mask;
760 while ((redMask & 1) == 0)
765 greenMask = Im->green_mask;
767 while ((greenMask & 1) == 0)
772 blueMask = Im->blue_mask;
774 while ((blueMask & 1) == 0)
780 /* scale each icon: */
781 for (y1 = 0, y2 = 0; (y1 < height1) && (y2 < height2); y1 += iy, y2 += oy)
783 for (x1 = 0, x2 = 0; (x1 < width1) && (x2 < width2); x1 += ix, x2 += ox)
785 ScaleIcon(Im, Tmp, x1, y1, x2, y2,
795 * Resize an image. XXX XXX XXX
797 * Also appears in "main-xaw.c".
799 static XImage *ResizeImage(Display *dpy, XImage *Im,
800 int ix, int iy, int ox, int oy)
802 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
804 int width1, height1, width2, height2;
805 int x1, x2, y1, y2, Tx, Ty;
806 int *px1, *px2, *dx1, *dx2;
807 int *py1, *py2, *dy1, *dy2;
813 if (smoothRescaling && (ix != ox || iy != oy) &&
814 visual->class == TrueColor)
816 return ResizeImageSmooth(dpy, Im, ix, iy, ox, oy);
820 height1 = Im->height;
822 width2 = ox * width1 / ix;
823 height2 = oy * height1 / iy;
825 Data = (char *)malloc(width2 * height2 * Im->bits_per_pixel / 8);
827 Tmp = XCreateImage(dpy, visual,
828 Im->depth, ZPixmap, 0, Data, width2, height2,
863 for (y1 = 0, y2 = 0; (y1 < height1) && (y2 < height2); )
867 for (x1 = 0, x2 = 0; (x1 < width1) && (x2 < width2); )
869 XPutPixel(Tmp, x2, y2, XGetPixel(Im, x1, y1));
894 #endif /* !USE_XFT */