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 extern Pixmap
R_CreateXImageMappedPixmap(RContext
* context
, RXImage
* ximage
);
44 #define NFREE(n) if (n) free(n)
46 #define HAS_ALPHA(I) ((I)->format == RRGBAFormat)
48 typedef struct RConversionTable
{
49 unsigned short table
[256];
52 struct RConversionTable
*next
;
55 typedef struct RStdConversionTable
{
56 unsigned int table
[256];
61 struct RStdConversionTable
*next
;
62 } RStdConversionTable
;
64 static RConversionTable
*conversionTable
= NULL
;
65 static RStdConversionTable
*stdConversionTable
= NULL
;
67 static void release_conversion_table(void)
69 RConversionTable
*tmp
= conversionTable
;
72 RConversionTable
*tmp_to_delete
= tmp
;
77 conversionTable
= NULL
;
80 static void release_std_conversion_table(void)
82 RStdConversionTable
*tmp
= stdConversionTable
;
85 RStdConversionTable
*tmp_to_delete
= tmp
;
90 stdConversionTable
= NULL
;
93 void r_destroy_conversion_tables(void)
95 release_conversion_table();
96 release_std_conversion_table();
99 static unsigned short *computeTable(unsigned short mask
)
101 RConversionTable
*tmp
= conversionTable
;
105 if (tmp
->index
== mask
)
113 tmp
= (RConversionTable
*) malloc(sizeof(RConversionTable
));
117 for (i
= 0; i
< 256; i
++)
118 tmp
->table
[i
] = (i
* mask
+ 0x7f) / 0xff;
121 tmp
->next
= conversionTable
;
122 conversionTable
= tmp
;
126 static unsigned int *computeStdTable(unsigned int mult
, unsigned int max
)
128 RStdConversionTable
*tmp
= stdConversionTable
;
132 if (tmp
->mult
== mult
&& tmp
->max
== max
)
140 tmp
= (RStdConversionTable
*) malloc(sizeof(RStdConversionTable
));
144 for (i
= 0; i
< 256; i
++) {
145 tmp
->table
[i
] = (i
* max
) / 0xff * mult
;
150 tmp
->next
= stdConversionTable
;
151 stdConversionTable
= tmp
;
156 /***************************************************************************/
159 convertTrueColor_generic(RXImage
* ximg
, RImage
* image
,
160 signed char *err
, signed char *nerr
,
161 const unsigned short *rtable
,
162 const unsigned short *gtable
,
163 const unsigned short *btable
,
164 const int dr
, const int dg
, const int db
,
165 const unsigned short roffs
, const unsigned short goffs
, const unsigned short boffs
)
171 unsigned char *ptr
= image
->data
;
172 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
174 /* convert and dither the image to XImage */
175 for (y
= 0; y
< image
->height
; y
++) {
179 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
) {
182 pixel
= *ptr
+ err
[x
];
185 else if (pixel
> 0xff)
189 rer
= pixel
- r
* dr
;
192 pixel
= *(ptr
+ 1) + err
[x
+ 1];
195 else if (pixel
> 0xff)
199 ger
= pixel
- g
* dg
;
202 pixel
= *(ptr
+ 2) + err
[x
+ 2];
205 else if (pixel
> 0xff)
209 ber
= pixel
- b
* db
;
211 pixel
= (r
<< roffs
) | (g
<< goffs
) | (b
<< boffs
);
212 XPutPixel(ximg
->image
, x
, y
, pixel
);
214 /* distribute error */
220 err
[x
+ 1 + 3 * 1] += g
;
221 err
[x
+ 2 + 3 * 1] += b
;
227 nerr
[x
+ 3 * 1] = rer
- 2 * r
;
228 nerr
[x
+ 1 + 3 * 1] = ger
- 2 * g
;
229 nerr
[x
+ 2 + 3 * 1] = ber
- 2 * b
;
231 /* skip to next line */
237 /* redither the 1st line to distribute error better */
243 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
) {
246 pixel
= *ptr
+ err
[x
];
249 else if (pixel
> 0xff)
253 rer
= pixel
- r
* dr
;
256 pixel
= *(ptr
+ 1) + err
[x
+ 1];
259 else if (pixel
> 0xff)
263 ger
= pixel
- g
* dg
;
266 pixel
= *(ptr
+ 2) + err
[x
+ 2];
269 else if (pixel
> 0xff)
273 ber
= pixel
- b
* db
;
275 pixel
= (r
<< roffs
) | (g
<< goffs
) | (b
<< boffs
);
276 XPutPixel(ximg
->image
, x
, y
, pixel
);
278 /* distribute error */
284 err
[x
+ 1 + 3 * 1] += g
;
285 err
[x
+ 2 + 3 * 1] += b
;
291 nerr
[x
+ 3 * 1] = rer
- 2 * r
;
292 nerr
[x
+ 1 + 3 * 1] = ger
- 2 * g
;
293 nerr
[x
+ 2 + 3 * 1] = ber
- 2 * b
;
297 static RXImage
*image2TrueColor(RContext
* ctx
, RImage
* image
)
300 unsigned short rmask
, gmask
, bmask
;
301 unsigned short roffs
, goffs
, boffs
;
302 unsigned short *rtable
, *gtable
, *btable
;
303 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
305 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
310 roffs
= ctx
->red_offset
;
311 goffs
= ctx
->green_offset
;
312 boffs
= ctx
->blue_offset
;
314 rmask
= ctx
->visual
->red_mask
>> roffs
;
315 gmask
= ctx
->visual
->green_mask
>> goffs
;
316 bmask
= ctx
->visual
->blue_mask
>> boffs
;
318 rtable
= computeTable(rmask
);
319 gtable
= computeTable(gmask
);
320 btable
= computeTable(bmask
);
322 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
323 RErrorCode
= RERR_NOMEMORY
;
324 RDestroyXImage(ctx
, ximg
);
328 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
330 unsigned long r
, g
, b
;
333 unsigned char *ptr
= image
->data
;
337 fputs("true color match\n", stderr
);
339 if (rmask
== 0xff && gmask
== 0xff && bmask
== 0xff) {
340 for (y
= 0; y
< image
->height
; y
++) {
341 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
) {
346 pixel
= (r
<< roffs
) | (g
<< goffs
) | (b
<< boffs
);
347 XPutPixel(ximg
->image
, x
, y
, pixel
);
351 for (y
= 0, ofs
= 0; y
< image
->height
; y
++) {
352 for (x
= 0; x
< image
->width
; x
++, ofs
+= channels
- 3) {
354 r
= rtable
[ptr
[ofs
++]];
355 g
= gtable
[ptr
[ofs
++]];
356 b
= btable
[ptr
[ofs
++]];
357 pixel
= (r
<< roffs
) | (g
<< goffs
) | (b
<< boffs
);
358 XPutPixel(ximg
->image
, x
, y
, pixel
);
364 const int dr
= 0xff / rmask
;
365 const int dg
= 0xff / gmask
;
366 const int db
= 0xff / bmask
;
369 fputs("true color dither\n", stderr
);
375 int ch
= (HAS_ALPHA(image
) ? 4 : 3);
377 err
= malloc(ch
* (image
->width
+ 2));
378 nerr
= malloc(ch
* (image
->width
+ 2));
382 RErrorCode
= RERR_NOMEMORY
;
383 RDestroyXImage(ctx
, ximg
);
387 memset(err
, 0, ch
* (image
->width
+ 2));
388 memset(nerr
, 0, ch
* (image
->width
+ 2));
390 convertTrueColor_generic(ximg
, image
, err
, nerr
,
391 rtable
, gtable
, btable
, dr
, dg
, db
, roffs
, goffs
, boffs
);
401 /***************************************************************************/
404 convertPseudoColor_to_8(RXImage
* ximg
, RImage
* image
,
405 signed char *err
, signed char *nerr
,
406 const unsigned short *rtable
,
407 const unsigned short *gtable
,
408 const unsigned short *btable
,
409 const int dr
, const int dg
, const int db
, unsigned long *pixels
, int cpc
)
415 unsigned char *ptr
= image
->data
;
416 unsigned char *optr
= (unsigned char *)ximg
->image
->data
;
417 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
418 int cpcpc
= cpc
* cpc
;
420 /* convert and dither the image to XImage */
421 for (y
= 0; y
< image
->height
; y
++) {
425 for (x
= 0; x
< image
->width
* 3; x
+= 3, ptr
+= channels
) {
428 pixel
= *ptr
+ err
[x
];
431 else if (pixel
> 0xff)
435 rer
= pixel
- r
* dr
;
438 pixel
= *(ptr
+ 1) + err
[x
+ 1];
441 else if (pixel
> 0xff)
445 ger
= pixel
- g
* dg
;
448 pixel
= *(ptr
+ 2) + err
[x
+ 2];
451 else if (pixel
> 0xff)
455 ber
= pixel
- b
* db
;
457 *optr
++ = pixels
[r
* cpcpc
+ g
* cpc
+ b
];
459 /* distribute error */
466 err
[x
+ 1 + 3 * 1] += g
;
467 err
[x
+ 2 + 3 * 1] += b
;
473 nerr
[x
+ 3 * 1] = rer
- 2 * r
;
474 nerr
[x
+ 1 + 3 * 1] = ger
- 2 * g
;
475 nerr
[x
+ 2 + 3 * 1] = ber
- 2 * b
;
477 /* skip to next line */
482 optr
+= ximg
->image
->bytes_per_line
- image
->width
;
486 static RXImage
*image2PseudoColor(RContext
* ctx
, RImage
* image
)
489 register int x
, y
, r
, g
, b
;
492 const int cpc
= ctx
->attribs
->colors_per_channel
;
493 const unsigned short rmask
= cpc
- 1; /* different sizes could be used */
494 const unsigned short gmask
= rmask
; /* for r,g,b */
495 const unsigned short bmask
= rmask
;
496 unsigned short *rtable
, *gtable
, *btable
;
497 const int cpccpc
= cpc
* cpc
;
498 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
500 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
507 /* Tables are same at the moment because rmask==gmask==bmask. */
508 rtable
= computeTable(rmask
);
509 gtable
= computeTable(gmask
);
510 btable
= computeTable(bmask
);
512 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
513 RErrorCode
= RERR_NOMEMORY
;
514 RDestroyXImage(ctx
, ximg
);
518 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
521 fprintf(stderr
, "pseudo color match with %d colors per channel\n", cpc
);
523 for (y
= 0; y
< image
->height
; y
++) {
524 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
- 3) {
529 pixel
= r
* cpccpc
+ g
* cpc
+ b
;
530 /*data[ofs] = ctx->colors[pixel].pixel; */
531 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[pixel
].pixel
);
538 const int dr
= 0xff / rmask
;
539 const int dg
= 0xff / gmask
;
540 const int db
= 0xff / bmask
;
543 fprintf(stderr
, "pseudo color dithering with %d colors per channel\n", cpc
);
545 err
= malloc(4 * (image
->width
+ 3));
546 nerr
= malloc(4 * (image
->width
+ 3));
550 RErrorCode
= RERR_NOMEMORY
;
551 RDestroyXImage(ctx
, ximg
);
554 memset(err
, 0, 4 * (image
->width
+ 3));
555 memset(nerr
, 0, 4 * (image
->width
+ 3));
557 convertPseudoColor_to_8(ximg
, image
, err
+ 4, nerr
+ 4,
558 rtable
, gtable
, btable
, dr
, dg
, db
, ctx
->pixels
, cpc
);
568 * For standard colormap
570 static RXImage
*image2StandardPseudoColor(RContext
* ctx
, RImage
* image
)
573 register int x
, y
, r
, g
, b
;
577 unsigned int *rtable
, *gtable
, *btable
;
578 unsigned int base_pixel
= ctx
->std_rgb_map
->base_pixel
;
579 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
581 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
588 data
= (unsigned char *)ximg
->image
->data
;
590 rtable
= computeStdTable(ctx
->std_rgb_map
->red_mult
, ctx
->std_rgb_map
->red_max
);
592 gtable
= computeStdTable(ctx
->std_rgb_map
->green_mult
, ctx
->std_rgb_map
->green_max
);
594 btable
= computeStdTable(ctx
->std_rgb_map
->blue_mult
, ctx
->std_rgb_map
->blue_max
);
596 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
597 RErrorCode
= RERR_NOMEMORY
;
598 RDestroyXImage(ctx
, ximg
);
602 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
603 for (y
= 0; y
< image
->height
; y
++) {
604 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
) {
606 pixel
= (rtable
[*ptr
] + gtable
[*(ptr
+ 1)]
607 + btable
[*(ptr
+ 2)] + base_pixel
) & 0xffffffff;
609 XPutPixel(ximg
->image
, x
, y
, pixel
);
614 signed short *err
, *nerr
;
620 fprintf(stderr
, "pseudo color dithering with %d colors per channel\n",
621 ctx
->attribs
->colors_per_channel
);
623 err
= (short *)malloc(3 * (image
->width
+ 2) * sizeof(short));
624 nerr
= (short *)malloc(3 * (image
->width
+ 2) * sizeof(short));
628 RErrorCode
= RERR_NOMEMORY
;
629 RDestroyXImage(ctx
, ximg
);
632 for (x
= 0, x1
= 0; x
< image
->width
* 3; x1
+= channels
- 3) {
633 err
[x
++] = ptr
[x1
++];
634 err
[x
++] = ptr
[x1
++];
635 err
[x
++] = ptr
[x1
++];
637 err
[x
] = err
[x
+ 1] = err
[x
+ 2] = 0;
638 /* convert and dither the image to XImage */
639 for (y
= 0, ofs
= 0; y
< image
->height
; y
++) {
640 if (y
< image
->height
- 1) {
642 for (x
= 0, x1
= (y
+ 1) * image
->width
* channels
;
643 x
< image
->width
* 3; x1
+= channels
- 3) {
644 nerr
[x
++] = ptr
[x1
++];
645 nerr
[x
++] = ptr
[x1
++];
646 nerr
[x
++] = ptr
[x1
++];
650 nerr
[x
++] = ptr
[x1
++];
651 nerr
[x
++] = ptr
[x1
++];
652 nerr
[x
++] = ptr
[x1
++];
654 for (x
= 0; x
< image
->width
* 3; x
+= 3, ofs
++) {
660 if (err
[x
+ 1] > 0xff)
662 else if (err
[x
+ 1] < 0)
664 if (err
[x
+ 2] > 0xff)
666 else if (err
[x
+ 2] < 0)
670 g
= gtable
[err
[x
+ 1]];
671 b
= btable
[err
[x
+ 2]];
675 data
[ofs
] = base_pixel
+ pixel
;
678 rer
= err
[x
] - (ctx
->colors
[pixel
].red
>> 8);
679 ger
= err
[x
+ 1] - (ctx
->colors
[pixel
].green
>> 8);
680 ber
= err
[x
+ 2] - (ctx
->colors
[pixel
].blue
>> 8);
682 /* distribute error */
683 err
[x
+ 3 * 1] += (rer
* 7) / 16;
684 err
[x
+ 1 + 3 * 1] += (ger
* 7) / 16;
685 err
[x
+ 2 + 3 * 1] += (ber
* 7) / 16;
687 nerr
[x
] += (rer
* 5) / 16;
688 nerr
[x
+ 1] += (ger
* 5) / 16;
689 nerr
[x
+ 2] += (ber
* 5) / 16;
692 nerr
[x
- 3 * 1] += (rer
* 3) / 16;
693 nerr
[x
- 3 * 1 + 1] += (ger
* 3) / 16;
694 nerr
[x
- 3 * 1 + 2] += (ber
* 3) / 16;
697 nerr
[x
+ 3 * 1] += rer
/ 16;
698 nerr
[x
+ 1 + 3 * 1] += ger
/ 16;
699 nerr
[x
+ 2 + 3 * 1] += ber
/ 16;
701 /* skip to next line */
706 ofs
+= ximg
->image
->bytes_per_line
- image
->width
;
711 ximg
->image
->data
= (char *)data
;
716 static RXImage
*image2GrayScale(RContext
* ctx
, RImage
* image
)
719 register int x
, y
, g
;
721 const int cpc
= ctx
->attribs
->colors_per_channel
;
722 unsigned short gmask
;
723 unsigned short *table
;
725 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
727 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
734 data
= (unsigned char *)ximg
->image
->data
;
736 if (ctx
->vclass
== StaticGray
)
737 gmask
= (1 << ctx
->depth
) - 1; /* use all grays */
739 gmask
= cpc
* cpc
* cpc
- 1;
741 table
= computeTable(gmask
);
744 RErrorCode
= RERR_NOMEMORY
;
745 RDestroyXImage(ctx
, ximg
);
749 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
752 fprintf(stderr
, "grayscale match with %d colors per channel\n", cpc
);
754 for (y
= 0; y
< image
->height
; y
++) {
755 for (x
= 0; x
< image
->width
; x
++) {
757 g
= table
[(*ptr
* 30 + *(ptr
+ 1) * 59 + *(ptr
+ 2) * 11) / 100];
759 /*data[ofs] = ctx->colors[g].pixel; */
760 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[g
].pixel
);
769 const int dg
= 0xff / gmask
;
772 fprintf(stderr
, "grayscale dither with %d colors per channel\n", cpc
);
774 gerr
= (short *)malloc((image
->width
+ 2) * sizeof(short));
775 ngerr
= (short *)malloc((image
->width
+ 2) * sizeof(short));
776 if (!gerr
|| !ngerr
) {
779 RErrorCode
= RERR_NOMEMORY
;
780 RDestroyXImage(ctx
, ximg
);
783 for (x
= 0, y
= 0; x
< image
->width
; x
++, y
+= channels
) {
784 gerr
[x
] = (ptr
[y
] * 30 + ptr
[y
+ 1] * 59 + ptr
[y
+ 2] * 11) / 100;
787 /* convert and dither the image to XImage */
788 for (y
= 0; y
< image
->height
; y
++) {
789 if (y
< image
->height
- 1) {
791 for (x
= 0, x1
= (y
+ 1) * image
->width
* channels
; x
< image
->width
;
792 x
++, x1
+= channels
) {
793 ngerr
[x
] = (ptr
[x1
] * 30 + ptr
[x1
+ 1] * 59 + ptr
[x1
+ 2] * 11) / 100;
797 ngerr
[x
] = (ptr
[x1
] * 30 + ptr
[x1
+ 1] * 59 + ptr
[x1
+ 2] * 11) / 100;
799 for (x
= 0; x
< image
->width
; x
++) {
803 else if (gerr
[x
] < 0)
808 /*data[ofs] = ctx->colors[g].pixel; */
809 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[g
].pixel
);
811 ger
= gerr
[x
] - g
* dg
;
813 /* distribute error */
820 ngerr
[x
+ 1] += ger
- 2 * g
;
822 /* skip to next line */
830 ximg
->image
->data
= (char *)data
;
835 static RXImage
*image2Bitmap(RContext
* ctx
, RImage
* image
, int threshold
)
838 unsigned char *alpha
;
841 ximg
= RCreateXImage(ctx
, 1, image
->width
, image
->height
);
845 alpha
= image
->data
+ 3;
847 for (y
= 0; y
< image
->height
; y
++) {
848 for (x
= 0; x
< image
->width
; x
++) {
849 XPutPixel(ximg
->image
, x
, y
, (*alpha
<= threshold
? 0 : 1));
857 int RConvertImage(RContext
* context
, RImage
* image
, Pixmap
* pixmap
)
859 RXImage
*ximg
= NULL
;
864 assert(context
!= NULL
);
865 assert(image
!= NULL
);
866 assert(pixmap
!= NULL
);
868 switch (context
->vclass
) {
870 ximg
= image2TrueColor(context
, image
);
875 if (context
->attribs
->standard_colormap_mode
!= RIgnoreStdColormap
)
876 ximg
= image2StandardPseudoColor(context
, image
);
878 ximg
= image2PseudoColor(context
, image
);
883 ximg
= image2GrayScale(context
, image
);
891 *pixmap
= XCreatePixmap(context
->dpy
, context
->drawable
, image
->width
, image
->height
, context
->depth
);
894 if (context
->flags
.use_shared_pixmap
&& ximg
->is_shared
)
895 tmp
= R_CreateXImageMappedPixmap(context
, ximg
);
900 * We have to copy the shm Pixmap into a normal Pixmap because
901 * otherwise, we would have to control when Pixmaps are freed so
902 * that we can detach their shm segments. This is a problem if the
903 * program crash, leaving stale shared memory segments in the
904 * system (lots of them). But with some work, we can optimize
905 * things and remove this XCopyArea. This will require
906 * explicitly freeing all pixmaps when exiting or restarting
909 XCopyArea(context
->dpy
, tmp
, *pixmap
, context
->copy_gc
, 0, 0, image
->width
, image
->height
, 0, 0);
910 XFreePixmap(context
->dpy
, tmp
);
912 RPutXImage(context
, *pixmap
, context
->copy_gc
, ximg
, 0, 0, 0, 0, image
->width
, image
->height
);
914 #else /* !USE_XSHM */
915 RPutXImage(context
, *pixmap
, context
->copy_gc
, ximg
, 0, 0, 0, 0, image
->width
, image
->height
);
916 #endif /* !USE_XSHM */
918 RDestroyXImage(context
, ximg
);
923 /* make the gc permanent (create with context creation).
924 * GC creation is very expensive. altering its properties is not. -Dan
926 int RConvertImageMask(RContext
* context
, RImage
* image
, Pixmap
* pixmap
, Pixmap
* mask
, int threshold
)
930 RXImage
*ximg
= NULL
;
932 assert(context
!= NULL
);
933 assert(image
!= NULL
);
934 assert(pixmap
!= NULL
);
935 assert(mask
!= NULL
);
937 if (!RConvertImage(context
, image
, pixmap
))
940 if (image
->format
== RRGBFormat
) {
945 ximg
= image2Bitmap(context
, image
, threshold
);
950 *mask
= XCreatePixmap(context
->dpy
, context
->drawable
, image
->width
, image
->height
, 1);
951 gcv
.foreground
= context
->black
;
952 gcv
.background
= context
->white
;
953 gcv
.graphics_exposures
= False
;
954 gc
= XCreateGC(context
->dpy
, *mask
, GCForeground
| GCBackground
| GCGraphicsExposures
, &gcv
);
955 RPutXImage(context
, *mask
, gc
, ximg
, 0, 0, 0, 0, image
->width
, image
->height
);
956 RDestroyXImage(context
, ximg
);
957 XFreeGC(context
->dpy
, gc
);
962 Bool
RGetClosestXColor(RContext
* context
, const RColor
* color
, XColor
* retColor
)
964 if (context
->vclass
== TrueColor
) {
965 unsigned short rmask
, gmask
, bmask
;
966 unsigned short roffs
, goffs
, boffs
;
967 unsigned short *rtable
, *gtable
, *btable
;
969 roffs
= context
->red_offset
;
970 goffs
= context
->green_offset
;
971 boffs
= context
->blue_offset
;
973 rmask
= context
->visual
->red_mask
>> roffs
;
974 gmask
= context
->visual
->green_mask
>> goffs
;
975 bmask
= context
->visual
->blue_mask
>> boffs
;
977 rtable
= computeTable(rmask
);
978 gtable
= computeTable(gmask
);
979 btable
= computeTable(bmask
);
981 retColor
->pixel
= (((unsigned long) rtable
[color
->red
]) << roffs
)
982 | (((unsigned long) gtable
[color
->green
]) << goffs
)
983 | (((unsigned long) btable
[color
->blue
]) << boffs
);
985 retColor
->red
= color
->red
<< 8;
986 retColor
->green
= color
->green
<< 8;
987 retColor
->blue
= color
->blue
<< 8;
988 retColor
->flags
= DoRed
| DoGreen
| DoBlue
;
990 } else if (context
->vclass
== PseudoColor
|| context
->vclass
== StaticColor
) {
992 if (context
->attribs
->standard_colormap_mode
!= RIgnoreStdColormap
) {
993 unsigned int *rtable
, *gtable
, *btable
;
995 rtable
= computeStdTable(context
->std_rgb_map
->red_mult
, context
->std_rgb_map
->red_max
);
997 gtable
= computeStdTable(context
->std_rgb_map
->green_mult
,
998 context
->std_rgb_map
->green_max
);
1000 btable
= computeStdTable(context
->std_rgb_map
->blue_mult
, context
->std_rgb_map
->blue_max
);
1002 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
1003 RErrorCode
= RERR_NOMEMORY
;
1007 retColor
->pixel
= (rtable
[color
->red
]
1008 + gtable
[color
->green
]
1009 + btable
[color
->blue
]
1010 + context
->std_rgb_map
->base_pixel
) & 0xffffffff;
1011 retColor
->red
= color
->red
<< 8;
1012 retColor
->green
= color
->green
<< 8;
1013 retColor
->blue
= color
->blue
<< 8;
1014 retColor
->flags
= DoRed
| DoGreen
| DoBlue
;
1017 const int cpc
= context
->attribs
->colors_per_channel
;
1018 const unsigned short rmask
= cpc
- 1; /* different sizes could be used */
1019 const unsigned short gmask
= rmask
; /* for r,g,b */
1020 const unsigned short bmask
= rmask
;
1021 unsigned short *rtable
, *gtable
, *btable
;
1022 const int cpccpc
= cpc
* cpc
;
1025 rtable
= computeTable(rmask
);
1026 gtable
= computeTable(gmask
);
1027 btable
= computeTable(bmask
);
1029 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
1030 RErrorCode
= RERR_NOMEMORY
;
1033 index
= rtable
[color
->red
] * cpccpc
+ gtable
[color
->green
] * cpc
+ btable
[color
->blue
];
1034 *retColor
= context
->colors
[index
];
1037 } else if (context
->vclass
== GrayScale
|| context
->vclass
== StaticGray
) {
1039 const int cpc
= context
->attribs
->colors_per_channel
;
1040 unsigned short gmask
;
1041 unsigned short *table
;
1044 if (context
->vclass
== StaticGray
)
1045 gmask
= (1 << context
->depth
) - 1; /* use all grays */
1047 gmask
= cpc
* cpc
* cpc
- 1;
1049 table
= computeTable(gmask
);
1053 index
= table
[(color
->red
* 30 + color
->green
* 59 + color
->blue
* 11) / 100];
1055 *retColor
= context
->colors
[index
];
1057 RErrorCode
= RERR_INTERNAL
;