How to determine if a photo is corrupted?

I have a requirement where in I have to determine whether a photo is corrupted and accordingly tag it as such.

You can try some things, but with certain file formats (example: BMP, JPEG to some extent) only a human can ultimately decide if the file is OK or corrupted. The simplest test is to simply load the file into a corresponding object (TJpegImage, TPngObject, etc). If you get an exception while loading you’ve surely got a corrupted file. Unfortunately if no exception is raised you can’t really say the file is not corrupted. I’ve seen corrupted JPEG files that load just fine into a Delphi TImage and can be opened with Windows’s Image Viewer, but are obviously corrupted to a human observer. With BMP images it’s even clearer: open up a bitmap, overwrite some bytes in the middle of the file and then open it in a viewer. How can any automated system tell those wrongly colored bits in the middle of the bitmap are actually wrong?

Another thing, I need is to determine if an Image has got wrong extension. What I mean by wrong extension is that sometimes I have come across a photo that has extension of jpg but when I load this photo into IrfanView it reports that the photo is in different format that the extension.

How about doing some of the same, trying to load the file into the object that corresponds to it’s extension, and if you fail, try opening up with some other formats? This should be easy.

Alternatively you can investigate image headers: Most file formats start with a short signature, a few bytes. You can look up the documentation of all image file formats and find the signature, or you can simply open up an large number of files and look for a pattern in the first 4 bytes. I’d go for this second alternative since finding proper documentation for all image file formats might be a challenge.

Leave a Comment