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>
42 #define NFREE(n) if (n) free(n)
44 #define HAS_ALPHA(I) ((I)->format == RRGBAFormat)
46 typedef struct RConversionTable
{
47 unsigned short table
[256];
50 struct RConversionTable
*next
;
53 typedef struct RStdConversionTable
{
54 unsigned int table
[256];
59 struct RStdConversionTable
*next
;
60 } RStdConversionTable
;
62 static RConversionTable
*conversionTable
= NULL
;
63 static RStdConversionTable
*stdConversionTable
= NULL
;
65 static void release_conversion_table(void)
67 RConversionTable
*tmp
= conversionTable
;
70 RConversionTable
*tmp_to_delete
= tmp
;
75 conversionTable
= NULL
;
78 static void release_std_conversion_table(void)
80 RStdConversionTable
*tmp
= stdConversionTable
;
83 RStdConversionTable
*tmp_to_delete
= tmp
;
88 stdConversionTable
= NULL
;
91 void r_destroy_conversion_tables(void)
93 release_conversion_table();
94 release_std_conversion_table();
97 static unsigned short *computeTable(unsigned short mask
)
99 RConversionTable
*tmp
= conversionTable
;
103 if (tmp
->index
== mask
)
111 tmp
= (RConversionTable
*) malloc(sizeof(RConversionTable
));
115 for (i
= 0; i
< 256; i
++)
116 tmp
->table
[i
] = (i
* mask
+ 0x7f) / 0xff;
119 tmp
->next
= conversionTable
;
120 conversionTable
= tmp
;
124 static unsigned int *computeStdTable(unsigned int mult
, unsigned int max
)
126 RStdConversionTable
*tmp
= stdConversionTable
;
130 if (tmp
->mult
== mult
&& tmp
->max
== max
)
138 tmp
= (RStdConversionTable
*) malloc(sizeof(RStdConversionTable
));
142 for (i
= 0; i
< 256; i
++) {
143 tmp
->table
[i
] = (i
* max
) / 0xff * mult
;
148 tmp
->next
= stdConversionTable
;
149 stdConversionTable
= tmp
;
154 /***************************************************************************/
157 convertTrueColor_generic(RXImage
* ximg
, RImage
* image
,
158 signed char *err
, signed char *nerr
,
159 const unsigned short *rtable
,
160 const unsigned short *gtable
,
161 const unsigned short *btable
,
162 const int dr
, const int dg
, const int db
,
163 const unsigned short roffs
, const unsigned short goffs
, const unsigned short boffs
)
169 unsigned char *ptr
= image
->data
;
170 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
172 /* convert and dither the image to XImage */
173 for (y
= 0; y
< image
->height
; y
++) {
177 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
) {
180 pixel
= *ptr
+ err
[x
];
183 else if (pixel
> 0xff)
187 rer
= pixel
- r
* dr
;
190 pixel
= *(ptr
+ 1) + err
[x
+ 1];
193 else if (pixel
> 0xff)
197 ger
= pixel
- g
* dg
;
200 pixel
= *(ptr
+ 2) + err
[x
+ 2];
203 else if (pixel
> 0xff)
207 ber
= pixel
- b
* db
;
209 pixel
= (r
<< roffs
) | (g
<< goffs
) | (b
<< boffs
);
210 XPutPixel(ximg
->image
, x
, y
, pixel
);
212 /* distribute error */
218 err
[x
+ 1 + 3 * 1] += g
;
219 err
[x
+ 2 + 3 * 1] += b
;
225 nerr
[x
+ 3 * 1] = rer
- 2 * r
;
226 nerr
[x
+ 1 + 3 * 1] = ger
- 2 * g
;
227 nerr
[x
+ 2 + 3 * 1] = ber
- 2 * b
;
229 /* skip to next line */
235 /* redither the 1st line to distribute error better */
241 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
) {
244 pixel
= *ptr
+ err
[x
];
247 else if (pixel
> 0xff)
251 rer
= pixel
- r
* dr
;
254 pixel
= *(ptr
+ 1) + err
[x
+ 1];
257 else if (pixel
> 0xff)
261 ger
= pixel
- g
* dg
;
264 pixel
= *(ptr
+ 2) + err
[x
+ 2];
267 else if (pixel
> 0xff)
271 ber
= pixel
- b
* db
;
273 pixel
= (r
<< roffs
) | (g
<< goffs
) | (b
<< boffs
);
274 XPutPixel(ximg
->image
, x
, y
, pixel
);
276 /* distribute error */
282 err
[x
+ 1 + 3 * 1] += g
;
283 err
[x
+ 2 + 3 * 1] += b
;
289 nerr
[x
+ 3 * 1] = rer
- 2 * r
;
290 nerr
[x
+ 1 + 3 * 1] = ger
- 2 * g
;
291 nerr
[x
+ 2 + 3 * 1] = ber
- 2 * b
;
295 static RXImage
*image2TrueColor(RContext
* ctx
, RImage
* image
)
298 unsigned short rmask
, gmask
, bmask
;
299 unsigned short roffs
, goffs
, boffs
;
300 unsigned short *rtable
, *gtable
, *btable
;
301 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
303 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
308 roffs
= ctx
->red_offset
;
309 goffs
= ctx
->green_offset
;
310 boffs
= ctx
->blue_offset
;
312 rmask
= ctx
->visual
->red_mask
>> roffs
;
313 gmask
= ctx
->visual
->green_mask
>> goffs
;
314 bmask
= ctx
->visual
->blue_mask
>> boffs
;
316 rtable
= computeTable(rmask
);
317 gtable
= computeTable(gmask
);
318 btable
= computeTable(bmask
);
320 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
321 RErrorCode
= RERR_NOMEMORY
;
322 RDestroyXImage(ctx
, ximg
);
326 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
328 unsigned long r
, g
, b
;
331 unsigned char *ptr
= image
->data
;
335 fputs("true color match\n", stderr
);
337 if (rmask
== 0xff && gmask
== 0xff && bmask
== 0xff) {
338 for (y
= 0; y
< image
->height
; y
++) {
339 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
) {
344 pixel
= (r
<< roffs
) | (g
<< goffs
) | (b
<< boffs
);
345 XPutPixel(ximg
->image
, x
, y
, pixel
);
349 for (y
= 0, ofs
= 0; y
< image
->height
; y
++) {
350 for (x
= 0; x
< image
->width
; x
++, ofs
+= channels
- 3) {
352 r
= rtable
[ptr
[ofs
++]];
353 g
= gtable
[ptr
[ofs
++]];
354 b
= btable
[ptr
[ofs
++]];
355 pixel
= (r
<< roffs
) | (g
<< goffs
) | (b
<< boffs
);
356 XPutPixel(ximg
->image
, x
, y
, pixel
);
362 const int dr
= 0xff / rmask
;
363 const int dg
= 0xff / gmask
;
364 const int db
= 0xff / bmask
;
367 fputs("true color dither\n", stderr
);
373 int ch
= (HAS_ALPHA(image
) ? 4 : 3);
375 err
= malloc(ch
* (image
->width
+ 2));
376 nerr
= malloc(ch
* (image
->width
+ 2));
380 RErrorCode
= RERR_NOMEMORY
;
381 RDestroyXImage(ctx
, ximg
);
385 memset(err
, 0, ch
* (image
->width
+ 2));
386 memset(nerr
, 0, ch
* (image
->width
+ 2));
388 convertTrueColor_generic(ximg
, image
, err
, nerr
,
389 rtable
, gtable
, btable
, dr
, dg
, db
, roffs
, goffs
, boffs
);
399 /***************************************************************************/
402 convertPseudoColor_to_8(RXImage
* ximg
, RImage
* image
,
403 signed char *err
, signed char *nerr
,
404 const unsigned short *rtable
,
405 const unsigned short *gtable
,
406 const unsigned short *btable
,
407 const int dr
, const int dg
, const int db
, unsigned long *pixels
, int cpc
)
413 unsigned char *ptr
= image
->data
;
414 unsigned char *optr
= (unsigned char *)ximg
->image
->data
;
415 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
416 int cpcpc
= cpc
* cpc
;
418 /* convert and dither the image to XImage */
419 for (y
= 0; y
< image
->height
; y
++) {
423 for (x
= 0; x
< image
->width
* 3; x
+= 3, ptr
+= channels
) {
426 pixel
= *ptr
+ err
[x
];
429 else if (pixel
> 0xff)
433 rer
= pixel
- r
* dr
;
436 pixel
= *(ptr
+ 1) + err
[x
+ 1];
439 else if (pixel
> 0xff)
443 ger
= pixel
- g
* dg
;
446 pixel
= *(ptr
+ 2) + err
[x
+ 2];
449 else if (pixel
> 0xff)
453 ber
= pixel
- b
* db
;
455 *optr
++ = pixels
[r
* cpcpc
+ g
* cpc
+ b
];
457 /* distribute error */
464 err
[x
+ 1 + 3 * 1] += g
;
465 err
[x
+ 2 + 3 * 1] += b
;
471 nerr
[x
+ 3 * 1] = rer
- 2 * r
;
472 nerr
[x
+ 1 + 3 * 1] = ger
- 2 * g
;
473 nerr
[x
+ 2 + 3 * 1] = ber
- 2 * b
;
475 /* skip to next line */
480 optr
+= ximg
->image
->bytes_per_line
- image
->width
;
484 static RXImage
*image2PseudoColor(RContext
* ctx
, RImage
* image
)
487 register int x
, y
, r
, g
, b
;
490 const int cpc
= ctx
->attribs
->colors_per_channel
;
491 const unsigned short rmask
= cpc
- 1; /* different sizes could be used */
492 const unsigned short gmask
= rmask
; /* for r,g,b */
493 const unsigned short bmask
= rmask
;
494 unsigned short *rtable
, *gtable
, *btable
;
495 const int cpccpc
= cpc
* cpc
;
496 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
498 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
505 /* Tables are same at the moment because rmask==gmask==bmask. */
506 rtable
= computeTable(rmask
);
507 gtable
= computeTable(gmask
);
508 btable
= computeTable(bmask
);
510 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
511 RErrorCode
= RERR_NOMEMORY
;
512 RDestroyXImage(ctx
, ximg
);
516 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
519 fprintf(stderr
, "pseudo color match with %d colors per channel\n", cpc
);
521 for (y
= 0; y
< image
->height
; y
++) {
522 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
- 3) {
527 pixel
= r
* cpccpc
+ g
* cpc
+ b
;
528 /*data[ofs] = ctx->colors[pixel].pixel; */
529 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[pixel
].pixel
);
536 const int dr
= 0xff / rmask
;
537 const int dg
= 0xff / gmask
;
538 const int db
= 0xff / bmask
;
541 fprintf(stderr
, "pseudo color dithering with %d colors per channel\n", cpc
);
543 err
= malloc(4 * (image
->width
+ 3));
544 nerr
= malloc(4 * (image
->width
+ 3));
548 RErrorCode
= RERR_NOMEMORY
;
549 RDestroyXImage(ctx
, ximg
);
552 memset(err
, 0, 4 * (image
->width
+ 3));
553 memset(nerr
, 0, 4 * (image
->width
+ 3));
555 convertPseudoColor_to_8(ximg
, image
, err
+ 4, nerr
+ 4,
556 rtable
, gtable
, btable
, dr
, dg
, db
, ctx
->pixels
, cpc
);
566 * For standard colormap
568 static RXImage
*image2StandardPseudoColor(RContext
* ctx
, RImage
* image
)
571 register int x
, y
, r
, g
, b
;
575 unsigned int *rtable
, *gtable
, *btable
;
576 unsigned int base_pixel
= ctx
->std_rgb_map
->base_pixel
;
577 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
579 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
586 data
= (unsigned char *)ximg
->image
->data
;
588 rtable
= computeStdTable(ctx
->std_rgb_map
->red_mult
, ctx
->std_rgb_map
->red_max
);
590 gtable
= computeStdTable(ctx
->std_rgb_map
->green_mult
, ctx
->std_rgb_map
->green_max
);
592 btable
= computeStdTable(ctx
->std_rgb_map
->blue_mult
, ctx
->std_rgb_map
->blue_max
);
594 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
595 RErrorCode
= RERR_NOMEMORY
;
596 RDestroyXImage(ctx
, ximg
);
600 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
601 for (y
= 0; y
< image
->height
; y
++) {
602 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
) {
604 pixel
= (rtable
[*ptr
] + gtable
[*(ptr
+ 1)]
605 + btable
[*(ptr
+ 2)] + base_pixel
) & 0xffffffff;
607 XPutPixel(ximg
->image
, x
, y
, pixel
);
612 signed short *err
, *nerr
;
618 fprintf(stderr
, "pseudo color dithering with %d colors per channel\n",
619 ctx
->attribs
->colors_per_channel
);
621 err
= (short *)malloc(3 * (image
->width
+ 2) * sizeof(short));
622 nerr
= (short *)malloc(3 * (image
->width
+ 2) * sizeof(short));
626 RErrorCode
= RERR_NOMEMORY
;
627 RDestroyXImage(ctx
, ximg
);
630 for (x
= 0, x1
= 0; x
< image
->width
* 3; x1
+= channels
- 3) {
631 err
[x
++] = ptr
[x1
++];
632 err
[x
++] = ptr
[x1
++];
633 err
[x
++] = ptr
[x1
++];
635 err
[x
] = err
[x
+ 1] = err
[x
+ 2] = 0;
636 /* convert and dither the image to XImage */
637 for (y
= 0, ofs
= 0; y
< image
->height
; y
++) {
638 if (y
< image
->height
- 1) {
640 for (x
= 0, x1
= (y
+ 1) * image
->width
* channels
;
641 x
< image
->width
* 3; x1
+= channels
- 3) {
642 nerr
[x
++] = ptr
[x1
++];
643 nerr
[x
++] = ptr
[x1
++];
644 nerr
[x
++] = ptr
[x1
++];
648 nerr
[x
++] = ptr
[x1
++];
649 nerr
[x
++] = ptr
[x1
++];
650 nerr
[x
++] = ptr
[x1
++];
652 for (x
= 0; x
< image
->width
* 3; x
+= 3, ofs
++) {
658 if (err
[x
+ 1] > 0xff)
660 else if (err
[x
+ 1] < 0)
662 if (err
[x
+ 2] > 0xff)
664 else if (err
[x
+ 2] < 0)
668 g
= gtable
[err
[x
+ 1]];
669 b
= btable
[err
[x
+ 2]];
673 data
[ofs
] = base_pixel
+ pixel
;
676 rer
= err
[x
] - (ctx
->colors
[pixel
].red
>> 8);
677 ger
= err
[x
+ 1] - (ctx
->colors
[pixel
].green
>> 8);
678 ber
= err
[x
+ 2] - (ctx
->colors
[pixel
].blue
>> 8);
680 /* distribute error */
681 err
[x
+ 3 * 1] += (rer
* 7) / 16;
682 err
[x
+ 1 + 3 * 1] += (ger
* 7) / 16;
683 err
[x
+ 2 + 3 * 1] += (ber
* 7) / 16;
685 nerr
[x
] += (rer
* 5) / 16;
686 nerr
[x
+ 1] += (ger
* 5) / 16;
687 nerr
[x
+ 2] += (ber
* 5) / 16;
690 nerr
[x
- 3 * 1] += (rer
* 3) / 16;
691 nerr
[x
- 3 * 1 + 1] += (ger
* 3) / 16;
692 nerr
[x
- 3 * 1 + 2] += (ber
* 3) / 16;
695 nerr
[x
+ 3 * 1] += rer
/ 16;
696 nerr
[x
+ 1 + 3 * 1] += ger
/ 16;
697 nerr
[x
+ 2 + 3 * 1] += ber
/ 16;
699 /* skip to next line */
704 ofs
+= ximg
->image
->bytes_per_line
- image
->width
;
709 ximg
->image
->data
= (char *)data
;
714 static RXImage
*image2GrayScale(RContext
* ctx
, RImage
* image
)
717 register int x
, y
, g
;
719 const int cpc
= ctx
->attribs
->colors_per_channel
;
720 unsigned short gmask
;
721 unsigned short *table
;
723 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
725 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
732 data
= (unsigned char *)ximg
->image
->data
;
734 if (ctx
->vclass
== StaticGray
)
735 gmask
= (1 << ctx
->depth
) - 1; /* use all grays */
737 gmask
= cpc
* cpc
* cpc
- 1;
739 table
= computeTable(gmask
);
742 RErrorCode
= RERR_NOMEMORY
;
743 RDestroyXImage(ctx
, ximg
);
747 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
750 fprintf(stderr
, "grayscale match with %d colors per channel\n", cpc
);
752 for (y
= 0; y
< image
->height
; y
++) {
753 for (x
= 0; x
< image
->width
; x
++) {
755 g
= table
[(*ptr
* 30 + *(ptr
+ 1) * 59 + *(ptr
+ 2) * 11) / 100];
757 /*data[ofs] = ctx->colors[g].pixel; */
758 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[g
].pixel
);
767 const int dg
= 0xff / gmask
;
770 fprintf(stderr
, "grayscale dither with %d colors per channel\n", cpc
);
772 gerr
= (short *)malloc((image
->width
+ 2) * sizeof(short));
773 ngerr
= (short *)malloc((image
->width
+ 2) * sizeof(short));
774 if (!gerr
|| !ngerr
) {
777 RErrorCode
= RERR_NOMEMORY
;
778 RDestroyXImage(ctx
, ximg
);
781 for (x
= 0, y
= 0; x
< image
->width
; x
++, y
+= channels
) {
782 gerr
[x
] = (ptr
[y
] * 30 + ptr
[y
+ 1] * 59 + ptr
[y
+ 2] * 11) / 100;
785 /* convert and dither the image to XImage */
786 for (y
= 0; y
< image
->height
; y
++) {
787 if (y
< image
->height
- 1) {
789 for (x
= 0, x1
= (y
+ 1) * image
->width
* channels
; x
< image
->width
;
790 x
++, x1
+= channels
) {
791 ngerr
[x
] = (ptr
[x1
] * 30 + ptr
[x1
+ 1] * 59 + ptr
[x1
+ 2] * 11) / 100;
795 ngerr
[x
] = (ptr
[x1
] * 30 + ptr
[x1
+ 1] * 59 + ptr
[x1
+ 2] * 11) / 100;
797 for (x
= 0; x
< image
->width
; x
++) {
801 else if (gerr
[x
] < 0)
806 /*data[ofs] = ctx->colors[g].pixel; */
807 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[g
].pixel
);
809 ger
= gerr
[x
] - g
* dg
;
811 /* distribute error */
818 ngerr
[x
+ 1] += ger
- 2 * g
;
820 /* skip to next line */
828 ximg
->image
->data
= (char *)data
;
833 static RXImage
*image2Bitmap(RContext
* ctx
, RImage
* image
, int threshold
)
836 unsigned char *alpha
;
839 ximg
= RCreateXImage(ctx
, 1, image
->width
, image
->height
);
843 alpha
= image
->data
+ 3;
845 for (y
= 0; y
< image
->height
; y
++) {
846 for (x
= 0; x
< image
->width
; x
++) {
847 XPutPixel(ximg
->image
, x
, y
, (*alpha
<= threshold
? 0 : 1));
855 int RConvertImage(RContext
* context
, RImage
* image
, Pixmap
* pixmap
)
857 RXImage
*ximg
= NULL
;
862 assert(context
!= NULL
);
863 assert(image
!= NULL
);
864 assert(pixmap
!= NULL
);
866 switch (context
->vclass
) {
868 ximg
= image2TrueColor(context
, image
);
873 if (context
->attribs
->standard_colormap_mode
!= RIgnoreStdColormap
)
874 ximg
= image2StandardPseudoColor(context
, image
);
876 ximg
= image2PseudoColor(context
, image
);
881 ximg
= image2GrayScale(context
, image
);
889 *pixmap
= XCreatePixmap(context
->dpy
, context
->drawable
, image
->width
, image
->height
, context
->depth
);
892 if (context
->flags
.use_shared_pixmap
&& ximg
->is_shared
)
893 tmp
= R_CreateXImageMappedPixmap(context
, ximg
);
898 * We have to copy the shm Pixmap into a normal Pixmap because
899 * otherwise, we would have to control when Pixmaps are freed so
900 * that we can detach their shm segments. This is a problem if the
901 * program crash, leaving stale shared memory segments in the
902 * system (lots of them). But with some work, we can optimize
903 * things and remove this XCopyArea. This will require
904 * explicitly freeing all pixmaps when exiting or restarting
907 XCopyArea(context
->dpy
, tmp
, *pixmap
, context
->copy_gc
, 0, 0, image
->width
, image
->height
, 0, 0);
908 XFreePixmap(context
->dpy
, tmp
);
910 RPutXImage(context
, *pixmap
, context
->copy_gc
, ximg
, 0, 0, 0, 0, image
->width
, image
->height
);
912 #else /* !USE_XSHM */
913 RPutXImage(context
, *pixmap
, context
->copy_gc
, ximg
, 0, 0, 0, 0, image
->width
, image
->height
);
914 #endif /* !USE_XSHM */
916 RDestroyXImage(context
, ximg
);
921 /* make the gc permanent (create with context creation).
922 * GC creation is very expensive. altering its properties is not. -Dan
924 int RConvertImageMask(RContext
* context
, RImage
* image
, Pixmap
* pixmap
, Pixmap
* mask
, int threshold
)
928 RXImage
*ximg
= NULL
;
930 assert(context
!= NULL
);
931 assert(image
!= NULL
);
932 assert(pixmap
!= NULL
);
933 assert(mask
!= NULL
);
935 if (!RConvertImage(context
, image
, pixmap
))
938 if (image
->format
== RRGBFormat
) {
943 ximg
= image2Bitmap(context
, image
, threshold
);
948 *mask
= XCreatePixmap(context
->dpy
, context
->drawable
, image
->width
, image
->height
, 1);
949 gcv
.foreground
= context
->black
;
950 gcv
.background
= context
->white
;
951 gcv
.graphics_exposures
= False
;
952 gc
= XCreateGC(context
->dpy
, *mask
, GCForeground
| GCBackground
| GCGraphicsExposures
, &gcv
);
953 RPutXImage(context
, *mask
, gc
, ximg
, 0, 0, 0, 0, image
->width
, image
->height
);
954 RDestroyXImage(context
, ximg
);
955 XFreeGC(context
->dpy
, gc
);
960 Bool
RGetClosestXColor(RContext
* context
, const RColor
* color
, XColor
* retColor
)
962 if (context
->vclass
== TrueColor
) {
963 unsigned short rmask
, gmask
, bmask
;
964 unsigned short roffs
, goffs
, boffs
;
965 unsigned short *rtable
, *gtable
, *btable
;
967 roffs
= context
->red_offset
;
968 goffs
= context
->green_offset
;
969 boffs
= context
->blue_offset
;
971 rmask
= context
->visual
->red_mask
>> roffs
;
972 gmask
= context
->visual
->green_mask
>> goffs
;
973 bmask
= context
->visual
->blue_mask
>> boffs
;
975 rtable
= computeTable(rmask
);
976 gtable
= computeTable(gmask
);
977 btable
= computeTable(bmask
);
979 retColor
->pixel
= (((unsigned long) rtable
[color
->red
]) << roffs
)
980 | (((unsigned long) gtable
[color
->green
]) << goffs
)
981 | (((unsigned long) btable
[color
->blue
]) << boffs
);
983 retColor
->red
= color
->red
<< 8;
984 retColor
->green
= color
->green
<< 8;
985 retColor
->blue
= color
->blue
<< 8;
986 retColor
->flags
= DoRed
| DoGreen
| DoBlue
;
988 } else if (context
->vclass
== PseudoColor
|| context
->vclass
== StaticColor
) {
990 if (context
->attribs
->standard_colormap_mode
!= RIgnoreStdColormap
) {
991 unsigned int *rtable
, *gtable
, *btable
;
993 rtable
= computeStdTable(context
->std_rgb_map
->red_mult
, context
->std_rgb_map
->red_max
);
995 gtable
= computeStdTable(context
->std_rgb_map
->green_mult
,
996 context
->std_rgb_map
->green_max
);
998 btable
= computeStdTable(context
->std_rgb_map
->blue_mult
, context
->std_rgb_map
->blue_max
);
1000 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
1001 RErrorCode
= RERR_NOMEMORY
;
1005 retColor
->pixel
= (rtable
[color
->red
]
1006 + gtable
[color
->green
]
1007 + btable
[color
->blue
]
1008 + context
->std_rgb_map
->base_pixel
) & 0xffffffff;
1009 retColor
->red
= color
->red
<< 8;
1010 retColor
->green
= color
->green
<< 8;
1011 retColor
->blue
= color
->blue
<< 8;
1012 retColor
->flags
= DoRed
| DoGreen
| DoBlue
;
1015 const int cpc
= context
->attribs
->colors_per_channel
;
1016 const unsigned short rmask
= cpc
- 1; /* different sizes could be used */
1017 const unsigned short gmask
= rmask
; /* for r,g,b */
1018 const unsigned short bmask
= rmask
;
1019 unsigned short *rtable
, *gtable
, *btable
;
1020 const int cpccpc
= cpc
* cpc
;
1023 rtable
= computeTable(rmask
);
1024 gtable
= computeTable(gmask
);
1025 btable
= computeTable(bmask
);
1027 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
1028 RErrorCode
= RERR_NOMEMORY
;
1031 index
= rtable
[color
->red
] * cpccpc
+ gtable
[color
->green
] * cpc
+ btable
[color
->blue
];
1032 *retColor
= context
->colors
[index
];
1035 } else if (context
->vclass
== GrayScale
|| context
->vclass
== StaticGray
) {
1037 const int cpc
= context
->attribs
->colors_per_channel
;
1038 unsigned short gmask
;
1039 unsigned short *table
;
1042 if (context
->vclass
== StaticGray
)
1043 gmask
= (1 << context
->depth
) - 1; /* use all grays */
1045 gmask
= cpc
* cpc
* cpc
- 1;
1047 table
= computeTable(gmask
);
1051 index
= table
[(color
->red
* 30 + color
->green
* 59 + color
->blue
* 11) / 100];
1053 *retColor
= context
->colors
[index
];
1055 RErrorCode
= RERR_INTERNAL
;