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 / semiflatten.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-24  |  6.3 KB  |  234 lines

  1. /*
  2.  *  Semi-Flatten plug-in v1.0 by Adam D. Moss, adam@foxbox.org.  1998/01/27
  3.  */
  4.  
  5. /* The GIMP -- an image manipulation program
  6.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21.  */
  22.  
  23. /*
  24.  * TODO:
  25.  *     Use tile iteration instead of dumb row-walking
  26.  */
  27.  
  28. #include "config.h"
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32.  
  33. #include <libgimp/gimp.h>
  34.  
  35. #include "libgimp/stdplugins-intl.h"
  36.  
  37.  
  38. /* Declare local functions.
  39.  */
  40. static void      query  (void);
  41. static void      run    (gchar   *name,
  42.              gint     nparams,
  43.              GimpParam  *param,
  44.              gint    *nreturn_vals,
  45.              GimpParam **return_vals);
  46.  
  47. static void      semiflatten            (GimpDrawable    *drawable);
  48. static void      semiflatten_render_row (const guchar *src_row,
  49.                      guchar       *dest_row,
  50.                      gint          row,
  51.                      gint          row_width,
  52.                      gint          bytes);
  53.  
  54.  
  55. static guchar bgred, bggreen, bgblue;
  56.  
  57.  
  58. GimpPlugInInfo PLUG_IN_INFO =
  59. {
  60.   NULL,  /* init_proc  */
  61.   NULL,  /* quit_proc  */
  62.   query, /* query_proc */
  63.   run,   /* run_proc   */
  64. };
  65.  
  66. MAIN ()
  67.  
  68. static void
  69. query (void)
  70. {
  71.   static GimpParamDef args[] =
  72.   {
  73.     { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
  74.     { GIMP_PDB_IMAGE, "image", "Input image (unused)" },
  75.     { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" }
  76.   };
  77.   static gint nargs = sizeof (args) / sizeof (args[0]);
  78.  
  79.   gimp_install_procedure ("plug_in_semiflatten",
  80.               "Flatten pixels in an RGBA image that aren't "
  81.               "completely transparent against the current GIMP "
  82.               "background color",
  83.               "This plugin flattens pixels in an RGBA image that "
  84.               "aren't completely transparent against the current "
  85.               "GIMP background color",
  86.               "Adam D. Moss (adam@foxbox.org)",
  87.               "Adam D. Moss (adam@foxbox.org)",
  88.               "27th January 1998",
  89.               N_("<Image>/Filters/Colors/Semi-Flatten"),
  90.               "RGBA",
  91.               GIMP_PLUGIN,
  92.               nargs, 0,
  93.               args, NULL);
  94. }
  95.  
  96.  
  97. static void
  98. run (gchar   *name,
  99.      gint     nparams,
  100.      GimpParam  *param,
  101.      gint    *nreturn_vals,
  102.      GimpParam **return_vals)
  103. {
  104.   static GimpParam values[1];
  105.   GimpDrawable *drawable;
  106.   gint32 image_ID;
  107.   GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  108.  
  109.   *nreturn_vals = 1;
  110.   *return_vals = values;
  111.  
  112.   values[0].type = GIMP_PDB_STATUS;
  113.   values[0].data.d_status = status;
  114.  
  115.   INIT_I18N();
  116.  
  117.   /*  Get the specified drawable  */
  118.   drawable = gimp_drawable_get (param[2].data.d_drawable);
  119.   image_ID = param[1].data.d_image;
  120.  
  121.   if (status == GIMP_PDB_SUCCESS)
  122.     {
  123.       /*  Make sure that the drawable is indexed or RGB color  */
  124.       if (gimp_drawable_is_rgb (drawable->id))
  125.     {
  126.       gimp_progress_init ( _("Semi-Flatten..."));
  127.       gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width ()
  128.                        + 1));
  129.       semiflatten (drawable);
  130.       gimp_displays_flush ();
  131.     }
  132.       else
  133.     {
  134.       status = GIMP_PDB_EXECUTION_ERROR;
  135.     }
  136.     }
  137.  
  138.   values[0].data.d_status = status;
  139.  
  140.   gimp_drawable_detach (drawable);
  141. }
  142.  
  143.  
  144. static void
  145. semiflatten_render_row (const guchar *src_row,
  146.             guchar       *dest_row,
  147.             gint          row,
  148.             gint          row_width,
  149.             gint          bytes)
  150. {
  151.   gint col;
  152.  
  153.   for (col = 0; col < row_width ; col++)
  154.     {
  155.       dest_row[col*bytes+0] =
  156.     (src_row[col*bytes+0]*src_row[col*bytes+3])/255 +
  157.     (bgred*(255-src_row[col*bytes+3]))/255;
  158.  
  159.       dest_row[col*bytes+1] =
  160.     (src_row[col*bytes+1]*src_row[col*bytes+3])/255 +
  161.     (bggreen*(255-src_row[col*bytes+3]))/255;
  162.  
  163.       dest_row[col*bytes+2] =
  164.     (src_row[col*bytes+2]*src_row[col*bytes+3])/255 +
  165.     (bgblue*(255-src_row[col*bytes+3]))/255;
  166.  
  167.       dest_row[col*bytes+3] = (src_row[col*bytes+3] == 0) ? 0 : 255;
  168.     }
  169. }
  170.  
  171.  
  172. static void
  173. semiflatten (GimpDrawable *drawable)
  174. {
  175.   GimpPixelRgn srcPR, destPR;
  176.   gint    width, height;
  177.   gint    bytes;
  178.   guchar *src_row;
  179.   guchar *dest_row;
  180.   gint    row;
  181.   gint    x1, y1, x2, y2;
  182.  
  183.   /* Fetch the GIMP current background colour, to semi-flatten against */
  184.   gimp_palette_get_background (&bgred, &bggreen, &bgblue);
  185.  
  186.   /* Get the input area. This is the bounding box of the selection in
  187.    *  the image (or the entire image if there is no selection). Only
  188.    *  operating on the input area is simply an optimization. It doesn't
  189.    *  need to be done for correct operation. (It simply makes it go
  190.    *  faster, since fewer pixels need to be operated on).
  191.    */
  192.   gimp_drawable_mask_bounds (drawable->id, &x1, &y1, &x2, &y2);
  193.  
  194.   /* Get the size of the input image. (This will/must be the same
  195.    *  as the size of the output image.
  196.    */
  197.   width = drawable->width;
  198.   height = drawable->height;
  199.   bytes = drawable->bpp;
  200.  
  201.   /*  allocate row buffers  */
  202.   src_row  = g_new (guchar, (x2 - x1) * bytes);
  203.   dest_row = g_new (guchar, (x2 - x1) * bytes);
  204.  
  205.   /*  initialize the pixel regions  */
  206.   gimp_pixel_rgn_init (&srcPR, drawable, 0, 0, width, height, FALSE, FALSE);
  207.   gimp_pixel_rgn_init (&destPR, drawable, 0, 0, width, height, TRUE, TRUE);
  208.  
  209.   for (row = y1; row < y2; row++)
  210.     {
  211.       gimp_pixel_rgn_get_row (&srcPR, src_row, x1, row, (x2 - x1));
  212.  
  213.       semiflatten_render_row (src_row,
  214.                   dest_row,
  215.                   row,
  216.                   (x2 - x1),
  217.                   bytes);
  218.  
  219.       /*  store the dest  */
  220.       gimp_pixel_rgn_set_row (&destPR, dest_row, x1, row, (x2 - x1));
  221.  
  222.       if ((row % 10) == 0)
  223.     gimp_progress_update ((double) row / (double) (y2 - y1));
  224.     }
  225.  
  226.   /*  update the processed region  */
  227.   gimp_drawable_flush (drawable);
  228.   gimp_drawable_merge_shadow (drawable->id, TRUE);
  229.   gimp_drawable_update (drawable->id, x1, y1, (x2 - x1), (y2 - y1));
  230.  
  231.   free (src_row);
  232.   free (dest_row);
  233. }
  234.