Permalink
Browse files

Add new Image#paint_transparent, #opacity_channel methods available i…

…n 6.3.7-10.
  • Loading branch information...
1 parent 303515d commit 9622e9837e7972ecb4461c4a9308b16cc5d063be rmagick committed Jan 17, 2008
Showing with 178 additions and 8 deletions.
  1. +2 −0 ext/RMagick/extconf.rb
  2. +4 −2 ext/RMagick/rmagick.h
  3. +168 −4 ext/RMagick/rmimage.c
  4. +4 −2 ext/RMagick/rmmain.c
View
@@ -173,6 +173,7 @@ def exit_failure(msg)
"IsHistogramImage", # 6.3.5
"LinearStretchImage", # 6.3.1
"MagickCoreGenesis", # 6.3.4
+ "OpaquePaintImageChannel", # 6.3.7-10
"PolaroidImage", # 6.3.1-6
"RecolorImage", # 6.3.1-3
"ResetImagePage", # 6.3.3
@@ -181,6 +182,7 @@ def exit_failure(msg)
"SetImageProperty", # 6.3.1
"SetImageRegistry", # 6.3.4-?
"SyncImageProfiles", # 6.3.2
+ "TransparentPaintImage", # 6.3.7-10
].each do |func|
have_func(func, headers)
end
View
@@ -1,4 +1,4 @@
-/* $Id: rmagick.h,v 1.220 2008/01/05 19:24:22 rmagick Exp $ */
+/* $Id: rmagick.h,v 1.221 2008/01/17 23:48:19 rmagick Exp $ */
/*=============================================================================
| Copyright (C) 2008 by Timothy P. Hunter
| Name: rmagick.h
@@ -42,7 +42,7 @@
#undef PACKAGE_STRING
-#include RUBY_EXTCONF_H
+#include "extconf.h"
// For quoting preprocessor symbols
@@ -877,8 +877,10 @@ extern VALUE Image_normalize(VALUE);
extern VALUE Image_normalize_channel(int, VALUE *, VALUE);
extern VALUE Image_oil_paint(int, VALUE *, VALUE);
extern VALUE Image_opaque(VALUE, VALUE, VALUE);
+extern VALUE Image_opaque_channel(int, VALUE *, VALUE);
extern VALUE Image_opaque_q(VALUE);
extern VALUE Image_ordered_dither(int, VALUE *, VALUE);
+extern VALUE Image_paint_transparent(int, VALUE *, VALUE);
extern VALUE Image_palette_q(VALUE);
extern VALUE Image_ping(VALUE, VALUE);
extern VALUE Image_pixel_color(int, VALUE *, VALUE);
View
@@ -1,4 +1,4 @@
-/* $Id: rmimage.c,v 1.277 2008/01/08 23:47:33 rmagick Exp $ */
+/* $Id: rmimage.c,v 1.278 2008/01/17 23:48:19 rmagick Exp $ */
/*============================================================================\
| Copyright (C) 2008 by Timothy P. Hunter
| Name: rmimage.c
@@ -6373,6 +6373,7 @@ Image_opaque(VALUE self, VALUE target, VALUE fill)
Image *image, *new_image;
MagickPixelPacket target_pp;
MagickPixelPacket fill_pp;
+ MagickBooleanType okay;
image = rm_check_destroyed(self);
new_image = rm_clone_image(image);
@@ -6381,12 +6382,99 @@ Image_opaque(VALUE self, VALUE target, VALUE fill)
Color_to_MagickPixelPacket(image, &target_pp, target);
Color_to_MagickPixelPacket(image, &fill_pp, fill);
- (void) PaintOpaqueImage(new_image, &target_pp, &fill_pp);
+#if defined(HAVE_OPAQUEPAINTIMAGECHANNEL)
+ okay = OpaquePaintImageChannel(new_image, DefaultChannels, &target_pp, &fill_pp, MagickFalse);
+#else
+ okay = PaintOpaqueImageChannel(new_image, DefaultChannels, &target_pp, &fill_pp);
+#endif
rm_check_image_exception(new_image, DestroyOnError);
+ if (!okay)
+ {
+ // Force exception
+ DestroyImage(new_image);
+ rm_ensure_result(NULL);
+ }
+
return rm_image_new(new_image);
}
+
+/*
+ Method: Image#opaque_channel
+ Purpose: Improved Image#opaque available in 6.3.7-10
+ Notes: opaque_channel(target, fill, invert=false, fuzz=img.fuzz [, channel...])
+*/
+VALUE
+Image_opaque_channel(int argc, VALUE *argv, VALUE self)
+{
+#if defined(HAVE_OPAQUEPAINTIMAGECHANNEL)
+ Image *image, *new_image;
+ MagickPixelPacket target_pp, fill_pp;
+ ChannelType channels;
+ double keep, fuzz;
+ MagickBooleanType okay, invert = MagickFalse;
+
+ image = rm_check_destroyed(self);
+ channels = extract_channels(&argc, argv);
+ if (argc > 4)
+ {
+ raise_ChannelType_error(argv[argc-1]);
+ }
+
+ // Default fuzz value is image's fuzz attribute.
+ fuzz = image->fuzz;
+
+ switch (argc)
+ {
+ case 4:
+ fuzz = NUM2DBL(argv[3]);
+ if (fuzz < 0.0)
+ {
+ rb_raise(rb_eArgError, "fuzz must be >= 0.0", fuzz);
+ }
+ case 3:
+ invert = RTEST(argv[2]);
+ case 2:
+ // Allow color name or Pixel
+ Color_to_MagickPixelPacket(image, &fill_pp, argv[1]);
+ Color_to_MagickPixelPacket(image, &target_pp, argv[0]);
+ break;
+ default:
+ rb_raise(rb_eArgError, "wrong number of arguments (got %d, expected 2 or more)", argc);
+ break;
+ }
+
+ new_image = rm_clone_image(image);
+ keep = new_image->fuzz;
+ new_image->fuzz = fuzz;
+
+ okay = OpaquePaintImageChannel(new_image, channels, &target_pp, &fill_pp, invert);
+
+ // Restore saved fuzz value
+ new_image->fuzz = keep;
+ rm_check_image_exception(new_image, DestroyOnError);
+
+ if (!okay)
+ {
+ // Force exception
+ DestroyImage(new_image);
+ rm_ensure_result(NULL);
+ }
+
+ return rm_image_new(new_image);
+
+#else
+ argc = argc; // defeat "unused parameter" messages
+ argv = argv;
+ self = self;
+ rm_not_implemented();
+ return (VALUE)0;
+#endif
+}
+
+
+
/*
Method: Image#opaque?
Purpose: return true if any of the pixels in the image have an opacity
@@ -6509,6 +6597,71 @@ Image_page_eq(VALUE self, VALUE rect)
return self;
}
+
+/*
+ Method: Image#paint_transparent(target, opacity=TransparentOpacity, invert=false, fuzz=img.fuzz)
+ Purpose: Improved version of Image#transparent available in 6.3.7-10
+*/
+VALUE
+Image_paint_transparent(int argc, VALUE *argv, VALUE self)
+{
+#if defined(HAVE_TRANSPARENTPAINTIMAGE)
+ Image *image, *new_image;
+ MagickPixelPacket color;
+ Quantum opacity = TransparentOpacity;
+ double keep, fuzz;
+ MagickBooleanType okay, invert = MagickFalse;
+
+ image = rm_check_destroyed(self);
+
+ // Default fuzz value is image's fuzz attribute.
+ fuzz = image->fuzz;
+
+ switch (argc)
+ {
+ case 4:
+ fuzz = NUM2DBL(argv[3]);
+ case 3:
+ invert = RTEST(argv[2]);
+ case 2:
+ opacity = APP2QUANTUM(argv[1]);
+ case 1:
+ Color_to_MagickPixelPacket(image, &color, argv[0]);
+ break;
+ default:
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 to 4)", argc);
+ break;
+ }
+
+ new_image = rm_clone_image(image);
+
+ // Use fuzz value from caller
+ keep = new_image->fuzz;
+ new_image->fuzz = fuzz;
+
+ okay = TransparentPaintImage(new_image, &color, opacity, invert);
+ new_image->fuzz = keep;
+
+ // Is it possible for TransparentPaintImage to silently fail?
+ rm_check_image_exception(new_image, DestroyOnError);
+ if (!okay)
+ {
+ // Force exception
+ DestroyImage(new_image);
+ rm_ensure_result(NULL);
+ }
+
+ return rm_image_new(new_image);
+#else
+ argc = argc; // defeat "unused parameter" messages
+ argv = argv;
+ self = self;
+ rm_not_implemented();
+ return (VALUE)0;
+#endif
+}
+
+
/*
Method: Image#palette?
Purpose: return true if the image is PseudoClass and has 256 unique
@@ -9095,6 +9248,7 @@ Image_transparent(int argc, VALUE *argv, VALUE self)
Image *image, *new_image;
MagickPixelPacket color;
Quantum opacity = TransparentOpacity;
+ MagickBooleanType okay;
image = rm_check_destroyed(self);
@@ -9112,8 +9266,18 @@ Image_transparent(int argc, VALUE *argv, VALUE self)
new_image = rm_clone_image(image);
- (void) PaintTransparentImage(new_image, &color, opacity);
+#if defined(HAVE_TRANSPARENTPAINTIMAGE)
+ okay = TransparentPaintImage(new_image, &color, opacity, MagickFalse);
+#else
+ okay = PaintTransparentImage(new_image, &color, opacity);
+#endif
rm_check_image_exception(new_image, DestroyOnError);
+ if (!okay)
+ {
+ // Force exception
+ DestroyImage(new_image);
+ rm_ensure_result(NULL);
+ }
return rm_image_new(new_image);
}
@@ -10186,7 +10350,7 @@ ChannelType extract_channels(
void
raise_ChannelType_error(VALUE arg)
{
- rb_raise(rb_eTypeError, "argument needs to be a ChannelType (%s given)"
+ rb_raise(rb_eTypeError, "argument must be a ChannelType value (%s given)"
, rb_class2name(CLASS_OF(arg)));
}
View
@@ -1,4 +1,4 @@
-/* $Id: rmmain.c,v 1.233 2008/01/02 23:06:29 rmagick Exp $ */
+/* $Id: rmmain.c,v 1.234 2008/01/17 23:48:19 rmagick Exp $ */
/*============================================================================\
| Copyright (C) 2008 by Timothy P. Hunter
| Name: rmmain.c
@@ -581,8 +581,10 @@ Init_RMagick2(void)
rb_define_method(Class_Image, "normalize_channel", Image_normalize_channel, -1);
rb_define_method(Class_Image, "oil_paint", Image_oil_paint, -1);
rb_define_method(Class_Image, "opaque", Image_opaque, 2);
+ rb_define_method(Class_Image, "opaque_channel", Image_opaque_channel, -1);
rb_define_method(Class_Image, "opaque?", Image_opaque_q, 0);
rb_define_method(Class_Image, "ordered_dither", Image_ordered_dither, -1);
+ rb_define_method(Class_Image, "paint_transparent", Image_paint_transparent, -1);
rb_define_method(Class_Image, "palette?", Image_palette_q, 0);
rb_define_method(Class_Image, "pixel_color", Image_pixel_color, -1);
rb_define_method(Class_Image, "polaroid", Image_polaroid, -1);
@@ -1684,7 +1686,7 @@ static void version_constants(void)
rb_define_const(Module_Magick, "Version", str);
sprintf(long_version,
- "This is %s ($Date: 2008/01/02 23:06:29 $) Copyright (C) 2008 by Timothy P. Hunter\n"
+ "This is %s ($Date: 2008/01/17 23:48:19 $) Copyright (C) 2008 by Timothy P. Hunter\n"
"Built with %s\n"
"Built for %s\n"
"Web page: http://rmagick.rubyforge.org\n"

0 comments on commit 9622e98

Please sign in to comment.