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.
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)
72 static bool gamma_table_ready = FALSE;
73 static int gamma_val = 0;
74 #endif /* SUPPORT_GAMMA */
78 * Hack -- Convert an RGB value to an X11 Pixel, or die.
80 static unsigned long create_pixel(Display *dpy, byte red, byte green, byte blue)
82 Colormap cmap = DefaultColormapOfScreen(DefaultScreenOfDisplay(dpy));
87 if (!gamma_table_ready)
89 concptr str = getenv("ANGBAND_X11_GAMMA");
90 if (str != NULL) gamma_val = atoi(str);
92 gamma_table_ready = TRUE;
94 /* Only need to build the table if gamma exists */
95 if (gamma_val) build_gamma_table(gamma_val);
98 /* Hack -- Gamma Correction */
101 red = gamma_table[red];
102 green = gamma_table[green];
103 blue = gamma_table[blue];
106 #endif /* SUPPORT_GAMMA */
108 /* Build the color */
110 xcolour.red = red * 255;
111 xcolour.green = green * 255;
112 xcolour.blue = blue * 255;
113 xcolour.flags = DoRed | DoGreen | DoBlue;
115 /* Attempt to Allocate the Parsed color */
116 if (!(XAllocColor(dpy, cmap, &xcolour)))
118 quit_fmt("Couldn't allocate bitmap color '#%02x%02x%02x'\n",
122 return (xcolour.pixel);
130 * The Win32 "BITMAPFILEHEADER" type.
132 typedef struct BITMAPFILEHEADER
142 * The Win32 "BITMAPINFOHEADER" type.
144 typedef struct BITMAPINFOHEADER
153 u32b biXPelsPerMeter;
154 u32b biYPelsPerMeter;
160 * The Win32 "RGBQUAD" type.
162 typedef struct RGBQUAD
164 unsigned char b, g, r;
165 unsigned char filler;
169 /*** Helper functions for system independent file loading. ***/
171 static byte get_byte(FILE *fff)
173 /* Get a character, and return it */
174 return (getc(fff) & 0xFF);
177 static void rd_byte(FILE *fff, byte *ip)
182 static void rd_u16b(FILE *fff, u16b *ip)
184 (*ip) = get_byte(fff);
185 (*ip) |= ((u16b)(get_byte(fff)) << 8);
188 static void rd_u32b(FILE *fff, u32b *ip)
190 (*ip) = get_byte(fff);
191 (*ip) |= ((u32b)(get_byte(fff)) << 8);
192 (*ip) |= ((u32b)(get_byte(fff)) << 16);
193 (*ip) |= ((u32b)(get_byte(fff)) << 24);
198 * Read a Win32 BMP file.
200 * This function replaces the old ReadRaw and RemapColors functions.
202 * Assumes that the bitmap has a size such that no padding is needed in
203 * various places. Currently only handles bitmaps with 3 to 256 colors.
205 static XImage *ReadBMP(Display *dpy, char *Name)
207 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
209 int depth = DefaultDepth(dpy, DefaultScreen(dpy));
213 BITMAPFILEHEADER fileheader;
214 BITMAPINFOHEADER infoheader;
228 unsigned long clr_pixels[256];
231 /* Open the BMP file */
232 f = fopen(Name, "r");
240 /* Read the "BITMAPFILEHEADER" */
241 rd_u16b(f, &(fileheader.bfType));
242 rd_u32b(f, &(fileheader.bfSize));
243 rd_u16b(f, &(fileheader.bfReserved1));
244 rd_u16b(f, &(fileheader.bfReserved2));
245 rd_u32b(f, &(fileheader.bfOffBits));
247 /* Read the "BITMAPINFOHEADER" */
248 rd_u32b(f, &(infoheader.biSize));
249 rd_u32b(f, &(infoheader.biWidth));
250 rd_u32b(f, &(infoheader.biHeight));
251 rd_u16b(f, &(infoheader.biPlanes));
252 rd_u16b(f, &(infoheader.biBitCount));
253 rd_u32b(f, &(infoheader.biCompresion));
254 rd_u32b(f, &(infoheader.biSizeImage));
255 rd_u32b(f, &(infoheader.biXPelsPerMeter));
256 rd_u32b(f, &(infoheader.biYPelsPerMeter));
257 rd_u32b(f, &(infoheader.biClrUsed));
258 rd_u32b(f, &(infoheader.biClrImportand));
260 /* Verify the header */
262 (fileheader.bfType != 19778) ||
263 (infoheader.biSize != 40))
265 quit_fmt("Incorrect BMP file format %s", Name);
268 /* The two headers above occupy 54 bytes total */
269 /* The "bfOffBits" field says where the data starts */
270 /* The "biClrUsed" field does not seem to be reliable */
271 /* Compute number of colors recorded */
272 ncol = (fileheader.bfOffBits - 54) / 4;
274 for (i = 0; i < ncol; i++)
278 /* Read an "RGBQUAD" */
279 rd_byte(f, &(clrg.b));
280 rd_byte(f, &(clrg.g));
281 rd_byte(f, &(clrg.r));
282 rd_byte(f, &(clrg.filler));
284 /* Analyze the color */
285 clr_pixels[i] = create_pixel(dpy, clrg.r, clrg.g, clrg.b);
288 /* Determine total bytes needed for image */
290 j = (depth - 1) >> 2;
291 while (j >>= 1) i <<= 1;
292 total = infoheader.biWidth * infoheader.biHeight * i;
294 /* Allocate image memory */
295 C_MAKE(Data, total, char);
297 Res = XCreateImage(dpy, visual, depth, ZPixmap, 0 /*offset*/,
298 Data, infoheader.biWidth, infoheader.biHeight,
299 8 /*bitmap_pad*/, 0 /*bytes_per_line*/);
304 C_KILL(Data, total, char);
309 for (y = 0; y < infoheader.biHeight; y++)
311 int y2 = infoheader.biHeight - y - 1;
313 for (x = 0; x < infoheader.biWidth; x++)
317 /* Verify not at end of file XXX XXX */
318 if (feof(f)) quit_fmt("Unexpected end of file in %s", Name);
320 if (infoheader.biBitCount == 24)
325 /* Verify not at end of file XXX XXX */
326 if (feof(f)) quit_fmt("Unexpected end of file in %s", Name);
328 XPutPixel(Res, x, y2, create_pixel(dpy, ch, c2, c3));
330 else if (infoheader.biBitCount == 8)
332 XPutPixel(Res, x, y2, clr_pixels[ch]);
334 else if (infoheader.biBitCount == 4)
336 XPutPixel(Res, x, y2, clr_pixels[ch / 16]);
338 XPutPixel(Res, x, y2, clr_pixels[ch % 16]);
342 /* Technically 1 bit is legal too */
343 quit_fmt("Illegal biBitCount %d in %s",
344 infoheader.biBitCount, Name);
355 /* ========================================================*/
356 /* Code for smooth icon rescaling from Uwe Siems, Jan 2000 */
357 /* ========================================================*/
360 * to save ourselves some labour, define a maximum expected icon width here:
362 #define MAX_ICON_WIDTH 32
365 /* some static variables for composing and decomposing pixel values into
366 * red, green and blue values
368 static unsigned long redMask, greenMask, blueMask;
369 static int redShift, greenShift, blueShift;
373 * Use smooth rescaling?
375 static bool smoothRescaling = TRUE;
379 * GetScaledRow reads a scan from the given XImage, scales it smoothly
380 * and returns the red, green and blue values in arrays.
381 * The values in this arrays must be divided by a certain value that is
382 * calculated in ScaleIcon.
383 * x, y is the position, iw is the input width and ow the output width
384 * redScan, greenScan and blueScan must be sufficiently sized
386 static void GetScaledRow(XImage *Im, int x, int y, int iw, int ow,
387 unsigned long *redScan, unsigned long *greenScan,
388 unsigned long *blueScan)
390 int xi, si, sifrac, ci, cifrac, addWhole, addFrac;
392 int prevRed, prevGreen, prevBlue, nextRed, nextGreen, nextBlue;
398 for (xi = 0; xi < ow; xi++)
400 pix = XGetPixel(Im, x + xi, y);
401 redScan [xi] = (pix >> redShift) & redMask;
402 greenScan [xi] = (pix >> greenShift) & greenMask;
403 blueScan [xi] = (pix >> blueShift) & blueMask;
408 /* scaling by subsampling (grow) */
411 /* read first pixel: */
412 pix = XGetPixel(Im, x, y);
413 nextRed = (pix >> redShift) & redMask;
414 nextGreen = (pix >> greenShift) & greenMask;
415 nextBlue = (pix >> blueShift) & blueMask;
417 prevGreen = nextGreen;
419 /* si and sifrac give the subsampling position: */
422 /* getNextPix tells us, that we need the next pixel */
425 for (xi = 0; xi <= ow; xi++)
430 prevGreen = nextGreen;
434 /* only get next pixel if in same icon */
435 pix = XGetPixel(Im, si + 1, y);
436 nextRed = (pix >> redShift) & redMask;
437 nextGreen = (pix >> greenShift) & greenMask;
438 nextBlue = (pix >> blueShift) & blueMask;
442 /* calculate subsampled color values: */
443 /* division by ow occurs in ScaleIcon */
444 redScan [xi] = prevRed * (ow - sifrac) + nextRed * sifrac;
445 greenScan [xi] = prevGreen * (ow - sifrac) + nextGreen * sifrac;
446 blueScan [xi] = prevBlue * (ow - sifrac) + nextBlue * sifrac;
448 /* advance sampling position: */
465 /* scaling by averaging (shrink) */
466 /* width of an output pixel in input pixels: */
469 /* start position of the first output pixel: */
472 /* get first input pixel: */
473 pix = XGetPixel(Im, x, y);
474 nextRed = (pix >> redShift) & redMask;
475 nextGreen = (pix >> greenShift) & greenMask;
476 nextBlue = (pix >> blueShift) & blueMask;
477 for (xi = 0; xi < ow; xi++)
479 /* find endpoint of the current output pixel: */
481 cifrac = sifrac + addFrac;
487 /* take fraction of current input pixel (starting segment): */
488 redScan[xi] = nextRed * (ow - sifrac);
489 greenScan[xi] = nextGreen * (ow - sifrac);
490 blueScan[xi] = nextBlue * (ow - sifrac);
492 /* add values for whole pixels: */
495 pix = XGetPixel(Im, si, y);
496 redScan[xi] += ((pix >> redShift) & redMask) *ow;
497 greenScan[xi] += ((pix >> greenShift) & greenMask) *ow;
498 blueScan[xi] += ((pix >> blueShift) & blueMask) *ow;
501 /* add fraction of current input pixel (ending segment): */
504 /* only get next pixel if still in icon: */
505 pix = XGetPixel(Im, si, y);
506 nextRed = (pix >> redShift) & redMask;
507 nextGreen = (pix >> greenShift) & greenMask;
508 nextBlue = (pix >> blueShift) & blueMask;
513 redScan[xi] += nextRed * sifrac;
514 greenScan[xi] += nextGreen * sifrac;
515 blueScan[xi] += nextBlue * sifrac;
523 * PutRGBScan takes arrays for red, green and blue and writes pixel values
524 * according to this values in the XImage-structure. w is the number of
525 * pixels to write and div is the value by which all red/green/blue values
528 static void PutRGBScan(XImage *Im, int x, int y, int w, int div,
529 unsigned long *redScan, unsigned long *greenScan,
530 unsigned long *blueScan)
534 unsigned long adj = div / 2;
535 for (xi = 0; xi < w; xi++)
537 pix = (((((redScan[xi] + adj) / div) & redMask) << redShift) +
538 ((((greenScan[xi] + adj) / div) & greenMask) << greenShift) +
539 ((((blueScan[xi] + adj) / div) & blueMask) << blueShift));
540 XPutPixel(Im, x + xi, y, pix);
546 * ScaleIcon transfers an area from XImage ImIn, locate (x1,y1) to ImOut,
548 * Source size is (ix, iy) and destination size is (ox, oy).
549 * It does this by getting icon scan line from GetScaledScan and handling
550 * them the same way as pixels are handled in GetScaledScan.
551 * This even allows icons to be scaled differently in horizontal and
552 * vertical directions (eg. shrink horizontal, grow vertical).
554 static void ScaleIcon(XImage *ImIn, XImage *ImOut,
555 int x1, int y1, int x2, int y2,
556 int ix, int iy, int ox, int oy)
559 int xi, yi, si, sifrac, ci, cifrac, addWhole, addFrac;
561 /* buffers for pixel rows: */
562 unsigned long prevRed [MAX_ICON_WIDTH];
563 unsigned long prevGreen [MAX_ICON_WIDTH];
564 unsigned long prevBlue [MAX_ICON_WIDTH];
565 unsigned long nextRed [MAX_ICON_WIDTH];
566 unsigned long nextGreen [MAX_ICON_WIDTH];
567 unsigned long nextBlue [MAX_ICON_WIDTH];
568 unsigned long tempRed [MAX_ICON_WIDTH];
569 unsigned long tempGreen [MAX_ICON_WIDTH];
570 unsigned long tempBlue [MAX_ICON_WIDTH];
574 /* get divider value for the horizontal scaling: */
584 /* no scaling needed vertically: */
585 for (yi = 0; yi < oy; yi++)
587 GetScaledRow(ImIn, x1, y1 + yi, ix, ox,
588 tempRed, tempGreen, tempBlue);
589 PutRGBScan(ImOut, x2, y2 + yi, ox, div,
590 tempRed, tempGreen, tempBlue);
595 /* scaling by subsampling (grow): */
600 GetScaledRow(ImIn, x1, y1, ix, ox, nextRed, nextGreen, nextBlue);
601 /* si and sifrac give the subsampling position: */
604 /* getNextRow tells us, that we need the next row */
606 for (yi = 0; yi <= oy; yi++)
610 for (xi = 0; xi < ox; xi++)
612 prevRed[xi] = nextRed[xi];
613 prevGreen[xi] = nextGreen[xi];
614 prevBlue[xi] = nextBlue[xi];
618 /* only get next row if in same icon */
619 GetScaledRow(ImIn, x1, si + 1, ix, ox,
620 nextRed, nextGreen, nextBlue);
624 /* calculate subsampled color values: */
625 /* division by oy occurs in PutRGBScan */
626 for (xi = 0; xi < ox; xi++)
628 tempRed[xi] = (prevRed[xi] * (oy - sifrac) +
629 nextRed[xi] * sifrac);
630 tempGreen[xi] = (prevGreen[xi] * (oy - sifrac) +
631 nextGreen[xi] * sifrac);
632 tempBlue[xi] = (prevBlue[xi] * (oy - sifrac) +
633 nextBlue[xi] * sifrac);
636 /* write row to output image: */
637 PutRGBScan(ImOut, x2, y2 + yi, ox, div,
638 tempRed, tempGreen, tempBlue);
640 /* advance sampling position: */
657 /* scaling by averaging (shrink) */
659 /* height of a output row in input rows: */
662 /* start position of the first output row: */
665 /* get first input row: */
666 GetScaledRow(ImIn, x1, y1, ix, ox, nextRed, nextGreen, nextBlue);
667 for (yi = 0; yi < oy; yi++)
669 /* find endpoint of the current output row: */
671 cifrac = sifrac + addFrac;
677 /* take fraction of current input row (starting segment): */
678 for (xi = 0; xi < ox; xi++)
680 tempRed[xi] = nextRed[xi] * (oy - sifrac);
681 tempGreen[xi] = nextGreen[xi] * (oy - sifrac);
682 tempBlue[xi] = nextBlue[xi] * (oy - sifrac);
685 /* add values for whole pixels: */
688 GetScaledRow(ImIn, x1, si, ix, ox,
689 nextRed, nextGreen, nextBlue);
690 for (xi = 0; xi < ox; xi++)
692 tempRed[xi] += nextRed[xi] * oy;
693 tempGreen[xi] += nextGreen[xi] * oy;
694 tempBlue[xi] += nextBlue[xi] * oy;
698 /* add fraction of current input row (ending segment): */
701 /* only get next row if still in icon: */
702 GetScaledRow(ImIn, x1, si, ix, ox,
703 nextRed, nextGreen, nextBlue);
706 for (xi = 0; xi < ox; xi++)
708 tempRed[xi] += nextRed[xi] * sifrac;
709 tempGreen[xi] += nextGreen[xi] * sifrac;
710 tempBlue[xi] += nextBlue[xi] * sifrac;
712 /* write row to output image: */
713 PutRGBScan(ImOut, x2, y2 + yi, ox, div,
714 tempRed, tempGreen, tempBlue);
721 static XImage *ResizeImageSmooth(Display *dpy, XImage *Im,
722 int ix, int iy, int ox, int oy)
724 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
726 int width1, height1, width2, height2;
734 height1 = Im->height;
736 width2 = ox * width1 / ix;
737 height2 = oy * height1 / iy;
739 Data = (char *)malloc(width2 * height2 * Im->bits_per_pixel / 8);
741 Tmp = XCreateImage(dpy, visual,
742 Im->depth, ZPixmap, 0, Data, width2, height2,
745 /* compute values for decomposing pixel into color values: */
746 redMask = Im->red_mask;
748 while ((redMask & 1) == 0)
753 greenMask = Im->green_mask;
755 while ((greenMask & 1) == 0)
760 blueMask = Im->blue_mask;
762 while ((blueMask & 1) == 0)
768 /* scale each icon: */
769 for (y1 = 0, y2 = 0; (y1 < height1) && (y2 < height2); y1 += iy, y2 += oy)
771 for (x1 = 0, x2 = 0; (x1 < width1) && (x2 < width2); x1 += ix, x2 += ox)
773 ScaleIcon(Im, Tmp, x1, y1, x2, y2,
783 * Resize an image. XXX XXX XXX
785 * Also appears in "main-xaw.c".
787 static XImage *ResizeImage(Display *dpy, XImage *Im,
788 int ix, int iy, int ox, int oy)
790 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
792 int width1, height1, width2, height2;
793 int x1, x2, y1, y2, Tx, Ty;
794 int *px1, *px2, *dx1, *dx2;
795 int *py1, *py2, *dy1, *dy2;
801 if (smoothRescaling && (ix != ox || iy != oy) &&
802 visual->class == TrueColor)
804 return ResizeImageSmooth(dpy, Im, ix, iy, ox, oy);
808 height1 = Im->height;
810 width2 = ox * width1 / ix;
811 height2 = oy * height1 / iy;
813 Data = (char *)malloc(width2 * height2 * Im->bits_per_pixel / 8);
815 Tmp = XCreateImage(dpy, visual,
816 Im->depth, ZPixmap, 0, Data, width2, height2,
851 for (y1 = 0, y2 = 0; (y1 < height1) && (y2 < height2); )
855 for (x1 = 0, x2 = 0; (x1 < width1) && (x2 < width2); )
857 XPutPixel(Tmp, x2, y2, XGetPixel(Im, x1, y1));
882 #endif /* USE_GRAPHICS */