pictureex.cpp

来自「仿造QQ聊天界面 超级相向 第一次作品 不足之处见谅」· C++ 代码 · 共 1,294 行 · 第 1/3 页

CPP
1,294
字号
//////////////////////////////////////////////////////////////////////
// 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, 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);
}

void CPictureEx::UnLoad()
{
 Stop();
 if (m_pPicture)
 {
  m_pPicture->Release();
  m_pPicture = NULL;
 };
 
 std::vector<TFrame>::iterator it;
 for (it=m_arrFrames.begin();it<m_arrFrames.end();it++)
  (*it).m_pPicture->Release();
 m_arrFrames.clear();

 if (m_hMemDC)
 {
  SelectObject(m_hMemDC,m_hOldBitmap);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?