BlobToImage fails with .ico file

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
rmagick
Posts: 245
Joined: 2006-03-16T17:30:48-07:00
Location: Durham, NC, USA

BlobToImage fails with .ico file

Post by rmagick »

This is Version: ImageMagick 6.3.7 12/09/07 Q16 http://www.imagemagick.org on Kubuntu Gutsy Gibbon.

Here's the input image: Image.

In the test program, BlobToImage returns NULL and does not set any values in the exception structure. If I store "ICO" in the image_info->magick field, BlobToImage returns a correct image. Please let me know if you need any other information.

Code: Select all

/*
Call BlobToImage on a .ICO file

gcc `Magick-config --cflags --cppflags` icoblob.c `Magick-config --ldflags --libs` -o icoblob
*/


#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include "magick/MagickCore.h"


int main()
{
  Image *image;
  ImageInfo *image_info;
  FILE *img;
  char blob[4000];
  size_t length;
  ExceptionInfo exception;

  MagickCoreGenesis("icoblob" ,MagickFalse);

  image_info = CloneImageInfo(NULL);
  if (!image_info)
  {
    puts("CloneImageInfo failed.");
    exit(1);
  }

  img = fopen("favicon.ico", "r");
  length = fread(blob, 1, sizeof(blob), img);
  if (ferror(img))
  {
      puts("ferror");
      exit(1);
  }

  GetExceptionInfo(&exception);

  // strcpy(image_info->magick, "ico");
  image = BlobToImage(image_info,  blob, (size_t)length, &exception);

  DestroyImageInfo(image_info);

  if (!image)
  {
    puts("No image returned.");
    if (exception.severity > UndefinedException)
    {
        printf("%s: %s", exception.reason, exception.description);
    }
  }
  else
  {
      puts("Got an image");
      DestroyImage(image);
  }

  DestroyMagick();

  exit(0);
  }
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: BlobToImage fails with .ico file

Post by magick »

ICO images do not have a magic number so you must explicitly set the image magick or filename. We added an exception reason in the event the image has no magic number and neither the image magick or filename is set.
rmagick
Posts: 245
Joined: 2006-03-16T17:30:48-07:00
Location: Durham, NC, USA

Re: BlobToImage fails with .ico file

Post by rmagick »

Interesting. Thanks!
Post Reply