123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701 |
- #include "stdafx.h"
- #include "ImageEx.h"
- #include <process.h>
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- ImageEx::ImageEx(LPCTSTR sResourceType, LPCTSTR sResource)
- {
- Initialize();
- m_bStretch = false;
- if (Load(sResourceType, sResource))
- {
- nativeImage = NULL;
-
- lastResult = DllExports::GdipLoadImageFromStreamICM(m_pStream, &nativeImage);
-
- TestForAnimatedGIF();
- }
- }
- ImageEx::ImageEx(const WCHAR* filename, BOOL useEmbeddedColorManagement) : Image(filename, useEmbeddedColorManagement)
- {
- Initialize();
- m_bIsInitialized = true;
- TestForAnimatedGIF();
-
- delete []filename;
- filename = NULL;
- }
- ImageEx::~ImageEx()
- {
- Destroy();
- }
- bool ImageEx::InitAnimation(HWND hWnd, CPoint pt)
- {
- m_hWnd = hWnd;
- m_pt = pt;
- if (!m_bIsInitialized)
- {
- TRACE(_T("GIF not initialized\n"));
- return false;
- }
- if (IsAnimatedGIF())
- {
- if (m_hThread == NULL)
- {
- unsigned int nTID = 0;
- m_hThread = (HANDLE) _beginthreadex( NULL, 0, _ThreadAnimationProc, this, CREATE_SUSPENDED,&nTID);
-
- if (!m_hThread)
- {
- TRACE(_T("Couldn't start a GIF animation thread\n"));
- return true;
- }
- else
- ResumeThread(m_hThread);
- }
- }
- return false;
- }
- bool ImageEx::InitAnimation(HWND hWnd, CRect rect,BOOL bStretch)
- {
- m_hWnd = hWnd;
- m_Rect.X = rect.left;
- m_Rect.Y = rect.top;
- m_Rect.Width = rect.Width();
- m_Rect.Height = rect.Height();
- m_bStretch = bStretch;
- if (!m_bIsInitialized)
- {
- TRACE(_T("GIF not initialized\n"));
- return false;
- }
- if (IsAnimatedGIF())
- {
- if (m_hThread == NULL)
- {
- unsigned int nTID = 0;
- m_hThread = (HANDLE) _beginthreadex( NULL, 0, _ThreadAnimationProc, this, CREATE_SUSPENDED,&nTID);
-
- if (!m_hThread)
- {
- TRACE(_T("Couldn't start a GIF animation thread\n"));
- return true;
- }
- else
- ResumeThread(m_hThread);
- }
- }
- return false;
- }
- bool ImageEx::InitAnimation(HDC hDC, CRect rect,BOOL bStretch)
- {
- m_hDC = hDC;
- m_Rect.X = rect.left;
- m_Rect.Y = rect.top;
- m_Rect.Width = rect.Width();
- m_Rect.Height = rect.Height();
- m_bStretch = bStretch;
- if (!m_bIsInitialized)
- {
- TRACE(_T("GIF not initialized\n"));
- return false;
- };
- if (IsAnimatedGIF())
- {
- if (m_hThread == NULL)
- {
- unsigned int nTID = 0;
- m_hThread = (HANDLE) _beginthreadex( NULL, 0, _ThreadAnimationProc, this, CREATE_SUSPENDED,&nTID);
-
- if (!m_hThread)
- {
- TRACE(_T("Couldn't start a GIF animation thread\n"));
- return true;
- }
- else
- ResumeThread(m_hThread);
- }
- }
- return false;
- }
- bool ImageEx::LoadFromBuffer(BYTE* pBuff, int nSize)
- {
- bool bResult = false;
- HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize);
- if (hGlobal)
- {
- void* pData = GlobalLock(hGlobal);
- if (pData)
- memcpy(pData, pBuff, nSize);
-
- GlobalUnlock(hGlobal);
- if (CreateStreamOnHGlobal(hGlobal, TRUE, &m_pStream) == S_OK)
- bResult = true;
- }
- return bResult;
- }
- bool ImageEx::GetResource(LPCTSTR lpName, LPCTSTR lpType, void* pResource, int& nBufSize)
- {
- HRSRC hResInfo;
- HANDLE hRes;
- LPSTR lpRes = NULL;
- int nLen = 0;
- bool bResult = FALSE;
-
- hResInfo = FindResource(m_hInst , lpName, lpType);
- if (hResInfo == NULL)
- {
- DWORD dwErr = GetLastError();
- return false;
- }
-
- hRes = LoadResource(m_hInst , hResInfo);
- if (hRes == NULL)
- return false;
-
- lpRes = (char*)LockResource(hRes);
- if (lpRes != NULL)
- {
- if (pResource == NULL)
- {
- nBufSize = SizeofResource(m_hInst , hResInfo);
- bResult = true;
- }
- else
- {
- if (nBufSize >= (int)SizeofResource(m_hInst , hResInfo))
- {
- memcpy(pResource, lpRes, nBufSize);
- bResult = true;
- }
- }
- UnlockResource(hRes);
- }
-
- FreeResource(hRes);
- return bResult;
- }
- bool ImageEx::Load(CString sResourceType, CString sResource)
- {
- bool bResult = false;
- BYTE* pBuff = NULL;
- int nSize = 0;
- if (GetResource(sResource.GetBuffer(0), sResourceType.GetBuffer(0), pBuff, nSize))
- {
- if (nSize > 0)
- {
- pBuff = new BYTE[nSize];
- if (GetResource(sResource, sResourceType.GetBuffer(0), pBuff, nSize))
- {
- if (LoadFromBuffer(pBuff, nSize))
- {
- bResult = true;
- }
- }
- delete [] pBuff;
- }
- }
- m_bIsInitialized = bResult;
- return bResult;
- }
- CSize ImageEx::GetSize()
- {
- return CSize(GetWidth(), GetHeight());
- }
- bool ImageEx::TestForAnimatedGIF()
- {
- UINT count = 0;
- count = GetFrameDimensionsCount();
- GUID* pDimensionIDs = new GUID[count];
-
- GetFrameDimensionsList(pDimensionIDs, count);
-
- m_nFrameCount = GetFrameCount(&pDimensionIDs[0]);
-
-
- int nSize = GetPropertyItemSize(PropertyTagFrameDelay);
-
- m_pPropertyItem = (PropertyItem*) malloc(nSize);
- GetPropertyItem(PropertyTagFrameDelay, nSize, m_pPropertyItem);
-
- delete []pDimensionIDs;
- return m_nFrameCount > 1;
- }
- void ImageEx::Initialize()
- {
- m_pStream = NULL;
- m_nFramePosition = 0;
- m_nFrameCount = 0;
- lastResult = InvalidParameter;
- m_hThread = NULL;
- m_bIsInitialized = false;
- m_pPropertyItem = NULL;
-
- #ifdef INDIGO_CTRL_PROJECT
- m_hInst = _Module.GetResourceInstance();
- #else
- m_hInst = AfxGetResourceHandle();
- #endif
- m_bPause = false;
- m_hExitEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
- m_hPause = CreateEvent(NULL,TRUE,TRUE,NULL);
- }
- UINT WINAPI ImageEx::_ThreadAnimationProc(LPVOID pParam)
- {
- ASSERT(pParam);
- ImageEx *pImage = reinterpret_cast<ImageEx *> (pParam);
- pImage->ThreadAnimation();
- return 0;
- }
- void ImageEx::ThreadAnimation()
- {
- m_nFramePosition = 0;
- bool bExit = false;
- while (bExit == false)
- {
- bExit = DrawFrameGIF();
- Sleep(10);
- }
- }
- bool ImageEx::DrawFrameGIF()
- {
- ::WaitForSingleObject(m_hPause, INFINITE);
- GUID pageGuid = FrameDimensionTime;
- #if 1
- long hmWidth = GetWidth();
- long hmHeight = GetHeight();
- HDC hDC = GetDC(m_hWnd);
- if (hDC)
- {
- Graphics graphics(hDC);
- if(m_bStretch)
- {
- graphics.DrawImage(this, m_Rect.X, m_Rect.Y, m_Rect.Width, m_Rect.Height);
- }
- else
- {
- graphics.DrawImage( this, m_Rect.X, m_Rect.Y,0,0,m_Rect.Width,m_Rect.Height,UnitPixel );
- }
-
- ReleaseDC(m_hWnd, m_hDC);
-
- DeleteObject( hDC );
- }
- SelectActiveFrame(&pageGuid, m_nFramePosition++);
-
- if (m_nFramePosition == m_nFrameCount)
- m_nFramePosition = 0;
- long lPause = ((long*) m_pPropertyItem->value)[m_nFramePosition] * 10;
- DWORD dwErr = WaitForSingleObject(m_hExitEvent, lPause);
- return dwErr == WAIT_OBJECT_0;
- #else
- if( m_hDC )
- {
- Graphics graphics(m_hDC);
-
- graphics.DrawImage(this, 10, 10, 20, 20);
- }
- SelectActiveFrame(&pageGuid, m_nFramePosition++);
-
- if (m_nFramePosition == m_nFrameCount)
- m_nFramePosition = 0;
- long lPause = ((long*) m_pPropertyItem->value)[m_nFramePosition] * 10;
- DWORD dwErr = WaitForSingleObject(m_hExitEvent, lPause);
- return dwErr == WAIT_OBJECT_0;
- #endif
- }
- void ImageEx::SetPause(bool bPause)
- {
- if (!IsAnimatedGIF())
- return;
- if (bPause && !m_bPause)
- {
- ::ResetEvent(m_hPause);
- }
- else
- {
- if (m_bPause && !bPause)
- {
- ::SetEvent(m_hPause);
- }
- }
- m_bPause = bPause;
- }
- void ImageEx::Destroy()
- {
-
- if (m_hThread)
- {
-
- SetPause(false);
- SetEvent(m_hExitEvent);
- WaitForSingleObject(m_hThread, INFINITE);
- }
- CloseHandle(m_hThread);
- CloseHandle(m_hExitEvent);
- CloseHandle(m_hPause);
- free(m_pPropertyItem);
- m_pPropertyItem = NULL;
- m_hThread = NULL;
- m_hExitEvent = NULL;
- m_hPause = NULL;
- if (m_pStream)
- m_pStream->Release();
- }
|