#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, GetDeviceCaps(hDC,LOGPIXELSX), 2540);
m_PictureSize.cy = MulDiv(hmHeight, GetDeviceCaps(hDC,LOGPIXELSY), 2540);
::ReleaseDC(m_hWnd,hDC);
}
else
{
// it's a GIF
m_bIsGIF = TRUE;
m_pGIFLSDescriptor = reinterpret_cast<TGIFLSDescriptor *>
(m_pRawData + sizeof(TGIFHeader));
if (m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_GLOBALCT) == 1)
{
// calculate the globat color table size
m_nGlobalCTSize = static_cast<int>
(3* (1 << (m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_GLOBALCTSIZE)+1)));
// get the background color if GCT is present
unsigned char *pBkClr = m_pRawData + sizeof(TGIFHeader) +
sizeof(TGIFLSDescriptor) + 3*m_pGIFLSDescriptor->m_cBkIndex;
m_clrBackground = RGB(pBkClr[0],pBkClr[1],pBkClr[2]);
};
// store the picture's size
m_PictureSize.cx = m_pGIFLSDescriptor->m_wWidth;
m_PictureSize.cy = m_pGIFLSDescriptor->m_wHeight;
// determine frame count for this picture
UINT nFrameCount=0;
ResetDataPointer();
while (SkipNextGraphicBlock())
nFrameCount++;
#ifdef GIF_TRACING
TRACE(
_T(" -= GIF encountered\n"
"Logical Screen dimensions = %dx%d\n"
"Global color table = %d\n"
"Color depth = %d\n"
"Sort flag = %d\n"
"Size of Global Color Table = %d\n"
"Background color index = %d\n"
"Pixel aspect ratio = %d\n"
"Frame count = %d\n"
"Background color = %06Xh\n\n"
),
m_pGIFLSDescriptor->m_wWidth,
m_pGIFLSDescriptor->m_wHeight,
m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_GLOBALCT),
m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_CRESOLUTION),
m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_SORT),
m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_GLOBALCTSIZE),
m_pGIFLSDescriptor->m_cBkIndex,
m_pGIFLSDescriptor->m_cPixelAspect,
nFrameCount,
m_clrBackground
);
EnumGIFBlocks();
#endif
if (nFrameCount == 0) // it's an empty GIF!
{
m_pRawData = NULL;
GlobalUnlock(hGlobal);
return FALSE;
};
// now check the frame count
// if there's only one frame, no need to animate this GIF
// therefore, treat it like any other pic
if (nFrameCount == 1)
{
// 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,
(LPVOID *)&m_pPicture) != S_OK)
{
pStream->Release();
return FALSE;
};
pStream->Release();
}
else
{
// if, on the contrary, there are several frames
// then store separate frames in an array
TFrame frame;
UINT nBlockLen;
HGLOBAL hFrameData;
UINT nCurFrame = 0;
ResetDataPointer();
while (hFrameData = GetNextGraphicBlock(&nBlockLen,
&frame.m_nDelay, &frame.m_frameSize,
&frame.m_frameOffset, &frame.m_nDisposal) )
{
#ifdef GIF_TRACING
//
// uncomment the following strings if you want
// to write separate frames on disk
//
// CString szName;
// szName.Format(_T("%.4d.gif"),nCurFrame);
// WriteDataOnDisk(szName,hFrameData,nBlockLen);
// nCurFrame++;
#endif // GIF_TRACING
IStream *pStream = NULL;
// delete memory on object's release
if (CreateStreamOnHGlobal(hFrameData,TRUE,&pStream) != S_OK)
{
GlobalFree(hFrameData);
continue;
};
if (OleLoadPicture(pStream,nBlockLen,FALSE,
IID_IPicture,
reinterpret_cast<LPVOID *>(&frame.m_pPicture)) != S_OK)
{
pStream->Release();
continue;
};
pStream->Release();
// everything went well, add this frame
m_arrFrames.push_back(frame);
};
// clean after ourselves
m_pRawData = NULL;
GlobalUnlock(hGlobal);
if (m_arrFrames.empty()) // couldn't load any frames
return FALSE;
};
}; // if (!IsGIF...
return PrepareDC(m_PictureSize.cx,m_PictureSize.cy);
}