hw-135
hw-135

Reputation: 172

Get binary data from image using Magick Wand API

I am trying to get an image in binary data so that I can base64 encode it. I am using the MagickGetImageBlob() of the Magick Wand API but the Blob I am receiving does not contain the entire information.

My code is as follows. opt and enc are two structs containing user provided parameters and encoding information respectively. The library I am using to encode in base64 is this.

void WriteImg(UserDefinedOptions *options, MyStruct *enc, char *format){

    MagickWand *wand;
    char *outputPath;
    unsigned char *buffer = malloc(sizeof(char)*1000);
    size_t length;
    int flen;

    MagickWandGenesis();

    wand = NewMagickWand();

    MagickConstituteImage(wand, enc->image->width, enc->image->height,
            "RGB", CharPixel, enc->image->pxl);

    MagickSetImageResolution(wand, (double) options->dpi, (double) options->dpi);
    MagickSetImageUnits(wand, PixelsPerInchResolution);

    MagickSetImageFormat(wand, format);

    outputPath = (options->outputPath == NULL) ? "-" : options->outputPath;

    MagickWriteImage(wand, outputPath);  // This works and generates correct image


    buffer = MagickGetImageBlob(wand, &length); // Incomplete binary data
    /* Encode base64 */
    encbuffer = base64(buffer, strlen((const char *)buffer), &flen);    
    printf("Base64:%s\n", encbuffer);  

    CleanupMagick(&wand, DmtxFalse);

    MagickWandTerminus();

}

What am I doing wrong? Is there a better way to get the base64 encoded string from the image using Magick Wand?

Upvotes: 0

Views: 464

Answers (1)

emcconville
emcconville

Reputation: 24419

The second parameter to base64() should be length, not the return value of strlen().

/* Encode base64 */
encbuffer = base64(buffer, length, &flen); 

Upvotes: 1

Related Questions