forked from yuryfdr/pbtk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPBGIFImage.cxx
More file actions
129 lines (125 loc) · 4.23 KB
/
PBGIFImage.cxx
File metadata and controls
129 lines (125 loc) · 4.23 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
/* Copyright (C) 2011 Yury P. Fedorchenko (yuryfdr at users.sf.net) */
/*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "PBImg.h"
#include <stdio.h>
#include <stdlib.h>
#include "gif_lib.h"
#include <vector>
static int InterlacedOffset[] = { 0, 4, 2, 1 };
static int InterlacedJumps[] = { 8, 8, 4, 2 };
PBGIFImage::PBGIFImage(const char *fname){
GifFileType *GifFile;
if ((GifFile = DGifOpenFileName(fname)) == NULL) {
PrintGifError();
return;
}
std::vector<std::vector<unsigned char> > ScreenBuffer;
ScreenBuffer.resize(GifFile->SHeight);
int Size = GifFile->SWidth;
for (int i = 0; i < GifFile->SHeight; i++) {
ScreenBuffer[i].resize(Size);
for (int j = 0; j < GifFile->SWidth; j++)
ScreenBuffer[i][j] = GifFile->SBackGroundColor;
}
//printf("BG:%d\n",GifFile->SBackGroundColor);
GifRecordType RecordType;
int ImageNum=0;
int ExtCode;
GifByteType *Extension;
do {
if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR) {
PrintGifError();
return;
}
switch (RecordType) {
case IMAGE_DESC_RECORD_TYPE:
if (DGifGetImageDesc(GifFile) == GIF_ERROR) {
PrintGifError();
return;
}
{
int Row = GifFile->Image.Top;
int Col = GifFile->Image.Left;
int Width = GifFile->Image.Width;
int Height = GifFile->Image.Height;
//printf("Image %d at (%d, %d) [%dx%d]:\n",++ImageNum, Col, Row, Width, Height);
if (GifFile->Image.Left + GifFile->Image.Width > GifFile->SWidth ||
GifFile->Image.Top + GifFile->Image.Height > GifFile->SHeight) {
fprintf(stderr, "Image %d is not confined to screen dimension, aborted.\n",ImageNum);
return;
}
if (GifFile->Image.Interlace) {
/* Need to perform 4 passes on the images: */
for (int i = 0; i < 4; i++)
for (int j = Row + InterlacedOffset[i]; j < Row + Height;j += InterlacedJumps[i]) {
if (DGifGetLine(GifFile, &ScreenBuffer[j][Col],Width) == GIF_ERROR) {
PrintGifError();
return;
}
}
} else {
for (int i = 0; i < Height; i++) {
if (DGifGetLine(GifFile, &ScreenBuffer[Row++][Col],Width) == GIF_ERROR) {
PrintGifError();
return;
}
}
}
}
break;
case EXTENSION_RECORD_TYPE:
/* Skip any extension blocks in file: */
if (DGifGetExtension(GifFile, &ExtCode, &Extension) == GIF_ERROR) {
PrintGifError();
return;
}
while (Extension != NULL) {
if (DGifGetExtensionNext(GifFile, &Extension) == GIF_ERROR) {
PrintGifError();
return;
}
}
break;
case TERMINATE_RECORD_TYPE:
break;
default: /* Should be traps by DGifGetRecordType. */
break;
}
}while (RecordType != TERMINATE_RECORD_TYPE);
ColorMapObject* ColorMap = (GifFile->Image.ColorMap ? GifFile->Image.ColorMap
: GifFile->SColorMap);
depth = 3;
width = GifFile->SWidth;
height = GifFile->SHeight;
array = new unsigned char[width*height*depth];
unsigned char *ptr=(unsigned char *)array;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
/*if(ScreenBuffer[i][j]==GifFile->SBackGroundColor){
GifColorType *ColorMapEntry = &ColorMap->Colors[ScreenBuffer[i][j]];
*ptr++ = 0;//ColorMapEntry->Red;
*ptr++ = 0;//ColorMapEntry->Green;
*ptr++ = 0;//ColorMapEntry->Blue;
}else{*/
GifColorType *ColorMapEntry = &ColorMap->Colors[ScreenBuffer[i][j]];
*ptr++ = ColorMapEntry->Red;
*ptr++ = ColorMapEntry->Green;
*ptr++ = ColorMapEntry->Blue;
//}
}
}
}