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

  1. /*
  2.  * Value-Invert plug-in v1.1 by Adam D. Moss, adam@foxbox.org.  1999/02/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.  * BUGS:
  25.  *     Is not undoable when operating on indexed images - GIMP's fault.
  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      vinvert            (GimpDrawable    *drawable);
  48. static void      indexed_vinvert    (gint32        image_ID);
  49. static void      vinvert_render_row (const guchar *src_row,
  50.                      guchar       *dest_row,
  51.                      gint          row_width,
  52.                      const gint    bytes);
  53.  
  54.  
  55. static GimpRunModeType run_mode;
  56.  
  57. GimpPlugInInfo PLUG_IN_INFO =
  58. {
  59.   NULL,  /* init_proc  */
  60.   NULL,  /* quit_proc  */
  61.   query, /* query_proc */
  62.   run,   /* run_proc   */
  63. };
  64.  
  65.  
  66. MAIN ()
  67.  
  68. static void
  69. query ()
  70. {
  71.   static GimpParamDef args[] =
  72.   {
  73.     { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
  74.     { GIMP_PDB_IMAGE, "image", "Input image (used for indexed images)" },
  75.     { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" }
  76.   };
  77.   static gint nargs = sizeof (args) / sizeof (args[0]);
  78.  
  79.   gimp_install_procedure ("plug_in_vinvert",
  80.               "Invert the 'value' component of an indexed/RGB image in HSV colorspace",
  81.               "This function takes an indexed/RGB image and "
  82.               "inverts its 'value' in HSV space.  The upshot of "
  83.               "this is that the color and saturation at any given "
  84.               "point remains the same, but its brightness is "
  85.               "effectively inverted.  Quite strange.  Sometimes "
  86.               "produces unpleasant color artifacts on images from "
  87.               "lossy sources (ie. JPEG).",
  88.               "Adam D. Moss (adam@foxbox.org)",
  89.               "Adam D. Moss (adam@foxbox.org)",
  90.               "27th March 1997",
  91.               N_("<Image>/Filters/Colors/Value Invert"),
  92.               "RGB*, INDEXED*",
  93.               GIMP_PLUGIN,
  94.               nargs, 0,
  95.               args, NULL);
  96. }
  97.  
  98. static void
  99. run (char    *name,
  100.      int      nparams,
  101.      GimpParam  *param,
  102.      int     *nreturn_vals,
  103.      GimpParam **return_vals)
  104. {
  105.   static GimpParam values[1];
  106.   GimpDrawable *drawable;
  107.   gint32 image_ID;
  108.   GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  109.  
  110.   run_mode = param[0].data.d_int32;
  111.  
  112.   *nreturn_vals = 1;
  113.   *return_vals = values;
  114.  
  115.   values[0].type = GIMP_PDB_STATUS;
  116.   values[0].data.d_status = status;
  117.  
  118.  
  119.   /*  Get the specified drawable  */
  120.   drawable = gimp_drawable_get (param[2].data.d_drawable);
  121.   image_ID = param[1].data.d_image;
  122.  
  123.   if (status == GIMP_PDB_SUCCESS)
  124.     {
  125.       /*  Make sure that the drawable is indexed or RGB color  */
  126.       if (gimp_drawable_is_rgb (drawable->id))
  127.     {
  128.       if (run_mode != GIMP_RUN_NONINTERACTIVE)
  129.         {
  130.           INIT_I18N();
  131.           gimp_progress_init ("Value Invert...");
  132.         }
  133.  
  134.       vinvert (drawable);
  135.           if (run_mode != GIMP_RUN_NONINTERACTIVE)
  136.         gimp_displays_flush ();
  137.     }
  138.       else
  139.     if (gimp_drawable_is_indexed (drawable->id))
  140.       {
  141.         indexed_vinvert (image_ID);
  142.             if (run_mode != GIMP_RUN_NONINTERACTIVE)
  143.           gimp_displays_flush ();
  144.       }
  145.     else
  146.       {
  147.         status = GIMP_PDB_EXECUTION_ERROR;
  148.       }
  149.     }
  150.  
  151.   values[0].data.d_status = status;
  152.  
  153.   gimp_drawable_detach (drawable);
  154. }
  155.  
  156.  
  157. static void
  158. indexed_vinvert (gint32 image_ID)
  159. {
  160.   guchar *cmap;
  161.   gint    ncols;
  162.  
  163.   cmap = gimp_image_get_cmap (image_ID, &ncols);
  164.  
  165.   if (cmap==NULL)
  166.     {
  167.       g_print ("vinvert: cmap was NULL!  Quitting...\n");
  168.       gimp_quit ();
  169.     }
  170.  
  171.   vinvert_render_row (cmap,
  172.               cmap,
  173.               ncols,
  174.               3);
  175.  
  176.   gimp_image_set_cmap (image_ID, cmap, ncols);
  177. }
  178.  
  179. static void
  180. vinvert_render_row (const guchar *src_data,
  181.             guchar       *dest_data,
  182.             gint          col,       /* row width in pixels */
  183.             const gint    bytes)
  184. {
  185.   while (col--)
  186.     {
  187.       gint v1, v2, v3;
  188.  
  189.       v1 = src_data[col*bytes   ];
  190.       v2 = src_data[col*bytes +1];
  191.       v3 = src_data[col*bytes +2];
  192.  
  193.       gimp_rgb_to_hsv (&v1, &v2, &v3);
  194.       v3 = 255-v3;
  195.       gimp_hsv_to_rgb (&v1, &v2, &v3);
  196.  
  197.       dest_data[col*bytes   ] = v1;
  198.       dest_data[col*bytes +1] = v2;
  199.       dest_data[col*bytes +2] = v3;
  200.  
  201.       if (bytes>3)
  202.     {
  203.       gint bytenum;
  204.  
  205.       for (bytenum = 3; bytenum<bytes; bytenum++)
  206.         {
  207.           dest_data[col*bytes+bytenum] =
  208.         src_data[col*bytes+bytenum];
  209.         }
  210.     }
  211.     }
  212. }
  213.  
  214.  
  215.  
  216. static void
  217. vinvert_render_region (const GimpPixelRgn srcPR,
  218.                const GimpPixelRgn destPR)
  219. {
  220.   gint row;
  221.   guchar* src_ptr  = srcPR.data;
  222.   guchar* dest_ptr = destPR.data;
  223.   
  224.   for (row = 0; row < srcPR.h ; row++)
  225.     {
  226.       vinvert_render_row (src_ptr, dest_ptr,
  227.               srcPR.w,
  228.               srcPR.bpp);
  229.  
  230.       src_ptr  += srcPR.rowstride;
  231.       dest_ptr += destPR.rowstride;
  232.     }
  233. }
  234.  
  235. static void
  236. vinvert (GimpDrawable *drawable)
  237. {
  238.   GimpPixelRgn srcPR, destPR;
  239.   gint      x1, y1, x2, y2;
  240.   gpointer  pr;
  241.   gint      total_area, area_so_far;
  242.   gint      progress_skip;
  243.  
  244.  
  245.   /* Get the input area. This is the bounding box of the selection in
  246.    *  the image (or the entire image if there is no selection). Only
  247.    *  operating on the input area is simply an optimization. It doesn't
  248.    *  need to be done for correct operation. (It simply makes it go
  249.    *  faster, since fewer pixels need to be operated on).
  250.    */
  251.   gimp_drawable_mask_bounds (drawable->id, &x1, &y1, &x2, &y2);
  252.  
  253.   total_area = (x2 - x1) * (y2 - y1);
  254.   area_so_far = 0;
  255.   progress_skip = 0;
  256.  
  257.   /* Initialize the pixel regions. */
  258.   gimp_pixel_rgn_init (&srcPR, drawable, x1, y1, (x2 - x1), (y2 - y1),
  259.                FALSE, FALSE);
  260.   gimp_pixel_rgn_init (&destPR, drawable, x1, y1, (x2 - x1), (y2 - y1),
  261.                TRUE, TRUE);
  262.   
  263.   for (pr = gimp_pixel_rgns_register (2, &srcPR, &destPR);
  264.        pr != NULL;
  265.        pr = gimp_pixel_rgns_process (pr))
  266.     {
  267.       vinvert_render_region (srcPR, destPR);
  268.  
  269.       if ((run_mode != GIMP_RUN_NONINTERACTIVE))
  270.     {
  271.       area_so_far += srcPR.w * srcPR.h;
  272.       if (((progress_skip++)%10) == 0)
  273.         gimp_progress_update ((double) area_so_far / (double) total_area);
  274.     }
  275.     }
  276.  
  277.   /*  update the processed region  */
  278.   gimp_drawable_flush (drawable);
  279.   gimp_drawable_merge_shadow (drawable->id, TRUE);
  280.   gimp_drawable_update (drawable->id, x1, y1, (x2 - x1), (y2 - y1));
  281. }
  282.