-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGifView.cpp
258 lines (244 loc) · 7.57 KB
/
GifView.cpp
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// GifView.cpp : 实现文件
//
#include "stdafx.h"
#include "GifView.h"
//
#include "gif_lib.h"
#include <functional>
// CGifView
struct CGifView::Frame {
const SavedImage* img;
const ColorMapObject* color_map;
GraphicsControlBlock gcb, last_gcb;
//
static Frame Parse(const SavedImage* img, const ColorMapObject* default_color_map, \
const GraphicsControlBlock& gcb, const GraphicsControlBlock& last_gcb)
{
Frame frame;
frame.gcb = gcb;
frame.color_map = img->ImageDesc.ColorMap ? img->ImageDesc.ColorMap : default_color_map;
frame.img = img;
frame.last_gcb = last_gcb;
return frame;
}
};
IMPLEMENT_DYNAMIC(CGifView, CStatic)
CGifView::CGifView()
: mGifFile(nullptr)
, mOldCanvas(nullptr)
, mCanvas(nullptr)
, mMemDcBuffer(nullptr)
, mShouldStop(true)
, mBackgroundColor(GetSysColor(COLOR_MENU))
, mAutoScale(true)
, mCurrentFrameIdx(0)
, mCurrentStatus(eStopped)
{
}
CGifView::~CGifView()
{
}
BEGIN_MESSAGE_MAP(CGifView, CStatic)
ON_WM_DESTROY()
ON_WM_PAINT()
END_MESSAGE_MAP()
// CGifView 消息处理程序
void CGifView::OnDestroy()
{
CStatic::OnDestroy();
//
Stop();
//
ReleaseMemDc();
//
if (mGifFile) {
DGifCloseFile((GifFileType*)mGifFile, nullptr);
}
}
void CGifView::ReleaseMemDc()
{
if (mOldCanvas) {
mMemDc.SelectObject(mOldCanvas);
mOldCanvas = nullptr;
}
mMemDc.DeleteDC();
if (mCanvas) {
mCanvas->DeleteObject();
mCanvas = nullptr;
}
}
void CGifView::BuildMemDc(int sx, int sy)
{
// release old one
ReleaseMemDc();
//
CDC *pDC = GetDC();
mMemDc.CreateCompatibleDC(pDC);
BITMAPINFO bmi;
memset(&bmi, 0, sizeof(bmi));
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biSize = sizeof(bmi);
bmi.bmiHeader.biWidth = sx;
bmi.bmiHeader.biHeight = -sy; // flip
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biSizeImage = 4 * sx*sy;
mCanvas = CBitmap::FromHandle(
CreateDIBSection(mMemDc.GetSafeHdc(), &bmi, DIB_RGB_COLORS, &mMemDcBuffer, 0, 0));
mOldCanvas = mMemDc.SelectObject(mCanvas);
ReleaseDC(pDC);
}
bool CGifView::Load(const CString& filename)
{
// If there's a playing movie, destroy it.
Stop();
if (mGifFile) {
DGifCloseFile((GifFileType*)mGifFile, nullptr);
mGifFile = nullptr;
}
USES_CONVERSION;
mGifFile = DGifOpenFileName(T2A(filename), nullptr);
if (mGifFile) {
GifFileType* gif = (GifFileType*)mGifFile;
DGifSlurp(gif);
// Now, build the compatible dc , i.e the virtual canvas.
BuildMemDc(gif->SWidth, gif->SHeight);
}
return mGifFile != nullptr;
}
static void thread_loop(std::function<bool()> func)
{
while (1) {
if (!func()) {
break;
}
}
}
void CGifView::Stop()
{
mShouldStop = true;
if (mThread.joinable()) {
mThread.join();
}
mCurrentFrameIdx = 0;
mCurrentStatus = eStopped;
}
void CGifView::Pause()
{
if (mCurrentStatus == ePlaying) {
mCurrentStatus = ePaused;
mShouldStop = true;
if (mThread.joinable()) {
mThread.join();
}
}
}
void CGifView::Play()
{
if (mCurrentStatus == ePlaying) return;
if (mGifFile) {
mShouldStop = false; // before start the thread, set the flag to false.
mThread = std::thread(thread_loop, [=]()->bool {
auto gif = (GifFileType*)mGifFile;
mCurrentFrameIdx = mCurrentFrameIdx % gif->ImageCount;
try {
GraphicsControlBlock gcb;
if (DGifSavedExtensionToGCB(gif, mCurrentFrameIdx, &gcb) >= 0) {
GraphicsControlBlock last_gcb = gcb;
if (mCurrentFrameIdx > 0) {
DGifSavedExtensionToGCB(gif, mCurrentFrameIdx - 1, &last_gcb);
}
auto& frame = Frame::Parse(&gif->SavedImages[mCurrentFrameIdx], gif->SColorMap, gcb, last_gcb);
// MemDc is shared
mMutex.lock();
RenderToMemDc(frame);
mMutex.unlock();
// Refresh
Invalidate();
//
mCurrentFrameIdx++;
//
for (int k = 0; k < gcb.DelayTime && !mShouldStop; ++k) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
//
return true && !mShouldStop;
}
return false;
} catch (const std::exception& e) {
_CRT_UNUSED(e);
return false;
}
});
mCurrentStatus = ePlaying;
}
}
#define SET_PIXEL(i,j,c) do { \
if (i + y < gif->SHeight && j + x < gif->SWidth) { \
dc_buffer[bitmap.bmWidthBytes*(i + y) + (j + x)*pb + 0] = c.Blue; \
dc_buffer[bitmap.bmWidthBytes*(i + y) + (j + x)*pb + 1] = c.Green; \
dc_buffer[bitmap.bmWidthBytes*(i + y) + (j + x)*pb + 2] = c.Red; \
} \
} while (0)
void CGifView::RenderToMemDc(const Frame& frame)
{
auto gif = (GifFileType*)mGifFile;
// 根据上一个的显示状态来绘制当前帧.
switch (frame.last_gcb.DisposalMode) {
case DISPOSE_PREVIOUS:
break;
case DISPOSE_BACKGROUND:
mMemDc.FillSolidRect(0, 0, gif->SWidth, gif->SHeight, mBackgroundColor);
case DISPOSAL_UNSPECIFIED:
case DISPOSE_DO_NOT:if (1) {
BITMAP bitmap;
mCanvas->GetBitmap(&bitmap);
int pb = bitmap.bmBitsPixel / 8;
if (pb >= 3) {
const auto& color_map = frame.color_map;
const auto& gcb = frame.gcb;
const SavedImage* img = frame.img;
auto& x = img->ImageDesc.Left;
auto& y = img->ImageDesc.Top;
auto& w = img->ImageDesc.Width;
auto& h = img->ImageDesc.Height;
uint8_t* dc_buffer = (uint8_t*)mMemDcBuffer;
if (dc_buffer) {
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
int idx = img->RasterBits[w*i + j];
if (idx != gcb.TransparentColor) {
SET_PIXEL(i, j, color_map->Colors[idx]);
}
}
}
}
}
} break;
}
}
void CGifView::OnPaint()
{
CPaintDC dc(this);
// copy to dc.
CRect rc; GetClientRect(&rc);
auto gif = (GifFileType*)mGifFile;
// 当图像过大时, 自动缩放, 保持比例.
if ((rc.Width() < gif->SWidth || rc.Height() < gif->SHeight) && mAutoScale) {
double sx = (double)rc.Width() / gif->SWidth;
double sy = (double)rc.Height() / gif->SHeight;
double ss = min(sx, sy);
int w = (int)round(gif->SWidth * ss);
int h = (int)round(gif->SHeight * ss);
int x = (rc.Width() - w) / 2, y = (rc.Height() - h) / 2;
dc.SetStretchBltMode(COLORONCOLOR);
mMutex.lock();
dc.StretchBlt(x, y, w, h, &mMemDc, 0, 0, gif->SWidth, gif->SHeight, SRCCOPY);
mMutex.unlock();
} else {
int x = (rc.Width() - gif->SWidth) / 2;
int y = (rc.Height() - gif->SHeight) / 2;
mMutex.lock();
dc.BitBlt(x, y, gif->SWidth, gif->SHeight, &mMemDc, 0, 0, SRCCOPY);
mMutex.unlock();
}
}