Page 1 of 1

Detect corrupt images with the C API

Posted: 2011-11-17T07:00:58-07:00
by dirk1976
Hello,

is there a way to detect corrupted images with the C API, like image files with a "premature end of data segment" error?
The command line tools have the "regard-warnings" to fail for such errors.

Thank you

Re: Detect corrupt images with the C API

Posted: 2011-11-17T21:31:38-07:00
by anthony
The errors and warnings are returned from all operations as status errors. See an API example.

Re: Detect corrupt images with the C API

Posted: 2011-11-17T23:48:02-07:00
by dirk1976
This is the behavior I read in the api and I expect it. This works for non image files or non existing files. In this cases a error is thrown. For images like corrupt jpg files (not all image data was written) it don't work.

I'm a Java programmer and not familiar with C. I use a java library (it's like JNA (Java Native Access)) to call the C methods. All works fine until I test some corrupt jpg files. To find out a solution by myself I read the documentation, the *.c and header files. Also goolge find no answer for my question.

How my code looks like (pseudo code)

Code: Select all

 MagickWandGenesis();
 magickWand = NewMagickWand();
 status = MagickReadImage(magickWand, imageFile);
 type = MagickGetExceptionType(magickWand)
 if (type != UndefinedException) {
   exceptionMsg = MagickGetException(magickWand, exceptionType);
   MagickClearException(magickWand);
   throw new Exception(exceptionMsg);
 }

Re: Detect corrupt images with the C API

Posted: 2011-11-21T03:41:55-07:00
by dirk1976
I test the same with a small C program with the same result. A corrupt jpg file (Premature end of JPEG file) produce no error or warning.

This is my test program

Code: Select all

#include "wand/MagickWand.h"

int main() {
	(void) printf("Copyright: %s\n", GetMagickCopyright());
	(void) printf("Features: %s\n", GetMagickFeatures());
	(void) printf("Package-Name: %s\n", GetMagickPackageName());
	(void) printf("ReleaseDate: %s\n", GetMagickReleaseDate());

	MagickWandGenesis();
	MagickWand* magick_wand = NewMagickWand();
	unsigned int status = MagickReadImage(magick_wand,"d:\\temp\\2011-11\\pictures\\test_corrupt.jpg");
	(void) printf("Status: %i\n", status);
	ExceptionType type = type = MagickGetExceptionType(magick_wand);
	(void) printf("ExceptionType: %i\n", type);
	if (type == UndefinedException) {
		int resized = MagickResizeImage(magick_wand, 200, 200, UndefinedFilter,	0.0);
		(void) printf("Resized: %i\n", resized);
		type = MagickGetExceptionType(magick_wand);
		(void) printf("ExceptionType: %i\n", type);
	}
	DestroyMagickWand(magick_wand);
	MagickWandTerminus();

	return 0;
}
Do I something wrong in the program code? Can someone help me?

Thank you

Re: Detect corrupt images with the C API

Posted: 2011-11-21T05:18:43-07:00
by magick
Post a URL to you corrupt image. We need to reproduce the problem before we can comment.

Re: Detect corrupt images with the C API

Posted: 2011-11-21T05:45:50-07:00
by dirk1976
My test images can be downloaded here http://goo.gl/Kg2Bq
You can load a normal jpg image in a hex editor and cut some data from the end of the file.

Re: Detect corrupt images with the C API

Posted: 2011-11-21T07:42:00-07:00
by magick
We're getting expected results with ImageMagick 6.7.3-7, the current release. We added this code:
  • if (type != UndefinedException) {
    ExceptionType severity;
    puts(MagickGetException(magick_wand,&severity));
    }
and its returns a corrupt image:
  • -> wand
    Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
    Features: OpenMP OpenCL
    Package-Name: ImageMagick
    ReleaseDate: 2011-11-21
    Status: 1
    ExceptionType: 325
    Corrupt JPEG data: premature end of data segment `corrupt.jpg' @ warning/jpeg.c/JPEGWarningHandler/338

Re: Detect corrupt images with the C API

Posted: 2011-11-21T11:35:22-07:00
by dirk1976
Thank you for your test. I try it now on three different windows machines. On two it don't work and on one it works like expected. Two machines contains older IM versions in the past and the working node was a clean machine. Have you an explanation for this result?

Thank you

Re: Detect corrupt images with the C API

Posted: 2011-11-21T22:58:03-07:00
by dirk1976
I found out why it don't work. If IM is installed during the Windows installer then no error is reported. If only the IM folder is copied to a "clean" (no installed IM) machine then it works fine.
I try to rename some registry keys. If the key "HKEY_LOCAL_MACHINE\SOFTWARE\ImageMagick\6.7.3\Q:16" is renamed then it works also with an installed IM version.

Have you an explanation for this?

Thank you.

Re: Detect corrupt images with the C API

Posted: 2011-11-21T23:10:46-07:00
by dirk1976
Forgot my last post. The error is different on a "clean" machine if IM is only copied (no decode delegate for this image format). This means IM can't find the jpg library.

I installed IM on some other "clean" machines and I get the same result as on my own computer. The error/warning is not reported.

Have you an explanation for this?

Thank you.

Re: Detect corrupt images with the C API

Posted: 2012-06-28T02:15:29-07:00
by dirk1976
Hello,

now I find some time to try the tests again. But my program cannot detect a corrupt jpeg file (premature end of data segment) over the wand api. I test it on Windows XP and Windows 7. My test program was compiled with mingw. The identify command line program can detect the corrupt image. The strange thing, it seems only this error is not thrown. A jpeg without a data segment is reported and also corrupt png and gif files are detected. It also don't belongs to the IM version I use different versions the behavior is still the same.

Test files:
jpeg: http://goo.gl/dc2Q2
gif: http://goo.gl/Cb2ao
program: http://goo.gl/AE8bE
program source: http://goo.gl/BFKqF

Compile commands:

Code: Select all

>gcc -I{PATH}\ImageMagick-6.7.7-7 -O0 -g3 -Wall -c -fmessage-length=0 -o main.o main.c
>gcc -L{PATH}\ImageMagick-6.7.7-Q16 -o magick_test.exe main.o -lCORE_RL_magick_ -lCORE_RL_wand_
Console output:

Code: Select all

>magick_test.exe test_corrupt.jpg
0: magick_test.exe
1: test_corrupt.jpg
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Version: ImageMagick 6.7.7-4 2012-05-29 Q16 http://www.imagemagick.org
Features: OpenMP
Package-Name: ImageMagick
ReleaseDate: 2012-05-29
Status: 1
Width: 640
Height: 400
ExceptionType: 0

Code: Select all

>magick_test.exe test_corrupt.gif
0: magick_test.exe
1: test_corrupt.gif
Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
Version: ImageMagick 6.7.3-7 2011-11-17 Q16 http://www.imagemagick.org
Features: OpenMP
Package-Name: ImageMagick
ReleaseDate: 2011-11-17
Status: 0
Width: 0
Height: 0
ExceptionType: 470
wand contains no images `MagickWand-1' @ error/magick-image.c/MagickGetImageHeight/5183
identify console output:

Code: Select all

>identify -verbose -regard-warnings test_corrupt.jpg
Image: test_corrupt.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Class: DirectClass
  ...
  Version: ImageMagick 6.7.3-7 2011-11-17 Q16 http://www.imagemagick.org
identify.exe: Premature end of JPEG file `test_corrupt.jpg' @ warning/jpeg.c/JPE
GWarningHandler/325.
identify.exe: Corrupt JPEG data: premature end of data segment `test_corrupt.jpg
' @ warning/jpeg.c/JPEGWarningHandler/325.
>echo %errorlevel%
1
What is going wrong? Belongs it to the mingw compiler or to the installed IM versions (there are different versions installed on my system)? I also inspect the loaded libraries and all seems correct.
Can someone reproduce this behavior? And is there an explanation for this?

Many Thanks

Re: Detect corrupt images with the C API

Posted: 2012-06-28T07:41:41-07:00
by el_supremo
I tried your code in 6.7.5-10 and it reads the jpg without throwing an exception. Both Firefox and Paint Shop Pro X4 open the JPG without complaint. However, they don't display exactly the same image. OTOH, Corel Instant Viewer also opens the image but then hangs.
Does, perhaps, the JPG standard have some defined way of handling that problem which maybe Instant Viewer doesn't do as well as the others?

Pete

Re: Detect corrupt images with the C API

Posted: 2012-07-09T23:44:16-07:00
by dirk1976
No one can help or explain this behaviour?