cbashimagemagickimagemagick-convertmagickwand

Custom emboss using MagickWand C API


I want to achieve this Emboss effect using ImageMagick's C API. The script is from this page. I have achieved emboss for method 1 option but I'm looking to convert for method 2. Please look at the options in bash script.

When I debug above script for method 2 I've got this.

 convert -quiet binaryimage.gif +repage ./emboss_1_18210.mpc
 convert '(' ./emboss_1_18210.mpc -colorspace gray ')' '(' -clone 0 -negate ')' '(' -clone 0 -distort SRT '0,0 1 0 -2.12132,-2.12132' ')' '(' -clone 1 -distort SRT '0,0 1 0 2.12132,2.12132' ')' -delete 0,1 -define compose:args=50 -compose blend -composite -level 100% ./emboss_2_18210.mpc
 convert ./emboss_2_18210.mpc binaryimageCm2.gif

Below id my C program, it's not done yet.

#include <stdio.h>
#include <MagickWand/MagickWand.h>
#include <math.h>

#define PI 3.14159265

double *arguments(double r[],int i){
  double azimuth = 135;
  int depth = 6;

  r[0] = 0;
  r[1] = 0;
  r[2] = 1;
  r[3] = 0;
  if(i==1){
    r[4] = cos(0.78539816339) * depth/2;
    r[5] = sin(0.78539816339) * depth/2;
  }else{
    r[4] = -cos(0.78539816339) * depth/2;
    r[5] = -sin(0.78539816339) * depth/2;
  }
  return(r);
}
int emboss(){

  double r[6];
  MagickWand *wand = NULL;
  wand = NewMagickWand();

  MagickReadImage(wand,"binaryimage.gif");

  int test = MagickSetImageColorspace(wand, 3);
  printf("%d\n", test);
  MagickDistortImage(wand, ScaleRotateTranslateDistortion ,6, arguments(r,0) ,1);
  MagickDistortImage(wand, ScaleRotateTranslateDistortion ,6, arguments(r,1) ,1);
  MagickNegateImage(wand,1);
  MagickCompositeImage(wand, wand, BlendCompositeOp, 1, 0, 0);
  MagickWriteImages(wand,"binaryimage_c_emboss.gif",MagickTrue);

  wand=DestroyMagickWand(wand);
  MagickWandTerminus();

  return 1;
}
int main(int argc, char **argv) {
    emboss();
}

Solution

  • This is a bit tricky, because you need to transform the colorspace as well as define a compose-argument property. Also note that bestfit should be set to false on all distortions.

    Here's the CLI & C equivalent.

    CLI

    convert \( wizard:  -colorspace gray \) \
            \( -clone 0 -negate \) \
            \( -clone 0 -distort SRT '0,0 1 0 -2.12132,-2.12132' \) \
            \( -clone 1 -distort SRT '0,0 1 0  2.12132, 2.12132' \) \
            -delete 0,1 \
            -define compose:args=50 \
            -compose blend -composite \
            -level 100% output.png
    

    C

    #include <wand/MagickWand.h> // or <MagickWand/MagickWand.h> if using IM 7
    
    #define DISTORT_ARG_COUNT 6
    
    double distort_ags[DISTORT_ARG_COUNT] = {
        0.0f,
        0.0f,
        1.0f,
        0.0f,
        -2.12132,
        -2.12132,
    };
    
    int main(int argc, const char * argv[]) {
        MagickWandGenesis();
    
        MagickWand
            * wand0,
            * wand1,
            * wand2,
            * wand3;
        wand0 = NewMagickWand();
        // '(' wizard: -colorspace gray ')'
        MagickReadImage(wand0, "wizard:");
        MagickTransformImageColorspace(wand0, GRAYColorspace);
        // '(' -clone 0 -negate ')'
        wand1 = CloneMagickWand(wand0);
        MagickNegateImage(wand1, MagickFalse);
        // '(' -clone 0 -distort SRT '0,0 1 0 -2.12132,-2.12132' ')'
        wand2 = CloneMagickWand(wand0);
        MagickDistortImage(wand2,
                           ScaleRotateTranslateDistortion,
                           DISTORT_ARG_COUNT,
                           distort_ags,
                           MagickFalse);
        // '(' -clone 1 -distort SRT '0,0 1 0 2.12132,2.12132' ')'
        wand3 = CloneMagickWand(wand1);
        distort_ags[4] *= -1;
        distort_ags[5] *= -1;
        MagickDistortImage(wand3,
                           ScaleRotateTranslateDistortion,
                           DISTORT_ARG_COUNT,
                           distort_ags,
                           MagickFalse);
        // -delete 0,1
        wand0 = DestroyMagickWand(wand0);
        wand1 = DestroyMagickWand(wand1);
        // -define compose:args=50
        MagickSetImageArtifact(wand3, "compose:args", "50"); // Might also be set on wand2
        // -compose blend -composite
        // If using ImageMagick 7, define `clip_to_self'.
        // MagickCompositeImage(wand2, wand3, BlendCompositeOp, MagickTrue, 0, 0);
        MagickCompositeImage(wand2, wand3, BlendCompositeOp, 0, 0);
        // -level 100%
        MagickLevelImage(wand2, QuantumRange, 1.0, 0);
        // output.png
        MagickWriteImage(wand2, "output.png");
    
        wand2 = DestroyMagickWand(wand2);
        wand3 = DestroyMagickWand(wand3);
        MagickWandTerminus();
        return 0;
    }
    

    output.png