4 * send an Image to an X pixmap
9 /* extra colors to try allocating in private color maps to minimise flashing */
10 #define NOFLASH_COLORS 256
12 Image *monochrome(Image *cimage)
15 unsigned char *sp, *dp, *dp2; /* data pointers */
16 unsigned int spl; /* source pixel length in bytes */
17 unsigned int dll; /* destination line length in bytes */
18 Pixel color; /* pixel color */
19 unsigned int x, y; /* random counters */
25 printf(" Converting to monochrome...");
28 image= newBitImage(cimage->width, cimage->height);
31 image->title= (char *)lmalloc(strlen(cimage->title) + 13);
32 sprintf(image->title, "%s (monochrome)", cimage->title);
36 dll = (image->width / 8) + (image->width % 8 ? 1 : 0);
41 for (y= 0; y < cimage->height; y++)
43 for (x= 0; x < cimage->width; x++)
45 dp2 = dp + (x / 8); /* dp + x/8 */
46 color= memToVal(sp, spl);
48 if (cimage->rgb.red[color] > 0x0000 ||
49 cimage->rgb.green[color] > 0x0000 ||
50 cimage->rgb.blue[color] > 0x0000)
55 *dp2 |= bitmap_pixel >> ( x % 8);
59 dp += dll; /* next row */
67 static unsigned int *buildIndex(unsigned int width,
82 fzoom= (float)zoom / 100.0;
83 *rwidth= (unsigned int)(fzoom * width + 0.5);
85 index= (unsigned int *)lmalloc(sizeof(unsigned int) * *rwidth);
86 for (a= 0; a < *rwidth; a++)
89 *(index + a)= (unsigned int)((float)a / fzoom + 0.5);
96 Image *zoom(Image *oimage, unsigned int xzoom, unsigned int yzoom)
99 unsigned int *xindex, *yindex;
100 unsigned int xwidth, ywidth;
101 unsigned int x, y, xsrc, ysrc;
103 unsigned int srclinelen;
104 unsigned int destlinelen;
105 byte *srcline, *srcptr;
106 byte *destline, *destptr;
107 byte srcmask, destmask, bit;
110 if ((!xzoom || xzoom==100) && (!yzoom || yzoom==100))
114 printf(" Zooming image Y axis by %d%%...", yzoom);
116 printf(" Zooming image X axis by %d%%...", xzoom);
117 else if (xzoom == yzoom)
118 printf(" Zooming image by %d%%...", xzoom);
120 printf(" Zooming image X axis by %d%% and Y axis by %d%%...",
124 xindex= buildIndex(oimage->width, xzoom, &xwidth);
125 yindex= buildIndex(oimage->height, yzoom, &ywidth);
127 switch (oimage->type)
130 image= newBitImage(xwidth, ywidth);
131 for (x= 0; x < oimage->rgb.used; x++)
133 *(image->rgb.red + x)= *(oimage->rgb.red + x);
134 *(image->rgb.green + x)= *(oimage->rgb.green + x);
135 *(image->rgb.blue + x)= *(oimage->rgb.blue + x);
137 image->rgb.used= oimage->rgb.used;
138 destline= image->data;
139 destlinelen= (xwidth / 8) + (xwidth % 8 ? 1 : 0);
140 srcline= oimage->data;
141 srclinelen= (oimage->width / 8) + (oimage->width % 8 ? 1 : 0);
142 for (y= 0, ysrc= *(yindex + y); y < ywidth; y++)
144 while (ysrc != *(yindex + y))
147 srcline += srclinelen;
153 bit= srcmask & *srcptr;
154 for (x= 0, xsrc= *(xindex + x); x < xwidth; x++)
156 if (xsrc != *(xindex + x))
161 if (!(srcmask >>= 1))
167 while (xsrc != *(xindex + x));
169 bit= srcmask & *srcptr;
172 *destptr |= destmask;
173 if (!(destmask >>= 1))
179 destline += destlinelen;
184 image= newRGBImage(xwidth, ywidth, oimage->depth);
185 for (x= 0; x < oimage->rgb.used; x++)
187 *(image->rgb.red + x)= *(oimage->rgb.red + x);
188 *(image->rgb.green + x)= *(oimage->rgb.green + x);
189 *(image->rgb.blue + x)= *(oimage->rgb.blue + x);
191 image->rgb.used= oimage->rgb.used;
193 pixlen= oimage->pixlen;
194 destptr= image->data;
195 srcline= oimage->data;
196 srclinelen= oimage->width * pixlen;
197 for (y= 0, ysrc= *(yindex + y); y < ywidth; y++)
199 while (ysrc != *(yindex + y))
202 srcline += srclinelen;
206 value = memToVal(srcptr, image->pixlen);
208 for (x=0, xsrc= *(xindex + x); x<xwidth; x++)
210 if (xsrc != *(xindex + x))
217 while (xsrc != *(xindex + x));
218 value = memToVal(srcptr, 1);
220 valToMem(value, destptr, 1);
232 image->title = dupString(oimage->title);
233 lfree((byte *)xindex);
234 lfree((byte *)yindex);
241 void compress(Image *image)
243 unsigned char *used, fast[32][32][32];
244 unsigned int dmask; /* Depth mask protection */
246 unsigned int next_index;
247 Intensity *red = image->rgb.red,
248 *green = image->rgb.green,
249 *blue = image->rgb.blue;
251 unsigned int x, y, badcount = 0, dupcount = 0, unusedcount = 0;
252 unsigned char *pixptr, *pixend;
254 if (!RGBP(image) || image->rgb.compressed)
257 printf(" Compressing colormap...");
260 used = (unsigned char *)lcalloc(sizeof(unsigned char) * depthToColors(image->depth));
261 dmask = (1 << image->depth) -1; /* Mask any illegal bits for that depth */
262 map = (Pixel *)lcalloc(sizeof(Pixel) * depthToColors(image->depth));
264 /* init fast duplicate check table */
270 /* do pass 1 through the image to check index usage */
272 pixptr = image->data;
273 pixend = pixptr + (image->height * image->width);
274 for(;pixptr < pixend; pixptr++)
275 used[(*pixptr) & dmask] = 1;
277 /* count the bad pixels */
278 for (x = image->rgb.used; x < depthToColors(image->depth); x++)
282 /* figure out duplicates and unuseds, and create the new mapping */
284 for (x = 0; x < image->rgb.used; x++)
289 continue; /* delete this index */
292 /* check for duplicate */
296 if (fast[r>>11][g>>11][b>>11]) /* if matches fast check */
298 /* then do a linear search */
299 for (y = x+1; y < image->rgb.used; y++)
301 if (r == red[y] && g == green[y] && b == blue[y])
304 if (y < image->rgb.used) /* found match */
308 continue; /* delete this index */
310 fast[r>>11][g>>11][b>>11] = 1;
312 /* will map to this index */
317 /* change the image pixels */
318 pixptr = image->data;
319 pixend = pixptr + (image->height * image->width);
320 for(;pixptr < pixend; pixptr++)
321 *pixptr = map[(*pixptr) & dmask];
323 /* change the colormap */
324 for (x = 0; x < image->rgb.used; x++)
328 red[map[x]] = red[x];
329 green[map[x]] = green[x];
330 blue[map[x]] = blue[x];
332 image->rgb.used = next_index;
339 printf("%d out-of-range pixels, ", badcount);
341 if (!unusedcount && !dupcount)
342 printf("no improvment\n");
346 printf("%d duplicate%s and %d unused color%s removed...",
347 dupcount, (dupcount == 1 ? "" : "s"),
348 unusedcount, (unusedcount == 1 ? "" : "s"));
349 printf("%d unique color%s\n",
350 next_index, (next_index == 1 ? "" : "s"));
353 image->rgb.compressed= TRUE; /* don't do it again */
359 Pixmap ximageToPixmap(Display *disp, Window parent, XImageInfo *ximageinfo)
363 pixmap = XCreatePixmap(disp, parent,
364 ximageinfo->ximage->width, ximageinfo->ximage->height,
367 ximageinfo->drawable = pixmap;
369 sendXImage(ximageinfo, 0, 0, 0, 0,
370 ximageinfo->ximage->width, ximageinfo->ximage->height);
374 /* find the best pixmap depth supported by the server for a particular
375 * visual and return that depth.
377 * this is complicated by R3's lack of XListPixmapFormats so we fake it
378 * by looking at the structure ourselves.
381 static unsigned int bitsPerPixelAtDepth(Display *disp, int scrn,
384 #if defined(XlibSpecificationRelease) && (XlibSpecificationRelease >= 4)
385 /* the way things are */
386 XPixmapFormatValues *xf;
389 xf = XListPixmapFormats(disp, &nxf);
390 for (a = 0; a < nxf; a++)
392 if (xf[a].depth == depth)
395 bpp = xf[a].bits_per_pixel;
397 return (unsigned int) bpp;
401 #else /* the way things were (X11R3) */
404 for (a= 0; a < disp->nformats; a++)
405 if (disp->pixmap_format[a].depth == depth)
406 return(disp->pixmap_format[a].bits_per_pixel);
409 /* this should never happen; if it does, we're in trouble
412 fprintf(stderr, "bitsPerPixelAtDepth: Can't find pixmap depth info!\n");
417 visual: visual to use
418 ddepth: depth of the visual to use
420 XImageInfo *imageToXImage(Display *disp,
426 Pixel *redvalue, *greenvalue, *bluevalue;
427 unsigned int a, c=0, x, y, linelen, dpixlen, dbits;
430 XImageInfo *ximageinfo;
433 static Colormap our_default_cmap = 0;
434 static Pixel *our_default_index;
435 static int free_cmap_entries, max_cmap_entries;
438 if (!our_default_cmap)
441 our_default_cmap = DefaultColormap(disp, scrn);
444 our_default_cmap = XCreateColormap(disp, RootWindow(disp, scrn),
446 our_default_index = (Pixel *)lmalloc(sizeof(Pixel) * NOFLASH_COLORS);
448 for (a=0; a<NOFLASH_COLORS; a++) /* count entries we got */
449 if (!XAllocColorCells(disp, our_default_cmap, FALSE, NULL, 0,
450 our_default_index + a, 1))
453 free_cmap_entries = max_cmap_entries = a;
455 printf("We've got %d colormap entries.\n", free_cmap_entries);
457 for(a=0; a<max_cmap_entries; a++) /* copy default colors */
459 xcolor.pixel = *(our_default_index + a);
460 XQueryColor(disp, DefaultColormap(disp, scrn), &xcolor);
461 XStoreColor(disp, our_default_cmap, &xcolor);
465 xcolor.flags= DoRed | DoGreen | DoBlue;
466 redvalue= greenvalue= bluevalue= NULL;
468 ximageinfo= (XImageInfo *)lmalloc(sizeof(XImageInfo));
469 ximageinfo->disp= disp;
470 ximageinfo->scrn= scrn;
471 ximageinfo->depth= 0;
472 ximageinfo->drawable= None;
473 ximageinfo->index= NULL;
474 ximageinfo->rootimage= FALSE; /* assume not */
475 ximageinfo->foreground= ximageinfo->background= 0;
476 ximageinfo->gc= NULL;
477 ximageinfo->ximage= NULL;
479 /* do color allocation
482 switch (visual->class)
488 unsigned int redcolors, greencolors, bluecolors;
489 unsigned int redstep, greenstep, bluestep;
490 unsigned int redbottom, greenbottom, bluebottom;
491 unsigned int redtop, greentop, bluetop;
493 redvalue= (Pixel *)lmalloc(sizeof(Pixel) * 256);
494 greenvalue= (Pixel *)lmalloc(sizeof(Pixel) * 256);
495 bluevalue= (Pixel *)lmalloc(sizeof(Pixel) * 256);
498 if (visual == DefaultVisual(disp, scrn))
499 ximageinfo->cmap= DefaultColormap(disp, scrn);
501 ximageinfo->cmap= XCreateColormap(disp, RootWindow(disp, scrn),
504 ximageinfo->cmap = our_default_cmap;
507 retry_direct: /* tag we hit if a DirectColor allocation fails on
508 * default colormap */
510 /* calculate number of distinct colors in each band
513 redcolors= greencolors= bluecolors= 1;
514 for (pixval= 1; pixval; pixval <<= 1)
516 if (pixval & visual->red_mask)
518 if (pixval & visual->green_mask)
520 if (pixval & visual->blue_mask)
527 if ((redcolors > visual->map_entries) ||
528 (greencolors > visual->map_entries) ||
529 (bluecolors > visual->map_entries))
531 fprintf(stderr, "Warning: inconsistency in color information (this may be ugly)\n");
534 redstep = 256 / redcolors;
535 greenstep = 256 / greencolors;
536 bluestep = 256 / bluecolors;
537 redbottom = greenbottom = bluebottom = 0;
538 redtop = greentop = bluetop = 0;
539 for (a= 0; a < visual->map_entries; a++)
542 redtop= redbottom + redstep;
543 if (greenbottom < 256)
544 greentop= greenbottom + greenstep;
545 if (bluebottom < 256)
546 bluetop= bluebottom + bluestep;
548 xcolor.red= (redtop - 1) << 8;
549 xcolor.green= (greentop - 1) << 8;
550 xcolor.blue= (bluetop - 1) << 8;
551 if (! XAllocColor(disp, ximageinfo->cmap, &xcolor))
553 /* if an allocation fails for a DirectColor default visual then
554 * we should create a private colormap and try again.
557 if ((visual->class == DirectColor) &&
558 (visual == DefaultVisual(disp, scrn)))
561 ximageinfo->cmap = XCreateColormap(disp, RootWindow(disp, scrn),
564 our_default_cmap = XCopyColormapAndFree(disp, our_default_cmap);
565 ximageinfo->cmap = our_default_cmap;
570 /* something completely unexpected happened
573 fprintf(stderr, "imageToXImage: XAllocColor failed on a TrueColor/Directcolor visual\n");
574 lfree((byte *)redvalue);
575 lfree((byte *)greenvalue);
576 lfree((byte *)bluevalue);
577 lfree((byte *)ximageinfo);
581 /* fill in pixel values for each band at this intensity
584 while ((redbottom < 256) && (redbottom < redtop))
585 redvalue[redbottom++]= xcolor.pixel & visual->red_mask;
586 while ((greenbottom < 256) && (greenbottom < greentop))
587 greenvalue[greenbottom++]= xcolor.pixel & visual->green_mask;
588 while ((bluebottom < 256) && (bluebottom < bluetop))
589 bluevalue[bluebottom++]= xcolor.pixel & visual->blue_mask;
594 default: /* Not TrueColor or DirectColor */
596 ximageinfo->index= (Pixel *)lmalloc(sizeof(Pixel) * (image->rgb.used+NOFLASH_COLORS));
599 /* get the colormap to use.
602 ximageinfo->cmap = our_default_cmap;
604 /* allocate colors shareable (if we can)
607 for (a= 0; a < image->rgb.used; a++)
612 xcolor.red= *(image->rgb.red + a);
613 xcolor.green= *(image->rgb.green + a);
614 xcolor.blue= *(image->rgb.blue + a);
616 for (i=max_cmap_entries-1; i>=free_cmap_entries; i--)
618 xcolor2.pixel = *(our_default_index + i);
619 XQueryColor(disp, ximageinfo->cmap, &xcolor2);
621 if ((xcolor.red >> 8) == (xcolor2.red >> 8) &&
622 (xcolor.green >> 8) == (xcolor2.green >> 8) &&
623 (xcolor.blue >> 8) == (xcolor2.blue >> 8))
629 if (use_cmap_entry < free_cmap_entries)
632 if (free_cmap_entries < 0)
634 printf("imageToXImage: too many global colors!\n");
638 xcolor.pixel = use_cmap_entry;
639 *(ximageinfo->index + a) = xcolor.pixel;
640 XStoreColor(disp, ximageinfo->cmap, &xcolor);
643 ximageinfo->no = a; /* number of pixels allocated in default visual */
645 printf("still %d free colormap entries\n", free_cmap_entries);
650 /* create an XImage and related colormap based on the image type
654 printf(" Building XImage...");
663 /* we copy the data to be more consistent
666 linelen = ((image->width + 7) / 8);
667 data= lmalloc(linelen * image->height);
669 memcpy((char *)data, (char *)image->data, linelen * image->height);
671 gcv.function= GXcopy;
672 ximageinfo->ximage= XCreateImage(disp, visual, 1, XYBitmap,
673 0, (char *)data, image->width, image->height,
676 /* use this if you want to use the bitmap as a mask */
677 ximageinfo->depth = image->depth;
679 if(visual->class == DirectColor || visual->class == TrueColor)
682 dbits= bitsPerPixelAtDepth(disp, scrn, ddepth);
683 dpixlen= (dbits + 7) / 8;
684 pixval= redvalue[image->rgb.red[0] >> 8] |
685 greenvalue[image->rgb.green[0] >> 8] |
686 bluevalue[image->rgb.blue[0] >> 8];
687 ximageinfo->background = pixval;
688 pixval= redvalue[image->rgb.red[1] >> 8] |
689 greenvalue[image->rgb.green[1] >> 8] |
690 bluevalue[image->rgb.blue[1] >> 8];
691 ximageinfo->foreground = pixval;
693 else /* Not Direct or True Color */
695 ximageinfo->foreground = BlackPixel(disp,scrn);
696 ximageinfo->background = WhitePixel(disp,scrn);
698 ximageinfo->ximage->bitmap_bit_order= MSBFirst;
699 ximageinfo->ximage->byte_order= MSBFirst;
706 /* modify image data to match visual and colormap
709 byte *data, *destptr, *srcptr;
711 dbits = bitsPerPixelAtDepth(disp, scrn, ddepth); /* bits per pixel */
712 dpixlen = (dbits + 7) / 8; /* bytes per pixel */
714 ximageinfo->ximage = XCreateImage(disp, visual, ddepth, ZPixmap, 0,
715 NULL, image->width, image->height,
716 8, image->width * dpixlen);
718 data = (byte *)lmalloc(image->width * image->height * dpixlen);
719 ximageinfo->depth = ddepth;
720 ximageinfo->ximage->data = (char *)data;
721 ximageinfo->ximage->byte_order = MSBFirst;
722 srcptr = image->data;
725 switch (visual->class)
732 for (y=0; y<image->height; y++)
734 for (x=0; x<image->width; x++)
736 pixval = memToVal(srcptr, 1);
737 pixval = redvalue[image->rgb.red[pixval] >> 8] |
738 greenvalue[image->rgb.green[pixval] >> 8] |
739 bluevalue[image->rgb.blue[pixval] >> 8];
740 valToMem(pixval, destptr, dpixlen);
750 if (dpixlen == 1) /* most common */
752 for (y=0; y<image->height; y++)
754 for (x=0; x<image->width; x++)
756 *destptr = ximageinfo->index[c + *srcptr];
762 else /* less common */
764 for (y=0; y<image->height; y++)
766 for (x=0; x<image->width; x++)
768 register unsigned long temp;
769 temp = memToVal(srcptr, 1);
770 valToMem(ximageinfo->index[c + temp], destptr, dpixlen);
786 lfree((byte *)redvalue);
787 lfree((byte *)greenvalue);
788 lfree((byte *)bluevalue);
791 if (image != orig_image)
796 /* Given an XImage and a drawable, move a rectangle from the Ximage
800 void sendXImage(XImageInfo *ximageinfo,
801 int src_x, int src_y, int dst_x, int dst_y,
802 unsigned int w, unsigned int h)
806 /* build and cache the GC
811 gcv.function = GXcopy;
812 if (ximageinfo->ximage->depth == 1)
814 gcv.foreground = ximageinfo->foreground;
815 gcv.background = ximageinfo->background;
816 ximageinfo->gc = XCreateGC(ximageinfo->disp, ximageinfo->drawable,
817 GCFunction | GCForeground | GCBackground,
821 ximageinfo->gc = XCreateGC(ximageinfo->disp, ximageinfo->drawable,
825 XPutImage(ximageinfo->disp, ximageinfo->drawable, ximageinfo->gc,
826 ximageinfo->ximage, src_x, src_y, dst_x, dst_y, w, h);
829 /* free up anything cached in the local Ximage structure.
832 void freeXImage(Image *image, XImageInfo *ximageinfo)
834 if (ximageinfo->index != NULL) /* if we allocated colors */
836 if (ximageinfo->no > 0 && !ximageinfo->rootimage) /* don't free root colors */
837 XFreeColors(ximageinfo->disp, ximageinfo->cmap, ximageinfo->index, ximageinfo->no, 0);
838 lfree(ximageinfo->index);
841 XFreeGC(ximageinfo->disp, ximageinfo->gc);
842 lfree((byte *)ximageinfo->ximage->data);
843 ximageinfo->ximage->data= NULL;
844 XDestroyImage(ximageinfo->ximage);
845 lfree((byte *)ximageinfo);
846 /* should we free private color map to ??? */