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
);
45 extern void x86_PseudoColor_32_to_8(unsigned char *image
,
46 unsigned char *ximage
,
47 char *err
, char *nerr
,
49 int dr
, int dg
, int db
,
50 unsigned long *pixels
,
52 int width
, int height
,
59 extern int x86_check_mmx();
61 extern void x86_mmx_TrueColor_32_to_16(unsigned char *image
,
62 unsigned short *ximage
,
63 short *err
, short *nerr
,
64 short *rtable
, short *gtable
,
66 int dr
, int dg
, int db
,
70 int width
, int height
,
75 #endif /* ASM_X86_MMX */
79 typedef struct RConversionTable
{
80 unsigned short table
[256];
83 struct RConversionTable
*next
;
87 typedef struct RStdConversionTable
{
88 unsigned int table
[256];
93 struct RStdConversionTable
*next
;
94 } RStdConversionTable
;
98 static RConversionTable
*conversionTable
= NULL
;
99 static RStdConversionTable
*stdConversionTable
= NULL
;
102 static unsigned short*
103 computeTable(unsigned short mask
)
105 RConversionTable
*tmp
= conversionTable
;
109 if (tmp
->index
== mask
)
117 tmp
= (RConversionTable
*)malloc(sizeof(RConversionTable
));
122 tmp
->table
[i
] = (i
*mask
+ 0x7f)/0xff;
125 tmp
->next
= conversionTable
;
126 conversionTable
= tmp
;
132 computeStdTable(unsigned int mult
, unsigned int max
)
134 RStdConversionTable
*tmp
= stdConversionTable
;
138 if (tmp
->mult
== mult
&& tmp
->max
== max
)
146 tmp
= (RStdConversionTable
*)malloc(sizeof(RStdConversionTable
));
150 for (i
=0; i
<256; i
++) {
151 tmp
->table
[i
] = (i
*max
)/0xff * mult
;
156 tmp
->next
= stdConversionTable
;
157 stdConversionTable
= tmp
;
162 /***************************************************************************/
166 convertTrueColor_generic(RXImage
*ximg
, RImage
*image
,
167 char *err
, char *nerr
,
171 const int dr
, const int dg
, const int db
,
172 const unsigned short roffs
,
173 const unsigned short goffs
,
174 const unsigned short boffs
)
180 unsigned char *ptr
= image
->data
;
181 int channels
= image
->format
== RRGBAFormat
? 4 : 3;
183 /* convert and dither the image to XImage */
184 for (y
=0; y
<image
->height
; y
++) {
188 for (x
=0; x
<image
->width
; x
++, ptr
+=channels
) {
191 pixel
= *ptr
+ err
[x
];
192 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
198 pixel
= *(ptr
+1) + err
[x
+1];
199 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
205 pixel
= *(ptr
+2) + err
[x
+2];
206 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
212 pixel
= (r
<<roffs
) | (g
<<goffs
) | (b
<<boffs
);
213 XPutPixel(ximg
->image
, x
, y
, pixel
);
215 /* distribute error */
229 nerr
[x
+1+3*1]=ger
-2*g
;
230 nerr
[x
+2+3*1]=ber
-2*b
;
232 /* skip to next line */
243 image2TrueColor(RContext
*ctx
, RImage
*image
)
246 unsigned short rmask
, gmask
, bmask
;
247 unsigned short roffs
, goffs
, boffs
;
248 unsigned short *rtable
, *gtable
, *btable
;
249 int channels
= (image
->format
== RRGBAFormat
? 4 : 3);
251 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
256 roffs
= ctx
->red_offset
;
257 goffs
= ctx
->green_offset
;
258 boffs
= ctx
->blue_offset
;
260 rmask
= ctx
->visual
->red_mask
>> roffs
;
261 gmask
= ctx
->visual
->green_mask
>> goffs
;
262 bmask
= ctx
->visual
->blue_mask
>> boffs
;
264 rtable
= computeTable(rmask
);
265 gtable
= computeTable(gmask
);
266 btable
= computeTable(bmask
);
268 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
269 RErrorCode
= RERR_NOMEMORY
;
270 RDestroyXImage(ctx
, ximg
);
279 if (ctx
->attribs
->render_mode
==RBestMatchRendering
) {
283 unsigned char *ptr
= image
->data
;
287 puts("true color match");
289 for (y
=0, ofs
=0; y
< image
->height
; y
++) {
290 for (x
=0; x
< image
->width
; x
++, ofs
+=channels
-3) {
292 r
= rtable
[ptr
[ofs
++]];
293 g
= gtable
[ptr
[ofs
++]];
294 b
= btable
[ptr
[ofs
++]];
295 pixel
= (r
<<roffs
) | (g
<<goffs
) | (b
<<boffs
);
296 XPutPixel(ximg
->image
, x
, y
, pixel
);
301 const int dr
=0xff/rmask
;
302 const int dg
=0xff/gmask
;
303 const int db
=0xff/bmask
;
306 puts("true color dither");
310 if (ctx
->depth
== 16 && image
->format
== RRGBAFormat
311 && x86_check_mmx()) {
315 err
= malloc(8*(image
->width
+3));
316 nerr
= malloc(8*(image
->width
+3));
320 RErrorCode
= RERR_NOMEMORY
;
321 RDestroyXImage(ctx
, ximg
);
324 memset(err
, 0, 8*(image
->width
+3));
325 memset(nerr
, 0, 8*(image
->width
+3));
327 x86_mmx_TrueColor_32_to_16(image
->data
,
328 (unsigned short*)ximg
->image
->data
,
330 rtable
, gtable
, btable
,
333 image
->width
, image
->height
,
334 ximg
->image
->bytes_per_line
- 2*image
->width
);
339 #endif /* ASM_X86_MMX */
343 int ch
= image
->format
== RRGBAFormat
? 4 : 3;
345 err
= malloc(ch
*(image
->width
+2));
346 nerr
= malloc(ch
*(image
->width
+2));
350 RErrorCode
= RERR_NOMEMORY
;
351 RDestroyXImage(ctx
, ximg
);
355 memset(err
, 0, ch
*(image
->width
+2));
356 memset(nerr
, 0, ch
*(image
->width
+2));
358 convertTrueColor_generic(ximg
, image
, err
, nerr
,
359 rtable
, gtable
, btable
,
360 dr
, dg
, db
, roffs
, goffs
, boffs
);
375 /***************************************************************************/
378 convertPseudoColor_to_8(RXImage
*ximg
, RImage
*image
,
379 char *err
, char *nerr
,
383 const int dr
, const int dg
, const int db
,
384 unsigned long *pixels
,
391 unsigned char *ptr
= image
->data
;
392 unsigned char *optr
= ximg
->image
->data
;
393 int channels
= image
->format
== RRGBAFormat
? 4 : 3;
396 /* convert and dither the image to XImage */
397 for (y
=0; y
<image
->height
; y
++) {
401 for (x
=0; x
<image
->width
*3; x
+=3, ptr
+=channels
) {
404 pixel
= *ptr
+ err
[x
];
405 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
411 pixel
= *(ptr
+1) + err
[x
+1];
412 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
418 pixel
= *(ptr
+2) + err
[x
+2];
419 if (pixel
<0) pixel
=0; else if (pixel
>0xff) pixel
=0xff;
424 *optr
++ = pixels
[r
*cpcpc
+ g
*cpc
+ b
];
426 /* distribute error */
441 nerr
[x
+1+3*1]=ger
-2*g
;
442 nerr
[x
+2+3*1]=ber
-2*b
;
444 /* skip to next line */
449 optr
+= ximg
->image
->bytes_per_line
- image
->width
;
456 image2PseudoColor(RContext
*ctx
, RImage
*image
)
459 register int x
, y
, r
, g
, b
;
462 const int cpc
=ctx
->attribs
->colors_per_channel
;
463 const unsigned short rmask
= cpc
-1; /* different sizes could be used */
464 const unsigned short gmask
= rmask
; /* for r,g,b */
465 const unsigned short bmask
= rmask
;
466 unsigned short *rtable
, *gtable
, *btable
;
467 const int cpccpc
= cpc
*cpc
;
468 int channels
= image
->format
== RRGBAFormat
? 4 : 3;
470 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
477 /* Tables are same at the moment because rmask==gmask==bmask. */
478 rtable
= computeTable(rmask
);
479 gtable
= computeTable(gmask
);
480 btable
= computeTable(bmask
);
482 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
483 RErrorCode
= RERR_NOMEMORY
;
484 RDestroyXImage(ctx
, ximg
);
488 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
491 printf("pseudo color match with %d colors per channel\n", cpc
);
493 for (y
=0; y
<image
->height
; y
++) {
494 for (x
=0; x
<image
->width
; x
++, ptr
+=channels
-1) {
499 pixel
= r
*cpccpc
+ g
*cpc
+ b
;
500 /*data[ofs] = ctx->colors[pixel].pixel;*/
501 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[pixel
].pixel
);
508 const int dr
=0xff/rmask
;
509 const int dg
=0xff/gmask
;
510 const int db
=0xff/bmask
;
514 printf("pseudo color dithering with %d colors per channel\n", cpc
);
516 err
= malloc(4*(image
->width
+3));
517 nerr
= malloc(4*(image
->width
+3));
521 RErrorCode
= RERR_NOMEMORY
;
522 RDestroyXImage(ctx
, ximg
);
525 memset(err
, 0, 4*(image
->width
+3));
526 memset(nerr
, 0, 4*(image
->width
+3));
530 x86_PseudoColor_32_to_8(image
->data
, ximg
->image
->data
,
533 dr
, dg
, db
, ctx
->pixels
, cpc
,
534 image
->width
, image
->height
,
536 ximg
->image
->bytes_per_line
- image
->width
);
538 convertPseudoColor_to_8(ximg
, image
, err
+4, nerr
+4,
539 rtable
, gtable
, btable
,
540 dr
, dg
, db
, ctx
->pixels
, cpc
);
552 * For standard colormap
555 image2StandardPseudoColor(RContext
*ctx
, RImage
*image
)
558 register int x
, y
, r
, g
, b
;
562 unsigned int *rtable
, *gtable
, *btable
;
563 unsigned int base_pixel
= ctx
->std_rgb_map
->base_pixel
;
564 int channels
= image
->format
== RRGBAFormat
? 4 : 3;
565 /*register unsigned char maxrgb = 0xff;*/
567 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
574 data
= (unsigned char *)ximg
->image
->data
;
577 rtable
= computeStdTable(ctx
->std_rgb_map
->red_mult
,
578 ctx
->std_rgb_map
->red_max
);
580 gtable
= computeStdTable(ctx
->std_rgb_map
->green_mult
,
581 ctx
->std_rgb_map
->green_max
);
583 btable
= computeStdTable(ctx
->std_rgb_map
->blue_mult
,
584 ctx
->std_rgb_map
->blue_max
);
586 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
587 RErrorCode
= RERR_NOMEMORY
;
588 RDestroyXImage(ctx
, ximg
);
593 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
594 for (y
=0; y
<image
->height
; y
++) {
595 for (x
=0; x
<image
->width
; x
++, ptr
+=channels
-3) {
597 pixel
= (rtable
[*ptr
++] + gtable
[*ptr
++]
598 + btable
[*ptr
++] + base_pixel
) & 0xffffffff;
600 XPutPixel(ximg
->image
, x
, y
, pixel
);
611 printf("pseudo color dithering with %d colors per channel\n", cpc
);
613 err
= (short*)malloc(3*(image
->width
+2)*sizeof(short));
614 nerr
= (short*)malloc(3*(image
->width
+2)*sizeof(short));
618 RErrorCode
= RERR_NOMEMORY
;
619 RDestroyXImage(ctx
, ximg
);
622 for (x
=0, x1
=0; x
<image
->width
*3; x1
+=channels
-3) {
623 err
[x
++] = ptr
[x1
++];
624 err
[x
++] = ptr
[x1
++];
625 err
[x
++] = ptr
[x1
++];
627 err
[x
++] = err
[x
++] = err
[x
++] = 0;
628 /* convert and dither the image to XImage */
629 for (y
=0, ofs
=0; y
<image
->height
; y
++) {
630 if (y
<image
->height
-1) {
632 for (x
=0, x1
=(y
+1)*image
->width
*channels
;
635 nerr
[x
++] = ptr
[x1
++];
636 nerr
[x
++] = ptr
[x1
++];
637 nerr
[x
++] = ptr
[x1
++];
641 nerr
[x
++] = ptr
[x1
++];
642 nerr
[x
++] = ptr
[x1
++];
643 nerr
[x
++] = ptr
[x1
++];
645 for (x
=0; x
<image
->width
*3; x
+=3, ofs
++) {
647 if (err
[x
]>0xff) err
[x
]=0xff; else if (err
[x
]<0) err
[x
]=0;
648 if (err
[x
+1]>0xff) err
[x
+1]=0xff; else if (err
[x
+1]<0) err
[x
+1]=0;
649 if (err
[x
+2]>0xff) err
[x
+2]=0xff; else if (err
[x
+2]<0) err
[x
+2]=0;
652 g
= gtable
[err
[x
+1]];
653 b
= btable
[err
[x
+2]];
657 data
[ofs
] = base_pixel
+ pixel
;
660 rer
= err
[x
] - (ctx
->colors
[pixel
].red
>>8);
661 ger
= err
[x
+1] - (ctx
->colors
[pixel
].green
>>8);
662 ber
= err
[x
+2] - (ctx
->colors
[pixel
].blue
>>8);
664 /* distribute error */
665 err
[x
+3*1]+=(rer
*7)/16;
666 err
[x
+1+3*1]+=(ger
*7)/16;
667 err
[x
+2+3*1]+=(ber
*7)/16;
670 nerr
[x
+1]+=(ger
*5)/16;
671 nerr
[x
+2]+=(ber
*5)/16;
674 nerr
[x
-3*1]+=(rer
*3)/16;
675 nerr
[x
-3*1+1]+=(ger
*3)/16;
676 nerr
[x
-3*1+2]+=(ber
*3)/16;
680 nerr
[x
+1+3*1]+=ger
/16;
681 nerr
[x
+2+3*1]+=ber
/16;
683 /* skip to next line */
688 ofs
+= ximg
->image
->bytes_per_line
- image
->width
;
693 ximg
->image
->data
= (char*)data
;
701 image2GrayScale(RContext
*ctx
, RImage
*image
)
704 register int x
, y
, g
;
706 const int cpc
=ctx
->attribs
->colors_per_channel
;
707 unsigned short gmask
;
708 unsigned short *table
;
710 int channels
= image
->format
== RRGBAFormat
? 4 : 3;
712 /*register unsigned char maxrgb = 0xff;*/
714 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
721 data
= (unsigned char *)ximg
->image
->data
;
723 if (ctx
->vclass
== StaticGray
)
724 gmask
= (1<<ctx
->depth
) - 1; /* use all grays */
726 gmask
= cpc
*cpc
*cpc
-1;
728 table
= computeTable(gmask
);
731 RErrorCode
= RERR_NOMEMORY
;
732 RDestroyXImage(ctx
, ximg
);
736 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
739 printf("grayscale match with %d colors per channel\n", cpc
);
741 for (y
=0; y
<image
->height
; y
++) {
742 for (x
=0; x
<image
->width
; x
++) {
744 g
= table
[(*ptr
++ * 30 + *ptr
++ * 59 + *ptr
++ * 11)/100];
746 /*data[ofs] = ctx->colors[g].pixel;*/
747 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[g
].pixel
);
756 const int dg
=0xff/gmask
;
759 printf("grayscale dither with %d colors per channel\n", cpc
);
761 gerr
= (short*)malloc((image
->width
+2)*sizeof(short));
762 ngerr
= (short*)malloc((image
->width
+2)*sizeof(short));
763 if (!gerr
|| !ngerr
) {
766 RErrorCode
= RERR_NOMEMORY
;
767 RDestroyXImage(ctx
, ximg
);
770 for (x
=0; x
<image
->width
; x
++) {
771 gerr
[x
] = (ptr
[x
*3]*30 + ptr
[x
*3+1]*59 + ptr
[x
*3+2]*11)/100;
774 /* convert and dither the image to XImage */
775 for (y
=0; y
<image
->height
; y
++) {
776 if (y
<image
->height
-1) {
778 for (x
=0, x1
=(y
+1)*image
->width
*3;
781 ngerr
[x
] = (ptr
[x1
++]*30 + ptr
[x1
++]*59 + ptr
[x1
++]*11)/100;
785 ngerr
[x
] = (ptr
[x1
++]*30 + ptr
[x1
++]*59 + ptr
[x1
++]*11)/100;
787 for (x
=0; x
<image
->width
; x
++) {
789 if (gerr
[x
]>0xff) gerr
[x
]=0xff; else if (gerr
[x
]<0) gerr
[x
]=0;
793 /*data[ofs] = ctx->colors[g].pixel;*/
794 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[g
].pixel
);
796 ger
= gerr
[x
] - g
*dg
;
798 /* distribute error */
807 /* skip to next line */
815 ximg
->image
->data
= (char*)data
;
822 image2Bitmap(RContext
*ctx
, RImage
*image
, int threshold
)
825 unsigned char *alpha
;
828 ximg
= RCreateXImage(ctx
, 1, image
->width
, image
->height
);
832 alpha
= image
->data
+3;
834 for (y
= 0; y
< image
->height
; y
++) {
835 for (x
= 0; x
< image
->width
; x
++) {
836 XPutPixel(ximg
->image
, x
, y
, (*alpha
<= threshold
? 0 : 1));
847 RConvertImage(RContext
*context
, RImage
*image
, Pixmap
*pixmap
)
854 assert(context
!=NULL
);
856 assert(pixmap
!=NULL
);
858 /* clear error message */
859 if (context
->vclass
== TrueColor
) {
861 ximg
= image2TrueColor(context
, image
);
863 } else if (context
->vclass
== PseudoColor
864 || context
->vclass
== StaticColor
) {
869 if (context
->attribs
->standard_colormap_mode
!= RIgnoreStdColormap
)
870 ximg
= image2StandardPseudoColor(context
, image
);
872 ximg
= image2PseudoColor(context
, image
);
876 } else if (context
->vclass
== GrayScale
|| context
->vclass
== StaticGray
) {
878 ximg
= image2GrayScale(context
, image
);
886 *pixmap
= XCreatePixmap(context
->dpy
, context
->drawable
, image
->width
,
887 image
->height
, context
->depth
);
890 if (context
->flags
.use_shared_pixmap
&& ximg
->is_shared
)
891 tmp
= R_CreateXImageMappedPixmap(context
, ximg
);
896 * We have to copy the shm Pixmap into a normal Pixmap because
897 * otherwise, we would have to control when Pixmaps are freed so
898 * that we can detach their shm segments. This is a problem if the
899 * program crash, leaving stale shared memory segments in the
900 * system (lots of them). But with some work, we can optimize
901 * things and remove this XCopyArea. This will require
902 * explicitly freeing all pixmaps when exiting or restarting
905 XCopyArea(context
->dpy
, tmp
, *pixmap
, context
->copy_gc
, 0, 0,
906 image
->width
, image
->height
, 0, 0);
907 XFreePixmap(context
->dpy
, tmp
);
909 RPutXImage(context
, *pixmap
, context
->copy_gc
, ximg
, 0, 0, 0, 0,
910 image
->width
, image
->height
);
913 RPutXImage(context
, *pixmap
, context
->copy_gc
, ximg
, 0, 0, 0, 0,
914 image
->width
, image
->height
);
917 RDestroyXImage(context
, ximg
);
924 RConvertImageMask(RContext
*context
, RImage
*image
, Pixmap
*pixmap
,
925 Pixmap
*mask
, int threshold
)
931 assert(context
!=NULL
);
933 assert(pixmap
!=NULL
);
936 if (!RConvertImage(context
, image
, pixmap
))
939 if (image
->format
==RRGBFormat
) {
944 ximg
= image2Bitmap(context
, image
, threshold
);
949 *mask
= XCreatePixmap(context
->dpy
, context
->drawable
, image
->width
,
951 gcv
.foreground
= context
->black
;
952 gcv
.background
= context
->white
;
953 gcv
.graphics_exposures
= False
;
954 gc
= XCreateGC(context
->dpy
, *mask
, GCForeground
|GCBackground
955 |GCGraphicsExposures
, &gcv
);
956 RPutXImage(context
, *mask
, gc
, ximg
, 0, 0, 0, 0,
957 image
->width
, image
->height
);
958 RDestroyXImage(context
, ximg
);
965 RGetClosestXColor(RContext
*context
, RColor
*color
, XColor
*retColor
)
967 if (context
->vclass
== TrueColor
) {
968 unsigned short rmask
, gmask
, bmask
;
969 unsigned short roffs
, goffs
, boffs
;
970 unsigned short *rtable
, *gtable
, *btable
;
972 roffs
= context
->red_offset
;
973 goffs
= context
->green_offset
;
974 boffs
= context
->blue_offset
;
976 rmask
= context
->visual
->red_mask
>> roffs
;
977 gmask
= context
->visual
->green_mask
>> goffs
;
978 bmask
= context
->visual
->blue_mask
>> boffs
;
980 rtable
= computeTable(rmask
);
981 gtable
= computeTable(gmask
);
982 btable
= computeTable(bmask
);
984 retColor
->pixel
= (rtable
[color
->red
]<<roffs
) |
985 (gtable
[color
->green
]<<goffs
) | (btable
[color
->blue
]<<boffs
);
987 retColor
->red
= color
->red
<< 8;
988 retColor
->green
= color
->green
<< 8;
989 retColor
->blue
= color
->blue
<< 8;
990 retColor
->flags
= DoRed
|DoGreen
|DoBlue
;
992 } else if (context
->vclass
== PseudoColor
993 || context
->vclass
== StaticColor
) {
995 if (context
->attribs
->standard_colormap_mode
!= RIgnoreStdColormap
) {
996 unsigned int *rtable
, *gtable
, *btable
;
998 rtable
= computeStdTable(context
->std_rgb_map
->red_mult
,
999 context
->std_rgb_map
->red_max
);
1001 gtable
= computeStdTable(context
->std_rgb_map
->green_mult
,
1002 context
->std_rgb_map
->green_max
);
1004 btable
= computeStdTable(context
->std_rgb_map
->blue_mult
,
1005 context
->std_rgb_map
->blue_max
);
1007 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
1008 RErrorCode
= RERR_NOMEMORY
;
1012 retColor
->pixel
= (rtable
[color
->red
]
1013 + gtable
[color
->green
]
1014 + btable
[color
->blue
]
1015 + context
->std_rgb_map
->base_pixel
) & 0xffffffff;
1016 retColor
->red
= color
->red
<<8;
1017 retColor
->green
= color
->green
<<8;
1018 retColor
->blue
= color
->blue
<<8;
1019 retColor
->flags
= DoRed
|DoGreen
|DoBlue
;
1022 const int cpc
=context
->attribs
->colors_per_channel
;
1023 const unsigned short rmask
= cpc
-1; /* different sizes could be used */
1024 const unsigned short gmask
= rmask
; /* for r,g,b */
1025 const unsigned short bmask
= rmask
;
1026 unsigned short *rtable
, *gtable
, *btable
;
1027 const int cpccpc
= cpc
*cpc
;
1030 rtable
= computeTable(rmask
);
1031 gtable
= computeTable(gmask
);
1032 btable
= computeTable(bmask
);
1034 if (rtable
==NULL
|| gtable
==NULL
|| btable
==NULL
) {
1035 RErrorCode
= RERR_NOMEMORY
;
1038 index
= rtable
[color
->red
]*cpccpc
+ gtable
[color
->green
]*cpc
1039 + btable
[color
->blue
];
1040 *retColor
= context
->colors
[index
];
1043 } else if (context
->vclass
== GrayScale
|| context
->vclass
== StaticGray
) {
1045 const int cpc
= context
->attribs
->colors_per_channel
;
1046 unsigned short gmask
;
1047 unsigned short *table
;
1050 if (context
->vclass
== StaticGray
)
1051 gmask
= (1<<context
->depth
) - 1; /* use all grays */
1053 gmask
= cpc
*cpc
*cpc
-1;
1055 table
= computeTable(gmask
);
1059 index
= table
[(color
->red
*30 + color
->green
*59 + color
->blue
*11)/100];
1061 *retColor
= context
->colors
[index
];
1063 RErrorCode
= RERR_INTERNAL
;