Page 1 of 1

MagickGetImage(s)Blob results in an empty image

Posted: 2020-05-16T18:13:23-07:00
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.

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

Posted: 2020-05-17T00:32:55-07:00
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.