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.
13 #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",
26 * 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) (((unsigned)(keysym) >= XK_KP_Space) && ((unsigned)(keysym) <= XK_KP_Equal))
40 #define IsCursorKey(keysym) (((unsigned)(keysym) >= XK_Home) && ((unsigned)(keysym) < XK_Select))
42 #define IsPFKey(keysym) (((unsigned)(keysym) >= XK_KP_F1) && ((unsigned)(keysym) <= XK_KP_F4))
44 #define IsFunctionKey(keysym) (((unsigned)(keysym) >= XK_F1) && ((unsigned)(keysym) <= XK_F35))
46 #define IsMiscFunctionKey(keysym) (((unsigned)(keysym) >= XK_Select) && ((unsigned)(keysym) < XK_KP_Space))
48 #define IsModifierKey(keysym) (((unsigned)(keysym) >= XK_Shift_L) && ((unsigned)(keysym) <= XK_Hyper_R))
50 #endif /* IsModifierKey */
53 * Checks if the keysym is a special key or a normal key
54 * Assume that XK_MISCELLANY keysyms are special
56 * Also appears in "main-x11.c".
58 #define IsSpecialKey(keysym) ((unsigned)(keysym) >= 0xFF00)
60 static bool gamma_table_ready = true;
61 static int gamma_val = 0;
64 * Hack -- Convert an RGB value to an X11 Pixel, or die.
67 static XftColor create_pixel(Display *dpy, byte red, byte green, byte blue)
69 static unsigned long create_pixel(Display *dpy, byte red, byte green, byte blue)
72 Colormap cmap = DefaultColormapOfScreen(DefaultScreenOfDisplay(dpy));
74 if (!gamma_table_ready) {
75 concptr str = getenv("ANGBAND_X11_GAMMA");
77 gamma_val = atoi(str);
80 gamma_table_ready = true;
82 /* Only need to build the table if gamma exists */
84 build_gamma_table(gamma_val);
88 /* Hack -- Gamma Correction */
90 red = gamma_table[red];
91 green = gamma_table[green];
92 blue = gamma_table[blue];
97 xcolour.red = red * 255;
98 xcolour.green = green * 255;
99 xcolour.blue = blue * 255;
100 xcolour.flags = DoRed | DoGreen | DoBlue;
105 xcol.red = xcolour.red;
106 xcol.green = xcolour.green;
107 xcol.blue = xcolour.blue;
109 if (!XftColorAllocValue(dpy, DefaultVisual(dpy, 0), cmap, &xcol, &color)) {
110 quit_fmt("Couldn't allocate bitmap color '#%02x%02x%02x'\n", red, green, blue);
115 /* Attempt to Allocate the Parsed color */
116 if (!(XAllocColor(dpy, cmap, &xcolour))) {
117 quit_fmt("Couldn't allocate bitmap color '#%02x%02x%02x'\n", red, green, blue);
120 return xcolour.pixel;
127 * The Win32 "BITMAPFILEHEADER" type.
129 struct BITMAPFILEHEADER {
132 uint16_t bfReserved1;
133 uint16_t bfReserved2;
138 * The Win32 "BITMAPINFOHEADER" type.
140 struct BITMAPINFOHEADER {
146 uint32_t biCompresion;
147 uint32_t biSizeImage;
148 uint32_t biXPelsPerMeter;
149 uint32_t biYPelsPerMeter;
151 uint32_t biClrImportand;
155 * The Win32 "RGBQUAD" type.
158 unsigned char b, g, r;
159 unsigned char filler;
162 /*** Helper functions for system independent file loading. ***/
164 static byte get_byte(FILE *fff)
166 /* Get a character, and return it */
167 return getc(fff) & 0xFF;
170 static void rd_byte(FILE *fff, byte *ip)
175 static void rd_u16b(FILE *fff, uint16_t *ip)
177 (*ip) = get_byte(fff);
178 (*ip) |= ((uint16_t)(get_byte(fff)) << 8);
181 static void rd_u32b(FILE *fff, uint32_t *ip)
183 (*ip) = get_byte(fff);
184 (*ip) |= ((uint32_t)(get_byte(fff)) << 8);
185 (*ip) |= ((uint32_t)(get_byte(fff)) << 16);
186 (*ip) |= ((uint32_t)(get_byte(fff)) << 24);
190 * Read a Win32 BMP file.
192 * This function replaces the old ReadRaw and RemapColors functions.
194 * Assumes that the bitmap has a size such that no padding is needed in
195 * various places. Currently only handles bitmaps with 3 to 256 colors.
197 static XImage *ReadBMP(Display *dpy, char *Name)
199 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
201 int depth = DefaultDepth(dpy, DefaultScreen(dpy));
205 BITMAPFILEHEADER fileheader;
206 BITMAPINFOHEADER infoheader;
208 XImage *Res = nullptr;
220 unsigned long clr_pixels[256];
222 /* Open the BMP file */
223 f = fopen(Name, "r");
230 /* Read the "BITMAPFILEHEADER" */
231 rd_u16b(f, &(fileheader.bfType));
232 rd_u32b(f, &(fileheader.bfSize));
233 rd_u16b(f, &(fileheader.bfReserved1));
234 rd_u16b(f, &(fileheader.bfReserved2));
235 rd_u32b(f, &(fileheader.bfOffBits));
237 /* Read the "BITMAPINFOHEADER" */
238 rd_u32b(f, &(infoheader.biSize));
239 rd_u32b(f, &(infoheader.biWidth));
240 rd_u32b(f, &(infoheader.biHeight));
241 rd_u16b(f, &(infoheader.biPlanes));
242 rd_u16b(f, &(infoheader.biBitCount));
243 rd_u32b(f, &(infoheader.biCompresion));
244 rd_u32b(f, &(infoheader.biSizeImage));
245 rd_u32b(f, &(infoheader.biXPelsPerMeter));
246 rd_u32b(f, &(infoheader.biYPelsPerMeter));
247 rd_u32b(f, &(infoheader.biClrUsed));
248 rd_u32b(f, &(infoheader.biClrImportand));
250 /* Verify the header */
251 if (feof(f) || (fileheader.bfType != 19778) || (infoheader.biSize != 40)) {
252 quit_fmt("Incorrect BMP file format %s", Name);
255 /* The two headers above occupy 54 bytes total */
256 /* The "bfOffBits" field says where the data starts */
257 /* The "biClrUsed" field does not seem to be reliable */
258 /* Compute number of colors recorded */
259 ncol = (fileheader.bfOffBits - 54) / 4;
261 for (i = 0; i < ncol; i++) {
264 /* Read an "RGBQUAD" */
265 rd_byte(f, &(clrg.b));
266 rd_byte(f, &(clrg.g));
267 rd_byte(f, &(clrg.r));
268 rd_byte(f, &(clrg.filler));
270 /* Analyze the color */
271 clr_pixels[i] = create_pixel(dpy, clrg.r, clrg.g, clrg.b);
274 /* Determine total bytes needed for image */
276 j = (depth - 1) >> 2;
280 total = infoheader.biWidth * infoheader.biHeight * i;
282 /* Allocate image memory */
283 Data = (char *)malloc(total);
285 Res = XCreateImage(dpy, visual, depth, ZPixmap, 0 /*offset*/, Data, infoheader.biWidth, infoheader.biHeight, 8 /*bitmap_pad*/, 0 /*bytes_per_line*/);
288 if (Res == nullptr) {
294 for (y = 0; y < static_cast<int>(infoheader.biHeight); y++) {
295 int y2 = infoheader.biHeight - y - 1;
297 for (x = 0; x < static_cast<int>(infoheader.biWidth); x++) {
300 /* Verify not at end of file XXX XXX */
302 quit_fmt("Unexpected end of file in %s", Name);
305 if (infoheader.biBitCount == 24) {
309 /* Verify not at end of file XXX XXX */
311 quit_fmt("Unexpected end of file in %s", Name);
314 XPutPixel(Res, x, y2, create_pixel(dpy, ch, c2, c3));
315 } else if (infoheader.biBitCount == 8) {
316 XPutPixel(Res, x, y2, clr_pixels[ch]);
317 } else if (infoheader.biBitCount == 4) {
318 XPutPixel(Res, x, y2, clr_pixels[ch / 16]);
320 XPutPixel(Res, x, y2, clr_pixels[ch % 16]);
322 /* Technically 1 bit is legal too */
323 quit_fmt("Illegal biBitCount %d in %s", infoheader.biBitCount, Name);
333 /* ========================================================*/
334 /* Code for smooth icon rescaling from Uwe Siems, Jan 2000 */
335 /* ========================================================*/
338 * to save ourselves some labour, define a maximum expected icon width here:
340 #define MAX_ICON_WIDTH 32
342 /* some static variables for composing and decomposing pixel values into
343 * red, green and blue values
345 static unsigned long redMask, greenMask, blueMask;
346 static int redShift, greenShift, blueShift;
349 * Use smooth rescaling?
351 static bool smoothRescaling = true;
354 * GetScaledRow reads a scan from the given XImage, scales it smoothly
355 * and returns the red, green and blue values in arrays.
356 * The values in this arrays must be divided by a certain value that is
357 * calculated in ScaleIcon.
358 * x, y is the position, iw is the input width and ow the output width
359 * redScan, greenScan and blueScan must be sufficiently sized
361 static void GetScaledRow(XImage *Im, int x, int y, int iw, int ow, unsigned long *redScan, unsigned long *greenScan, unsigned long *blueScan)
363 int xi, si, sifrac, ci, cifrac, addWhole, addFrac;
365 int prevRed, prevGreen, prevBlue, nextRed, nextGreen, nextBlue;
370 for (xi = 0; xi < ow; xi++) {
371 pix = XGetPixel(Im, x + xi, y);
372 redScan[xi] = (pix >> redShift) & redMask;
373 greenScan[xi] = (pix >> greenShift) & greenMask;
374 blueScan[xi] = (pix >> blueShift) & blueMask;
376 } else if (iw < ow) {
377 /* scaling by subsampling (grow) */
380 /* read first pixel: */
381 pix = XGetPixel(Im, x, y);
382 nextRed = (pix >> redShift) & redMask;
383 nextGreen = (pix >> greenShift) & greenMask;
384 nextBlue = (pix >> blueShift) & blueMask;
386 prevGreen = nextGreen;
388 /* si and sifrac give the subsampling position: */
391 /* getNextPix tells us, that we need the next pixel */
394 for (xi = 0; xi <= ow; xi++) {
397 prevGreen = nextGreen;
400 /* only get next pixel if in same icon */
401 pix = XGetPixel(Im, si + 1, y);
402 nextRed = (pix >> redShift) & redMask;
403 nextGreen = (pix >> greenShift) & greenMask;
404 nextBlue = (pix >> blueShift) & blueMask;
408 /* calculate subsampled color values: */
409 /* division by ow occurs in ScaleIcon */
410 redScan[xi] = prevRed * (ow - sifrac) + nextRed * sifrac;
411 greenScan[xi] = prevGreen * (ow - sifrac) + nextGreen * sifrac;
412 blueScan[xi] = prevBlue * (ow - sifrac) + nextBlue * sifrac;
414 /* 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++) {
438 /* find endpoint of the current output pixel: */
440 cifrac = sifrac + addFrac;
445 /* take fraction of current input pixel (starting segment): */
446 redScan[xi] = nextRed * (ow - sifrac);
447 greenScan[xi] = nextGreen * (ow - sifrac);
448 blueScan[xi] = nextBlue * (ow - sifrac);
450 /* add values for whole pixels: */
452 pix = XGetPixel(Im, si, y);
453 redScan[xi] += ((pix >> redShift) & redMask) * ow;
454 greenScan[xi] += ((pix >> greenShift) & greenMask) * ow;
455 blueScan[xi] += ((pix >> blueShift) & blueMask) * ow;
458 /* add fraction of current input pixel (ending segment): */
460 /* only get next pixel if still in icon: */
461 pix = XGetPixel(Im, si, y);
462 nextRed = (pix >> redShift) & redMask;
463 nextGreen = (pix >> greenShift) & greenMask;
464 nextBlue = (pix >> blueShift) & blueMask;
468 redScan[xi] += nextRed * sifrac;
469 greenScan[xi] += nextGreen * sifrac;
470 blueScan[xi] += nextBlue * sifrac;
477 * PutRGBScan takes arrays for red, green and blue and writes pixel values
478 * according to this values in the XImage-structure. w is the number of
479 * pixels to write and div is the value by which all red/green/blue values
482 static void PutRGBScan(XImage *Im, int x, int y, int w, int div, unsigned long *redScan, unsigned long *greenScan, unsigned long *blueScan)
486 unsigned long adj = div / 2;
487 for (xi = 0; xi < w; xi++) {
488 pix = (((((redScan[xi] + adj) / div) & redMask) << redShift) + ((((greenScan[xi] + adj) / div) & greenMask) << greenShift) + ((((blueScan[xi] + adj) / div) & blueMask) << blueShift));
489 XPutPixel(Im, x + xi, y, pix);
494 * ScaleIcon transfers an area from XImage ImIn, locate (x1,y1) to ImOut,
496 * Source size is (ix, iy) and destination size is (ox, oy).
497 * It does this by getting icon scan line from GetScaledScan and handling
498 * them the same way as pixels are handled in GetScaledScan.
499 * This even allows icons to be scaled differently in horizontal and
500 * vertical directions (eg. shrink horizontal, grow vertical).
502 static void ScaleIcon(XImage *ImIn, XImage *ImOut, int x1, int y1, int x2, int y2, int ix, int iy, int ox, int oy)
505 int xi, yi, si, sifrac, ci, cifrac, addWhole, addFrac;
507 /* buffers for pixel rows: */
508 unsigned long prevRed[MAX_ICON_WIDTH];
509 unsigned long prevGreen[MAX_ICON_WIDTH];
510 unsigned long prevBlue[MAX_ICON_WIDTH];
511 unsigned long nextRed[MAX_ICON_WIDTH];
512 unsigned long nextGreen[MAX_ICON_WIDTH];
513 unsigned long nextBlue[MAX_ICON_WIDTH];
514 unsigned long tempRed[MAX_ICON_WIDTH];
515 unsigned long tempGreen[MAX_ICON_WIDTH];
516 unsigned long tempBlue[MAX_ICON_WIDTH];
520 /* get divider value for the horizontal scaling: */
523 } else if (ix < ox) {
530 /* no scaling needed vertically: */
531 for (yi = 0; yi < oy; yi++) {
532 GetScaledRow(ImIn, x1, y1 + yi, ix, ox, tempRed, tempGreen, tempBlue);
533 PutRGBScan(ImOut, x2, y2 + yi, ox, div, tempRed, tempGreen, tempBlue);
535 } else if (iy < oy) {
536 /* scaling by subsampling (grow): */
541 GetScaledRow(ImIn, x1, y1, ix, ox, nextRed, nextGreen, nextBlue);
542 /* si and sifrac give the subsampling position: */
545 /* getNextRow tells us, that we need the next row */
547 for (yi = 0; yi <= oy; yi++) {
549 for (xi = 0; xi < ox; xi++) {
550 prevRed[xi] = nextRed[xi];
551 prevGreen[xi] = nextGreen[xi];
552 prevBlue[xi] = nextBlue[xi];
555 /* only get next row if in same icon */
556 GetScaledRow(ImIn, x1, si + 1, ix, ox, nextRed, nextGreen, nextBlue);
560 /* calculate subsampled color values: */
561 /* division by oy occurs in PutRGBScan */
562 for (xi = 0; xi < ox; xi++) {
563 tempRed[xi] = (prevRed[xi] * (oy - sifrac) + nextRed[xi] * sifrac);
564 tempGreen[xi] = (prevGreen[xi] * (oy - sifrac) + nextGreen[xi] * sifrac);
565 tempBlue[xi] = (prevBlue[xi] * (oy - sifrac) + nextBlue[xi] * sifrac);
568 /* write row to output image: */
569 PutRGBScan(ImOut, x2, y2 + yi, ox, div, tempRed, tempGreen, tempBlue);
571 /* advance sampling position: */
582 /* scaling by averaging (shrink) */
584 /* height of a output row in input rows: */
587 /* start position of the first output row: */
590 /* get first input row: */
591 GetScaledRow(ImIn, x1, y1, ix, ox, nextRed, nextGreen, nextBlue);
592 for (yi = 0; yi < oy; yi++) {
593 /* find endpoint of the current output row: */
595 cifrac = sifrac + addFrac;
600 /* take fraction of current input row (starting segment): */
601 for (xi = 0; xi < ox; xi++) {
602 tempRed[xi] = nextRed[xi] * (oy - sifrac);
603 tempGreen[xi] = nextGreen[xi] * (oy - sifrac);
604 tempBlue[xi] = nextBlue[xi] * (oy - sifrac);
607 /* add values for whole pixels: */
609 GetScaledRow(ImIn, x1, si, ix, ox, nextRed, nextGreen, nextBlue);
610 for (xi = 0; xi < ox; xi++) {
611 tempRed[xi] += nextRed[xi] * oy;
612 tempGreen[xi] += nextGreen[xi] * oy;
613 tempBlue[xi] += nextBlue[xi] * oy;
617 /* add fraction of current input row (ending segment): */
619 /* only get next row if still in icon: */
620 GetScaledRow(ImIn, x1, si, ix, ox, nextRed, nextGreen, nextBlue);
623 for (xi = 0; xi < ox; xi++) {
624 tempRed[xi] += nextRed[xi] * sifrac;
625 tempGreen[xi] += nextGreen[xi] * sifrac;
626 tempBlue[xi] += nextBlue[xi] * sifrac;
628 /* write row to output image: */
629 PutRGBScan(ImOut, x2, y2 + yi, ox, div, tempRed, tempGreen, tempBlue);
634 static XImage *ResizeImageSmooth(Display *dpy, XImage *Im, int ix, int iy, int ox, int oy)
636 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
638 int width1, height1, width2, height2;
646 height1 = Im->height;
648 width2 = ox * width1 / ix;
649 height2 = oy * height1 / iy;
651 Data = (char *)malloc(width2 * height2 * Im->bits_per_pixel / 8);
653 Tmp = XCreateImage(dpy, visual, Im->depth, ZPixmap, 0, Data, width2, height2, 32, 0);
655 /* compute values for decomposing pixel into color values: */
656 redMask = Im->red_mask;
658 while ((redMask & 1) == 0) {
662 greenMask = Im->green_mask;
664 while ((greenMask & 1) == 0) {
668 blueMask = Im->blue_mask;
670 while ((blueMask & 1) == 0) {
675 /* scale each icon: */
676 for (y1 = 0, y2 = 0; (y1 < height1) && (y2 < height2); y1 += iy, y2 += oy) {
677 for (x1 = 0, x2 = 0; (x1 < width1) && (x2 < width2); x1 += ix, x2 += ox) {
678 ScaleIcon(Im, Tmp, x1, y1, x2, y2, ix, iy, ox, oy);
688 static XImage *ResizeImage(Display *dpy, XImage *Im, int ix, int iy, int ox, int oy)
690 Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
692 int width1, height1, width2, height2;
693 volatile int x1, x2, y1, y2, Tx, Ty;
694 volatile int *px1, *px2, *dx1, *dx2;
695 volatile int *py1, *py2, *dy1, *dy2;
701 if (smoothRescaling && (ix != ox || iy != oy) && visual->c_class == TrueColor) {
702 return ResizeImageSmooth(dpy, Im, ix, iy, ox, oy);
706 height1 = Im->height;
708 width2 = ox * width1 / ix;
709 height2 = oy * height1 / iy;
711 Data = (char *)malloc(width2 * height2 * Im->bits_per_pixel / 8);
713 Tmp = XCreateImage(dpy, visual, Im->depth, ZPixmap, 0, Data, width2, height2, 32, 0);
741 for (y1 = 0, y2 = 0; (y1 < height1) && (y2 < height2);) {
744 for (x1 = 0, x2 = 0; (x1 < width1) && (x2 < width2);) {
745 XPutPixel(Tmp, x2, y2, XGetPixel(Im, x1, y1));
768 #endif /* !USE_XFT */