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., 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
);
52 extern void x86_PseudoColor_32_to_8(unsigned char *image
,
53 unsigned char *ximage
,
54 char *err
, char *nerr
,
56 int dr
, int dg
, int db
,
57 unsigned long *pixels
,
59 int width
, int height
,
66 extern int x86_check_mmx();
68 extern void x86_mmx_TrueColor_32_to_16(unsigned char *image
,
69 unsigned short *ximage
,
70 short *err
, short *nerr
,
71 short *rtable
, short *gtable
,
73 int dr
, int dg
, int db
,
77 int width
, int height
,
82 #endif /* ASM_X86_MMX */
84 #define NFREE(n) if (n) free(n)
86 #define HAS_ALPHA(I) ((I)->format == RRGBAFormat)
89 typedef struct RConversionTable
{
90 unsigned short table
[256];
93 struct RConversionTable
*next
;
97 typedef struct RStdConversionTable
{
98 unsigned int table
[256];
103 struct RStdConversionTable
*next
;
104 } RStdConversionTable
;
108 static RConversionTable
*conversionTable
= NULL
;
109 static RStdConversionTable
*stdConversionTable
= NULL
;
112 static unsigned short*
113 computeTable(unsigned short mask
)
115 RConversionTable
*tmp
= conversionTable
;
119 if (tmp
->index
== mask
)
127 tmp
= (RConversionTable
*)malloc(sizeof(RConversionTable
));
132 tmp
->table
[i
] = (i
*mask
+ 0x7f)/0xff;
135 tmp
->next
= conversionTable
;
136 conversionTable
= tmp
;
142 computeStdTable(unsigned int mult
, unsigned int max
)
144 RStdConversionTable
*tmp
= stdConversionTable
;
148 if (tmp
->mult
== mult
&& tmp
->max
== max
)
156 tmp
= (RStdConversionTable
*)malloc(sizeof(RStdConversionTable
));
160 for (i
=0; i
<256; i
++) {
161 tmp
->table
[i
] = (i
*max
)/0xff * mult
;
166 tmp
->next
= stdConversionTable
;
167 stdConversionTable
= tmp
;
172 /***************************************************************************/
176 convertTrueColor_generic(RXImage
*ximg
, RImage
*image
,
177 signed char *err
, signed char *nerr
,
181 const int dr
, const int dg
, const int db
,
182 const unsigned short roffs
,
183 const unsigned short goffs
,
184 const unsigned short boffs
)
190 unsigned char *ptr
= image
->data
;
191 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
193 /* convert and dither the image to XImage */
194 for (y
=0; y
<image
->height
; y
++) {
198 for (x
=0; x
<image
->width
; x
++, ptr
+=channels
) {
201 pixel
= *ptr
+ err
[x
];
202 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
208 pixel
= *(ptr
+1) + err
[x
+1];
209 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
215 pixel
= *(ptr
+2) + err
[x
+2];
216 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
222 pixel
= (r
<<roffs
) | (g
<<goffs
) | (b
<<boffs
);
223 XPutPixel(ximg
->image
, x
, y
, pixel
);
225 /* distribute error */
239 nerr
[x
+1+3*1]=ger
-2*g
;
240 nerr
[x
+2+3*1]=ber
-2*b
;
242 /* skip to next line */
248 /* redither the 1st line to distribute error better */
254 for (x
=0; x
<image
->width
; x
++, ptr
+=channels
) {
257 pixel
= *ptr
+ err
[x
];
258 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
264 pixel
= *(ptr
+1) + err
[x
+1];
265 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
271 pixel
= *(ptr
+2) + err
[x
+2];
272 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
278 pixel
= (r
<<roffs
) | (g
<<goffs
) | (b
<<boffs
);
279 XPutPixel(ximg
->image
, x
, y
, pixel
);
281 /* distribute error */
295 nerr
[x
+1+3*1]=ger
-2*g
;
296 nerr
[x
+2+3*1]=ber
-2*b
;
304 image2TrueColor(RContext
*ctx
, RImage
*image
)
307 unsigned short rmask
, gmask
, bmask
;
308 unsigned short roffs
, goffs
, boffs
;
309 unsigned short *rtable
, *gtable
, *btable
;
310 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
312 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
317 roffs
= ctx
->red_offset
;
318 goffs
= ctx
->green_offset
;
319 boffs
= ctx
->blue_offset
;
321 rmask
= ctx
->visual
->red_mask
>> roffs
;
322 gmask
= ctx
->visual
->green_mask
>> goffs
;
323 bmask
= ctx
->visual
->blue_mask
>> boffs
;
325 rtable
= computeTable(rmask
);
326 gtable
= computeTable(gmask
);
327 btable
= computeTable(bmask
);
329 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
330 RErrorCode
= RERR_NOMEMORY
;
331 RDestroyXImage(ctx
, ximg
);
340 if (ctx
->attribs
->render_mode
==RBestMatchRendering
) {
344 unsigned char *ptr
= image
->data
;
348 puts("true color match");
350 if (rmask
==0xff && gmask
==0xff && bmask
==0xff) {
351 for (y
=0; y
< image
->height
; y
++) {
352 for (x
=0; x
< image
->width
; x
++, ptr
+=channels
) {
354 pixel
= (*(ptr
)<<roffs
) | (*(ptr
+1)<<goffs
) | (*(ptr
+2)<<boffs
);
355 XPutPixel(ximg
->image
, x
, y
, pixel
);
359 for (y
=0, ofs
=0; y
< image
->height
; y
++) {
360 for (x
=0; x
< image
->width
; x
++, ofs
+=channels
-3) {
362 r
= rtable
[ptr
[ofs
++]];
363 g
= gtable
[ptr
[ofs
++]];
364 b
= btable
[ptr
[ofs
++]];
365 pixel
= (r
<<roffs
) | (g
<<goffs
) | (b
<<boffs
);
366 XPutPixel(ximg
->image
, x
, y
, pixel
);
372 const int dr
=0xff/rmask
;
373 const int dg
=0xff/gmask
;
374 const int db
=0xff/bmask
;
377 puts("true color dither");
381 if (ctx
->depth
==16 && HAS_ALPHA(image
) && x86_check_mmx()) {
385 err
= malloc(8*(image
->width
+3));
386 nerr
= malloc(8*(image
->width
+3));
390 RErrorCode
= RERR_NOMEMORY
;
391 RDestroyXImage(ctx
, ximg
);
394 memset(err
, 0, 8*(image
->width
+3));
395 memset(nerr
, 0, 8*(image
->width
+3));
397 x86_mmx_TrueColor_32_to_16(image
->data
,
398 (unsigned short*)ximg
->image
->data
,
400 rtable
, gtable
, btable
,
403 image
->width
, image
->height
,
404 ximg
->image
->bytes_per_line
- 2*image
->width
);
409 #endif /* ASM_X86_MMX */
413 int ch
= (HAS_ALPHA(image
) ? 4 : 3);
415 err
= malloc(ch
*(image
->width
+2));
416 nerr
= malloc(ch
*(image
->width
+2));
420 RErrorCode
= RERR_NOMEMORY
;
421 RDestroyXImage(ctx
, ximg
);
425 memset(err
, 0, ch
*(image
->width
+2));
426 memset(nerr
, 0, ch
*(image
->width
+2));
428 convertTrueColor_generic(ximg
, image
, err
, nerr
,
429 rtable
, gtable
, btable
,
430 dr
, dg
, db
, roffs
, goffs
, boffs
);
445 /***************************************************************************/
448 convertPseudoColor_to_8(RXImage
*ximg
, RImage
*image
,
449 signed char *err
, signed char *nerr
,
453 const int dr
, const int dg
, const int db
,
454 unsigned long *pixels
,
461 unsigned char *ptr
= image
->data
;
462 unsigned char *optr
= ximg
->image
->data
;
463 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
466 /* convert and dither the image to XImage */
467 for (y
=0; y
<image
->height
; y
++) {
471 for (x
=0; x
<image
->width
*3; x
+=3, ptr
+=channels
) {
474 pixel
= *ptr
+ err
[x
];
475 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
481 pixel
= *(ptr
+1) + err
[x
+1];
482 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
488 pixel
= *(ptr
+2) + err
[x
+2];
489 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
494 *optr
++ = pixels
[r
*cpcpc
+ g
*cpc
+ b
];
496 /* distribute error */
511 nerr
[x
+1+3*1]=ger
-2*g
;
512 nerr
[x
+2+3*1]=ber
-2*b
;
514 /* skip to next line */
519 optr
+= ximg
->image
->bytes_per_line
- image
->width
;
526 image2PseudoColor(RContext
*ctx
, RImage
*image
)
529 register int x
, y
, r
, g
, b
;
532 const int cpc
=ctx
->attribs
->colors_per_channel
;
533 const unsigned short rmask
= cpc
-1; /* different sizes could be used */
534 const unsigned short gmask
= rmask
; /* for r,g,b */
535 const unsigned short bmask
= rmask
;
536 unsigned short *rtable
, *gtable
, *btable
;
537 const int cpccpc
= cpc
*cpc
;
538 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
540 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
547 /* Tables are same at the moment because rmask==gmask==bmask. */
548 rtable
= computeTable(rmask
);
549 gtable
= computeTable(gmask
);
550 btable
= computeTable(bmask
);
552 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
553 RErrorCode
= RERR_NOMEMORY
;
554 RDestroyXImage(ctx
, ximg
);
558 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
561 printf("pseudo color match with %d colors per channel\n", cpc
);
563 for (y
=0; y
<image
->height
; y
++) {
564 for (x
=0; x
<image
->width
; x
++, ptr
+=channels
-3) {
569 pixel
= r
*cpccpc
+ g
*cpc
+ b
;
570 /*data[ofs] = ctx->colors[pixel].pixel;*/
571 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[pixel
].pixel
);
578 const int dr
=0xff/rmask
;
579 const int dg
=0xff/gmask
;
580 const int db
=0xff/bmask
;
584 printf("pseudo color dithering with %d colors per channel\n", cpc
);
586 err
= malloc(4*(image
->width
+3));
587 nerr
= malloc(4*(image
->width
+3));
591 RErrorCode
= RERR_NOMEMORY
;
592 RDestroyXImage(ctx
, ximg
);
595 memset(err
, 0, 4*(image
->width
+3));
596 memset(nerr
, 0, 4*(image
->width
+3));
600 x86_PseudoColor_32_to_8(image
->data
, ximg
->image
->data
,
603 dr
, dg
, db
, ctx
->pixels
, cpc
,
604 image
->width
, image
->height
,
606 ximg
->image
->bytes_per_line
- image
->width
);
608 convertPseudoColor_to_8(ximg
, image
, err
+4, nerr
+4,
609 rtable
, gtable
, btable
,
610 dr
, dg
, db
, ctx
->pixels
, cpc
);
622 * For standard colormap
625 image2StandardPseudoColor(RContext
*ctx
, RImage
*image
)
628 register int x
, y
, r
, g
, b
;
632 unsigned int *rtable
, *gtable
, *btable
;
633 unsigned int base_pixel
= ctx
->std_rgb_map
->base_pixel
;
634 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
637 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
644 data
= (unsigned char *)ximg
->image
->data
;
647 rtable
= computeStdTable(ctx
->std_rgb_map
->red_mult
,
648 ctx
->std_rgb_map
->red_max
);
650 gtable
= computeStdTable(ctx
->std_rgb_map
->green_mult
,
651 ctx
->std_rgb_map
->green_max
);
653 btable
= computeStdTable(ctx
->std_rgb_map
->blue_mult
,
654 ctx
->std_rgb_map
->blue_max
);
656 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
657 RErrorCode
= RERR_NOMEMORY
;
658 RDestroyXImage(ctx
, ximg
);
663 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
664 for (y
=0; y
<image
->height
; y
++) {
665 for (x
=0; x
<image
->width
; x
++, ptr
+=channels
) {
667 pixel
= (rtable
[*ptr
] + gtable
[*(ptr
+1)]
668 + btable
[*(ptr
+2)] + base_pixel
) & 0xffffffff;
670 XPutPixel(ximg
->image
, x
, y
, pixel
);
675 signed short *err
, *nerr
;
681 printf("pseudo color dithering with %d colors per channel\n",
682 ctx
->attribs
->colors_per_channel
);
684 err
= (short*)malloc(3*(image
->width
+2)*sizeof(short));
685 nerr
= (short*)malloc(3*(image
->width
+2)*sizeof(short));
689 RErrorCode
= RERR_NOMEMORY
;
690 RDestroyXImage(ctx
, ximg
);
693 for (x
=0, x1
=0; x
<image
->width
*3; x1
+=channels
-3) {
694 err
[x
++] = ptr
[x1
++];
695 err
[x
++] = ptr
[x1
++];
696 err
[x
++] = ptr
[x1
++];
698 err
[x
] = err
[x
+1] = err
[x
+2] = 0;
699 /* convert and dither the image to XImage */
700 for (y
=0, ofs
=0; y
<image
->height
; y
++) {
701 if (y
<image
->height
-1) {
703 for (x
=0, x1
=(y
+1)*image
->width
*channels
;
706 nerr
[x
++] = ptr
[x1
++];
707 nerr
[x
++] = ptr
[x1
++];
708 nerr
[x
++] = ptr
[x1
++];
712 nerr
[x
++] = ptr
[x1
++];
713 nerr
[x
++] = ptr
[x1
++];
714 nerr
[x
++] = ptr
[x1
++];
716 for (x
=0; x
<image
->width
*3; x
+=3, ofs
++) {
718 if (err
[x
]>0xff) err
[x
]=0xff; else if (err
[x
]<0) err
[x
]=0;
719 if (err
[x
+1]>0xff) err
[x
+1]=0xff; else if (err
[x
+1]<0) err
[x
+1]=0;
720 if (err
[x
+2]>0xff) err
[x
+2]=0xff; else if (err
[x
+2]<0) err
[x
+2]=0;
723 g
= gtable
[err
[x
+1]];
724 b
= btable
[err
[x
+2]];
728 data
[ofs
] = base_pixel
+ pixel
;
731 rer
= err
[x
] - (ctx
->colors
[pixel
].red
>>8);
732 ger
= err
[x
+1] - (ctx
->colors
[pixel
].green
>>8);
733 ber
= err
[x
+2] - (ctx
->colors
[pixel
].blue
>>8);
735 /* distribute error */
736 err
[x
+3*1]+=(rer
*7)/16;
737 err
[x
+1+3*1]+=(ger
*7)/16;
738 err
[x
+2+3*1]+=(ber
*7)/16;
741 nerr
[x
+1]+=(ger
*5)/16;
742 nerr
[x
+2]+=(ber
*5)/16;
745 nerr
[x
-3*1]+=(rer
*3)/16;
746 nerr
[x
-3*1+1]+=(ger
*3)/16;
747 nerr
[x
-3*1+2]+=(ber
*3)/16;
751 nerr
[x
+1+3*1]+=ger
/16;
752 nerr
[x
+2+3*1]+=ber
/16;
754 /* skip to next line */
759 ofs
+= ximg
->image
->bytes_per_line
- image
->width
;
764 ximg
->image
->data
= (char*)data
;
772 image2GrayScale(RContext
*ctx
, RImage
*image
)
775 register int x
, y
, g
;
777 const int cpc
=ctx
->attribs
->colors_per_channel
;
778 unsigned short gmask
;
779 unsigned short *table
;
781 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
784 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
791 data
= (unsigned char *)ximg
->image
->data
;
793 if (ctx
->vclass
== StaticGray
)
794 gmask
= (1<<ctx
->depth
) - 1; /* use all grays */
796 gmask
= cpc
*cpc
*cpc
-1;
798 table
= computeTable(gmask
);
801 RErrorCode
= RERR_NOMEMORY
;
802 RDestroyXImage(ctx
, ximg
);
806 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
809 printf("grayscale match with %d colors per channel\n", cpc
);
811 for (y
=0; y
<image
->height
; y
++) {
812 for (x
=0; x
<image
->width
; x
++) {
814 g
= table
[(*ptr
* 30 + *(ptr
+1) * 59 + *(ptr
+2) * 11)/100];
816 /*data[ofs] = ctx->colors[g].pixel;*/
817 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[g
].pixel
);
826 const int dg
=0xff/gmask
;
829 printf("grayscale dither with %d colors per channel\n", cpc
);
831 gerr
= (short*)malloc((image
->width
+2)*sizeof(short));
832 ngerr
= (short*)malloc((image
->width
+2)*sizeof(short));
833 if (!gerr
|| !ngerr
) {
836 RErrorCode
= RERR_NOMEMORY
;
837 RDestroyXImage(ctx
, ximg
);
840 for (x
=0, y
=0; x
<image
->width
; x
++, y
+=channels
) {
841 gerr
[x
] = (ptr
[y
]*30 + ptr
[y
+1]*59 + ptr
[y
+2]*11)/100;
844 /* convert and dither the image to XImage */
845 for (y
=0; y
<image
->height
; y
++) {
846 if (y
<image
->height
-1) {
848 for (x
=0, x1
=(y
+1)*image
->width
*channels
; x
<image
->width
; x
++, x1
+=channels
) {
849 ngerr
[x
] = (ptr
[x1
]*30 + ptr
[x1
+1]*59 + ptr
[x1
+2]*11)/100;
853 ngerr
[x
] = (ptr
[x1
]*30 + ptr
[x1
+1]*59 + ptr
[x1
+2]*11)/100;
855 for (x
=0; x
<image
->width
; x
++) {
857 if (gerr
[x
]>0xff) gerr
[x
]=0xff; else if (gerr
[x
]<0) gerr
[x
]=0;
861 /*data[ofs] = ctx->colors[g].pixel;*/
862 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[g
].pixel
);
864 ger
= gerr
[x
] - g
*dg
;
866 /* distribute error */
875 /* skip to next line */
883 ximg
->image
->data
= (char*)data
;
890 image2Bitmap(RContext
*ctx
, RImage
*image
, int threshold
)
893 unsigned char *alpha
;
896 ximg
= RCreateXImage(ctx
, 1, image
->width
, image
->height
);
900 alpha
= image
->data
+3;
902 for (y
= 0; y
< image
->height
; y
++) {
903 for (x
= 0; x
< image
->width
; x
++) {
904 XPutPixel(ximg
->image
, x
, y
, (*alpha
<= threshold
? 0 : 1));
914 RConvertImage(RContext
*context
, RImage
*image
, Pixmap
*pixmap
)
921 assert(context
!=NULL
);
923 assert(pixmap
!=NULL
);
925 switch (context
->vclass
) {
930 ximg
= image2TrueColor(context
, image
);
941 if (context
->attribs
->standard_colormap_mode
!= RIgnoreStdColormap
)
942 ximg
= image2StandardPseudoColor(context
, image
);
944 ximg
= image2PseudoColor(context
, image
);
952 ximg
= image2GrayScale(context
, image
);
961 *pixmap
= XCreatePixmap(context
->dpy
, context
->drawable
, image
->width
,
962 image
->height
, context
->depth
);
965 if (context
->flags
.use_shared_pixmap
&& ximg
->is_shared
)
966 tmp
= R_CreateXImageMappedPixmap(context
, ximg
);
971 * We have to copy the shm Pixmap into a normal Pixmap because
972 * otherwise, we would have to control when Pixmaps are freed so
973 * that we can detach their shm segments. This is a problem if the
974 * program crash, leaving stale shared memory segments in the
975 * system (lots of them). But with some work, we can optimize
976 * things and remove this XCopyArea. This will require
977 * explicitly freeing all pixmaps when exiting or restarting
980 XCopyArea(context
->dpy
, tmp
, *pixmap
, context
->copy_gc
, 0, 0,
981 image
->width
, image
->height
, 0, 0);
982 XFreePixmap(context
->dpy
, tmp
);
984 RPutXImage(context
, *pixmap
, context
->copy_gc
, ximg
, 0, 0, 0, 0,
985 image
->width
, image
->height
);
988 RPutXImage(context
, *pixmap
, context
->copy_gc
, ximg
, 0, 0, 0, 0,
989 image
->width
, image
->height
);
992 RDestroyXImage(context
, ximg
);
998 /* make the gc permanent (create with context creation).
999 * GC creation is very expensive. altering its properties is not. -Dan
1002 RConvertImageMask(RContext
*context
, RImage
*image
, Pixmap
*pixmap
,
1003 Pixmap
*mask
, int threshold
)
1009 assert(context
!=NULL
);
1010 assert(image
!=NULL
);
1011 assert(pixmap
!=NULL
);
1014 if (!RConvertImage(context
, image
, pixmap
))
1017 if (image
->format
==RRGBFormat
) {
1022 ximg
= image2Bitmap(context
, image
, threshold
);
1027 *mask
= XCreatePixmap(context
->dpy
, context
->drawable
, image
->width
,
1029 gcv
.foreground
= context
->black
;
1030 gcv
.background
= context
->white
;
1031 gcv
.graphics_exposures
= False
;
1032 gc
= XCreateGC(context
->dpy
, *mask
, GCForeground
|GCBackground
1033 |GCGraphicsExposures
, &gcv
);
1034 RPutXImage(context
, *mask
, gc
, ximg
, 0, 0, 0, 0,
1035 image
->width
, image
->height
);
1036 RDestroyXImage(context
, ximg
);
1037 XFreeGC(context
->dpy
, gc
);
1044 RGetClosestXColor(RContext
*context
, RColor
*color
, XColor
*retColor
)
1046 if (context
->vclass
== TrueColor
) {
1047 unsigned short rmask
, gmask
, bmask
;
1048 unsigned short roffs
, goffs
, boffs
;
1049 unsigned short *rtable
, *gtable
, *btable
;
1051 roffs
= context
->red_offset
;
1052 goffs
= context
->green_offset
;
1053 boffs
= context
->blue_offset
;
1055 rmask
= context
->visual
->red_mask
>> roffs
;
1056 gmask
= context
->visual
->green_mask
>> goffs
;
1057 bmask
= context
->visual
->blue_mask
>> boffs
;
1059 rtable
= computeTable(rmask
);
1060 gtable
= computeTable(gmask
);
1061 btable
= computeTable(bmask
);
1063 retColor
->pixel
= (rtable
[color
->red
]<<roffs
) |
1064 (gtable
[color
->green
]<<goffs
) | (btable
[color
->blue
]<<boffs
);
1066 retColor
->red
= color
->red
<< 8;
1067 retColor
->green
= color
->green
<< 8;
1068 retColor
->blue
= color
->blue
<< 8;
1069 retColor
->flags
= DoRed
|DoGreen
|DoBlue
;
1071 } else if (context
->vclass
== PseudoColor
1072 || context
->vclass
== StaticColor
) {
1074 if (context
->attribs
->standard_colormap_mode
!= RIgnoreStdColormap
) {
1075 unsigned int *rtable
, *gtable
, *btable
;
1077 rtable
= computeStdTable(context
->std_rgb_map
->red_mult
,
1078 context
->std_rgb_map
->red_max
);
1080 gtable
= computeStdTable(context
->std_rgb_map
->green_mult
,
1081 context
->std_rgb_map
->green_max
);
1083 btable
= computeStdTable(context
->std_rgb_map
->blue_mult
,
1084 context
->std_rgb_map
->blue_max
);
1086 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
1087 RErrorCode
= RERR_NOMEMORY
;
1091 retColor
->pixel
= (rtable
[color
->red
]
1092 + gtable
[color
->green
]
1093 + btable
[color
->blue
]
1094 + context
->std_rgb_map
->base_pixel
) & 0xffffffff;
1095 retColor
->red
= color
->red
<<8;
1096 retColor
->green
= color
->green
<<8;
1097 retColor
->blue
= color
->blue
<<8;
1098 retColor
->flags
= DoRed
|DoGreen
|DoBlue
;
1101 const int cpc
=context
->attribs
->colors_per_channel
;
1102 const unsigned short rmask
= cpc
-1; /* different sizes could be used */
1103 const unsigned short gmask
= rmask
; /* for r,g,b */
1104 const unsigned short bmask
= rmask
;
1105 unsigned short *rtable
, *gtable
, *btable
;
1106 const int cpccpc
= cpc
*cpc
;
1109 rtable
= computeTable(rmask
);
1110 gtable
= computeTable(gmask
);
1111 btable
= computeTable(bmask
);
1113 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
1114 RErrorCode
= RERR_NOMEMORY
;
1117 index
= rtable
[color
->red
]*cpccpc
+ gtable
[color
->green
]*cpc
1118 + btable
[color
->blue
];
1119 *retColor
= context
->colors
[index
];
1122 } else if (context
->vclass
== GrayScale
|| context
->vclass
== StaticGray
) {
1124 const int cpc
= context
->attribs
->colors_per_channel
;
1125 unsigned short gmask
;
1126 unsigned short *table
;
1129 if (context
->vclass
== StaticGray
)
1130 gmask
= (1<<context
->depth
) - 1; /* use all grays */
1132 gmask
= cpc
*cpc
*cpc
-1;
1134 table
= computeTable(gmask
);
1138 index
= table
[(color
->red
*30 + color
->green
*59 + color
->blue
*11)/100];
1140 *retColor
= context
->colors
[index
];
1142 RErrorCode
= RERR_INTERNAL
;