- fixed code in wrlib dependant on the order of evaluation.
[wmaker-crm.git] / wrlib / convert.c
blob547e275721a535f40ea9210c709c4dd603b5df5f
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
45 extern void x86_PseudoColor_32_to_8(unsigned char *image,
46 unsigned char *ximage,
47 char *err, char *nerr,
48 short *ctable,
49 int dr, int dg, int db,
50 unsigned long *pixels,
51 int cpc,
52 int width, int height,
53 int bytesPerPixel,
54 int line_offset);
55 #endif /* ASM_X86 */
57 #ifdef ASM_X86_MMX
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,
65 short *btable,
66 int dr, int dg, int db,
67 unsigned int roffs,
68 unsigned int goffs,
69 unsigned int boffs,
70 int width, int height,
71 int line_offset);
75 #endif /* ASM_X86_MMX */
79 typedef struct RConversionTable {
80 unsigned short table[256];
81 unsigned short index;
83 struct RConversionTable *next;
84 } RConversionTable;
87 typedef struct RStdConversionTable {
88 unsigned int table[256];
90 unsigned short mult;
91 unsigned short max;
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;
106 int i;
108 while (tmp) {
109 if (tmp->index == mask)
110 break;
111 tmp = tmp->next;
114 if (tmp)
115 return tmp->table;
117 tmp = (RConversionTable *)malloc(sizeof(RConversionTable));
118 if (tmp == NULL)
119 return NULL;
121 for (i=0;i<256;i++)
122 tmp->table[i] = (i*mask + 0x7f)/0xff;
124 tmp->index = mask;
125 tmp->next = conversionTable;
126 conversionTable = tmp;
127 return tmp->table;
131 static unsigned int*
132 computeStdTable(unsigned int mult, unsigned int max)
134 RStdConversionTable *tmp = stdConversionTable;
135 unsigned int i;
137 while (tmp) {
138 if (tmp->mult == mult && tmp->max == max)
139 break;
140 tmp = tmp->next;
143 if (tmp)
144 return tmp->table;
146 tmp = (RStdConversionTable *)malloc(sizeof(RStdConversionTable));
147 if (tmp == NULL)
148 return NULL;
150 for (i=0; i<256; i++) {
151 tmp->table[i] = (i*max)/0xff * mult;
153 tmp->mult = mult;
154 tmp->max = max;
156 tmp->next = stdConversionTable;
157 stdConversionTable = tmp;
159 return tmp->table;
162 /***************************************************************************/
165 static void
166 convertTrueColor_generic(RXImage *ximg, RImage *image,
167 char *err, char *nerr,
168 const short *rtable,
169 const short *gtable,
170 const short *btable,
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)
176 char *terr;
177 int x, y, r, g, b;
178 int pixel;
179 int rer, ger, ber;
180 unsigned char *ptr = image->data;
181 int channels = image->format == RRGBAFormat ? 4 : 3;
184 /* convert and dither the image to XImage */
185 for (y=0; y<image->height; y++) {
186 nerr[0] = 0;
187 nerr[1] = 0;
188 nerr[2] = 0;
189 for (x=0; x<image->width; x++, ptr+=channels) {
191 /* reduce pixel */
192 pixel = *ptr + err[x];
193 if (pixel<0) pixel=0; else if (pixel>0xff) pixel=0xff;
194 r = rtable[pixel];
195 /* calc error */
196 rer = pixel - r*dr;
198 /* reduce pixel */
199 pixel = *(ptr+1) + err[x+1];
200 if (pixel<0) pixel=0; else if (pixel>0xff) pixel=0xff;
201 g = gtable[pixel];
202 /* calc error */
203 ger = pixel - g*dg;
205 /* reduce pixel */
206 pixel = *(ptr+2) + err[x+2];
207 if (pixel<0) pixel=0; else if (pixel>0xff) pixel=0xff;
208 b = btable[pixel];
209 /* calc error */
210 ber = pixel - b*db;
213 pixel = (r<<roffs) | (g<<goffs) | (b<<boffs);
214 XPutPixel(ximg->image, x, y, pixel);
216 /* distribute error */
217 r = (rer*3)/8;
218 g = (ger*3)/8;
219 b = (ber*3)/8;
220 /* x+1, y */
221 err[x+3*1]+=r;
222 err[x+1+3*1]+=g;
223 err[x+2+3*1]+=b;
224 /* x, y+1 */
225 nerr[x]+=r;
226 nerr[x+1]+=g;
227 nerr[x+2]+=b;
228 /* x+1, y+1 */
229 nerr[x+3*1]=rer-2*r;
230 nerr[x+1+3*1]=ger-2*g;
231 nerr[x+2+3*1]=ber-2*b;
233 /* skip to next line */
234 terr = err;
235 err = nerr;
236 nerr = terr;
243 static RXImage*
244 image2TrueColor(RContext *ctx, RImage *image)
246 RXImage *ximg;
247 unsigned short rmask, gmask, bmask;
248 unsigned short roffs, goffs, boffs;
249 unsigned short *rtable, *gtable, *btable;
250 int channels = (image->format == RRGBAFormat ? 4 : 3);
252 ximg = RCreateXImage(ctx, ctx->depth, image->width, image->height);
253 if (!ximg) {
254 return NULL;
257 roffs = ctx->red_offset;
258 goffs = ctx->green_offset;
259 boffs = ctx->blue_offset;
261 rmask = ctx->visual->red_mask >> roffs;
262 gmask = ctx->visual->green_mask >> goffs;
263 bmask = ctx->visual->blue_mask >> boffs;
265 rtable = computeTable(rmask);
266 gtable = computeTable(gmask);
267 btable = computeTable(bmask);
269 if (rtable==NULL || gtable==NULL || btable==NULL) {
270 RErrorCode = RERR_NOMEMORY;
271 RDestroyXImage(ctx, ximg);
272 return NULL;
276 #ifdef BENCH
277 cycle_bench(1);
278 #endif
280 if (ctx->attribs->render_mode==RBestMatchRendering) {
281 int ofs, r, g, b;
282 int x, y;
283 unsigned long pixel;
284 unsigned char *ptr = image->data;
286 /* fake match */
287 #ifdef DEBUG
288 puts("true color match");
289 #endif
290 for (y=0, ofs=0; y < image->height; y++) {
291 for (x=0; x < image->width; x++, ofs+=channels-3) {
292 /* reduce pixel */
293 r = rtable[ptr[ofs++]];
294 g = gtable[ptr[ofs++]];
295 b = btable[ptr[ofs++]];
296 pixel = (r<<roffs) | (g<<goffs) | (b<<boffs);
297 XPutPixel(ximg->image, x, y, pixel);
300 } else {
301 /* dither */
302 const int dr=0xff/rmask;
303 const int dg=0xff/gmask;
304 const int db=0xff/bmask;
306 #ifdef DEBUG
307 puts("true color dither");
308 #endif
310 #ifdef ASM_X86_MMX
311 if (ctx->depth == 16 && image->format == RRGBAFormat
312 && x86_check_mmx()) {
313 short *err;
314 short *nerr;
316 err = malloc(8*(image->width+3));
317 nerr = malloc(8*(image->width+3));
318 if (!err || !nerr) {
319 if (nerr)
320 free(nerr);
321 RErrorCode = RERR_NOMEMORY;
322 RDestroyXImage(ctx, ximg);
323 return NULL;
325 memset(err, 0, 8*(image->width+3));
326 memset(nerr, 0, 8*(image->width+3));
328 x86_mmx_TrueColor_32_to_16(image->data,
329 (unsigned short*)ximg->image->data,
330 err+8, nerr+8,
331 rtable, gtable, btable,
332 dr, dg, db,
333 roffs, goffs, boffs,
334 image->width, image->height,
335 ximg->image->bytes_per_line - 2*image->width);
337 free(err);
338 free(nerr);
339 } else
340 #endif /* ASM_X86_MMX */
342 char *err;
343 char *nerr;
344 int ch = image->format == RRGBAFormat ? 4 : 3;
346 err = malloc(ch*(image->width+2));
347 nerr = malloc(ch*(image->width+2));
348 if (!err || !nerr) {
349 if (nerr)
350 free(nerr);
351 RErrorCode = RERR_NOMEMORY;
352 RDestroyXImage(ctx, ximg);
353 return NULL;
356 memset(err, 0, ch*(image->width+2));
357 memset(nerr, 0, ch*(image->width+2));
359 convertTrueColor_generic(ximg, image, err, nerr,
360 rtable, gtable, btable,
361 dr, dg, db, roffs, goffs, boffs);
362 free(err);
363 free(nerr);
368 #ifdef BENCH
369 cycle_bench(0);
370 #endif
372 return ximg;
376 /***************************************************************************/
378 static void
379 convertPseudoColor_to_8(RXImage *ximg, RImage *image,
380 char *err, char *nerr,
381 const short *rtable,
382 const short *gtable,
383 const short *btable,
384 const int dr, const int dg, const int db,
385 unsigned long *pixels,
386 int cpc)
388 char *terr;
389 int x, y, r, g, b;
390 int pixel;
391 int rer, ger, ber;
392 unsigned char *ptr = image->data;
393 unsigned char *optr = ximg->image->data;
394 int channels = image->format == RRGBAFormat ? 4 : 3;
395 int cpcpc = cpc*cpc;
397 /* convert and dither the image to XImage */
398 for (y=0; y<image->height; y++) {
399 nerr[0] = 0;
400 nerr[1] = 0;
401 nerr[2] = 0;
402 for (x=0; x<image->width*3; x+=3, ptr+=channels) {
404 /* reduce pixel */
405 pixel = *ptr + err[x];
406 if (pixel<0) pixel=0; else if (pixel>0xff) pixel=0xff;
407 r = rtable[pixel];
408 /* calc error */
409 rer = pixel - r*dr;
411 /* reduce pixel */
412 pixel = *(ptr+1) + err[x+1];
413 if (pixel<0) pixel=0; else if (pixel>0xff) pixel=0xff;
414 g = gtable[pixel];
415 /* calc error */
416 ger = pixel - g*dg;
418 /* reduce pixel */
419 pixel = *(ptr+2) + err[x+2];
420 if (pixel<0) pixel=0; else if (pixel>0xff) pixel=0xff;
421 b = btable[pixel];
422 /* calc error */
423 ber = pixel - b*db;
425 *optr++ = pixels[r*cpcpc + g*cpc + b];
427 /* distribute error */
428 r = (rer*3)/8;
429 g = (ger*3)/8;
430 b = (ber*3)/8;
432 /* x+1, y */
433 err[x+3*1]+=r;
434 err[x+1+3*1]+=g;
435 err[x+2+3*1]+=b;
436 /* x, y+1 */
437 nerr[x]+=r;
438 nerr[x+1]+=g;
439 nerr[x+2]+=b;
440 /* x+1, y+1 */
441 nerr[x+3*1]=rer-2*r;
442 nerr[x+1+3*1]=ger-2*g;
443 nerr[x+2+3*1]=ber-2*b;
445 /* skip to next line */
446 terr = err;
447 err = nerr;
448 nerr = terr;
450 optr += ximg->image->bytes_per_line - image->width;
456 static RXImage*
457 image2PseudoColor(RContext *ctx, RImage *image)
459 RXImage *ximg;
460 register int x, y, r, g, b;
461 unsigned char *ptr;
462 unsigned long pixel;
463 const int cpc=ctx->attribs->colors_per_channel;
464 const unsigned short rmask = cpc-1; /* different sizes could be used */
465 const unsigned short gmask = rmask; /* for r,g,b */
466 const unsigned short bmask = rmask;
467 unsigned short *rtable, *gtable, *btable;
468 const int cpccpc = cpc*cpc;
469 int channels = image->format == RRGBAFormat ? 4 : 3;
471 ximg = RCreateXImage(ctx, ctx->depth, image->width, image->height);
472 if (!ximg) {
473 return NULL;
476 ptr = image->data;
478 /* Tables are same at the moment because rmask==gmask==bmask. */
479 rtable = computeTable(rmask);
480 gtable = computeTable(gmask);
481 btable = computeTable(bmask);
483 if (rtable==NULL || gtable==NULL || btable==NULL) {
484 RErrorCode = RERR_NOMEMORY;
485 RDestroyXImage(ctx, ximg);
486 return NULL;
489 if (ctx->attribs->render_mode == RBestMatchRendering) {
490 /* fake match */
491 #ifdef DEBUG
492 printf("pseudo color match with %d colors per channel\n", cpc);
493 #endif
494 for (y=0; y<image->height; y++) {
495 for (x=0; x<image->width; x++, ptr+=channels-1) {
496 /* reduce pixel */
497 r = rtable[*ptr++];
498 g = gtable[*ptr++];
499 b = btable[*ptr++];
500 pixel = r*cpccpc + g*cpc + b;
501 /*data[ofs] = ctx->colors[pixel].pixel;*/
502 XPutPixel(ximg->image, x, y, ctx->colors[pixel].pixel);
505 } else {
506 /* dither */
507 char *err;
508 char *nerr;
509 const int dr=0xff/rmask;
510 const int dg=0xff/gmask;
511 const int db=0xff/bmask;
514 #ifdef DEBUG
515 printf("pseudo color dithering with %d colors per channel\n", cpc);
516 #endif
517 err = malloc(4*(image->width+3));
518 nerr = malloc(4*(image->width+3));
519 if (!err || !nerr) {
520 if (nerr)
521 free(nerr);
522 RErrorCode = RERR_NOMEMORY;
523 RDestroyXImage(ctx, ximg);
524 return NULL;
526 memset(err, 0, 4*(image->width+3));
527 memset(nerr, 0, 4*(image->width+3));
529 /*#ifdef ASM_X86*/
530 #if 0
531 x86_PseudoColor_32_to_8(image->data, ximg->image->data,
532 err+4, nerr+4,
533 rtable,
534 dr, dg, db, ctx->pixels, cpc,
535 image->width, image->height,
536 channels,
537 ximg->image->bytes_per_line - image->width);
538 #else
539 convertPseudoColor_to_8(ximg, image, err+4, nerr+4,
540 rtable, gtable, btable,
541 dr, dg, db, ctx->pixels, cpc);
542 #endif
544 free(err);
545 free(nerr);
548 return ximg;
553 * For standard colormap
555 static RXImage*
556 image2StandardPseudoColor(RContext *ctx, RImage *image)
558 RXImage *ximg;
559 register int x, y, r, g, b;
560 unsigned char *ptr;
561 unsigned long pixel;
562 unsigned char *data;
563 unsigned int *rtable, *gtable, *btable;
564 unsigned int base_pixel = ctx->std_rgb_map->base_pixel;
565 int channels = image->format == RRGBAFormat ? 4 : 3;
566 /*register unsigned char maxrgb = 0xff;*/
568 ximg = RCreateXImage(ctx, ctx->depth, image->width, image->height);
569 if (!ximg) {
570 return NULL;
573 ptr = image->data;
575 data = (unsigned char *)ximg->image->data;
578 rtable = computeStdTable(ctx->std_rgb_map->red_mult,
579 ctx->std_rgb_map->red_max);
581 gtable = computeStdTable(ctx->std_rgb_map->green_mult,
582 ctx->std_rgb_map->green_max);
584 btable = computeStdTable(ctx->std_rgb_map->blue_mult,
585 ctx->std_rgb_map->blue_max);
587 if (rtable==NULL || gtable==NULL || btable==NULL) {
588 RErrorCode = RERR_NOMEMORY;
589 RDestroyXImage(ctx, ximg);
590 return NULL;
594 if (ctx->attribs->render_mode == RBestMatchRendering) {
595 for (y=0; y<image->height; y++) {
596 for (x=0; x<image->width; x++, ptr+=channels) {
597 /* reduce pixel */
598 pixel = (rtable[*ptr] + gtable[*(ptr+1)]
599 + btable[*(ptr+2)] + base_pixel) & 0xffffffff;
601 XPutPixel(ximg->image, x, y, pixel);
604 } else {
605 /* dither */
606 short *err, *nerr;
607 short *terr;
608 int rer, ger, ber;
609 int x1, ofs;
611 #ifdef DEBUG
612 printf("pseudo color dithering with %d colors per channel\n", cpc);
613 #endif
614 err = (short*)malloc(3*(image->width+2)*sizeof(short));
615 nerr = (short*)malloc(3*(image->width+2)*sizeof(short));
616 if (!err || !nerr) {
617 if (nerr)
618 free(nerr);
619 RErrorCode = RERR_NOMEMORY;
620 RDestroyXImage(ctx, ximg);
621 return NULL;
623 for (x=0, x1=0; x<image->width*3; x1+=channels-3) {
624 err[x++] = ptr[x1++];
625 err[x++] = ptr[x1++];
626 err[x++] = ptr[x1++];
628 err[x] = err[x+1] = err[x+2] = 0;
629 x += 3;
630 /* convert and dither the image to XImage */
631 for (y=0, ofs=0; y<image->height; y++) {
632 if (y<image->height-1) {
633 int x1;
634 for (x=0, x1=(y+1)*image->width*channels;
635 x<image->width*3;
636 x1+=channels-3) {
637 nerr[x++] = ptr[x1++];
638 nerr[x++] = ptr[x1++];
639 nerr[x++] = ptr[x1++];
641 /* last column */
642 x1-=channels;
643 nerr[x++] = ptr[x1++];
644 nerr[x++] = ptr[x1++];
645 nerr[x++] = ptr[x1++];
647 for (x=0; x<image->width*3; x+=3, ofs++) {
648 /* reduce pixel */
649 if (err[x]>0xff) err[x]=0xff; else if (err[x]<0) err[x]=0;
650 if (err[x+1]>0xff) err[x+1]=0xff; else if (err[x+1]<0) err[x+1]=0;
651 if (err[x+2]>0xff) err[x+2]=0xff; else if (err[x+2]<0) err[x+2]=0;
653 r = rtable[err[x]];
654 g = gtable[err[x+1]];
655 b = btable[err[x+2]];
657 pixel = r + g + b;
659 data[ofs] = base_pixel + pixel;
661 /* calc error */
662 rer = err[x] - (ctx->colors[pixel].red>>8);
663 ger = err[x+1] - (ctx->colors[pixel].green>>8);
664 ber = err[x+2] - (ctx->colors[pixel].blue>>8);
666 /* distribute error */
667 err[x+3*1]+=(rer*7)/16;
668 err[x+1+3*1]+=(ger*7)/16;
669 err[x+2+3*1]+=(ber*7)/16;
671 nerr[x]+=(rer*5)/16;
672 nerr[x+1]+=(ger*5)/16;
673 nerr[x+2]+=(ber*5)/16;
675 if (x>0) {
676 nerr[x-3*1]+=(rer*3)/16;
677 nerr[x-3*1+1]+=(ger*3)/16;
678 nerr[x-3*1+2]+=(ber*3)/16;
681 nerr[x+3*1]+=rer/16;
682 nerr[x+1+3*1]+=ger/16;
683 nerr[x+2+3*1]+=ber/16;
685 /* skip to next line */
686 terr = err;
687 err = nerr;
688 nerr = terr;
690 ofs += ximg->image->bytes_per_line - image->width;
692 free(err);
693 free(nerr);
695 ximg->image->data = (char*)data;
697 return ximg;
702 static RXImage*
703 image2GrayScale(RContext *ctx, RImage *image)
705 RXImage *ximg;
706 register int x, y, g;
707 unsigned char *ptr;
708 const int cpc=ctx->attribs->colors_per_channel;
709 unsigned short gmask;
710 unsigned short *table;
711 unsigned char *data;
712 int channels = image->format == RRGBAFormat ? 4 : 3;
714 /*register unsigned char maxrgb = 0xff;*/
716 ximg = RCreateXImage(ctx, ctx->depth, image->width, image->height);
717 if (!ximg) {
718 return NULL;
721 ptr = image->data;
723 data = (unsigned char *)ximg->image->data;
725 if (ctx->vclass == StaticGray)
726 gmask = (1<<ctx->depth) - 1; /* use all grays */
727 else
728 gmask = cpc*cpc*cpc-1;
730 table = computeTable(gmask);
732 if (table==NULL) {
733 RErrorCode = RERR_NOMEMORY;
734 RDestroyXImage(ctx, ximg);
735 return NULL;
738 if (ctx->attribs->render_mode == RBestMatchRendering) {
739 /* fake match */
740 #ifdef DEBUG
741 printf("grayscale match with %d colors per channel\n", cpc);
742 #endif
743 for (y=0; y<image->height; y++) {
744 for (x=0; x<image->width; x++) {
745 /* reduce pixel */
746 g = table[(*ptr * 30 + *(ptr+1) * 59 + *(ptr+2) * 11)/100];
747 ptr += 3;
748 /*data[ofs] = ctx->colors[g].pixel;*/
749 XPutPixel(ximg->image, x, y, ctx->colors[g].pixel);
752 } else {
753 /* dither */
754 short *gerr;
755 short *ngerr;
756 short *terr;
757 int ger;
758 const int dg=0xff/gmask;
760 #ifdef DEBUG
761 printf("grayscale dither with %d colors per channel\n", cpc);
762 #endif
763 gerr = (short*)malloc((image->width+2)*sizeof(short));
764 ngerr = (short*)malloc((image->width+2)*sizeof(short));
765 if (!gerr || !ngerr) {
766 if (ngerr)
767 free(ngerr);
768 RErrorCode = RERR_NOMEMORY;
769 RDestroyXImage(ctx, ximg);
770 return NULL;
772 for (x=0; x<image->width; x++) {
773 gerr[x] = (ptr[x*3]*30 + ptr[x*3+1]*59 + ptr[x*3+2]*11)/100;
775 gerr[x] = 0;
776 /* convert and dither the image to XImage */
777 for (y=0; y<image->height; y++) {
778 if (y<image->height-1) {
779 int x1;
780 for (x=0, x1=(y+1)*image->width*3; x<image->width; x1+=channels) {
781 ngerr[x] = (ptr[x1]*30 + ptr[x1+1]*59 + ptr[x1+2]*11)/100;
783 /* last column */
784 x1-=channels;
785 ngerr[x] = (ptr[x1]*30 + ptr[x1+1]*59 + ptr[x1+2]*11)/100;
787 for (x=0; x<image->width; x++) {
788 /* reduce pixel */
789 if (gerr[x]>0xff) gerr[x]=0xff; else if (gerr[x]<0) gerr[x]=0;
791 g = table[gerr[x]];
793 /*data[ofs] = ctx->colors[g].pixel;*/
794 XPutPixel(ximg->image, x, y, ctx->colors[g].pixel);
795 /* calc error */
796 ger = gerr[x] - g*dg;
798 /* distribute error */
799 g = (ger*3)/8;
800 /* x+1, y */
801 gerr[x+1]+=g;
802 /* x, y+1 */
803 ngerr[x]+=g;
804 /* x+1, y+1 */
805 ngerr[x+1]+=ger-2*g;
807 /* skip to next line */
808 terr = gerr;
809 gerr = ngerr;
810 ngerr = terr;
812 free(gerr);
813 free(ngerr);
815 ximg->image->data = (char*)data;
817 return ximg;
821 static RXImage*
822 image2Bitmap(RContext *ctx, RImage *image, int threshold)
824 RXImage *ximg;
825 unsigned char *alpha;
826 int x, y;
828 ximg = RCreateXImage(ctx, 1, image->width, image->height);
829 if (!ximg) {
830 return NULL;
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));
837 alpha+=4;
841 return ximg;
846 int
847 RConvertImage(RContext *context, RImage *image, Pixmap *pixmap)
849 RXImage *ximg=NULL;
850 #ifdef XSHM
851 Pixmap tmp;
852 #endif
854 assert(context!=NULL);
855 assert(image!=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) {
866 #ifdef BENCH
867 cycle_bench(1);
868 #endif
869 if (context->attribs->standard_colormap_mode != RIgnoreStdColormap)
870 ximg = image2StandardPseudoColor(context, image);
871 else
872 ximg = image2PseudoColor(context, image);
873 #ifdef BENCH
874 cycle_bench(0);
875 #endif
876 } else if (context->vclass == GrayScale || context->vclass == StaticGray) {
878 ximg = image2GrayScale(context, image);
881 if (!ximg) {
882 return False;
886 *pixmap = XCreatePixmap(context->dpy, context->drawable, image->width,
887 image->height, context->depth);
889 #ifdef XSHM
890 if (context->flags.use_shared_pixmap && ximg->is_shared)
891 tmp = R_CreateXImageMappedPixmap(context, ximg);
892 else
893 tmp = None;
894 if (tmp) {
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
903 * wmaker.
905 XCopyArea(context->dpy, tmp, *pixmap, context->copy_gc, 0, 0,
906 image->width, image->height, 0, 0);
907 XFreePixmap(context->dpy, tmp);
908 } else {
909 RPutXImage(context, *pixmap, context->copy_gc, ximg, 0, 0, 0, 0,
910 image->width, image->height);
912 #else /* !XSHM */
913 RPutXImage(context, *pixmap, context->copy_gc, ximg, 0, 0, 0, 0,
914 image->width, image->height);
915 #endif /* !XSHM */
917 RDestroyXImage(context, ximg);
919 return True;
923 int
924 RConvertImageMask(RContext *context, RImage *image, Pixmap *pixmap,
925 Pixmap *mask, int threshold)
927 GC gc;
928 XGCValues gcv;
929 RXImage *ximg=NULL;
931 assert(context!=NULL);
932 assert(image!=NULL);
933 assert(pixmap!=NULL);
934 assert(mask!=NULL);
936 if (!RConvertImage(context, image, pixmap))
937 return False;
939 if (image->format==RRGBFormat) {
940 *mask = None;
941 return True;
944 ximg = image2Bitmap(context, image, threshold);
946 if (!ximg) {
947 return False;
949 *mask = XCreatePixmap(context->dpy, context->drawable, image->width,
950 image->height, 1);
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);
960 return True;
964 Bool
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;
1009 return False;
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;
1021 } else {
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;
1028 int index;
1030 rtable = computeTable(rmask);
1031 gtable = computeTable(gmask);
1032 btable = computeTable(bmask);
1034 if (rtable==NULL || gtable==NULL || btable==NULL) {
1035 RErrorCode = RERR_NOMEMORY;
1036 return False;
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;
1048 int index;
1050 if (context->vclass == StaticGray)
1051 gmask = (1<<context->depth) - 1; /* use all grays */
1052 else
1053 gmask = cpc*cpc*cpc-1;
1055 table = computeTable(gmask);
1056 if (!table)
1057 return False;
1059 index = table[(color->red*30 + color->green*59 + color->blue*11)/100];
1061 *retColor = context->colors[index];
1062 } else {
1063 RErrorCode = RERR_INTERNAL;
1064 return False;
1067 return True;