Best way to detect image type

Help
Matt G
2012-06-09
2013-03-27
  • Matt G
    Matt G
    2012-06-09

    I'm using PingImage (magick API) to find the image type without loading the pixels. That works. I'm also using PingBlob to find the image type of a blob but that doesn't work because PingBlob calls ReadImage to do the work and ReadImage tries to open a file on disk. It's clear that PingBlob is broken and should call BlobToImage instead of ReadImage. But while I work on fixing that bug, is there a better way than the Ping* functions to detect the image type?

     
  • Both PingBlob and BlobToImage invoke ReadImage.  ReadImage is supposed to be able to handle reading from an in-memory blob provided that it has been given correct parameters.  I do see that BlobToImage contains additional code for the case where there is not a way to interject our own I/O mechanisms in the reader, and it creates a temporary file in that case.  For some formats, it may be necessary to use a temporary file.

    I see that PingBlob is not used anywhere in GraphicsMagick (not even in the test suite) so perhaps it is broken.  Anything which is not tested is likely to be broken.

     
  • Matt G
    Matt G
    2012-06-09

    It was my mistake. I didn't set the filename when calling PingBlob because I figured that there would be enough information in the blob itself to determine the file format. PingBlob appears to work correctly when the filename is set. I'm going to verify that ReadImage doesn't try to actually read the file when called for a blob and I'll be all set. Thanks.

     
  • Regardless, today I am re-working PingBlob() to be based on BlobToImage() (which is very well tested) and am adding tests in the test suite for PingBlob() and PingImage() to make sure there are no issues.  I will commit the updates once it is verified that there are no remaining memory leaks discovered by the additional testing (I did find one).

    The ability to auto-detect file format is very dependent on the nature of the file format.  If the format provides a reliable and distinct header, then we can auto-detect it.

     
  • Changes are  now committed to Mercurial so that PingBlob() uses BlobToImage() and this path is included in the test suite.  New source snapshot package is being produced.