Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MPCPngImage.cpp « mpc-hc « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3bfbd73d5b4ec2e01f5af6431e045ba8e92ab979 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "stdafx.h"
#include "MPCPngImage.h"


//////////////////////////////////////////////////////////////////////
// CPngImage

CImage* CMPCPngImage::m_pImage;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMPCPngImage::CMPCPngImage()
{
}

CMPCPngImage::~CMPCPngImage()
{
}

//////////////////////////////////////////////////////////////////////
// Operations
//////////////////////////////////////////////////////////////////////

BOOL CMPCPngImage::Load(UINT uiResID, HINSTANCE hinstRes)
{
    return Load(MAKEINTRESOURCE(uiResID), hinstRes);
}

BOOL CMPCPngImage::Load(LPCTSTR lpszResourceName, HINSTANCE hinstRes)
{
    if (hinstRes == nullptr) {
        hinstRes = AfxFindResourceHandle(lpszResourceName, _T("PNG"));
    }

    HRSRC hRsrc = ::FindResource(hinstRes, lpszResourceName, _T("PNG"));
    if (hRsrc == nullptr) {
        // Fallback to the instance handle
        hinstRes = AfxGetInstanceHandle();
        hRsrc = ::FindResource(hinstRes, lpszResourceName, _T("PNG"));
        if (hRsrc == nullptr) {
            return FALSE;
        }
    }

    HGLOBAL hGlobal = LoadResource(hinstRes, hRsrc);
    if (hGlobal == nullptr) {
        return FALSE;
    }

    LPVOID lpBuffer = ::LockResource(hGlobal);
    if (lpBuffer == nullptr) {
        FreeResource(hGlobal);
        return FALSE;
    }

    BOOL bRes = LoadFromBuffer((LPBYTE) lpBuffer, (UINT) ::SizeofResource(hinstRes, hRsrc));

    UnlockResource(hGlobal);
    FreeResource(hGlobal);

    return bRes;
}
//*******************************************************************************
BOOL CMPCPngImage::LoadFromFile(LPCTSTR lpszPath)
{
    BOOL bRes = FALSE;

    if (m_pImage == nullptr) {
        m_pImage = DEBUG_NEW CImage;
        ENSURE(m_pImage != nullptr);
    }

    if (m_pImage->Load(lpszPath) == S_OK) {
        bRes = Attach(m_pImage->Detach());
    }

    return bRes;
}
//*******************************************************************************
BOOL CMPCPngImage::LoadFromBuffer(const LPBYTE lpBuffer, UINT uiSize)
{
    ASSERT(lpBuffer != nullptr);

    HGLOBAL hRes = ::GlobalAlloc(GMEM_MOVEABLE, uiSize);
    if (hRes == nullptr) {
        return FALSE;
    }

    IStream* pStream = nullptr;
    LPVOID lpResBuffer = ::GlobalLock(hRes);
    ASSERT(lpResBuffer != nullptr);

    memcpy(lpResBuffer, lpBuffer, uiSize);

    HRESULT hResult = ::CreateStreamOnHGlobal(hRes, FALSE, &pStream);

    if (hResult != S_OK) {
        return FALSE;
    }

    if (m_pImage == nullptr) {
        m_pImage = DEBUG_NEW CImage;
        ENSURE(m_pImage != nullptr);
    }

    m_pImage->Load(pStream);
    pStream->Release();

    BOOL bRes = Attach(m_pImage->Detach());

    return bRes;
}