home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / common / xpm.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-24  |  21.8 KB  |  850 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 2 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17.  */
  18.  
  19. /* XPM plugin version 1.2.3 */
  20.  
  21. /*
  22. 1.2.3 fixes bug when running in noninteractive mode
  23. changes alpha_threshold range from [0, 1] to [0,255] for consistency with
  24. the threshold_alpha plugin
  25.  
  26. 1.2.2 fixes bug that generated bad digits on images with more than 20000
  27. colors. (thanks, yanele)
  28. parses gtkrc (thanks, yosh)
  29. doesn't load parameter screen on images that don't have alpha
  30.  
  31. 1.2.1 fixes some minor bugs -- spaces in #XXXXXX strings, small typos in code.
  32.  
  33. 1.2 compute color indexes so that we don't have to use XpmSaveXImage*
  34.  
  35. Previous...Inherited code from Ray Lehtiniemi, who inherited it from S & P.
  36. */
  37.  
  38. #include "config.h"
  39.  
  40. #include <stdlib.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <X11/Xlib.h>
  44. #include <X11/xpm.h>
  45.  
  46. #include <gtk/gtk.h>
  47.  
  48. #include <libgimp/gimp.h>
  49. #include <libgimp/gimpui.h>
  50.  
  51. #include "libgimp/stdplugins-intl.h"
  52.  
  53.  
  54. static const gchar linenoise [] =
  55. " .+@#$%&*=-;>,')!~{]^/(_:<[}|1234567890abcdefghijklmnopqrstuvwxyz\
  56. ABCDEFGHIJKLMNOPQRSTUVWXYZ`";
  57.  
  58. #define SCALE_WIDTH 125
  59.  
  60. /* Structs for the save dialog */
  61. typedef struct
  62. {
  63.   gint threshold;
  64. } XpmSaveVals;
  65.  
  66. typedef struct
  67. {
  68.   gint run;
  69. } XpmSaveInterface;
  70.  
  71.  
  72. typedef struct
  73. {
  74.   guchar r;
  75.   guchar g;
  76.   guchar b;
  77. } rgbkey;
  78.  
  79. /*  whether the image is color or not.  global so I only have to pass
  80.  *  one user value to the GHFunc
  81.  */
  82. gboolean   color;
  83.  
  84. /*  bytes per pixel.  global so I only have to pass one user value
  85.  *  to the GHFunc
  86.  */
  87. gint       cpp;
  88.  
  89. /* Declare local functions */
  90. static void     query               (void);
  91. static void     run                 (gchar         *name,
  92.                      gint           nparams,
  93.                      GimpParam        *param,
  94.                      gint          *nreturn_vals,
  95.                      GimpParam       **return_vals);
  96.  
  97. static gint32   load_image          (gchar         *filename);
  98. static void     parse_colors        (XpmImage      *xpm_image,
  99.                      guchar       **cmap);
  100. static void     parse_image         (gint32         image_ID,
  101.                      XpmImage      *xpm_image,
  102.                      guchar        *cmap);
  103. static gboolean save_image          (gchar         *filename,
  104.                      gint32         image_ID,
  105.                      gint32         drawable_ID);
  106.  
  107. static gint     save_dialog         (void);
  108. static void     save_ok_callback    (GtkWidget     *widget,
  109.                      gpointer       data);
  110.  
  111.  
  112. GimpPlugInInfo PLUG_IN_INFO =
  113. {
  114.   NULL,  /* init_proc  */
  115.   NULL,  /* quit_proc  */
  116.   query, /* query_proc */
  117.   run,   /* run_proc   */
  118. };
  119.  
  120. static XpmSaveVals xpmvals = 
  121. {
  122.   127  /* alpha threshold */
  123. };
  124.  
  125. static XpmSaveInterface xpmint =
  126. {
  127.   FALSE   /*  run  */
  128. };
  129.  
  130.  
  131. MAIN ()
  132.  
  133. static void
  134. query (void)
  135. {
  136.   static GimpParamDef load_args[] =
  137.   {
  138.     { GIMP_PDB_INT32,     "run_mode",     "Interactive, non-interactive" },
  139.     { GIMP_PDB_STRING,    "filename",     "The name of the file to load" },
  140.     { GIMP_PDB_STRING,    "raw_filename", "The name entered" }
  141.   };
  142.   static gint nload_args        = sizeof (load_args) / sizeof (load_args[0]);
  143.  
  144.   static GimpParamDef load_return_vals[] =
  145.   {
  146.     { GIMP_PDB_IMAGE,    "image",         "Output image" }
  147.   };
  148.   static gint nload_return_vals = (sizeof (load_return_vals) /
  149.                    sizeof (load_return_vals[0]));
  150.  
  151.   static GimpParamDef save_args[] =
  152.   {
  153.     { GIMP_PDB_INT32,    "run_mode",      "Interactive, non-interactive" },
  154.     { GIMP_PDB_IMAGE,    "image",         "Input image" },
  155.     { GIMP_PDB_DRAWABLE, "drawable",      "Drawable to save" },
  156.     { GIMP_PDB_STRING,   "filename",      "The name of the file to save the image in" },
  157.     { GIMP_PDB_STRING,   "raw_filename",  "The name of the file to save the image in" },
  158.     { GIMP_PDB_INT32,    "threshold",     "Alpha threshold (0-255)" }
  159.   };
  160.   static gint nsave_args = sizeof (save_args) / sizeof (save_args[0]);
  161.  
  162.   gimp_install_procedure ("file_xpm_load",
  163.                           "loads files of the xpm file format",
  164.                           "FIXME: write help for xpm_load",
  165.                           "Spencer Kimball & Peter Mattis & Ray Lehtiniemi",
  166.                           "Spencer Kimball & Peter Mattis",
  167.                           "1997",
  168.                           "<Load>/Xpm",
  169.                           NULL,
  170.                           GIMP_PLUGIN,
  171.                           nload_args, nload_return_vals,
  172.                           load_args, load_return_vals);
  173.   
  174.   gimp_install_procedure ("file_xpm_save",
  175.                           "saves files in the xpm file format (if you're on a 16 bit display...)",
  176.                           "FIXME: write help for xpm",
  177.                           "Spencer Kimball & Peter Mattis & Ray Lehtiniemi & Nathan Summers",
  178.                           "Spencer Kimball & Peter Mattis",
  179.                           "1997",
  180.                           "<Save>/Xpm",
  181.                           "RGB*, GRAY*, INDEXED*",
  182.                           GIMP_PLUGIN,
  183.                           nsave_args, 0,
  184.                           save_args, NULL);
  185.  
  186.   gimp_register_magic_load_handler ("file_xpm_load",
  187.                     "xpm",
  188.                     "<Load>/Xpm",
  189.                     "0, string,/*\\040XPM\\040*/");
  190.   gimp_register_save_handler ("file_xpm_save",
  191.                   "xpm",
  192.                   "<Save>/Xpm");
  193. }
  194.  
  195. static void
  196. run (gchar   *name,
  197.      gint     nparams,
  198.      GimpParam  *param,
  199.      gint    *nreturn_vals,
  200.      GimpParam **return_vals)
  201. {
  202.   static GimpParam values[2];
  203.   GimpRunModeType  run_mode;
  204.   GimpPDBStatusType   status = GIMP_PDB_SUCCESS;
  205.   gint32        image_ID;
  206.   gint32        drawable_ID;
  207.   GimpExportReturnType export = GIMP_EXPORT_CANCEL;
  208.  
  209.   run_mode = param[0].data.d_int32;
  210.  
  211.   *nreturn_vals = 1;
  212.   *return_vals  = values;
  213.   values[0].type = GIMP_PDB_STATUS;
  214.   values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
  215.  
  216.   if (strcmp (name, "file_xpm_load") == 0)
  217.     {
  218.       INIT_I18N();
  219.  
  220.       image_ID = load_image (param[1].data.d_string);
  221.  
  222.       if (image_ID != -1)
  223.         {
  224.           *nreturn_vals = 2;
  225.           values[1].type         = GIMP_PDB_IMAGE;
  226.           values[1].data.d_image = image_ID;
  227.         }
  228.       else
  229.         {
  230.           status = GIMP_PDB_EXECUTION_ERROR;
  231.         }
  232.     }
  233.   else if (strcmp (name, "file_xpm_save") == 0)
  234.     {
  235.       INIT_I18N_UI();
  236.  
  237.       gimp_ui_init ("xpm", FALSE);
  238.  
  239.       image_ID    = param[1].data.d_int32;
  240.       drawable_ID = param[2].data.d_int32;
  241.  
  242.       /*  eventually export the image */ 
  243.       switch (run_mode)
  244.     {
  245.     case GIMP_RUN_INTERACTIVE:
  246.     case GIMP_RUN_WITH_LAST_VALS:
  247.       export = gimp_export_image (&image_ID, &drawable_ID, "XPM", 
  248.                       (GIMP_EXPORT_CAN_HANDLE_RGB |
  249.                        GIMP_EXPORT_CAN_HANDLE_GRAY |
  250.                        GIMP_EXPORT_CAN_HANDLE_INDEXED |
  251.                        GIMP_EXPORT_CAN_HANDLE_ALPHA ));
  252.       if (export == GIMP_EXPORT_CANCEL)
  253.         {
  254.           values[0].data.d_status = GIMP_PDB_CANCEL;
  255.           return;
  256.         }
  257.       break;
  258.     default:
  259.       break;
  260.     }
  261.  
  262.       switch (run_mode)
  263.     {
  264.     case GIMP_RUN_INTERACTIVE:
  265.       /*  Possibly retrieve data  */
  266.       gimp_get_data ("file_xpm_save", &xpmvals);
  267.  
  268.       /*  First acquire information with a dialog  */
  269.       if (gimp_drawable_has_alpha (drawable_ID))
  270.         if (! save_dialog ())
  271.           status = GIMP_PDB_CANCEL;
  272.       break;
  273.  
  274.     case GIMP_RUN_NONINTERACTIVE:
  275.       /*  Make sure all the arguments are there!  */
  276.       if (nparams != 6)
  277.         {
  278.           status = GIMP_PDB_CALLING_ERROR;
  279.         }
  280.       else
  281.         {
  282.           xpmvals.threshold = param[5].data.d_int32;
  283.  
  284.           if (xpmvals.threshold < 0 ||
  285.           xpmvals.threshold > 255)
  286.         status = GIMP_PDB_CALLING_ERROR;
  287.         }
  288.       break;
  289.  
  290.     case GIMP_RUN_WITH_LAST_VALS:
  291.       /*  Possibly retrieve data  */
  292.       gimp_get_data ("file_xpm_save", &xpmvals);
  293.       break;
  294.  
  295.     default:
  296.       break;
  297.     }
  298.  
  299.       if (status == GIMP_PDB_SUCCESS)
  300.     {
  301.       if (save_image (param[3].data.d_string,
  302.               image_ID,
  303.               drawable_ID))
  304.         {
  305.           gimp_set_data ("file_xpm_save", &xpmvals, sizeof (XpmSaveVals));
  306.         }
  307.       else
  308.         {
  309.           status = GIMP_PDB_EXECUTION_ERROR;
  310.         }
  311.     }
  312.  
  313.       if (export == GIMP_EXPORT_EXPORT)
  314.     gimp_image_delete (image_ID);
  315.     }
  316.   else
  317.     {
  318.       status = GIMP_PDB_CALLING_ERROR;
  319.     }
  320.  
  321.   values[0].data.d_status = status;
  322. }
  323.  
  324.  
  325. static gint32
  326. load_image (gchar *filename)
  327. {
  328.   XpmImage  xpm_image;
  329.   guchar   *cmap;
  330.   gint32    image_ID;
  331.   gchar    *name;
  332.  
  333.   /* put up a progress bar */
  334.   name = g_strdup_printf (_("Loading %s:"), filename);
  335.   gimp_progress_init (name);
  336.   g_free (name);
  337.  
  338.   /* read the raw file */
  339.   XpmReadFileToXpmImage (filename, &xpm_image, NULL);
  340.  
  341.   /* parse out the colors into a cmap */
  342.   parse_colors (&xpm_image, &cmap);
  343.   if (cmap == NULL)
  344.     gimp_quit();
  345.  
  346.   /* create the new image */
  347.   image_ID = gimp_image_new (xpm_image.width,
  348.                              xpm_image.height,
  349.                              GIMP_RGB);
  350.  
  351.   /* name it */
  352.   gimp_image_set_filename (image_ID, filename);
  353.  
  354.   /* fill it */
  355.   parse_image(image_ID, &xpm_image, cmap);
  356.   
  357.   /* clean up and exit */
  358.   g_free(cmap);
  359.   
  360.   return image_ID;
  361. }
  362.  
  363. static void
  364. parse_colors (XpmImage  *xpm_image, 
  365.           guchar   **cmap)
  366. {
  367.   Display  *display;
  368.   Colormap  colormap;
  369.   gint      i, j;
  370.  
  371.   /* open the display and get the default color map */
  372.   display  = XOpenDisplay (NULL);
  373.   colormap = DefaultColormap (display, DefaultScreen (display));
  374.     
  375.   /* alloc a buffer to hold the parsed colors */
  376.   *cmap = g_new (guchar, sizeof (guchar) * 4 * xpm_image->ncolors);
  377.  
  378.   if ((*cmap) != NULL)
  379.     {
  380.       /* default to black transparent */
  381.       memset((void*)(*cmap), 0, sizeof (guchar) * 4 * xpm_image->ncolors);
  382.       
  383.       /* parse each color in the file */
  384.       for (i = 0, j = 0; i < xpm_image->ncolors; i++)
  385.         {
  386.           gchar     *colorspec = "None";
  387.           XpmColor *xpm_color;
  388.           XColor    xcolor;
  389.         
  390.           xpm_color = &(xpm_image->colorTable[i]);
  391.         
  392.           /* pick the best spec available */
  393.           if (xpm_color->c_color)
  394.             colorspec = xpm_color->c_color;
  395.           else if (xpm_color->g_color)
  396.             colorspec = xpm_color->g_color;
  397.           else if (xpm_color->g4_color)
  398.             colorspec = xpm_color->g4_color;
  399.           else if (xpm_color->m_color)
  400.             colorspec = xpm_color->m_color;
  401.         
  402.           /* parse if it's not transparent.  the assumption is that
  403.              g_new will memset the buffer to zeros */
  404.           if (strcmp (colorspec, "None") != 0)
  405.         {
  406.           XParseColor (display, colormap, colorspec, &xcolor);
  407.           (*cmap)[j++] = xcolor.red >> 8;
  408.           (*cmap)[j++] = xcolor.green >> 8;
  409.           (*cmap)[j++] = xcolor.blue >> 8;
  410.           (*cmap)[j++] = ~0;
  411.         }
  412.       else
  413.         {
  414.           j += 4;
  415.         }
  416.         }
  417.     }
  418.     
  419.   XCloseDisplay (display);
  420. }
  421.  
  422. static void
  423. parse_image (gint32    image_ID, 
  424.          XpmImage *xpm_image, 
  425.          guchar   *cmap)
  426. {
  427.   gint       tile_height;
  428.   gint       scanlines;
  429.   gint       val;  
  430.   guchar    *buf;
  431.   guchar    *dest;
  432.   guint     *src;
  433.   GimpPixelRgn  pixel_rgn;
  434.   GimpDrawable *drawable;
  435.   gint32     layer_ID;
  436.   gint       i, j;
  437.     
  438.   layer_ID = gimp_layer_new (image_ID,
  439.                              _("Color"),
  440.                              xpm_image->width,
  441.                              xpm_image->height,
  442.                              GIMP_RGBA_IMAGE,
  443.                              100,
  444.                              GIMP_NORMAL_MODE);
  445.     
  446.   gimp_image_add_layer (image_ID, layer_ID, 0);
  447.  
  448.   drawable = gimp_drawable_get (layer_ID);
  449.     
  450.   gimp_pixel_rgn_init (&pixel_rgn, drawable,
  451.                        0, 0,
  452.                        drawable->width, drawable->height,
  453.                        TRUE, FALSE);
  454.  
  455.   tile_height = gimp_tile_height ();
  456.     
  457.   buf  = g_new (guchar, tile_height * xpm_image->width * 4);
  458.  
  459.   if (buf != NULL)
  460.     {
  461.       src  = xpm_image->data;
  462.       for (i = 0; i < xpm_image->height; i+=tile_height)
  463.         {
  464.           dest = buf;
  465.           scanlines = MIN(tile_height, xpm_image->height - i);
  466.           j = scanlines * xpm_image->width;
  467.           while (j--) {
  468.             {
  469.               val = *(src++) * 4;
  470.               *(dest)   = cmap[val];
  471.               *(dest+1) = cmap[val+1];
  472.               *(dest+2) = cmap[val+2];
  473.               *(dest+3) = cmap[val+3];
  474.               dest += 4;
  475.             }
  476.           
  477.             if ((j % 100) == 0)
  478.               gimp_progress_update ((double) i / (double) xpm_image->height);
  479.           }
  480.         
  481.           gimp_pixel_rgn_set_rect (&pixel_rgn, buf,
  482.                                    0, i,
  483.                                    drawable->width, scanlines);
  484.         
  485.         }
  486.   
  487.       g_free(buf);
  488.     }
  489.  
  490.   gimp_drawable_detach (drawable);
  491. }
  492.  
  493. guint
  494. rgbhash (rgbkey *c)
  495. {
  496.   return ((guint)c->r) ^ ((guint)c->g) ^ ((guint)c->b);
  497. }
  498.  
  499. guint
  500. compare (rgbkey *c1, 
  501.      rgbkey *c2)
  502. {
  503.   return (c1->r == c2->r) && (c1->g == c2->g) && (c1->b == c2->b);
  504. }
  505.     
  506. void
  507. set_XpmImage (XpmColor *array, 
  508.           guint     index, 
  509.           gchar    *colorstring)
  510. {
  511.   gchar *p;
  512.   gint i, charnum, indtemp;
  513.   
  514.   indtemp=index;
  515.   array[index].string = p = g_new (gchar, cpp+1);
  516.   
  517.   /*convert the index number to base sizeof(linenoise)-1 */
  518.   for (i=0; i<cpp; ++i)
  519.     {
  520.       charnum = indtemp % (sizeof (linenoise) - 1);
  521.       indtemp = indtemp / (sizeof (linenoise) - 1);
  522.       *p++ = linenoise[charnum];
  523.     }
  524.   /* *p++=linenoise[indtemp]; */
  525.   
  526.   *p = '\0'; /* C and its stupid null-terminated strings... */
  527.  
  528.   array[index].symbolic = NULL;
  529.   array[index].m_color  = NULL;
  530.   array[index].g4_color = NULL;
  531.  
  532.   if (color)
  533.     {
  534.       array[index].g_color = NULL;
  535.       array[index].c_color = colorstring;
  536.     } else {    
  537.       array[index].c_color = NULL;
  538.       array[index].g_color = colorstring;
  539.     }
  540. }
  541.  
  542. void
  543. create_colormap_from_hash (gpointer gkey, 
  544.                gpointer value, 
  545.                gpointer user_data)
  546. {
  547.   rgbkey *key = gkey;
  548.   gchar *string = g_new(char, 8);
  549.  
  550.   sprintf (string, "#%02X%02X%02X", (int)key->r, (int)key->g, (int)key->b);
  551.   set_XpmImage (user_data, *((int *) value), string);
  552. }
  553.  
  554. static gboolean
  555. save_image (gchar  *filename,
  556.             gint32  image_ID,
  557.             gint32  drawable_ID)
  558. {
  559.   GimpDrawable *drawable;    
  560.  
  561.   gint       width;
  562.   gint       height;
  563.   gint          ncolors = 1;
  564.   gint        *indexno;
  565.   gboolean   indexed;
  566.   gboolean   alpha;
  567.  
  568.   XpmColor  *colormap;
  569.   XpmImage  *image;
  570.  
  571.   guint     *ibuff   = NULL;
  572.   /*guint   *mbuff   = NULL;*/
  573.   GimpPixelRgn  pixel_rgn;
  574.   guchar    *buffer;
  575.   guchar    *data;
  576.  
  577.   GHashTable *hash = NULL;
  578.  
  579.   gint       i, j, k;
  580.   gint       threshold = xpmvals.threshold;
  581.  
  582.   gboolean   rc = FALSE;
  583.  
  584.   /* get some basic stats about the image */
  585.   switch (gimp_drawable_type (drawable_ID)) 
  586.     {
  587.     case GIMP_RGBA_IMAGE:
  588.     case GIMP_INDEXEDA_IMAGE:
  589.     case GIMP_GRAYA_IMAGE:
  590.       alpha = TRUE;
  591.       break;
  592.     case GIMP_RGB_IMAGE:
  593.     case GIMP_INDEXED_IMAGE:
  594.     case GIMP_GRAY_IMAGE:
  595.       alpha = FALSE;
  596.       break;
  597.     default:
  598.       return FALSE;
  599.     }
  600.  
  601.   switch (gimp_drawable_type (drawable_ID)) 
  602.     {
  603.     case GIMP_GRAYA_IMAGE:
  604.     case GIMP_GRAY_IMAGE:
  605.       color = FALSE;
  606.       break;
  607.     case GIMP_RGBA_IMAGE:
  608.     case GIMP_RGB_IMAGE:
  609.     case GIMP_INDEXED_IMAGE:
  610.     case GIMP_INDEXEDA_IMAGE:            
  611.     color = TRUE;
  612.     break;
  613.     default:
  614.       return FALSE;
  615.     }
  616.   
  617.   switch (gimp_drawable_type (drawable_ID)) 
  618.     {
  619.     case GIMP_GRAYA_IMAGE:
  620.     case GIMP_GRAY_IMAGE:
  621.     case GIMP_RGBA_IMAGE:
  622.     case GIMP_RGB_IMAGE:
  623.       indexed = FALSE;
  624.       break;
  625.     case GIMP_INDEXED_IMAGE:
  626.     case GIMP_INDEXEDA_IMAGE:            
  627.       indexed = TRUE;
  628.       break;
  629.     default:
  630.       return FALSE;
  631.     }
  632.   
  633.   drawable = gimp_drawable_get (drawable_ID);
  634.   width    = drawable->width;
  635.   height   = drawable->height;
  636.   
  637.   /* allocate buffers making the assumption that ibuff and mbuff
  638.      are 32 bit aligned... */
  639.   if ((ibuff = g_new (guint, width * height)) == NULL)
  640.     goto cleanup;
  641.  
  642.   /*if ((mbuff = g_new(guint, width*height)) == NULL)
  643.     goto cleanup;*/
  644.   
  645.   if ((hash = g_hash_table_new ((GHashFunc)rgbhash, 
  646.                 (GCompareFunc) compare)) == NULL)
  647.     goto cleanup;
  648.   
  649.   /* put up a progress bar */
  650.   {
  651.     gchar *name;
  652.  
  653.     name = g_strdup_printf (_("Saving %s:"), filename);
  654.     gimp_progress_init (name);
  655.     g_free (name);
  656.   }
  657.  
  658.   
  659.   /* allocate a pixel region to work with */
  660.   if ((buffer = g_new (guchar, 
  661.                gimp_tile_height()*width*drawable->bpp)) == NULL)
  662.     return 0;
  663.  
  664.   gimp_pixel_rgn_init (&pixel_rgn, drawable,
  665.                        0, 0,
  666.                        width, height,
  667.                        TRUE, FALSE);
  668.  
  669.   /* process each row of tiles */
  670.   for (i = 0; i < height; i+=gimp_tile_height())
  671.     {
  672.       gint scanlines;
  673.   
  674.       /* read the next row of tiles */
  675.       scanlines = MIN (gimp_tile_height(), height - i);
  676.       gimp_pixel_rgn_get_rect (&pixel_rgn, buffer, 0, i, width, scanlines);
  677.       data = buffer;
  678.       
  679.       /* process each pixel row */
  680.       for (j=0; j<scanlines; j++)
  681.         {
  682.           /* go to the start of this row in each image */
  683.           guint *idata = ibuff + (i+j) * width;
  684.           /*guint *mdata = mbuff + (i+j) * width;*/
  685.  
  686.           /* do each pixel in the row */
  687.           for (k=0; k<width; k++)
  688.             {
  689.           rgbkey *key = g_new (rgbkey, 1);
  690.           guchar a;
  691.   
  692.               /* get pixel data */
  693.               key->r = *(data++);
  694.           key->g = color && !indexed ? *(data++) : key->r;
  695.           key->b = color && !indexed ? *(data++) : key->r;
  696.           a = alpha ? *(data++) : 255;
  697.           
  698.           if (a < threshold) 
  699.         {
  700.           *(idata++) = 0;
  701.         }
  702.           else
  703.         {
  704.           if (indexed)
  705.             {
  706.               *(idata++) = (key->r)+1;
  707.             }
  708.           else
  709.             {
  710.               indexno = g_hash_table_lookup (hash, key);
  711.               if (!indexno)
  712.             {
  713.               indexno = g_new (gint, 1);
  714.               *indexno = ncolors++;
  715.               g_hash_table_insert (hash, key, indexno);
  716.               key = g_new (rgbkey, 1);
  717.             }
  718.               *(idata++) = *indexno;
  719.             }
  720.         }
  721.             }
  722.  
  723.           /* kick the progress bar */
  724.           gimp_progress_update ((gdouble) (i+j) / (gdouble) height);
  725.         }    
  726.     } 
  727.   g_free (buffer);
  728.  
  729.   if (indexed)
  730.     {
  731.       guchar *cmap;
  732.       cmap = gimp_image_get_cmap(image_ID, &ncolors);
  733.       ncolors++; /* for transparency */
  734.       colormap = g_new (XpmColor, ncolors);
  735.       cpp = (gdouble) 1.0 + 
  736.     (gdouble) log (ncolors) / (double) log (sizeof (linenoise)-1.0);
  737.       set_XpmImage (colormap, 0, "None");
  738.       for (i=0; i<ncolors-1; i++)
  739.     {
  740.       gchar *string;
  741.       guchar r, g, b;
  742.       r = *(cmap++);
  743.       g = *(cmap++);
  744.       b = *(cmap++);
  745.       string = g_new (gchar, 8);
  746.       sprintf (string, "#%02X%02X%02X", (int)r, (int)g, (int)b);
  747.       set_XpmImage (colormap, i+1, string);
  748.     } 
  749.     }
  750.   else
  751.     {
  752.       colormap = g_new (XpmColor, ncolors);
  753.       
  754.       cpp = (gdouble) 1.0 + 
  755.     (gdouble) log (ncolors) / (gdouble) log (sizeof (linenoise) - 1.0);
  756.       set_XpmImage (colormap, 0, "None");
  757.       
  758.       g_hash_table_foreach (hash, create_colormap_from_hash, colormap);
  759.     }
  760.  
  761.   image = g_new (XpmImage, 1);
  762.   
  763.   image->width=width;
  764.   image->height=height;
  765.   image->ncolors=ncolors;
  766.   image->cpp=cpp;
  767.   image->colorTable=colormap;        
  768.   image->data = ibuff;
  769.   
  770.   /* do the save */
  771.   XpmWriteFileFromXpmImage (filename, image, NULL);
  772.   rc = TRUE;
  773.  
  774.  cleanup:
  775.   /* clean up resources */  
  776.   gimp_drawable_detach (drawable);
  777.   
  778.   if (ibuff) 
  779.     g_free (ibuff);
  780.   /*if (mbuff) g_free(mbuff);*/
  781.   if (hash)  
  782.     g_hash_table_destroy (hash);
  783.   
  784.   return rc;
  785. }
  786.  
  787. static gint
  788. save_dialog (void)
  789. {
  790.   GtkWidget *dlg;
  791.   GtkWidget *frame;
  792.   GtkWidget *table;
  793.   GtkObject *scale_data;
  794.  
  795.   dlg = gimp_dialog_new (_("Save as XPM"), "xpm",
  796.              gimp_standard_help_func, "filters/xpm.html",
  797.              GTK_WIN_POS_MOUSE,
  798.              FALSE, TRUE, FALSE,
  799.  
  800.              _("OK"), save_ok_callback,
  801.              NULL, NULL, NULL, TRUE, FALSE,
  802.              _("Cancel"), gtk_widget_destroy,
  803.              NULL, 1, NULL, FALSE, TRUE,
  804.  
  805.              NULL);
  806.  
  807.   gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
  808.               GTK_SIGNAL_FUNC (gtk_main_quit),
  809.               NULL);
  810.  
  811.   /*  parameter settings  */
  812.   frame = gtk_frame_new (_("Parameter Settings"));
  813.   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  814.   gtk_container_set_border_width (GTK_CONTAINER (frame), 6);
  815.   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), frame, TRUE, TRUE, 0);
  816.   gtk_widget_show (frame);
  817.  
  818.   table = gtk_table_new (1, 3, FALSE);
  819.   gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  820.   gtk_container_set_border_width (GTK_CONTAINER (table), 4);
  821.   gtk_container_add (GTK_CONTAINER (frame), table);
  822.   gtk_widget_show (table);
  823.  
  824.   scale_data = gimp_scale_entry_new (GTK_TABLE (table), 0, 0,
  825.                      _("Alpha Threshold:"), SCALE_WIDTH, 0,
  826.                      xpmvals.threshold, 0, 255, 1, 8, 0,
  827.                      TRUE, 0, 0,
  828.                      NULL, NULL);
  829.  
  830.   gtk_signal_connect (GTK_OBJECT (scale_data), "value_changed",
  831.               GTK_SIGNAL_FUNC (gimp_int_adjustment_update),
  832.               &xpmvals.threshold);
  833.  
  834.   gtk_widget_show (dlg);
  835.  
  836.   gtk_main ();
  837.   gdk_flush ();
  838.  
  839.   return xpmint.run;
  840. }
  841.  
  842. static void
  843. save_ok_callback (GtkWidget *widget,
  844.           gpointer   data)
  845. {
  846.   xpmint.run = TRUE;
  847.  
  848.   gtk_widget_destroy (GTK_WIDGET (data));
  849. }
  850.