A question about reading and writing gif image

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
sunng1

A question about reading and writing gif image

Post 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.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: A question about reading and writing gif image

Post by magick »

Use CompareImageLayers().
sunng1

Re: A question about reading and writing gif image

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: A question about reading and writing gif image

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: A question about reading and writing gif image

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
sunng1

Re: A question about reading and writing gif image

Post 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!


:)
Post Reply