PictureEx.cpp 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. #include "stdafx.h"
  2. #include "PictureEx.h"
  3. #include <process.h>
  4. #ifdef _DEBUG
  5. #undef THIS_FILE
  6. static char THIS_FILE[]=__FILE__;
  7. #define new DEBUG_NEW
  8. #endif
  9. //////////////////////////////////////////////////////////////////////
  10. // Nested structures member functions
  11. //////////////////////////////////////////////////////////////////////
  12. inline int CPictureEx::TGIFControlExt::GetPackedValue(enum ControlExtValues Value)
  13. {
  14. int nRet = (int)m_cPacked;
  15. switch (Value)
  16. {
  17. case GCX_PACKED_DISPOSAL:
  18. nRet = (nRet & 28) >> 2;
  19. break;
  20. case GCX_PACKED_USERINPUT:
  21. nRet = (nRet & 2) >> 1;
  22. break;
  23. case GCX_PACKED_TRANSPCOLOR:
  24. nRet &= 1;
  25. break;
  26. };
  27. return nRet;
  28. }
  29. inline int CPictureEx::TGIFLSDescriptor::GetPackedValue(enum LSDPackedValues Value)
  30. {
  31. int nRet = (int)m_cPacked;
  32. switch (Value)
  33. {
  34. case LSD_PACKED_GLOBALCT:
  35. nRet = nRet >> 7;
  36. break;
  37. case LSD_PACKED_CRESOLUTION:
  38. nRet = ((nRet & 0x70) >> 4) + 1;
  39. break;
  40. case LSD_PACKED_SORT:
  41. nRet = (nRet & 8) >> 3;
  42. break;
  43. case LSD_PACKED_GLOBALCTSIZE:
  44. nRet &= 7;
  45. break;
  46. };
  47. return nRet;
  48. }
  49. inline int CPictureEx::TGIFImageDescriptor::GetPackedValue(enum IDPackedValues Value)
  50. {
  51. int nRet = (int)m_cPacked;
  52. switch (Value)
  53. {
  54. case ID_PACKED_LOCALCT:
  55. nRet >>= 7;
  56. break;
  57. case ID_PACKED_INTERLACE:
  58. nRet = ((nRet & 0x40) >> 6);
  59. break;
  60. case ID_PACKED_SORT:
  61. nRet = (nRet & 0x20) >> 5;
  62. break;
  63. case ID_PACKED_LOCALCTSIZE:
  64. nRet &= 7;
  65. break;
  66. };
  67. return nRet;
  68. }
  69. //////////////////////////////////////////////////////////////////////
  70. // Construction/Destruction
  71. //////////////////////////////////////////////////////////////////////
  72. CPictureEx::CPictureEx()
  73. {
  74. // check structures size
  75. ASSERT(sizeof(TGIFImageDescriptor) == 10);
  76. ASSERT(sizeof(TGIFAppExtension) == 14);
  77. ASSERT(sizeof(TGIFPlainTextExt) == 15);
  78. ASSERT(sizeof(TGIFLSDescriptor) == 7);
  79. ASSERT(sizeof(TGIFControlExt) == 8);
  80. ASSERT(sizeof(TGIFCommentExt) == 2);
  81. ASSERT(sizeof(TGIFHeader) == 6);
  82. m_pGIFLSDescriptor = NULL;
  83. m_pGIFHeader = NULL;
  84. m_pPicture = NULL;
  85. m_pRawData = NULL;
  86. m_hThread = NULL;
  87. m_hBitmap = NULL;
  88. m_hMemDC = NULL;
  89. m_hDispMemDC = NULL;
  90. m_hDispMemBM = NULL;
  91. m_hDispOldBM = NULL;
  92. m_bIsInitialized = FALSE;
  93. m_bExitThread = FALSE;
  94. m_bIsPlaying = FALSE;
  95. m_bIsGIF = FALSE;
  96. m_clrBackground = RGB(255,255,255); // white by default
  97. m_nGlobalCTSize = 0;
  98. m_nCurrOffset = 0;
  99. m_nCurrFrame = 0;
  100. m_nDataSize = 0;
  101. m_PictureSize.cx = m_PictureSize.cy = 0;
  102. SetRect(&m_PaintRect,0,0,0,0);
  103. m_hExitEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
  104. }
  105. CPictureEx::~CPictureEx()
  106. {
  107. UnLoad();
  108. CloseHandle(m_hExitEvent);
  109. }
  110. BEGIN_MESSAGE_MAP(CPictureEx, CStatic)
  111. //{{AFX_MSG_MAP(CPictureEx)
  112. ON_WM_DESTROY()
  113. ON_WM_PAINT()
  114. //}}AFX_MSG_MAP
  115. END_MESSAGE_MAP()
  116. BOOL CPictureEx::Load(HGLOBAL hGlobal, DWORD dwSize)
  117. {
  118. IStream *pStream = NULL;
  119. UnLoad();
  120. if (!(m_pRawData = reinterpret_cast<unsigned char*> (GlobalLock(hGlobal))) )
  121. {
  122. TRACE(_T("Load: Error locking memory\n"));
  123. return FALSE;
  124. };
  125. m_nDataSize = dwSize;
  126. m_pGIFHeader = reinterpret_cast<TGIFHeader *> (m_pRawData);
  127. if ((memcmp(&m_pGIFHeader->m_cSignature,"GIF",3) != 0) &&
  128. ((memcmp(&m_pGIFHeader->m_cVersion,"87a",3) != 0) ||
  129. (memcmp(&m_pGIFHeader->m_cVersion,"89a",3) != 0)) )
  130. {
  131. // it's neither GIF87a nor GIF89a
  132. // do the default processing
  133. // clear GIF variables
  134. m_pRawData = NULL;
  135. GlobalUnlock(hGlobal);
  136. // don't delete memory on object's release
  137. if (CreateStreamOnHGlobal(hGlobal,FALSE,&pStream) != S_OK)
  138. return FALSE;
  139. if (OleLoadPicture(pStream,dwSize,FALSE,IID_IPicture,
  140. reinterpret_cast<LPVOID *>(&m_pPicture)) != S_OK)
  141. {
  142. pStream->Release();
  143. return FALSE;
  144. };
  145. pStream->Release();
  146. // store picture's size
  147. long hmWidth;
  148. long hmHeight;
  149. m_pPicture->get_Width(&hmWidth);
  150. m_pPicture->get_Height(&hmHeight);
  151. HDC hDC = ::GetDC(m_hWnd);
  152. m_PictureSize.cx = MulDiv(hmWidth, GetDeviceCaps(hDC,LOGPIXELSX), 2540);
  153. m_PictureSize.cy = MulDiv(hmHeight, GetDeviceCaps(hDC,LOGPIXELSY), 2540);
  154. ::ReleaseDC(m_hWnd,hDC);
  155. }
  156. else
  157. {
  158. // it's a GIF
  159. m_bIsGIF = TRUE;
  160. m_pGIFLSDescriptor = reinterpret_cast<TGIFLSDescriptor *>
  161. (m_pRawData + sizeof(TGIFHeader));
  162. if (m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_GLOBALCT) == 1)
  163. {
  164. // calculate the globat color table size
  165. m_nGlobalCTSize = static_cast<int>
  166. (3* (1 << (m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_GLOBALCTSIZE)+1)));
  167. // get the background color if GCT is present
  168. unsigned char *pBkClr = m_pRawData + sizeof(TGIFHeader) +
  169. sizeof(TGIFLSDescriptor) + 3*m_pGIFLSDescriptor->m_cBkIndex;
  170. m_clrBackground = RGB(pBkClr[0],pBkClr[1],pBkClr[2]);
  171. };
  172. // store the picture's size
  173. m_PictureSize.cx = m_pGIFLSDescriptor->m_wWidth;
  174. m_PictureSize.cy = m_pGIFLSDescriptor->m_wHeight;
  175. // determine frame count for this picture
  176. UINT nFrameCount=0;
  177. ResetDataPointer();
  178. while (SkipNextGraphicBlock())
  179. nFrameCount++;
  180. #ifdef GIF_TRACING
  181. TRACE(
  182. _T(" -= GIF encountered\n"
  183. "Logical Screen dimensions = %dx%d\n"
  184. "Global color table = %d\n"
  185. "Color depth = %d\n"
  186. "Sort flag = %d\n"
  187. "Size of Global Color Table = %d\n"
  188. "Background color index = %d\n"
  189. "Pixel aspect ratio = %d\n"
  190. "Frame count = %d\n"
  191. "Background color = %06Xh\n\n"
  192. ),
  193. m_pGIFLSDescriptor->m_wWidth,
  194. m_pGIFLSDescriptor->m_wHeight,
  195. m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_GLOBALCT),
  196. m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_CRESOLUTION),
  197. m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_SORT),
  198. m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_GLOBALCTSIZE),
  199. m_pGIFLSDescriptor->m_cBkIndex,
  200. m_pGIFLSDescriptor->m_cPixelAspect,
  201. nFrameCount,
  202. m_clrBackground
  203. );
  204. EnumGIFBlocks();
  205. #endif
  206. if (nFrameCount == 0) // it's an empty GIF!
  207. {
  208. m_pRawData = NULL;
  209. GlobalUnlock(hGlobal);
  210. return FALSE;
  211. };
  212. // now check the frame count
  213. // if there's only one frame, no need to animate this GIF
  214. // therefore, treat it like any other pic
  215. if (nFrameCount == 1)
  216. {
  217. // clear GIF variables
  218. m_pRawData = NULL;
  219. GlobalUnlock(hGlobal);
  220. // don't delete memory on object's release
  221. if (CreateStreamOnHGlobal(hGlobal,FALSE,&pStream) != S_OK)
  222. return FALSE;
  223. if (OleLoadPicture(pStream,dwSize,FALSE,IID_IPicture,
  224. (LPVOID *)&m_pPicture) != S_OK)
  225. {
  226. pStream->Release();
  227. return FALSE;
  228. };
  229. pStream->Release();
  230. }
  231. else
  232. {
  233. // if, on the contrary, there are several frames
  234. // then store separate frames in an array
  235. TFrame frame;
  236. UINT nBlockLen;
  237. HGLOBAL hFrameData;
  238. UINT nCurFrame = 0;
  239. ResetDataPointer();
  240. while (hFrameData = GetNextGraphicBlock(&nBlockLen,
  241. &frame.m_nDelay, &frame.m_frameSize,
  242. &frame.m_frameOffset, &frame.m_nDisposal) )
  243. {
  244. #ifdef GIF_TRACING
  245. //////////////////////////////////////////////
  246. // uncomment the following strings if you want
  247. // to write separate frames on disk
  248. //
  249. // CString szName;
  250. // szName.Format(_T("%.4d.gif"),nCurFrame);
  251. // WriteDataOnDisk(szName,hFrameData,nBlockLen);
  252. // nCurFrame++;
  253. #endif // GIF_TRACING
  254. IStream *pStream = NULL;
  255. // delete memory on object's release
  256. if (CreateStreamOnHGlobal(hFrameData,TRUE,&pStream) != S_OK)
  257. {
  258. GlobalFree(hFrameData);
  259. continue;
  260. };
  261. if (OleLoadPicture(pStream,nBlockLen,FALSE,
  262. IID_IPicture,
  263. reinterpret_cast<LPVOID *>(&frame.m_pPicture)) != S_OK)
  264. {
  265. pStream->Release();
  266. continue;
  267. };
  268. pStream->Release();
  269. // everything went well, add this frame
  270. m_arrFrames.push_back(frame);
  271. };
  272. // clean after ourselves
  273. m_pRawData = NULL;
  274. GlobalUnlock(hGlobal);
  275. if (m_arrFrames.empty()) // couldn't load any frames
  276. return FALSE;
  277. };
  278. }; // if (!IsGIF...
  279. return PrepareDC(m_PictureSize.cx,m_PictureSize.cy);
  280. }
  281. void CPictureEx::UnLoad()
  282. {
  283. Stop();
  284. if (m_pPicture)
  285. {
  286. m_pPicture->Release();
  287. m_pPicture = NULL;
  288. };
  289. std::vector<TFrame>::iterator it;
  290. for (it=m_arrFrames.begin();it<m_arrFrames.end();it++)
  291. (*it).m_pPicture->Release();
  292. m_arrFrames.clear();
  293. if (m_hMemDC)
  294. {
  295. SelectObject(m_hMemDC,m_hOldBitmap);
  296. ::DeleteDC(m_hMemDC);
  297. ::DeleteObject(m_hBitmap);
  298. m_hMemDC = NULL;
  299. m_hBitmap = NULL;
  300. };
  301. if (m_hDispMemDC)
  302. {
  303. SelectObject(m_hDispMemDC,m_hDispOldBM);
  304. ::DeleteDC(m_hDispMemDC);
  305. ::DeleteObject(m_hDispMemBM);
  306. m_hDispMemDC = NULL;
  307. m_hDispMemBM = NULL;
  308. };
  309. SetRect(&m_PaintRect,0,0,0,0);
  310. m_pGIFLSDescriptor = NULL;
  311. m_pGIFHeader = NULL;
  312. m_pRawData = NULL;
  313. m_hThread = NULL;
  314. m_bIsInitialized = FALSE;
  315. m_bExitThread = FALSE;
  316. m_bIsGIF = FALSE;
  317. m_clrBackground = RGB(255,255,255); // white by default
  318. m_nGlobalCTSize = 0;
  319. m_nCurrOffset = 0;
  320. m_nCurrFrame = 0;
  321. m_nDataSize = 0;
  322. }
  323. BOOL CPictureEx::Draw()
  324. {
  325. if (!m_bIsInitialized)
  326. {
  327. TRACE(_T("Call one of the CPictureEx::Load() member functions before calling Draw()\n"));
  328. return FALSE;
  329. };
  330. if (IsAnimatedGIF())
  331. {
  332. // the picture needs animation
  333. // we'll start the thread that will handle it for us
  334. unsigned int nDummy;
  335. m_hThread = (HANDLE) _beginthreadex(NULL,0,_ThreadAnimation,this,
  336. CREATE_SUSPENDED,&nDummy);
  337. if (!m_hThread)
  338. {
  339. TRACE(_T("Draw: Couldn't start a GIF animation thread\n"));
  340. return FALSE;
  341. }
  342. else
  343. ResumeThread(m_hThread);
  344. }
  345. else
  346. {
  347. if (m_pPicture)
  348. {
  349. long hmWidth;
  350. long hmHeight;
  351. m_pPicture->get_Width(&hmWidth);
  352. m_pPicture->get_Height(&hmHeight);
  353. if (m_pPicture->Render(m_hMemDC, 0, 0, m_PictureSize.cx, m_PictureSize.cy,
  354. 0, hmHeight, hmWidth, -hmHeight, NULL) == S_OK)
  355. {
  356. Invalidate(FALSE);
  357. return TRUE;
  358. };
  359. };
  360. };
  361. return FALSE;
  362. }
  363. SIZE CPictureEx::GetSize() const
  364. {
  365. return m_PictureSize;
  366. }
  367. BOOL CPictureEx::Load(LPCTSTR szFileName)
  368. {
  369. ASSERT(szFileName);
  370. CFile file;
  371. HGLOBAL hGlobal;
  372. DWORD dwSize;
  373. if (!file.Open(szFileName,
  374. CFile::modeRead |
  375. CFile::shareDenyWrite) )
  376. {
  377. TRACE(_T("Load (file): Error opening file %s\n"),szFileName);
  378. return FALSE;
  379. };
  380. dwSize = file.GetLength();
  381. hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD,dwSize);
  382. if (!hGlobal)
  383. {
  384. TRACE(_T("Load (file): Error allocating memory\n"));
  385. return FALSE;
  386. };
  387. char *pData = reinterpret_cast<char*>(GlobalLock(hGlobal));
  388. if (!pData)
  389. {
  390. TRACE(_T("Load (file): Error locking memory\n"));
  391. GlobalFree(hGlobal);
  392. return FALSE;
  393. };
  394. TRY
  395. {
  396. file.Read(pData,dwSize);
  397. }
  398. CATCH(CFileException, e);
  399. {
  400. TRACE(_T("Load (file): An exception occured while reading the file %s\n"),
  401. szFileName);
  402. GlobalFree(hGlobal);
  403. e->Delete();
  404. file.Close();
  405. return FALSE;
  406. }
  407. END_CATCH
  408. GlobalUnlock(hGlobal);
  409. file.Close();
  410. BOOL bRetValue = Load(hGlobal,dwSize);
  411. GlobalFree(hGlobal);
  412. return bRetValue;
  413. }
  414. BOOL CPictureEx::Load(LPCTSTR szResourceName, LPCTSTR szResourceType)
  415. {
  416. ASSERT(szResourceName);
  417. ASSERT(szResourceType);
  418. HRSRC hPicture = FindResource(AfxGetResourceHandle(),szResourceName,szResourceType);
  419. HGLOBAL hResData;
  420. if (!hPicture || !(hResData = LoadResource(AfxGetResourceHandle(),hPicture)))
  421. {
  422. TRACE(_T("Load (resource): Error loading resource %s\n"),szResourceName);
  423. return FALSE;
  424. };
  425. DWORD dwSize = SizeofResource(AfxGetResourceHandle(),hPicture);
  426. // hResData is not the real HGLOBAL (we can't lock it)
  427. // let's make it real
  428. HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD,dwSize);
  429. if (!hGlobal)
  430. {
  431. TRACE(_T("Load (resource): Error allocating memory\n"));
  432. FreeResource(hResData);
  433. return FALSE;
  434. };
  435. char *pDest = reinterpret_cast<char *> (GlobalLock(hGlobal));
  436. char *pSrc = reinterpret_cast<char *> (LockResource(hResData));
  437. if (!pSrc || !pDest)
  438. {
  439. TRACE(_T("Load (resource): Error locking memory\n"));
  440. GlobalFree(hGlobal);
  441. FreeResource(hResData);
  442. return FALSE;
  443. };
  444. CopyMemory(pDest,pSrc,dwSize);
  445. FreeResource(hResData);
  446. GlobalUnlock(hGlobal);
  447. BOOL bRetValue = Load(hGlobal,dwSize);
  448. GlobalFree(hGlobal);
  449. return bRetValue;
  450. }
  451. void CPictureEx::ResetDataPointer()
  452. {
  453. // skip header and logical screen descriptor
  454. m_nCurrOffset =
  455. sizeof(TGIFHeader)+sizeof(TGIFLSDescriptor)+m_nGlobalCTSize;
  456. }
  457. BOOL CPictureEx::SkipNextGraphicBlock()
  458. {
  459. if (!m_pRawData) return FALSE;
  460. // GIF header + LSDescriptor [+ GCT] [+ Control block] + Data
  461. enum GIFBlockTypes nBlock;
  462. nBlock = GetNextBlock();
  463. while ((nBlock != BLOCK_CONTROLEXT) &&
  464. (nBlock != BLOCK_IMAGE) &&
  465. (nBlock != BLOCK_PLAINTEXT) &&
  466. (nBlock != BLOCK_UNKNOWN) &&
  467. (nBlock != BLOCK_TRAILER) )
  468. {
  469. if (!SkipNextBlock()) return NULL;
  470. nBlock = GetNextBlock();
  471. };
  472. if ((nBlock == BLOCK_UNKNOWN) ||
  473. (nBlock == BLOCK_TRAILER))
  474. return FALSE;
  475. // it's either a control ext.block, an image or a plain text
  476. if (GetNextBlockLen() <= 0) return FALSE;
  477. if (nBlock == BLOCK_CONTROLEXT)
  478. {
  479. if (!SkipNextBlock()) return FALSE;
  480. nBlock = GetNextBlock();
  481. // skip everything until we meet an image block or a plain-text block
  482. while ((nBlock != BLOCK_IMAGE) &&
  483. (nBlock != BLOCK_PLAINTEXT) &&
  484. (nBlock != BLOCK_UNKNOWN) &&
  485. (nBlock != BLOCK_TRAILER) )
  486. {
  487. if (!SkipNextBlock()) return NULL;
  488. nBlock = GetNextBlock();
  489. };
  490. if ((nBlock == BLOCK_UNKNOWN) ||
  491. (nBlock == BLOCK_TRAILER))
  492. return FALSE;
  493. };
  494. // skip the found data block (image or plain-text)
  495. if (!SkipNextBlock()) return FALSE;
  496. return TRUE;
  497. }
  498. UINT CPictureEx::GetSubBlocksLen(UINT nStartingOffset) const
  499. {
  500. UINT nRet = 0;
  501. UINT nCurOffset = nStartingOffset;
  502. while (m_pRawData[nCurOffset] != 0)
  503. {
  504. nRet += m_pRawData[nCurOffset]+1;
  505. nCurOffset += m_pRawData[nCurOffset]+1;
  506. };
  507. return nRet+1;
  508. }
  509. enum CPictureEx::GIFBlockTypes CPictureEx::GetNextBlock() const
  510. {
  511. switch(m_pRawData[m_nCurrOffset])
  512. {
  513. case 0x21:
  514. // extension block
  515. switch(m_pRawData[m_nCurrOffset+1])
  516. {
  517. case 0x01:
  518. // plain text extension
  519. return BLOCK_PLAINTEXT;
  520. break;
  521. case 0xF9:
  522. // graphic control extension
  523. return BLOCK_CONTROLEXT;
  524. break;
  525. case 0xFE:
  526. // comment extension
  527. return BLOCK_COMMEXT;
  528. break;
  529. case 0xFF:
  530. // application extension
  531. return BLOCK_APPEXT;
  532. break;
  533. };
  534. break;
  535. case 0x3B:
  536. // trailer
  537. return BLOCK_TRAILER;
  538. break;
  539. case 0x2C:
  540. // image data
  541. return BLOCK_IMAGE;
  542. break;
  543. };
  544. return BLOCK_UNKNOWN;
  545. }
  546. BOOL CPictureEx::SkipNextBlock()
  547. {
  548. if (!m_pRawData) return FALSE;
  549. int nLen = GetNextBlockLen();
  550. if ((nLen <= 0) || ((m_nCurrOffset+nLen) > m_nDataSize))
  551. return FALSE;
  552. m_nCurrOffset += nLen;
  553. return TRUE;
  554. }
  555. int CPictureEx::GetNextBlockLen() const
  556. {
  557. GIFBlockTypes nBlock = GetNextBlock();
  558. int nTmp;
  559. switch(nBlock)
  560. {
  561. case BLOCK_UNKNOWN:
  562. return -1;
  563. break;
  564. case BLOCK_TRAILER:
  565. return 1;
  566. break;
  567. case BLOCK_APPEXT:
  568. nTmp = GetSubBlocksLen(m_nCurrOffset+sizeof(TGIFAppExtension));
  569. if (nTmp > 0)
  570. return sizeof(TGIFAppExtension)+nTmp;
  571. break;
  572. case BLOCK_COMMEXT:
  573. nTmp = GetSubBlocksLen(m_nCurrOffset+sizeof(TGIFCommentExt));
  574. if (nTmp > 0)
  575. return sizeof(TGIFCommentExt)+nTmp;
  576. break;
  577. case BLOCK_CONTROLEXT:
  578. return sizeof(TGIFControlExt);
  579. break;
  580. case BLOCK_PLAINTEXT:
  581. nTmp = GetSubBlocksLen(m_nCurrOffset+sizeof(TGIFPlainTextExt));
  582. if (nTmp > 0)
  583. return sizeof(TGIFPlainTextExt)+nTmp;
  584. break;
  585. case BLOCK_IMAGE:
  586. TGIFImageDescriptor *pIDescr =
  587. reinterpret_cast<TGIFImageDescriptor *> (&m_pRawData[m_nCurrOffset]);
  588. int nLCTSize = (int)
  589. (pIDescr->GetPackedValue(ID_PACKED_LOCALCT)*3*
  590. (1 << (pIDescr->GetPackedValue(ID_PACKED_LOCALCTSIZE)+1)));
  591. int nTmp = GetSubBlocksLen(m_nCurrOffset+
  592. sizeof(TGIFImageDescriptor) + nLCTSize + 1);
  593. if (nTmp > 0)
  594. return sizeof(TGIFImageDescriptor) + nLCTSize + 1 + nTmp;
  595. break;
  596. };
  597. return 0;
  598. }
  599. UINT WINAPI CPictureEx::_ThreadAnimation(LPVOID pParam)
  600. {
  601. ASSERT(pParam);
  602. CPictureEx *pPic = reinterpret_cast<CPictureEx *> (pParam);
  603. pPic->m_bIsPlaying = TRUE;
  604. pPic->ThreadAnimation();
  605. pPic->m_bIsPlaying = FALSE;
  606. // this thread has finished its work so we close the handle
  607. CloseHandle(pPic->m_hThread);
  608. // and init the handle to zero (so that Stop() doesn't Wait on it)
  609. pPic->m_hThread = 0;
  610. return 0;
  611. }
  612. void CPictureEx::ThreadAnimation()
  613. {
  614. // first, restore background (for stop/draw support)
  615. // disposal method #2
  616. if (m_arrFrames[m_nCurrFrame].m_nDisposal == 2)
  617. {
  618. HBRUSH hBrush = CreateSolidBrush(m_clrBackground);
  619. if (hBrush)
  620. {
  621. RECT rect = {
  622. m_arrFrames[m_nCurrFrame].m_frameOffset.cx,
  623. m_arrFrames[m_nCurrFrame].m_frameOffset.cy,
  624. m_arrFrames[m_nCurrFrame].m_frameOffset.cx + m_arrFrames[m_nCurrFrame].m_frameSize.cx,
  625. m_arrFrames[m_nCurrFrame].m_frameOffset.cy + m_arrFrames[m_nCurrFrame].m_frameSize.cy };
  626. FillRect(m_hMemDC,&rect,hBrush);
  627. DeleteObject(hBrush);
  628. };
  629. }
  630. else
  631. // disposal method #3
  632. if (m_hDispMemDC && (m_arrFrames[m_nCurrFrame].m_nDisposal == 3) )
  633. {
  634. // put it back
  635. BitBlt(m_hMemDC,
  636. m_arrFrames[m_nCurrFrame].m_frameOffset.cx,
  637. m_arrFrames[m_nCurrFrame].m_frameOffset.cy,
  638. m_arrFrames[m_nCurrFrame].m_frameSize.cx,
  639. m_arrFrames[m_nCurrFrame].m_frameSize.cy,
  640. m_hDispMemDC,0,0, SRCCOPY);
  641. // init variables
  642. SelectObject(m_hDispMemDC,m_hDispOldBM);
  643. DeleteDC(m_hDispMemDC); m_hDispMemDC = NULL;
  644. DeleteObject(m_hDispMemBM); m_hDispMemBM = NULL;
  645. };
  646. while (!m_bExitThread)
  647. {
  648. if (m_arrFrames[m_nCurrFrame].m_pPicture)
  649. {
  650. ///////////////////////////////////////////////////////
  651. // Before rendering a frame we should take care of what's
  652. // behind that frame. TFrame::m_nDisposal will be our guide:
  653. // 0 - no disposal specified (do nothing)
  654. // 1 - do not dispose (again, do nothing)
  655. // 2 - restore to background color (m_clrBackground)
  656. // 3 - restore to previous
  657. //////// disposal method #3
  658. if (m_arrFrames[m_nCurrFrame].m_nDisposal == 3)
  659. {
  660. // prepare a memory DC and store the background in it
  661. m_hDispMemDC = CreateCompatibleDC(m_hMemDC);
  662. m_hDispMemBM = CreateCompatibleBitmap(m_hMemDC,
  663. m_arrFrames[m_nCurrFrame].m_frameSize.cx,
  664. m_arrFrames[m_nCurrFrame].m_frameSize.cy);
  665. if (m_hDispMemDC && m_hDispMemBM)
  666. {
  667. m_hDispOldBM = reinterpret_cast<HBITMAP> (SelectObject(m_hDispMemDC,m_hDispMemBM));
  668. BitBlt(m_hDispMemDC,0,0,
  669. m_arrFrames[m_nCurrFrame].m_frameSize.cx,
  670. m_arrFrames[m_nCurrFrame].m_frameSize.cy,
  671. m_hMemDC,
  672. m_arrFrames[m_nCurrFrame].m_frameOffset.cx,
  673. m_arrFrames[m_nCurrFrame].m_frameOffset.cy,
  674. SRCCOPY);
  675. };
  676. };
  677. ///////////////////////
  678. long hmWidth;
  679. long hmHeight;
  680. m_arrFrames[m_nCurrFrame].m_pPicture->get_Width(&hmWidth);
  681. m_arrFrames[m_nCurrFrame].m_pPicture->get_Height(&hmHeight);
  682. if (m_arrFrames[m_nCurrFrame].m_pPicture->Render(m_hMemDC,
  683. m_arrFrames[m_nCurrFrame].m_frameOffset.cx,
  684. m_arrFrames[m_nCurrFrame].m_frameOffset.cy,
  685. m_arrFrames[m_nCurrFrame].m_frameSize.cx,
  686. m_arrFrames[m_nCurrFrame].m_frameSize.cy,
  687. 0, hmHeight, hmWidth, -hmHeight, NULL) == S_OK)
  688. {
  689. Invalidate(FALSE);
  690. };
  691. if (m_bExitThread) break;
  692. // if the delay time is too short (like in old GIFs), wait for 100ms
  693. if (m_arrFrames[m_nCurrFrame].m_nDelay < 5)
  694. WaitForSingleObject(m_hExitEvent, 100);
  695. else
  696. WaitForSingleObject(m_hExitEvent, 10*m_arrFrames[m_nCurrFrame].m_nDelay);
  697. if (m_bExitThread) break;
  698. // disposal method #2
  699. if (m_arrFrames[m_nCurrFrame].m_nDisposal == 2)
  700. {
  701. HBRUSH hBrush = CreateSolidBrush(m_clrBackground);
  702. if (hBrush)
  703. {
  704. RECT rect = {
  705. m_arrFrames[m_nCurrFrame].m_frameOffset.cx,
  706. m_arrFrames[m_nCurrFrame].m_frameOffset.cy,
  707. m_arrFrames[m_nCurrFrame].m_frameOffset.cx + m_arrFrames[m_nCurrFrame].m_frameSize.cx,
  708. m_arrFrames[m_nCurrFrame].m_frameOffset.cy + m_arrFrames[m_nCurrFrame].m_frameSize.cy };
  709. FillRect(m_hMemDC,&rect,hBrush);
  710. DeleteObject(hBrush);
  711. };
  712. }
  713. else
  714. if (m_hDispMemDC && (m_arrFrames[m_nCurrFrame].m_nDisposal == 3) )
  715. {
  716. // put it back
  717. BitBlt(m_hMemDC,
  718. m_arrFrames[m_nCurrFrame].m_frameOffset.cx,
  719. m_arrFrames[m_nCurrFrame].m_frameOffset.cy,
  720. m_arrFrames[m_nCurrFrame].m_frameSize.cx,
  721. m_arrFrames[m_nCurrFrame].m_frameSize.cy,
  722. m_hDispMemDC,0,0, SRCCOPY);
  723. // init variables
  724. SelectObject(m_hDispMemDC,m_hDispOldBM);
  725. DeleteDC(m_hDispMemDC); m_hDispMemDC = NULL;
  726. DeleteObject(m_hDispMemBM); m_hDispMemBM = NULL;
  727. };
  728. };
  729. m_nCurrFrame++;
  730. if (m_nCurrFrame == m_arrFrames.size())
  731. {
  732. m_nCurrFrame
  733. = 0;
  734. // init the screen for the first frame,
  735. HBRUSH hBrush = CreateSolidBrush(m_clrBackground);
  736. if (hBrush)
  737. {
  738. RECT rect = {0,0,m_PictureSize.cx,m_PictureSize.cy};
  739. FillRect(m_hMemDC,&rect,hBrush);
  740. DeleteObject(hBrush);
  741. };
  742. };
  743. };
  744. }
  745. void CPictureEx::Stop()
  746. {
  747. m_bIsPlaying = FALSE;
  748. m_bExitThread = TRUE;
  749. SetEvent(m_hExitEvent);
  750. if (m_hThread)
  751. {
  752. // we'll wait for 5 seconds then continue execution
  753. WaitForSingleObject(m_hThread,5000);
  754. CloseHandle(m_hThread);
  755. m_hThread = NULL;
  756. }
  757. // make it possible to Draw() again
  758. ResetEvent(m_hExitEvent);
  759. m_bExitThread = FALSE;
  760. }
  761. HGLOBAL CPictureEx::GetNextGraphicBlock(UINT *pBlockLen,
  762. UINT *pDelay, SIZE *pBlockSize, SIZE *pBlockOffset,
  763. UINT *pDisposal)
  764. {
  765. if (!m_pRawData) return NULL;
  766. // GIF header + LSDescriptor [+ GCT] [+ Control block] + Data
  767. *pDisposal = 0;
  768. enum GIFBlockTypes nBlock;
  769. nBlock = GetNextBlock();
  770. while (
  771. (nBlock != BLOCK_CONTROLEXT) &&
  772. (nBlock != BLOCK_IMAGE) &&
  773. (nBlock != BLOCK_PLAINTEXT) &&
  774. (nBlock != BLOCK_UNKNOWN) &&
  775. (nBlock != BLOCK_TRAILER)
  776. )
  777. {
  778. if (!SkipNextBlock()) return NULL;
  779. nBlock = GetNextBlock();
  780. };
  781. if ((nBlock == BLOCK_UNKNOWN) ||
  782. (nBlock == BLOCK_TRAILER))
  783. return NULL;
  784. // it's either a control ext.block, an image or a plain text
  785. int nStart = m_nCurrOffset;
  786. int nBlockLen = GetNextBlockLen();
  787. if (nBlockLen <= 0) return NULL;
  788. if (nBlock == BLOCK_CONTROLEXT)
  789. {
  790. // get the following data
  791. TGIFControlExt *pControl =
  792. reinterpret_cast<TGIFControlExt *> (&m_pRawData[m_nCurrOffset]);
  793. // store delay time
  794. *pDelay = pControl->m_wDelayTime;
  795. // store disposal method
  796. *pDisposal = pControl->GetPackedValue(GCX_PACKED_DISPOSAL);
  797. if (!SkipNextBlock()) return NULL;
  798. nBlock = GetNextBlock();
  799. // skip everything until we find data to display
  800. // (image block or plain-text block)
  801. while (
  802. (nBlock != BLOCK_IMAGE) &&
  803. (nBlock != BLOCK_PLAINTEXT) &&
  804. (nBlock != BLOCK_UNKNOWN) &&
  805. (nBlock != BLOCK_TRAILER)
  806. )
  807. {
  808. if (!SkipNextBlock()) return NULL;
  809. nBlock = GetNextBlock();
  810. nBlockLen += GetNextBlockLen();
  811. };
  812. if ((nBlock == BLOCK_UNKNOWN) || (nBlock == BLOCK_TRAILER))
  813. return NULL;
  814. nBlockLen += GetNextBlockLen();
  815. }
  816. else
  817. *pDelay = -1; // to indicate that there was no delay value
  818. if (nBlock == BLOCK_IMAGE)
  819. {
  820. // store size and offsets
  821. TGIFImageDescriptor *pImage =
  822. reinterpret_cast<TGIFImageDescriptor *> (&m_pRawData[m_nCurrOffset]);
  823. pBlockSize->cx = pImage->m_wWidth;
  824. pBlockSize->cy = pImage->m_wHeight;
  825. pBlockOffset->cx = pImage->m_wLeftPos;
  826. pBlockOffset->cy = pImage->m_wTopPos;
  827. };
  828. if (!SkipNextBlock()) return NULL;
  829. HGLOBAL hGlobal = GlobalAlloc(GMEM_FIXED,
  830. sizeof(TGIFHeader) +
  831. sizeof(TGIFLSDescriptor) +
  832. m_nGlobalCTSize +
  833. nBlockLen +
  834. 1); // for the trailer
  835. if (!hGlobal) return NULL;
  836. int nOffset = 0;
  837. // GMEM_FIXED means we get a pointer
  838. unsigned char *pGlobal = reinterpret_cast<unsigned char *> (hGlobal);
  839. CopyMemory(pGlobal,m_pRawData,
  840. sizeof(TGIFHeader)+sizeof(TGIFLSDescriptor)+m_nGlobalCTSize);
  841. nOffset += sizeof(TGIFHeader)+sizeof(TGIFLSDescriptor)+m_nGlobalCTSize;
  842. CopyMemory(pGlobal + nOffset,&m_pRawData[nStart], nBlockLen);
  843. nOffset += nBlockLen;
  844. pGlobal[nOffset] = 0x3B; // trailer
  845. nOffset++;
  846. *pBlockLen = nOffset;
  847. return hGlobal;
  848. }
  849. BOOL CPictureEx::IsGIF() const
  850. {
  851. return m_bIsGIF;
  852. }
  853. BOOL CPictureEx::IsAnimatedGIF() const
  854. {
  855. return (m_bIsGIF && (m_arrFrames.size() > 1));
  856. }
  857. BOOL CPictureEx::IsPlaying() const
  858. {
  859. return m_bIsPlaying;
  860. }
  861. int CPictureEx::GetFrameCount() const
  862. {
  863. if (!IsAnimatedGIF())
  864. return 0;
  865. return m_arrFrames.size();
  866. }
  867. COLORREF CPictureEx::GetBkColor() const
  868. {
  869. return m_clrBackground;
  870. }
  871. void CPictureEx::OnPaint()
  872. {
  873. CPaintDC dc(this); // device context for painting
  874. LONG nPaintWidth = m_PaintRect.right-m_PaintRect.left;
  875. if (nPaintWidth > 0)
  876. {
  877. LONG nPaintHeight = m_PaintRect.bottom - m_PaintRect.top;
  878. ::BitBlt(dc.m_hDC, 0, 0, nPaintWidth, nPaintHeight,
  879. m_hMemDC, m_PaintRect.left, m_PaintRect.top, SRCCOPY);
  880. }
  881. else
  882. {
  883. ::BitBlt(dc.m_hDC, 0, 0, m_PictureSize.cx, m_PictureSize.cy,
  884. m_hMemDC, 0, 0, SRCCOPY);
  885. };
  886. }
  887. BOOL CPictureEx::PrepareDC(int nWidth, int nHeight)
  888. {
  889. SetWindowPos(NULL,0,0,nWidth,nHeight,SWP_NOMOVE | SWP_NOZORDER);
  890. HDC hWinDC = ::GetDC(m_hWnd);
  891. if (!hWinDC) return FALSE;
  892. m_hMemDC = CreateCompatibleDC(hWinDC);
  893. if (!m_hMemDC)
  894. {
  895. ::ReleaseDC(m_hWnd,hWinDC);
  896. return FALSE;
  897. };
  898. m_hBitmap = CreateCompatibleBitmap(hWinDC,nWidth,nHeight);
  899. if (!m_hBitmap)
  900. {
  901. ::ReleaseDC(m_hWnd,hWinDC);
  902. ::DeleteDC(m_hMemDC);
  903. return FALSE;
  904. };
  905. m_hOldBitmap = reinterpret_cast<HBITMAP>
  906. (SelectObject(m_hMemDC,m_hBitmap));
  907. // fill the background
  908. m_clrBackground = GetSysColor(COLOR_3DFACE);
  909. RECT rect = {0,0,nWidth,nHeight};
  910. FillRect(m_hMemDC,&rect,(HBRUSH)(COLOR_WINDOW));
  911. ::ReleaseDC(m_hWnd,hWinDC);
  912. m_bIsInitialized = TRUE;
  913. return TRUE;
  914. }
  915. void CPictureEx::OnDestroy()
  916. {
  917. Stop();
  918. CStatic::OnDestroy();
  919. }
  920. void CPictureEx::SetBkColor(COLORREF clr)
  921. {
  922. if (!m_bIsInitialized) return;
  923. m_clrBackground = clr;
  924. HBRUSH hBrush = CreateSolidBrush(clr);
  925. if (hBrush)
  926. {
  927. RECT rect = {0,0,m_PictureSize.cx,m_PictureSize.cy};
  928. FillRect(m_hMemDC,&rect,hBrush);
  929. DeleteObject(hBrush);
  930. };
  931. }
  932. #ifdef GIF_TRACING
  933. void CPictureEx::WriteDataOnDisk(CString szFileName, HGLOBAL hData, DWORD dwSize)
  934. {
  935. CFile file;
  936. if (!file.Open(szFileName,
  937. CFile::modeCreate |
  938. CFile::modeWrite |
  939. CFile::shareDenyNone))
  940. {
  941. TRACE(_T("WriteData: Error creating file %s\n"),szFileName);
  942. return;
  943. };
  944. char *pData = reinterpret_cast<char *> (GlobalLock(hData));
  945. if (!pData)
  946. {
  947. TRACE(_T("WriteData: Error locking memory\n"));
  948. return;
  949. };
  950. TRY
  951. {
  952. file.Write(pData,dwSize);
  953. }
  954. CATCH(CFileException, e);
  955. {
  956. TRACE(_T("WriteData: An exception occured while writing to the file %s\n"),
  957. szFileName);
  958. e->Delete();
  959. GlobalUnlock(hData);
  960. file.Close();
  961. return;
  962. }
  963. END_CATCH
  964. GlobalUnlock(hData);
  965. file.Close();
  966. }
  967. void CPictureEx::EnumGIFBlocks()
  968. {
  969. enum GIFBlockTypes nBlock;
  970. ResetDataPointer();
  971. while(m_nCurrOffset < m_nDataSize)
  972. {
  973. nBlock = GetNextBlock();
  974. switch(nBlock)
  975. {
  976. case BLOCK_UNKNOWN:
  977. TRACE(_T("- Unknown block\n"));
  978. return;
  979. break;
  980. case BLOCK_TRAILER:
  981. TRACE(_T("- Trailer block\n"));
  982. break;
  983. case BLOCK_APPEXT:
  984. TRACE(_T("- Application extension block\n"));
  985. break;
  986. case BLOCK_COMMEXT:
  987. TRACE(_T("- Comment extension block\n"));
  988. break;
  989. case BLOCK_CONTROLEXT:
  990. {
  991. TGIFControlExt *pControl =
  992. reinterpret_cast<TGIFControlExt *> (&m_pRawData[m_nCurrOffset]);
  993. TRACE(_T("- Graphic control extension block (delay %d, disposal %d)\n"),
  994. pControl->m_wDelayTime, pControl->GetPackedValue(GCX_PACKED_DISPOSAL));
  995. };
  996. break;
  997. case BLOCK_PLAINTEXT:
  998. TRACE(_T("- Plain text extension block\n"));
  999. break;
  1000. case BLOCK_IMAGE:
  1001. TGIFImageDescriptor *pIDescr =
  1002. reinterpret_cast<TGIFImageDescriptor *> (&m_pRawData[m_nCurrOffset]);
  1003. TRACE(_T("- Image data block (%dx%d %d,%d)\n"),
  1004. pIDescr->m_wWidth,
  1005. pIDescr->m_wHeight,
  1006. pIDescr->m_wLeftPos,
  1007. pIDescr->m_wTopPos);
  1008. break;
  1009. };
  1010. SkipNextBlock();
  1011. };
  1012. TRACE(_T("\n"));
  1013. }
  1014. #endif // GIF_TRACING
  1015. BOOL CPictureEx::SetPaintRect(const RECT *lpRect)
  1016. {
  1017. return CopyRect(&m_PaintRect, lpRect);
  1018. }
  1019. BOOL CPictureEx::GetPaintRect(RECT *lpRect)
  1020. {
  1021. return CopyRect(lpRect, &m_PaintRect);
  1022. }