⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gifimage.pas

📁 TGIFiMAGE 2.2 支持D7等 Changed 2007.01.18 This is a version of Anders Melander s TGIFImage ported t
💻 PAS
📖 第 1 页 / 共 5 页
字号:
unit GIFImage;
////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// Project:	GIF Graphics Object                                           //
// Module:	gifimage                                                      //
// Description:	TGraphic implementation of the GIF89a graphics format         //
// Version:	2.2                                                           //
// Release:	5                                                             //
// Date:	23-MAY-1999                                                   //
// Target:	Win32, Delphi 2, 3, 4 & 5, C++ Builder 3 & 4                  //
// Author(s):	anme: Anders Melander, anders@melander.dk                     //
//		fila: Filip Larsen                                            //
//		rps: Reinier Sterkenburg                                      //
// Copyright:	(c) 1997-99 Anders Melander.                                  //
//		All rights reserved.                                          //
// Formatting:	2 space indent, 8 space tabs, 80 columns.                     //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
// Changed 2001.07.23 by Finn Tolderlund:                                     //
// Changed according to e-mail from "Rolf Frei" <rolf@eicom.ch>               //
//   on 2001.07.23 so that it works in Delphi 6.                              //
//                                                                            //
// Changed 2002.07.07 by Finn Tolderlund:                                     //
// Incorporated additional modifications by Alexey Barkovoy (clootie@reactor.ru)
// found in his Delphi 6 GifImage.pas (from 22-Dec-2001).                     //
// Alexey Barkovoy's Delphi 6 gifimage.pas can be downloaded from             //
//   http://clootie.narod.ru/delphi/download_vcl.html                         //
// These changes made showing of animated gif files more stable. The code     //
// from 2001.07.23 could crash sometimes with an Execption EAccessViolation.  //
//                                                                            //
// Changed 2002.10.06 by Finn Tolderlund:                                     //
// Delphi 7 compatible.                                                       //
//                                                                            //
// Changed 2003-03-06 by Finn Tolderlund:                                     //
// Changes made as a result of postings in borland.public.delphi.graphics     //
// from 2003-02-28 to 2003-03-05 where white (255,255,255) in a bitmap        //
// was converted to (254,254,254) in the gif.                                 //
// The doCreateOptimizedPaletteFromSingleBitmap function and                  //
// the CreateOptimizedPaletteFromManyBitmaps function is changed so that      //
// the correct offset 246 is used instead of 245.                             //
// The ReduceColors function is changed according to Anders Melander's post   //
// so that a colour get converted to the precise colour if that colour is     //
// present in the palette when using ColorReduction rmQuantize.               //
//                                                                            //
// Changed 2003-03-09 by Finn Tolderlund:                                     //
// Delphi 7 version is now assumed if unknown compiler version is unknown     //
// for better compatibility with future Delphi versions.                      //
// Hopefully this code is now compatible with future Delphi versions,         //
// unless Borland makes some changes that breaks existing code.               //
//                                                                            //
// Changed 2003-08-04 by Finn Tolderlund:                                     //
// Changed procedure AddMaskOnly so that it doesn't leak a GDI HBitmap-object //
// and it doesn't release the handle of the source bitmap which               //
// is used to assign to the GIF object as in gif.assign(bm);                  //
// These changes were made as a result of a news post made by Renate Schaaf   //
// with the subject "TGifImage HBitmap leak on assign?"                       //
// in borland.public.delphi.graphics on Mon 28 Jul 2003 and Sun 03 Aug 2003.  //
//                                                                            //
// Changed 2004.03.09 by Finn Tolderlund:                                     //
// Added a ForceFrame property to the TGIFImage class.                        //
// The ForceFrame property can be used to make TGIFImage display a apecific   //
// sub frame from an animated gif.                                            //
// How to use: Set the Animate property to False and set the ForceFrame       //
// property to a desired frame number (0-N)                                   //
// Normal display: Set the ForceFrame property to -1 and set Animate to True. //
// If ForceFrame is negative TGIFImage behaves just as before this change.    //
// Note that if the sub frame in the gif only contains part of the image      //
// (i.e. only the changes from previous frames) the result is unpredictable.  //
// The result is best if each sub frame contains a whole image.               //
// If the sub frame is transparent the background is not automatically        //
// restored, you must do so yourself if you want that.                        //
// If you are using a TImage to display the gif you can use                   //
// Image.Parent.Invalidate or Image.Parent.Refresh to restore the background. //
// This change was made as a result of a email correspondance with            //
// Tineke Kosmis (http://www.classe.nl/) which requested such a property.     //
//                                                                            //
// Changed 2006.07.09 by Finn Tolderlund:                                     //
// Added conditional switch as default: FIXHEADER_WIDTHHEIGHT_SILENT          //
// When the switch is defined:                                                //
// When loading a gif all frames are examined. If a frame has a larger        //
// Width/Height than the header values then the header values are updated     //
// with the larger values from the frame.                                     //
// I had a MANTA.GIF where the header said 120x89 but the frames said 200x148 //
// and the frames got clipped. MSIE didn't clip it.                           //
// http://www.graphcomp.com/info/specs/ani_gif.html :                         //
// Do not assume all of your images are the same size. Read through their     //
// sizes and set the logical screen to the largest width & height included    //
// in the file.                                                               //
// By removing the define FIXHEADER_WIDTHHEIGHT_SILENT                        //
// the header is not altered. This makes the unit work as before.             //
//                                                                            //
// Changed 2006.07.10 by Finn Tolderlund:                                     //
// Added conditional switch as default: DEFAULT_GOCLEARLOOP                   //
// When the switch is defined:                                                //
// When loading a gif default DrawOptions include goClearLoop                 //
// Same as adding goClearLoop manually to DrawOptions.                        //
// This will clear an animated gif before first frame on each loop.           //
// Someone sent me a 'conductor.gif' where some of the last frame was retaind //
// when beginning a new loop and that was visually incorrect.                 //
// Without glClearLoop the first frame may look different on the second loop  //
// because some part of the last frame could still be present.                //
// With goClearLoop the first frame will always look the same on each loop.   //
// I think the last is better.                                                //
//                                                                            //
// Changed 2006.07.29 by Finn Tolderlund:                                     //
// Added a check in procedure TGIFSubImage.Decompress to make sure that       //
// the InitialBitsPerCode variable never exeeds the value 15.                 //
// Someone sent an animated iup110296.gif (corrupt I think) which caused      //
// this unit to crash in function NextLZW because InitialBitsPerCode was 20.  //
// This fix prevents the crash and should not cause problems with other gifs. //
// Not sure that the fix is the correct way to handle it. It seems to work.   //
//                                                                            //
// Changed 2006.10.09 by Finn Tolderlund:                                     //
// Received a mail from Michael Thomas Greer with a fix that allows           //
// the TGIFSubImage.Pixels[] property to be writeable. The help file states   //
// that the Pixels property can be written, but it was read-only.             //
// Help file: "Write Pixels to change the color index of individual pixels".  //
//                                                                            //
// Changed 2006.10.16 by Finn Tolderlund:                                     //
// Received a mail from Maurizio Lotauro who was using Delphi 5 and FastMM4.  //
// FastMM4 complains about a memory leak when using Delphi 5.                 //
// I don't have Delphi 5 installed so I can't test if there really is a       //
// memory leak or if it's just FastMM4 which can't detect it correctly.       //
// The problem and fix only applies to Delphi 5 or older.                     //
// Added a fix to keep FastMM4 happy. See more at this link:                  //
// http://sourceforge.net/forum/forum.php?thread_id=1559584&forum_id=443400   //
//                                                                            //
// Changed 2007.01.18 by Finn Tolderlund:                                     //
// The ReduceColors function is changed so that it's now possible to use      //
// the TFastColorLookup class if you use ColorReduction rmQuantize.           //
// The TFastColorLookup class was removed 2003-03-06, but is introduced again //
// because Paul Lopez needed speed when adding images to a gif.               //
// This changes how rmQuantize works: It's now fast but less precise.         //
// This means:                                                                //
// Use rmQuantizeWindows to get precision, use rmQuantize if you need speed.  //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// Please read the "Conditions of use" in the release notes.                  //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
// Known problems:
//
// * The combination of buffered, tiled and transparent draw will display the
//   background incorrectly (scaled).
//   If this is a problem for you, use non-buffered (goDirectDraw) drawing
//   instead.
//
// * The combination of non-buffered, transparent and stretched draw is
//   sometimes distorted with a pattern effect when the image is displayed
//   smaller than the real size (shrinked).
//
// * Buffered display flickers when TGIFImage is used by a transparent TImage
//   component.
//   This is a problem with TImage caused by the fact that TImage was designed
//   with static images in mind. Not much I can do about it.
//
////////////////////////////////////////////////////////////////////////////////
// To do (in rough order of priority):
// { TODO -oanme -cFeature : TImage hook for destroy notification. }
// { TODO -oanme -cFeature : TBitmap pool to limit resource consumption on Win95/98. }
// { TODO -oanme -cImprovement : Make BitsPerPixel property writable. }
// { TODO -oanme -cFeature : Visual GIF component. }
// { TODO -oanme -cImprovement : Easier method to determine DrawPainter status. }
// { TODO -oanme -cFeature : Import to 256+ color GIF. }
// { TODO -oanme -cFeature : Make some of TGIFImage's properties persistent (DrawOptions etc). }
// { TODO -oanme -cFeature : Add TGIFImage.Persistent property. Should save published properties in application extension when this options is set. }
// { TODO -oanme -cBugFix : Solution for background buffering in scrollbox. }
//
//////////////////////////////////////////////////////////////////////////////////
{$ifdef BCB}
{$ObjExportAll On}
{$endif}

interface
////////////////////////////////////////////////////////////////////////////////
//
//		Conditional Compiler Symbols
//
////////////////////////////////////////////////////////////////////////////////
(*
  DEBUG				Must be defined if any of the DEBUG_xxx
  				symbols are defined.
                                If the symbol is defined the source will not be
                                optimized and overflow- and range checks will be
                                enabled.

  DEBUG_HASHPERFORMANCE		Calculates hash table performance data.
  DEBUG_HASHFILLFACTOR		Calculates fill factor of hash table -
  				Interferes with DEBUG_HASHPERFORMANCE.
  DEBUG_COMPRESSPERFORMANCE	Calculates LZW compressor performance data.
  DEBUG_DECOMPRESSPERFORMANCE	Calculates LZW decompressor performance data.
  DEBUG_DITHERPERFORMANCE	Calculates color reduction performance data.
  DEBUG_DRAWPERFORMANCE		Calculates low level drawing performance data.
  				The performance data for DEBUG_DRAWPERFORMANCE
                                will be displayed when you press the Ctrl key.
  DEBUG_RENDERPERFORMANCE	Calculates performance data for the GIF to
  				bitmap converter.
  				The performance data for DEBUG_DRAWPERFORMANCE
                                will be displayed when you press the Ctrl key.

  GIF_NOSAFETY			Define this symbol to disable overflow- and
				range checks.
                                Ignored if the DEBUG symbol is defined.

  STRICT_MOZILLA		Define to mimic Mozilla as closely as possible.
  				If not defined, a slightly more "optimal"
                                implementation is used (IMHO).

  FAST_AS_HELL			Define this symbol to use strictly GIF compliant
  				(but too fast) animation timing.
                                Since our paint routines are much faster and
                                more precise timed than Mozilla's, the standard
                                GIF and Mozilla values causes animations to loop
                                faster than they would in Mozilla.
                                If the symbol is _not_ defined, an alternative
                                set of tweaked timing values will be used.
                                The tweaked values are not optimal but are based
                                on tests performed on my reference system:
                                - Windows 95
                                - 133 MHz Pentium
                                - 64Mb RAM
                                - Diamond Stealth64/V3000
                                - 1600*1200 in 256 colors
                                The alternate values can be modified if you are
                                not satisfied with my defaults (they can be
                                found a few pages down).

  REGISTER_TGIFIMAGE            Define this symbol to register TGIFImage with
  				the TPicture class and integrate with TImage.
                                This is required to be able to display GIFs in
                                the TImage component.
                                The symbol is defined by default.
                                Undefine if you use another GIF library to
                                provide GIF support for TImage.

  PIXELFORMAT_TOO_SLOW		When this symbol is defined, the internal
  				PixelFormat routines are used in some places
                                instead of TBitmap.PixelFormat.
                                The current implementation (Delphi4, Builder 3)
                                of TBitmap.PixelFormat can in some situation
                                degrade performance.
                                The symbol is defined by default.

  CREATEDIBSECTION_SLOW		If this symbol is defined, TDIBWriter will
  				use global memory as scanline storage, instead
                                of a DIB section.
                                Benchmarks have shown that a DIB section is
                                twice as slow as global memory.
                                The symbol is defined by default.
                                The symbol requires that PIXELFORMAT_TOO_SLOW
                                is defined.

  SERIALIZE_RENDER		Define this symbol to serialize threaded
  				GIF to bitmap rendering.
                                When a GIF is displayed with the goAsync option
                                (the default), the GIF to bitmap rendering is
                                executed in the context of the draw thread.
                                If more than one thread is drawing the same GIF
                                or the GIF is being modified while it is
                                animating, the GIF to bitmap rendering should be
                                serialized to guarantee that the bitmap isn't
                                modified by more than one thread at a time. If
                                SERIALIZE_RENDER is defined, the draw threads
                                uses TThread.Synchronize to serialize GIF to
                                bitmap rendering.

  FIXHEADER_WIDTHHEIGHT_SILENT  Define this symbol to adjust Width and Height
                                in the header if any of the frames has a larger
                                Width or Height.

  DEFAULT_GOCLEARLOOP           Define this symbol to clear animation on each
                                loop before first frame.
                                Same as adding goClearLoop to DrawOptions.
                                STRICT_MOZILLA does the same,
                                but STRICT_MOZILLA does something more.

*)

{$DEFINE REGISTER_TGIFIMAGE}
{$DEFINE PIXELFORMAT_TOO_SLOW}
{$DEFINE CREATEDIBSECTION_SLOW}
{$DEFINE FIXHEADER_WIDTHHEIGHT_SILENT}
{$DEFINE DEFAULT_GOCLEARLOOP}

////////////////////////////////////////////////////////////////////////////////
//
//		Determine Delphi and C++ Builder version
//
////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -