-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeoCalculator.cpp
153 lines (136 loc) · 5.33 KB
/
GeoCalculator.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
#include "GeoCalculator.h"
using std::cout;
using std::endl;
int inline calcSize(int size, double ratio) {return int(double(size)*ratio);}
GeoCalculator::GeoCalculator(double _tolerance)
: tolerance(3), geoWidth(-1), geoHeight(-1)
{
tolerance=_tolerance;
}
GeoCalculator::~GeoCalculator()
{
}
void GeoCalculator::calcGeo(Geometry &imgGeo, Geometry &pageGeo, Image *&image, char &mode, char *&inputSize)
{
double ratio=0;
this->parseInputSize(mode, inputSize);
if(this->geoWidth==0 || this->geoHeight==0) {
cout << "Incompatible inputSize Error. Exiting..." << endl;
exit(EXIT_FAILURE);
} else {
this->maxRatio=double(image->page().width())/double(image->page().height())*this->tolerance;
this->minRatio=double(image->page().width())/double(image->page().height())/this->tolerance;
switch(mode) {
case 'w':
this->setWidthOption(image, imgGeo, pageGeo);
this->setNoresize(image, imgGeo, pageGeo);
break;
case 'h':
this->setHeightOption(image, imgGeo, pageGeo);
this->setNoresize(image, imgGeo, pageGeo);
break;
case 'a':
this->setAutoOption(image, imgGeo, pageGeo);
break;
default:
cout << "Imcompatible Resize Mode Error. Set to Default Mode 'm'" << endl;
case 'm':
this->setMobileOption(image, imgGeo, pageGeo);
this->setNoresize(image, imgGeo, pageGeo);
break;
case 'f':
this->setFixedOption(image, imgGeo, pageGeo);
break;
}
}
}
void GeoCalculator::parseInputSize(const char mode, const char *inputSize)
{
strncpy(this->inputSize, inputSize, 20);
char input[20];
strncpy(input, this->inputSize, 20);
char *param=strtok(input, "x");
this->geoWidth=atoi(param);
if(mode=='h')
this->geoHeight=this->geoWidth;
param=strtok(NULL, "x");
if(param!=NULL) {
this->geoHeight=atoi(param);
}
}
void GeoCalculator::setWidthOption(Image *&image, Geometry &imgGeo, Geometry &pageGeo)
{
double ratio=double(geoWidth)/double(image->page().width());
imgGeo=Geometry(calcSize(image->columns(), ratio), 0);
pageGeo=Geometry(calcSize(image->page().width(), ratio), calcSize(image->page().height(), ratio),
calcSize(image->page().xOff(), ratio), calcSize(image->page().yOff(), ratio));
}
void GeoCalculator::setHeightOption(Image *&image, Geometry &imgGeo, Geometry &pageGeo)
{
double ratio=double(geoHeight)/double(image->page().height());
imgGeo=Geometry(0, calcSize(image->rows(), ratio));
pageGeo=Geometry(calcSize(image->page().width(), ratio), calcSize(image->page().height(), ratio),
calcSize(image->page().xOff(), ratio), calcSize(image->page().yOff(), ratio));
}
void GeoCalculator::setFixedOption(Image *&image, Geometry &imgGeo, Geometry &pageGeo)
{
double ratio=0;
if(this->geoWidth/image->page().width() > this->geoHeight/image->page().height()) {
ratio=double(geoWidth)/double(image->page().width());
imgGeo=Geometry(calcSize(image->columns(), ratio), 0);
} else {
ratio=double(geoHeight)/double(image->page().height());
imgGeo=Geometry(0, calcSize(image->rows(), ratio));
}
pageGeo=Geometry(calcSize(image->page().width(), ratio), calcSize(image->page().height(), ratio),
calcSize(image->page().xOff(), ratio), calcSize(image->page().yOff(), ratio));
}
void GeoCalculator::setMobileOption(Image *&image, Geometry &imgGeo, Geometry &pageGeo)
{
double ratio=0;
if(image->page().width()/this->geoWidth > image->page().height()/this->geoHeight) {
ratio=double(geoWidth)/double(image->page().width());
imgGeo=Geometry(calcSize(image->columns(), ratio), 0);
} else {
ratio=double(geoHeight)/double(image->page().height());
imgGeo=Geometry(0, calcSize(image->rows(), ratio));
}
pageGeo=Geometry(calcSize(image->page().width(), ratio), calcSize(image->page().height(), ratio),
calcSize(image->page().xOff(), ratio), calcSize(image->page().yOff(), ratio));
}
void GeoCalculator::setAutoOption(Image *&image, Geometry &imgGeo, Geometry &pageGeo)
{
double ratio=double(image->page().width())/double(image->page().height());
Geometry preCropGeo;
if(ratio > this->minRatio*2 && ratio < this->maxRatio/2) { // f mode
this->setFixedOption(image, imgGeo, pageGeo);
} else if(ratio > this->minRatio && ratio < this->maxRatio) { // m mode
this->setMobileOption(image, imgGeo, pageGeo);
this->setNoresize(image, imgGeo, pageGeo);
} else if(image->page().height() > this->geoHeight || image->page().width() > this->geoWidth) { // f+m mode
if(ratio > this->maxRatio) { // wide
preCropGeo=Geometry(calcSize(geoHeight, this->tolerance), this->geoHeight);
} else { // long
preCropGeo=Geometry(this->geoWidth, calcSize(geoWidth, this->tolerance));
}
//crop
preCropGeo.xOff(int((double(image->page().width())-double(preCropGeo.width()))/2));
preCropGeo.xOff(int((double(image->page().height())-double(preCropGeo.height()))/2));
image->crop(preCropGeo);
preCropGeo.xOff(image->page().xOff()-preCropGeo.xOff());
preCropGeo.yOff(image->page().yOff()-preCropGeo.yOff());
image->page(preCropGeo);
this->setMobileOption(image, imgGeo, pageGeo);
this->setNoresize(image, imgGeo, pageGeo);
} else { // Wide or Long but too Small to Crop
this->setMobileOption(image, imgGeo, pageGeo);
this->setNoresize(image, imgGeo, pageGeo);
}
}
void GeoCalculator::setNoresize(Image *&image, Geometry &imgGeo, Geometry &pageGeo)
{
if(imgGeo.width() > image->page().width() || imgGeo.height() > image->page().height()) {
imgGeo=Geometry(0, 0, 0, 0);
pageGeo=Geometry(0, 0, 0, 0);
}
}