使用 MagickWand C 自定义浮雕 API

Custom emboss using MagickWand C API

我想实现这个 Emboss effect using ImageMagick's C API. The script is from this 页面。我已经实现了方法 1 选项的浮雕,但我正在寻找方法 2 的转换。请查看 bash 脚本中的选项。

当我为方法 2 调试上面的脚本时,我得到了这个。

 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

下面是我的C程序,还没有完成

#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();
}

这有点棘手,因为您需要 转换 颜色空间并定义 compose-argument 属性。 另请注意,bestfit 应在所有失真上设置为 false。

这是 CLI 和 C 的等价物。

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;
}