Stack Overflow when load wmf file

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
jstph
Posts: 31
Joined: 2011-01-27T10:07:43-07:00
Authentication code: 8675308

Stack Overflow when load wmf file

Post by jstph »

Sorry for the silly problem again.
I try to load a in-memory wmf image, got the stack overflow exception. I couldn't figure out which steps I missed. The exception is generated at ReadPSImage in the ps.c, and the wmf file is in the WMF fold coming with IM.
  • my env: IM 6.6.7
    installed GS 9.00 (x86)
    sys: win2008 R2

Code: Select all

int main(int argc, char** argv)
{
		MagickCoreGenesis(*argv,MagickTrue);

	  FILE* file = NULL;
	  if( fopen_s( &file, "anim0002.wmf", "rb" ) != 0)
		{
			printf("load file error");
			exit(1);
		}

		fseek(file, 0, SEEK_END);

		size_t length = ftell(file);

		fseek(file, 0, SEEK_SET);

		BYTE* databyte = new BYTE[length];

		int rlength = fread(databyte, 1, length, file);

    Image* image = NULL;
    ImageInfo *image_info;
    ExceptionInfo* exception = AcquireExceptionInfo();
    image_info=AcquireImageInfo();

		try
		{
			image = BlobToImage(image_info, databyte, length, &exception);
		}
		catch(...)
		{
			return 1;
		}

		delete [] databyte;
		fclose(file);

		if(image != NULL)
		{
			DestroyImage(image);
		}

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

Re: Stack Overflow when load wmf file

Post by magick »

The problem could be specific to your WMF image file. Can you post a URL to anim0002.wmf so we can download it and reproduce the problem?
jstph
Posts: 31
Joined: 2011-01-27T10:07:43-07:00
Authentication code: 8675308

Re: Stack Overflow when load wmf file

Post by jstph »

thank you for the quick response.
The test file is coming with ImageMagick. It locates at ImageMagick-windows\ImageMagick-6.6.7\wmf\examples\anim0002.wmf. I am not able to upload this file in this moment. I can find a place to put it later.
I have a quick question to ask. Do I need freetype library in order to load WMF file? Someone told me so, I am not sure if it will be the case.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Stack Overflow when load wmf file

Post by magick »

You do not need Freetype to open WMF image files.

We're Linux developers. We took your code snippet and plopped it into a Magick++ Posix threads framework with 5 workers and 5 iterations for a total of 25 near simultaneous reads of the anim0002.wmf image. We ran it about 1000 times and it did not fail once. If you want us to try it under Windows, we'll need code that we can download and compile since we're not well versed on Windows threads.
jstph
Posts: 31
Joined: 2011-01-27T10:07:43-07:00
Authentication code: 8675308

Re: Stack Overflow when load wmf file

Post by jstph »

I really appreciate your help. I will try to construct a complete windows project for you to test.
jstph
Posts: 31
Joined: 2011-01-27T10:07:43-07:00
Authentication code: 8675308

Re: Stack Overflow when load wmf file

Post by jstph »

magick,
I am really bad at constructing a running environment. So instead of creating a simply example, I spend some time to study IM source code. I found something I am not so sure inside the Image magic. Would you mind to clarify it for me?
When call *ReadWMFImage(const ImageInfo *image_info,ExceptionInfo *exception) in wmf.c file, I think it will decode wmf and create a temporary EPS file.

The following snippet is starting at line 213 in wmf.c

read_info=CloneImageInfo(image_info);
(void) FormatMagickString(read_info->filename,MaxTextExtent,"eps:%.1024s",
filename);
image=ReadImage(read_info,exception);

My question in here is, if I use BlobToImage, the image_info->blob will not be NULL, and it will be copy over to read_info. In that case, when call ReadPSImage(const ImageInfo *image_info,ExceptionInfo *exception), the parameter image_info->blob will be pointed to original WMF file.
Then at line 424 of ps.c, status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);

When call OpenBlob, it will check if image_info->blob is NULL or not first.
The following code is from blob.c, it starts from line 2306

if (image_info->blob != (void *) NULL)
{
if (image_info->stream != (StreamHandler) NULL)
image->blob->stream=(StreamHandler) image_info->stream;
AttachBlob(image->blob,image_info->blob,image_info->length);
return(MagickTrue);
}

From here, the reader will read it from original wmf file instead of the decoded EPS file. Then it will form an infinite loop, which cause my stack overflow.

I add two line of code to wmf.c

read_info=CloneImageInfo(image_info);
(void) FormatMagickString(read_info->filename,MaxTextExtent,"eps:%.1024s",
filename);

//******my code need to be removed******
read_info->blob = 0;
read_info->length = 0;
//***************************************
image=ReadImage(read_info,exception);

It solved my problem, and be able to load WMF image from memory.

Since I am really new to ImageMagick, I am not so sure my trace is correct or not. Please let me know.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Stack Overflow when load wmf file

Post by magick »

Good catch. We typically use this statement:
  • SetImageInfoBlob(read_info,(void *) NULL,0);
after a clone but it was missing from the WMF coder. Thanks, we'll get the patch into ImageMagick 6.6.7-5 Beta by sometime tomorrow.
jstph
Posts: 31
Joined: 2011-01-27T10:07:43-07:00
Authentication code: 8675308

Re: Stack Overflow when load wmf file

Post by jstph »

No problem. Although I only work with IM for a week or so, I really like IM and the effort your guys put in.
Thank you for the good work. I may ask more silly questions in the future, apology in advance.
Post Reply