-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAniGIFMaker.cpp
171 lines (147 loc) · 3.95 KB
/
AniGIFMaker.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
#include "AniGIFMaker.h"
#include "GeoCalculator.h"
#include <exception>
using namespace Magick;
using std::exception;
AniGIFMaker::AniGIFMaker(double _tolerance, bool _cOption, char _mode, char *_inputSize)
: tolerance(3), cOption(false), mode('m'), inputSize(NULL), maxGeo(0, 0, 0, 0)
{
tolerance=_tolerance;
cOption=_cOption;
mode=_mode;
inputSize=_inputSize;
}
AniGIFMaker::~AniGIFMaker()
{
list<GIF*>::iterator itr=GIFImageList.begin();
for(; itr!=GIFImageList.end(); itr++) {
delete *itr;
}
}
void AniGIFMaker::addToAniGIF(const char* filename)
{
GIF *inputGIFImage=NULL;
Image *inputImage=NULL;
GeoCalculator calc(this->tolerance);
string file=filename;
string suffix=file.substr(file.rfind(".")+1);
int inputGIFCnt=0;
bool done=false;
const Geometry initGeo(0, 0, 0, 0);
Geometry imgGeo=initGeo;
Geometry pageGeo=initGeo;
while(!done) {
try {
if(inputGIFImage!=NULL) {
cout << "NOT NULL" << endl;
this->makeInputImage(inputGIFImage, inputGIFCnt, inputImage);
}
else if (suffix.compare("gif")==0 || suffix.compare("GIF")==0) {
cout << "GIF COMES" << endl;
inputGIFImage=new GIF(filename);
this->makeInputImage(inputGIFImage, inputGIFCnt, inputImage);
cout << "inputImage Made : " << inputGIFCnt << endl;
}
else {
cout << "ELSE" << endl;
inputImage=new Image(filename);
}
calc.calcGeo(imgGeo, pageGeo, inputImage, (this->mode), (this->inputSize));
this->setAniGIFProperties(inputImage);
if(!this->isNoresize(imgGeo, pageGeo)) {
inputImage->resize(imgGeo);
inputImage->page(pageGeo);
}
if(this->mode=='f') {
this->cropImage(inputImage);
}
this->insertEndGIF(inputImage);
imgGeo=initGeo;
pageGeo=initGeo;
if(cOption || inputGIFImage==NULL || inputGIFImage->getImgCnt()==inputGIFCnt) {
cout << "done : true" << endl;
done=true;
if(inputGIFImage!=NULL) {
delete inputGIFImage;
inputGIFImage=NULL;
}
}
}
catch(const exception &e) {
cout << "addToAniGIF Process Error : " << e.what() << endl;
return;
}
}
cout << filename << " Add Done : " << this->getImgList() << endl;
}
void AniGIFMaker::makeAniGIF(const char *outfilename)
{
char filename[100];
if(outfilename!=NULL) {
strncpy(filename, outfilename, 100);
}
else { //TODO
sprintf(filename, "makeAniV3_%s%s%s_t%f.gif", this->cOption ? "c" : "_", this->mode, this->inputSize, this->tolerance);
}
//DEBUG
GIF merged(this->GIFImageList);
merged.writeFile(filename, ALLIMGS);
cout << "makeAniGIF Done" << endl;
}
void AniGIFMaker::makeInputImage(GIF *&inputGIFImage, int &cnt, Image *&inputImage)
{
GIFBlobData* tmpStream=new GIFBlobData[inputGIFImage->getGIFSize(cnt)];
inputGIFImage->writeBlob(tmpStream, inputGIFImage->getGIFSize(cnt));
Blob* blob=new Blob(tmpStream, inputGIFImage->getGIFSize(cnt));
inputImage=new Image(*blob);
cnt++;
if(blob!=NULL) {
delete blob;
blob=NULL;
}
if(tmpStream!=NULL) {
delete[] tmpStream;
tmpStream=NULL;
}
}
void AniGIFMaker::setAniGIFProperties(Image *&image)
{
image->animationIterations(0);
if(image->magick()!="GIF" || this->cOption) {
image->gifDisposeMethod(2);
image->animationDelay(100);
}
image->magick("GIF");
}
bool AniGIFMaker::isNoresize(const Geometry imgGeo, const Geometry pageGeo)
{
return ((imgGeo.width()==0 && imgGeo.height()==0) || (pageGeo.width()==0 && pageGeo.height()==0));
}
void AniGIFMaker::cropImage(Image *&inputImage)
{
try {
Geometry cropGeo(this->inputSize);
cropGeo.xOff();
cropGeo.yOff();
inputImage->crop(cropGeo);
cropGeo.xOff();
cropGeo.yOff();
inputImage->page(cropGeo);
} catch(exception e) {
cout << "Crop Error : " << e.what() << endl;
}
}
void AniGIFMaker::insertEndGIF(Image *&inputImage)
{
Blob *endBlob=new Blob();
inputImage->write(endBlob);
this->GIFImageList.push_back(new GIF((GIFBlobData*)endBlob->data(), endBlob->length()));
if(endBlob!=NULL) {
delete endBlob;
endBlob=NULL;
}
if(inputImage!=NULL) {
delete inputImage;
inputImage=NULL;
}
}