home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / src / xpm / XpmWrFFrI.c < prev    next >
C/C++ Source or Header  |  2000-07-29  |  9KB  |  342 lines

  1. /* Copyright 1990-92 GROUPE BULL -- See license conditions in file COPYRIGHT */
  2. /*****************************************************************************\
  3. * XpmWrFFrI.c:                                                                *
  4. *                                                                             *
  5. *  XPM library                                                                *
  6. *  Write an image and possibly its mask to an XPM file                        *
  7. *                                                                             *
  8. *  Developed by Arnaud Le Hors                                                *
  9. \*****************************************************************************/
  10.  
  11. #include "xpmP.h"
  12. #ifdef VMS
  13. #include "sys$library:string.h"
  14. #else
  15. #if defined(SYSV) || defined(SVR4)
  16. #include <string.h>
  17. #define index strchr
  18. #define rindex strrchr
  19. #else
  20. #include <strings.h>
  21. #endif
  22. #endif
  23.  
  24. LFUNC(WriteTransparentColor, void, (FILE *file, char **colors,
  25.                     unsigned int cpp, unsigned int mask_pixel,
  26.                     char ***colorTable));
  27.  
  28. LFUNC(WriteOtherColors, void, (FILE *file, char **colors, XColor *xcolors,
  29.                    unsigned int ncolors, unsigned int cpp,
  30.                    unsigned int mask_pixel, char ***colorTable,
  31.                    unsigned int ncolors2, Pixel *pixels,
  32.                    char *rgb_fname));
  33.  
  34. LFUNC(WritePixels, int, (FILE *file, unsigned int width, unsigned int height,
  35.              unsigned int cpp, unsigned int *pixels,
  36.              char **colors));
  37.  
  38. LFUNC(WriteExtensions, void, (FILE *file, XpmExtension *ext,
  39.                   unsigned int num));
  40.  
  41. int
  42. XpmWriteFileFromImage(display, filename, image, shapeimage, attributes)
  43.     Display *display;
  44.     char *filename;
  45.     XImage *image;
  46.     XImage *shapeimage;
  47.     XpmAttributes *attributes;
  48. {
  49.     xpmData mdata;
  50.     char *name, *dot, *s, *new_name = NULL;
  51.     int ErrorStatus;
  52.     xpmInternAttrib attrib;
  53.  
  54.     if ((ErrorStatus = xpmWriteFile(filename, &mdata)) != XpmSuccess)
  55.     return (ErrorStatus);
  56.  
  57.     if (filename) {
  58. #ifdef VMS
  59.     name = filename;
  60. #else
  61.     if (!(name = rindex(filename, '/')))
  62.         name = filename;
  63.     else
  64.         name++;
  65. #endif
  66.     if (dot = index(name, '.')) {
  67.         new_name = (char*)strdup(name);
  68.         if (!new_name) {
  69.         new_name = NULL;
  70.         name = "image_name";
  71.         } else {
  72.         /* change '.' to '_' to get a valid C syntax name */
  73.         name = s = new_name;
  74.         while (dot = index(s, '.')) {
  75.             *dot = '_';
  76.             s = dot;
  77.         }
  78.         }
  79.     }
  80.     } else
  81.     name = "image_name";
  82.  
  83.     xpmInitInternAttrib(&attrib);
  84.  
  85.     /*
  86.      * Scan image then write it out 
  87.      */
  88.     ErrorStatus = xpmScanImage(display, image, shapeimage,
  89.                    attributes, &attrib);
  90.  
  91.     if (ErrorStatus == XpmSuccess) {
  92.     attrib.name = name;
  93.     ErrorStatus = xpmWriteData(&mdata, &attrib, attributes);
  94.     }
  95.     xpmFreeInternAttrib(&attrib);
  96.     XpmDataClose(&mdata);
  97.     if (new_name)
  98.     free(name);
  99.  
  100.     return (ErrorStatus);
  101. }
  102.  
  103.  
  104. int
  105. xpmWriteData(mdata, attrib, attributes)
  106.     xpmData *mdata;
  107.     xpmInternAttrib *attrib;
  108.     XpmAttributes *attributes;
  109. {
  110.     /* calculation variables */
  111.     unsigned int offset, infos;
  112.     FILE *file;
  113.     int ErrorStatus;
  114.  
  115.     /* store this to speed up */
  116.     file = mdata->stream.file;
  117.  
  118.     infos = attributes && (attributes->valuemask & XpmInfos);
  119.  
  120.     /*
  121.      * print the header line 
  122.      */
  123.     fprintf(file, "/* XPM */\nstatic char * %s[] = {\n", attrib->name);
  124.  
  125.     /*
  126.      * print the hints line 
  127.      */
  128.     if (infos && attributes->hints_cmt)
  129.     fprintf(file, "/*%s*/\n", attributes->hints_cmt);
  130.  
  131.     fprintf(file, "\"%d %d %d %d", attrib->width, attrib->height,
  132.         attrib->ncolors, attrib->cpp);
  133.  
  134.     if (attributes && (attributes->valuemask & XpmHotspot))
  135.     fprintf(file, " %d %d", attributes->x_hotspot, attributes->y_hotspot);
  136.  
  137.     if (attributes && (attributes->valuemask & XpmExtensions)
  138.     && attributes->nextensions)
  139.     fprintf(file, " XPMEXT");
  140.  
  141.     fprintf(file, "\",\n");
  142.  
  143.     /*
  144.      * print colors 
  145.      */
  146.     if (infos && attributes->colors_cmt)
  147.     fprintf(file, "/*%s*/\n", attributes->colors_cmt);
  148.  
  149.     /* transparent color */
  150.     if (attrib->mask_pixel != UNDEF_PIXEL) {
  151.     WriteTransparentColor(file, attrib->colorStrings, attrib->cpp,
  152.                   (infos ? attributes->mask_pixel : 0),
  153.                   (infos ? attributes->colorTable : NULL));
  154.     offset = 1;
  155.     } else
  156.     offset = 0;
  157.  
  158.     /* other colors */
  159.     WriteOtherColors(file, attrib->colorStrings + offset,
  160.              attrib->xcolors + offset, attrib->ncolors - offset,
  161.              attrib->cpp, (infos ? attributes->mask_pixel : 0),
  162.              (infos ? attributes->colorTable : NULL),
  163.              (infos ? attributes->ncolors : 0),
  164.              (infos ? attributes->pixels : NULL),
  165.              (attributes && (attributes->valuemask & XpmRgbFilename) ?
  166.               attributes->rgb_fname : NULL));
  167.  
  168.     /*
  169.      * print pixels 
  170.      */
  171.     if (infos && attributes->pixels_cmt)
  172.     fprintf(file, "/*%s*/\n", attributes->pixels_cmt);
  173.  
  174.     ErrorStatus = WritePixels(file, attrib->width, attrib->height, attrib->cpp,
  175.                   attrib->pixelindex, attrib->colorStrings);
  176.     if (ErrorStatus != XpmSuccess)
  177.     return(ErrorStatus);
  178.  
  179.     /*
  180.      * print extensions
  181.      */
  182.     if (attributes && (attributes->valuemask & XpmExtensions)
  183.     && attributes->nextensions)
  184.     WriteExtensions(file, attributes->extensions, attributes->nextensions);
  185.  
  186.     /* close the array */
  187.     fprintf(file, "};\n");
  188.  
  189.     return (XpmSuccess);
  190. }
  191.  
  192. static void
  193. WriteTransparentColor(file, colors, cpp, mask_pixel, colorTable)
  194. FILE *file;
  195. char **colors;
  196. unsigned int cpp;
  197. unsigned int mask_pixel;
  198. char ***colorTable;
  199. {
  200.     unsigned int key, i;
  201.     char *s;
  202.  
  203.     putc('"', file);
  204.     for (i = 0, s = *colors; i < cpp; i++, s++)
  205.     putc(*s, file);
  206.  
  207.     if (colorTable && mask_pixel != UNDEF_PIXEL) {
  208.     for (key = 1; key <= NKEYS; key++) {
  209.         if (s = colorTable[mask_pixel][key])
  210.         fprintf(file, "\t%s %s", xpmColorKeys[key - 1], s);
  211.     }
  212.     } else
  213.     fprintf(file, "\tc %s", TRANSPARENT_COLOR);
  214.  
  215.     fprintf(file, "\",\n");
  216. }
  217.  
  218. static void
  219. WriteOtherColors(file, colors, xcolors, ncolors, cpp, mask_pixel, colorTable,
  220.          ncolors2, pixels, rgb_fname)
  221. FILE *file;
  222. char **colors;
  223. XColor *xcolors;
  224. unsigned int ncolors;
  225. unsigned int cpp;
  226. unsigned int mask_pixel;
  227. char ***colorTable;
  228. unsigned int ncolors2;
  229. Pixel *pixels;
  230. char *rgb_fname;
  231. {
  232.     unsigned int a, b, c, d, key;
  233.     char *s, *colorname;
  234.     xpmRgbName rgbn[MAX_RGBNAMES];
  235.     int rgbn_max = 0;
  236.  
  237.     /* read the rgb file if any was specified */
  238.     if (rgb_fname)
  239.     rgbn_max = xpmReadRgbNames(rgb_fname, rgbn);
  240.  
  241.     for (a = 0; a < ncolors; a++, colors++, xcolors++) {
  242.  
  243.     putc('"', file);
  244.     for (b = 0, s = *colors; b < cpp; b++, s++)
  245.         putc(*s, file);
  246.  
  247.     c = 1;
  248.     if (colorTable) {
  249.         d = 0;
  250.         for (b = 0; b < ncolors2; b++) {
  251.         if (b == mask_pixel) {
  252.             d = 1;
  253.             continue;
  254.         }
  255.         if (pixels[b - d] == xcolors->pixel)
  256.             break;
  257.         }
  258.         if (b != ncolors2) {
  259.         c = 0;
  260.         for (key = 1; key <= NKEYS; key++) {
  261.             if (s = colorTable[b][key])
  262.             fprintf(file, "\t%s %s", xpmColorKeys[key - 1], s);
  263.         }
  264.         }
  265.     }
  266.     if (c) {
  267.         colorname = NULL;
  268.         if (rgbn_max)
  269.         colorname = xpmGetRgbName(rgbn, rgbn_max, xcolors->red,
  270.                       xcolors->green, xcolors->blue);
  271.         if (colorname)
  272.         fprintf(file, "\tc %s", colorname);
  273.         else
  274.         fprintf(file, "\tc #%04X%04X%04X", xcolors->red,
  275.             xcolors->green, xcolors->blue);
  276.     }
  277.     fprintf(file, "\",\n");
  278.     }
  279.     xpmFreeRgbNames(rgbn, rgbn_max);
  280. }
  281.  
  282.  
  283. static int
  284. WritePixels(file, width, height, cpp, pixels, colors)
  285. FILE *file;
  286. unsigned int width;
  287. unsigned int height;
  288. unsigned int cpp;
  289. unsigned int *pixels;
  290. char **colors;
  291. {
  292.     char *s, *p, *buf;
  293.     unsigned int x, y, h;
  294.  
  295.     h = height - 1;
  296.     p = buf = (char *) malloc(width * cpp + 3);
  297.     *buf = '"';
  298.     if (!buf)
  299.     return(XpmNoMemory);
  300.     p++;
  301.     for (y = 0; y < h; y++) {
  302.     s = p;
  303.     for (x = 0; x < width; x++, pixels++) {
  304.         strncpy(s, colors[*pixels], cpp);
  305.         s += cpp;
  306.     }
  307.     *s++ = '"';
  308.     *s = '\0';
  309.     fprintf(file, "%s,\n", buf);
  310.     }
  311.     /* duplicate some code to avoid a test in the loop */
  312.     s = p;
  313.     for (x = 0; x < width; x++, pixels++) {
  314.     strncpy(s, colors[*pixels], cpp);
  315.     s += cpp;
  316.     }
  317.     *s++ = '"';
  318.     *s = '\0';
  319.     fprintf(file, "%s", buf);
  320.  
  321.     free(buf);
  322.     return(XpmSuccess);
  323. }
  324.  
  325. static void
  326. WriteExtensions(file, ext, num)
  327. FILE *file;
  328. XpmExtension *ext;
  329. unsigned int num;
  330. {
  331.     unsigned int x, y, n;
  332.     char **line;
  333.  
  334.     for (x = 0; x < num; x++, ext++) {
  335.     fprintf(file, ",\n\"XPMEXT %s\"", ext->name);
  336.     n = ext->nlines;
  337.     for (y = 0, line = ext->lines; y < n; y++, line++)
  338.         fprintf(file, ",\n\"%s\"", *line);
  339.     }
  340.     fprintf(file, ",\n\"XPMENDEXT\"");
  341. }
  342.