~ubuntu-branches/ubuntu/natty/qtpfsgui/natty

« back to all changes in this revision

Viewing changes to src/Threads/loadHdrThread.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-01-06 04:39:36 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080106043936-a9u9g7yih3w16ru5
Tags: 1.9.0-1
* New upstream release.
* Replace “COPYING” with “LICENSE” in the NOT_NEEDED variable of
  debian/rules, following upstream's renaming.
* Update debian/links accordingly.
* Delete the matching TODO item since there's no longer needed to have a
  patched (with HTML tags) license file to get a correct display in the
  “License agreement” tab.
* Update the gcc4.3 patch (drop the hunk touching src/Libpfs/pfs.cpp):
   - 20_gcc4.3_includes.
* Add a link from /usr/share/qtpfsgui/html to the HTML documentation
  under /usr/share/doc/qtpfsgui/html since the former is used at runtime
  to display the manual.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * This file is a part of Qtpfsgui package.
 
3
 * ---------------------------------------------------------------------- 
 
4
 * Copyright (C) 2006,2007 Giuseppe Rota
 
5
 * 
 
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.
 
10
 *
 
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.
 
15
 *
 
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
 * ---------------------------------------------------------------------- 
 
20
 *
 
21
 * @author Giuseppe Rota <grota@users.sourceforge.net>
 
22
 */
 
23
 
 
24
#include <QFileInfo>
 
25
#include <QMessageBox>
 
26
#include <QProcess>
 
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);
 
32
 
 
33
LoadHdrThread::LoadHdrThread(QString fname, QString RecentDirHDRSetting, qtpfsgui_opts *opts) : QThread(0), fname(fname),RecentDirHDRSetting(RecentDirHDRSetting), qtpfsgui_options(opts) {
 
34
}
 
35
 
 
36
LoadHdrThread::~LoadHdrThread() {
 
37
        wait();
 
38
}
 
39
 
 
40
void LoadHdrThread::run() {
 
41
        if( fname.isEmpty() )
 
42
                return;
 
43
 
 
44
        QFileInfo qfi(fname);
 
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));
 
48
                return;
 
49
        }
 
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());
 
53
        }
 
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);
 
59
        try {
 
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") {
 
65
                        pfs::DOMIO pfsio;
 
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
 
71
                }
 
72
                else if (rawinput) {
 
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."));
 
77
                        return;
 
78
                        }
 
79
                        QProcess *rawconversion = new QProcess(0);
 
80
                        rawconversion->setWorkingDirectory(qtpfsgui_options->tempfilespath);
 
81
                        #ifdef WIN32
 
82
                        QString separator(";");
 
83
                        #else
 
84
                        QString separator(":");
 
85
                        #endif
 
86
                        QStringList env = QProcess::systemEnvironment();
 
87
                        env.replaceInStrings(QRegExp("^PATH=(.*)", Qt::CaseInsensitive), "PATH=\\1"+separator+QCoreApplication::applicationDirPath());
 
88
                        rawconversion->setEnvironment(env);
 
89
                        
 
90
                        QStringList params = qtpfsgui_options->dcraw_options;
 
91
                        params << fname;
 
92
                        
 
93
                        #ifdef Q_WS_MAC
 
94
                        rawconversion->start(QCoreApplication::applicationDirPath()+"/dcraw", params);
 
95
                        #else
 
96
                        rawconversion->start("dcraw", params);
 
97
                        #endif
 
98
 
 
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));
 
103
                                return;
 
104
                        }
 
105
                        
 
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));
 
110
                                return;
 
111
                        }
 
112
 
 
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
 
119
                else {
 
120
                        qDebug("TH: File %s has unsupported extension.", qPrintable(fname));
 
121
                        emit load_failed(tr("ERROR: File %1 has unsupported extension.").arg(fname));
 
122
                        return;
 
123
                }
 
124
#if 0
 
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;
 
141
                }
 
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);
 
146
#endif
 
147
        if (hdrpfsframe == NULL)
 
148
                throw "Error loading file";
 
149
        } catch (...) {
 
150
                qDebug("TH: catched exception");
 
151
                emit load_failed(tr("ERROR: Failed loading file: %1").arg(fname));
 
152
                return;
 
153
        }
 
154
        emit hdr_ready(hdrpfsframe,fname);
 
155
}