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
|
// vim: set tabstop=4 shiftwidth=4 noexpandtab
/*
Gwenview - A simple image viewer for TDE
Copyright 2000-2004 Aurélien Gâteau
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <iostream>
// TQt
#include <tqdir.h>
#include <tqfile.h>
#include <tqimage.h>
#include <tqstring.h>
// KDE
#include <kapplication.h>
#include <kaboutdata.h>
#include <kcmdlineargs.h>
#include <kdebug.h>
#include <kfilemetainfo.h>
// Local
#include "imageutils/imageutils.h"
#include "imageutils/jpegcontent.h"
using namespace std;
const char* ORIENT6_FILE="orient6.jpg";
const int ORIENT6_WIDTH=128; // This size is the size *after* orientation
const int ORIENT6_HEIGHT=256; // has been applied
const char* CUT_FILE="cut.jpg";
const TQString ORIENT6_COMMENT="a comment";
const TQString ORIENT1_VFLIP_FILE="test_orient1_vflip.jpg";
const char* THUMBNAIL_FILE="test_thumbnail.jpg";
const char* TMP_FILE="tmp.jpg";
class TestEnvironment {
public:
TestEnvironment() {
bool result;
TQFile in(ORIENT6_FILE);
result=in.open(IO_ReadOnly);
Q_ASSERT(result);
TQFileInfo info(in);
int size=info.size()/2;
char* data=new char[size];
int readSize=in.readBlock(data, size);
Q_ASSERT(size==readSize);
TQFile out(CUT_FILE);
result=out.open(IO_WriteOnly);
Q_ASSERT(result);
int wroteSize=out.writeBlock(data, size);
Q_ASSERT(size==wroteSize);
delete []data;
}
~TestEnvironment() {
TQDir::current().remove(CUT_FILE);
}
};
typedef TQMap<TQString,TQString> MetaInfoMap;
MetaInfoMap getMetaInfo(const TQString& path) {
KFileMetaInfo fmi(path);
TQStringList list=fmi.supportedKeys();
TQStringList::ConstIterator it=list.begin();
MetaInfoMap map;
for ( ; it!=list.end(); ++it) {
KFileMetaInfoItem item=fmi.item(*it);
map[*it]=item.string();
}
return map;
}
void compareMetaInfo(const TQString& path1, const TQString& path2, const TQStringList& ignoredKeys) {
MetaInfoMap mim1=getMetaInfo(path1);
MetaInfoMap mim2=getMetaInfo(path2);
Q_ASSERT(mim1.keys()==mim2.keys());
TQValueList<TQString> keys=mim1.keys();
TQValueList<TQString>::ConstIterator it=keys.begin();
for ( ; it!=keys.end(); ++it) {
TQString key=*it;
if (ignoredKeys.contains(key)) continue;
if (mim1[key]!=mim2[key]) {
kdError() << "Meta info differs. Key:" << key << ", V1:" << mim1[key] << ", V2:" << mim2[key] << endl;
}
}
}
void testResetOrientation() {
ImageUtils::JPEGContent content;
bool result;
// Test resetOrientation without transform
result=content.load(ORIENT6_FILE);
Q_ASSERT(result);
content.resetOrientation();
result=content.save(TMP_FILE);
Q_ASSERT(result);
result=content.load(TMP_FILE);
Q_ASSERT(result);
Q_ASSERT(content.orientation() == ImageUtils::NORMAL);
// Test resetOrientation with transform
result=content.load(ORIENT6_FILE);
Q_ASSERT(result);
content.resetOrientation();
content.transform(ImageUtils::ROT_90);
result=content.save(TMP_FILE);
Q_ASSERT(result);
result=content.load(TMP_FILE);
Q_ASSERT(result);
Q_ASSERT(content.orientation() == ImageUtils::NORMAL);
}
/**
* This function tests JPEGContent::transform() by applying a ROT_90
* transformation, saving, reloading and applying a ROT_270 to undo the ROT_90.
* Saving and reloading are necessary because lossless transformation only
* happens in JPEGContent::save()
*/
void testTransform() {
bool result;
TQImage finalImage, expectedImage;
ImageUtils::JPEGContent content;
result = content.load(ORIENT6_FILE);
Q_ASSERT(result);
content.transform(ImageUtils::ROT_90);
result = content.save(TMP_FILE);
Q_ASSERT(result);
result = content.load(TMP_FILE);
Q_ASSERT(result);
content.transform(ImageUtils::ROT_270);
result = content.save(TMP_FILE);
Q_ASSERT(result);
result = finalImage.load(TMP_FILE);
Q_ASSERT(result);
result = expectedImage.load(ORIENT6_FILE);
Q_ASSERT(result);
Q_ASSERT(finalImage == expectedImage);
}
void testSetComment() {
TQString comment = "test comment";
ImageUtils::JPEGContent content;
bool result;
result = content.load(ORIENT6_FILE);
Q_ASSERT(result);
content.setComment(comment);
Q_ASSERT(content.comment() == comment);
result = content.save(TMP_FILE);
Q_ASSERT(result);
result = content.load(TMP_FILE);
Q_ASSERT(result);
Q_ASSERT(content.comment() == comment);
}
int main(int argc, char* argv[]) {
TestEnvironment testEnv;
bool result;
KAboutData aboutData("testjpegcontent", "testjpegcontent", "0");
KCmdLineArgs::init( argc, argv, &aboutData );
KApplication kapplication;
// Reading info
ImageUtils::JPEGContent content;
result=content.load(ORIENT6_FILE);
Q_ASSERT(result);
Q_ASSERT(content.orientation() == 6);
Q_ASSERT(content.comment() == ORIENT6_COMMENT);
Q_ASSERT(content.size() == TQSize(ORIENT6_WIDTH, ORIENT6_HEIGHT));
// thumbnail()
TQImage thumbnail=content.thumbnail();
result=thumbnail.save(THUMBNAIL_FILE, "JPEG");
Q_ASSERT(result);
testResetOrientation();
testTransform();
testSetComment();
// Test that rotating a file a lot of times does not cause findJxform() to fail
result = content.load(ORIENT6_FILE);
Q_ASSERT(result);
// 12*4 + 1 is the same as 1, since rotating four times brings you back
for(int loop=0; loop< 12*4 + 1; ++loop) {
content.transform(ImageUtils::ROT_90);
}
result = content.save(TMP_FILE);
Q_ASSERT(result);
result = content.load(TMP_FILE);
Q_ASSERT(result);
Q_ASSERT(content.size() == TQSize(ORIENT6_HEIGHT, ORIENT6_WIDTH));
// Check the other meta info are still here
TQStringList ignoredKeys;
ignoredKeys << "Orientation" << "Comment";
compareMetaInfo(ORIENT6_FILE, ORIENT1_VFLIP_FILE, ignoredKeys);
// Test that loading and manipulating a truncated file does not crash
result=content.load(CUT_FILE);
Q_ASSERT(result);
Q_ASSERT(content.orientation() == 6);
Q_ASSERT(content.comment() == ORIENT6_COMMENT);
content.transform(ImageUtils::VFLIP);
kdWarning() << "# Next function should output errors about incomplete image" << endl;
content.save(TMP_FILE);
kdWarning() << "#" << endl;
}
|