home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / app / desaturate.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  2.8 KB  |  111 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. #include "config.h"
  20.  
  21. #include <glib.h>
  22.  
  23. #include "apptypes.h"
  24.  
  25. #include "appenv.h"
  26. #include "drawable.h"
  27. #include "desaturate.h"
  28. #include "paint_funcs.h"
  29. #include "gimage.h"
  30.  
  31. #include "config.h"
  32. #include "libgimp/gimpintl.h"
  33.  
  34. void
  35. image_desaturate (GimpImage *gimage)
  36. {
  37.   GimpDrawable *drawable;
  38.  
  39.   drawable = gimage_active_drawable (gimage);
  40.  
  41.   if (! drawable_color (drawable))
  42.     {
  43.       g_message (_("Desaturate operates only on RGB color drawables."));
  44.       return;
  45.     }
  46.   desaturate (drawable);
  47. }
  48.  
  49.  
  50. /*  Desaturater  */
  51.  
  52. void
  53. desaturate (GimpDrawable *drawable)
  54. {
  55.   PixelRegion srcPR, destPR;
  56.   unsigned char *src, *s;
  57.   unsigned char *dest, *d;
  58.   int h, j;
  59.   int lightness, min, max;
  60.   int has_alpha;
  61.   void *pr;
  62.   int x1, y1, x2, y2;
  63.  
  64.   if (!drawable) 
  65.     return;
  66.  
  67.   has_alpha = drawable_has_alpha (drawable);
  68.   drawable_mask_bounds (drawable, &x1, &y1, &x2, &y2);
  69.   pixel_region_init (&srcPR, drawable_data (drawable), x1, y1, (x2 - x1), (y2 - y1), FALSE);
  70.   pixel_region_init (&destPR, drawable_shadow (drawable), x1, y1, (x2 - x1), (y2 - y1), TRUE);
  71.  
  72.   for (pr = pixel_regions_register (2, &srcPR, &destPR); pr != NULL; pr = pixel_regions_process (pr))
  73.     {
  74.       src = srcPR.data;
  75.       dest = destPR.data;
  76.       h = srcPR.h;
  77.  
  78.       while (h--)
  79.     {
  80.       s = src;
  81.       d = dest;
  82.  
  83.       for (j = 0; j < srcPR.w; j++)
  84.         {
  85.           max = MAX (s[RED_PIX], s[GREEN_PIX]);
  86.           max = MAX (max, s[BLUE_PIX]);
  87.           min = MIN (s[RED_PIX], s[GREEN_PIX]);
  88.           min = MIN (min, s[BLUE_PIX]);
  89.  
  90.           lightness = (max + min) / 2;
  91.  
  92.           d[RED_PIX] = lightness;
  93.           d[GREEN_PIX] = lightness;
  94.           d[BLUE_PIX] = lightness;
  95.  
  96.           if (has_alpha)
  97.         d[ALPHA_PIX] = s[ALPHA_PIX];
  98.  
  99.           d += destPR.bytes;
  100.           s += srcPR.bytes;
  101.         }
  102.  
  103.       src += srcPR.rowstride;
  104.       dest += destPR.rowstride;
  105.     }
  106.     }
  107.  
  108.   drawable_merge_shadow (drawable, TRUE);
  109.   drawable_update (drawable, x1, y1, (x2 - x1), (y2 - y1));
  110. }
  111.