1 /* convert.c - convert RImage to Pixmap
3 * Raster graphics library
5 * Copyright (c) 1997-2000 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., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * 1. Using Grayscale visual with Dithering crashes wmaker
24 * 2. Ghost dock/appicon is wrong in Pseudocolor, Staticgray, Grayscale
31 #include <X11/Xutil.h>
46 extern Pixmap
R_CreateXImageMappedPixmap(RContext
*context
, RXImage
*ximage
);
51 #define HAS_ALPHA(I) ((I)->format == RRGBAFormat)
54 typedef struct RConversionTable
{
55 unsigned short table
[256];
58 struct RConversionTable
*next
;
62 typedef struct RStdConversionTable
{
63 unsigned int table
[256];
68 struct RStdConversionTable
*next
;
69 } RStdConversionTable
;
73 static RConversionTable
*conversionTable
= NULL
;
74 static RStdConversionTable
*stdConversionTable
= NULL
;
77 static unsigned short*
78 computeTable(unsigned short mask
)
80 RConversionTable
*tmp
= conversionTable
;
84 if (tmp
->index
== mask
)
92 tmp
= (RConversionTable
*)malloc(sizeof(RConversionTable
));
97 tmp
->table
[i
] = (i
*mask
+ 0x7f)/0xff;
100 tmp
->next
= conversionTable
;
101 conversionTable
= tmp
;
107 computeStdTable(unsigned int mult
, unsigned int max
)
109 RStdConversionTable
*tmp
= stdConversionTable
;
113 if (tmp
->mult
== mult
&& tmp
->max
== max
)
121 tmp
= (RStdConversionTable
*)malloc(sizeof(RStdConversionTable
));
125 for (i
=0; i
<256; i
++) {
126 tmp
->table
[i
] = (i
*max
)/0xff * mult
;
131 tmp
->next
= stdConversionTable
;
132 stdConversionTable
= tmp
;
137 /***************************************************************************/
141 convertTrueColor_generic(RXImage
*ximg
, RImage
*image
,
142 signed char *err
, signed char *nerr
,
146 const int dr
, const int dg
, const int db
,
147 const unsigned short roffs
,
148 const unsigned short goffs
,
149 const unsigned short boffs
)
155 unsigned char *ptr
= image
->data
;
156 int channels
= (image
->format
== RRGBAFormat
? 4 : 3);
159 /* convert and dither the image to XImage */
160 for (y
=0; y
<image
->height
; y
++) {
164 for (x
=0; x
<image
->width
; x
++, ptr
+=channels
) {
167 pixel
= *ptr
+ err
[x
];
168 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
174 pixel
= *(ptr
+1) + err
[x
+1];
175 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
181 pixel
= *(ptr
+2) + err
[x
+2];
182 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
188 pixel
= (r
<<roffs
) | (g
<<goffs
) | (b
<<boffs
);
189 XPutPixel(ximg
->image
, x
, y
, pixel
);
191 /* distribute error */
205 nerr
[x
+1+3*1]=ger
-2*g
;
206 nerr
[x
+2+3*1]=ber
-2*b
;
208 /* skip to next line */
219 image2TrueColor(RContext
*ctx
, RImage
*image
)
222 unsigned short rmask
, gmask
, bmask
;
223 unsigned short roffs
, goffs
, boffs
;
224 unsigned short *rtable
, *gtable
, *btable
;
225 int channels
= (image
->format
== RRGBAFormat
? 4 : 3);
227 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
232 roffs
= ctx
->red_offset
;
233 goffs
= ctx
->green_offset
;
234 boffs
= ctx
->blue_offset
;
236 rmask
= ctx
->visual
->red_mask
>> roffs
;
237 gmask
= ctx
->visual
->green_mask
>> goffs
;
238 bmask
= ctx
->visual
->blue_mask
>> boffs
;
240 rtable
= computeTable(rmask
);
241 gtable
= computeTable(gmask
);
242 btable
= computeTable(bmask
);
244 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
245 RErrorCode
= RERR_NOMEMORY
;
246 RDestroyXImage(ctx
, ximg
);
255 if (ctx
->attribs
->render_mode
==RBestMatchRendering
) {
259 unsigned char *ptr
= image
->data
;
263 puts("true color match");
265 if (rmask
==0xff && gmask
==0xff && bmask
==0xff) {
266 for (y
=0; y
< image
->height
; y
++) {
267 for (x
=0; x
< image
->width
; x
++, ptr
+=channels
) {
269 pixel
= (*(ptr
)<<roffs
) | (*(ptr
+1)<<goffs
) | (*(ptr
+2)<<boffs
);
270 XPutPixel(ximg
->image
, x
, y
, pixel
);
274 for (y
=0, ofs
=0; y
< image
->height
; y
++) {
275 for (x
=0; x
< image
->width
; x
++, ofs
+=channels
-3) {
277 r
= rtable
[ptr
[ofs
++]];
278 g
= gtable
[ptr
[ofs
++]];
279 b
= btable
[ptr
[ofs
++]];
280 pixel
= (r
<<roffs
) | (g
<<goffs
) | (b
<<boffs
);
281 XPutPixel(ximg
->image
, x
, y
, pixel
);
287 const int dr
=0xff/rmask
;
288 const int dg
=0xff/gmask
;
289 const int db
=0xff/bmask
;
292 puts("true color dither");
297 int ch
= (image
->format
== RRGBAFormat
? 4 : 3);
299 err
= malloc(ch
*(image
->width
+2));
300 nerr
= malloc(ch
*(image
->width
+2));
304 RErrorCode
= RERR_NOMEMORY
;
305 RDestroyXImage(ctx
, ximg
);
309 memset(err
, 0, ch
*(image
->width
+2));
310 memset(nerr
, 0, ch
*(image
->width
+2));
312 convertTrueColor_generic(ximg
, image
, err
, nerr
,
313 rtable
, gtable
, btable
,
314 dr
, dg
, db
, roffs
, goffs
, boffs
);
329 /***************************************************************************/
332 convertPseudoColor_to_8(RXImage
*ximg
, RImage
*image
,
333 signed char *err
, signed char *nerr
,
337 const int dr
, const int dg
, const int db
,
338 unsigned long *pixels
,
345 unsigned char *ptr
= image
->data
;
346 unsigned char *optr
= ximg
->image
->data
;
347 int channels
= (image
->format
== RRGBAFormat
? 4 : 3);
350 /* convert and dither the image to XImage */
351 for (y
=0; y
<image
->height
; y
++) {
355 for (x
=0; x
<image
->width
*3; x
+=3, ptr
+=channels
) {
358 pixel
= *ptr
+ err
[x
];
359 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
365 pixel
= *(ptr
+1) + err
[x
+1];
366 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
372 pixel
= *(ptr
+2) + err
[x
+2];
373 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
378 *optr
++ = pixels
[r
*cpcpc
+ g
*cpc
+ b
];
380 /* distribute error */
395 nerr
[x
+1+3*1]=ger
-2*g
;
396 nerr
[x
+2+3*1]=ber
-2*b
;
398 /* skip to next line */
403 optr
+= ximg
->image
->bytes_per_line
- image
->width
;
410 image2PseudoColor(RContext
*ctx
, RImage
*image
)
413 register int x
, y
, r
, g
, b
;
416 const int cpc
=ctx
->attribs
->colors_per_channel
;
417 const unsigned short rmask
= cpc
-1; /* different sizes could be used */
418 const unsigned short gmask
= rmask
; /* for r,g,b */
419 const unsigned short bmask
= rmask
;
420 unsigned short *rtable
, *gtable
, *btable
;
421 const int cpccpc
= cpc
*cpc
;
422 int channels
= (image
->format
== RRGBAFormat
? 4 : 3);
424 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
431 /* Tables are same at the moment because rmask==gmask==bmask. */
432 rtable
= computeTable(rmask
);
433 gtable
= computeTable(gmask
);
434 btable
= computeTable(bmask
);
436 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
437 RErrorCode
= RERR_NOMEMORY
;
438 RDestroyXImage(ctx
, ximg
);
442 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
445 printf("pseudo color match with %d colors per channel\n", cpc
);
447 for (y
=0; y
<image
->height
; y
++) {
448 for (x
=0; x
<image
->width
; x
++, ptr
+=channels
-3) {
453 pixel
= r
*cpccpc
+ g
*cpc
+ b
;
454 /*data[ofs] = ctx->colors[pixel].pixel;*/
455 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[pixel
].pixel
);
462 const int dr
=0xff/rmask
;
463 const int dg
=0xff/gmask
;
464 const int db
=0xff/bmask
;
468 printf("pseudo color dithering with %d colors per channel\n", cpc
);
470 err
= malloc(4*(image
->width
+3));
471 nerr
= malloc(4*(image
->width
+3));
475 RErrorCode
= RERR_NOMEMORY
;
476 RDestroyXImage(ctx
, ximg
);
479 memset(err
, 0, 4*(image
->width
+3));
480 memset(nerr
, 0, 4*(image
->width
+3));
482 convertPseudoColor_to_8(ximg
, image
, err
+4, nerr
+4,
483 rtable
, gtable
, btable
,
484 dr
, dg
, db
, ctx
->pixels
, cpc
);
495 * For standard colormap
498 image2StandardPseudoColor(RContext
*ctx
, RImage
*image
)
501 register int x
, y
, r
, g
, b
;
505 unsigned int *rtable
, *gtable
, *btable
;
506 unsigned int base_pixel
= ctx
->std_rgb_map
->base_pixel
;
507 int channels
= (image
->format
== RRGBAFormat
? 4 : 3);
510 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
517 data
= (unsigned char *)ximg
->image
->data
;
520 rtable
= computeStdTable(ctx
->std_rgb_map
->red_mult
,
521 ctx
->std_rgb_map
->red_max
);
523 gtable
= computeStdTable(ctx
->std_rgb_map
->green_mult
,
524 ctx
->std_rgb_map
->green_max
);
526 btable
= computeStdTable(ctx
->std_rgb_map
->blue_mult
,
527 ctx
->std_rgb_map
->blue_max
);
529 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
530 RErrorCode
= RERR_NOMEMORY
;
531 RDestroyXImage(ctx
, ximg
);
536 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
537 for (y
=0; y
<image
->height
; y
++) {
538 for (x
=0; x
<image
->width
; x
++, ptr
+=channels
) {
540 pixel
= (rtable
[*ptr
] + gtable
[*(ptr
+1)]
541 + btable
[*(ptr
+2)] + base_pixel
) & 0xffffffff;
543 XPutPixel(ximg
->image
, x
, y
, pixel
);
548 signed short *err
, *nerr
;
554 printf("pseudo color dithering with %d colors per channel\n", cpc
);
556 err
= (short*)malloc(3*(image
->width
+2)*sizeof(short));
557 nerr
= (short*)malloc(3*(image
->width
+2)*sizeof(short));
563 RErrorCode
= RERR_NOMEMORY
;
564 RDestroyXImage(ctx
, ximg
);
567 for (x
=0, x1
=0; x
<image
->width
*3; x1
+=channels
-3) {
568 err
[x
++] = ptr
[x1
++];
569 err
[x
++] = ptr
[x1
++];
570 err
[x
++] = ptr
[x1
++];
572 err
[x
] = err
[x
+1] = err
[x
+2] = 0;
573 /* convert and dither the image to XImage */
574 for (y
=0, ofs
=0; y
<image
->height
; y
++) {
575 if (y
<image
->height
-1) {
577 for (x
=0, x1
=(y
+1)*image
->width
*channels
;
580 nerr
[x
++] = ptr
[x1
++];
581 nerr
[x
++] = ptr
[x1
++];
582 nerr
[x
++] = ptr
[x1
++];
586 nerr
[x
++] = ptr
[x1
++];
587 nerr
[x
++] = ptr
[x1
++];
588 nerr
[x
++] = ptr
[x1
++];
590 for (x
=0; x
<image
->width
*3; x
+=3, ofs
++) {
592 if (err
[x
]>0xff) err
[x
]=0xff; else if (err
[x
]<0) err
[x
]=0;
593 if (err
[x
+1]>0xff) err
[x
+1]=0xff; else if (err
[x
+1]<0) err
[x
+1]=0;
594 if (err
[x
+2]>0xff) err
[x
+2]=0xff; else if (err
[x
+2]<0) err
[x
+2]=0;
597 g
= gtable
[err
[x
+1]];
598 b
= btable
[err
[x
+2]];
602 data
[ofs
] = base_pixel
+ pixel
;
605 rer
= err
[x
] - (ctx
->colors
[pixel
].red
>>8);
606 ger
= err
[x
+1] - (ctx
->colors
[pixel
].green
>>8);
607 ber
= err
[x
+2] - (ctx
->colors
[pixel
].blue
>>8);
609 /* distribute error */
610 err
[x
+3*1]+=(rer
*7)/16;
611 err
[x
+1+3*1]+=(ger
*7)/16;
612 err
[x
+2+3*1]+=(ber
*7)/16;
615 nerr
[x
+1]+=(ger
*5)/16;
616 nerr
[x
+2]+=(ber
*5)/16;
619 nerr
[x
-3*1]+=(rer
*3)/16;
620 nerr
[x
-3*1+1]+=(ger
*3)/16;
621 nerr
[x
-3*1+2]+=(ber
*3)/16;
625 nerr
[x
+1+3*1]+=ger
/16;
626 nerr
[x
+2+3*1]+=ber
/16;
628 /* skip to next line */
633 ofs
+= ximg
->image
->bytes_per_line
- image
->width
;
638 ximg
->image
->data
= (char*)data
;
646 image2GrayScale(RContext
*ctx
, RImage
*image
)
649 register int x
, y
, g
;
651 const int cpc
=ctx
->attribs
->colors_per_channel
;
652 unsigned short gmask
;
653 unsigned short *table
;
655 int channels
= (image
->format
== RRGBAFormat
? 4 : 3);
658 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
665 data
= (unsigned char *)ximg
->image
->data
;
667 if (ctx
->vclass
== StaticGray
)
668 gmask
= (1<<ctx
->depth
) - 1; /* use all grays */
670 gmask
= cpc
*cpc
*cpc
-1;
672 table
= computeTable(gmask
);
675 RErrorCode
= RERR_NOMEMORY
;
676 RDestroyXImage(ctx
, ximg
);
680 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
683 printf("grayscale match with %d colors per channel\n", cpc
);
685 for (y
=0; y
<image
->height
; y
++) {
686 for (x
=0; x
<image
->width
; x
++) {
688 g
= table
[(*ptr
* 30 + *(ptr
+1) * 59 + *(ptr
+2) * 11)/100];
690 /*data[ofs] = ctx->colors[g].pixel;*/
691 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[g
].pixel
);
700 const int dg
=0xff/gmask
;
703 printf("grayscale dither with %d colors per channel\n", cpc
);
705 gerr
= (short*)malloc((image
->width
+2)*sizeof(short));
706 ngerr
= (short*)malloc((image
->width
+2)*sizeof(short));
707 if (!gerr
|| !ngerr
) {
710 RErrorCode
= RERR_NOMEMORY
;
711 RDestroyXImage(ctx
, ximg
);
714 for (x
=0, y
=0; x
<image
->width
; x
++, y
+=channels
) {
715 gerr
[x
] = (ptr
[y
]*30 + ptr
[y
+1]*59 + ptr
[y
+2]*11)/100;
718 /* convert and dither the image to XImage */
719 for (y
=0; y
<image
->height
; y
++) {
720 if (y
<image
->height
-1) {
722 for (x
=0, x1
=(y
+1)*image
->width
*channels
; x
<image
->width
; x
++, x1
+=channels
) {
723 ngerr
[x
] = (ptr
[x1
]*30 + ptr
[x1
+1]*59 + ptr
[x1
+2]*11)/100;
727 ngerr
[x
] = (ptr
[x1
]*30 + ptr
[x1
+1]*59 + ptr
[x1
+2]*11)/100;
729 for (x
=0; x
<image
->width
; x
++) {
731 if (gerr
[x
]>0xff) gerr
[x
]=0xff; else if (gerr
[x
]<0) gerr
[x
]=0;
735 /*data[ofs] = ctx->colors[g].pixel;*/
736 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[g
].pixel
);
738 ger
= gerr
[x
] - g
*dg
;
740 /* distribute error */
749 /* skip to next line */
757 ximg
->image
->data
= (char*)data
;
764 image2Bitmap(RContext
*ctx
, RImage
*image
, int threshold
)
767 unsigned char *alpha
;
770 ximg
= RCreateXImage(ctx
, 1, image
->width
, image
->height
);
774 alpha
= image
->data
+3;
776 for (y
= 0; y
< image
->height
; y
++) {
777 for (x
= 0; x
< image
->width
; x
++) {
778 XPutPixel(ximg
->image
, x
, y
, (*alpha
<= threshold
? 0 : 1));
790 hermesConvert(RContext
*context
, RImage
*image
)
797 ximage
= RCreateXImage(context
, context
->depth
,
798 image
->width
, image
->height
);
803 /* The masks look weird for images with alpha. but they work this way.
804 * wth does hermes do internally?
806 source
.bits
= (HAS_ALPHA(image
) ? 32 : 24);
807 if (ximage
->image
->byte_order
==LSBFirst
) {
812 if (source
.bits
== 32) {
813 source
.r
= 0xff000000;
814 source
.g
= 0x00ff0000;
815 source
.b
= 0x0000ff00;
822 source
.a
= 0; /* Don't care about alpha */
824 source
.has_colorkey
= 0;
826 /* This is a hack and certainly looks weird, but it works :P
827 * it assumes though that green is inbetween red and blue (the mask) */
828 if (ximage
->image
->byte_order
==LSBFirst
) {
829 dest
.r
= context
->visual
->blue_mask
;
830 dest
.g
= context
->visual
->green_mask
;
831 dest
.b
= context
->visual
->red_mask
;
833 dest
.r
= context
->visual
->red_mask
;
834 dest
.g
= context
->visual
->green_mask
;
835 dest
.b
= context
->visual
->blue_mask
;
838 dest
.bits
= ximage
->image
->bits_per_pixel
;
839 if (context
->vclass
== TrueColor
)
843 dest
.has_colorkey
= 0;
845 /*printf("source r=0x%x, g=0x%x, b=0x%x, a=0x%x, b=%d, i=%d, c=%d\n",
846 source.r, source.g, source.b, source.a,
847 source.bits, source.indexed, source.has_colorkey);
848 printf("dest r=0x%x, g=0x%x, b=0x%x, a=0x%x, b=%d, i=%d, c=%d\n",
849 dest.r, dest.g, dest.b, dest.a,
850 dest.bits, dest.indexed, dest.has_colorkey);
853 Hermes_ConverterRequest(context
->hermes_data
->converter
, &source
, &dest
);
855 Hermes_ConverterPalette(context
->hermes_data
->converter
,
856 context
->hermes_data
->palette
, 0);
858 Hermes_ConverterCopy(context
->hermes_data
->converter
,
859 image
->data
, 0, 0, image
->width
, image
->height
,
860 image
->width
* (image
->format
== RRGBFormat
? 3 : 4),
861 ximage
->image
->data
, 0, 0,
862 image
->width
, image
->height
,
863 ximage
->image
->bytes_per_line
);
867 #endif /* HAVE_HERMES */
871 RConvertImage(RContext
*context
, RImage
*image
, Pixmap
*pixmap
)
878 assert(context
!=NULL
);
880 assert(pixmap
!=NULL
);
882 switch (context
->vclass
) {
885 if (context
->attribs
->render_mode
== RBestMatchRendering
) {
886 ximg
= hermesConvert(context
, image
);
888 ximg
= image2TrueColor(context
, image
);
890 #else /* !HAVE_HERMES */
891 ximg
= image2TrueColor(context
, image
);
897 /* For StaticColor we can also use hermes, but it doesn't dither */
901 if (context
->attribs
->standard_colormap_mode
!= RIgnoreStdColormap
)
902 ximg
= image2StandardPseudoColor(context
, image
);
904 ximg
= image2PseudoColor(context
, image
);
912 ximg
= image2GrayScale(context
, image
);
921 *pixmap
= XCreatePixmap(context
->dpy
, context
->drawable
, image
->width
,
922 image
->height
, context
->depth
);
925 if (context
->flags
.use_shared_pixmap
&& ximg
->is_shared
)
926 tmp
= R_CreateXImageMappedPixmap(context
, ximg
);
931 * We have to copy the shm Pixmap into a normal Pixmap because
932 * otherwise, we would have to control when Pixmaps are freed so
933 * that we can detach their shm segments. This is a problem if the
934 * program crash, leaving stale shared memory segments in the
935 * system (lots of them). But with some work, we can optimize
936 * things and remove this XCopyArea. This will require
937 * explicitly freeing all pixmaps when exiting or restarting
940 XCopyArea(context
->dpy
, tmp
, *pixmap
, context
->copy_gc
, 0, 0,
941 image
->width
, image
->height
, 0, 0);
942 XFreePixmap(context
->dpy
, tmp
);
944 RPutXImage(context
, *pixmap
, context
->copy_gc
, ximg
, 0, 0, 0, 0,
945 image
->width
, image
->height
);
948 RPutXImage(context
, *pixmap
, context
->copy_gc
, ximg
, 0, 0, 0, 0,
949 image
->width
, image
->height
);
952 RDestroyXImage(context
, ximg
);
959 RConvertImageMask(RContext
*context
, RImage
*image
, Pixmap
*pixmap
,
960 Pixmap
*mask
, int threshold
)
966 assert(context
!=NULL
);
968 assert(pixmap
!=NULL
);
971 if (!RConvertImage(context
, image
, pixmap
))
974 if (image
->format
==RRGBFormat
) {
979 ximg
= image2Bitmap(context
, image
, threshold
);
984 *mask
= XCreatePixmap(context
->dpy
, context
->drawable
, image
->width
,
986 gcv
.foreground
= context
->black
;
987 gcv
.background
= context
->white
;
988 gcv
.graphics_exposures
= False
;
989 gc
= XCreateGC(context
->dpy
, *mask
, GCForeground
|GCBackground
990 |GCGraphicsExposures
, &gcv
);
991 RPutXImage(context
, *mask
, gc
, ximg
, 0, 0, 0, 0,
992 image
->width
, image
->height
);
993 RDestroyXImage(context
, ximg
);
1000 RGetClosestXColor(RContext
*context
, RColor
*color
, XColor
*retColor
)
1002 if (context
->vclass
== TrueColor
) {
1003 unsigned short rmask
, gmask
, bmask
;
1004 unsigned short roffs
, goffs
, boffs
;
1005 unsigned short *rtable
, *gtable
, *btable
;
1007 roffs
= context
->red_offset
;
1008 goffs
= context
->green_offset
;
1009 boffs
= context
->blue_offset
;
1011 rmask
= context
->visual
->red_mask
>> roffs
;
1012 gmask
= context
->visual
->green_mask
>> goffs
;
1013 bmask
= context
->visual
->blue_mask
>> boffs
;
1015 rtable
= computeTable(rmask
);
1016 gtable
= computeTable(gmask
);
1017 btable
= computeTable(bmask
);
1019 retColor
->pixel
= (rtable
[color
->red
]<<roffs
) |
1020 (gtable
[color
->green
]<<goffs
) | (btable
[color
->blue
]<<boffs
);
1022 retColor
->red
= color
->red
<< 8;
1023 retColor
->green
= color
->green
<< 8;
1024 retColor
->blue
= color
->blue
<< 8;
1025 retColor
->flags
= DoRed
|DoGreen
|DoBlue
;
1027 } else if (context
->vclass
== PseudoColor
1028 || context
->vclass
== StaticColor
) {
1030 if (context
->attribs
->standard_colormap_mode
!= RIgnoreStdColormap
) {
1031 unsigned int *rtable
, *gtable
, *btable
;
1033 rtable
= computeStdTable(context
->std_rgb_map
->red_mult
,
1034 context
->std_rgb_map
->red_max
);
1036 gtable
= computeStdTable(context
->std_rgb_map
->green_mult
,
1037 context
->std_rgb_map
->green_max
);
1039 btable
= computeStdTable(context
->std_rgb_map
->blue_mult
,
1040 context
->std_rgb_map
->blue_max
);
1042 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
1043 RErrorCode
= RERR_NOMEMORY
;
1047 retColor
->pixel
= (rtable
[color
->red
]
1048 + gtable
[color
->green
]
1049 + btable
[color
->blue
]
1050 + context
->std_rgb_map
->base_pixel
) & 0xffffffff;
1051 retColor
->red
= color
->red
<<8;
1052 retColor
->green
= color
->green
<<8;
1053 retColor
->blue
= color
->blue
<<8;
1054 retColor
->flags
= DoRed
|DoGreen
|DoBlue
;
1057 const int cpc
=context
->attribs
->colors_per_channel
;
1058 const unsigned short rmask
= cpc
-1; /* different sizes could be used */
1059 const unsigned short gmask
= rmask
; /* for r,g,b */
1060 const unsigned short bmask
= rmask
;
1061 unsigned short *rtable
, *gtable
, *btable
;
1062 const int cpccpc
= cpc
*cpc
;
1065 rtable
= computeTable(rmask
);
1066 gtable
= computeTable(gmask
);
1067 btable
= computeTable(bmask
);
1069 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
1070 RErrorCode
= RERR_NOMEMORY
;
1073 index
= rtable
[color
->red
]*cpccpc
+ gtable
[color
->green
]*cpc
1074 + btable
[color
->blue
];
1075 *retColor
= context
->colors
[index
];
1078 } else if (context
->vclass
== GrayScale
|| context
->vclass
== StaticGray
) {
1080 const int cpc
= context
->attribs
->colors_per_channel
;
1081 unsigned short gmask
;
1082 unsigned short *table
;
1085 if (context
->vclass
== StaticGray
)
1086 gmask
= (1<<context
->depth
) - 1; /* use all grays */
1088 gmask
= cpc
*cpc
*cpc
-1;
1090 table
= computeTable(gmask
);
1094 index
= table
[(color
->red
*30 + color
->green
*59 + color
->blue
*11)/100];
1096 *retColor
= context
->colors
[index
];
1098 RErrorCode
= RERR_INTERNAL
;