Hello Maintainers,

>From last few days, we were trying to perform the chroma operation using Image 
>Magick (IM) Package. For this operation, we need to pass two pixels one high 
>and another low (RGB value of high pixel will be always greater than or equal 
>to low pixel) and the concerned function should make all the pixel transparent 
>that lie between the given range. As fuzz factor is defined for all three 
>channels (which makes us difficult to pass different ranges for different 
>channels ), we were not able to find any function (including transparent API) 
>that can help us out. 

Thus we wrote our own functions (on the lines of transparent API). I am 
attaching the patch containing our function (for version 6.4.3-6), and wants to 
request to the maintainers that if they found this function useful/suitable 
please include it in the later releases of IM.

Thanks and Regards,
Manish Aggarwal
[EMAIL PROTECTED]

diff -aur ImageMagick-6.4.3/magick/paint.c ImageMagick-6.4.3_mod/magick/paint.c
--- ImageMagick-6.4.3/magick/paint.c    2008-08-22 21:45:13.000000000 +0530
+++ ImageMagick-6.4.3_mod/magick/paint.c        2008-11-14 17:24:40.000000000 
+0530
@@ -823,3 +823,132 @@
   image_view=DestroyCacheViewThreadSet(image_view);
   return(status);
 }
+
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%     T r a n s p a r e n t P a i n t I m a g e C h r o m a                   %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  TransparentPaintImageChroma() changes the opacity value associated with any
+%  pixel that matches color to the value defined by opacity.
+%
+%  As there is one fuzz value for the all the channels, the 
TransparentPaintImage()
+%  API is not suitable for the operations like chroma, where the tolerance for 
+%  similarity of two color component (RGB) can be different, Thus we defined 
this
+%  functon which takes two target pixel (one high and one low) and all the 
+%  pixels of an image which are lying between these two pixels are made 
transparent.
+%
+%  The format of the TransparentPaintImage method is:
+%
+%      MagickBooleanType TransparentPaintImage(Image *image,
+%        const MagickPixelPacket *target,const Quantum opacity,
+%        const MagickBooleanType invert)
+%
+%  A description of each parameter follows:
+%
+%    o image: the image.
+%
+%    o targetHigh: the high target color.
+%
+%    o targetLow: the low target color.
+%
+%    o opacity: the replacement opacity value.
+%
+%    o invert: paint any pixel that does not match the target color.
+%
+*/
+MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
+  const MagickPixelPacket *targetLow, const MagickPixelPacket *targetHigh, 
const Quantum opacity,
+  const MagickBooleanType invert)
+{
+#define TransparentPaintImageTag  "Transparent/Image"
+
+  long
+    progress,
+    y;
+
+  MagickBooleanType
+    status;
+
+  ViewInfo
+    **image_view;
+
+  assert(image != (Image *) NULL);
+  assert(image->signature == MagickSignature);
+  assert(targetHigh != (MagickPixelPacket *) NULL);
+  assert(targetLow != (MagickPixelPacket *) NULL);
+  if (image->debug != MagickFalse)
+    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
+  if (SetImageStorageClass(image,DirectClass) == MagickFalse)
+    return(MagickFalse);
+  if (image->matte == MagickFalse)
+    (void) SetImageAlphaChannel(image,ResetAlphaChannel);
+  /*
+    Make image color transparent.
+  */
+  status=MagickTrue;
+  progress=0;
+  image_view=AcquireCacheViewThreadSet(image);
+  #pragma omp parallel for
+  for (y=0; y < (long) image->rows; y++)
+  {
+    MagickPixelPacket
+      pixel;
+
+    register IndexPacket
+      *indexes;
+
+    register long
+      id,
+      x;
+
+    register PixelPacket
+      *q;
+
+    if (status == MagickFalse)
+      continue;
+    id=GetCacheViewThreadId();
+    q=GetCacheViewPixels(image_view[id],0,y,image->columns,1);
+    if (q == (PixelPacket *) NULL)
+      {
+        status=MagickFalse;
+        continue;
+      }
+    indexes=GetCacheViewIndexes(image_view[id]);
+    GetMagickPixelPacket(image,&pixel);
+    for (x=0; x < (long) image->columns; x++)
+    {
+      SetMagickPixelPacket(image,q,indexes+x,&pixel);
+      //if (IsMagickColorSimilar(&pixel,target) != invert)
+      if( ( pixel.red   >= targetLow->red   && pixel.red   <= targetHigh->red  
 &&
+            pixel.green >= targetLow->green && pixel.green <= 
targetHigh->green &&
+            pixel.blue  >= targetLow->blue  && pixel.blue  <= targetHigh->blue 
) != invert  )
+        q->opacity=opacity;
+        q++;
+    }
+    if (SyncCacheView(image_view[id]) == MagickFalse)
+      status=MagickFalse;
+    if (image->progress_monitor != (MagickProgressMonitor) NULL)
+      {
+        MagickBooleanType
+          proceed;
+
+        #pragma omp critical
+        proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
+          image->rows);
+        if (proceed == MagickFalse)
+          status=MagickFalse;
+      }
+  }
+  image_view=DestroyCacheViewThreadSet(image_view);
+  return(status);
+}
+
+
+
diff -aur ImageMagick-6.4.3/magick/paint.h ImageMagick-6.4.3_mod/magick/paint.h
--- ImageMagick-6.4.3/magick/paint.h    2008-01-08 08:40:03.000000000 +0530
+++ ImageMagick-6.4.3_mod/magick/paint.h        2008-11-14 17:47:34.000000000 
+0530
@@ -38,6 +38,10 @@
   TransparentPaintImage(Image *,const MagickPixelPacket *,
     const Quantum,const MagickBooleanType);
 
+extern MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
+  const MagickPixelPacket *targetLow, const MagickPixelPacket *targetHigh, 
const Quantum opacity,
+  const MagickBooleanType invert);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif
diff -aur ImageMagick-6.4.3/Magick++/lib/Image.cpp 
ImageMagick-6.4.3_mod/Magick++/lib/Image.cpp
--- ImageMagick-6.4.3/Magick++/lib/Image.cpp    2008-07-28 19:06:47.000000000 
+0530
+++ ImageMagick-6.4.3_mod/Magick++/lib/Image.cpp        2008-11-14 
17:34:18.000000000 +0530
@@ -1861,6 +1861,28 @@
   throwImageException();
 }
 
+// Add matte image to image, setting pixels matching color to transparent
+void Magick::Image::transparentChroma ( const Color &colorLow_, const Color 
&colorHigh_)
+{
+  if ( !colorLow_.isValid() || !colorHigh_.isValid() )
+  {
+    throwExceptionExplicit( OptionError,
+                           "Color argument is invalid" );
+  }
+
+  std::string colorLow = colorLow_;
+  std::string colorHigh = colorHigh_;
+
+  MagickPixelPacket targetLow;
+  MagickPixelPacket targetHigh;
+  (void) 
QueryMagickColor(std::string(colorLow_).c_str(),&targetLow,&image()->exception);
+  (void) 
QueryMagickColor(std::string(colorHigh_).c_str(),&targetHigh,&image()->exception);
+  modifyImage();
+  TransparentPaintImageChroma ( image(), &targetLow, &targetHigh, 
TransparentOpacity, MagickFalse );
+  throwImageException();
+}
+
+
 // Trim edges that are the background color from the image
 void Magick::Image::trim ( void )
 {
diff -aur ImageMagick-6.4.3/Magick++/lib/Magick++/Image.h 
ImageMagick-6.4.3_mod/Magick++/lib/Magick++/Image.h
--- ImageMagick-6.4.3/Magick++/lib/Magick++/Image.h     2008-07-28 
19:06:47.000000000 +0530
+++ ImageMagick-6.4.3_mod/Magick++/lib/Magick++/Image.h 2008-11-14 
16:54:43.000000000 +0530
@@ -603,6 +603,10 @@
     // transparent
     void            transparent ( const Color &color_ );
     
+    // Add matte image to image, for all the pixels that lies in between
+    // the given two color
+    void transparentChroma ( const Color &colorLow_, const Color &colorHigh_);
+
     // Trim edges that are the background color from the image
     void            trim ( void );
 
_______________________________________________
Magick-users mailing list
[email protected]
http://studio.imagemagick.org/mailman/listinfo/magick-users

Reply via email to