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

  1. /* Contrast Autostretch 1.06 --- image filter plug-in for The Gimp image
  2.  * manipulation program
  3.  *
  4.  * Copyright (C) 1996 Federico Mena Quintero
  5.  *
  6.  * You can contact me at quartic@polloux.fciencias.unam.mx
  7.  * You can contact the original The Gimp authors at gimp@xcf.berkeley.edu
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22.  */
  23.  
  24. /* This simple plug-in does an automatic contrast stretch.  For each
  25.    channel in the image, it finds the minimum and maximum values... it
  26.    uses those values to stretch the individual histograms to the full
  27.    contrast range.  For some images it may do just what you want; for
  28.    others it may be total crap :) */
  29.  
  30. #include "config.h"
  31.  
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34.  
  35. #include <libgimp/gimp.h>
  36.  
  37. #include "libgimp/stdplugins-intl.h"
  38.  
  39.  
  40. /* Declare local functions.
  41.  */
  42. static void      query  (void);
  43. static void      run    (gchar     *name,
  44.              gint       nparams,
  45.              GimpParam    *param,
  46.              gint      *nreturn_vals,
  47.              GimpParam   **return_vals);
  48.  
  49. static void      c_astretch (GimpDrawable * drawable);
  50. static void      indexed_c_astretch (gint32 image_ID);
  51.  
  52.  
  53. GimpPlugInInfo PLUG_IN_INFO =
  54. {
  55.   NULL,  /* init_proc  */
  56.   NULL,  /* quit_proc  */
  57.   query, /* query_proc */
  58.   run,   /* run_proc   */
  59. };
  60.  
  61.  
  62. MAIN ()
  63.  
  64. static void
  65. query (void)
  66. {
  67.   static GimpParamDef args[] =
  68.   {
  69.     { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
  70.     { GIMP_PDB_IMAGE, "image", "Input image" },
  71.     { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" }
  72.   };
  73.   static gint nargs = sizeof (args) / sizeof (args[0]);
  74.  
  75.   gimp_install_procedure ("plug_in_c_astretch",
  76.               "Automatically stretch the contrast of the "
  77.               "specified drawable to cover all possible ranges.",
  78.               "This simple plug-in does an automatic contrast "
  79.               "stretch.  For each channel in the image, it finds "
  80.               "the minimum and maximum values... it uses those "
  81.               "values to stretch the individual histograms to the "
  82.               "full contrast range.  For some images it may do "
  83.               "just what you want; for others it may be total "
  84.               "crap :)",
  85.               "Federico Mena Quintero",
  86.               "Federico Mena Quintero",
  87.               "1996",
  88.               N_("<Image>/Image/Colors/Auto/Stretch Contrast"),
  89.               "RGB*, GRAY*, INDEXED*",
  90.               GIMP_PLUGIN,
  91.               nargs, 0,
  92.               args, NULL);
  93. }
  94.  
  95. static void
  96. run (gchar   *name,
  97.      gint     nparams,
  98.      GimpParam  *param,
  99.      gint    *nreturn_vals,
  100.      GimpParam **return_vals)
  101. {
  102.   static GimpParam values[1];
  103.   GimpDrawable *drawable;
  104.   GimpRunModeType run_mode;
  105.   GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  106.  
  107.   gint32 image_ID;
  108.  
  109.   INIT_I18N();
  110.  
  111.   run_mode = param[0].data.d_int32;
  112.  
  113.   /*  Get the specified drawable  */
  114.   drawable = gimp_drawable_get (param[2].data.d_drawable);
  115.   image_ID = param[1].data.d_image;
  116.  
  117.   /*  Make sure that the drawable is gray or RGB color  */
  118.   if (gimp_drawable_is_rgb (drawable->id) ||
  119.       gimp_drawable_is_gray (drawable->id))
  120.     {
  121.       gimp_progress_init (_("Auto-Stretching Contrast..."));
  122.       gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width () + 1));
  123.       c_astretch (drawable);
  124.  
  125.       if (run_mode != GIMP_RUN_NONINTERACTIVE)
  126.     gimp_displays_flush ();
  127.     }
  128.   else if (gimp_drawable_is_indexed (drawable->id))
  129.     {
  130.       indexed_c_astretch (image_ID);
  131.  
  132.       if (run_mode != GIMP_RUN_NONINTERACTIVE)
  133.     gimp_displays_flush ();
  134.     }
  135.   else
  136.     {
  137.       /* gimp_message ("c_astretch: cannot operate on indexed color images"); */
  138.       status = GIMP_PDB_EXECUTION_ERROR;
  139.     }
  140.  
  141.   *nreturn_vals = 1;
  142.   *return_vals = values;
  143.  
  144.   values[0].type = GIMP_PDB_STATUS;
  145.   values[0].data.d_status = status;
  146.  
  147.   gimp_drawable_detach (drawable);
  148. }
  149.  
  150.  
  151. static void
  152. indexed_c_astretch(gint32 image_ID)  /* a.d.m. */
  153. {
  154.   guchar *cmap;
  155.   gint ncols,i;
  156.   gint rhi=0,ghi=0,bhi=0,rlo=255,glo=255,blo=255;
  157.  
  158.   cmap = gimp_image_get_cmap (image_ID, &ncols);
  159.  
  160.   if (cmap==NULL)
  161.     {
  162.       printf("c_astretch: cmap was NULL!  Quitting...\n");
  163.       gimp_quit();
  164.     }
  165.  
  166.   for (i=0;i<ncols;i++)
  167.     {
  168.       if (cmap[i*3 +0] > rhi) rhi=cmap[i*3 +0];
  169.       if (cmap[i*3 +1] > ghi) ghi=cmap[i*3 +1];
  170.       if (cmap[i*3 +2] > bhi) bhi=cmap[i*3 +2];
  171.       if (cmap[i*3 +0] < rlo) rlo=cmap[i*3 +0];
  172.       if (cmap[i*3 +1] < glo) glo=cmap[i*3 +1];
  173.       if (cmap[i*3 +2] < blo) blo=cmap[i*3 +2];
  174.     }
  175.  
  176.   for (i=0;i<ncols;i++)
  177.     {
  178.       if (rhi!=rlo)
  179.     cmap[i*3 +0] = (255 * (cmap[i*3 +0] - rlo)) / (rhi-rlo);
  180.       if (ghi!=glo)
  181.     cmap[i*3 +1] = (255 * (cmap[i*3 +1] - glo)) / (ghi-glo);
  182.       if (rhi!=rlo)
  183.     cmap[i*3 +2] = (255 * (cmap[i*3 +2] - blo)) / (bhi-blo);
  184.     }
  185.  
  186.   gimp_image_set_cmap (image_ID, cmap, ncols);
  187. }
  188.  
  189.  
  190. static void
  191. c_astretch (GimpDrawable *drawable)
  192. {
  193.   GimpPixelRgn src_rgn, dest_rgn;
  194.   guchar *src, *s;
  195.   guchar *dest, *d;
  196.   guchar  min[3], max[3];
  197.   guchar  range;
  198.   guchar  lut[256][3];
  199.   gint    progress, max_progress;
  200.   gint    has_alpha, alpha;
  201.   gint    x1, y1, x2, y2;
  202.   gint    x, y, b;
  203.   gpointer pr;
  204.  
  205.   /* Get selection area */
  206.   gimp_drawable_mask_bounds (drawable->id, &x1, &y1, &x2, &y2);
  207.   has_alpha = gimp_drawable_has_alpha (drawable->id);
  208.   alpha = (has_alpha) ? drawable->bpp - 1 : drawable->bpp;
  209.  
  210.   /* Initialize progress */
  211.   progress = 0;
  212.   max_progress = (x2 - x1) * (y2 - y1) * 2;
  213.  
  214.   /* Get minimum and maximum values for each channel */
  215.   min[0] = min[1] = min[2] = 255;
  216.   max[0] = max[1] = max[2] = 0;
  217.  
  218.   gimp_pixel_rgn_init (&src_rgn, drawable,
  219.                x1, y1, (x2 - x1), (y2 - y1), FALSE, FALSE);
  220.  
  221.   for (pr = gimp_pixel_rgns_register (1, &src_rgn);
  222.        pr != NULL;
  223.        pr = gimp_pixel_rgns_process (pr))
  224.     {
  225.       src = src_rgn.data;
  226.  
  227.       for (y = 0; y < src_rgn.h; y++)
  228.     {
  229.       s = src;
  230.       for (x = 0; x < src_rgn.w; x++)
  231.         {
  232.           for (b = 0; b < alpha; b++)
  233.         {
  234.           if (!has_alpha || (has_alpha && s[alpha]))
  235.             {
  236.               if (s[b] < min[b])
  237.             min[b] = s[b];
  238.               if (s[b] > max[b])
  239.             max[b] = s[b];
  240.             }
  241.         }
  242.  
  243.           s += src_rgn.bpp;
  244.         }
  245.  
  246.       src += src_rgn.rowstride;
  247.     }
  248.  
  249.       /* Update progress */
  250.       progress += src_rgn.w * src_rgn.h;
  251.  
  252.       gimp_progress_update ((double) progress / (double) max_progress);
  253.     }
  254.  
  255.   /* Calculate LUTs with stretched contrast */
  256.   for (b = 0; b < alpha; b++)
  257.     {
  258.       range = max[b] - min[b];
  259.  
  260.       if (range != 0)
  261.     for (x = min[b]; x <= max[b]; x++)
  262.       lut[x][b] = 255 * (x - min[b]) / range;
  263.       else
  264.     lut[min[b]][b] = min[b];
  265.     }
  266.  
  267.   /* Now substitute pixel vales */
  268.   gimp_pixel_rgn_init (&src_rgn, drawable,
  269.                x1, y1, (x2 - x1), (y2 - y1), FALSE, FALSE);
  270.   gimp_pixel_rgn_init (&dest_rgn, drawable,
  271.                x1, y1, (x2 - x1), (y2 - y1), TRUE, TRUE);
  272.  
  273.   for (pr = gimp_pixel_rgns_register (2, &src_rgn, &dest_rgn);
  274.        pr != NULL;
  275.        pr = gimp_pixel_rgns_process (pr))
  276.     {
  277.       src = src_rgn.data;
  278.       dest = dest_rgn.data;
  279.  
  280.       for (y = 0; y < src_rgn.h; y++)
  281.     {
  282.       s = src;
  283.       d = dest;
  284.  
  285.       for (x = 0; x < src_rgn.w; x++)
  286.         {
  287.           for (b = 0; b < alpha; b++)
  288.         d[b] = lut[s[b]][b];
  289.  
  290.           if (has_alpha)
  291.         d[alpha] = s[alpha];
  292.  
  293.           s += src_rgn.bpp;
  294.           d += dest_rgn.bpp;
  295.         }
  296.  
  297.       src += src_rgn.rowstride;
  298.       dest += dest_rgn.rowstride;
  299.  
  300.     }
  301.  
  302.       /* Update progress */
  303.       progress += src_rgn.w * src_rgn.h;
  304.  
  305.       gimp_progress_update ((double) progress / (double) max_progress);
  306.     }
  307.  
  308.   /*  update the region  */
  309.   gimp_drawable_flush (drawable);
  310.   gimp_drawable_merge_shadow (drawable->id, TRUE);
  311.   gimp_drawable_update (drawable->id, x1, y1, (x2 - x1), (y2 - y1));
  312. }
  313.