Click here to Skip to main content
5,788,212 members and growing! (18,955 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Mobile Development » Graphics and Multimedia     Intermediate License: The Creative Commons Attribution-ShareAlike 2.5 License

Decompress and display animated GIF on PC and PPC

By OdeeanRDeathshead

A class to allow animated gifs to be displayed, manualy scanning and decompressing.
C++, Windows, Win Mobile, Win32, Dev

Posted: 23 Mar 2008
Updated: 23 Mar 2008
Views: 7,815
Bookmarked: 10 times
Note: This is an unedited reader contribution
Announcements
Loading...



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 2.53 Rating: 3.25 out of 5
1 vote, 16.7%
1
0 votes, 0.0%
2
1 vote, 16.7%
3
2 votes, 33.3%
4
2 votes, 33.3%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

The code provided here will allow animated gif files to be displayed on windows within your projects. It does not rely on any library to do this but rather manually scans the gif to determine its contents, decompresses the image data and displays it on the window of your choice. By doing this manually the code has some degree of portability on win32 systems allowing the same code to work in both desktop and pocket applications.

Background

Often while coding a project I have had the need to show some kind of animation, either as a status message to the user or to spice up an attribution dialog. After doing this manually several times using timers and and icons to switch images I needed a better more flexible way. The popularity of the GIF image format and it's ability to display animations made it a prime candidate to store the animations for my code.

Using the code

The class that you will interact with is gifDisplayer, it has four methods to interact with the desired gif file.

    
      //Calls the load from reader then prepairs bitmaps for display,
      //a gif must be loaded before it can be displayed. 
      BOOL loadGif(CString filePath,
                HDC hDCin,
                 BOOL animate,
                    HWND hWnd,
               int multiplySizeBy,
                  int Xoffset,
              int Yoffset);


    BOOL loadGif(unsigned char* bufferIn,
              unsigned long lengthIn,
                   HDC hDCin,
                BOOL animate,
                   HWND hWnd,
              int multiplySizeBy,
                 int Xoffset,
                 int Yoffset); 

    //An image must be loaded by calling the load method from the gifReader first.
    BOOL displayGif();

    //clean up.
    void unloadGif();

Multiple objects of this class can be used simultaneously. Multiple animations can be drawn to the same window simultaneously. What is not supported are multiple animations being stacked in the same place. If that is done the tranceparency will be lost.

To use the class some static members will need to be set up:

#include "stdafx.h"
#include "gifDisplayer.h"
HANDLE gifDisplayer::hAccessMutex=0;
HANDLE gifDisplayer::hTimingThread=0;
int gifDisplayer::countOfObjects=0;
gifDisplayer* gifDisplayer::gifDisplayerObjectArray[MAX_SURFACES];

This is done from the main .cpp file or included in a file from there.

You can instanciate objects from the class after that as normally would be done:

   //The parameter is the starting size for the decompressed gif.
   //This can be set small but a small size will make decompression take longer
   //because the output will have to be grown many times. 
   gifDisplayer* gr=new gifDisplayer(100000);
   gifDisplayer* gr2=new gifDisplayer(100000);
   gifDisplayer* gr3=new gifDisplayer(100000);


 

It is important to be sure your window has been drawn properly before calling loadGif(...) or displayGif() This is because in order to allow the gif to seem transparent on your window, the contents of the window will be copied to form the base over which the gif will show. To do this I post a message to the window after the window has been created allowing a delay for the initial drawing to occure.

     switch (message) 
    {
    case SETUP_MESSAGE:
    {
          //on a mobile device this can be a bit slow
          //so its a good idea to show a wait cursor.
       SetCursor(LoadCursor (NULL,IDC_WAIT));

          //load and display three gifs.
          gr->loadGif(gifPath,//the path of the file
          surfaceDC,//the device context of the window
          TRUE,//do you want to animate?
          hSurfaceWindow,//the handle of the window
          1,//scale the gif by a factor of 1 (possible values 1,2,3,4...)
          50,//x position
          100);//y position

        gr->displayGif();

        gr2->loadGif(gifPath2,
            surfaceDC,
                 TRUE,
           hSurfaceWindow,1,150,200);

        gr2->displayGif();

        gr3->loadGif(gifPath3,
            surfaceDC,
                 TRUE,
           hSurfaceWindow,2,0,0);

        gr3->displayGif();

        SetCursor(0);
    }
    break; 

Remember to call unloadGif()on each object before ending your program.

The gifDisplayer class inherits from a number of other classes I wrote. You only need to include them in your project as shown in the demos. I split the functionality up this way because the scanning, decompressing and displaying are distinctly different parts that are better suited to being independant. Note that I use mfc cstrings and cobjectarrays in my code. The projects themselves are win32 and NOT mfc projects. Some changes need to be made in the project settings and included files to let this work in win32 code. It would be easy enough to replace these classes with your own if you want to get rid of mfc all together.

Points of Interest

All the code in this project is based on the document "LZWand GIF explained" by steve Blackstock and on the compuserve gif standard. Although these documents do not contain any c++ code they are an accurate description of what a gif is.

History

Submitted 24 March 2008.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License

About the Author

OdeeanRDeathshead


Started programming eight years ago in an attempt to see how my computer worked. I then found that coding myself was often the best way to get what I want.

Most of my work has been around windows mobile devices and communications. My most popular project released was gsmBeam, a device to device file transfer tool used over CSD lines on GSM mobile networks.

This is not my job, I just do it for the fun.
visit me at www.odeean.com

Location: Australia Australia

Other popular Mobile Development articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Mar 2008
Editor:
Copyright 2008 by OdeeanRDeathshead
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project