//////////////////////////////////////////////////////////////////////
// PictureEx.cpp: implementation of the CPictureEx class.
//
// Picture displaying control with support for the following formats:
// GIF (including animated GIF87a and GIF89a), JPEG, BMP, WMF, ICO, CUR
//
// Written by Oleg Bykov (oleg_bykoff@rsdn.ru)
// Copyright (c) 2001
//
// To use CPictureEx, follow these steps:
// - place a static control on your dialog (either a text or a bitmap)
// - change its identifier to something else (e.g. IDC_MYPIC)
// - associate a CStatic with it using ClassWizard
// - in your dialog's header file replace CStatic with CPictureEx
// (don't forget to #include "PictureEx.h" and add
// PictureEx.h and PictureEx.cpp to your project)
// - call one of the overloaded CPictureEx::Load() functions somewhere
// (OnInitDialog is a good place to start)
// - if the preceding Load() succeeded call Draw()
//
// You can also add the control by defining a member variable of type
// CPictureEx, calling CPictureEx::Create (derived from CStatic), then
// CPictureEx::Load and CPictureEx::Draw.
//
// By default, the control initializes its background to COLOR_3DFACE
// (see CPictureEx::PrepareDC()). You can change the background by
// calling CPictureEx::SetBkColor(COLORREF) after CPictureEx::Load().
//
// I decided to leave in the class the functions to write separate frames from
// animated GIF to disk. If you want to use them, uncomment #define GIF_TRACING
// and an appropriate section in CPictureEx::Load(HGLOBAL, DWORD). These functions
// won't be compiled and linked to your project unless you uncomment #define GIF_TRACING,
// so you don't have to worry.
//
// Warning: this code hasn't been subject to a heavy testing, so
// use it on your own risk. The author accepts no liability for the
// possible damage caused by this code.
//
// Version 1.0 7 Aug 2001
// Initial release
//
// Version 1.1 6 Sept 2001
// ATL version of the class
//
// Version 1.2 14 Oct 2001
// - Fixed a problem with loading GIFs from resources
// in MFC-version of the class for multi-modules apps.
// Thanks to Ruben Avila-Carretero for finding this out.
//
// - Got rid of waitable timer in ThreadAnimation()
// Now CPictureEx[Wnd] works in Win95 too.
// Thanks to Alex Egiazarov and Wayne King for the idea.
//
// - Fixed a visual glitch of using SetBkColor.
// Thanks to Kwangjin Lee for finding this out.
//
// Version 1.3 10 Nov 2001
// - Fixed a DC leak. One DC leaked per each UnLoad()
// (forgot to put a ReleaseDC() in the end of
// CPictureExWnd::PrepareDC() function).
//
// - Now it is possible to set a clipping rectangle using
// CPictureEx[Wnd]::SetPaintRect(const LPRECT) function.
// The LPRECT parameter tells the class what portion of
// a picture should it display. If the clipping rect is
// not set, the whole picture is shown.
// Thanks to Fabrice Rodriguez for the idea.
//
// - Added support for Stop/Draw. Now you can Stop() an
// animated GIF, then Draw() it again, it will continue
// animation from the frame it was stopped on. You can
// also know if a GIF is currently playing with the
// IsPlaying() function.
//
// - Got rid of math.h and made m_bExitThread volatile.
// Thanks to Piotr Sawicki for the suggestion.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "PictureEx.h"
#include <process.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Nested structures member functions
//////////////////////////////////////////////////////////////////////
inline int CPictureEx::TGIFControlExt::GetPackedValue(enum ControlExtValues Value)
{
int nRet = (int)m_cPacked;
switch (Value)
{
case GCX_PACKED_DISPOSAL:
nRet = (nRet & 28) >> 2;
break;
case GCX_PACKED_USERINPUT:
nRet = (nRet & 2) >> 1;
break;
case GCX_PACKED_TRANSPCOLOR:
nRet &= 1;
break;
};
return nRet;
}
inline int CPictureEx::TGIFLSDescriptor::GetPackedValue(enum LSDPackedValues Value)
{
int nRet = (int)m_cPacked;
switch (Value)
{
case LSD_PACKED_GLOBALCT:
nRet = nRet >> 7;
break;
case LSD_PACKED_CRESOLUTION:
nRet = ((nRet & 0x70) >> 4) + 1;
break;
case LSD_PACKED_SORT:
nRet = (nRet & 8) >> 3;
break;
case LSD_PACKED_GLOBALCTSIZE:
nRet &= 7;
break;
};
return nRet;
}
inline int CPictureEx::TGIFImageDescriptor::GetPackedValue(enum IDPackedValues Value)
{
int nRet = (int)m_cPacked;
switch (Value)
{
case ID_PACKED_LOCALCT:
nRet >>= 7;
break;
case ID_PACKED_INTERLACE:
nRet = ((nRet & 0x40) >> 6);
break;
case ID_PACKED_SORT:
nRet = (nRet & 0x20) >> 5;
break;
case ID_PACKED_LOCALCTSIZE:
nRet &= 7;
break;
};
return nRet;
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CPictureEx::CPictureEx()
{
// check structures size
ASSERT(sizeof(TGIFImageDescriptor) == 10);
ASSERT(sizeof(TGIFAppExtension) == 14);
ASSERT(sizeof(TGIFPlainTextExt) == 15);
ASSERT(sizeof(TGIFLSDescriptor) == 7);
ASSERT(sizeof(TGIFControlExt) == 8);
ASSERT(sizeof(TGIFCommentExt) == 2);
ASSERT(sizeof(TGIFHeader) == 6);
m_pGIFLSDescriptor = NULL;
m_pGIFHeader = NULL;
m_pPicture = NULL;
m_pRawData = NULL;
m_hThread = NULL;
m_hBitmap = NULL;
m_hMemDC = NULL;
m_hDispMemDC = NULL;
m_hDispMemBM = NULL;
m_hDispOldBM = NULL;
m_bIsInitialized = FALSE;
m_bExitThread = FALSE;
m_bIsPlaying = FALSE;
m_bIsGIF = FALSE;
m_clrBackground = RGB(255,255,255); // white by default
m_nGlobalCTSize = 0;
m_nCurrOffset = 0;
m_nCurrFrame = 0;
m_nDataSize = 0;
m_PictureSize.cx = m_PictureSize.cy = 0;
SetRect(&m_PaintRect,0,0,0,0);
m_hExitEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
}
CPictureEx::~CPictureEx()
{
UnLoad();
CloseHandle(m_hExitEvent);
}
BEGIN_MESSAGE_MAP(CPictureEx, CStatic)
//{{AFX_MSG_MAP(CPictureEx)
ON_WM_DESTROY()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CPictureEx::Load(HGLOBAL hGlobal, DWORD dwSize)
{
IStream *pStream = NULL;
UnLoad();
if (!(m_pRawData = reinterpret_cast<unsigned char*> (GlobalLock(hGlobal))) )
{
TRACE(_T("Load: Error locking memory\n"));
return FALSE;
};
m_nDataSize = dwSize;
m_pGIFHeader = reinterpret_cast<TGIFHeader *> (m_pRawData);
if ((memcmp(&m_pGIFHeader->m_cSignature,"GIF",3) != 0) &&
((memcmp(&m_pGIFHeader->m_cVersion,"87a",3) != 0) ||
(memcmp(&m_pGIFHeader->m_cVersion,"89a",3) != 0)) )
{
// it's neither GIF87a nor GIF89a
// do the default processing
// clear GIF variables
m_pRawData = NULL;
GlobalUnlock(hGlobal);
// don't delete memory on object's release
if (CreateStreamOnHGlobal(hGlobal,FALSE,&pStream) != S_OK)
return FALSE;
if (OleLoadPicture(pStream,dwSize,FALSE,IID_IPicture,
reinterpret_cast<LPVOID *>(&m_pPicture)) != S_OK)
{
pStream->Release();
return FALSE;
};
pStream->Release();
// store picture's size
long hmWidth;
long hmHeight;
m_pPicture->get_Width(&hmWidth);
m_pPicture->get_Height(&hmHeight);
HDC hDC = ::GetDC(m_hWnd);
m_PictureSize.cx = MulDiv(hmWidth, GetDevice