Debian Codegraph @

libUnionImage/UnionImage/giflib/unused/cmanagerattributeservice.cpp

raw
1 /*
2 * Copyright (C) 2019 ~ 2020 Uniontech Software Technology Co.,Ltd
3 *
4 * Author: Iceyer<[email protected]>
5 * Maintainer: Iceyer <[email protected]>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "cmanagerattributeservice.h"
18 #include <stdlib.h>
19 #include "malloc.h"
20 #include <QGraphicsItem>
21 #include <QDebug>
22 #include <QtConcurrent>
23
24 CManagerAttributeService *CManagerAttributeService::instance = nullptr;
25 CManagerAttributeService *CManagerAttributeService::getInstance()
26 {
27 if (nullptr == instance) {
28 instance = new CManagerAttributeService();
29 }
30 return instance;
31 }
32
33 void CManagerAttributeService::setfilePathWithSignalPlay(const QString &path)
34 {
35 m_couldRun = true;
36
37 m_th = QThread::create([ = ]() {
38 while (m_couldRun) {
39 m_isFirst = true;
40 gEffectGifFile = path;
41 int32_t ret = GifLoadFile();
42 if (ret >= 0) {
43 GifFrameShow();
44 }
45
46 }
47 GifFreeFile();
48 });
49
50 m_th->start();
51 }
52
53 void CManagerAttributeService::setfilePath(const QString &path)
54 {
55 m_couldRun = true;
56 m_isFirst = true;
57 gEffectGifFile = path;
58 GifLoadFile();
59 }
60
61 void CManagerAttributeService::setCouldRun(bool couldRun)
62 {
63 m_couldRun = couldRun;
64 if (nullptr != m_th) {
65 m_th->quit();
66 m_th->wait();
67 }
68 }
69
70 int32_t CManagerAttributeService::GifLoadFile()
71 {
72 int32_t error = 0;
73 int32_t size = 0;
74 int32_t idx = 0;
75 int32_t ret = 0;
76
77 do {
78 if (nullptr == gEffectGifFile) {
79 ret = -1;
80 break;
81 }
82
83 gpGifFile = DGifOpenFileName(gEffectGifFile.toStdString().c_str(), &error);
84 if (nullptr == gpGifFile) {
85 ret = -2;
86 break;
87 }
88
89 if ((gpGifFile->SHeight == 0) || (gpGifFile->SWidth == 0)) {
90 ret = -3;
91 break;
92 }
93
94 gpScreenBuffer = (GifRowType *)malloc(gpGifFile->SHeight * sizeof(GifRowType));
95 if (nullptr == gpScreenBuffer) {
96 ret = -4;
97 break;
98 }
99
100 /* Size in bytes one row */
101 size = gpGifFile->SWidth * sizeof(GifPixelType);
102 gpScreenBuffer[0] = (GifRowType)malloc(size);
103 if (nullptr == gpScreenBuffer[0]) {
104 ret = -5;
105 break;
106 }
107
108 /* Set its color to BackGround */
109 for (idx = 0; idx < gpGifFile->SWidth; idx++) {
110 gpScreenBuffer[0][idx] = gpGifFile->SBackGroundColor;
111 }
112
113 /* Allocate the other rows, and set their color to background too */
114 for (idx = 1; idx < gpGifFile->SHeight; idx++) {
115 gpScreenBuffer[idx] = (GifRowType)malloc(size);
116 if (nullptr == gpScreenBuffer[idx]) {
117 ret = -6;
118 break;
119 }
120 memcpy(gpScreenBuffer[idx], gpScreenBuffer[0], size);
121 }
122 if (0 > ret) {
123 break;
124 }
125 } while (0);
126
127 if (0 > ret) {
128 GifFreeFile();
129 }
130
131 return ret;
132 }
133
134 void CManagerAttributeService::GifFreeFile()
135 {
136 int32_t idx = 0;
137 int32_t error = 0;
138 if (nullptr != gpGifFile) {
139 for (idx = 0; idx < gpGifFile->SHeight; idx++) {
140 if (nullptr != gpScreenBuffer[idx]) {
141 free(gpScreenBuffer[idx]);
142 gpScreenBuffer[idx] = nullptr;
143 }
144 }
145 }
146
147
148 if (nullptr != gpScreenBuffer) {
149 free(gpScreenBuffer);
150 gpScreenBuffer = nullptr;
151 }
152
153 if (nullptr != gpGifFile) {
154 DGifCloseFile(gpGifFile, &error);
155 gpGifFile = nullptr;
156 }
157 }
158
159
160 int32_t CManagerAttributeService::GifFrameShow()
161 {
162 ColorMapObject *colorMap = nullptr;
163 GifByteType *extension = nullptr;
164
165 int32_t InterlacedOffset[] = { 0, 4, 2, 1 }; // The way Interlaced image should
166 int32_t InterlacedJumps[] = { 8, 8, 4, 2 }; // be read - offsets and jumps...
167 uint8_t rgbBuf[240 * 320] = {0};
168
169 int32_t extCode = 0;
170 int32_t row = 0;
171 int32_t col = 0;
172 int32_t width = 0;
173 int32_t height = 0;
174 int32_t iW = 0;
175 int32_t iH = 0;
176 int32_t ret = 0;
177 static int temp = 0;
178 do {
179 if (!m_couldRun) {
180 break;
181 }
182 if (DGifGetRecordType(gpGifFile, &gRecordType) == GIF_ERROR) {
183 ret = -1;
184 break;
185 }
186
187 switch (gRecordType) {
188 case IMAGE_DESC_RECORD_TYPE: {
189 if (!m_couldRun) {
190 break;
191 }
192 if (DGifGetImageDesc(gpGifFile) == GIF_ERROR) {
193 ret = -2;
194 break;
195 }
196
197 row = gpGifFile->Image.Top;
198 col = gpGifFile->Image.Left;
199 width = gpGifFile->Image.Width;
200 height = gpGifFile->Image.Height;
201
202 if (gpGifFile->Image.Interlace) {
203 for (iH = 0; iH < 4; iH++) {
204 for (iW = row + InterlacedOffset[iH]; iW < row + height; iW += InterlacedJumps[iH]) {
205 DGifGetLine(gpGifFile, &gpScreenBuffer[iW][col], width);
206 }
207 }
208 } else {
209 for (iH = 0; iH < height; iH++) {
210 DGifGetLine(gpGifFile, &gpScreenBuffer[row++][col], width);
211 }
212 }
213
214 colorMap = (gpGifFile->Image.ColorMap ?
215 gpGifFile->Image.ColorMap : gpGifFile->SColorMap);
216 if (colorMap == nullptr) {
217 ret = -3;
218 break;
219 }
220
221 GifScreenBufferToRgb888(colorMap, rgbBuf, gpScreenBuffer,
222 gpGifFile->SWidth, gpGifFile->SHeight, tras);
223 //QImage img((uchar *)rgbBuf, gpGifFile->SWidth, gpGifFile->SHeight, QImage::Format_RGB32);
224 break;
225 }
226 case EXTENSION_RECORD_TYPE: {
227 if (!m_couldRun) {
228 break;
229 }
230 /* Skip any extension blocks in file: */
231 if (DGifGetExtension(gpGifFile, &extCode, &extension) == GIF_ERROR) {
232 ret = -4;
233 break;
234 }
235 if (extension != nullptr) {
236 if (extension[0] & 0x01)
237 tras = NO_TRANSPARENT_COLOR;
238 else
239 tras = (int)extension[4];
240 }
241
242 while (extension != NULL) {
243 if (!m_couldRun) {
244 break;
245 }
246 if (DGifGetExtensionNext(gpGifFile, &extension) == GIF_ERROR) {
247 ret = -5;
248 break;
249 }
250 if (extension != nullptr) {
251 if (extension[0] & 0x01)
252 tras = NO_TRANSPARENT_COLOR;
253 else
254 tras = (int)extension[4];
255 }
256 }
257 break;
258 }
259 case TERMINATE_RECORD_TYPE:
260 break;
261 default:
262 break;
263 }
264 if (0 < ret) {
265 break;
266 }
267 } while (gRecordType != TERMINATE_RECORD_TYPE);
268 return ret;
269 }
270
271 int32_t CManagerAttributeService::GifFrameShowSingle()
272 {
273 ColorMapObject *colorMap = nullptr;
274 GifByteType *extension = nullptr;
275 int32_t InterlacedOffset[] = { 0, 4, 2, 1 }; // The way Interlaced image should
276 int32_t InterlacedJumps[] = { 8, 8, 4, 2 }; // be read - offsets and jumps...
277 uint8_t rgbBuf[240 * 320] = {0};
278 int32_t extCode = 0;
279 int32_t row = 0;
280 int32_t col = 0;
281 int32_t width = 0;
282 int32_t height = 0;
283 int32_t iW = 0;
284 int32_t iH = 0;
285 int32_t ret = 0;
286 if (gRecordType != TERMINATE_RECORD_TYPE) {
287 switch (gRecordType) {
288 case IMAGE_DESC_RECORD_TYPE: {
289 if (!m_couldRun) {
290 break;
291 }
292 if (DGifGetImageDesc(gpGifFile) == GIF_ERROR) {
293 ret = -2;
294 break;
295 }
296 row = gpGifFile->Image.Top;
297 col = gpGifFile->Image.Left;
298 width = gpGifFile->Image.Width;
299 height = gpGifFile->Image.Height;
300 if (gpGifFile->Image.Interlace) {
301 for (iH = 0; iH < 4; iH++) {
302 for (iW = row + InterlacedOffset[iH]; iW < row + height; iW += InterlacedJumps[iH]) {
303 DGifGetLine(gpGifFile, &gpScreenBuffer[iW][col], width);
304 }
305 }
306 } else {
307 for (iH = 0; iH < height; iH++) {
308 DGifGetLine(gpGifFile, &gpScreenBuffer[row++][col], width);
309 }
310 }
311 colorMap = (gpGifFile->Image.ColorMap ?
312 gpGifFile->Image.ColorMap : gpGifFile->SColorMap);
313 if (colorMap == nullptr) {
314 ret = -3;
315 break;
316 }
317 GifScreenBufferToRgb888(colorMap, rgbBuf, gpScreenBuffer,
318 gpGifFile->SWidth, gpGifFile->SHeight, tras);
319 //QImage img((uchar *)rgbBuf, gpGifFile->SWidth, gpGifFile->SHeight, QImage::Format_RGB32);
320 break;
321 }
322 case EXTENSION_RECORD_TYPE: {
323 if (!m_couldRun) {
324 break;
325 }
326 /* Skip any extension blocks in file: */
327 if (DGifGetExtension(gpGifFile, &extCode, &extension) == GIF_ERROR) {
328 ret = -4;
329 break;
330 }
331 if (extension != nullptr) {
332 if (extension[0] & 0x01)
333 tras = NO_TRANSPARENT_COLOR;
334 else
335 tras = (int)extension[4];
336 }
337 while (extension != NULL) {
338 if (!m_couldRun) {
339 break;
340 }
341 if (DGifGetExtensionNext(gpGifFile, &extension) == GIF_ERROR) {
342 ret = -5;
343 break;
344 }
345 if (extension != nullptr) {
346 if (extension[0] & 0x01)
347 tras = NO_TRANSPARENT_COLOR;
348 else
349 tras = (int)extension[4];
350 }
351
352 }
353 break;
354 }
355 case TERMINATE_RECORD_TYPE:
356 break;
357
358 default:
359 break;
360 }
361
362 if (0 < ret) {
363 return 0;
364 }
365 }
366 return 0;
367 }
368
369 void CManagerAttributeService::GifScreenBufferToRgb888(ColorMapObject *ColorMap,
370 uint8_t *inRgb,
371 GifRowType *ScreenBuffer,
372 int32_t ScreenWidth,
373 int32_t ScreenHeight,
374 int alphaIndex)
375 {
376 Q_UNUSED(inRgb);
377 if (m_isFirst) {
378 first = QImage(ScreenWidth, ScreenHeight, QImage::Format_RGB32);
379 m_isFirst = false;
380 }
381 GifColorType *ColorMapEntry = nullptr;
382 // GifRowType GifRow = nullptr;
383 QByteArray byte;
384 int32_t idxH = 0;
385 int32_t idxW = 0;
386 int startTime = static_cast<int>(QDateTime::currentMSecsSinceEpoch());
387 // QImage img(ScreenWidth, ScreenHeight, QImage::Format_ARGB32);
388 for (idxH = 0; idxH < ScreenHeight; idxH++) {
389
390 for (idxW = 0; idxW < ScreenWidth; idxW++) {
391 ColorMapEntry = &ColorMap->Colors[ScreenBuffer[idxH][idxW]];
392 //如果是透明色
393 if (alphaIndex == ScreenBuffer[idxH][idxW] || first.pixel(idxW, idxH) == qRgb(ColorMapEntry->Red, ColorMapEntry->Green, ColorMapEntry->Blue)) {
394 //img.setPixel(idxW, idxH, qRgba(ColorMapEntry->Red, ColorMapEntry->Green, ColorMapEntry->Blue, 0));
395 } else {
396 // img.setPixel(idxW, idxH, qRgba(ColorMapEntry->Red, ColorMapEntry->Green, ColorMapEntry->Blue, 255));
397 first.setPixel(idxW, idxH, qRgba(ColorMapEntry->Red, ColorMapEntry->Green, ColorMapEntry->Blue, 255));
398 }
399 }
400 }
401
402 if (!m_couldRun) {
403 return;
404 }
405
406 int endTime = static_cast<int>(QDateTime::currentMSecsSinceEpoch());
407 int tolTime = endTime - startTime;
408
409 if (tolTime < 100) {
410 QThread::msleep(static_cast<unsigned long>((100 - tolTime)));
411 }
412
413 emit emitImageSignal(first, m_isFirst);
414 }
415
416 //void CManagerAttributeService::GifScreenBufferToRgb888(ColorMapObject *ColorMap, uint8_t *inRgb, GifRowType *ScreenBuffer, int32_t ScreenWidth, int32_t ScreenHeight, int alphaIndex)
417 //{
418 // GifColorType *ColorMapEntry = nullptr;
419 // GifRowType GifRow = nullptr;
420 // uint8_t *rgbBuf = inRgb;
421 // int32_t idxH = 0;
422 // int32_t idxW = 0;
423 // m_pImg = QImage(ScreenWidth, ScreenHeight, QImage::Format_ARGB32);
424 // m_time.restart();
425 // for (idxH = 0; idxH < ScreenHeight; idxH++) {
426
427 // for (idxW = 0; idxW < ScreenWidth; idxW++) {
428 // ColorMapEntry = &ColorMap->Colors[ScreenBuffer[idxH][idxW]];
429 // //如果是透明色
430 // if (alphaIndex == ScreenBuffer[idxH][idxW] || first.pixel(idxW, idxH) == qRgb(ColorMapEntry->Red, ColorMapEntry->Green, ColorMapEntry->Blue)) {
431 // //img.setPixel(idxW, idxH, qRgba(ColorMapEntry->Red, ColorMapEntry->Green, ColorMapEntry->Blue, 0));
432 // } else {
433 // // img.setPixel(idxW, idxH, qRgba(ColorMapEntry->Red, ColorMapEntry->Green, ColorMapEntry->Blue, 255));
434 // m_pImg.setPixel(idxW, idxH, qRgba(ColorMapEntry->Red, ColorMapEntry->Green, ColorMapEntry->Blue, 255));
435 // }
436 // }
437 // }
438 // int temp = m_time.elapsed();
439 // //emit emitImageSignal(img, m_isFirst);
440 // if (temp >= 100) {
441 // emit emitImageSignal(m_pImg, m_isFirst);
442 // } else {
443 // QThread::msleep(100 - temp);
444 // emit emitImageSignal(m_pImg, m_isFirst);
445 // }
446
447 // m_isFirst = false;
448 // //m_map.insert(m_index++, img);
449 //}
450
451 CManagerAttributeService::CManagerAttributeService()
452 {
453 }
454
455