-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeAniV2.cpp
257 lines (230 loc) · 7.24 KB
/
makeAniV2.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
#include <Magick++.h>
#include "gifV2.h"
using namespace Magick;
using std::cout;
using std::endl;
using std::list;
using std::string;
using std::exception;
void calcResizeGeo(Geometry & imgGeo, Geometry & pageGeo, const Image img, const char mode, const char* param);
void resize(Image* image, Geometry & imgResizeGeo, Geometry & pageResizeGeo, const char mode, const char* param, Geometry & maxGeo, list<GIF*> & endList);
ofstream log("makeAniV2.log");
int main(int argc, char** argv)
{
if(!log)
{
cout << "log File Open Failed" << endl;
exit(EXIT_FAILURE);
}
log << "log Start" << endl;
char str[30];
sprintf(str, "/proc/%d/status", getpid());
ifstream in(str);
if(!in)
{
cout << "Process Status File Open Failed" << endl;
exit(EXIT_FAILURE);
}
double startTime=clock();
cout.setf(ios::hex, ios::basefield);
cout << "argc : " << argc << endl;
char mode=argv[1][0];
char* param=&argv[1][1];
Image* image=NULL;
int resizeCnt=0;
Geometry maxGeo(0, 0);
Geometry imgResizeGeo;
Geometry pageResizeGeo;
list<GIF*> endList;
for(int i=2; i<argc; i++)
{
cout << argv[i] << endl;
string arg=argv[i];
string suffix=arg.substr(arg.rfind(".")+1);
if(suffix.compare("gif")==0 || suffix.compare("GIF")==0)
{
cout << "GIF comes!" << endl;
GIF GIFImg(argv[i]);
list<GIFImage*> & GIFImgList=GIFImg.getImageList();
list<GIFImage*>::iterator itr=GIFImgList.begin();
cout << "GIF Read Done" << endl;
for(int i=0; itr!=GIFImgList.end(); i++, itr++)
{
int fileSize=GIFImg.getGIFSize()+(*itr)->getImageSize();
//cout << "fileSize : " << fileSize << endl;
GIFData* tmpStream=new GIFData[fileSize];
GIFImg.writeBlob(tmpStream, i);
Blob* blob=new Blob(tmpStream, fileSize);
/*for(int t=0; t<fileSize+1; t++)
{
cout << int(tmpStream[t]) << " ";
if(t%26==0 && t>1)
cout << endl;
}
cin.get();
*/
image=new Image(*blob);
delete blob;
delete[] tmpStream;
if(i==GIFImg.getImageCnt()-1)
image->gifDisposeMethod(2);
if(GIFImg.getImageList().size()==1)
image->animationDelay(100);
resize(image, imgResizeGeo, pageResizeGeo, mode, param, maxGeo, endList);
in.seekg(ios::beg);
for(int t=0; t<16; t++)
in.getline(str, 100);
log << "resized : " << str << endl;
if(image!=NULL)
{
delete image;
image=NULL;
}
}
}
else
{
//cout << "else" << endl;
image=new Image(argv[i]);
resize(image, imgResizeGeo, pageResizeGeo, mode, param, maxGeo, endList);
in.seekg(ios::beg);
for(int t=0; t<16; t++)
in.getline(str, 100);
log << "resized : " << str << endl;
if(image!=NULL)
{
delete image;
image=NULL;
}
}
log << argv[i] << " Resize Done" << endl;
} // end argc : reading input file
cout << "resize done" << endl;
list<GIF*>::iterator enditr=endList.begin();
char name[30];
/*for(int i=0; enditr!=endList.end(); i++, enditr++)
{
sprintf(name, "asdf_%03d.gif", i);
(*enditr)->writeFile(name, ALLIMGS);
}*/
GIF merged(endList);
merged.writeFile("animated_maker.gif", ALLIMGS);
in.seekg(ios::beg);
for(int t=0; t<16; t++)
in.getline(str, 100);
log << endl << "Process Done : " << str << endl;
for(list<GIF*>::iterator endItr=endList.begin(); endItr!=endList.end(); endItr++)
delete *endItr;
log << "Process Time : " << ((clock()-startTime)/CLOCKS_PER_SEC) << endl;
in.close();
log.close();
cout << "Process Done" << endl;
return 0;
}
void calcResizeGeo(Geometry & imgGeo, Geometry & pageGeo, const Image img, const char mode, const char* param)
{
char par[12];
strncpy(&par[0], param, 11);
char* pparam=strtok(par, "x");
double param1=atof(pparam);
double param2=-1;
pparam=strtok(NULL, "x");
if(pparam!=NULL)
param2=atof(pparam);
Geometry* geo=NULL;
double ratio=0;
if(param1!=0 && param2!=0)
{
switch(mode)
{
case 'w':
ratio=param1/img.page().width();
imgGeo=Geometry(int(img.columns()*ratio), 0);
pageGeo=Geometry(int(img.page().width()*ratio), int(img.page().height()*ratio), int(img.page().xOff()*ratio), int(img.page().yOff()*ratio));
break;
case 'h':
ratio=param1/img.page().height();
imgGeo=Geometry(0, int(img.rows()*ratio));
pageGeo=Geometry(int(img.page().width()*ratio), int(img.page().height()*ratio), int(img.page().xOff()*ratio), int(img.page().yOff()*ratio));
break;
default:
cout << "Wrong Resize Mode, Set to Default mode m" << endl;
case 'm':
ratio=(img.page().width()/param1>img.page().height()/param2 ? param1/img.page().width() : param2/img.page().height());
imgGeo=img.page().width()/param1>img.page().height()/param2 ? Geometry(int(img.columns()*ratio), 0) : Geometry(0, int(img.rows()*ratio));
pageGeo=Geometry(int(img.page().width()*ratio), int(img.page().height()*ratio), int(img.page().xOff()*ratio), int(img.page().yOff()*ratio));
break;
case 'f':
ratio=(param1/img.page().width()>param2/img.page().height() ? param1/img.page().width() : param2/img.page().height());
imgGeo=param1/img.page().width()>param2/img.page().height() ? Geometry(int(img.columns()*ratio), 0) : Geometry(0, int(img.rows()*ratio));
pageGeo=Geometry(int(img.page().width()*ratio), int(img.page().height()*ratio), int(img.page().xOff()*ratio), int(img.page().yOff()*ratio));
cout << "imgGeo : " << imgGeo.width() << " " << imgGeo.height() << endl;
break;
}
if(mode!='f' && (imgGeo.width()>img.page().width() || imgGeo.height()>img.page().height()))
{
imgGeo=Geometry(0, 0, 0, 0);
pageGeo=Geometry(0, 0, 0, 0);
imgGeo.greater(MagickTrue);
imgGeo.less(MagickFalse);
pageGeo.greater(MagickTrue);
pageGeo.less(MagickFalse);
}
}
}
void resize(Image* image, Geometry & imgResizeGeo, Geometry & pageResizeGeo, const char mode, const char* param, Geometry & maxGeo, list<GIF*> & endList)
{
calcResizeGeo(imgResizeGeo, pageResizeGeo, *image, mode, param);
if(image->magick()!="GIF")
{
//cout << "setting" << endl;
//image->gifDisposeMethod(2);
image->animationDelay(100);
//image->matte(MagickFalse);
image->magick("GIF");
}
image->animationIterations(0);
if((imgResizeGeo.width()==0 && imgResizeGeo.height()==0) || (pageResizeGeo.width()==0 && pageResizeGeo.height()==0))
{
cout << "noresize" << endl;
//cout << "Wrong Size Parameter, Exit..." << endl;
//exit(EXIT_FAILURE);
}
else
{
image->resize(imgResizeGeo);
image->page(pageResizeGeo);
}
try
{
if(mode=='f')
{
cout << "f" << image->columns() << " " << image->rows() << endl;
Geometry cropGeo(param);
cropGeo.xOff((image->page().width()-cropGeo.width())/2);
cropGeo.yOff((image->page().height()-cropGeo.height())/2);
image->crop(cropGeo);
cropGeo.xOff(image->page().xOff()-cropGeo.xOff());
cropGeo.yOff(image->page().yOff()-cropGeo.yOff());
image->page(cropGeo);
}
}
catch(Exception e)
{
cout << "ERR!!! " <<e.what() << endl;
return;
}
//cout << "resize" << endl;
if(maxGeo.width()<image->page().width())
maxGeo.width(image->page().width());
if(maxGeo.height()<image->page().height())
maxGeo.height(image->page().height());
//cin.get();
Blob* endBlob=new Blob();
image->write(endBlob);
//cout << "writeBlob" << endl;
//image->write("asdf.gif");
endList.push_back(new GIF((GIFData*)endBlob->data(), endBlob->length()));
//cout << "endList : " << endList.size() << endl;
delete endBlob;
}