forked from yuezhao/ezviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagewrapper.cpp
433 lines (378 loc) · 15 KB
/
imagewrapper.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/****************************************************************************
* EZ Viewer
* Copyright (C) 2013 huangezhao. CHINA.
* Contact: huangezhao (huangezhao@gmail.com)
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***************************************************************************/
#include "imagewrapper.h"
#include "imageheader.h"
#include "toolkit.h"
#include "config.h"
#include <QApplication>
#include <QDateTime>
#include <QMovie>
#include <QPainter>
#include <QSvgRenderer>
#include <QMutex>
#ifdef TESTING_RAW
#include <libraw.h>
#endif // TESTING_RAW
ImageWrapper::ImageWrapper()
: Cache(), imageFrames(0), formatFlag(REGULAR_FLAG),
movie(NULL), svgRender(NULL), header(new ImageHeader())
{
}
ImageWrapper::~ImageWrapper()
{
recycle();
delete header;
}
QImage ImageWrapper::currentImage() const
{
if (!isValid())
return QImage();
if ((formatFlag & MOVIE_FLAG) && movie)
return movie->currentImage();
return image;
}
void ImageWrapper::recycle()
{
disconnect(this, SIGNAL(animationUpdated()));
disconnect(this, SIGNAL(frameUpdated()));
SafeDelete(movie);
SafeDelete(svgRender);
}
bool ImageWrapper::isAnimationFromat(const QString &format)
{
return format == "gif" || format == "mng" || format == "svg";
}
void ImageWrapper::load(const QString &filePath, bool isPreReading)
{
if (filePath.isEmpty())
return;
// static QMutex mutex;
// QMutexLocker locker(&mutex); //! FIXME: if the thread pool has more than one thread, when they call this function at the same time, may cause problem by the calling order.
recycle();
imagePath = filePath;
imageAttribute = QString::null;
formatFlag = REGULAR_FLAG;
QImageReader reader(imagePath);
reader.setDecideFormatFromContent(true);
imageFormat = reader.format();
imageFrames = reader.imageCount();
qDebug("format is %s, frame count is %d, file name %s", qPrintable(imageFormat),
imageFrames, qPrintable(ToolKit::filename(imagePath)));
if(isAnimationFromat(imageFormat)){
if (imageFormat == "svg") {
svgRender = new QSvgRenderer(imagePath); // this svgRender was created in pre-reading thread.
if (svgRender->animated()) {
formatFlag |= SVG_FLAG;
image = QImage(svgRender->defaultSize(), QImage::Format_ARGB32);
QPainter painter(&image);
svgRender->render(&painter);
}/* else {
SafeDelete(svgRender);
}*/
} else if (imageFrames != 1) { // "gif" or "mng" animation
movie = new QMovie(imagePath); // this movie was created in pre-reading thread.
movie->setFormat(imageFormat.toLocal8Bit()); /// this is important when the file name didn't end with 'gif'.
if(movie->isValid()) {
formatFlag |= MOVIE_FLAG;
if (movie->jumpToFrame(0))
image = movie->currentImage();
}/*else{ //cannot read image, so delete
SafeDelete(movie);
}*/
}
}
if(formatFlag == REGULAR_FLAG){
if(imageFormat == "ico"){//! is ico image has the same height and width?
reader.read(&image);
int maxIndex = 0;
int maxWidth = image.width();
for(int i=1; i < imageFrames; ++i){
if(!reader.jumpToNextImage())
break;
reader.read(&image);
if(maxWidth < image.width()){
maxWidth = image.width();
maxIndex = i;
}
}
currentFrame = maxIndex;
reader.jumpToImage(maxIndex);
}
#ifdef TESTING_RAW
QImage ret = loadRawImage(filePath);
if (!ret.isNull()) {
image = ret;
imageFormat = "raw";
} else
#endif // TESTING_RAW
if (!reader.read(&image))
image = QImage(); // cannot read image
if (Config::autoRotateImage() && !image.isNull()
&& ImageHeader::isFormatSupport(imageFormat)) {
if (header->loadFile(imagePath) && header->hasOrientation())
header->autoRotateImage(image);
}
}
// if (isPreReading) { // will re-create these when animation start.
SafeDelete(movie);
SafeDelete(svgRender);
// }
if(image.isNull()){
imageFormat = "";
imageFrames = 0;
}
isReady = true;
}
#ifdef TESTING_RAW
QImage ImageWrapper::loadRawImage(const QString &filePath)
{
LibRaw *raw = new LibRaw;
QImage rawImage;
if (raw->open_file(filePath.toLocal8Bit()) != LIBRAW_SUCCESS) {
delete raw;
return rawImage;
}
QSize defaultSize = QSize(raw->imgdata.sizes.width,
raw->imgdata.sizes.height);
if (raw->imgdata.sizes.flip == 5 || raw->imgdata.sizes.flip == 6) {
defaultSize.transpose();
}
const libraw_data_t &imgdata = raw->imgdata;
libraw_processed_image_t *output;
qDebug("Decoding raw data");
raw->unpack();
raw->dcraw_process();
qDebug("after dcraw_process...");
output = raw->dcraw_make_mem_image();
QImage image;
uchar *pixels = 0;
if (output->type == LIBRAW_IMAGE_JPEG) {
qDebug("libraw_image_jpeg");
image.loadFromData(output->data, output->data_size, "JPEG");
if (imgdata.sizes.flip != 0) {
QTransform rotation;
int angle = 0;
if (imgdata.sizes.flip == 3) angle = 180;
else if (imgdata.sizes.flip == 5) angle = -90;
else if (imgdata.sizes.flip == 6) angle = 90;
if (angle != 0) {
rotation.rotate(angle);
image = image.transformed(rotation);
}
}
} else {
qDebug("get raw data, will convert");
int numPixels = output->width * output->height;
int colorSize = output->bits / 8;
int pixelSize = output->colors * colorSize;
pixels = new uchar[numPixels * 4];
uchar *data = output->data;
for (int i = 0; i < numPixels; i++, data += pixelSize) {
if (output->colors == 3) {
pixels[i * 4] = data[2 * colorSize];
pixels[i * 4 + 1] = data[1 * colorSize];
pixels[i * 4 + 2] = data[0];
} else {
pixels[i * 4] = data[0];
pixels[i * 4 + 1] = data[0];
pixels[i * 4 + 2] = data[0];
}
}
image = QImage(pixels,
output->width, output->height,
QImage::Format_RGB32);
}
rawImage = image;
if (output->type == LIBRAW_IMAGE_BITMAP) {
// make sure that the bits are copied
uchar *b = rawImage.bits();
Q_UNUSED(b);
}
raw->dcraw_clear_mem(output);
delete pixels;
delete raw;
return rawImage;
}
#endif // TESTING_RAW
/*! NOTE: QMovie use QTimer for changing frame, and QTimer muse start in the thread that it has been created.
* Since QTimer has parent(QMovie) and we couldn't use moveToThread with it (it is a d-pointer member of QMovie),
* so muse create the movie in main thread.
*/
void ImageWrapper::startAnimation()
{
qDebug("isAnimation, will start animation");
if (formatFlag & MOVIE_FLAG) {
if (!movie) {
movie = new QMovie(imagePath); // this movie was created in main thread.
movie->setFormat(imageFormat.toLocal8Bit());
}
if(movie->state() == QMovie::NotRunning)
movie->start();
connect(movie, SIGNAL(updated(QRect)), SIGNAL(animationUpdated()));
} else if (formatFlag & SVG_FLAG) {
if (!svgRender)
svgRender = new QSvgRenderer(imagePath); // this svgRender was created in main thread.
connect(svgRender, SIGNAL(repaintNeeded()), SLOT(updateSvgImage()));
}
}
void ImageWrapper::updateSvgImage()
{
// for (int i = 0; i < image.height(); ++i) {
// quint8 * line = image.scanLine(i);
// quint8 * end = line + image.width();
// while(line != end)
// *line++ = 0;
// }
// TODO: imporve perfermance
image = QImage(image.size(), QImage::Format_ARGB32);
QPainter painter(&image);
svgRender->render(&painter);
emit animationUpdated();
}
void ImageWrapper::switchAnimationPaused()
{
if(movie) {
switch(movie->state()){
case QMovie::Running:
movie->setPaused(true);
break;
case QMovie::Paused:
movie->setPaused(false);
break;
default:
break;
}
}
}
void ImageWrapper::nextAnimationFrame()
{
if(movie) {
switch(movie->state()){
case QMovie::Running:
movie->setPaused(true);
break;
case QMovie::Paused:
movie->jumpToNextFrame();
break;
default:
break;
}
} else if(imageFrames > 1 && imageFormat == "ico"){
QImageReader reader(imagePath);
reader.setDecideFormatFromContent(true);
reader.jumpToImage(currentFrame++);
if(!reader.jumpToNextImage()) {
currentFrame = 0;
reader.jumpToImage(0);
}
if (reader.read(&image)) {
imageAttribute = QString::null;
emit frameUpdated();
}
}
}
void ImageWrapper::setAnimationPaused(bool paused)
{
if(movie)
movie->setPaused(paused);
}
QString ImageWrapper::attribute()
{
if (!imageAttribute.isEmpty())
return imageAttribute;
QFileInfo fileInfo(imagePath);
if(fileInfo.exists()){
qint64 size = fileInfo.size();
imageAttribute += tr("File name: %1").arg(ToolKit::filename(imagePath));
imageAttribute += "<br>" + tr("File size: %1 (%2 bytes)")
.arg(ToolKit::fileSize2Str(size)).arg(size);
imageAttribute += "<br>" + tr("Created time: %1")
.arg(fileInfo.created().toString(tr("yyyy-MM-dd, hh:mm:ss")));
}
QImage curImage = currentImage();
if(!curImage.isNull()){
if(!imageAttribute.isEmpty())
imageAttribute += QString("<br>");
if(!imageFormat.isEmpty())
imageAttribute += tr("Image format: %1").arg(imageFormat);
int gcd = ToolKit::gcd(curImage.width(), curImage.height());
QString ratioStr = (gcd == 0) ? "1:1" : QString("%1:%2")
.arg(curImage.width() / gcd)
.arg(curImage.height() / gcd);
imageAttribute += "<br>" + tr("Image size: %1 x %2 (%3)")
.arg(curImage.width()).arg(curImage.height()).arg(ratioStr);
// if(curImage.colorCount() > 0) // indexed
// imageAttribute += "<br>" + tr("Color count: %1").arg(curImage.colorCount());
// else /*if(curImage.depth() >= 16)*/ // non-indexed (do not use color tables)
// imageAttribute += "<br>" + tr("Color count: true color");
// imageAttribute += "<br>" + tr("Depth: %1").arg(curImage.depth());
if(fileInfo.exists() && imageFrames > 1)
imageAttribute += "<br>" + tr("Frame count: %1").arg(imageFrames);
}
if (!curImage.isNull() && ImageHeader::isFormatSupport(imageFormat) && header->loadFile(imagePath)) {
if (header->isJpeg()) {
if (header->hasQuality()) // even if there is no exif tag, the quality info may also exist.
imageAttribute += "<br>" + tr("JPEG Quality: %1").arg(header->quality());
}
if (header->hasExif()) {
if (header->hasOrientation() && (header->orientation() != ImageHeader::NORMAL))
imageAttribute += "<br>" + tr("Orientation: %1").arg(header->orientationString());
if (header->hasSoftware())
imageAttribute += "<br>" + tr("Software: %1").arg(header->software());
if (header->hasMake())
imageAttribute += "<br>" + tr("Camera make: %1").arg(header->make());
if (header->hasModel())
imageAttribute += "<br>" + tr("Camera model: %1").arg(header->model());
if (header->hasDateTimeOriginal())
imageAttribute += "<br>" + tr("Original date/time: %1").arg(header->dateTimeOriginal());
if (header->hasFNumber())
imageAttribute += "<br>" + tr("F-stop: f/%1").arg(header->fNumber(), 0, 'f', 1);
if (header->hasExposureTime())
imageAttribute += "<br>" + tr("Exposure time: 1/%1 s").arg((unsigned) (1.0/header->exposureTime()));
if (header->hasISOSpeed())
imageAttribute += "<br>" + tr("ISO speed: ISO-%1").arg(header->ISOSpeed());
if (header->hasExposureBias())
imageAttribute += "<br>" + tr("Exposure bias: %1 EV").arg(header->exposureBias());
if (header->hasFocalLength())
imageAttribute += "<br>" + tr("Lens focal length: %1 mm").arg(header->focalLength());
// if (header->focalLengthIn35mm())
// imageAttribute += "<br>" + tr("35mm focal length: %1 mm").arg(header->focalLengthIn35mm());
if (header->hasMeteringMode())
imageAttribute += "<br>" + tr("Metering mode: %1").arg(header->meteringMode());
if (header->hasSubjectDistance())
imageAttribute += "<br>" + tr("Subject distance: %1 m").arg(header->subjectDistance());
if (header->hasFlash())
imageAttribute += "<br>" + tr("Flash mode: %1").arg(header->flashMode());
if (header->hasLightSource())
imageAttribute += "<br>" + tr("Light source: %1").arg(header->lightSource());
if (header->hasExposureProgram())
imageAttribute += "<br>" + tr("Exposure program: %1").arg(header->exposureProgram());
if (header->hasWhiteBalance())
imageAttribute += "<br>" + tr("White balance: %1").arg(header->whiteBalance());
if (header->hasGPSLatitude())
imageAttribute += "<br>" + tr("GPS Latitude: %1").arg(header->GPSLatitudeString());
if (header->hasGPSLongitude())
imageAttribute += "<br>" + tr("GPS Longitude: %1").arg(header->GPSLongitudeString());
if (header->hasGPSAltitude())
imageAttribute += "<br>" + tr("GPS Altitude: %1").arg(header->GPSAltitudeString());
}
}
return imageAttribute;
}