Skip to content

Commit

Permalink
Fix file name encoding issues with QString
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelaya committed Aug 26, 2014
1 parent b53f93f commit 7c6fa9e
Show file tree
Hide file tree
Showing 18 changed files with 85 additions and 78 deletions.
4 changes: 2 additions & 2 deletions DngFloatWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ enum {
} Constant;


void DngFloatWriter::write(Array2D<float> && rawPixels, const RawParameters & p, const string & filename) {
void DngFloatWriter::write(Array2D<float> && rawPixels, const RawParameters & p, const QString & filename) {
params = &p;
rawData = std::move(rawPixels);
width = rawData.getWidth();
height = rawData.getHeight();

renderPreviews();

file.open(filename, ios_base::binary);
file.open(filename.toLocal8Bit().constData(), ios_base::binary);
createMainIFD();
subIFDoffsets[0] = 8 + mainIFD.length();
createRawIFD();
Expand Down
4 changes: 2 additions & 2 deletions DngFloatWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#define _DNGFLOATWRITER_HPP_

#include <fstream>
#include <string>
#include <QString>
#include <QImage>
#include "config.h"
#include "Array2D.hpp"
Expand All @@ -45,7 +45,7 @@ class DngFloatWriter {
bps = b;
}
void setPreview(const QImage & p);
void write(Array2D<float> && rawPixels, const RawParameters & p, const std::string & filename);
void write(Array2D<float> && rawPixels, const RawParameters & p, const QString & filename);

private:
uint32_t previewWidth;
Expand Down
8 changes: 4 additions & 4 deletions DngPropertiesDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ DngPropertiesDialog::DngPropertiesDialog(QWidget * parent, Qt::WindowFlags f)
"- %of " + trHelp("Replaced by the base file name of the output file.") + "\n" +
"- %od " + trHelp("Replaced by the directory name of the output file.") + "\n" +
"- %%: " + trHelp("Replaced by a single %.") + "\n");
maskFileEditor->setText(maskFileName.c_str());
maskFileEditor->setText(maskFileName);
QPushButton * showFileDialog = new QPushButton("...", maskFileSelector);
connect(showFileDialog, SIGNAL(clicked(bool)), this, SLOT(setMaskFileName()));
maskFileSelectorLayout->addWidget(maskFileEditor);
Expand Down Expand Up @@ -130,13 +130,13 @@ DngPropertiesDialog::DngPropertiesDialog(QWidget * parent, Qt::WindowFlags f)

void DngPropertiesDialog::accept() {
saveMask = maskFileSelector->isEnabled();
maskFileName = QDir::toNativeSeparators(maskFileEditor->text()).toUtf8().constData();
maskFileName = QDir::toNativeSeparators(maskFileEditor->text()).toLocal8Bit().constData();
if (saveOptions->isChecked()) {
QSettings settings;
settings.setValue("bps", bps);
settings.setValue("previewSize", previewSize);
settings.setValue("saveMask", saveMask);
settings.setValue("maskFileName", maskFileName.c_str());
settings.setValue("maskFileName", maskFileName);
settings.setValue("featherRadius", featherRadius);
}
QDialog::accept();
Expand All @@ -148,7 +148,7 @@ void DngPropertiesDialog::loadDefaultOptions() {
bps = settings.value("bps", 16).toInt();
previewSize = settings.value("previewSize", 2).toInt();
saveMask = settings.value("saveMask", false).toBool();
maskFileName = settings.value("maskFileName", "%od/%of_mask.png").toString().toUtf8().constData();
maskFileName = settings.value("maskFileName", "%od/%of_mask.png").toString().toLocal8Bit().constData();
featherRadius = settings.value("featherRadius", 3).toInt();
}

Expand Down
10 changes: 5 additions & 5 deletions ExifTransfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ using namespace std;

class ExifTransfer {
public:
ExifTransfer(const std::string & srcFile, const std::string & dstFile) : srcFile(srcFile), dstFile(dstFile) {}
ExifTransfer(const QString & srcFile, const QString & dstFile) : srcFile(srcFile), dstFile(dstFile) {}

void copyMetadata();

private:
std::string srcFile, dstFile;
QString srcFile, dstFile;
Exiv2::Image::AutoPtr src, dst;

void copyXMP();
Expand All @@ -44,17 +44,17 @@ class ExifTransfer {
};


void hdrmerge::Exif::transfer(const std::string & srcFile, const std::string & dstFile) {
void hdrmerge::Exif::transfer(const QString & srcFile, const QString & dstFile) {
ExifTransfer exif(srcFile, dstFile);
exif.copyMetadata();
}


void ExifTransfer::copyMetadata() {
try {
src = Exiv2::ImageFactory::open(srcFile);
src = Exiv2::ImageFactory::open(srcFile.toLocal8Bit().constData());
src->readMetadata();
dst = Exiv2::ImageFactory::open(dstFile);
dst = Exiv2::ImageFactory::open(dstFile.toLocal8Bit().constData());
dst->readMetadata();
copyXMP();
copyIPTC();
Expand Down
4 changes: 2 additions & 2 deletions ExifTransfer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
#ifndef _EXIFTRANSFER_HPP_
#define _EXIFTRANSFER_HPP_

#include <string>
#include <QString>

namespace hdrmerge {

namespace Exif {
void transfer(const std::string & srcFile, const std::string & dstFile);
void transfer(const QString & srcFile, const QString & dstFile);
}

}
Expand Down
34 changes: 17 additions & 17 deletions ImageIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ using namespace hdrmerge;
Image ImageIO::loadRawImage(RawParameters & rawParameters) {
LibRaw rawProcessor;
auto & d = rawProcessor.imgdata;
if (rawProcessor.open_file(rawParameters.fileName.c_str()) == LIBRAW_SUCCESS) {
if (rawProcessor.open_file(rawParameters.fileName.toLocal8Bit().constData()) == LIBRAW_SUCCESS) {
libraw_decoder_info_t decoder_info;
rawProcessor.get_decoder_info(&decoder_info);
if(!decoder_info.decoder_flags & LIBRAW_DECODER_FLATFIELD) {
Expand All @@ -56,10 +56,10 @@ Image ImageIO::loadRawImage(RawParameters & rawParameters) {
}


ImageIO::QDateInterval ImageIO::getImageCreationInterval(const std::string & fileName) {
ImageIO::QDateInterval ImageIO::getImageCreationInterval(const QString & fileName) {
LibRaw rawProcessor;
QDateInterval result;
if (rawProcessor.open_file(fileName.c_str()) == LIBRAW_SUCCESS) {
if (rawProcessor.open_file(fileName.toLocal8Bit().constData()) == LIBRAW_SUCCESS) {
result.end = QDateTime::fromTime_t(rawProcessor.imgdata.other.timestamp);
result.start = result.end.addMSecs(-rawProcessor.imgdata.other.shutter * 1000.0);
}
Expand All @@ -79,9 +79,9 @@ int ImageIO::load(const LoadOptions & options, ProgressIndicator & progress) {
#pragma omp parallel for schedule(dynamic)
for (int i = 0; i < numImages; ++i) {
if (!error) { // We cannot break from the for loop if we are using OpenMP
string name = options.fileNames[i];
QString name = options.fileNames[i];
#pragma omp critical
progress.advance(p += step, "Loading %1", name.c_str());
progress.advance(p += step, "Loading %1", name.toLocal8Bit().constData());
unique_ptr<RawParameters> params(new RawParameters(name));
Image image = loadRawImage(*params);
#pragma omp critical
Expand Down Expand Up @@ -149,13 +149,13 @@ int ImageIO::save(const SaveOptions & options, ProgressIndicator & progress) {
progress.advance(100, "Done writing!");

if (options.saveMask) {
string name = replaceArguments(options.maskFileName, options.fileName);
QString name = replaceArguments(options.maskFileName, options.fileName);
writeMaskImage(name);
}
}


void ImageIO::writeMaskImage(const std::string & maskFile) {
void ImageIO::writeMaskImage(const QString & maskFile) {
Log::debug("Saving mask to ", maskFile);
EditableMask & mask = stack.getMask();
QImage maskImage(mask.getWidth(), mask.getHeight(), QImage::Format_Indexed8);
Expand All @@ -170,7 +170,7 @@ void ImageIO::writeMaskImage(const std::string & maskFile) {
maskImage.setPixel(x, y, mask[pos]);
}
}
if (!maskImage.save(QString(maskFile.c_str()))) {
if (!maskImage.save(maskFile)) {
Log::progress("Cannot save mask image to ", maskFile);
}
}
Expand Down Expand Up @@ -210,7 +210,7 @@ QImage ImageIO::renderPreview(const Array2D<float> & rawData, const RawParameter
d.params.exp_correc = 1;
d.params.exp_shift = expShift;
d.params.exp_preser = 1.0;
if (rawProcessor.open_file(params.fileName.c_str()) == LIBRAW_SUCCESS) {
if (rawProcessor.open_file(params.fileName.toLocal8Bit().constData()) == LIBRAW_SUCCESS) {
// && rawProcessor.unpack() == LIBRAW_SUCCESS) {
prepareRawBuffer(rawProcessor);
// Assume the other sizes are the same as in the raw parameters
Expand Down Expand Up @@ -254,7 +254,7 @@ class FileNameManipulator {
FileNameManipulator(const vector<unique_ptr<RawParameters>> & paramList) {
names.reserve(paramList.size());
for (auto & rp : paramList) {
names.push_back(rp->fileName.c_str());
names.push_back(rp->fileName);
}
sort(names.begin(), names.end());
}
Expand Down Expand Up @@ -301,7 +301,7 @@ class FileNameManipulator {
};


string ImageIO::buildOutputFileName() const {
QString ImageIO::buildOutputFileName() const {
if (rawParameters.size() > 1)
return replaceArguments("%id[-1]/%iF[0]-%in[-1].dng", "");
else
Expand All @@ -310,12 +310,12 @@ string ImageIO::buildOutputFileName() const {


QString ImageIO::getInputPath() const {
return FileNameManipulator::getDirName(QString(rawParameters[0]->fileName.c_str()));
return FileNameManipulator::getDirName(rawParameters[0]->fileName);
}


string ImageIO::replaceArguments(const string & pattern, const string & outFileName) const {
QString result(pattern.c_str());
QString ImageIO::replaceArguments(const QString & pattern, const QString & outFileName) const {
QString result(pattern);
QRegExp re;
if (outFileName == "") {
re = QRegExp("%(?:i[fFdn]\\[(-?[0-9]+)\\]|%)");
Expand All @@ -331,9 +331,9 @@ string ImageIO::replaceArguments(const string & pattern, const string & outFileN
result.replace(index, 2, '%');
} else if (token[1] == 'o') {
if (token[2] == 'f') {
result.replace(index, 3, fnm.getBaseName(outFileName.c_str()));
result.replace(index, 3, fnm.getBaseName(outFileName));
} else {
result.replace(index, 3, fnm.getDirName(outFileName.c_str()));
result.replace(index, 3, fnm.getDirName(outFileName));
}
} else { // 'i'
int imageIndex = re.cap(1).toInt();
Expand All @@ -350,5 +350,5 @@ string ImageIO::replaceArguments(const string & pattern, const string & outFileN
}
index++;
}
return result.toUtf8().constData();
return result;
}
9 changes: 5 additions & 4 deletions ImageIO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <vector>
#include <QImage>
#include <QDateTime>
#include <QString>
#include "ImageStack.hpp"
#include "ProgressIndicator.hpp"
#include "LoadSaveOptions.hpp"
Expand All @@ -47,9 +48,9 @@ class ImageIO {
return stack;
}

std::string buildOutputFileName() const;
QString buildOutputFileName() const;
QString getInputPath() const;
std::string replaceArguments(const std::string & pattern, const std::string & outFileName) const;
QString replaceArguments(const QString & pattern, const QString & outFileName) const;
static Image loadRawImage(RawParameters & rawParameters);
static QImage renderPreview(const Array2D<float> & rawData, const RawParameters & rawParameters, float expShift);

Expand All @@ -62,13 +63,13 @@ class ImageIO {
return end.msecsTo(r.start) / 1000.0;
}
};
static QDateInterval getImageCreationInterval(const std::string & fileName);
static QDateInterval getImageCreationInterval(const QString & fileName);

private:
ImageStack stack;
std::vector<std::unique_ptr<RawParameters>> rawParameters;

void writeMaskImage(const std::string & maskFile);
void writeMaskImage(const QString & maskFile);
};

} // namespace hdrmerge
Expand Down
18 changes: 9 additions & 9 deletions Launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ struct CoutProgressIndicator : public ProgressIndicator {

list<LoadOptions> Launcher::getBracketedSets() {
list<LoadOptions> result;
list<pair<ImageIO::QDateInterval, string>> dateNames;
for (string & name : generalOptions.fileNames) {
list<pair<ImageIO::QDateInterval, QString>> dateNames;
for (QString & name : generalOptions.fileNames) {
ImageIO::QDateInterval interval = ImageIO::getImageCreationInterval(name);
if (interval.start.isValid()) {
dateNames.emplace_back(interval, name);
Expand Down Expand Up @@ -122,24 +122,24 @@ int Launcher::automaticMerge() {
int format = result & 1;
int i = result >> 1;
if (format) {
cerr << tr("Error loading %1, it has a different format.").arg(options.fileNames[i].c_str()) << endl;
cerr << tr("Error loading %1, it has a different format.").arg(options.fileNames[i]) << endl;
} else {
cerr << tr("Error loading %1, file not found.").arg(options.fileNames[i].c_str()) << endl;
cerr << tr("Error loading %1, file not found.").arg(options.fileNames[i]) << endl;
}
result = 1;
continue;
}
SaveOptions setOptions = saveOptions;
if (!setOptions.fileName.empty()) {
if (!setOptions.fileName.isEmpty()) {
setOptions.fileName = io.replaceArguments(setOptions.fileName, "");
size_t extPos = setOptions.fileName.find_last_of('.');
if (extPos > setOptions.fileName.length() || setOptions.fileName.substr(extPos) != ".dng") {
size_t extPos = setOptions.fileName.lastIndexOf('.');
if (extPos > setOptions.fileName.length() || setOptions.fileName.mid(extPos) != ".dng") {
setOptions.fileName += ".dng";
}
} else {
setOptions.fileName = io.buildOutputFileName();
}
Log::progress(tr("Writing result to %1").arg(setOptions.fileName.c_str()));
Log::progress(tr("Writing result to %1").arg(setOptions.fileName));
io.save(setOptions, progress);
}
return result;
Expand Down Expand Up @@ -209,7 +209,7 @@ void Launcher::parseCommandLine() {
}
}
} else if (argv[i][0] != '-') {
generalOptions.fileNames.push_back(argv[i]);
generalOptions.fileNames.push_back(QString::fromLocal8Bit(argv[i]));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions LoadOptionsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void LoadOptionsDialog::showEvent(QShowEvent * event) {
addFiles();
} else {
for (auto & i : fileNames) {
new FileItem(i.c_str(), fileList);
new FileItem(i, fileList);
}
fileNames.clear();
}
Expand Down Expand Up @@ -154,7 +154,7 @@ void LoadOptionsDialog::accept() {
crop = cropBox->isChecked();
settings.setValue("cropOnLoad", crop);
for (int i = 0; i < fileList->count(); ++i) {
fileNames.push_back(fileList->item(i)->data(Qt::UserRole).toString().toUtf8().constData());
fileNames.push_back(fileList->item(i)->data(Qt::UserRole).toString());
}
QDialog::accept();
}
Expand Down
8 changes: 4 additions & 4 deletions LoadSaveOptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
#define _LOADSAVEOPTIONS_H_

#include <vector>
#include <string>
#include <QString>

namespace hdrmerge {

struct LoadOptions {
std::vector<std::string> fileNames;
std::vector<QString> fileNames;
bool align;
bool crop;
bool batch;
Expand All @@ -41,9 +41,9 @@ struct LoadOptions {
struct SaveOptions {
int bps;
int previewSize;
std::string fileName;
QString fileName;
bool saveMask;
std::string maskFileName;
QString maskFileName;
int featherRadius;
SaveOptions() : bps(16), previewSize(0), saveMask(false), featherRadius(3) {}
};
Expand Down
2 changes: 1 addition & 1 deletion Log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class Log {


inline std::ostream & operator<<(std::ostream & os, const QString & s) {
return os << std::string(s.toUtf8().constData());
return os << std::string(s.toLocal8Bit().constData());
}


Expand Down
Loading

0 comments on commit 7c6fa9e

Please sign in to comment.