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>
39 extern Pixmap
R_CreateXImageMappedPixmap(RContext
* context
, RXImage
* ximage
);
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 unsigned short *computeTable(unsigned short mask
)
67 RConversionTable
*tmp
= conversionTable
;
71 if (tmp
->index
== mask
)
79 tmp
= (RConversionTable
*) malloc(sizeof(RConversionTable
));
83 for (i
= 0; i
< 256; i
++)
84 tmp
->table
[i
] = (i
* mask
+ 0x7f) / 0xff;
87 tmp
->next
= conversionTable
;
88 conversionTable
= tmp
;
92 static unsigned int *computeStdTable(unsigned int mult
, unsigned int max
)
94 RStdConversionTable
*tmp
= stdConversionTable
;
98 if (tmp
->mult
== mult
&& tmp
->max
== max
)
106 tmp
= (RStdConversionTable
*) malloc(sizeof(RStdConversionTable
));
110 for (i
= 0; i
< 256; i
++) {
111 tmp
->table
[i
] = (i
* max
) / 0xff * mult
;
116 tmp
->next
= stdConversionTable
;
117 stdConversionTable
= tmp
;
122 /***************************************************************************/
125 convertTrueColor_generic(RXImage
* ximg
, RImage
* image
,
126 signed char *err
, signed char *nerr
,
127 const unsigned short *rtable
,
128 const unsigned short *gtable
,
129 const unsigned short *btable
,
130 const int dr
, const int dg
, const int db
,
131 const unsigned short roffs
, const unsigned short goffs
, const unsigned short boffs
)
137 unsigned char *ptr
= image
->data
;
138 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
140 /* convert and dither the image to XImage */
141 for (y
= 0; y
< image
->height
; y
++) {
145 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
) {
148 pixel
= *ptr
+ err
[x
];
151 else if (pixel
> 0xff)
155 rer
= pixel
- r
* dr
;
158 pixel
= *(ptr
+ 1) + err
[x
+ 1];
161 else if (pixel
> 0xff)
165 ger
= pixel
- g
* dg
;
168 pixel
= *(ptr
+ 2) + err
[x
+ 2];
171 else if (pixel
> 0xff)
175 ber
= pixel
- b
* db
;
177 pixel
= (r
<< roffs
) | (g
<< goffs
) | (b
<< boffs
);
178 XPutPixel(ximg
->image
, x
, y
, pixel
);
180 /* distribute error */
186 err
[x
+ 1 + 3 * 1] += g
;
187 err
[x
+ 2 + 3 * 1] += b
;
193 nerr
[x
+ 3 * 1] = rer
- 2 * r
;
194 nerr
[x
+ 1 + 3 * 1] = ger
- 2 * g
;
195 nerr
[x
+ 2 + 3 * 1] = ber
- 2 * b
;
197 /* skip to next line */
203 /* redither the 1st line to distribute error better */
209 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
) {
212 pixel
= *ptr
+ err
[x
];
215 else if (pixel
> 0xff)
219 rer
= pixel
- r
* dr
;
222 pixel
= *(ptr
+ 1) + err
[x
+ 1];
225 else if (pixel
> 0xff)
229 ger
= pixel
- g
* dg
;
232 pixel
= *(ptr
+ 2) + err
[x
+ 2];
235 else if (pixel
> 0xff)
239 ber
= pixel
- b
* db
;
241 pixel
= (r
<< roffs
) | (g
<< goffs
) | (b
<< boffs
);
242 XPutPixel(ximg
->image
, x
, y
, pixel
);
244 /* distribute error */
250 err
[x
+ 1 + 3 * 1] += g
;
251 err
[x
+ 2 + 3 * 1] += b
;
257 nerr
[x
+ 3 * 1] = rer
- 2 * r
;
258 nerr
[x
+ 1 + 3 * 1] = ger
- 2 * g
;
259 nerr
[x
+ 2 + 3 * 1] = ber
- 2 * b
;
263 static RXImage
*image2TrueColor(RContext
* ctx
, RImage
* image
)
266 unsigned short rmask
, gmask
, bmask
;
267 unsigned short roffs
, goffs
, boffs
;
268 unsigned short *rtable
, *gtable
, *btable
;
269 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
271 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
276 roffs
= ctx
->red_offset
;
277 goffs
= ctx
->green_offset
;
278 boffs
= ctx
->blue_offset
;
280 rmask
= ctx
->visual
->red_mask
>> roffs
;
281 gmask
= ctx
->visual
->green_mask
>> goffs
;
282 bmask
= ctx
->visual
->blue_mask
>> boffs
;
284 rtable
= computeTable(rmask
);
285 gtable
= computeTable(gmask
);
286 btable
= computeTable(bmask
);
288 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
289 RErrorCode
= RERR_NOMEMORY
;
290 RDestroyXImage(ctx
, ximg
);
294 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
298 unsigned char *ptr
= image
->data
;
302 fputs("true color match\n", stderr
);
304 if (rmask
== 0xff && gmask
== 0xff && bmask
== 0xff) {
305 for (y
= 0; y
< image
->height
; y
++) {
306 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
) {
308 pixel
= (*(ptr
) << roffs
) | (*(ptr
+ 1) << goffs
) | (*(ptr
+ 2) << boffs
);
309 XPutPixel(ximg
->image
, x
, y
, pixel
);
313 for (y
= 0, ofs
= 0; y
< image
->height
; y
++) {
314 for (x
= 0; x
< image
->width
; x
++, ofs
+= channels
- 3) {
316 r
= rtable
[ptr
[ofs
++]];
317 g
= gtable
[ptr
[ofs
++]];
318 b
= btable
[ptr
[ofs
++]];
319 pixel
= (r
<< roffs
) | (g
<< goffs
) | (b
<< boffs
);
320 XPutPixel(ximg
->image
, x
, y
, pixel
);
326 const int dr
= 0xff / rmask
;
327 const int dg
= 0xff / gmask
;
328 const int db
= 0xff / bmask
;
331 fputs("true color dither\n", stderr
);
337 int ch
= (HAS_ALPHA(image
) ? 4 : 3);
339 err
= malloc(ch
* (image
->width
+ 2));
340 nerr
= malloc(ch
* (image
->width
+ 2));
344 RErrorCode
= RERR_NOMEMORY
;
345 RDestroyXImage(ctx
, ximg
);
349 memset(err
, 0, ch
* (image
->width
+ 2));
350 memset(nerr
, 0, ch
* (image
->width
+ 2));
352 convertTrueColor_generic(ximg
, image
, err
, nerr
,
353 rtable
, gtable
, btable
, dr
, dg
, db
, roffs
, goffs
, boffs
);
363 /***************************************************************************/
366 convertPseudoColor_to_8(RXImage
* ximg
, RImage
* image
,
367 signed char *err
, signed char *nerr
,
368 const unsigned short *rtable
,
369 const unsigned short *gtable
,
370 const unsigned short *btable
,
371 const int dr
, const int dg
, const int db
, unsigned long *pixels
, int cpc
)
377 unsigned char *ptr
= image
->data
;
378 unsigned char *optr
= (unsigned char *)ximg
->image
->data
;
379 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
380 int cpcpc
= cpc
* cpc
;
382 /* convert and dither the image to XImage */
383 for (y
= 0; y
< image
->height
; y
++) {
387 for (x
= 0; x
< image
->width
* 3; x
+= 3, ptr
+= channels
) {
390 pixel
= *ptr
+ err
[x
];
393 else if (pixel
> 0xff)
397 rer
= pixel
- r
* dr
;
400 pixel
= *(ptr
+ 1) + err
[x
+ 1];
403 else if (pixel
> 0xff)
407 ger
= pixel
- g
* dg
;
410 pixel
= *(ptr
+ 2) + err
[x
+ 2];
413 else if (pixel
> 0xff)
417 ber
= pixel
- b
* db
;
419 *optr
++ = pixels
[r
* cpcpc
+ g
* cpc
+ b
];
421 /* distribute error */
428 err
[x
+ 1 + 3 * 1] += g
;
429 err
[x
+ 2 + 3 * 1] += b
;
435 nerr
[x
+ 3 * 1] = rer
- 2 * r
;
436 nerr
[x
+ 1 + 3 * 1] = ger
- 2 * g
;
437 nerr
[x
+ 2 + 3 * 1] = ber
- 2 * b
;
439 /* skip to next line */
444 optr
+= ximg
->image
->bytes_per_line
- image
->width
;
448 static RXImage
*image2PseudoColor(RContext
* ctx
, RImage
* image
)
451 register int x
, y
, r
, g
, b
;
454 const int cpc
= ctx
->attribs
->colors_per_channel
;
455 const unsigned short rmask
= cpc
- 1; /* different sizes could be used */
456 const unsigned short gmask
= rmask
; /* for r,g,b */
457 const unsigned short bmask
= rmask
;
458 unsigned short *rtable
, *gtable
, *btable
;
459 const int cpccpc
= cpc
* cpc
;
460 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
462 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
469 /* Tables are same at the moment because rmask==gmask==bmask. */
470 rtable
= computeTable(rmask
);
471 gtable
= computeTable(gmask
);
472 btable
= computeTable(bmask
);
474 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
475 RErrorCode
= RERR_NOMEMORY
;
476 RDestroyXImage(ctx
, ximg
);
480 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
483 fprintf(stderr
, "pseudo color match with %d colors per channel\n", cpc
);
485 for (y
= 0; y
< image
->height
; y
++) {
486 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
- 3) {
491 pixel
= r
* cpccpc
+ g
* cpc
+ b
;
492 /*data[ofs] = ctx->colors[pixel].pixel; */
493 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[pixel
].pixel
);
500 const int dr
= 0xff / rmask
;
501 const int dg
= 0xff / gmask
;
502 const int db
= 0xff / bmask
;
505 fprintf(stderr
, "pseudo color dithering with %d colors per channel\n", cpc
);
507 err
= malloc(4 * (image
->width
+ 3));
508 nerr
= malloc(4 * (image
->width
+ 3));
512 RErrorCode
= RERR_NOMEMORY
;
513 RDestroyXImage(ctx
, ximg
);
516 memset(err
, 0, 4 * (image
->width
+ 3));
517 memset(nerr
, 0, 4 * (image
->width
+ 3));
519 convertPseudoColor_to_8(ximg
, image
, err
+ 4, nerr
+ 4,
520 rtable
, gtable
, btable
, dr
, dg
, db
, ctx
->pixels
, cpc
);
530 * For standard colormap
532 static RXImage
*image2StandardPseudoColor(RContext
* ctx
, RImage
* image
)
535 register int x
, y
, r
, g
, b
;
539 unsigned int *rtable
, *gtable
, *btable
;
540 unsigned int base_pixel
= ctx
->std_rgb_map
->base_pixel
;
541 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
543 ximg
= RCreateXImage(ctx
, ctx
->depth
, image
->width
, image
->height
);
550 data
= (unsigned char *)ximg
->image
->data
;
552 rtable
= computeStdTable(ctx
->std_rgb_map
->red_mult
, ctx
->std_rgb_map
->red_max
);
554 gtable
= computeStdTable(ctx
->std_rgb_map
->green_mult
, ctx
->std_rgb_map
->green_max
);
556 btable
= computeStdTable(ctx
->std_rgb_map
->blue_mult
, ctx
->std_rgb_map
->blue_max
);
558 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
559 RErrorCode
= RERR_NOMEMORY
;
560 RDestroyXImage(ctx
, ximg
);
564 if (ctx
->attribs
->render_mode
== RBestMatchRendering
) {
565 for (y
= 0; y
< image
->height
; y
++) {
566 for (x
= 0; x
< image
->width
; x
++, ptr
+= channels
) {
568 pixel
= (rtable
[*ptr
] + gtable
[*(ptr
+ 1)]
569 + btable
[*(ptr
+ 2)] + base_pixel
) & 0xffffffff;
571 XPutPixel(ximg
->image
, x
, y
, pixel
);
576 signed short *err
, *nerr
;
582 fprintf(stderr
, "pseudo color dithering with %d colors per channel\n",
583 ctx
->attribs
->colors_per_channel
);
585 err
= (short *)malloc(3 * (image
->width
+ 2) * sizeof(short));
586 nerr
= (short *)malloc(3 * (image
->width
+ 2) * sizeof(short));
590 RErrorCode
= RERR_NOMEMORY
;
591 RDestroyXImage(ctx
, ximg
);
594 for (x
= 0, x1
= 0; x
< image
->width
* 3; x1
+= channels
- 3) {
595 err
[x
++] = ptr
[x1
++];
596 err
[x
++] = ptr
[x1
++];
597 err
[x
++] = ptr
[x1
++];
599 err
[x
] = err
[x
+ 1] = err
[x
+ 2] = 0;
600 /* convert and dither the image to XImage */
601 for (y
= 0, ofs
= 0; y
< image
->height
; y
++) {
602 if (y
< image
->height
- 1) {
604 for (x
= 0, x1
= (y
+ 1) * image
->width
* channels
;
605 x
< image
->width
* 3; x1
+= channels
- 3) {
606 nerr
[x
++] = ptr
[x1
++];
607 nerr
[x
++] = ptr
[x1
++];
608 nerr
[x
++] = ptr
[x1
++];
612 nerr
[x
++] = ptr
[x1
++];
613 nerr
[x
++] = ptr
[x1
++];
614 nerr
[x
++] = ptr
[x1
++];
616 for (x
= 0; x
< image
->width
* 3; x
+= 3, ofs
++) {
622 if (err
[x
+ 1] > 0xff)
624 else if (err
[x
+ 1] < 0)
626 if (err
[x
+ 2] > 0xff)
628 else if (err
[x
+ 2] < 0)
632 g
= gtable
[err
[x
+ 1]];
633 b
= btable
[err
[x
+ 2]];
637 data
[ofs
] = base_pixel
+ pixel
;
640 rer
= err
[x
] - (ctx
->colors
[pixel
].red
>> 8);
641 ger
= err
[x
+ 1] - (ctx
->colors
[pixel
].green
>> 8);
642 ber
= err
[x
+ 2] - (ctx
->colors
[pixel
].blue
>> 8);
644 /* distribute error */
645 err
[x
+ 3 * 1] += (rer
* 7) / 16;
646 err
[x
+ 1 + 3 * 1] += (ger
* 7) / 16;
647 err
[x
+ 2 + 3 * 1] += (ber
* 7) / 16;
649 nerr
[x
] += (rer
* 5) / 16;
650 nerr
[x
+ 1] += (ger
* 5) / 16;
651 nerr
[x
+ 2] += (ber
* 5) / 16;
654 nerr
[x
- 3 * 1] += (rer
* 3) / 16;
655 nerr
[x
- 3 * 1 + 1] += (ger
* 3) / 16;
656 nerr
[x
- 3 * 1 + 2] += (ber
* 3) / 16;
659 nerr
[x
+ 3 * 1] += rer
/ 16;
660 nerr
[x
+ 1 + 3 * 1] += ger
/ 16;
661 nerr
[x
+ 2 + 3 * 1] += ber
/ 16;
663 /* skip to next line */
668 ofs
+= ximg
->image
->bytes_per_line
- image
->width
;
673 ximg
->image
->data
= (char *)data
;
678 static RXImage
*image2GrayScale(RContext
* ctx
, RImage
* image
)
681 register int x
, y
, g
;
683 const int cpc
= ctx
->attribs
->colors_per_channel
;
684 unsigned short gmask
;
685 unsigned short *table
;
687 int channels
= (HAS_ALPHA(image
) ? 4 : 3);
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 fprintf(stderr
, "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
+ 1) * 59 + *(ptr
+ 2) * 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 fprintf(stderr
, "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, y
= 0; x
< image
->width
; x
++, y
+= channels
) {
746 gerr
[x
] = (ptr
[y
] * 30 + ptr
[y
+ 1] * 59 + ptr
[y
+ 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
* channels
; x
< image
->width
;
754 x
++, x1
+= channels
) {
755 ngerr
[x
] = (ptr
[x1
] * 30 + ptr
[x1
+ 1] * 59 + ptr
[x1
+ 2] * 11) / 100;
759 ngerr
[x
] = (ptr
[x1
] * 30 + ptr
[x1
+ 1] * 59 + ptr
[x1
+ 2] * 11) / 100;
761 for (x
= 0; x
< image
->width
; x
++) {
765 else if (gerr
[x
] < 0)
770 /*data[ofs] = ctx->colors[g].pixel; */
771 XPutPixel(ximg
->image
, x
, y
, ctx
->colors
[g
].pixel
);
773 ger
= gerr
[x
] - g
* dg
;
775 /* distribute error */
782 ngerr
[x
+ 1] += ger
- 2 * g
;
784 /* skip to next line */
792 ximg
->image
->data
= (char *)data
;
797 static RXImage
*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));
819 int RConvertImage(RContext
* context
, RImage
* image
, Pixmap
* pixmap
)
821 RXImage
*ximg
= NULL
;
826 assert(context
!= NULL
);
827 assert(image
!= NULL
);
828 assert(pixmap
!= NULL
);
830 switch (context
->vclass
) {
832 ximg
= image2TrueColor(context
, image
);
837 if (context
->attribs
->standard_colormap_mode
!= RIgnoreStdColormap
)
838 ximg
= image2StandardPseudoColor(context
, image
);
840 ximg
= image2PseudoColor(context
, image
);
845 ximg
= image2GrayScale(context
, image
);
853 *pixmap
= XCreatePixmap(context
->dpy
, context
->drawable
, image
->width
, image
->height
, context
->depth
);
856 if (context
->flags
.use_shared_pixmap
&& ximg
->is_shared
)
857 tmp
= R_CreateXImageMappedPixmap(context
, ximg
);
862 * We have to copy the shm Pixmap into a normal Pixmap because
863 * otherwise, we would have to control when Pixmaps are freed so
864 * that we can detach their shm segments. This is a problem if the
865 * program crash, leaving stale shared memory segments in the
866 * system (lots of them). But with some work, we can optimize
867 * things and remove this XCopyArea. This will require
868 * explicitly freeing all pixmaps when exiting or restarting
871 XCopyArea(context
->dpy
, tmp
, *pixmap
, context
->copy_gc
, 0, 0, image
->width
, image
->height
, 0, 0);
872 XFreePixmap(context
->dpy
, tmp
);
874 RPutXImage(context
, *pixmap
, context
->copy_gc
, ximg
, 0, 0, 0, 0, image
->width
, image
->height
);
877 RPutXImage(context
, *pixmap
, context
->copy_gc
, ximg
, 0, 0, 0, 0, image
->width
, image
->height
);
880 RDestroyXImage(context
, ximg
);
885 /* make the gc permanent (create with context creation).
886 * GC creation is very expensive. altering its properties is not. -Dan
888 int RConvertImageMask(RContext
* context
, RImage
* image
, Pixmap
* pixmap
, Pixmap
* mask
, int threshold
)
892 RXImage
*ximg
= NULL
;
894 assert(context
!= NULL
);
895 assert(image
!= NULL
);
896 assert(pixmap
!= NULL
);
897 assert(mask
!= NULL
);
899 if (!RConvertImage(context
, image
, pixmap
))
902 if (image
->format
== RRGBFormat
) {
907 ximg
= image2Bitmap(context
, image
, threshold
);
912 *mask
= XCreatePixmap(context
->dpy
, context
->drawable
, image
->width
, image
->height
, 1);
913 gcv
.foreground
= context
->black
;
914 gcv
.background
= context
->white
;
915 gcv
.graphics_exposures
= False
;
916 gc
= XCreateGC(context
->dpy
, *mask
, GCForeground
| GCBackground
| GCGraphicsExposures
, &gcv
);
917 RPutXImage(context
, *mask
, gc
, ximg
, 0, 0, 0, 0, image
->width
, image
->height
);
918 RDestroyXImage(context
, ximg
);
919 XFreeGC(context
->dpy
, gc
);
924 Bool
RGetClosestXColor(RContext
* context
, const RColor
* color
, XColor
* retColor
)
926 if (context
->vclass
== TrueColor
) {
927 unsigned short rmask
, gmask
, bmask
;
928 unsigned short roffs
, goffs
, boffs
;
929 unsigned short *rtable
, *gtable
, *btable
;
931 roffs
= context
->red_offset
;
932 goffs
= context
->green_offset
;
933 boffs
= context
->blue_offset
;
935 rmask
= context
->visual
->red_mask
>> roffs
;
936 gmask
= context
->visual
->green_mask
>> goffs
;
937 bmask
= context
->visual
->blue_mask
>> boffs
;
939 rtable
= computeTable(rmask
);
940 gtable
= computeTable(gmask
);
941 btable
= computeTable(bmask
);
943 retColor
->pixel
= (rtable
[color
->red
] << roffs
) |
944 (gtable
[color
->green
] << goffs
) | (btable
[color
->blue
] << boffs
);
946 retColor
->red
= color
->red
<< 8;
947 retColor
->green
= color
->green
<< 8;
948 retColor
->blue
= color
->blue
<< 8;
949 retColor
->flags
= DoRed
| DoGreen
| DoBlue
;
951 } else if (context
->vclass
== PseudoColor
|| context
->vclass
== StaticColor
) {
953 if (context
->attribs
->standard_colormap_mode
!= RIgnoreStdColormap
) {
954 unsigned int *rtable
, *gtable
, *btable
;
956 rtable
= computeStdTable(context
->std_rgb_map
->red_mult
, context
->std_rgb_map
->red_max
);
958 gtable
= computeStdTable(context
->std_rgb_map
->green_mult
,
959 context
->std_rgb_map
->green_max
);
961 btable
= computeStdTable(context
->std_rgb_map
->blue_mult
, context
->std_rgb_map
->blue_max
);
963 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
964 RErrorCode
= RERR_NOMEMORY
;
968 retColor
->pixel
= (rtable
[color
->red
]
969 + gtable
[color
->green
]
970 + btable
[color
->blue
]
971 + context
->std_rgb_map
->base_pixel
) & 0xffffffff;
972 retColor
->red
= color
->red
<< 8;
973 retColor
->green
= color
->green
<< 8;
974 retColor
->blue
= color
->blue
<< 8;
975 retColor
->flags
= DoRed
| DoGreen
| DoBlue
;
978 const int cpc
= context
->attribs
->colors_per_channel
;
979 const unsigned short rmask
= cpc
- 1; /* different sizes could be used */
980 const unsigned short gmask
= rmask
; /* for r,g,b */
981 const unsigned short bmask
= rmask
;
982 unsigned short *rtable
, *gtable
, *btable
;
983 const int cpccpc
= cpc
* cpc
;
986 rtable
= computeTable(rmask
);
987 gtable
= computeTable(gmask
);
988 btable
= computeTable(bmask
);
990 if (rtable
== NULL
|| gtable
== NULL
|| btable
== NULL
) {
991 RErrorCode
= RERR_NOMEMORY
;
994 index
= rtable
[color
->red
] * cpccpc
+ gtable
[color
->green
] * cpc
+ btable
[color
->blue
];
995 *retColor
= context
->colors
[index
];
998 } else if (context
->vclass
== GrayScale
|| context
->vclass
== StaticGray
) {
1000 const int cpc
= context
->attribs
->colors_per_channel
;
1001 unsigned short gmask
;
1002 unsigned short *table
;
1005 if (context
->vclass
== StaticGray
)
1006 gmask
= (1 << context
->depth
) - 1; /* use all grays */
1008 gmask
= cpc
* cpc
* cpc
- 1;
1010 table
= computeTable(gmask
);
1014 index
= table
[(color
->red
* 30 + color
->green
* 59 + color
->blue
* 11) / 100];
1016 *retColor
= context
->colors
[index
];
1018 RErrorCode
= RERR_INTERNAL
;