Page 1 of 1

A question about reading and writing gif image

Posted: 2009-06-19T01:31:20-07:00
by sunng1
hi everyone

I have a GIF image :
Image

It's 27.4KB

My code is :

Code: Select all

int main()
{
	ExceptionInfo * exception = AcquireExceptionInfo();
	ImageInfo *image_info = CloneImageInfo((ImageInfo *) NULL);
	(void) CopyMagickString(image_info->filename, "/root/346889.gif" ,MaxTextExtent);
	Image * image = ReadImage(image_info, exception);
	
	Image * coalImage = CoalesceImages(image, exception);
	
	(void) CopyMagickString(coalImage->filename, "/root/1.gif", MaxTextExtent);
	WriteImage(image_info ,coalImage);
	
	DestroyImages(image);
	DestroyImages(coalImage);
	DestroyImageInfo(image_info);
	DestroyExceptionInfo(exception);
	
	return 0;
}

the output GIF image is :

Image

It's 271KB

In my code, I used CoalesceImages to get the same size images of te GIF image, and my question is :

How to write a GIF image with the same file size after use CoalesceImages ?

thanks.

Re: A question about reading and writing gif image

Posted: 2009-06-19T06:40:02-07:00
by magick
Use CompareImageLayers().

Re: A question about reading and writing gif image

Posted: 2009-06-19T19:39:30-07:00
by sunng1
I modified the code :

Code: Select all

int main()
{
   ExceptionInfo * exception = AcquireExceptionInfo();
   ImageInfo *image_info = CloneImageInfo((ImageInfo *) NULL);
   (void) CopyMagickString(image_info->filename, "/root/346889.gif" ,MaxTextExtent);
   Image * image = ReadImage(image_info, exception);
   
   Image * coalImage = CoalesceImages(image, exception);
   
    //Image * compImage = CompareImageLayers(coalImage, CompareAnyLayer, exception);
    //Image * compImage = CompareImageLayers(coalImage, CompareClearLayer, exception);
    Image * compImage = CompareImageLayers(coalImage, CompareOverlayLayer, exception);

   
    //(void) CopyMagickString(compImage->filename, "/root/CompareAnyLayer.gif", MaxTextExtent);
    //(void) CopyMagickString(compImage->filename, "/root/CompareClearLayer.gif", MaxTextExtent);
    (void) CopyMagickString(compImage->filename, "/root/CompareOverlayLayer.gif", MaxTextExtent);

    WriteImage(image_info ,compImage);
   
   DestroyImages(image);
   DestroyImages(coalImage);
   DestroyImageInfo(image_info);
   DestroyExceptionInfo(exception);
   
   return 0;
}
Tried all three ImageLayerMethod which CompareImageLayers support, the result is :

CompareAnyLayer
Image

121.8KB
---------------------------------------------------------------------
CompareClearLayer
Image

14.6KB, but ,all frames are the same
---------------------------------------------------------------------
CompareOverlayLayer
Image

121.8KB
---------------------------------------------------------------------

the file size still bigger then the original GIF image, so what can I do?

thanks

Re: A question about reading and writing gif image

Posted: 2009-06-19T19:45:16-07:00
by fmw42
I know very little about gif animations and especially doing them with an API. But you might glean some ideas from Anthony's pages on animations from the command line and especially the one on optimizations at http://www.imagemagick.org/Usage/anim_opt/

Your problem may be one of having more than 256 colors used throughout the full set of images rather than a common set of 256 colors used by every frame.

Re: A question about reading and writing gif image

Posted: 2009-06-21T23:58:26-07:00
by anthony
Actually to do it properly you would use the Optimize Layer Images function.

But I am not certain how it would be called in the C API you are using.

Re: A question about reading and writing gif image

Posted: 2009-06-22T21:03:18-07:00
by sunng1
yeah,

After using OptimizeImageLayers and OptimizeImageTransparency, the file size has down to about 30K,

then after using global color table instead of local color table, the file size has reached the original file size.

thanks, anthony!
thanks, fmw42!
tanks, magick!


:)