-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathInMemoryBitmap.h
More file actions
185 lines (161 loc) · 4.51 KB
/
InMemoryBitmap.h
File metadata and controls
185 lines (161 loc) · 4.51 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**************************************************************************************
* Project: MapWindow Open Source (MapWinGis ActiveX control)
**************************************************************************************
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at http://www.mozilla.org/mpl/
* See the License for the specific language governing rights and limitations
* under the License.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
**************************************************************************************
* Contributor(s):
* (Open source contributors should list themselves and their modifications here). */
// lsu 21 aug 2011 - created the file
#pragma once
#include "base64.h"
// A wrapper for GDI+ bitmap located in the global heap
class CMemoryBitmap
{
private:
// icon stream (must be open for the lifetime of the Bitmap according to MSDN Bitmap constructor)
IStream* m_stream;
// underlying data for the image (must be released in destructor)
HGLOBAL m_data;
int m_size;
public:
// for tiles
int Provider;
HGLOBAL getData()
{
return m_data;
}
// The actual bitmap
Gdiplus::Bitmap* m_bitmap;
int get_Width()
{
if (m_bitmap)
return m_bitmap->GetWidth();
else
return 0;
}
int get_Height()
{
if (m_bitmap)
return m_bitmap->GetHeight();
else
return 0;
}
int get_Size()
{
return m_size;
}
// **************************************************
// Constructor()
// **************************************************
// Creates an empty instance of CMemoryBitmap class
CMemoryBitmap::CMemoryBitmap()
{
m_bitmap = NULL;
m_stream = NULL;
m_data = NULL;
m_size = 0;
};
// **************************************************
// LoadFromBase64String()
// **************************************************
// Loads data from base64 string
bool LoadFromBase64String(std::string str)
{
if (str.size() != 0)
{
std::string strNew = base64_decode(str);
const char* data = strNew.c_str();
return this->LoadFromRawData(data, (int)strNew.size());
}
return false;
}
// **************************************************
// SerializeToBase64String()
// **************************************************
std::string SerializeToBase64String()
{
if (m_data && m_size > 0)
{
void* hMem = ::GlobalLock(m_data);
if (hMem)
{
char* data = new char[m_size];
memcpy(data, hMem, m_size);
::GlobalUnlock(hMem);
std::string str = base64_encode(reinterpret_cast<unsigned char*>(data), m_size);
delete[] data;
return str;
}
}
return "";
}
// **************************************************
// LoadFromRawData()
// **************************************************
bool LoadFromRawData(const char* data, int size)
{
this->Release();
if (size <= 0)
return false;
m_data = ::GlobalAlloc(GMEM_MOVEABLE, size);
if (m_data)
{
void* hMem = ::GlobalLock(m_data);
if (hMem)
{
memcpy(hMem, data, size);
::GlobalUnlock(hMem);
if (::CreateStreamOnHGlobal(m_data, FALSE, &m_stream) == S_OK)
{
m_bitmap = new Gdiplus::Bitmap(m_stream);
m_size = size;
if (m_bitmap) {
Gdiplus::Status status = m_bitmap->GetLastStatus();
return true;
}
}
}
}
this->Release();
return false;
}
// ************************************************
// Release()
// ************************************************
void Release()
{
if (m_bitmap)
{
delete m_bitmap;
m_bitmap = NULL;
}
if (m_stream)
{
m_stream->Release();
m_stream = NULL;
}
if (m_data)
{
m_data = GlobalFree(m_data);
m_data = NULL;
}
m_size = 0;
}
// Destructor. Underlying stream must be closed and data deleted
virtual ~CMemoryBitmap()
{
this->Release();
}
};