cimagemagickmagickwand

How to pass Parameters?


I have been converting convert rose.png -sparse-color barycentric '0,0 black 69,0 white roseModified.png into MagickWand C API.

double arguments[6];
arguments[0] = 0.0;
arguments[1] = 0.0;
// arguments[2] = "black";
arguments[2] = 69.0;
arguments[3] = 0.0;
// arguments[5] = "white";

MagickSparseColorImage(wand0, BarycentricColorInterpolate, 4,arguments);
MagickWriteImage(wand0,"rose_cylinder_22.png");

I don't know how to pass the double argument. click here for method's defenition.

UPDATE: Source Image

enter image description here

After I executed convert rose.png -sparse-color barycentric '0,0 black 69,0 white' roseModified.png, I got below Image

enter image description here

I haven't got the output like this with my C program. There might be something with white and black.


Solution

  • For sparse colors, you need to convert the color to doubles for each channel. Depending on how dynamic you need to generate spares color points, you may need to start building basic stack-management methods.

    Here's an example. (Mind that this is a quick example, and can be improved on greatly)

    #include <stdlib.h>
    #include <MagickWand/MagickWand.h>
    
    // Let's create a structure to keep track of arguments.
    struct arguments {
        size_t count;
        double * values;
    };
    
    // Set-up structure, and allocate enough memory for all colors.
    void allocate_arguments(struct arguments * stack, size_t size)
    {
        stack->count = 0;
        // (2 coords + 3 color channel) * number of colors
        stack->values = malloc(sizeof(double) * (size * 5));
    }
    
    // Append a double value to structure.
    void push_double(struct arguments * stack, double value)
    {
        stack->values[stack->count++] = value;
    }
    
    // Append all parts of a color to structure.
    void push_color(struct arguments * stack, PixelWand * color)
    {
        push_double(stack, PixelGetRed(color));
        push_double(stack, PixelGetGreen(color));
        push_double(stack, PixelGetBlue(color));
    }
    
    #define NUMBER_OF_COLORS 2
    
    int main(int argc, const char * argv[]) {
    
        MagickWandGenesis();
    
        MagickWand * wand;
        PixelWand ** colors;
    
        struct arguments A;
        allocate_arguments(&A, NUMBER_OF_COLORS);
    
        colors = NewPixelWands(NUMBER_OF_COLORS);
        PixelSetColor(colors[0], "black");
        PixelSetColor(colors[1], "white");
        // 0,0 black
        push_double(&A, 0);
        push_double(&A, 0);
        push_color(&A, colors[0]);
        // 69,0 white
        push_double(&A, 69);
        push_double(&A, 0);
        push_color(&A, colors[1]);
    
        // convert rose:
        wand = NewMagickWand();
        MagickReadImage(wand, "rose:");
        // -sparse-color barycentric '0,0 black 69,0 white'
        MagickSparseColorImage(wand, BarycentricColorInterpolate, A.count, A.values);
        MagickWriteImage(wand, "/tmp/output.png");
    
        MagickWandTerminus();
        return 0;
    }