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)
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.
78 static unsigned long create_pixel(Display *dpy, byte red, byte green, byte blue)
80 Colormap cmap = DefaultColormapOfScreen(DefaultScreenOfDisplay(dpy));
82 if (!gamma_table_ready)
84 concptr str = getenv("ANGBAND_X11_GAMMA");
85 if (str != NULL) gamma_val = atoi(str);
87 gamma_table_ready = TRUE;
89 /* Only need to build the table if gamma exists */
90 if (gamma_val) build_gamma_table(gamma_val);
93 /* Hack -- Gamma Correction */
96 red = gamma_table[red];
97 green = gamma_table[green];
98 blue = gamma_table[blue];
101 /* Build the color */
103 xcolour.red = red * 255;
104 xcolour.green = green * 255;
105 xcolour.blue = blue * 255;
106 xcolour.flags = DoRed | DoGreen | DoBlue;
108 /* Attempt to Allocate the Parsed color */
109 if (!(XAllocColor(dpy, cmap, &xcolour)))
111 quit_fmt("Couldn't allocate bitmap color '#%02x%02x%02x'\n",
115 return (xcolour.pixel);
121 * The Win32 "BITMAPFILEHEADER" type.
123 typedef struct BITMAPFILEHEADER
133 * The Win32 "BITMAPINFOHEADER" type.
135 typedef struct BITMAPINFOHEADER
144 u32b biXPelsPerMeter;
145 u32b biYPelsPerMeter;
151 * The Win32 "RGBQUAD" type.
153 typedef struct RGBQUAD
155 unsigned char b, g, r;
156 unsigned char filler;
160 /*** Helper functions for system independent file loading. ***/
162 static byte get_byte(FILE *fff)
164 /* Get a character, and return it */
165 return (getc(fff) & 0xFF);
168 static void rd_byte(FILE *fff, byte *ip)
173 static void rd_u16b(FILE *fff, u16b *ip)
175 (*ip) = get_byte(fff);
176 (*ip) |= ((u16b)(get_byte(fff)) << 8);
179 static void rd_u32b(FILE *fff, u32b *ip)
181 (*ip) = get_byte(fff);
182 (*ip) |= ((u32b)(get_byte(fff)) << 8);
183 (*ip) |= ((u32b)(get_byte(fff)) << 16);
184 (*ip) |= ((u32b)(get_byte(fff)) << 24);
189 * Read a Win32 BMP file.
191 * This function replaces the old ReadRaw and RemapColors functions.
193 * Assumes that the bitmap has a size such that no padding is needed in
194 * various places. Currently only handles bitmaps with 3 to 256 colors.
196 static XImage *ReadBMP(Display *dpy, char *Name)
198 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
200 int depth = DefaultDepth(dpy, DefaultScreen(dpy));
204 BITMAPFILEHEADER fileheader;
205 BITMAPINFOHEADER infoheader;
219 unsigned long clr_pixels[256];
222 /* Open the BMP file */
223 f = fopen(Name, "r");
231 /* Read the "BITMAPFILEHEADER" */
232 rd_u16b(f, &(fileheader.bfType));
233 rd_u32b(f, &(fileheader.bfSize));
234 rd_u16b(f, &(fileheader.bfReserved1));
235 rd_u16b(f, &(fileheader.bfReserved2));
236 rd_u32b(f, &(fileheader.bfOffBits));
238 /* Read the "BITMAPINFOHEADER" */
239 rd_u32b(f, &(infoheader.biSize));
240 rd_u32b(f, &(infoheader.biWidth));
241 rd_u32b(f, &(infoheader.biHeight));
242 rd_u16b(f, &(infoheader.biPlanes));
243 rd_u16b(f, &(infoheader.biBitCount));
244 rd_u32b(f, &(infoheader.biCompresion));
245 rd_u32b(f, &(infoheader.biSizeImage));
246 rd_u32b(f, &(infoheader.biXPelsPerMeter));
247 rd_u32b(f, &(infoheader.biYPelsPerMeter));
248 rd_u32b(f, &(infoheader.biClrUsed));
249 rd_u32b(f, &(infoheader.biClrImportand));
251 /* Verify the header */
253 (fileheader.bfType != 19778) ||
254 (infoheader.biSize != 40))
256 quit_fmt("Incorrect BMP file format %s", Name);
259 /* The two headers above occupy 54 bytes total */
260 /* The "bfOffBits" field says where the data starts */
261 /* The "biClrUsed" field does not seem to be reliable */
262 /* Compute number of colors recorded */
263 ncol = (fileheader.bfOffBits - 54) / 4;
265 for (i = 0; i < ncol; i++)
269 /* Read an "RGBQUAD" */
270 rd_byte(f, &(clrg.b));
271 rd_byte(f, &(clrg.g));
272 rd_byte(f, &(clrg.r));
273 rd_byte(f, &(clrg.filler));
275 /* Analyze the color */
276 clr_pixels[i] = create_pixel(dpy, clrg.r, clrg.g, clrg.b);
279 /* Determine total bytes needed for image */
281 j = (depth - 1) >> 2;
282 while (j >>= 1) i <<= 1;
283 total = infoheader.biWidth * infoheader.biHeight * i;
285 /* Allocate image memory */
286 C_MAKE(Data, total, char);
288 Res = XCreateImage(dpy, visual, depth, ZPixmap, 0 /*offset*/,
289 Data, infoheader.biWidth, infoheader.biHeight,
290 8 /*bitmap_pad*/, 0 /*bytes_per_line*/);
295 C_KILL(Data, total, char);
300 for (y = 0; y < infoheader.biHeight; y++)
302 int y2 = infoheader.biHeight - y - 1;
304 for (x = 0; x < infoheader.biWidth; x++)
308 /* Verify not at end of file XXX XXX */
309 if (feof(f)) quit_fmt("Unexpected end of file in %s", Name);
311 if (infoheader.biBitCount == 24)
316 /* Verify not at end of file XXX XXX */
317 if (feof(f)) quit_fmt("Unexpected end of file in %s", Name);
319 XPutPixel(Res, x, y2, create_pixel(dpy, ch, c2, c3));
321 else if (infoheader.biBitCount == 8)
323 XPutPixel(Res, x, y2, clr_pixels[ch]);
325 else if (infoheader.biBitCount == 4)
327 XPutPixel(Res, x, y2, clr_pixels[ch / 16]);
329 XPutPixel(Res, x, y2, clr_pixels[ch % 16]);
333 /* Technically 1 bit is legal too */
334 quit_fmt("Illegal biBitCount %d in %s",
335 infoheader.biBitCount, Name);
346 /* ========================================================*/
347 /* Code for smooth icon rescaling from Uwe Siems, Jan 2000 */
348 /* ========================================================*/
351 * to save ourselves some labour, define a maximum expected icon width here:
353 #define MAX_ICON_WIDTH 32
356 /* some static variables for composing and decomposing pixel values into
357 * red, green and blue values
359 static unsigned long redMask, greenMask, blueMask;
360 static int redShift, greenShift, blueShift;
364 * Use smooth rescaling?
366 static bool smoothRescaling = TRUE;
370 * GetScaledRow reads a scan from the given XImage, scales it smoothly
371 * and returns the red, green and blue values in arrays.
372 * The values in this arrays must be divided by a certain value that is
373 * calculated in ScaleIcon.
374 * x, y is the position, iw is the input width and ow the output width
375 * redScan, greenScan and blueScan must be sufficiently sized
377 static void GetScaledRow(XImage *Im, int x, int y, int iw, int ow,
378 unsigned long *redScan, unsigned long *greenScan,
379 unsigned long *blueScan)
381 int xi, si, sifrac, ci, cifrac, addWhole, addFrac;
383 int prevRed, prevGreen, prevBlue, nextRed, nextGreen, nextBlue;
389 for (xi = 0; xi < ow; xi++)
391 pix = XGetPixel(Im, x + xi, y);
392 redScan [xi] = (pix >> redShift) & redMask;
393 greenScan [xi] = (pix >> greenShift) & greenMask;
394 blueScan [xi] = (pix >> blueShift) & blueMask;
399 /* scaling by subsampling (grow) */
402 /* read first pixel: */
403 pix = XGetPixel(Im, x, y);
404 nextRed = (pix >> redShift) & redMask;
405 nextGreen = (pix >> greenShift) & greenMask;
406 nextBlue = (pix >> blueShift) & blueMask;
408 prevGreen = nextGreen;
410 /* si and sifrac give the subsampling position: */
413 /* getNextPix tells us, that we need the next pixel */
416 for (xi = 0; xi <= ow; xi++)
421 prevGreen = nextGreen;
425 /* only get next pixel if in same icon */
426 pix = XGetPixel(Im, si + 1, y);
427 nextRed = (pix >> redShift) & redMask;
428 nextGreen = (pix >> greenShift) & greenMask;
429 nextBlue = (pix >> blueShift) & blueMask;
433 /* calculate subsampled color values: */
434 /* division by ow occurs in ScaleIcon */
435 redScan [xi] = prevRed * (ow - sifrac) + nextRed * sifrac;
436 greenScan [xi] = prevGreen * (ow - sifrac) + nextGreen * sifrac;
437 blueScan [xi] = prevBlue * (ow - sifrac) + nextBlue * sifrac;
439 /* advance sampling position: */
456 /* scaling by averaging (shrink) */
457 /* width of an output pixel in input pixels: */
460 /* start position of the first output pixel: */
463 /* get first input pixel: */
464 pix = XGetPixel(Im, x, y);
465 nextRed = (pix >> redShift) & redMask;
466 nextGreen = (pix >> greenShift) & greenMask;
467 nextBlue = (pix >> blueShift) & blueMask;
468 for (xi = 0; xi < ow; xi++)
470 /* find endpoint of the current output pixel: */
472 cifrac = sifrac + addFrac;
478 /* take fraction of current input pixel (starting segment): */
479 redScan[xi] = nextRed * (ow - sifrac);
480 greenScan[xi] = nextGreen * (ow - sifrac);
481 blueScan[xi] = nextBlue * (ow - sifrac);
483 /* add values for whole pixels: */
486 pix = XGetPixel(Im, si, y);
487 redScan[xi] += ((pix >> redShift) & redMask) *ow;
488 greenScan[xi] += ((pix >> greenShift) & greenMask) *ow;
489 blueScan[xi] += ((pix >> blueShift) & blueMask) *ow;
492 /* add fraction of current input pixel (ending segment): */
495 /* only get next pixel if still in icon: */
496 pix = XGetPixel(Im, si, y);
497 nextRed = (pix >> redShift) & redMask;
498 nextGreen = (pix >> greenShift) & greenMask;
499 nextBlue = (pix >> blueShift) & blueMask;
504 redScan[xi] += nextRed * sifrac;
505 greenScan[xi] += nextGreen * sifrac;
506 blueScan[xi] += nextBlue * sifrac;
514 * PutRGBScan takes arrays for red, green and blue and writes pixel values
515 * according to this values in the XImage-structure. w is the number of
516 * pixels to write and div is the value by which all red/green/blue values
519 static void PutRGBScan(XImage *Im, int x, int y, int w, int div,
520 unsigned long *redScan, unsigned long *greenScan,
521 unsigned long *blueScan)
525 unsigned long adj = div / 2;
526 for (xi = 0; xi < w; xi++)
528 pix = (((((redScan[xi] + adj) / div) & redMask) << redShift) +
529 ((((greenScan[xi] + adj) / div) & greenMask) << greenShift) +
530 ((((blueScan[xi] + adj) / div) & blueMask) << blueShift));
531 XPutPixel(Im, x + xi, y, pix);
537 * ScaleIcon transfers an area from XImage ImIn, locate (x1,y1) to ImOut,
539 * Source size is (ix, iy) and destination size is (ox, oy).
540 * It does this by getting icon scan line from GetScaledScan and handling
541 * them the same way as pixels are handled in GetScaledScan.
542 * This even allows icons to be scaled differently in horizontal and
543 * vertical directions (eg. shrink horizontal, grow vertical).
545 static void ScaleIcon(XImage *ImIn, XImage *ImOut,
546 int x1, int y1, int x2, int y2,
547 int ix, int iy, int ox, int oy)
550 int xi, yi, si, sifrac, ci, cifrac, addWhole, addFrac;
552 /* buffers for pixel rows: */
553 unsigned long prevRed [MAX_ICON_WIDTH];
554 unsigned long prevGreen [MAX_ICON_WIDTH];
555 unsigned long prevBlue [MAX_ICON_WIDTH];
556 unsigned long nextRed [MAX_ICON_WIDTH];
557 unsigned long nextGreen [MAX_ICON_WIDTH];
558 unsigned long nextBlue [MAX_ICON_WIDTH];
559 unsigned long tempRed [MAX_ICON_WIDTH];
560 unsigned long tempGreen [MAX_ICON_WIDTH];
561 unsigned long tempBlue [MAX_ICON_WIDTH];
565 /* get divider value for the horizontal scaling: */
575 /* no scaling needed vertically: */
576 for (yi = 0; yi < oy; yi++)
578 GetScaledRow(ImIn, x1, y1 + yi, ix, ox,
579 tempRed, tempGreen, tempBlue);
580 PutRGBScan(ImOut, x2, y2 + yi, ox, div,
581 tempRed, tempGreen, tempBlue);
586 /* scaling by subsampling (grow): */
591 GetScaledRow(ImIn, x1, y1, ix, ox, nextRed, nextGreen, nextBlue);
592 /* si and sifrac give the subsampling position: */
595 /* getNextRow tells us, that we need the next row */
597 for (yi = 0; yi <= oy; yi++)
601 for (xi = 0; xi < ox; xi++)
603 prevRed[xi] = nextRed[xi];
604 prevGreen[xi] = nextGreen[xi];
605 prevBlue[xi] = nextBlue[xi];
609 /* only get next row if in same icon */
610 GetScaledRow(ImIn, x1, si + 1, ix, ox,
611 nextRed, nextGreen, nextBlue);
615 /* calculate subsampled color values: */
616 /* division by oy occurs in PutRGBScan */
617 for (xi = 0; xi < ox; xi++)
619 tempRed[xi] = (prevRed[xi] * (oy - sifrac) +
620 nextRed[xi] * sifrac);
621 tempGreen[xi] = (prevGreen[xi] * (oy - sifrac) +
622 nextGreen[xi] * sifrac);
623 tempBlue[xi] = (prevBlue[xi] * (oy - sifrac) +
624 nextBlue[xi] * sifrac);
627 /* write row to output image: */
628 PutRGBScan(ImOut, x2, y2 + yi, ox, div,
629 tempRed, tempGreen, tempBlue);
631 /* advance sampling position: */
648 /* scaling by averaging (shrink) */
650 /* height of a output row in input rows: */
653 /* start position of the first output row: */
656 /* get first input row: */
657 GetScaledRow(ImIn, x1, y1, ix, ox, nextRed, nextGreen, nextBlue);
658 for (yi = 0; yi < oy; yi++)
660 /* find endpoint of the current output row: */
662 cifrac = sifrac + addFrac;
668 /* take fraction of current input row (starting segment): */
669 for (xi = 0; xi < ox; xi++)
671 tempRed[xi] = nextRed[xi] * (oy - sifrac);
672 tempGreen[xi] = nextGreen[xi] * (oy - sifrac);
673 tempBlue[xi] = nextBlue[xi] * (oy - sifrac);
676 /* add values for whole pixels: */
679 GetScaledRow(ImIn, x1, si, ix, ox,
680 nextRed, nextGreen, nextBlue);
681 for (xi = 0; xi < ox; xi++)
683 tempRed[xi] += nextRed[xi] * oy;
684 tempGreen[xi] += nextGreen[xi] * oy;
685 tempBlue[xi] += nextBlue[xi] * oy;
689 /* add fraction of current input row (ending segment): */
692 /* only get next row if still in icon: */
693 GetScaledRow(ImIn, x1, si, ix, ox,
694 nextRed, nextGreen, nextBlue);
697 for (xi = 0; xi < ox; xi++)
699 tempRed[xi] += nextRed[xi] * sifrac;
700 tempGreen[xi] += nextGreen[xi] * sifrac;
701 tempBlue[xi] += nextBlue[xi] * sifrac;
703 /* write row to output image: */
704 PutRGBScan(ImOut, x2, y2 + yi, ox, div,
705 tempRed, tempGreen, tempBlue);
712 static XImage *ResizeImageSmooth(Display *dpy, XImage *Im,
713 int ix, int iy, int ox, int oy)
715 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
717 int width1, height1, width2, height2;
725 height1 = Im->height;
727 width2 = ox * width1 / ix;
728 height2 = oy * height1 / iy;
730 Data = (char *)malloc(width2 * height2 * Im->bits_per_pixel / 8);
732 Tmp = XCreateImage(dpy, visual,
733 Im->depth, ZPixmap, 0, Data, width2, height2,
736 /* compute values for decomposing pixel into color values: */
737 redMask = Im->red_mask;
739 while ((redMask & 1) == 0)
744 greenMask = Im->green_mask;
746 while ((greenMask & 1) == 0)
751 blueMask = Im->blue_mask;
753 while ((blueMask & 1) == 0)
759 /* scale each icon: */
760 for (y1 = 0, y2 = 0; (y1 < height1) && (y2 < height2); y1 += iy, y2 += oy)
762 for (x1 = 0, x2 = 0; (x1 < width1) && (x2 < width2); x1 += ix, x2 += ox)
764 ScaleIcon(Im, Tmp, x1, y1, x2, y2,
774 * Resize an image. XXX XXX XXX
776 * Also appears in "main-xaw.c".
778 static XImage *ResizeImage(Display *dpy, XImage *Im,
779 int ix, int iy, int ox, int oy)
781 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
783 int width1, height1, width2, height2;
784 int x1, x2, y1, y2, Tx, Ty;
785 int *px1, *px2, *dx1, *dx2;
786 int *py1, *py2, *dy1, *dy2;
792 if (smoothRescaling && (ix != ox || iy != oy) &&
793 visual->class == TrueColor)
795 return ResizeImageSmooth(dpy, Im, ix, iy, ox, oy);
799 height1 = Im->height;
801 width2 = ox * width1 / ix;
802 height2 = oy * height1 / iy;
804 Data = (char *)malloc(width2 * height2 * Im->bits_per_pixel / 8);
806 Tmp = XCreateImage(dpy, visual,
807 Im->depth, ZPixmap, 0, Data, width2, height2,
842 for (y1 = 0, y2 = 0; (y1 < height1) && (y2 < height2); )
846 for (x1 = 0, x2 = 0; (x1 < width1) && (x2 < width2); )
848 XPutPixel(Tmp, x2, y2, XGetPixel(Im, x1, y1));