home *** CD-ROM | disk | FTP | other *** search
/ ftp.sunet.sepub/pictures / 2014.11.ftp.sunet.se-pictures.tar / ftp.sunet.se / pub / pictures / ACiD-artpacks / programs / unix / editors / gimp-plugins-unstable-0_99_23_tar.gz / gimp-plugins-unstable-0_99_23_tar / gimp-plugins-unstable-0.99.23 / icon / iconread.c < prev    next >
C/C++ Source or Header  |  1998-02-24  |  17KB  |  668 lines

  1. /* Time-stamp: <97/12/29 00:42:54 riffraff> */
  2.  
  3. /*
  4.  * $Id: iconread.c,v 1.1 1998/02/24 07:28:51 yosh Exp $
  5.  *
  6.  * $Author: yosh $
  7.  * $Date: 1998/02/24 07:28:51 $
  8.  * $Header: /debian/home/gnomecvs/gimp-plugins-unstable/icon/iconread.c,v 1.1 1998/02/24 07:28:51 yosh Exp $
  9.  *
  10.  * $Log: iconread.c,v $
  11.  * Revision 1.1  1998/02/24 07:28:51  yosh
  12.  * More plugins from the registry... time to autoconf stuff now...
  13.  *
  14.  * -Yosh
  15.  *
  16.  * Revision 1.4  1997/12/29 05:43:56  riffraff
  17.  * kludge for images that improperly set bColorCount to 0...
  18.  * now figures it out later using biBitCount (may not be correct
  19.  * in all instances, but a good guess)
  20.  *
  21.  * Revision 1.3  1997/12/29 05:13:08  riffraff
  22.  * was to strict reading in image....now liberal input,
  23.  * strict output...
  24.  *
  25.  * apparently, images with 256 colors only put 255 in
  26.  * bColorCount (a char can only hold 0-255, i dont know
  27.  * why they (whoever 'they' are) didnt say that a color
  28.  * count of 0 equalled 256, but whatever...a small workaround
  29.  * to make a colorcount of 255 actually read in 256, but
  30.  * toss the last entry read...
  31.  *
  32.  */
  33.  
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <memory.h>
  38. #include <malloc.h>
  39. #include <sys/types.h>
  40. #include <libgimp/gimp.h>
  41. #include <gtk/gtk.h>
  42. #include "icon.h"
  43.  
  44. static char rcsid[] = "$Id: iconread.c,v 1.1 1998/02/24 07:28:51 yosh Exp $";
  45.  
  46. static FILE *fp;
  47. static ICO_Header ICO_header;
  48. static ICO_TIconDirEntry *ICO_direntry;
  49. static ICO_IconImage *ICO_iconimage;
  50. static int debug_level;
  51. static char *filename;
  52. static int bad_colors=0;
  53. /* debug levels (and together):
  54.    0x01 = fatal errors
  55.    0x02 = warnings
  56.    0x04 = info
  57.  */
  58.  
  59. static int ICO_read_iconheader(void);
  60. static int ICO_read_direntries(void);
  61. static int ICO_read_bitmapinfo(int);
  62. static int ICO_read_rgbquad(int);
  63. static int ICO_read_pixeldata(int);
  64. static gint32 make_image(void);
  65.  
  66. #define PERR(str)  if (debug_level & 0x04) printf(str);
  67.  
  68. gint32 ReadICON(char *fn) {
  69.   gint32 image_id;
  70.  
  71.   debug_level=0x03;  /* fatal errors and warnings */
  72.   filename=strdup(fn);
  73.   fp=fopen(filename,"rb");  /* why rb? */
  74.   if (fp==NULL) {
  75.     if (debug_level & 0x01)
  76.       printf("failed to open %s\n",filename);
  77.     free(filename);
  78.     return(-1);
  79.   }
  80.  
  81.   
  82.   if (debug_level & 0x04)
  83.     printf("ICO: Reading header\n");
  84.  
  85.  
  86.   if (ICO_read_iconheader()) {
  87.     free(filename);
  88.     fclose(fp);
  89.     return(-1);
  90.   }
  91.  
  92.   if (debug_level & 0x04) {
  93.     printf("ICO: ICON Information:\n");
  94.     printf("ICO: Number of Images: %d\n",(int)(ICO_header.idCount));
  95.   }
  96.  
  97.   if (ICO_read_direntries()) {
  98.     if (debug_level & 0x01)
  99.       printf("ICO: unable to read icon header\n");
  100.     free(filename);
  101.     fclose(fp);
  102.     return(-1);
  103.   }
  104.  
  105.   
  106.   if ((image_id=make_image())) {
  107.     g_free(ICO_direntry);
  108.     g_free(ICO_iconimage[0].icColors);
  109.     g_free(ICO_iconimage[0].icXOR);
  110.     g_free(ICO_iconimage[0].icAND);
  111.     g_free(ICO_iconimage);
  112.     free(filename);
  113.     return(image_id);
  114.   }
  115.   g_free(ICO_direntry);
  116.   g_free(ICO_iconimage[0].icColors);
  117.   g_free(ICO_iconimage[0].icXOR);
  118.   g_free(ICO_iconimage[0].icAND);
  119.   g_free(ICO_iconimage);
  120.   free(filename);
  121.   return(-1);
  122. }
  123.  
  124. static int ICO_read_iconheader(void) {
  125.   int b;
  126.   unsigned short i;
  127.  
  128.   /* read Reserved bit, abort if not 0 */
  129.   b=fread((void *)&i,1,sizeof(i),fp);
  130.   if (b!=sizeof(i))
  131.     return(-1);
  132.   ICO_header.idReserved=i;
  133.   if (ICO_header.idReserved!=0)
  134.     return(-1);
  135.  
  136.   /* read Resource Type, 1 is for icons, abort if different */
  137.   b=fread((void *)&i,1,sizeof(i),fp);
  138.   if (b!=sizeof(i))
  139.     return(-1);
  140.   ICO_header.idType=i;
  141.   if (ICO_header.idType!=1)
  142.     return(-1);
  143.  
  144.   /* read Number of images, abort if invalid value (0) */
  145.   b=fread((void *)&i,1,sizeof(i),fp);
  146.   if (b!=sizeof(i))
  147.     return(-1);
  148.   ICO_header.idCount=i;              /* Number of images (>0) */
  149.   if (ICO_header.idCount==0) {
  150.     if (debug_level & 0x01)
  151.       printf("ICO: no images in file!\n");
  152.     return(-1);
  153.   }
  154.   if (ICO_header.idCount>1) {
  155.     if (debug_level & 0x02) {
  156.       printf("ICO:  warning:  this plugin only handles one icon image at this time\n");
  157.       printf("ICO:            only reading one image\n");
  158.     }
  159.   }
  160.   return(0);
  161. }
  162.  
  163. static int ICO_read_direntries(void) {
  164.   int b,j;
  165.   unsigned short i;
  166.   unsigned char c;
  167.   unsigned long l;
  168.  
  169.   ICO_direntry=g_new(ICO_TIconDirEntry,ICO_header.idCount);
  170.   if (ICO_direntry==NULL) { /* shouldn't happen */
  171.     if (debug_level & 0x01)
  172.       printf("ICO:  fatal error:  out of memory!\n");
  173.     return(-1);
  174.   }
  175.  
  176.   /* read in direntries one by one,
  177.      perform some primitive sanity checks */
  178.   for (j=0; j<ICO_header.idCount; j++) {
  179.  
  180.     /* read Width, in pixels */
  181.     b=fread((void *)&c,1,sizeof(c),fp);
  182.     if (b!=sizeof(c)) {
  183.       PERR("ICO:  unable to read bWidth\n");
  184.       return(-1);
  185.     }
  186.     ICO_direntry[j].bWidth=c;
  187.  
  188.     /* read Height, in pixels */
  189.     b=fread((void *)&c,1,sizeof(c),fp);
  190.     if (b!=sizeof(c)) {
  191.       PERR("ICO:  unable to read bHeight\n");
  192.       return(-1);
  193.     }
  194.     ICO_direntry[j].bHeight=c;
  195.  
  196.     /* and validate data */
  197.     if (debug_level & 0x04)
  198.       printf("ICO:  bWidth==%d  bHeight==%d\n",
  199.          (int)ICO_direntry[j].bWidth,
  200.          (int)ICO_direntry[j].bHeight);
  201.     if (ICO_direntry[j].bWidth==0 || ICO_direntry[j].bHeight==0)
  202.       return(-1);
  203.  
  204.     /* Number of colors in image */
  205.     b=fread((void *)&c,1,sizeof(c),fp);
  206.     if (b!=sizeof(c)) {
  207.       PERR("ICO:  unable to read bColorCount\n");
  208.       return(-1);
  209.     }
  210.     ICO_direntry[j].bColorCount=c;
  211.     if (debug_level & 0x04)
  212.       printf("ICO:  bColorCount==%d\n",(int)ICO_direntry[j].bColorCount);
  213.     if (ICO_direntry[j].bColorCount==0) {
  214.       if (debug_level & 0x02) {
  215.     printf("ICO:  warning:  number of colors not defined in image...\n");
  216.     printf("ICO:            will try to figure it out myself\n");
  217.       }
  218.       bad_colors=1;
  219. /*      ICO_direntry[j].bColorCount=255; */
  220. /*      return(-1); */
  221.     }
  222.     if (ICO_direntry[j].bColorCount>256) {
  223.       if (debug_level & 0x01)
  224.     printf("ICO:  this plugin does not support images with more than 256 colors\n");
  225.       return(-1);
  226.     }
  227.  
  228.     /* Reserved (must be 0) */
  229.     b=fread((void *)&c,1,sizeof(c),fp);
  230.     if (b!=sizeof(c))
  231.       return(-1);
  232.     ICO_direntry[j].bReserved=c;
  233.     if (ICO_direntry[j].bReserved!=0)
  234.       return(-1);
  235.  
  236.     /* Color Planes, not used, 0 */
  237.     b=fread((void *)&i,1,sizeof(i),fp);
  238.     if (b!=sizeof(i))
  239.       return(-1);
  240.     ICO_direntry[j].wPlanes=i;
  241.  
  242.     /* Bits per pixel, not used, 0 */
  243.     b=fread((void *)&i,1,sizeof(i),fp);
  244.     if (b!=sizeof(i))
  245.       return(-1);
  246.     ICO_direntry[j].wBitCount=i;
  247.  
  248.     /* size of image data, in bytes */
  249.     b=fread((void *)&l,1,sizeof(l),fp);
  250.     if (b!=sizeof(l))
  251.       return(-1);
  252.     ICO_direntry[j].dwBytesInRes=l;
  253.     if (ICO_direntry[j].dwBytesInRes==0)
  254.       return(-1);
  255.  
  256.     /* offset from beginning image info */
  257.     b=fread((void *)&l,1,sizeof(l),fp);
  258.     if (b!=sizeof(l))
  259.       return(-1);
  260.     ICO_direntry[j].dwImageOffset=l;
  261.     if (ICO_direntry[j].dwImageOffset==0)
  262.       return(-1);
  263.  
  264.     if (debug_level & 0x04) {
  265.       printf("\nImage #%d\n",j);
  266.       printf("Width: %d\n",(int)(ICO_direntry[j].bWidth));
  267.       printf("Height: %d\n",(int)(ICO_direntry[j].bHeight));
  268.       printf("Colors: %d\n",(int)(ICO_direntry[j].bColorCount));
  269.       printf("Bit Count (unused): %d\n",(int)(ICO_direntry[j].wBitCount));
  270.       printf("Total bytes: %ld\n",(long)(ICO_direntry[j].dwBytesInRes));
  271.       printf("Offset: %ld\n",(long)(ICO_direntry[j].dwImageOffset));
  272.     }
  273.  
  274.   }
  275.  
  276.   return(0);
  277. }
  278.  
  279. static int ICO_read_bitmapinfo(int imagenum) {
  280.   int b;
  281.   unsigned long l;
  282.   unsigned short i;
  283.  
  284. #define ih ICO_iconimage[imagenum].icHeader
  285.   /* read bitmap info an perform some primitive sanity checks */
  286.  
  287.   /* sizeof(TBitmapInfoHeader) */
  288.   b=fread((void *)&l,1,sizeof(l),fp);
  289.   if (b!=sizeof(l)) {
  290.     PERR("ICO:  unable to read biSize\n");
  291.     return(-1);
  292.   }
  293.   ih.biSize=l;
  294.  
  295.   /* width of bitmap */
  296.   b=fread((void *)&l,1,sizeof(l),fp);
  297.   if (b!=sizeof(l)) {
  298.     PERR("ICO:  unable to read biWidth\n");
  299.     return(-1);
  300.   }
  301.   ih.biWidth=l;
  302.  
  303.   /* height of bitmap, see notes (icon.h) */
  304.   b=fread((void *)&l,1,sizeof(l),fp);
  305.   if (b!=sizeof(l)) {
  306.     PERR("ICO:  unable to read biHeight\n");
  307.     return(-1);
  308.   }
  309.   ih.biHeight=l;
  310.  
  311.   if (debug_level & 0x04)
  312.     printf("ICO:  biWidth==%d  biHeight==%d\n",(int)ih.biWidth,(int)ih.biHeight);
  313.   if (ih.biWidth==0 || ih.biHeight==0)
  314.     return(-1);
  315.  
  316.   /* planes, always 1 */
  317.   b=fread((void *)&i,1,sizeof(i),fp);
  318.   if (b!=sizeof(i)) {
  319.     PERR("ICO:  unable to read biPlanes\n");
  320.     return(-1);
  321.   }
  322.   ih.biPlanes=i;
  323.  
  324.   /* number of color bits (2,4,8) */
  325.   b=fread((void *)&i,1,sizeof(i),fp);
  326.   if (b!=sizeof(i)) {
  327.     PERR("ICO:  unable to read biBitCount\n");
  328.     return(-1);
  329.   }
  330.   ih.biBitCount=i;
  331.   if (bad_colors)
  332.     if (ih.biBitCount==8)
  333.       ICO_direntry[imagenum].bColorCount=255;
  334.     else
  335.       ICO_direntry[imagenum].bColorCount=1<<ih.biBitCount;
  336.  
  337.   /* compression used, 0 */
  338.   b=fread((void *)&l,1,sizeof(l),fp);
  339.   if (b!=sizeof(l)) {
  340.     PERR("ICO:  unable to read biCompression\n");
  341.     return(-1);
  342.   }
  343.   ih.biCompression=l;
  344.   if (ih.biCompression!=0) {
  345.     if (debug_level & 0x01)
  346.       printf("ICO:  invalid compression value of %d\n",(int)ih.biCompression);
  347.     return(-1);
  348.   }
  349.  
  350.   /* size of the pixel data, see icon.h */
  351.   b=fread((void *)&l,1,sizeof(l),fp);
  352.   if (b!=sizeof(l)) {
  353.     return(-1);
  354.     PERR("ICO:  unable to read biSizeImage\n");
  355.   }
  356.   ih.biSizeImage=l;
  357.   if (debug_level & 0x04)
  358.     printf("ICO:  biSizeImage==%d\n",(int)ih.biSizeImage);
  359.  
  360.   /* used to abort on this, but it's not really that important...
  361.       should be the len of the XORMASK + ANDMASK...
  362.  
  363.       as the saying goes, be liberal in what you accept,
  364.       and strict in what you output (or something to that effect 
  365.     */
  366.  
  367. /*  if (ih.biSizeImage==0)
  368.     return(-1); */
  369.  
  370.   /* not used, 0 */
  371.   b=fread((void *)&l,1,sizeof(l),fp);
  372.   if (b!=sizeof(l)) {
  373.     PERR("ICO:  unable to read biXPelsPerMeter\n");
  374.     return(-1);
  375.   }
  376.   ih.biXPelsPerMeter=l;
  377.  
  378.   /* not used, 0 */
  379.   b=fread((void *)&l,1,sizeof(l),fp);
  380.   if (b!=sizeof(l)) {
  381.     PERR("ICO:  unable to read biYPelsPerMeter\n");
  382.     return(-1);
  383.   }
  384.   ih.biYPelsPerMeter=l;
  385.  
  386.   /* # of colors used, set to 0 */
  387.   b=fread((void *)&l,1,sizeof(l),fp);
  388.   if (b!=sizeof(l)) {
  389.     PERR("ICO:  unable to read biClrUsed\n");
  390.     return(-1);
  391.   }
  392.   ih.biClrUsed=l;
  393.  
  394.   /* important colors, set to 0 */
  395.   b=fread((void *)&l,1,sizeof(l),fp);
  396.   if (b!=sizeof(l)) {
  397.     PERR("ICO:  unable to read biClrImportant\n");
  398.     return(-1);
  399.   }
  400.   ih.biClrImportant=l;
  401.  
  402. #undef ih
  403.  
  404.   return(0);
  405. }
  406.  
  407. static int ICO_read_rgbquad(int imagenum) {
  408.   int i,j;
  409.   unsigned char c;
  410.   int colors;
  411.  
  412.   colors=ICO_direntry[imagenum].bColorCount;
  413.   ICO_iconimage[imagenum].icColors=g_new(ICO_TRGBQuad,colors);
  414.  
  415.   if (ICO_iconimage[imagenum].icColors==NULL) {  /* shouldn't happen */
  416.     if (debug_level & 0x01)
  417.       printf("ICO:  fatal error:  out of memory!\n");
  418.     return(-1);
  419.   }
  420.  
  421. #define cc ICO_iconimage[imagenum].icColors
  422.  
  423.   if (debug_level & 0x04)
  424.     printf("\nRGB Data\n");
  425.  
  426.   for (j=0; j<colors; j++) {
  427.     i=fread((void *)&c,sizeof(c),1,fp);
  428.     if (i<1)
  429.       return(-1);
  430.     cc[j].rgbBlue=c;
  431.  
  432.     i=fread((void *)&c,sizeof(c),1,fp);
  433.     if (i<1) 
  434.       return(-1);
  435.     cc[j].rgbGreen=c;
  436.  
  437.     i=fread((void *)&c,sizeof(c),1,fp);
  438.     if (i<1)
  439.       return(-1);
  440.     cc[j].rgbRed=c;
  441.  
  442.     i=fread((void *)&c,sizeof(c),1,fp);
  443.     if (i<1)
  444.       return(-1);
  445.     cc[j].rgbReserved=c;
  446.  
  447.     if (debug_level & 0x04)
  448.       printf("#%d:  Red: %d  Green: %d  Blue: %d\n",j,cc[j].rgbRed,
  449.          cc[j].rgbGreen,cc[j].rgbBlue);
  450.   }
  451.  
  452. #undef cc
  453.  
  454.   if (colors==255) {
  455.     char buf[10];
  456.  
  457.     fread((void *)&buf,4,1,fp);
  458.   }
  459.   return(0);
  460. }
  461.  
  462. static int ICO_read_pixeldata(int imagenum) {
  463.   int xormasklen,andmasklen,xornum;
  464.   int b;
  465.   int i,j,k;
  466.   unsigned char *buf,*p;
  467.  
  468.   xornum=(ICO_direntry[imagenum].bWidth*
  469.       ICO_direntry[imagenum].bHeight);
  470.   xormasklen=(xornum*ICO_iconimage[imagenum].icHeader.biBitCount)/8;
  471.   if (debug_level & 0x04)
  472.     printf("ICO:  xormasklen==%d,  xornum==%d\n",xormasklen,xornum);
  473.   andmasklen=xornum/8;
  474.   ICO_iconimage[imagenum].xormasklen=xormasklen;
  475.   ICO_iconimage[imagenum].andmasklen=andmasklen;
  476.  
  477.   ICO_iconimage[imagenum].icXOR=g_new(unsigned char,xornum);
  478.  
  479.   if (ICO_iconimage[imagenum].icXOR==NULL) {  /* shouldnt happen */
  480.     if (debug_level & 0x01)
  481.       printf("ICO:  fatal error:  out of memory!\n");
  482.     return(-1);
  483.   }
  484.  
  485.   ICO_iconimage[imagenum].icAND=g_new(unsigned char,xornum);
  486.  
  487.   if (ICO_iconimage[imagenum].icAND==NULL) {  /* shouldnt happen */
  488.     if (debug_level & 0x01)
  489.       printf("ICO:  fatal error:  out of memory!\n");
  490.     g_free(ICO_iconimage[imagenum].icXOR);
  491.     return(-1);
  492.   }
  493.  
  494.   buf=g_new(unsigned char,xormasklen);
  495.  
  496.   if (buf==NULL) {  /* shouldnt happen */
  497.     if (debug_level & 0x01)
  498.       printf("ICO:  fatal error:  out of memory!\n");
  499.     g_free(ICO_iconimage[imagenum].icXOR);
  500.     g_free(ICO_iconimage[imagenum].icAND);
  501.     return(-1);
  502.   }
  503.  
  504.   fread((void *)buf,1,xormasklen,fp);
  505.   b=fread((void *)ICO_iconimage[imagenum].icAND,1,andmasklen,fp);
  506.   if (b<andmasklen) {
  507.     g_free(ICO_iconimage[imagenum].icXOR);
  508.     g_free(ICO_iconimage[imagenum].icAND);
  509.     g_free(buf);
  510.     return(-1);
  511.   }
  512.  
  513.   p=&ICO_iconimage[imagenum].icXOR[xornum-1];
  514.  
  515.   /* probably not the most efficient code, but it's fast, and
  516.      since the icon's are small anyway, doesn't take too much
  517.      memory
  518.    */
  519. #define height ICO_direntry[imagenum].bHeight
  520. #define width ICO_direntry[imagenum].bWidth
  521.  
  522.   for (j=0; j<height; j++) {
  523.     p=&ICO_iconimage[imagenum].icXOR[(height-j-1)*width];
  524.     for (i=0; i<width*ICO_iconimage[imagenum].icHeader.biBitCount/8; i++) {
  525.       k=buf[j*height*ICO_iconimage[imagenum].icHeader.biBitCount/8+i];
  526.       switch(ICO_iconimage[imagenum].icHeader.biBitCount) {
  527.         case 1:
  528.       *p++=BitSet(k,0x80);
  529.       *p++=BitSet(k,0x40);
  530.       *p++=BitSet(k,0x20);
  531.       *p++=BitSet(k,0x10);
  532.       *p++=BitSet(k,0x08);
  533.       *p++=BitSet(k,0x04);
  534.       *p++=BitSet(k,0x02);
  535.       *p++=BitSet(k,0x01);
  536.       break;
  537.     case 4:
  538.       *p++=(k>>4) & 0x0f;
  539.       *p++=k & 0x0f;
  540.       break;
  541.     case 8:
  542.       *p++=k;
  543.       break;
  544.     default:
  545.       if (debug_level & 0x01)
  546.         printf("ICO:  invalid bit count\n");
  547.       g_free(ICO_iconimage[imagenum].icXOR);
  548.       g_free(ICO_iconimage[imagenum].icAND);
  549.       g_free(buf);
  550.       return(-1);
  551.       }
  552.     }
  553.   }
  554.   g_free(buf);
  555.   return(0);
  556.  
  557. #undef height
  558. #undef width
  559. }
  560.  
  561.  
  562.  
  563. static gint32 make_image(void) {
  564.  
  565.   ICO_iconimage=g_new(ICO_IconImage,ICO_header.idCount);
  566.   if (ICO_iconimage==NULL) {  /* shouldnt happen */
  567.     if (debug_level & 0x01)
  568.       printf("ICO1:  fatal error:  out of memory!\n");
  569.     return(-1);
  570.   }
  571.  
  572.   /* for future expansion although we only read 1 image */
  573. /*  for (i=0; i<ICO_header.idCount; i++) { */
  574.  
  575.   fseek(fp,ICO_direntry[0].dwImageOffset,SEEK_SET);
  576.  
  577.   if (ICO_read_bitmapinfo(0)) {
  578.     if (debug_level & 0x01)
  579.       printf("ICO:  unable to read bitmap info\n");
  580.     return(-1);
  581.   }
  582.   if (ICO_read_rgbquad(0)) {
  583.     if (debug_level & 0x01)
  584.       printf("ICO:  unable to read palette table\n");
  585.     return(-1);
  586.   }
  587.   if (ICO_read_pixeldata(0)) {
  588.     if (debug_level & 0x01)
  589.       printf("ICO:  unable to read pixel data\n");
  590.     return(-1); 
  591.   }
  592.  
  593.   fclose(fp);
  594.  
  595.   {
  596.     int width,height;
  597.     int i,j;
  598.     GPixelRgn pixel_rgn;
  599.     gint32 image;
  600.     gint32 layer;
  601.     GDrawable *drawable;
  602.     guchar gimp_cmap[768];
  603.     char *name;
  604.     int xornum;
  605.  
  606.     name=g_malloc(strlen(filename)+15);
  607.     sprintf(name,"Loading %s:",filename);
  608.     gimp_progress_init(name);
  609.     g_free(name);
  610.  
  611.     width=ICO_direntry[0].bWidth;
  612.     height=ICO_direntry[0].bHeight;
  613.  
  614.     image=gimp_image_new(width,height,INDEXED);
  615.     layer=gimp_layer_new(image,"Background",width,height,INDEXED_IMAGE,100,NORMAL_MODE);
  616.     gimp_image_set_filename(image,filename);
  617.  
  618.     if (debug_level & 0x04) {
  619.     printf("ICO:  width==%d\n",width);
  620.     printf("ICO:  height==%d\n",height);
  621.     printf("ICO:  image==%ld\n",(long)image);
  622.     printf("ICO:  layer==%ld\n",(long)layer);
  623.     }
  624.     gimp_image_add_layer(image,layer,0);
  625.     drawable=gimp_drawable_get(layer);
  626.     gimp_pixel_rgn_init(&pixel_rgn,drawable,0,0,drawable->width,drawable->height,TRUE,FALSE);
  627.     
  628.     if (debug_level & 0x04)
  629.     printf("ICO:  after gimp_pixel_rgn_init\n");
  630.  
  631.     xornum=(ICO_direntry[0].bWidth*ICO_direntry[0].bHeight);
  632.     
  633.     gimp_pixel_rgn_set_rect(&pixel_rgn,ICO_iconimage[0].icXOR,0,0,drawable->width,drawable->height);
  634.  
  635.  
  636.     for (i=j=0; i<ICO_direntry[0].bColorCount; i++) {
  637.       gimp_cmap[j++]=ICO_iconimage[0].icColors[i].rgbRed;
  638.       gimp_cmap[j++]=ICO_iconimage[0].icColors[i].rgbGreen;
  639.       gimp_cmap[j++]=ICO_iconimage[0].icColors[i].rgbBlue;
  640.     }
  641.     gimp_progress_update((double)100.0);
  642.     gimp_image_set_cmap(image,gimp_cmap,ICO_direntry[0].bColorCount);
  643.     gimp_drawable_flush(drawable);
  644.     gimp_drawable_detach(drawable);
  645.     return(image);
  646. /*    gimp_pixel_rgn_set_rect(&pixel_rgn,ICO_iconimage[0].icXOR,0,0,drawable->width,height); */
  647.   }
  648. /*  } */
  649. }
  650.  
  651. #ifdef DEBUG
  652. int main(int argc, char **argv) {
  653.   int i,j;
  654.   gint32 ico_err;
  655.  
  656.   if (argc!=2) {
  657.     printf("no file!\n");
  658.     return(1);
  659.   }
  660.  
  661.   ico_err=ReadICON(argv[1]);
  662.   
  663.   return(ico_err);
  664. }
  665. #endif
  666.  
  667.   
  668.