1 /* convert.c - convert RImage to Pixmap
3 * Raster graphics library
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
24 * 1. Using Grayscale visual with Dithering crashes wmaker
25 * 2. Ghost dock/appicon is wrong in Pseudocolor, Staticgray, Grayscale
30 #include <X11/Xutil.h>
41 #define NFREE(n) if (n) free(n)
43 #define HAS_ALPHA(I) ((I)->format == RRGBAFormat)
45 typedef struct RConversionTable
{
46 unsigned short table
[256];
49 struct RConversionTable
*next
;
52 typedef struct RStdConversionTable
{
53 unsigned int table
[256];
58 struct RStdConversionTable
*next
;
59 } RStdConversionTable
;
61 static RConversionTable
*conversionTable
= NULL
;
62 static RStdConversionTable
*stdConversionTable
= NULL
;
64 static void release_conversion_table(void)
66 RConversionTable
*tmp
= conversionTable
;
69 RConversionTable
*tmp_to_delete
= tmp
;
74 conversionTable
= NULL
;
77 static void release_std_conversion_table(void)
79 RStdConversionTable
*tmp
= stdConversionTable
;
82 RStdConversionTable
*tmp_to_delete
= tmp
;
87 stdConversionTable
= NULL
;
90 void r_destroy_conversion_tables(void)
92 release_conversion_table();
93 release_std_conversion_table();
96 static unsigned short *computeTable(unsigned short mask
)
98 RConversionTable
*tmp
= conversionTable
;
102 if (tmp
->index
== mask
)
110 tmp
= (RConversionTable
*) malloc(sizeof(RConversionTable
));
114 for (i
= 0; i
< 256; i
++)
115 tmp
->table
[i
] = (i
* mask
+ 0x7f) / 0xff;
118 tmp
->next
= conversionTable
;
119 conversionTable
= tmp
;
123 static unsigned int *computeStdTable(unsigned int mult
, unsigned int max
)
125 RStdConversionTable
*tmp
= stdConversionTable
;
129 if (tmp
->mult
== mult
&& tmp
->max
== max
)
137 tmp
= (RStdConversionTable
*) malloc(sizeof(RStdConversionTable
));
141 for (i
= 0; i
< 256; i
++) {
142 tmp
->table
[i
] = (i
* max
) / 0xff * mult
;
147 tmp
->next
= stdConversionTable
;
148 stdConversionTable
= tmp
;
153 /***************************************************************************/
156 convertTrueColor_generic(RXImage
* ximg
, RImage
* image
,
157 signed char *err
, signed char *nerr
,
158 const unsigned short *rtable
,
159 const unsigned short *gtable
,
160 const unsigned short *btable
,
161 const int dr
, const int dg
, const int db
,
162 const unsigned short roffs
, const unsigned short goffs
, const unsigned short boffs
)
168 unsigned char *ptr
= image
->data
;
169 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
171 /* convert and dither the image to XImage */
172 for (y
= 0; y
< image
->height
; y
++) {
176 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
) {
179 pixel
= *ptr
+ err
[x
];
182 else if (pixel
> 0xff)
186 rer
= pixel
- r
* dr
;
189 pixel
= *(ptr
+ 1) + err
[x
+ 1];
192 else if (pixel
> 0xff)
196 ger
= pixel
- g
* dg
;
199 pixel
= *(ptr
+ 2) + err
[x
+ 2];
202 else if (pixel
> 0xff)
206 ber
= pixel
- b
* db
;
208 pixel
= (r
<< roffs
) | (g
<< goffs
) | (b
<< boffs
);
209 XPutPixel(ximg
->image
, x
, y
, pixel
);
211 /* distribute error */
217 err
[x
+ 1 + 3 * 1] += g
;
218 err
[x
+ 2 + 3 * 1] += b
;
224 nerr
[x
+ 3 * 1] = rer
- 2 * r
;
225 nerr
[x
+ 1 + 3 * 1] = ger
- 2 * g
;
226 nerr
[x
+ 2 + 3 * 1] = ber
- 2 * b
;
228 /* skip to next line */
234 /* redither the 1st line to distribute error better */
240 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
) {
243 pixel
= *ptr
+ err
[x
];
246 else if (pixel
> 0xff)
250 rer
= pixel
- r
* dr
;
253 pixel
= *(ptr
+ 1) + err
[x
+ 1];
256 else if (pixel
> 0xff)
260 ger
= pixel
- g
* dg
;
263 pixel
= *(ptr
+ 2) + err
[x
+ 2];
266 else if (pixel
> 0xff)
270 ber
= pixel
- b
* db
;
272 pixel
= (r
<< roffs
) | (g
<< goffs
) | (b
<< boffs
);
273 XPutPixel(ximg
->image
, x
, y
, pixel
);
275 /* distribute error */
281 err
[x
+ 1 + 3 * 1] += g
;
282 err
[x
+ 2 + 3 * 1] += b
;
288 nerr
[x
+ 3 * 1] = rer
- 2 * r
;
289 nerr
[x
+ 1 + 3 * 1] = ger
- 2 * g
;
290 nerr
[x
+ 2 + 3 * 1] = ber
- 2 * b
;
294 static RXImage
*image2TrueColor(RContext
* ctx
, RImage
* image
)
297 unsigned short rmask
, gmask
, bmask
;
298 unsigned short roffs
, goffs
, boffs
;
299 unsigned short *rtable
, *gtable
, *btable
;
300 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
302 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
307 roffs
= ctx
->red_offset
;
308 goffs
= ctx
->green_offset
;
309 boffs
= ctx
->blue_offset
;
311 rmask
= ctx
->visual
->red_mask
>> roffs
;
312 gmask
= ctx
->visual
->green_mask
>> goffs
;
313 bmask
= ctx
->visual
->blue_mask
>> boffs
;
315 rtable
= computeTable(rmask
);
316 gtable
= computeTable(gmask
);
317 btable
= computeTable(bmask
);
319 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
320 RErrorCode
= RERR_NOMEMORY
;
321 RDestroyXImage(ctx
, ximg
);
325 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
327 unsigned long r
, g
, b
;
330 unsigned char *ptr
= image
->data
;
334 fputs("true color match\n", stderr
);
336 if (rmask
== 0xff && gmask
== 0xff && bmask
== 0xff) {
337 for (y
= 0; y
< image
->height
; y
++) {
338 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
) {
343 pixel
= (r
<< roffs
) | (g
<< goffs
) | (b
<< boffs
);
344 XPutPixel(ximg
->image
, x
, y
, pixel
);
348 for (y
= 0, ofs
= 0; y
< image
->height
; y
++) {
349 for (x
= 0; x
< image
->width
; x
++, ofs
+= channels
- 3) {
351 r
= rtable
[ptr
[ofs
++]];
352 g
= gtable
[ptr
[ofs
++]];
353 b
= btable
[ptr
[ofs
++]];
354 pixel
= (r
<< roffs
) | (g
<< goffs
) | (b
<< boffs
);
355 XPutPixel(ximg
->image
, x
, y
, pixel
);
361 const int dr
= 0xff / rmask
;
362 const int dg
= 0xff / gmask
;
363 const int db
= 0xff / bmask
;
366 fputs("true color dither\n", stderr
);
372 int ch
= (HAS_ALPHA(image
) ? 4 : 3);
374 err
= malloc(ch
* (image
->width
+ 2));
375 nerr
= malloc(ch
* (image
->width
+ 2));
379 RErrorCode
= RERR_NOMEMORY
;
380 RDestroyXImage(ctx
, ximg
);
384 memset(err
, 0, ch
* (image
->width
+ 2));
385 memset(nerr
, 0, ch
* (image
->width
+ 2));
387 convertTrueColor_generic(ximg
, image
, err
, nerr
,
388 rtable
, gtable
, btable
, dr
, dg
, db
, roffs
, goffs
, boffs
);
398 /***************************************************************************/
401 convertPseudoColor_to_8(RXImage
* ximg
, RImage
* image
,
402 signed char *err
, signed char *nerr
,
403 const unsigned short *rtable
,
404 const unsigned short *gtable
,
405 const unsigned short *btable
,
406 const int dr
, const int dg
, const int db
, unsigned long *pixels
, int cpc
)
412 unsigned char *ptr
= image
->data
;
413 unsigned char *optr
= (unsigned char *)ximg
->image
->data
;
414 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
415 int cpcpc
= cpc
* cpc
;
417 /* convert and dither the image to XImage */
418 for (y
= 0; y
< image
->height
; y
++) {
422 for (x
= 0; x
< image
->width
* 3; x
+= 3, ptr
+= channels
) {
425 pixel
= *ptr
+ err
[x
];
428 else if (pixel
> 0xff)
432 rer
= pixel
- r
* dr
;
435 pixel
= *(ptr
+ 1) + err
[x
+ 1];
438 else if (pixel
> 0xff)
442 ger
= pixel
- g
* dg
;
445 pixel
= *(ptr
+ 2) + err
[x
+ 2];
448 else if (pixel
> 0xff)
452 ber
= pixel
- b
* db
;
454 *optr
++ = pixels
[r
* cpcpc
+ g
* cpc
+ b
];
456 /* distribute error */
463 err
[x
+ 1 + 3 * 1] += g
;
464 err
[x
+ 2 + 3 * 1] += b
;
470 nerr
[x
+ 3 * 1] = rer
- 2 * r
;
471 nerr
[x
+ 1 + 3 * 1] = ger
- 2 * g
;
472 nerr
[x
+ 2 + 3 * 1] = ber
- 2 * b
;
474 /* skip to next line */
479 optr
+= ximg
->image
->bytes_per_line
- image
->width
;
483 static RXImage
*image2PseudoColor(RContext
* ctx
, RImage
* image
)
486 register int x
, y
, r
, g
, b
;
489 const int cpc
= ctx
->attribs
->colors_per_channel
;
490 const unsigned short rmask
= cpc
- 1; /* different sizes could be used */
491 const unsigned short gmask
= rmask
; /* for r,g,b */
492 const unsigned short bmask
= rmask
;
493 unsigned short *rtable
, *gtable
, *btable
;
494 const int cpccpc
= cpc
* cpc
;
495 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
497 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
504 /* Tables are same at the moment because rmask==gmask==bmask. */
505 rtable
= computeTable(rmask
);
506 gtable
= computeTable(gmask
);
507 btable
= computeTable(bmask
);
509 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
510 RErrorCode
= RERR_NOMEMORY
;
511 RDestroyXImage(ctx
, ximg
);
515 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
518 fprintf(stderr
, "pseudo color match with %d colors per channel\n", cpc
);
520 for (y
= 0; y
< image
->height
; y
++) {
521 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
- 3) {
526 pixel
= r
* cpccpc
+ g
* cpc
+ b
;
527 /*data[ofs] = ctx->colors[pixel].pixel; */
528 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[pixel
].pixel
);
535 const int dr
= 0xff / rmask
;
536 const int dg
= 0xff / gmask
;
537 const int db
= 0xff / bmask
;
540 fprintf(stderr
, "pseudo color dithering with %d colors per channel\n", cpc
);
542 err
= malloc(4 * (image
->width
+ 3));
543 nerr
= malloc(4 * (image
->width
+ 3));
547 RErrorCode
= RERR_NOMEMORY
;
548 RDestroyXImage(ctx
, ximg
);
551 memset(err
, 0, 4 * (image
->width
+ 3));
552 memset(nerr
, 0, 4 * (image
->width
+ 3));
554 convertPseudoColor_to_8(ximg
, image
, err
+ 4, nerr
+ 4,
555 rtable
, gtable
, btable
, dr
, dg
, db
, ctx
->pixels
, cpc
);
565 * For standard colormap
567 static RXImage
*image2StandardPseudoColor(RContext
* ctx
, RImage
* image
)
570 register int x
, y
, r
, g
, b
;
574 unsigned int *rtable
, *gtable
, *btable
;
575 unsigned int base_pixel
= ctx
->std_rgb_map
->base_pixel
;
576 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
578 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
585 data
= (unsigned char *)ximg
->image
->data
;
587 rtable
= computeStdTable(ctx
->std_rgb_map
->red_mult
, ctx
->std_rgb_map
->red_max
);
589 gtable
= computeStdTable(ctx
->std_rgb_map
->green_mult
, ctx
->std_rgb_map
->green_max
);
591 btable
= computeStdTable(ctx
->std_rgb_map
->blue_mult
, ctx
->std_rgb_map
->blue_max
);
593 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
594 RErrorCode
= RERR_NOMEMORY
;
595 RDestroyXImage(ctx
, ximg
);
599 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
600 for (y
= 0; y
< image
->height
; y
++) {
601 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
) {
603 pixel
= (rtable
[*ptr
] + gtable
[*(ptr
+ 1)]
604 + btable
[*(ptr
+ 2)] + base_pixel
) & 0xffffffff;
606 XPutPixel(ximg
->image
, x
, y
, pixel
);
611 signed short *err
, *nerr
;
617 fprintf(stderr
, "pseudo color dithering with %d colors per channel\n",
618 ctx
->attribs
->colors_per_channel
);
620 err
= (short *)malloc(3 * (image
->width
+ 2) * sizeof(short));
621 nerr
= (short *)malloc(3 * (image
->width
+ 2) * sizeof(short));
625 RErrorCode
= RERR_NOMEMORY
;
626 RDestroyXImage(ctx
, ximg
);
629 for (x
= 0, x1
= 0; x
< image
->width
* 3; x1
+= channels
- 3) {
630 err
[x
++] = ptr
[x1
++];
631 err
[x
++] = ptr
[x1
++];
632 err
[x
++] = ptr
[x1
++];
634 err
[x
] = err
[x
+ 1] = err
[x
+ 2] = 0;
635 /* convert and dither the image to XImage */
636 for (y
= 0, ofs
= 0; y
< image
->height
; y
++) {
637 if (y
< image
->height
- 1) {
639 for (x
= 0, x1
= (y
+ 1) * image
->width
* channels
;
640 x
< image
->width
* 3; x1
+= channels
- 3) {
641 nerr
[x
++] = ptr
[x1
++];
642 nerr
[x
++] = ptr
[x1
++];
643 nerr
[x
++] = ptr
[x1
++];
647 nerr
[x
++] = ptr
[x1
++];
648 nerr
[x
++] = ptr
[x1
++];
649 nerr
[x
++] = ptr
[x1
++];
651 for (x
= 0; x
< image
->width
* 3; x
+= 3, ofs
++) {
657 if (err
[x
+ 1] > 0xff)
659 else if (err
[x
+ 1] < 0)
661 if (err
[x
+ 2] > 0xff)
663 else if (err
[x
+ 2] < 0)
667 g
= gtable
[err
[x
+ 1]];
668 b
= btable
[err
[x
+ 2]];
672 data
[ofs
] = base_pixel
+ pixel
;
675 rer
= err
[x
] - (ctx
->colors
[pixel
].red
>> 8);
676 ger
= err
[x
+ 1] - (ctx
->colors
[pixel
].green
>> 8);
677 ber
= err
[x
+ 2] - (ctx
->colors
[pixel
].blue
>> 8);
679 /* distribute error */
680 err
[x
+ 3 * 1] += (rer
* 7) / 16;
681 err
[x
+ 1 + 3 * 1] += (ger
* 7) / 16;
682 err
[x
+ 2 + 3 * 1] += (ber
* 7) / 16;
684 nerr
[x
] += (rer
* 5) / 16;
685 nerr
[x
+ 1] += (ger
* 5) / 16;
686 nerr
[x
+ 2] += (ber
* 5) / 16;
689 nerr
[x
- 3 * 1] += (rer
* 3) / 16;
690 nerr
[x
- 3 * 1 + 1] += (ger
* 3) / 16;
691 nerr
[x
- 3 * 1 + 2] += (ber
* 3) / 16;
694 nerr
[x
+ 3 * 1] += rer
/ 16;
695 nerr
[x
+ 1 + 3 * 1] += ger
/ 16;
696 nerr
[x
+ 2 + 3 * 1] += ber
/ 16;
698 /* skip to next line */
703 ofs
+= ximg
->image
->bytes_per_line
- image
->width
;
708 ximg
->image
->data
= (char *)data
;
713 static RXImage
*image2GrayScale(RContext
* ctx
, RImage
* image
)
716 register int x
, y
, g
;
718 const int cpc
= ctx
->attribs
->colors_per_channel
;
719 unsigned short gmask
;
720 unsigned short *table
;
722 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
724 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
731 data
= (unsigned char *)ximg
->image
->data
;
733 if (ctx
->vclass
== StaticGray
)
734 gmask
= (1 << ctx
->depth
) - 1; /* use all grays */
736 gmask
= cpc
* cpc
* cpc
- 1;
738 table
= computeTable(gmask
);
741 RErrorCode
= RERR_NOMEMORY
;
742 RDestroyXImage(ctx
, ximg
);
746 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
749 fprintf(stderr
, "grayscale match with %d colors per channel\n", cpc
);
751 for (y
= 0; y
< image
->height
; y
++) {
752 for (x
= 0; x
< image
->width
; x
++) {
754 g
= table
[(*ptr
* 30 + *(ptr
+ 1) * 59 + *(ptr
+ 2) * 11) / 100];
756 /*data[ofs] = ctx->colors[g].pixel; */
757 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[g
].pixel
);
766 const int dg
= 0xff / gmask
;
769 fprintf(stderr
, "grayscale dither with %d colors per channel\n", cpc
);
771 gerr
= (short *)malloc((image
->width
+ 2) * sizeof(short));
772 ngerr
= (short *)malloc((image
->width
+ 2) * sizeof(short));
773 if (!gerr
|| !ngerr
) {
776 RErrorCode
= RERR_NOMEMORY
;
777 RDestroyXImage(ctx
, ximg
);
780 for (x
= 0, y
= 0; x
< image
->width
; x
++, y
+= channels
) {
781 gerr
[x
] = (ptr
[y
] * 30 + ptr
[y
+ 1] * 59 + ptr
[y
+ 2] * 11) / 100;
784 /* convert and dither the image to XImage */
785 for (y
= 0; y
< image
->height
; y
++) {
786 if (y
< image
->height
- 1) {
788 for (x
= 0, x1
= (y
+ 1) * image
->width
* channels
; x
< image
->width
;
789 x
++, x1
+= channels
) {
790 ngerr
[x
] = (ptr
[x1
] * 30 + ptr
[x1
+ 1] * 59 + ptr
[x1
+ 2] * 11) / 100;
794 ngerr
[x
] = (ptr
[x1
] * 30 + ptr
[x1
+ 1] * 59 + ptr
[x1
+ 2] * 11) / 100;
796 for (x
= 0; x
< image
->width
; x
++) {
800 else if (gerr
[x
] < 0)
805 /*data[ofs] = ctx->colors[g].pixel; */
806 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[g
].pixel
);
808 ger
= gerr
[x
] - g
* dg
;
810 /* distribute error */
817 ngerr
[x
+ 1] += ger
- 2 * g
;
819 /* skip to next line */
827 ximg
->image
->data
= (char *)data
;
832 static RXImage
*image2Bitmap(RContext
* ctx
, RImage
* image
, int threshold
)
835 unsigned char *alpha
;
838 ximg
= RCreateXImage(ctx
, 1, image
->width
, image
->height
);
842 alpha
= image
->data
+ 3;
844 for (y
= 0; y
< image
->height
; y
++) {
845 for (x
= 0; x
< image
->width
; x
++) {
846 XPutPixel(ximg
->image
, x
, y
, (*alpha
<= threshold
? 0 : 1));
854 int RConvertImage(RContext
* context
, RImage
* image
, Pixmap
* pixmap
)
856 RXImage
*ximg
= NULL
;
861 assert(context
!= NULL
);
862 assert(image
!= NULL
);
863 assert(pixmap
!= NULL
);
865 switch (context
->vclass
) {
867 ximg
= image2TrueColor(context
, image
);
872 if (context
->attribs
->standard_colormap_mode
!= RIgnoreStdColormap
)
873 ximg
= image2StandardPseudoColor(context
, image
);
875 ximg
= image2PseudoColor(context
, image
);
880 ximg
= image2GrayScale(context
, image
);
888 *pixmap
= XCreatePixmap(context
->dpy
, context
->drawable
, image
->width
, image
->height
, context
->depth
);
891 if (context
->flags
.use_shared_pixmap
&& ximg
->is_shared
)
892 tmp
= R_CreateXImageMappedPixmap(context
, ximg
);
897 * We have to copy the shm Pixmap into a normal Pixmap because
898 * otherwise, we would have to control when Pixmaps are freed so
899 * that we can detach their shm segments. This is a problem if the
900 * program crash, leaving stale shared memory segments in the
901 * system (lots of them). But with some work, we can optimize
902 * things and remove this XCopyArea. This will require
903 * explicitly freeing all pixmaps when exiting or restarting
906 XCopyArea(context
->dpy
, tmp
, *pixmap
, context
->copy_gc
, 0, 0, image
->width
, image
->height
, 0, 0);
907 XFreePixmap(context
->dpy
, tmp
);
909 RPutXImage(context
, *pixmap
, context
->copy_gc
, ximg
, 0, 0, 0, 0, image
->width
, image
->height
);
911 #else /* !USE_XSHM */
912 RPutXImage(context
, *pixmap
, context
->copy_gc
, ximg
, 0, 0, 0, 0, image
->width
, image
->height
);
913 #endif /* !USE_XSHM */
915 RDestroyXImage(context
, ximg
);
920 /* make the gc permanent (create with context creation).
921 * GC creation is very expensive. altering its properties is not. -Dan
923 int RConvertImageMask(RContext
* context
, RImage
* image
, Pixmap
* pixmap
, Pixmap
* mask
, int threshold
)
927 RXImage
*ximg
= NULL
;
929 assert(context
!= NULL
);
930 assert(image
!= NULL
);
931 assert(pixmap
!= NULL
);
932 assert(mask
!= NULL
);
934 if (!RConvertImage(context
, image
, pixmap
))
937 if (image
->format
== RRGBFormat
) {
942 ximg
= image2Bitmap(context
, image
, threshold
);
947 *mask
= XCreatePixmap(context
->dpy
, context
->drawable
, image
->width
, image
->height
, 1);
948 gcv
.foreground
= context
->black
;
949 gcv
.background
= context
->white
;
950 gcv
.graphics_exposures
= False
;
951 gc
= XCreateGC(context
->dpy
, *mask
, GCForeground
| GCBackground
| GCGraphicsExposures
, &gcv
);
952 RPutXImage(context
, *mask
, gc
, ximg
, 0, 0, 0, 0, image
->width
, image
->height
);
953 RDestroyXImage(context
, ximg
);
954 XFreeGC(context
->dpy
, gc
);
959 Bool
RGetClosestXColor(RContext
* context
, const RColor
* color
, XColor
* retColor
)
961 if (context
->vclass
== TrueColor
) {
962 unsigned short rmask
, gmask
, bmask
;
963 unsigned short roffs
, goffs
, boffs
;
964 unsigned short *rtable
, *gtable
, *btable
;
966 roffs
= context
->red_offset
;
967 goffs
= context
->green_offset
;
968 boffs
= context
->blue_offset
;
970 rmask
= context
->visual
->red_mask
>> roffs
;
971 gmask
= context
->visual
->green_mask
>> goffs
;
972 bmask
= context
->visual
->blue_mask
>> boffs
;
974 rtable
= computeTable(rmask
);
975 gtable
= computeTable(gmask
);
976 btable
= computeTable(bmask
);
978 retColor
->pixel
= (((unsigned long) rtable
[color
->red
]) << roffs
)
979 | (((unsigned long) gtable
[color
->green
]) << goffs
)
980 | (((unsigned long) btable
[color
->blue
]) << boffs
);
982 retColor
->red
= color
->red
<< 8;
983 retColor
->green
= color
->green
<< 8;
984 retColor
->blue
= color
->blue
<< 8;
985 retColor
->flags
= DoRed
| DoGreen
| DoBlue
;
987 } else if (context
->vclass
== PseudoColor
|| context
->vclass
== StaticColor
) {
989 if (context
->attribs
->standard_colormap_mode
!= RIgnoreStdColormap
) {
990 unsigned int *rtable
, *gtable
, *btable
;
992 rtable
= computeStdTable(context
->std_rgb_map
->red_mult
, context
->std_rgb_map
->red_max
);
994 gtable
= computeStdTable(context
->std_rgb_map
->green_mult
,
995 context
->std_rgb_map
->green_max
);
997 btable
= computeStdTable(context
->std_rgb_map
->blue_mult
, context
->std_rgb_map
->blue_max
);
999 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
1000 RErrorCode
= RERR_NOMEMORY
;
1004 retColor
->pixel
= (rtable
[color
->red
]
1005 + gtable
[color
->green
]
1006 + btable
[color
->blue
]
1007 + context
->std_rgb_map
->base_pixel
) & 0xffffffff;
1008 retColor
->red
= color
->red
<< 8;
1009 retColor
->green
= color
->green
<< 8;
1010 retColor
->blue
= color
->blue
<< 8;
1011 retColor
->flags
= DoRed
| DoGreen
| DoBlue
;
1014 const int cpc
= context
->attribs
->colors_per_channel
;
1015 const unsigned short rmask
= cpc
- 1; /* different sizes could be used */
1016 const unsigned short gmask
= rmask
; /* for r,g,b */
1017 const unsigned short bmask
= rmask
;
1018 unsigned short *rtable
, *gtable
, *btable
;
1019 const int cpccpc
= cpc
* cpc
;
1022 rtable
= computeTable(rmask
);
1023 gtable
= computeTable(gmask
);
1024 btable
= computeTable(bmask
);
1026 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
1027 RErrorCode
= RERR_NOMEMORY
;
1030 index
= rtable
[color
->red
] * cpccpc
+ gtable
[color
->green
] * cpc
+ btable
[color
->blue
];
1031 *retColor
= context
->colors
[index
];
1034 } else if (context
->vclass
== GrayScale
|| context
->vclass
== StaticGray
) {
1036 const int cpc
= context
->attribs
->colors_per_channel
;
1037 unsigned short gmask
;
1038 unsigned short *table
;
1041 if (context
->vclass
== StaticGray
)
1042 gmask
= (1 << context
->depth
) - 1; /* use all grays */
1044 gmask
= cpc
* cpc
* cpc
- 1;
1046 table
= computeTable(gmask
);
1050 index
= table
[(color
->red
* 30 + color
->green
* 59 + color
->blue
* 11) / 100];
1052 *retColor
= context
->colors
[index
];
1054 RErrorCode
= RERR_INTERNAL
;