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.
15 * This file defines some "XImage" manipulation functions for X11.
17 * Original code by Desvignes Sebastien (desvigne@solar12.eerie.fr).
19 * BMP format support by Denis Eropkin (denis@dream.homepage.ru).
21 * Major fixes and cleanup by Ben Harrison (benh@phial.com).
23 * This file is designed to be "included" by "main-x11.c" or "main-xaw.c",
24 * which will have already "included" several relevant header files.
32 * Keysym macros, used on Keysyms to test for classes of symbols
33 * These were stolen from one of the X11 header files
35 * Also appears in "main-x11.c".
38 #define IsKeypadKey(keysym) \
39 (((unsigned)(keysym) >= XK_KP_Space) && ((unsigned)(keysym) <= XK_KP_Equal))
41 #define IsCursorKey(keysym) \
42 (((unsigned)(keysym) >= XK_Home) && ((unsigned)(keysym) < XK_Select))
44 #define IsPFKey(keysym) \
45 (((unsigned)(keysym) >= XK_KP_F1) && ((unsigned)(keysym) <= XK_KP_F4))
47 #define IsFunctionKey(keysym) \
48 (((unsigned)(keysym) >= XK_F1) && ((unsigned)(keysym) <= XK_F35))
50 #define IsMiscFunctionKey(keysym) \
51 (((unsigned)(keysym) >= XK_Select) && ((unsigned)(keysym) < XK_KP_Space))
53 #define IsModifierKey(keysym) \
54 (((unsigned)(keysym) >= XK_Shift_L) && ((unsigned)(keysym) <= XK_Hyper_R))
56 #endif /* IsModifierKey */
60 * Checks if the keysym is a special key or a normal key
61 * Assume that XK_MISCELLANY keysyms are special
63 * Also appears in "main-x11.c".
65 #define IsSpecialKey(keysym) \
66 ((unsigned)(keysym) >= 0xFF00)
70 static bool gamma_table_ready = FALSE;
71 #endif /* SUPPORT_GAMMA */
75 * Hack -- Convert an RGB value to an X11 Pixel, or die.
77 static unsigned long create_pixel(Display *dpy, byte red, byte green, byte blue)
79 Colormap cmap = DefaultColormapOfScreen(DefaultScreenOfDisplay(dpy));
86 if (!gamma_table_ready)
88 cptr str = getenv("ANGBAND_X11_GAMMA");
89 if (str != NULL) gamma = atoi(str);
91 gamma_table_ready = TRUE;
93 /* Only need to build the table if gamma exists */
94 if (gamma) build_gamma_table(gamma);
97 /* Hack -- Gamma Correction */
100 red = gamma_table[red];
101 green = gamma_table[green];
102 blue = gamma_table[blue];
105 #endif /* SUPPORT_GAMMA */
107 /* Build the color */
109 xcolour.red = red * 255;
110 xcolour.green = green * 255;
111 xcolour.blue = blue * 255;
112 xcolour.flags = DoRed | DoGreen | DoBlue;
114 /* Attempt to Allocate the Parsed color */
115 if (!(XAllocColor(dpy, cmap, &xcolour)))
117 quit_fmt("Couldn't allocate bitmap color '#%02x%02x%02x'\n",
121 return (xcolour.pixel);
129 * The Win32 "BITMAPFILEHEADER" type.
131 * Note the "bfAlign" field, which is a complete hack to ensure that the
132 * "u32b" fields in the structure get aligned. Thus, when reading this
133 * header from the file, we must be careful to skip this field.
135 typedef struct BITMAPFILEHEADER
137 u16b bfAlign; /* HATE this */
146 * The Win32 "BITMAPINFOHEADER" type.
148 typedef struct BITMAPINFOHEADER
157 u32b biXPelsPerMeter;
158 u32b biYPelsPerMeter;
164 * The Win32 "RGBQUAD" type.
166 typedef struct RGBQUAD
168 unsigned char b, g, r;
169 unsigned char filler;
174 * Read a Win32 BMP file.
176 * This function replaces the old ReadRaw and RemapColors functions.
178 * Assumes that the bitmap has a size such that no padding is needed in
179 * various places. Currently only handles bitmaps with 3 to 256 colors.
181 static XImage *ReadBMP(Display *dpy, char *Name)
183 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
185 int depth = DefaultDepth(dpy, DefaultScreen(dpy));
189 BITMAPFILEHEADER fileheader;
190 BITMAPINFOHEADER infoheader;
192 vptr fileheaderhack = (vptr)((char *)(&fileheader) + 2);
206 unsigned long clr_pixels[256];
209 /* Open the BMP file */
210 f = fopen(Name, "r");
218 /* Read the "BITMAPFILEHEADER" */
219 fread(fileheaderhack, sizeof(fileheader) - 2, 1, f);
221 /* Read the "BITMAPINFOHEADER" */
222 fread(&infoheader, sizeof(infoheader), 1, f);
224 /* Verify the header */
226 (fileheader.bfType != 19778) ||
227 (infoheader.biSize != 40))
229 quit_fmt("Incorrect BMP file format %s", Name);
232 /* The two headers above occupy 54 bytes total */
233 /* The "bfOffBits" field says where the data starts */
234 /* The "biClrUsed" field does not seem to be reliable */
235 /* Compute number of colors recorded */
236 ncol = (fileheader.bfOffBits - 54) / 4;
238 for (i = 0; i < ncol; i++)
242 fread(&clrg, 4, 1, f);
244 /* Analyze the color */
245 clr_pixels[i] = create_pixel(dpy, clrg.r, clrg.g, clrg.b);
248 /* Determine total bytes needed for image */
250 j = (depth - 1) >> 2;
251 while (j >>= 1) i <<= 1;
252 total = infoheader.biWidth * infoheader.biHeight * i;
254 /* Allocate image memory */
255 C_MAKE(Data, total, char);
257 Res = XCreateImage(dpy, visual, depth, ZPixmap, 0 /*offset*/,
258 Data, infoheader.biWidth, infoheader.biHeight,
259 8 /*bitmap_pad*/, 0 /*bytes_per_line*/);
264 C_KILL(Data, total, char);
269 for (y = 0; y < infoheader.biHeight; y++)
271 int y2 = infoheader.biHeight - y - 1;
273 for (x = 0; x < infoheader.biWidth; x++)
277 /* Verify not at end of file XXX XXX */
278 if (feof(f)) quit_fmt("Unexpected end of file in %s", Name);
280 if (infoheader.biBitCount == 24)
285 /* Verify not at end of file XXX XXX */
286 if (feof(f)) quit_fmt("Unexpected end of file in %s", Name);
288 XPutPixel(Res, x, y2, create_pixel(dpy, ch, c2, c3));
290 else if (infoheader.biBitCount == 8)
292 XPutPixel(Res, x, y2, clr_pixels[ch]);
294 else if (infoheader.biBitCount == 4)
296 XPutPixel(Res, x, y2, clr_pixels[ch / 16]);
298 XPutPixel(Res, x, y2, clr_pixels[ch % 16]);
302 /* Technically 1 bit is legal too */
303 quit_fmt("Illegal biBitCount %d in %s",
304 infoheader.biBitCount, Name);
315 /* ========================================================*/
316 /* Code for smooth icon rescaling from Uwe Siems, Jan 2000 */
317 /* ========================================================*/
320 * to save ourselves some labour, define a maximum expected icon width here:
322 #define MAX_ICON_WIDTH 32
325 /* some static variables for composing and decomposing pixel values into
326 * red, green and blue values
328 static unsigned long redMask, greenMask, blueMask;
329 static int redShift, greenShift, blueShift;
333 * Use smooth rescaling?
335 static bool smoothRescaling = TRUE;
339 * GetScaledRow reads a scan from the given XImage, scales it smoothly
340 * and returns the red, green and blue values in arrays.
341 * The values in this arrays must be divided by a certain value that is
342 * calculated in ScaleIcon.
343 * x, y is the position, iw is the input width and ow the output width
344 * redScan, greenScan and blueScan must be sufficiently sized
346 static void GetScaledRow(XImage *Im, int x, int y, int iw, int ow,
347 unsigned long *redScan, unsigned long *greenScan,
348 unsigned long *blueScan)
350 int xi, si, sifrac, ci, cifrac, addWhole, addFrac;
352 int prevRed, prevGreen, prevBlue, nextRed, nextGreen, nextBlue;
358 for (xi = 0; xi < ow; xi++)
360 pix = XGetPixel(Im, x + xi, y);
361 redScan [xi] = (pix >> redShift) & redMask;
362 greenScan [xi] = (pix >> greenShift) & greenMask;
363 blueScan [xi] = (pix >> blueShift) & blueMask;
368 /* scaling by subsampling (grow) */
371 /* read first pixel: */
372 pix = XGetPixel(Im, x, y);
373 nextRed = (pix >> redShift) & redMask;
374 nextGreen = (pix >> greenShift) & greenMask;
375 nextBlue = (pix >> blueShift) & blueMask;
377 prevGreen = nextGreen;
379 /* si and sifrac give the subsampling position: */
382 /* getNextPix tells us, that we need the next pixel */
385 for (xi = 0; xi <= ow; xi++)
390 prevGreen = nextGreen;
394 /* only get next pixel if in same icon */
395 pix = XGetPixel(Im, si + 1, y);
396 nextRed = (pix >> redShift) & redMask;
397 nextGreen = (pix >> greenShift) & greenMask;
398 nextBlue = (pix >> blueShift) & blueMask;
402 /* calculate subsampled color values: */
403 /* division by ow occurs in ScaleIcon */
404 redScan [xi] = prevRed * (ow - sifrac) + nextRed * sifrac;
405 greenScan [xi] = prevGreen * (ow - sifrac) + nextGreen * sifrac;
406 blueScan [xi] = prevBlue * (ow - sifrac) + nextBlue * sifrac;
408 /* advance sampling position: */
425 /* scaling by averaging (shrink) */
426 /* width of an output pixel in input pixels: */
429 /* start position of the first output pixel: */
432 /* get first input pixel: */
433 pix = XGetPixel(Im, x, y);
434 nextRed = (pix >> redShift) & redMask;
435 nextGreen = (pix >> greenShift) & greenMask;
436 nextBlue = (pix >> blueShift) & blueMask;
437 for (xi = 0; xi < ow; xi++)
439 /* find endpoint of the current output pixel: */
441 cifrac = sifrac + addFrac;
447 /* take fraction of current input pixel (starting segment): */
448 redScan[xi] = nextRed * (ow - sifrac);
449 greenScan[xi] = nextGreen * (ow - sifrac);
450 blueScan[xi] = nextBlue * (ow - sifrac);
452 /* add values for whole pixels: */
455 pix = XGetPixel(Im, si, y);
456 redScan[xi] += ((pix >> redShift) & redMask) *ow;
457 greenScan[xi] += ((pix >> greenShift) & greenMask) *ow;
458 blueScan[xi] += ((pix >> blueShift) & blueMask) *ow;
461 /* add fraction of current input pixel (ending segment): */
464 /* only get next pixel if still in icon: */
465 pix = XGetPixel(Im, si, y);
466 nextRed = (pix >> redShift) & redMask;
467 nextGreen = (pix >> greenShift) & greenMask;
468 nextBlue = (pix >> blueShift) & blueMask;
473 redScan[xi] += nextRed * sifrac;
474 greenScan[xi] += nextGreen * sifrac;
475 blueScan[xi] += nextBlue * sifrac;
483 * PutRGBScan takes arrays for red, green and blue and writes pixel values
484 * according to this values in the XImage-structure. w is the number of
485 * pixels to write and div is the value by which all red/green/blue values
488 static void PutRGBScan(XImage *Im, int x, int y, int w, int div,
489 unsigned long *redScan, unsigned long *greenScan,
490 unsigned long *blueScan)
494 unsigned long adj = div / 2;
495 for (xi = 0; xi < w; xi++)
497 pix = (((((redScan[xi] + adj) / div) & redMask) << redShift) +
498 ((((greenScan[xi] + adj) / div) & greenMask) << greenShift) +
499 ((((blueScan[xi] + adj) / div) & blueMask) << blueShift));
500 XPutPixel(Im, x + xi, y, pix);
506 * ScaleIcon transfers an area from XImage ImIn, locate (x1,y1) to ImOut,
508 * Source size is (ix, iy) and destination size is (ox, oy).
509 * It does this by getting icon scan line from GetScaledScan and handling
510 * them the same way as pixels are handled in GetScaledScan.
511 * This even allows icons to be scaled differently in horizontal and
512 * vertical directions (eg. shrink horizontal, grow vertical).
514 static void ScaleIcon(XImage *ImIn, XImage *ImOut,
515 int x1, int y1, int x2, int y2,
516 int ix, int iy, int ox, int oy)
519 int xi, yi, si, sifrac, ci, cifrac, addWhole, addFrac;
521 /* buffers for pixel rows: */
522 unsigned long prevRed [MAX_ICON_WIDTH];
523 unsigned long prevGreen [MAX_ICON_WIDTH];
524 unsigned long prevBlue [MAX_ICON_WIDTH];
525 unsigned long nextRed [MAX_ICON_WIDTH];
526 unsigned long nextGreen [MAX_ICON_WIDTH];
527 unsigned long nextBlue [MAX_ICON_WIDTH];
528 unsigned long tempRed [MAX_ICON_WIDTH];
529 unsigned long tempGreen [MAX_ICON_WIDTH];
530 unsigned long tempBlue [MAX_ICON_WIDTH];
534 /* get divider value for the horizontal scaling: */
544 /* no scaling needed vertically: */
545 for (yi = 0; yi < oy; yi++)
547 GetScaledRow(ImIn, x1, y1 + yi, ix, ox,
548 tempRed, tempGreen, tempBlue);
549 PutRGBScan(ImOut, x2, y2 + yi, ox, div,
550 tempRed, tempGreen, tempBlue);
555 /* scaling by subsampling (grow): */
560 GetScaledRow(ImIn, x1, y1, ix, ox, nextRed, nextGreen, nextBlue);
561 /* si and sifrac give the subsampling position: */
564 /* getNextRow tells us, that we need the next row */
566 for (yi = 0; yi <= oy; yi++)
570 for (xi = 0; xi < ox; xi++)
572 prevRed[xi] = nextRed[xi];
573 prevGreen[xi] = nextGreen[xi];
574 prevBlue[xi] = nextBlue[xi];
578 /* only get next row if in same icon */
579 GetScaledRow(ImIn, x1, si + 1, ix, ox,
580 nextRed, nextGreen, nextBlue);
584 /* calculate subsampled color values: */
585 /* division by oy occurs in PutRGBScan */
586 for (xi = 0; xi < ox; xi++)
588 tempRed[xi] = (prevRed[xi] * (oy - sifrac) +
589 nextRed[xi] * sifrac);
590 tempGreen[xi] = (prevGreen[xi] * (oy - sifrac) +
591 nextGreen[xi] * sifrac);
592 tempBlue[xi] = (prevBlue[xi] * (oy - sifrac) +
593 nextBlue[xi] * sifrac);
596 /* write row to output image: */
597 PutRGBScan(ImOut, x2, y2 + yi, ox, div,
598 tempRed, tempGreen, tempBlue);
600 /* advance sampling position: */
617 /* scaling by averaging (shrink) */
619 /* height of a output row in input rows: */
622 /* start position of the first output row: */
625 /* get first input row: */
626 GetScaledRow(ImIn, x1, y1, ix, ox, nextRed, nextGreen, nextBlue);
627 for (yi = 0; yi < oy; yi++)
629 /* find endpoint of the current output row: */
631 cifrac = sifrac + addFrac;
637 /* take fraction of current input row (starting segment): */
638 for (xi = 0; xi < ox; xi++)
640 tempRed[xi] = nextRed[xi] * (oy - sifrac);
641 tempGreen[xi] = nextGreen[xi] * (oy - sifrac);
642 tempBlue[xi] = nextBlue[xi] * (oy - sifrac);
645 /* add values for whole pixels: */
648 GetScaledRow(ImIn, x1, si, ix, ox,
649 nextRed, nextGreen, nextBlue);
650 for (xi = 0; xi < ox; xi++)
652 tempRed[xi] += nextRed[xi] * oy;
653 tempGreen[xi] += nextGreen[xi] * oy;
654 tempBlue[xi] += nextBlue[xi] * oy;
658 /* add fraction of current input row (ending segment): */
661 /* only get next row if still in icon: */
662 GetScaledRow(ImIn, x1, si, ix, ox,
663 nextRed, nextGreen, nextBlue);
666 for (xi = 0; xi < ox; xi++)
668 tempRed[xi] += nextRed[xi] * sifrac;
669 tempGreen[xi] += nextGreen[xi] * sifrac;
670 tempBlue[xi] += nextBlue[xi] * sifrac;
672 /* write row to output image: */
673 PutRGBScan(ImOut, x2, y2 + yi, ox, div,
674 tempRed, tempGreen, tempBlue);
681 static XImage *ResizeImageSmooth(Display *dpy, XImage *Im,
682 int ix, int iy, int ox, int oy)
684 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
686 int width1, height1, width2, height2;
694 height1 = Im->height;
696 width2 = ox * width1 / ix;
697 height2 = oy * height1 / iy;
699 Data = (char *)malloc(width2 * height2 * Im->bits_per_pixel / 8);
701 Tmp = XCreateImage(dpy, visual,
702 Im->depth, ZPixmap, 0, Data, width2, height2,
705 /* compute values for decomposing pixel into color values: */
706 redMask = Im->red_mask;
708 while ((redMask & 1) == 0)
713 greenMask = Im->green_mask;
715 while ((greenMask & 1) == 0)
720 blueMask = Im->blue_mask;
722 while ((blueMask & 1) == 0)
728 /* scale each icon: */
729 for (y1 = 0, y2 = 0; (y1 < height1) && (y2 < height2); y1 += iy, y2 += oy)
731 for (x1 = 0, x2 = 0; (x1 < width1) && (x2 < width2); x1 += ix, x2 += ox)
733 ScaleIcon(Im, Tmp, x1, y1, x2, y2,
743 * Resize an image. XXX XXX XXX
745 * Also appears in "main-xaw.c".
747 static XImage *ResizeImage(Display *dpy, XImage *Im,
748 int ix, int iy, int ox, int oy)
750 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
752 int width1, height1, width2, height2;
753 int x1, x2, y1, y2, Tx, Ty;
754 int *px1, *px2, *dx1, *dx2;
755 int *py1, *py2, *dy1, *dy2;
761 if (smoothRescaling && (ix != ox || iy != oy) &&
762 visual->class == TrueColor)
764 return ResizeImageSmooth(dpy, Im, ix, iy, ox, oy);
768 height1 = Im->height;
770 width2 = ox * width1 / ix;
771 height2 = oy * height1 / iy;
773 Data = (char *)malloc(width2 * height2 * Im->bits_per_pixel / 8);
775 Tmp = XCreateImage(dpy, visual,
776 Im->depth, ZPixmap, 0, Data, width2, height2,
811 for (y1 = 0, y2 = 0; (y1 < height1) && (y2 < height2); )
815 for (x1 = 0, x2 = 0; (x1 < width1) && (x2 < width2); )
817 XPutPixel(Tmp, x2, y2, XGetPixel(Im, x1, y1));
842 #endif /* USE_GRAPHICS */