How to do matrix-transform by magickCore

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
smileface

How to do matrix-transform by magickCore

Post by smileface »

it works well(do matrix-transform and output a png file) if u using ImageMagick6.4.6-7 or higer Version
Using interface magickwand

Code: Select all

//read a image src.jpg to handler magick_wand
...
MagickSetImageMatte(magick_wand,MagickTrue);
MagickSetImageVirtualPixelMethod(magick_wand,TransparentVirtualPixelMethod);
...
MagickDistortImage(magick_wand,AffineProjectionDistortion,6,doubleArray,MagickFalse);
//want to get a transparent background PNG file
MagickWriteImage(magick_wand,"out.png");
but I using similar method in magickCore,failed.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <magick/MagickCore.h>

int
main(int argc,char **argv)
{
   ExceptionInfo *exception;
   Image *image,*after;
   ImageInfo  *image_info;
   
   if(argc != 3)
   {
      (void)fprintf(stdout,"usage invalid!");
      exit(0);
   }
   MagickCoreGenesis(*argv,MagickTrue);
   exception=AcquireExceptionInfo();
   image_info=CloneImageInfo((ImageInfo*)NULL);
   (void)strcpy(image_info->filename,*(argv+1));
   image=ReadImage(image_info,exception);
   double  arr[6]={0.739,0,0,0.739,63,-79};
   //SetMatte(MagickTrue);   
   SetImageVirtualPixelMethod(image,8);
   after=DistortImage(image,AffineProjectionDistortion,6,arr,MagickFalse,exception);
   (void)strcpy(after->filename,argv[2]);
   WriteImage(image_info,after);
}
first i use setMatte(true),and then SetImageVirtualPixelMethod,
and last using DistortImage,but can't get the same result as using magickWand.
i found in magickCore there is a API function :

Code: Select all

MagickBooleanType DrawAffineImage(Image *image,const Image *source,
    const AffineMatrix *affine)
Do i need use this function to do matrix-transform? I am so worried about this question.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: How to do matrix-transform by magickCore

Post by magick »

MagickWand is a wrapper around MagickCore. To replicate the MagickWand method calls, inspect the wand/magick-image.c source module. For example, MagickSetImageMatte() makes this MagickCore API call:
  • if (image->matte == MagickFalse)
    (void) SetImageOpacity(image,OpaqueOpacity);
smileface

Re: How to do matrix-transform by magickCore

Post by smileface »

hi,
magick,just like u said i know the magickwand call magickCore API,and i read the source of
function 'MagickSetImageMatte',
function 'MagickSetImageVirtualPixelMethod'
function 'MagickDistortImage'.
And then i learned that what they call infact:
In MagickSetImageMatte call SetImageOpacity.
In MagickSetImageVirtualPixelMethod call SetImageVirtualPixelMethod.
In MagickDistortImage call DistortImage.
so,i modify my test program like this:

Code: Select all

  #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <magick/MagickCore.h>
int
main(int argc,char **argv)
{
   ExceptionInfo *exception;
   Image *image,*after;
   ImageInfo  *image_info;
   
   if(argc != 3)
   {
      (void)fprintf(stdout,"usage invalid!");
      exit(0);
   }
   MagickCoreGenesis(*argv,MagickTrue);
   exception=AcquireExceptionInfo();
   image_info=CloneImageInfo((ImageInfo*)NULL);
   (void)strcpy(image_info->filename,*(argv+1));
   image=ReadImage(image_info,exception);
   double  arr[6]={1.414,1.414,-1.414,1.414,0,0};
   if(image->matte == MagickFalse)
   (void)SetImageOpacity(image,OpaqueOpacity);   
   SetImageVirtualPixelMethod(image,TransparentVirtualPixelMethod);
   after=DistortImage(image,AffineProjectionDistortion,6,arr,MagickFalse,exception);
   (void)strcpy(after->filename,argv[2]);
   WriteImage(image_info,after);
}
  
But it works bad again,how to explain,and Is there sth i missed or make some wrong?please show me what is wrong?Are they equal?
I tried JPG,GIF files ,both of them failed. After call those functions ,they have black background and can'nt autofit(bestfit) width and height when needed.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to do matrix-transform by magickCore

Post by anthony »

Dont save to JPG - it don't save transpareny.

Dont save to GIF it only uses boolean transparency, and many GIF display programs
even ignore that.

Save to PNG which handles full transparency and semi-transparency.


An alturnative technique (one not usally recommended) for setting the Matte channel of an image is to set the image 'type" to "TrueColorMatte" before reading the image.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply