ooptimized code for 8bpp/PseudoColor
[wmaker-crm.git] / wrlib / convert.c
blob5310bcffd08c61d336a875bedbd83682c390ebe0
1 /* convert.c - convert RImage to Pixmap
2 *
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.
21 #include <config.h>
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include <assert.h>
32 #ifdef BENCH
33 #include "bench.h"
34 #endif
36 #include "wraster.h"
38 #ifdef XSHM
39 extern Pixmap R_CreateXImageMappedPixmap(RContext *context, RXImage *ximage);
41 #endif
44 #ifdef ASM_X86_MMX
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,
52 short *btable,
53 int dr, int dg, int db,
54 unsigned int roffs,
55 unsigned int goffs,
56 unsigned int boffs,
57 int width, int height,
58 int line_offset);
62 #endif /* X86_ASM */
66 typedef struct RConversionTable {
67 unsigned short table[256];
68 unsigned short index;
70 struct RConversionTable *next;
71 } RConversionTable;
74 typedef struct RStdConversionTable {
75 unsigned int table[256];
77 unsigned short mult;
78 unsigned short max;
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;
93 int i;
95 while (tmp) {
96 if (tmp->index == mask)
97 break;
98 tmp = tmp->next;
101 if (tmp)
102 return tmp->table;
104 tmp = (RConversionTable *)malloc(sizeof(RConversionTable));
105 if (tmp == NULL)
106 return NULL;
108 for (i=0;i<256;i++)
109 tmp->table[i] = (i*mask + 0x7f)/0xff;
111 tmp->index = mask;
112 tmp->next = conversionTable;
113 conversionTable = tmp;
114 return tmp->table;
118 static unsigned int*
119 computeStdTable(unsigned int mult, unsigned int max)
121 RStdConversionTable *tmp = stdConversionTable;
122 unsigned int i;
124 while (tmp) {
125 if (tmp->mult == mult && tmp->max == max)
126 break;
127 tmp = tmp->next;
130 if (tmp)
131 return tmp->table;
133 tmp = (RStdConversionTable *)malloc(sizeof(RStdConversionTable));
134 if (tmp == NULL)
135 return NULL;
137 for (i=0; i<256; i++) {
138 tmp->table[i] = (i*max)/0xff * mult;
140 tmp->mult = mult;
141 tmp->max = max;
143 tmp->next = stdConversionTable;
144 stdConversionTable = tmp;
146 return tmp->table;
149 /***************************************************************************/
152 static void
153 convertTrueColor_generic(RXImage *ximg, RImage *image,
154 char *err, char *nerr,
155 const short *rtable,
156 const short *gtable,
157 const short *btable,
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)
163 char *terr;
164 int x, y, r, g, b;
165 int pixel;
166 int rer, ger, ber;
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++) {
172 nerr[0] = 0;
173 nerr[1] = 0;
174 nerr[2] = 0;
175 for (x=0; x<image->width; x++, ptr+=channels) {
177 /* reduce pixel */
178 pixel = *ptr + err[x];
179 if (pixel<0) pixel=0; else if (pixel>0xff) pixel=0xff;
180 r = rtable[pixel];
181 /* calc error */
182 rer = pixel - r*dr;
184 /* reduce pixel */
185 pixel = *(ptr+1) + err[x+1];
186 if (pixel<0) pixel=0; else if (pixel>0xff) pixel=0xff;
187 g = gtable[pixel];
188 /* calc error */
189 ger = pixel - g*dg;
191 /* reduce pixel */
192 pixel = *(ptr+2) + err[x+2];
193 if (pixel<0) pixel=0; else if (pixel>0xff) pixel=0xff;
194 b = btable[pixel];
195 /* calc error */
196 ber = pixel - b*db;
199 pixel = (r<<roffs) | (g<<goffs) | (b<<boffs);
200 XPutPixel(ximg->image, x, y, pixel);
202 /* distribute error */
203 r = (rer*3)/8;
204 g = (ger*3)/8;
205 b = (ber*3)/8;
206 /* x+1, y */
207 err[x+3*1]+=r;
208 err[x+1+3*1]+=g;
209 err[x+2+3*1]+=b;
210 /* x, y+1 */
211 nerr[x]+=r;
212 nerr[x+1]+=g;
213 nerr[x+2]+=b;
214 /* x+1, y+1 */
215 nerr[x+3*1]=rer-2*r;
216 nerr[x+1+3*1]=ger-2*g;
217 nerr[x+2+3*1]=ber-2*b;
219 /* skip to next line */
220 terr = err;
221 err = nerr;
222 nerr = terr;
229 static RXImage*
230 image2TrueColor(RContext *ctx, RImage *image)
232 RXImage *ximg;
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);
239 if (!ximg) {
240 return NULL;
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);
258 return NULL;
262 if (ctx->attribs->render_mode==RBestMatchRendering) {
263 int ofs, r, g, b;
264 int x, y;
265 unsigned long pixel;
266 unsigned char *ptr = image->data;
268 /* fake match */
269 #ifdef DEBUG
270 puts("true color match");
271 #endif
272 for (y=0, ofs=0; y < image->height; y++) {
273 for (x=0; x < image->width; x++, ofs+=channels-3) {
274 /* reduce pixel */
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);
282 } else {
283 /* dither */
284 const int dr=0xff/rmask;
285 const int dg=0xff/gmask;
286 const int db=0xff/bmask;
288 #ifdef DEBUG
289 puts("true color dither");
290 #endif
292 #ifdef BENCH
293 cycle_bench(1);
294 #endif
296 #ifdef ASM_X86_MMX
297 if (ctx->depth == 16 && image->format == RRGBAFormat
298 && x86_check_mmx()) {
299 short *err;
300 short *nerr;
302 err = malloc(8*(image->width+3));
303 nerr = malloc(8*(image->width+3));
304 if (!err || !nerr) {
305 if (nerr)
306 free(nerr);
307 RErrorCode = RERR_NOMEMORY;
308 RDestroyXImage(ctx, ximg);
309 return NULL;
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,
316 err+8, nerr+8,
317 rtable, gtable, btable,
318 dr, dg, db,
319 roffs, goffs, boffs,
320 image->width, image->height,
321 ximg->image->bytes_per_line - 2*image->width);
323 free(err);
324 free(nerr);
325 } else
326 #endif /* ASM_X86_MMX */
328 char *err;
329 char *nerr;
330 int ch = image->format == RRGBAFormat ? 4 : 3;
332 err = malloc(ch*(image->width+2));
333 nerr = malloc(ch*(image->width+2));
334 if (!err || !nerr) {
335 if (nerr)
336 free(nerr);
337 RErrorCode = RERR_NOMEMORY;
338 RDestroyXImage(ctx, ximg);
339 return NULL;
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);
348 free(err);
349 free(nerr);
354 #ifdef BENCH
355 cycle_bench(0);
356 #endif
358 return ximg;
362 /***************************************************************************/
364 static void
365 convertPseudoColor_to_8(RXImage *ximg, RImage *image,
366 char *err, char *nerr,
367 const short *rtable,
368 const short *gtable,
369 const short *btable,
370 const int dr, const int dg, const int db,
371 unsigned long *pixels,
372 int cpc)
374 char *terr;
375 int x, y, r, g, b;
376 int pixel;
377 int rer, ger, ber;
378 unsigned char *ptr = image->data;
379 unsigned char *optr = ximg->image->data;
380 int channels = image->format == RRGBAFormat ? 4 : 3;
381 int cpcpc = cpc*cpc;
383 /* convert and dither the image to XImage */
384 for (y=0; y<image->height; y++) {
385 nerr[0] = 0;
386 nerr[1] = 0;
387 nerr[2] = 0;
388 for (x=0; x<image->width*3; x+=3, ptr+=channels) {
390 /* reduce pixel */
391 pixel = *ptr + err[x];
392 if (pixel<0) pixel=0; else if (pixel>0xff) pixel=0xff;
393 r = rtable[pixel];
394 /* calc error */
395 rer = pixel - r*dr;
397 /* reduce pixel */
398 pixel = *(ptr+1) + err[x+1];
399 if (pixel<0) pixel=0; else if (pixel>0xff) pixel=0xff;
400 g = gtable[pixel];
401 /* calc error */
402 ger = pixel - g*dg;
404 /* reduce pixel */
405 pixel = *(ptr+2) + err[x+2];
406 if (pixel<0) pixel=0; else if (pixel>0xff) pixel=0xff;
407 b = btable[pixel];
408 /* calc error */
409 ber = pixel - b*db;
411 *optr++ = pixels[r*cpcpc + g*cpc + b];
413 /* distribute error */
414 r = (rer*3)/8;
415 g = (ger*3)/8;
416 b = (ber*3)/8;
417 /* x+1, y */
418 err[x+3*1]+=r;
419 err[x+1+3*1]+=g;
420 err[x+2+3*1]+=b;
421 /* x, y+1 */
422 nerr[x]+=r;
423 nerr[x+1]+=g;
424 nerr[x+2]+=b;
425 /* x+1, y+1 */
426 nerr[x+3*1]=rer-2*r;
427 nerr[x+1+3*1]=ger-2*g;
428 nerr[x+2+3*1]=ber-2*b;
430 /* skip to next line */
431 terr = err;
432 err = nerr;
433 nerr = terr;
435 optr += ximg->image->bytes_per_line - image->width;
441 static RXImage*
442 image2PseudoColor(RContext *ctx, RImage *image)
444 RXImage *ximg;
445 register int x, y, r, g, b;
446 unsigned char *ptr;
447 unsigned long pixel;
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);
457 if (!ximg) {
458 return NULL;
461 ptr = image->data;
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);
471 return NULL;
474 if (ctx->attribs->render_mode == RBestMatchRendering) {
475 /* fake match */
476 #ifdef DEBUG
477 printf("pseudo color match with %d colors per channel\n", cpc);
478 #endif
479 for (y=0; y<image->height; y++) {
480 for (x=0; x<image->width; x++, ptr+=channels-1) {
481 /* reduce pixel */
482 r = rtable[*ptr++];
483 g = gtable[*ptr++];
484 b = btable[*ptr++];
485 pixel = r*cpccpc + g*cpc + b;
486 /*data[ofs] = ctx->colors[pixel].pixel;*/
487 XPutPixel(ximg->image, x, y, ctx->colors[pixel].pixel);
490 } else {
491 /* dither */
492 char *err;
493 char *nerr;
494 const int dr=0xff/rmask;
495 const int dg=0xff/gmask;
496 const int db=0xff/bmask;
499 #ifdef DEBUG
500 printf("pseudo color dithering with %d colors per channel\n", cpc);
501 #endif
502 err = malloc(3*(image->width+2));
503 nerr = malloc(3*(image->width+2));
504 if (!err || !nerr) {
505 if (nerr)
506 free(nerr);
507 RErrorCode = RERR_NOMEMORY;
508 RDestroyXImage(ctx, ximg);
509 return NULL;
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);
518 free(err);
519 free(nerr);
522 return ximg;
527 * For standard colormap
529 static RXImage*
530 image2StandardPseudoColor(RContext *ctx, RImage *image)
532 RXImage *ximg;
533 register int x, y, r, g, b;
534 unsigned char *ptr;
535 unsigned long pixel;
536 unsigned char *data;
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);
543 if (!ximg) {
544 return NULL;
547 ptr = image->data;
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);
564 return NULL;
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) {
571 /* reduce pixel */
572 pixel = (rtable[*ptr++] + gtable[*ptr++]
573 + btable[*ptr++] + base_pixel) & 0xffffffff;
575 XPutPixel(ximg->image, x, y, pixel);
578 } else {
579 /* dither */
580 short *err, *nerr;
581 short *terr;
582 int rer, ger, ber;
583 int x1, ofs;
585 #ifdef DEBUG
586 printf("pseudo color dithering with %d colors per channel\n", cpc);
587 #endif
588 err = (short*)malloc(3*(image->width+2)*sizeof(short));
589 nerr = (short*)malloc(3*(image->width+2)*sizeof(short));
590 if (!err || !nerr) {
591 if (nerr)
592 free(nerr);
593 RErrorCode = RERR_NOMEMORY;
594 RDestroyXImage(ctx, ximg);
595 return NULL;
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) {
606 int x1;
607 for (x=0, x1=(y+1)*image->width*channels;
608 x<image->width*3;
609 x1+=channels-3) {
610 nerr[x++] = ptr[x1++];
611 nerr[x++] = ptr[x1++];
612 nerr[x++] = ptr[x1++];
614 /* last column */
615 x1-=channels;
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++) {
621 /* reduce pixel */
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;
626 r = rtable[err[x]];
627 g = gtable[err[x+1]];
628 b = btable[err[x+2]];
630 pixel = r + g + b;
632 data[ofs] = base_pixel + pixel;
634 /* calc error */
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;
644 nerr[x]+=(rer*5)/16;
645 nerr[x+1]+=(ger*5)/16;
646 nerr[x+2]+=(ber*5)/16;
648 if (x>0) {
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;
654 nerr[x+3*1]+=rer/16;
655 nerr[x+1+3*1]+=ger/16;
656 nerr[x+2+3*1]+=ber/16;
658 /* skip to next line */
659 terr = err;
660 err = nerr;
661 nerr = terr;
663 ofs += ximg->image->bytes_per_line - image->width;
665 free(err);
666 free(nerr);
668 ximg->image->data = (char*)data;
670 return ximg;
675 static RXImage*
676 image2GrayScale(RContext *ctx, RImage *image)
678 RXImage *ximg;
679 register int x, y, g;
680 unsigned char *ptr;
681 const int cpc=ctx->attribs->colors_per_channel;
682 unsigned short gmask;
683 unsigned short *table;
684 unsigned char *data;
685 int channels = image->format == RRGBAFormat ? 4 : 3;
687 /*register unsigned char maxrgb = 0xff;*/
689 ximg = RCreateXImage(ctx, ctx->depth, image->width, image->height);
690 if (!ximg) {
691 return NULL;
694 ptr = image->data;
696 data = (unsigned char *)ximg->image->data;
698 if (ctx->vclass == StaticGray)
699 gmask = (1<<ctx->depth) - 1; /* use all grays */
700 else
701 gmask = cpc*cpc*cpc-1;
703 table = computeTable(gmask);
705 if (table==NULL) {
706 RErrorCode = RERR_NOMEMORY;
707 RDestroyXImage(ctx, ximg);
708 return NULL;
711 if (ctx->attribs->render_mode == RBestMatchRendering) {
712 /* fake match */
713 #ifdef DEBUG
714 printf("grayscale match with %d colors per channel\n", cpc);
715 #endif
716 for (y=0; y<image->height; y++) {
717 for (x=0; x<image->width; x++) {
718 /* reduce pixel */
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);
725 } else {
726 /* dither */
727 short *gerr;
728 short *ngerr;
729 short *terr;
730 int ger;
731 const int dg=0xff/gmask;
733 #ifdef DEBUG
734 printf("grayscale dither with %d colors per channel\n", cpc);
735 #endif
736 gerr = (short*)malloc((image->width+2)*sizeof(short));
737 ngerr = (short*)malloc((image->width+2)*sizeof(short));
738 if (!gerr || !ngerr) {
739 if (ngerr)
740 free(ngerr);
741 RErrorCode = RERR_NOMEMORY;
742 RDestroyXImage(ctx, ximg);
743 return NULL;
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;
748 gerr[x] = 0;
749 /* convert and dither the image to XImage */
750 for (y=0; y<image->height; y++) {
751 if (y<image->height-1) {
752 int x1;
753 for (x=0, x1=(y+1)*image->width*3;
754 x<image->width;
755 x1+=channels-3) {
756 ngerr[x] = (ptr[x1++]*30 + ptr[x1++]*59 + ptr[x1++]*11)/100;
758 /* last column */
759 x1-=channels;
760 ngerr[x] = (ptr[x1++]*30 + ptr[x1++]*59 + ptr[x1++]*11)/100;
762 for (x=0; x<image->width; x++) {
763 /* reduce pixel */
764 if (gerr[x]>0xff) gerr[x]=0xff; else if (gerr[x]<0) gerr[x]=0;
766 g = table[gerr[x]];
768 /*data[ofs] = ctx->colors[g].pixel;*/
769 XPutPixel(ximg->image, x, y, ctx->colors[g].pixel);
770 /* calc error */
771 ger = gerr[x] - g*dg;
773 /* distribute error */
774 g = (ger*3)/8;
775 /* x+1, y */
776 gerr[x+1]+=g;
777 /* x, y+1 */
778 ngerr[x]+=g;
779 /* x+1, y+1 */
780 ngerr[x+1]+=ger-2*g;
782 /* skip to next line */
783 terr = gerr;
784 gerr = ngerr;
785 ngerr = terr;
787 free(gerr);
788 free(ngerr);
790 ximg->image->data = (char*)data;
792 return ximg;
796 static RXImage*
797 image2Bitmap(RContext *ctx, RImage *image, int threshold)
799 RXImage *ximg;
800 unsigned char *alpha;
801 int x, y;
803 ximg = RCreateXImage(ctx, 1, image->width, image->height);
804 if (!ximg) {
805 return NULL;
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));
812 alpha+=4;
816 return ximg;
821 int
822 RConvertImage(RContext *context, RImage *image, Pixmap *pixmap)
824 RXImage *ximg=NULL;
825 #ifdef XSHM
826 Pixmap tmp;
827 #endif
829 assert(context!=NULL);
830 assert(image!=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) {
841 #ifdef BENCH
842 cycle_bench(1);
843 #endif
844 if (context->attribs->standard_colormap_mode != RIgnoreStdColormap)
845 ximg = image2StandardPseudoColor(context, image);
846 else
847 ximg = image2PseudoColor(context, image);
848 #ifdef BENCH
849 cycle_bench(0);
850 #endif
851 } else if (context->vclass == GrayScale || context->vclass == StaticGray) {
853 ximg = image2GrayScale(context, image);
856 if (!ximg) {
857 return False;
861 *pixmap = XCreatePixmap(context->dpy, context->drawable, image->width,
862 image->height, context->depth);
864 #ifdef XSHM
865 if (context->flags.use_shared_pixmap && ximg->is_shared)
866 tmp = R_CreateXImageMappedPixmap(context, ximg);
867 else
868 tmp = None;
869 if (tmp) {
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
878 * wmaker.
880 XCopyArea(context->dpy, tmp, *pixmap, context->copy_gc, 0, 0,
881 image->width, image->height, 0, 0);
882 XFreePixmap(context->dpy, tmp);
883 } else {
884 RPutXImage(context, *pixmap, context->copy_gc, ximg, 0, 0, 0, 0,
885 image->width, image->height);
887 #else /* !XSHM */
888 RPutXImage(context, *pixmap, context->copy_gc, ximg, 0, 0, 0, 0,
889 image->width, image->height);
890 #endif /* !XSHM */
892 RDestroyXImage(context, ximg);
894 return True;
898 int
899 RConvertImageMask(RContext *context, RImage *image, Pixmap *pixmap,
900 Pixmap *mask, int threshold)
902 GC gc;
903 XGCValues gcv;
904 RXImage *ximg=NULL;
906 assert(context!=NULL);
907 assert(image!=NULL);
908 assert(pixmap!=NULL);
909 assert(mask!=NULL);
911 if (!RConvertImage(context, image, pixmap))
912 return False;
914 if (image->format==RRGBFormat) {
915 *mask = None;
916 return True;
919 ximg = image2Bitmap(context, image, threshold);
921 if (!ximg) {
922 return False;
924 *mask = XCreatePixmap(context->dpy, context->drawable, image->width,
925 image->height, 1);
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);
935 return True;
939 Bool
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;
984 return False;
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;
996 } else {
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;
1003 int index;
1005 rtable = computeTable(rmask);
1006 gtable = computeTable(gmask);
1007 btable = computeTable(bmask);
1009 if (rtable==NULL || gtable==NULL || btable==NULL) {
1010 RErrorCode = RERR_NOMEMORY;
1011 return False;
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;
1023 int index;
1025 if (context->vclass == StaticGray)
1026 gmask = (1<<context->depth) - 1; /* use all grays */
1027 else
1028 gmask = cpc*cpc*cpc-1;
1030 table = computeTable(gmask);
1031 if (!table)
1032 return False;
1034 index = table[(color->red*30 + color->green*59 + color->blue*11)/100];
1036 *retColor = context->colors[index];
1037 } else {
1038 RErrorCode = RERR_INTERNAL;
1039 return False;
1042 return True;