Hi again,

I've noticed that the new procedure gimp-levels-auto I've added
does not yield exactly the same effect of the Auto button in the
levels dialog.  So, here is the revised patch to the GIMP 1.2.1
source tree.

BTW, I'd like to know if the patch is likely to be integrated
into the GIMP.  If it is not, I'd need to write an plug-in
instead of a patch to the GIMP itself, since a plug-in is much
more useful from user's point of view.  Could you please tell
me the likelihood?

Thanks,

-- 
KAJIYAMA, Tamito <[EMAIL PROTECTED]>

diff -ru gimp-1.2.1.orig/tools/pdbgen/pdb/color.pdb 
gimp-1.2.1/tools/pdbgen/pdb/color.pdb
--- gimp-1.2.1.orig/tools/pdbgen/pdb/color.pdb  Mon Dec 25 01:40:47 2000
+++ gimp-1.2.1/tools/pdbgen/pdb/color.pdb       Thu Mar 22 10:33:29 2001
@@ -175,6 +175,137 @@
     }
 }
 
+sub levels_auto {
+    $blurb = 'Modifies intensity levels in the specified drawable.';
+
+    $help = <<'HELP';
+This tool automatically adjusts intensity levels in the specified
+drawable. This tool is only valid on RGB color and grayscale
+images. It will not operate on indexed drawables.
+HELP
+
+    &std_pdb_misc;
+
+    @inargs = (
+       &drawable_arg
+    );
+
+    %invoke = (
+       vars => [ 'PixelRegion srcPR, destPR', 'int x1, y1, x2, y2',
+                 'GimpLut *lut', 'int i, j',
+                 'int low_input[5]',
+                 'int high_input[5]',
+                 'int low_output[5]',
+                 'int high_output[5]',
+                 'double gamma[5]',
+                 'int channel, channels[3], nchannels',
+                 'GimpHistogram *hist',
+                 'double count, new_count, percentage, next_percentage'],
+       code => <<'CODE'
+{
+  if (gimp_drawable_is_indexed (drawable))
+    success = FALSE;
+  else
+    {
+      for (i = 0; i < 5; i++)
+       {
+         low_input[i] = 0;
+         high_input[i] = 255;
+         low_output[i] = 0;
+         high_output[i] = 255;
+         gamma[i] = 1.0;
+       }
+
+      hist = gimp_histogram_new ();
+      gimp_histogram_calculate_drawable (hist, drawable);
+
+      if (gimp_drawable_is_rgb (drawable))
+       {
+         nchannels = 3;
+         channels[0] = RED_LUT;
+         channels[1] = GREEN_LUT;
+         channels[2] = BLUE_LUT;
+       }
+      else
+       {
+         nchannels = 1;
+         channels[0] = GRAY_LUT;
+       }
+      for (j = 0; j < nchannels; j++)
+       {
+         channel = channels[j];
+  
+         count = gimp_histogram_get_count (hist, 0, 255);
+  
+         if (count == 0.0)
+           {
+             low_input[channel] = 0;
+             high_input[channel] = 0;
+           }
+         else
+           {
+             /* set the low input */
+             new_count = 0.0;
+             for (i = 0; i < 255; i++)
+               {
+                 new_count += gimp_histogram_get_value(hist, channel, i);
+                 percentage = new_count / count;
+                 next_percentage =
+                   (new_count + gimp_histogram_get_value (hist, channel,
+                                                          i + 1)) / count;
+                 if (fabs (percentage - 0.006) <
+                     fabs (next_percentage - 0.006))
+                   {
+                     low_input[channel] = i + 1;
+                     break;
+                   }
+               }
+             /* set the high input */
+             new_count = 0.0;
+             for (i = 255; i > 0; i--)
+               {
+                 new_count += gimp_histogram_get_value(hist, channel, i);
+                 percentage = new_count / count;
+                 next_percentage =
+                   (new_count + gimp_histogram_get_value (hist, channel,
+                                                          i - 1)) / count;
+                 if (fabs (percentage - 0.006) <
+                     fabs (next_percentage - 0.006))
+                   {
+                     high_input[channel] = i - 1;
+                     break;
+                   }
+               }
+           }
+       }
+  
+      gimp_histogram_free (hist);
+  
+      /* setup the lut */
+      lut = levels_lut_new (gamma, low_input, high_input,
+                           low_output, high_output,
+                           gimp_drawable_bytes (drawable));
+  
+      /* The application should occur only within selection bounds */
+      gimp_drawable_mask_bounds (drawable, &x1, &y1, &x2, &y2);
+  
+      pixel_region_init (&srcPR, gimp_drawable_data (drawable),
+                        x1, y1, (x2 - x1), (y2 - y1), FALSE);
+      pixel_region_init (&destPR, gimp_drawable_shadow (drawable),
+                        x1, y1, (x2 - x1), (y2 - y1), TRUE);
+  
+      pixel_regions_process_parallel ((p_func) gimp_lut_process, lut, 2,
+                                     &srcPR, &destPR);
+  
+      gimp_lut_free(lut);
+      gimp_drawable_merge_shadow (drawable, TRUE);
+      drawable_update (drawable, x1, y1, (x2 - x1), (y2 - y1));
+    }
+}
+CODE
+    );
+}
+
 sub posterize {
     $blurb = 'Posterize the specified drawable.';
 
@@ -761,9 +892,9 @@
     );
 }
 
-@headers = qw("gimpimage.h" "gimpdrawable.h" "gimplut.h" "lut_funcs.h");
+@headers = qw("gimpimage.h" "gimpdrawable.h" "gimplut.h" "lut_funcs.h" 
+"libgimp/gimpmath.h");
 
-@procs = qw(brightness_contrast levels posterize desaturate equalize invert
+@procs = qw(brightness_contrast levels levels_auto posterize desaturate equalize 
+invert
            curves_spline curves_explicit color_balance histogram
            hue_saturation threshold);
 %exports = (app => [@procs], lib => [@procs]);




_______________________________________________
Gimp-developer mailing list
[EMAIL PROTECTED]
http://lists.xcf.berkeley.edu/mailman/listinfo/gimp-developer

Reply via email to