2
* This file is a part of Qtpfsgui package.
3
* ----------------------------------------------------------------------
4
* Copyright (C) 2006,2007 Giuseppe Rota
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
* ----------------------------------------------------------------------
21
* @author Giuseppe Rota <grota@users.sourceforge.net>
25
#include <QMessageBox>
27
#include <QCoreApplication>
28
#include "loadHdrThread.h"
29
#include "../Fileformat/pfstiff.h"
30
pfs::Frame* readEXRfile (const char * filename);
31
pfs::Frame* readRGBEfile (const char * filename);
33
LoadHdrThread::LoadHdrThread(QString fname, QString RecentDirHDRSetting, qtpfsgui_opts *opts) : QThread(0), fname(fname),RecentDirHDRSetting(RecentDirHDRSetting), qtpfsgui_options(opts) {
36
LoadHdrThread::~LoadHdrThread() {
40
void LoadHdrThread::run() {
45
if (!qfi.isReadable()) {
46
qDebug("File %s is not readable.", fname.toAscii().constData());
47
emit load_failed(tr("ERROR: The following file is not readable: %1").arg(fname));
50
// if the new dir, the one just chosen by the user, is different from the one stored in the settings, update the settings.
51
if (RecentDirHDRSetting != qfi.path() ) {
52
emit updateRecentDirHDRSetting(qfi.path());
54
pfs::Frame* hdrpfsframe = NULL;
55
QStringList rawextensions;
56
rawextensions << "CRW" << "CR2" << "NEF" << "DNG" << "MRW" << "ORF" << "KDC" << "DCR" << "ARW" << "RAF" << "PTX" << "PEF" << "X3F" << "RAW";
57
QString extension = qfi.suffix().toUpper();
58
bool rawinput = (rawextensions.indexOf(extension)!=-1);
60
if (extension=="EXR") {
61
hdrpfsframe = readEXRfile(qfi.filePath().toUtf8().constData());
62
} else if (extension=="HDR") {
63
hdrpfsframe = readRGBEfile(qfi.filePath().toUtf8().constData());
64
} else if (extension=="PFS") {
66
hdrpfsframe=pfsio.readFrame(qfi.filePath());
67
hdrpfsframe->convertXYZChannelsToRGB();
68
} else if (extension.startsWith("TIF")) {
69
TiffReader reader(qfi.filePath().toUtf8().constData());
70
hdrpfsframe = reader.readIntoPfsFrame(); //from 8,16,32,logluv to pfs::Frame
73
qDebug("TH: raw file");
74
if (!qtpfsgui_options->dcraw_options.contains("-T")) {
75
qDebug("TH: dcraw, -T parameter missing.");
76
emit load_failed(tr("ERROR: Tiff output for raw files currently disabled. Please add the \"-T\" option to the raw conversion parameters in the options panel."));
79
QProcess *rawconversion = new QProcess(0);
80
rawconversion->setWorkingDirectory(qtpfsgui_options->tempfilespath);
82
QString separator(";");
84
QString separator(":");
86
QStringList env = QProcess::systemEnvironment();
87
env.replaceInStrings(QRegExp("^PATH=(.*)", Qt::CaseInsensitive), "PATH=\\1"+separator+QCoreApplication::applicationDirPath());
88
rawconversion->setEnvironment(env);
90
QStringList params = qtpfsgui_options->dcraw_options;
94
rawconversion->start(QCoreApplication::applicationDirPath()+"/dcraw", params);
96
rawconversion->start("dcraw", params);
99
//blocking, timeout of 10 sec
100
if(!rawconversion->waitForStarted(10000)) {
101
qDebug("Cannot start dcraw on file: %s", qPrintable(fname));
102
emit load_failed(tr("ERROR: Cannot start dcraw on file: %1").arg(fname));
106
//blocking, timeout of 5mins
107
if(!rawconversion->waitForFinished(300000)) {
108
qDebug("Error or timeout occured while executing dcraw on file: %s", qPrintable(fname));
109
emit load_failed(tr("ERROR: Error or timeout occured while executing dcraw on file: %1").arg(fname));
113
QString outfname = QString(qfi.path() + "/"+qfi.completeBaseName()+".tiff");
114
qDebug("TH: Loading back file name=%s", qPrintable(outfname));
115
TiffReader reader(outfname.toUtf8().constData());
116
hdrpfsframe = reader.readIntoPfsFrame(); //from 8,16,32,logluv to pfs::Frame
117
QFile::remove(outfname);
118
} //raw file detected
120
qDebug("TH: File %s has unsupported extension.", qPrintable(fname));
121
emit load_failed(tr("ERROR: File %1 has unsupported extension.").arg(fname));
125
pfs::Channel *R,*G,*B;
126
hdrpfsframe->getRGBChannels( R, G, B );
127
float maxYval=-1; float minYval=1e6;
128
float maxRval=-1; float minRval=1e6;
129
float maxGval=-1; float minGval=1e6;
130
float maxBval=-1; float minBval=1e6;
131
for (int i=0; i<hdrpfsframe->getHeight()*hdrpfsframe->getWidth(); i++) {
132
float yval = 0.212656f*(*R)(i)+0.715158f*(*G)(i)+0.072186f*(*B)(i);
133
maxYval = (yval>maxYval) ? yval : maxYval;
134
minYval = (yval<minYval) ? yval : minYval;
135
maxRval = ((*R)(i)>maxRval) ? (*R)(i) : maxRval;
136
minRval = ((*R)(i)<minRval) ? (*R)(i) : minRval;
137
maxGval = ((*G)(i)>maxGval) ? (*G)(i) : maxGval;
138
minGval = ((*G)(i)<minGval) ? (*G)(i) : minGval;
139
maxBval = ((*B)(i)>maxBval) ? (*B)(i) : maxBval;
140
minBval = ((*B)(i)<minBval) ? (*B)(i) : minBval;
142
qDebug("minYval=%f, maxYval=%f",minYval,maxYval);
143
qDebug("minRval=%f, maxRval=%f",minRval,maxRval);
144
qDebug("minGval=%f, maxGval=%f",minGval,maxGval);
145
qDebug("minBval=%f, maxBval=%f",minBval,maxBval);
147
if (hdrpfsframe == NULL)
148
throw "Error loading file";
150
qDebug("TH: catched exception");
151
emit load_failed(tr("ERROR: Failed loading file: %1").arg(fname));
154
emit hdr_ready(hdrpfsframe,fname);