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.
25 #include <X11/Xutil.h>
39 extern Pixmap
R_CreateXImageMappedPixmap(RContext
*context
, RXImage
*ximage
);
46 extern int x86_check_mmx();
48 extern void x86_mmx_TrueColor_32_to_16(unsigned char *image
,
49 unsigned short *ximage
,
50 short *err
, short *nerr
,
51 short *rtable
, short *gtable
,
53 int dr
, int dg
, int db
,
57 int width
, int height
,
66 typedef struct RConversionTable
{
67 unsigned short table
[256];
70 struct RConversionTable
*next
;
74 typedef struct RStdConversionTable
{
75 unsigned int table
[256];
80 struct RStdConversionTable
*next
;
81 } RStdConversionTable
;
85 static RConversionTable
*conversionTable
= NULL
;
86 static RStdConversionTable
*stdConversionTable
= NULL
;
89 static unsigned short*
90 computeTable(unsigned short mask
)
92 RConversionTable
*tmp
= conversionTable
;
96 if (tmp
->index
== mask
)
104 tmp
= (RConversionTable
*)malloc(sizeof(RConversionTable
));
109 tmp
->table
[i
] = (i
*mask
+ 0x7f)/0xff;
112 tmp
->next
= conversionTable
;
113 conversionTable
= tmp
;
119 computeStdTable(unsigned int mult
, unsigned int max
)
121 RStdConversionTable
*tmp
= stdConversionTable
;
125 if (tmp
->mult
== mult
&& tmp
->max
== max
)
133 tmp
= (RStdConversionTable
*)malloc(sizeof(RStdConversionTable
));
137 for (i
=0; i
<256; i
++) {
138 tmp
->table
[i
] = (i
*max
)/0xff * mult
;
143 tmp
->next
= stdConversionTable
;
144 stdConversionTable
= tmp
;
149 /***************************************************************************/
153 convertTrueColor_generic(RXImage
*ximg
, RImage
*image
,
154 char *err
, char *nerr
,
158 const int dr
, const int dg
, const int db
,
159 const unsigned short roffs
,
160 const unsigned short goffs
,
161 const unsigned short boffs
)
167 unsigned char *ptr
= image
->data
;
168 int channels
= image
->format
== RRGBAFormat
? 4 : 3;
170 /* convert and dither the image to XImage */
171 for (y
=0; y
<image
->height
; y
++) {
175 for (x
=0; x
<image
->width
; x
++, ptr
+=channels
) {
178 pixel
= *ptr
+ err
[x
];
179 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
185 pixel
= *(ptr
+1) + err
[x
+1];
186 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
192 pixel
= *(ptr
+2) + err
[x
+2];
193 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
199 pixel
= (r
<<roffs
) | (g
<<goffs
) | (b
<<boffs
);
200 XPutPixel(ximg
->image
, x
, y
, pixel
);
202 /* distribute error */
216 nerr
[x
+1+3*1]=ger
-2*g
;
217 nerr
[x
+2+3*1]=ber
-2*b
;
219 /* skip to next line */
230 image2TrueColor(RContext
*ctx
, RImage
*image
)
233 unsigned short rmask
, gmask
, bmask
;
234 unsigned short roffs
, goffs
, boffs
;
235 unsigned short *rtable
, *gtable
, *btable
;
236 int channels
= (image
->format
== RRGBAFormat
? 4 : 3);
238 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
243 roffs
= ctx
->red_offset
;
244 goffs
= ctx
->green_offset
;
245 boffs
= ctx
->blue_offset
;
247 rmask
= ctx
->visual
->red_mask
>> roffs
;
248 gmask
= ctx
->visual
->green_mask
>> goffs
;
249 bmask
= ctx
->visual
->blue_mask
>> boffs
;
251 rtable
= computeTable(rmask
);
252 gtable
= computeTable(gmask
);
253 btable
= computeTable(bmask
);
255 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
256 RErrorCode
= RERR_NOMEMORY
;
257 RDestroyXImage(ctx
, ximg
);
262 if (ctx
->attribs
->render_mode
==RBestMatchRendering
) {
266 unsigned char *ptr
= image
->data
;
270 puts("true color match");
272 for (y
=0, ofs
=0; y
< image
->height
; y
++) {
273 for (x
=0; x
< image
->width
; x
++, ofs
+=channels
-3) {
275 r
= rtable
[ptr
[ofs
++]];
276 g
= gtable
[ptr
[ofs
++]];
277 b
= btable
[ptr
[ofs
++]];
278 pixel
= (r
<<roffs
) | (g
<<goffs
) | (b
<<boffs
);
279 XPutPixel(ximg
->image
, x
, y
, pixel
);
284 const int dr
=0xff/rmask
;
285 const int dg
=0xff/gmask
;
286 const int db
=0xff/bmask
;
289 puts("true color dither");
297 if (ctx
->depth
== 16 && image
->format
== RRGBAFormat
298 && x86_check_mmx()) {
302 err
= malloc(8*(image
->width
+3));
303 nerr
= malloc(8*(image
->width
+3));
307 RErrorCode
= RERR_NOMEMORY
;
308 RDestroyXImage(ctx
, ximg
);
311 memset(err
, 0, 8*(image
->width
+3));
312 memset(nerr
, 0, 8*(image
->width
+3));
314 x86_mmx_TrueColor_32_to_16(image
->data
,
315 (unsigned short*)ximg
->image
->data
,
317 rtable
, gtable
, btable
,
320 image
->width
, image
->height
,
321 ximg
->image
->bytes_per_line
- 2*image
->width
);
326 #endif /* ASM_X86_MMX */
330 int ch
= image
->format
== RRGBAFormat
? 4 : 3;
332 err
= malloc(ch
*(image
->width
+2));
333 nerr
= malloc(ch
*(image
->width
+2));
337 RErrorCode
= RERR_NOMEMORY
;
338 RDestroyXImage(ctx
, ximg
);
342 memset(err
, 0, ch
*(image
->width
+2));
343 memset(nerr
, 0, ch
*(image
->width
+2));
345 convertTrueColor_generic(ximg
, image
, err
, nerr
,
346 rtable
, gtable
, btable
,
347 dr
, dg
, db
, roffs
, goffs
, boffs
);
362 /***************************************************************************/
365 convertPseudoColor_to_8(RXImage
*ximg
, RImage
*image
,
366 char *err
, char *nerr
,
370 const int dr
, const int dg
, const int db
,
371 unsigned long *pixels
,
378 unsigned char *ptr
= image
->data
;
379 unsigned char *optr
= ximg
->image
->data
;
380 int channels
= image
->format
== RRGBAFormat
? 4 : 3;
383 /* convert and dither the image to XImage */
384 for (y
=0; y
<image
->height
; y
++) {
388 for (x
=0; x
<image
->width
*3; x
+=3, ptr
+=channels
) {
391 pixel
= *ptr
+ err
[x
];
392 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
398 pixel
= *(ptr
+1) + err
[x
+1];
399 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
405 pixel
= *(ptr
+2) + err
[x
+2];
406 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
411 *optr
++ = pixels
[r
*cpcpc
+ g
*cpc
+ b
];
413 /* distribute error */
427 nerr
[x
+1+3*1]=ger
-2*g
;
428 nerr
[x
+2+3*1]=ber
-2*b
;
430 /* skip to next line */
435 optr
+= ximg
->image
->bytes_per_line
- image
->width
;
442 image2PseudoColor(RContext
*ctx
, RImage
*image
)
445 register int x
, y
, r
, g
, b
;
448 const int cpc
=ctx
->attribs
->colors_per_channel
;
449 const unsigned short rmask
= cpc
-1; /* different sizes could be used */
450 const unsigned short gmask
= rmask
; /* for r,g,b */
451 const unsigned short bmask
= rmask
;
452 unsigned short *rtable
, *gtable
, *btable
;
453 const int cpccpc
= cpc
*cpc
;
454 int channels
= image
->format
== RRGBAFormat
? 4 : 3;
456 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
463 /* Tables are same at the moment because rmask==gmask==bmask. */
464 rtable
= computeTable(rmask
);
465 gtable
= computeTable(gmask
);
466 btable
= computeTable(bmask
);
468 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
469 RErrorCode
= RERR_NOMEMORY
;
470 RDestroyXImage(ctx
, ximg
);
474 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
477 printf("pseudo color match with %d colors per channel\n", cpc
);
479 for (y
=0; y
<image
->height
; y
++) {
480 for (x
=0; x
<image
->width
; x
++, ptr
+=channels
-1) {
485 pixel
= r
*cpccpc
+ g
*cpc
+ b
;
486 /*data[ofs] = ctx->colors[pixel].pixel;*/
487 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[pixel
].pixel
);
494 const int dr
=0xff/rmask
;
495 const int dg
=0xff/gmask
;
496 const int db
=0xff/bmask
;
500 printf("pseudo color dithering with %d colors per channel\n", cpc
);
502 err
= malloc(3*(image
->width
+2));
503 nerr
= malloc(3*(image
->width
+2));
507 RErrorCode
= RERR_NOMEMORY
;
508 RDestroyXImage(ctx
, ximg
);
511 memset(err
, 0, 3*(image
->width
+3));
512 memset(nerr
, 0, 3*(image
->width
+3));
514 convertPseudoColor_to_8(ximg
, image
, err
+4, nerr
+4,
515 rtable
, gtable
, btable
,
516 dr
, dg
, db
, ctx
->pixels
, cpc
);
527 * For standard colormap
530 image2StandardPseudoColor(RContext
*ctx
, RImage
*image
)
533 register int x
, y
, r
, g
, b
;
537 unsigned int *rtable
, *gtable
, *btable
;
538 unsigned int base_pixel
= ctx
->std_rgb_map
->base_pixel
;
539 int channels
= image
->format
== RRGBAFormat
? 4 : 3;
540 /*register unsigned char maxrgb = 0xff;*/
542 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
549 data
= (unsigned char *)ximg
->image
->data
;
552 rtable
= computeStdTable(ctx
->std_rgb_map
->red_mult
,
553 ctx
->std_rgb_map
->red_max
);
555 gtable
= computeStdTable(ctx
->std_rgb_map
->green_mult
,
556 ctx
->std_rgb_map
->green_max
);
558 btable
= computeStdTable(ctx
->std_rgb_map
->blue_mult
,
559 ctx
->std_rgb_map
->blue_max
);
561 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
562 RErrorCode
= RERR_NOMEMORY
;
563 RDestroyXImage(ctx
, ximg
);
568 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
569 for (y
=0; y
<image
->height
; y
++) {
570 for (x
=0; x
<image
->width
; x
++, ptr
+=channels
-3) {
572 pixel
= (rtable
[*ptr
++] + gtable
[*ptr
++]
573 + btable
[*ptr
++] + base_pixel
) & 0xffffffff;
575 XPutPixel(ximg
->image
, x
, y
, pixel
);
586 printf("pseudo color dithering with %d colors per channel\n", cpc
);
588 err
= (short*)malloc(3*(image
->width
+2)*sizeof(short));
589 nerr
= (short*)malloc(3*(image
->width
+2)*sizeof(short));
593 RErrorCode
= RERR_NOMEMORY
;
594 RDestroyXImage(ctx
, ximg
);
597 for (x
=0, x1
=0; x
<image
->width
*3; x1
+=channels
-3) {
598 err
[x
++] = ptr
[x1
++];
599 err
[x
++] = ptr
[x1
++];
600 err
[x
++] = ptr
[x1
++];
602 err
[x
++] = err
[x
++] = err
[x
++] = 0;
603 /* convert and dither the image to XImage */
604 for (y
=0, ofs
=0; y
<image
->height
; y
++) {
605 if (y
<image
->height
-1) {
607 for (x
=0, x1
=(y
+1)*image
->width
*channels
;
610 nerr
[x
++] = ptr
[x1
++];
611 nerr
[x
++] = ptr
[x1
++];
612 nerr
[x
++] = ptr
[x1
++];
616 nerr
[x
++] = ptr
[x1
++];
617 nerr
[x
++] = ptr
[x1
++];
618 nerr
[x
++] = ptr
[x1
++];
620 for (x
=0; x
<image
->width
*3; x
+=3, ofs
++) {
622 if (err
[x
]>0xff) err
[x
]=0xff; else if (err
[x
]<0) err
[x
]=0;
623 if (err
[x
+1]>0xff) err
[x
+1]=0xff; else if (err
[x
+1]<0) err
[x
+1]=0;
624 if (err
[x
+2]>0xff) err
[x
+2]=0xff; else if (err
[x
+2]<0) err
[x
+2]=0;
627 g
= gtable
[err
[x
+1]];
628 b
= btable
[err
[x
+2]];
632 data
[ofs
] = base_pixel
+ pixel
;
635 rer
= err
[x
] - (ctx
->colors
[pixel
].red
>>8);
636 ger
= err
[x
+1] - (ctx
->colors
[pixel
].green
>>8);
637 ber
= err
[x
+2] - (ctx
->colors
[pixel
].blue
>>8);
639 /* distribute error */
640 err
[x
+3*1]+=(rer
*7)/16;
641 err
[x
+1+3*1]+=(ger
*7)/16;
642 err
[x
+2+3*1]+=(ber
*7)/16;
645 nerr
[x
+1]+=(ger
*5)/16;
646 nerr
[x
+2]+=(ber
*5)/16;
649 nerr
[x
-3*1]+=(rer
*3)/16;
650 nerr
[x
-3*1+1]+=(ger
*3)/16;
651 nerr
[x
-3*1+2]+=(ber
*3)/16;
655 nerr
[x
+1+3*1]+=ger
/16;
656 nerr
[x
+2+3*1]+=ber
/16;
658 /* skip to next line */
663 ofs
+= ximg
->image
->bytes_per_line
- image
->width
;
668 ximg
->image
->data
= (char*)data
;
676 image2GrayScale(RContext
*ctx
, RImage
*image
)
679 register int x
, y
, g
;
681 const int cpc
=ctx
->attribs
->colors_per_channel
;
682 unsigned short gmask
;
683 unsigned short *table
;
685 int channels
= image
->format
== RRGBAFormat
? 4 : 3;
687 /*register unsigned char maxrgb = 0xff;*/
689 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
696 data
= (unsigned char *)ximg
->image
->data
;
698 if (ctx
->vclass
== StaticGray
)
699 gmask
= (1<<ctx
->depth
) - 1; /* use all grays */
701 gmask
= cpc
*cpc
*cpc
-1;
703 table
= computeTable(gmask
);
706 RErrorCode
= RERR_NOMEMORY
;
707 RDestroyXImage(ctx
, ximg
);
711 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
714 printf("grayscale match with %d colors per channel\n", cpc
);
716 for (y
=0; y
<image
->height
; y
++) {
717 for (x
=0; x
<image
->width
; x
++) {
719 g
= table
[(*ptr
++ * 30 + *ptr
++ * 59 + *ptr
++ * 11)/100];
721 /*data[ofs] = ctx->colors[g].pixel;*/
722 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[g
].pixel
);
731 const int dg
=0xff/gmask
;
734 printf("grayscale dither with %d colors per channel\n", cpc
);
736 gerr
= (short*)malloc((image
->width
+2)*sizeof(short));
737 ngerr
= (short*)malloc((image
->width
+2)*sizeof(short));
738 if (!gerr
|| !ngerr
) {
741 RErrorCode
= RERR_NOMEMORY
;
742 RDestroyXImage(ctx
, ximg
);
745 for (x
=0; x
<image
->width
; x
++) {
746 gerr
[x
] = (ptr
[x
*3]*30 + ptr
[x
*3+1]*59 + ptr
[x
*3+2]*11)/100;
749 /* convert and dither the image to XImage */
750 for (y
=0; y
<image
->height
; y
++) {
751 if (y
<image
->height
-1) {
753 for (x
=0, x1
=(y
+1)*image
->width
*3;
756 ngerr
[x
] = (ptr
[x1
++]*30 + ptr
[x1
++]*59 + ptr
[x1
++]*11)/100;
760 ngerr
[x
] = (ptr
[x1
++]*30 + ptr
[x1
++]*59 + ptr
[x1
++]*11)/100;
762 for (x
=0; x
<image
->width
; x
++) {
764 if (gerr
[x
]>0xff) gerr
[x
]=0xff; else if (gerr
[x
]<0) gerr
[x
]=0;
768 /*data[ofs] = ctx->colors[g].pixel;*/
769 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[g
].pixel
);
771 ger
= gerr
[x
] - g
*dg
;
773 /* distribute error */
782 /* skip to next line */
790 ximg
->image
->data
= (char*)data
;
797 image2Bitmap(RContext
*ctx
, RImage
*image
, int threshold
)
800 unsigned char *alpha
;
803 ximg
= RCreateXImage(ctx
, 1, image
->width
, image
->height
);
807 alpha
= image
->data
+3;
809 for (y
= 0; y
< image
->height
; y
++) {
810 for (x
= 0; x
< image
->width
; x
++) {
811 XPutPixel(ximg
->image
, x
, y
, (*alpha
<= threshold
? 0 : 1));
822 RConvertImage(RContext
*context
, RImage
*image
, Pixmap
*pixmap
)
829 assert(context
!=NULL
);
831 assert(pixmap
!=NULL
);
833 /* clear error message */
834 if (context
->vclass
== TrueColor
) {
836 ximg
= image2TrueColor(context
, image
);
838 } else if (context
->vclass
== PseudoColor
839 || context
->vclass
== StaticColor
) {
844 if (context
->attribs
->standard_colormap_mode
!= RIgnoreStdColormap
)
845 ximg
= image2StandardPseudoColor(context
, image
);
847 ximg
= image2PseudoColor(context
, image
);
851 } else if (context
->vclass
== GrayScale
|| context
->vclass
== StaticGray
) {
853 ximg
= image2GrayScale(context
, image
);
861 *pixmap
= XCreatePixmap(context
->dpy
, context
->drawable
, image
->width
,
862 image
->height
, context
->depth
);
865 if (context
->flags
.use_shared_pixmap
&& ximg
->is_shared
)
866 tmp
= R_CreateXImageMappedPixmap(context
, ximg
);
871 * We have to copy the shm Pixmap into a normal Pixmap because
872 * otherwise, we would have to control when Pixmaps are freed so
873 * that we can detach their shm segments. This is a problem if the
874 * program crash, leaving stale shared memory segments in the
875 * system (lots of them). But with some work, we can optimize
876 * things and remove this XCopyArea. This will require
877 * explicitly freeing all pixmaps when exiting or restarting
880 XCopyArea(context
->dpy
, tmp
, *pixmap
, context
->copy_gc
, 0, 0,
881 image
->width
, image
->height
, 0, 0);
882 XFreePixmap(context
->dpy
, tmp
);
884 RPutXImage(context
, *pixmap
, context
->copy_gc
, ximg
, 0, 0, 0, 0,
885 image
->width
, image
->height
);
888 RPutXImage(context
, *pixmap
, context
->copy_gc
, ximg
, 0, 0, 0, 0,
889 image
->width
, image
->height
);
892 RDestroyXImage(context
, ximg
);
899 RConvertImageMask(RContext
*context
, RImage
*image
, Pixmap
*pixmap
,
900 Pixmap
*mask
, int threshold
)
906 assert(context
!=NULL
);
908 assert(pixmap
!=NULL
);
911 if (!RConvertImage(context
, image
, pixmap
))
914 if (image
->format
==RRGBFormat
) {
919 ximg
= image2Bitmap(context
, image
, threshold
);
924 *mask
= XCreatePixmap(context
->dpy
, context
->drawable
, image
->width
,
926 gcv
.foreground
= context
->black
;
927 gcv
.background
= context
->white
;
928 gcv
.graphics_exposures
= False
;
929 gc
= XCreateGC(context
->dpy
, *mask
, GCForeground
|GCBackground
930 |GCGraphicsExposures
, &gcv
);
931 RPutXImage(context
, *mask
, gc
, ximg
, 0, 0, 0, 0,
932 image
->width
, image
->height
);
933 RDestroyXImage(context
, ximg
);
940 RGetClosestXColor(RContext
*context
, RColor
*color
, XColor
*retColor
)
942 if (context
->vclass
== TrueColor
) {
943 unsigned short rmask
, gmask
, bmask
;
944 unsigned short roffs
, goffs
, boffs
;
945 unsigned short *rtable
, *gtable
, *btable
;
947 roffs
= context
->red_offset
;
948 goffs
= context
->green_offset
;
949 boffs
= context
->blue_offset
;
951 rmask
= context
->visual
->red_mask
>> roffs
;
952 gmask
= context
->visual
->green_mask
>> goffs
;
953 bmask
= context
->visual
->blue_mask
>> boffs
;
955 rtable
= computeTable(rmask
);
956 gtable
= computeTable(gmask
);
957 btable
= computeTable(bmask
);
959 retColor
->pixel
= (rtable
[color
->red
]<<roffs
) |
960 (gtable
[color
->green
]<<goffs
) | (btable
[color
->blue
]<<boffs
);
962 retColor
->red
= color
->red
<< 8;
963 retColor
->green
= color
->green
<< 8;
964 retColor
->blue
= color
->blue
<< 8;
965 retColor
->flags
= DoRed
|DoGreen
|DoBlue
;
967 } else if (context
->vclass
== PseudoColor
968 || context
->vclass
== StaticColor
) {
970 if (context
->attribs
->standard_colormap_mode
!= RIgnoreStdColormap
) {
971 unsigned int *rtable
, *gtable
, *btable
;
973 rtable
= computeStdTable(context
->std_rgb_map
->red_mult
,
974 context
->std_rgb_map
->red_max
);
976 gtable
= computeStdTable(context
->std_rgb_map
->green_mult
,
977 context
->std_rgb_map
->green_max
);
979 btable
= computeStdTable(context
->std_rgb_map
->blue_mult
,
980 context
->std_rgb_map
->blue_max
);
982 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
983 RErrorCode
= RERR_NOMEMORY
;
987 retColor
->pixel
= (rtable
[color
->red
]
988 + gtable
[color
->green
]
989 + btable
[color
->blue
]
990 + context
->std_rgb_map
->base_pixel
) & 0xffffffff;
991 retColor
->red
= color
->red
<<8;
992 retColor
->green
= color
->green
<<8;
993 retColor
->blue
= color
->blue
<<8;
994 retColor
->flags
= DoRed
|DoGreen
|DoBlue
;
997 const int cpc
=context
->attribs
->colors_per_channel
;
998 const unsigned short rmask
= cpc
-1; /* different sizes could be used */
999 const unsigned short gmask
= rmask
; /* for r,g,b */
1000 const unsigned short bmask
= rmask
;
1001 unsigned short *rtable
, *gtable
, *btable
;
1002 const int cpccpc
= cpc
*cpc
;
1005 rtable
= computeTable(rmask
);
1006 gtable
= computeTable(gmask
);
1007 btable
= computeTable(bmask
);
1009 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
1010 RErrorCode
= RERR_NOMEMORY
;
1013 index
= rtable
[color
->red
]*cpccpc
+ gtable
[color
->green
]*cpc
1014 + btable
[color
->blue
];
1015 *retColor
= context
->colors
[index
];
1018 } else if (context
->vclass
== GrayScale
|| context
->vclass
== StaticGray
) {
1020 const int cpc
= context
->attribs
->colors_per_channel
;
1021 unsigned short gmask
;
1022 unsigned short *table
;
1025 if (context
->vclass
== StaticGray
)
1026 gmask
= (1<<context
->depth
) - 1; /* use all grays */
1028 gmask
= cpc
*cpc
*cpc
-1;
1030 table
= computeTable(gmask
);
1034 index
= table
[(color
->red
*30 + color
->green
*59 + color
->blue
*11)/100];
1036 *retColor
= context
->colors
[index
];
1038 RErrorCode
= RERR_INTERNAL
;