original 1.0.1 release
[xwelltris.git] / src / image / image.cxx
blob13647c137517235de683cbda0835855b2000f540
1 /****************************************************************************
2 * Copyright (C) 1996-98 by Leo Khramov
3 * email: leo@xnc.dubna.su
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 ****************************************************************************/
15 #include "image.h"
16 #include "im_exp.h"
18 #define ACCEL_H
19 #define ABS(x) ((x) > 0 ? (x) : (-(x)))
20 extern "C" void (*im_coding)(unsigned int,unsigned int,unsigned int,char**);
21 extern "C" void im_truetruecoding(unsigned int ir,unsigned int ig,unsigned int ib,char **imdata);
22 extern "C" void im_truenocoding(unsigned int ir,unsigned int ig,unsigned int ib,char **imdata);
23 XColor col;
24 int vclass;
25 Visual *vis;
26 int pcd_resolution=3;
27 int depth,bdep,translation,b_pad=1;
28 int fromMem=0;
29 Colormap defc;
30 PICINFO im;
31 char *trans;
32 uint rmask,gmask,bmask,rsh,bsh,gsh;
33 uint red_rsh,green_rsh,blue_rsh,red_lsh,green_lsh,blue_lsh;
34 XColor *XColorTable;
35 Colormap lcmp;
36 int ptnum,cmptype;
37 char check[256];
38 int conv24;
40 void (*im_coding256)(char **,int);
41 void im_256truecoding(char**,int);
42 void im_256nocoding(char**,int);
43 void im_256256coding(char**,int);
45 unsigned long *global_pal=NULL;
47 #ifdef PSEUDOCOLOR
48 unsigned long XPixelTable[256];
49 unsigned long Xfreepix[256];
50 byte rpart[256];
51 byte gpart[256];
52 byte bpart[256];
53 #endif
54 int im_getpixels(unsigned long* pt)
56 #ifdef PSEUDOCOLOR
57 if(vclass==PseudoColor)
58 for(int i=0;i<ptnum;i++)
59 pt[i]=Xfreepix[i];
60 #endif
61 return 0;
64 void im_clear_pic()
66 int i;
67 for(i=0;i<768;i++)
68 im.pal[i]=0;
69 for(i=0;i<256;i++)
70 check[i]=0;
73 Colormap im_choose_cmp()
75 Colormap cmp;
76 if(cmptype==CMP_OWN && vclass==PseudoColor)
78 cmp=XCreateColormap(disp,DefaultRootWindow(disp),vis,AllocNone);
79 lcmp=cmp;
80 XSync(disp,0);
81 } else return defc;
82 return cmp;
85 void im_freecmp(Colormap XColorMap,unsigned long *pt,int iptnum)
87 #ifdef PSEUDOCOLOR
88 if(XColorMap==0) XColorMap=defc;
89 if(vclass==PseudoColor && XColorMap==defc)
90 XFreeColors(disp, XColorMap, pt, iptnum, 0L);
91 if(XColorMap!=defc) XFreeColormap(disp,XColorMap);
92 #endif
95 Colormap im_get_colormap()
97 if(cmptype==CMP_OWN && vclass==PseudoColor) return lcmp;
98 else return 0;
101 #ifdef PSEUDOCOLOR
102 int im_get_checked()
104 int i,j=0;
105 return 256;
106 if(translation)
108 for(i=0;i<256;i++)
109 if(check[i]) j++;
110 return j;
112 return 256;
115 void im_separate_colors()
117 int i3;
118 for(int i=0;i<256;i++)
120 i3=i*3;
121 rpart[i]=im.pal[i3];
122 gpart[i]=im.pal[i3+1];
123 bpart[i]=im.pal[i3+2];
127 void im_union_colors()
129 int i3;
130 for(int i=0;i<256;i++)
132 i3=i*3;
133 im.pal[i3]=rpart[i];
134 im.pal[i3+1]=gpart[i];
135 im.pal[i3+2]=bpart[i];
139 void im_add_cmp_entry(unsigned long ent)
141 Xfreepix[ptnum++]=ent;
142 if(global_pal)
143 global_pal[ptnum-1]=ent;
146 void im_alloc_closest_cmp(Colormap XColorMap,int ColorMapSize=256)
148 int i, j, Index = 0, Count = 0, XNumOfColors;
149 unsigned long D, Distance, Red, Green, Blue;
150 int Failed = False;
151 XColor *XOldColorTable;
152 ptnum=0;
153 for (i = 0; i < 256; i++) {
154 if (i < ColorMapSize) { /* Prepere color entry in X format. */
155 XColorTable[i].red = im.pal[i*3] << 8;
156 XColorTable[i].green = im.pal[i*3+1] << 8;
157 XColorTable[i].blue = im.pal[i*3+2] << 8;
158 XColorTable[i].flags = DoRed | DoGreen | DoBlue;
159 XPixelTable[i] =(unsigned long)-1; /* Not allocated yet. */
161 else
162 XPixelTable[i] = 0; /* Put reasonable color for out of range. */
165 for (i = 0; i < ColorMapSize; i++) /* Allocate the colors from X: */
166 if (XAllocColor(disp, XColorMap, &XColorTable[i]))
168 XPixelTable[i] = XColorTable[i].pixel;
169 im_add_cmp_entry(XPixelTable[i]);
171 else
172 Failed = True;
173 if(ptnum<ColorMapSize/2 && cmptype==CMP_AUTO && Failed==True)
175 cmptype=CMP_OWN;
176 XColorMap=im_choose_cmp();
177 im_alloc_closest_cmp(XColorMap,ColorMapSize);
178 return;
180 if (Failed) {
181 XNumOfColors = DisplayCells(disp,DefaultScreen(disp));
182 XOldColorTable = (XColor *) malloc(sizeof(XColor) * XNumOfColors);
183 for (i = 0; i < XNumOfColors; i++) XOldColorTable[i].pixel = i;
184 XQueryColors(disp, XColorMap, XOldColorTable, XNumOfColors);
186 for (i = 0; i < ColorMapSize; i++) {
187 /* Allocate closest colors from X: */
188 if (XPixelTable[i] == -1) { /* Failed to allocate this one. */
189 Distance = 0xffffffff;
191 Red = XColorTable[i].red;
192 Green = XColorTable[i].green;
193 Blue = XColorTable[i].blue;
195 for (j = 0; j < XNumOfColors; j++) {
196 /* Find the closest color in 3D RGB space using L1 norm. */
197 if ((D = ABS(Red - XOldColorTable[j].red) +
198 ABS(Green - XOldColorTable[j].green) +
199 ABS(Blue - XOldColorTable[j].blue)) < Distance) {
200 Distance = D;
201 Index = j;
204 XPixelTable[i] = Index;
206 Count++;
209 free(XOldColorTable);
211 for(i=0;i<ColorMapSize;i++)
212 trans[i]=(XPixelTable[i]& 0xff);
216 void im_alloccmp(Colormap XColorMap,int ColorMapSize=256)
218 int Strip, Msk, i;
219 ptnum=0;
220 im_alloc_closest_cmp(XColorMap,ColorMapSize);
221 return;
222 for (i = 0; i < 256; i++)
223 XPixelTable[i] = 0; /* Put reasonable color for out of range. */
225 for (Strip = 0, Msk = 0xff; Strip < 8; Strip++, Msk <<= 1) {
226 for (i = 0; i < ColorMapSize; i++) {
227 /* Prepere color entry in X format. */
228 XColorTable[i].red = (im.pal[i*3] & Msk) << 8;
229 XColorTable[i].green = (im.pal[i*3+1] & Msk) << 8;
230 XColorTable[i].blue = (im.pal[i*3+2] & Msk) << 8;
231 XColorTable[i].flags = DoRed | DoGreen | DoBlue;
232 if (XAllocColor(disp,XColorMap , &XColorTable[i]))
234 XPixelTable[i] = XColorTable[i].pixel;
235 im_add_cmp_entry(XPixelTable[i]);
237 else
238 break;
240 if (i < ColorMapSize)
242 ptnum=0;
243 XFreeColors(disp, XColorMap, XPixelTable, i, 0L);
245 else
246 break;
249 if (Strip == 8)
250 fprintf(stderr,"Can not display the image - not enough colors available.\n");
251 else
252 for(i=0;i<ColorMapSize;i++)
253 trans[i]=(XPixelTable[i]& 0xff);
258 XImage *im_process_pseudo_256(int cmpsize=256)
260 char *imdata;
261 XImage *xim;
262 int imlen=im.w*im.h,i;
263 Colormap cmp;
264 if(translation==0)
266 cmp=im_choose_cmp();
267 trans=new char[256*bdep];
268 im_alloccmp(cmp,cmpsize);
269 for(i=0;i<imlen;i++)
270 im.pic[i]=trans[im.pic[i]];
272 imdata=(char*)im.pic;
273 delete trans;
274 if((xim=XCreateImage(disp,vis,depth,ZPixmap,0,imdata,im.w,im.h,8,0))==NULL)
276 delete imdata;
277 fprintf(stderr,"Image Engine Error: XCreateImage\n");
278 return NULL;
280 xim->byte_order=MSBFirst;
281 return xim;
283 #endif
285 char *getpicinfo()
287 char *tmp=new char[strlen(im.fullInfo)+1];
288 strcpy(tmp,im.fullInfo);
289 return tmp;
292 void do_masks(uint mask,uint &lsh,uint &rsh)
294 lsh=rsh=0;
295 while((mask & 1)==0) {lsh++;mask>>=1;};
296 while(mask & 1) {rsh++;mask>>=1;};
297 rsh=8-rsh;
300 void im_reset_coding()
302 im_coding256=im_256nocoding;
303 translation=0;
306 void im_set_coding()
308 im_coding256=im_256truecoding;
309 translation=1;
312 long cmap_distance[256];
313 int XCmapNumColors;
314 XColor *XCmapColorTable=NULL;
315 Colormap XCmapCmap;
317 long im_calc_weight(unsigned short red,
318 unsigned short green,
319 unsigned short blue)
321 double r=(double)red;
322 double g=(double)green;
323 double b=(double)blue;
324 return (long)sqrt(r*r+g*g+b*b);
327 void im_init_cmap(Colormap XColorMap)
330 int i, XNumOfColors;
331 int screen=DefaultScreen(disp);
333 depth=DefaultDepth(disp,screen);
334 vis=DefaultVisual(disp,screen);
335 defc=DefaultColormap(disp,screen);
336 vclass=vis->c_class;
337 XNumOfColors = DisplayCells(disp,DefaultScreen(disp));
338 if(XCmapColorTable == NULL)
339 XCmapColorTable = (XColor *) malloc(sizeof(XColor) * XNumOfColors);
340 for (i = 0; i < XNumOfColors; i++)
341 XCmapColorTable[i].pixel = i;
342 XQueryColors(disp, XColorMap, XCmapColorTable, XNumOfColors);
344 for(i=0;i< XNumOfColors;i++)
346 cmap_distance[i]=im_calc_weight(XCmapColorTable[i].red,
347 XCmapColorTable[i].green,
348 XCmapColorTable[i].blue);
350 XCmapNumColors=XNumOfColors;
351 XCmapCmap=XColorMap;
354 int im_find_closest_color(XColor* orig, XColor* tocol)
356 unsigned long weight,distance, Red, Green, Blue, D;
357 int idx=0,i;
358 if(vclass==TrueColor)
360 *tocol=*orig;
361 return 0;
363 Red=orig->red;
364 Green=orig->green;
365 Blue=orig->blue;
366 distance=0xffffffff;
367 for(i=0;i<XCmapNumColors;i++)
369 if ((D = ABS(Red - XCmapColorTable[i].red) +
370 ABS(Green - XCmapColorTable[i].green) +
371 ABS(Blue - XCmapColorTable[i].blue)) < distance)
373 distance = D;
374 idx = i;
377 *tocol=XCmapColorTable[idx];
378 tocol->flags = DoGreen | DoRed | DoBlue;
379 return 1;
382 int im_find_closest_color2(XColor* orig, XColor* tocol)
384 long weight,distance;
385 int idx=0,i;
386 if(vclass==TrueColor)
388 *tocol=*orig;
389 return 0;
391 weight=im_calc_weight(orig->red, orig->green, orig->blue);
392 distance=ABS(cmap_distance[idx]-weight);
393 for(i=1;i<XCmapNumColors;i++)
394 if(distance>ABS(cmap_distance[i]-weight))
396 idx=i;
397 distance=ABS(cmap_distance[i]-weight);
399 *tocol=XCmapColorTable[idx];
400 tocol->flags = DoGreen | DoRed | DoBlue;
401 return 1;
404 Status im_AllocColor(Display* d, Colormap cmap, XColor *cl)
406 XColor cl2;
407 Status st;
408 if((st=XAllocColor(disp, cmap, cl))!=0)
409 return st;
410 if(cmap!=XCmapCmap || XCmapColorTable==NULL)
411 im_init_cmap(cmap);
412 im_find_closest_color2(cl, &cl2);
413 *cl=cl2;
414 return XAllocColor(disp, cmap, cl);
417 Status (*pim_AllocColor)(Display*, Colormap, XColor*)=im_AllocColor;
419 void init_imagengine()
421 XImage* xim;
422 XPixmapFormatValues *pixformat;
423 int i, count;
424 int screen=DefaultScreen(disp);
426 depth=DefaultDepth(disp,screen);
427 vis=DefaultVisual(disp,screen);
428 defc=DefaultColormap(disp,screen);
429 vclass=vis->c_class;
430 fprintf(stderr, "**********Image Engine**********\n");
431 fprintf(stderr,"* *\n");
432 switch(vclass)
435 case TrueColor:
436 pixformat = XListPixmapFormats(disp, &count);
437 for (i=0; i<count; i++) {
438 if (pixformat[i].depth == depth) {
439 bdep = (pixformat[i].bits_per_pixel+7) / 8;
440 b_pad = pixformat[i].scanline_pad / 8;
441 break;
444 XFree(pixformat);
445 translation=1;
446 if((xim=XCreateImage(disp,vis,depth,ZPixmap,0,NULL,10,10,b_pad*8,0))==NULL)
448 fprintf(stderr,"Image Engine Error: XCreateImage\n");
449 return;
451 rmask=xim->red_mask;
452 gmask=xim->green_mask;
453 bmask=xim->blue_mask;
454 #ifdef PCD_SUPPORT
455 x11_create_lut(rmask,gmask,bmask);
456 #endif
457 do_masks(rmask,red_lsh,red_rsh);
458 do_masks(gmask,green_lsh,green_rsh);
459 do_masks(bmask,blue_lsh,blue_rsh);
460 im_coding=im_truetruecoding;
461 im_coding256=im_256truecoding;
462 fprintf(stderr,"*Visual: TrueColor *\n");
463 fprintf(stderr,"*Depth: %-3d (%d bytes/pixel) *\n",depth,bdep);
464 fprintf(stderr,"*RGB: %d:%d:%d *\n",8-red_rsh,8-green_rsh,8-blue_rsh);
465 fprintf(stderr,"*Colors: %-9d *\n",1<<depth);
466 fprintf(stderr,"*Images: GIF,JPEG,PCX *\n");
467 XFree(xim);
468 break;
469 case PseudoColor:
470 #ifdef PSEUDOCOLOR
471 bdep=(depth+7)/8;
472 b_pad=1;
473 translation=1;
474 XColorTable=new XColor[256];
475 im_init_cmap(defc);
476 im_coding256=im_256256coding;
477 im_coding=im_truenocoding;
478 fprintf(stderr,"*Visual: PseudoColor *\n");
479 fprintf(stderr,"*Depth: %-3d (%d bytes) *\n",depth,bdep);
480 fprintf(stderr,"*Colors: %-9d *\n",1<<depth);
481 fprintf(stderr,"*Images: GIF,JPEG,PCX *\n");
482 break;
483 #else
484 fprintf(stderr,"*Visual: PseudoColor *\n");
485 fprintf(stderr,"*Not supported *\n");
486 vclass=DirectColor;
487 break;
488 #endif
489 case DirectColor:
490 fprintf(stderr,"*Visual: DirectColor *\n");
491 fprintf(stderr,"*Not supported *\n");
492 break;
494 fprintf(stderr,"* *\n");
495 fprintf(stderr,"******** (c) Leo 96-98 *********\n");
498 //return -1 if not detected
499 int autodetect(char *f)
501 int fp=open(f,O_RDONLY);
502 if(fp==-1) return -1;
503 u_char buf[40];
504 if(read(fp,buf,30)<30)
506 close(fp);
507 return -1;
509 close(fp);
510 if(buf[0]=='G' && buf[1]=='I' && buf[2]=='F') return F_GIF;
511 #ifdef TIFFSUPPORT
512 else if(buf[0]==buf[1] && (buf[0]=='I' || buf[0]=='M')) return F_TIFF;
513 #endif
514 #ifdef PCXSUPPORT
515 else if(buf[0]==0xa && buf[1]<=5 && buf[2]==1) return F_PCX;
516 #endif
517 #ifdef BMPSUPPORT
518 else if(buf[0]=='B' && buf[1]=='M') return F_BMP;
519 #endif
520 #ifdef XPMSUPPORT
521 else if (strncmp((char*)buf, "/* XPM */", (size_t) 9)==0) return F_XPM;
522 #endif
523 #ifdef XBMSUPPORT
524 else if (strncmp((char*)buf,"#define", (size_t) 7)==0) return F_XBM;
525 #endif
526 #ifdef TGASUPPORT
527 else if (buf[0]==0 && buf[1]==0 &&
528 buf[2]==2 && buf[3]==0 &&
529 buf[4]==0 && buf[5]==0 &&
530 buf[6]==0 && buf[7]==0) return F_TARGA;
531 #endif
532 #ifdef PNG_SUPPORT
533 else if (strncmp((char*)buf+1,"PNG", (size_t) 3)==0) return F_PNG;
534 #endif
536 else if(buf[0]==0xff && buf[1]==0xd8 && buf[2]==0xff) return F_JPEG;
538 #ifdef PCD_SUPPORT
539 else
540 while(*f!=0)
542 if(*f=='.')
543 if(strncmp(f,".pcd",4)==0 || strncmp(f,".PCD",4)==0)
544 return F_PCD;
545 f++;
547 #endif
548 return -1;
552 void im_init_translation()
554 int i,j,i3,i2;
555 Colormap cmp;
556 if(translation)
558 trans=new char[256*bdep];
559 #ifdef PSEUDOCOLOR
560 if(vclass==PseudoColor)
562 cmp=im_choose_cmp();
563 im_alloccmp(cmp);
564 return;
566 #endif
567 col.flags=DoRed | DoGreen | DoBlue;
568 for(i=0;i<256;i++)
570 i3=i*3;i2=bdep*(i+1);
571 col.red=(unsigned short)(im.pal[i3])<<8;
572 col.green=(unsigned short)(im.pal[i3+1])<<8;
573 col.blue=(unsigned short)(im.pal[i3+2])<<8;
574 XAllocColor(disp,defc,&col);
575 if(global_pal)
576 global_pal[i]=col.pixel;
577 for(j=bdep-1;j>=0;j--)
579 trans[i2-j-1]=(char)((col.pixel>>(j<<3))& 0xff);
586 XImage *LoadXImage(char *file,int cmptype,int type)
588 int i,j,l,lw,x,y;
589 ulong mask;
590 char *imdata,*imdat;
591 int ind;
592 XImage *xim;
593 ::cmptype=cmptype;
594 if(vclass==DirectColor) return NULL;
595 #ifndef PSEUDOCOLOR
596 if(vclass==PseudoColor) return NULL;
597 #endif
598 if(type==F_AUTO) type=autodetect(file);
599 switch(type)
601 case F_GIF:
602 translation=0;
603 im_clear_pic();
604 im_load_gif(file,&im,fromMem);
605 break;
607 case F_JPEG:
608 #ifdef JPEGSUPPORT
609 im_clear_pic();
610 if(vclass==PseudoColor)
611 translation= 0;
612 else
613 translation= 1;
614 if(LoadJFIF(file,&im))
615 break;
616 else
618 fprintf(stderr,"Image Engine: LoadJpeg\n");
619 return NULL;
621 #else
622 return NULL;
623 #endif
624 case F_TIFF:
625 #ifdef TIFFSUPPORT
626 im_clear_pic();
627 translation= 0;
628 if(LoadTIFF(file,&im)) break;else {fprintf(stderr,"Image Engine: LoadTiff\n");return NULL;};
629 #else
630 return NULL;
631 #endif
632 case F_PNG:
633 #ifdef PNG_SUPPORT
634 im_clear_pic();
635 translation= 0;
636 if(LoadPNG(file,&im)) break;else {fprintf(stderr,"Image Engine: LoadPNG\n");return NULL;};
637 #else
638 return NULL;
639 #endif
640 case F_PCX:
641 #ifdef PCXSUPPORT
642 im_clear_pic();
643 translation= 0;
644 if(LoadPCX(file,&im)) break;else {fprintf(stderr,"Image Engine: LoadPcx\n");return NULL;};
645 #else
646 return NULL;
647 #endif
648 case F_PCD:
649 #ifdef PCD_SUPPORT
650 im_clear_pic();
651 if(vclass==PseudoColor) translation=0;
652 else translation= 1;
653 if(LoadPCD(file,&im)) break;else {fprintf(stderr,"Image Engine: LoadPcd\n");return NULL;};
654 #else
655 return NULL;
656 #endif
657 case F_XBM:
658 #ifdef XBMSUPPORT
659 im_clear_pic();
660 translation=0;
661 if(LoadXBM(file,&im)) break;else {fprintf(stderr,"Image Engine: LoadXBM\n");return NULL;};
662 #else
663 return NULL;
664 #endif
665 case F_XPM:
666 #ifdef XPMSUPPORT
667 im_clear_pic();
668 translation=0;
669 if(LoadXPM(file,&im)) break;else {fprintf(stderr,"Image Engine: LoadXPM\n");return NULL;};
670 #else
671 return NULL;
672 #endif
673 case F_BMP:
674 #ifdef BMPSUPPORT
675 im_clear_pic();
676 translation= 1;
677 if(LoadBMP(file,&im)) break;else {fprintf(stderr,"Image Engine: LoadBmp\n");return NULL;};
678 #else
679 return NULL;
680 #endif
681 case F_TARGA:
682 #ifdef TGASUPPORT
683 im_clear_pic();
684 translation=0;
685 if(LoadTarga(file,&im)) break;else {fprintf(stderr,"Image Engine: LoadTGA\n");return NULL;};
686 #else
687 return NULL;
688 #endif
689 default: return NULL;
691 #ifdef PSEUDOCOLOR
692 if(vclass==PseudoColor)
694 if(im.type==PIC8)
695 return im_process_pseudo_256(im_get_checked());
696 else {
697 im_convert_true_to_pseudo(im,256);
698 translation=0;
699 return im_process_pseudo_256(conv24!=CONV24_FAST ? 230:256);
702 #endif
703 if(im.type==PIC8 && vclass==TrueColor)
705 if(translation==0)
707 translation=1;
708 im_init_translation();
709 translation=0;
710 lw = im.w * bdep;
711 if (lw % b_pad)
712 lw += b_pad - lw % b_pad;
713 l=im.h*im.w;
714 imdata=new char[lw*im.h];
715 i = 0;
716 for (y=0; y<im.h; y++) {
717 l = y * lw;
718 for (x=0; x<im.w; x++) {
719 ind = im.pic[i++] * bdep;
720 for (j=0; j<bdep; j++)
721 imdata[l++] = trans[ind++];
724 free(im.pic);
726 else
727 imdata=(char*)im.pic;
728 delete trans;
729 if((xim=XCreateImage(disp,vis,depth,ZPixmap,0,imdata,im.w,im.h,b_pad*8,0))==NULL)
731 delete imdata;
732 fprintf(stderr,"Image Engine Error: XCreateImage\n");
733 return NULL;
735 xim->byte_order=MSBFirst;
736 return xim;
737 } else if(im.type==PIC24 && vclass==TrueColor)
739 if(translation==0)
741 lw = im.w * bdep;
742 if (lw % b_pad)
743 lw += b_pad - lw % b_pad;
744 imdata = new char[lw*im.h];
745 mask = 0xff;
746 i = 0;
747 for (y=0; y < im.h; y++) {
748 imdat = imdata + y * lw;
749 for (x=0; x < im.w; x++, i+=3)
750 im_truetruecoding(im.pic[i],im.pic[i+1],im.pic[i+2],&imdat);
752 free(im.pic);
754 else
755 imdata=(char*)im.pic;
756 if((xim=XCreateImage(disp,vis,depth,ZPixmap,0,imdata,im.w,im.h,b_pad*8,0))==NULL)
758 fprintf(stderr,"Image Engine Error: XCreateImage\n");
759 delete imdata;
760 return NULL;
762 xim->byte_order=MSBFirst;
763 return xim;
765 return NULL;
768 XImage* encoding256(PICINFO& im,int ncol)
770 int i,j,l,k,lw;
771 ulong mask;
772 char *trans;
773 char *imdata;
774 int ind,delta;
775 XImage *xim;
776 trans=new char[256*bdep];
777 col.flags=DoRed | DoGreen | DoBlue;
778 ptnum=0;
779 for(i=0;i<ncol;i++)
781 col.red=(unsigned short)(im.pal[i*3])<<8;
782 col.green=(unsigned short)(im.pal[i*3+1])<<8;
783 col.blue=(unsigned short)(im.pal[i*3+2])<<8;
784 XAllocColor(disp,defc,&col);
785 #ifdef PSEUDOCOLOR
786 if(vclass==PseudoColor)
787 im_add_cmp_entry(col.pixel);
788 #endif
789 mask=bdep*(i+1);
790 for(j=bdep-1;j>=0;j--)
792 trans[mask-j-1]=(char)((col.pixel>>(j<<3))& 0xff);
795 l=im.h*im.w;
796 lw = im.w * bdep;
797 if (lw % b_pad) {
798 lw += b_pad - lw % b_pad;
800 imdata=new char[im.h*lw];
801 for (i=0; i<im.h; i++) {
802 for (k=0; k<im.w; k++) {
803 ind = im.pic[i*im.w+k];
804 ind *= bdep;
805 delta = i * lw + k * bdep;
806 for(j=0;j<bdep;j++)
807 imdata[delta+j]=trans[ind+j];
810 delete trans;
811 if((xim=XCreateImage(disp,vis,depth,ZPixmap,0,imdata,im.w,im.h,b_pad*8,0))==NULL)
813 delete imdata;
814 fprintf(stderr,"Image Engine Error: XCreateImage\n");
815 return NULL;
817 xim->byte_order=MSBFirst;
818 return xim;
821 void im_256truecoding(char **data,int ind)
823 char *ltrans=trans+ind*bdep;
824 for(int i=0;i<bdep;i++)
825 *((*data)++)=*(ltrans++);
828 void im_256nocoding(char **data,int ind)
830 *((*data)++)=ind;
833 void im_256256coding(char **data,int ind)
835 *((*data)++)=trans[ind];
838 Pixmap LoadPixmap(char *fname,int type,int& l,int& h,int cmptype)
840 XImage *iim;
841 Pixmap im;
842 GC gcw;
843 iim=LoadXImage(fname,cmptype,type);
844 if(iim)
846 im=XCreatePixmap(disp,DefaultRootWindow(disp),
847 iim->width,iim->height,depth);
848 gcw=XCreateGC(disp,im,0,NULL);
849 l=iim->width;h=iim->height;
850 XPutImage(disp,im,gcw,iim,0,0,0,0,l,h);
851 XFreeGC(disp,gcw);
852 delete iim->data;iim->data=NULL;XDestroyImage(iim);
853 return im;
855 return 0;
858 Pixmap LoadPixmapFromMem(char *fname,int type,int& l,int& h,
859 int cmptype,int siz)
861 XImage *iim;
862 Pixmap im;
863 GC gcw;
864 fromMem=siz;
865 iim=LoadXImage(fname,cmptype,type);
866 fromMem=0;
867 if(iim)
869 im=XCreatePixmap(disp,DefaultRootWindow(disp),
870 iim->width,iim->height,depth);
871 gcw=XCreateGC(disp,im,0,NULL);
872 l=iim->width;h=iim->height;
873 XPutImage(disp,im,gcw,iim,0,0,0,0,l,h);
874 XFreeGC(disp,gcw);
875 delete iim->data;iim->data=NULL;XDestroyImage(iim);
876 return im;
878 return 0;
881 void im_populate_pallete(unsigned long* pal)
883 global_pal=pal;
886 void im_clear_global_pal()
888 global_pal=NULL;