59 if (!io->isReadable()) {
60 errFile <<
"Cannot read image contents";
64 if (!(io->peek(11) ==
"#?RADIANCE\n" || io->peek(7) ==
"#?RGBE\n")) {
65 errFile <<
"Invalid RGBE header!";
72 QByteArray line(
MAXLINE + 1, Qt::Uninitialized);
75 QByteArray rawExposure;
76 QByteArray rawHeaderInfo;
80 len = io->readLine(line.data(),
MAXLINE);
81 if (line.startsWith(
"# ")) {
82 rawHeaderInfo = line.mid(2, len - 2 - 1 );
83 }
else if (line.startsWith(
"GAMMA=")) {
84 rawGamma = line.mid(6, len - 6 - 1 );
85 }
else if (line.startsWith(
"EXPOSURE=")) {
86 rawExposure = line.mid(9, len - 9 - 1 );
87 }
else if (line.startsWith(
"FORMAT=")) {
88 rawFormat = line.mid(7, len - 7 - 1 );
90 }
while ((len > 0) && (line[0] !=
'\n'));
92 if (rawFormat !=
"32-bit_rle_rgbe") {
93 errFile <<
"Invalid RGBE format!";
97 const QString headerInfo = [&]() {
98 if (!rawHeaderInfo.isEmpty()) {
99 return QString(rawHeaderInfo).trimmed();
105 if (!rawGamma.isEmpty()) {
106 bool gammaOk =
false;
107 const float gammaTemp = QString(rawGamma).toFloat(&gammaOk);
109 d.m_gamma = gammaTemp;
112 if (!rawExposure.isEmpty()) {
113 bool exposureOk =
false;
114 const float expTemp = QString(rawExposure).toFloat(&exposureOk);
116 d.m_exposure = expTemp;
120 len = io->readLine(line.data(),
MAXLINE);
136 QRegularExpression resolutionRegExp(QStringLiteral(
"([+\\-][XY]) ([0-9]+) ([+\\-][XY]) ([0-9]+)\n"));
137 QRegularExpressionMatch match = resolutionRegExp.match(QString::fromLatin1(line));
138 if (!match.hasMatch()) {
139 errFile <<
"Invalid HDR file, the first line after the header didn't have the expected format:" << line;
143 if ((match.captured(1).at(1) !=
u'Y') || (match.captured(3).at(1) !=
u'X')) {
144 errFile <<
"Unsupported image orientation in HDR file.";
148 const int width = match.captured(4).toInt();
149 const int height = match.captured(2).toInt();
151 dbgFile <<
"RGBE image information:";
152 dbgFile <<
"Program info:" << headerInfo;
153 if (!rawGamma.isEmpty()) {
156 dbgFile <<
"No gamma metadata provided";
158 if (!rawExposure.isEmpty()) {
159 dbgFile <<
"Exposure:" <<
d.m_exposure;
161 dbgFile <<
"No exposure metadata provided";
163 dbgFile <<
"Dimension:" << width <<
"x" << height;
176 image =
new KisImage(document->createUndoStore(), width, height,
d.cs,
"RGBE image");
180 QDataStream stream(io);
184 errFile <<
"Error loading HDR file.";
191 document->setCurrentImage(image);