MagickGetImage(s)Blob results in an empty image

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Locked
Essem
Posts: 5
Joined: 2020-02-06T12:13:22-07:00
Authentication code: 1152

MagickGetImage(s)Blob results in an empty image

Post by Essem »

Hello,
I am attempting to use the MagickGetImagesBlob function to get a buffer of the image output using ImageMagick version 7.0.10-10 Q16. I've adapted some code from the MagickWand main page to print the output to stdout:

Code: Select all

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

int main(int argc,char **argv)
{
#define ThrowWandException(wand) \
{ \
  char \
    *description; \
 \
  ExceptionType \
    severity; \
 \
  description=MagickGetException(wand,&severity); \
  (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
  description=(char *) MagickRelinquishMemory(description); \
  exit(-1); \
}

  MagickBooleanType
    status;

  MagickWand
    *magick_wand;

  if (argc != 2)
    {
      (void) fprintf(stdout,"Usage: %s image\n",argv[0]);
      exit(0);
    }
  /*
    Read an image.
  */
  MagickWandGenesis();
  magick_wand=NewMagickWand();
  status=MagickReadImage(magick_wand,argv[1]);
  if (status == MagickFalse)
    ThrowWandException(magick_wand);
  /*
    Turn the images into a thumbnail sequence.
  */
  MagickResetIterator(magick_wand);
  while (MagickNextImage(magick_wand) != MagickFalse)
    MagickResizeImage(magick_wand,106,80,LanczosFilter);
  /*
    Write the image then destroy it.
  */
  size_t my_size;
  unsigned char * my_image = MagickGetImagesBlob(magick_wand, &my_size);
  fprintf(stdout, "%s\n", my_image);
  magick_wand = DestroyMagickWand(magick_wand);
  MagickWandTerminus();
  return(0);
}
However, when trying to run the program with any image (e.g. ./wand image.jpg), it outputs a buffer containing only the header:

Code: Select all

$ ./wand image.jpg
����
This is the case for every image I try. Attempting to use a PNG results in this:

Code: Select all

$ ./wand image.png
�PNG
�

As with GIF:

Code: Select all

$ ./wand image.gif
GIF89aj
The same also occurs when I try to use MagickGetImageBlob instead of MagickGetImagesBlob. This is very strange since the original example on the page that writes to a file works just fine for me. Is there something I'm missing or is this a bug in MagickWand? Thanks in advance.

User avatar
dlemstra
Posts: 1625
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: MagickGetImage(s)Blob results in an empty image

Post by dlemstra »

The end of a string is a null terminating character ('\0'). And when that is reached it will stop printing the string. In your examples it stops printing the character when that zero is reached.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate

Locked