209 if (!io->isReadable()) {
210 errFile <<
"Cannot read image contents";
214 if (!(io->peek(11) ==
"#?RADIANCE\n" || io->peek(7) ==
"#?RGBE\n")) {
215 errFile <<
"Invalid RGBE header!";
222 QByteArray line(
MAXLINE + 1, Qt::Uninitialized);
223 QByteArray rawFormat;
225 QByteArray rawExposure;
226 QByteArray rawHeaderInfo;
230 len = io->readLine(line.data(),
MAXLINE);
231 if (line.startsWith(
"# ")) {
232 rawHeaderInfo = line.mid(2, len - 2 - 1 );
233 }
else if (line.startsWith(
"GAMMA=")) {
234 rawGamma = line.mid(6, len - 6 - 1 );
235 }
else if (line.startsWith(
"EXPOSURE=")) {
236 rawExposure = line.mid(9, len - 9 - 1 );
237 }
else if (line.startsWith(
"FORMAT=")) {
238 rawFormat = line.mid(7, len - 7 - 1 );
240 }
while ((len > 0) && (line[0] !=
'\n'));
242 if (rawFormat !=
"32-bit_rle_rgbe") {
243 errFile <<
"Invalid RGBE format!";
247 const QString headerInfo = [&]() {
248 if (!rawHeaderInfo.isEmpty()) {
249 return QString(rawHeaderInfo).trimmed();
255 if (!rawGamma.isEmpty()) {
256 bool gammaOk =
false;
257 const float gammaTemp = QString(rawGamma).toFloat(&gammaOk);
259 d.m_gamma = gammaTemp;
262 if (!rawExposure.isEmpty()) {
263 bool exposureOk =
false;
264 const float expTemp = QString(rawExposure).toFloat(&exposureOk);
266 d.m_exposure = expTemp;
270 len = io->readLine(line.data(),
MAXLINE);
286 QRegularExpression resolutionRegExp(QStringLiteral(
"([+\\-][XY]) ([0-9]+) ([+\\-][XY]) ([0-9]+)\n"));
287 QRegularExpressionMatch match = resolutionRegExp.match(QString::fromLatin1(line));
288 if (!match.hasMatch()) {
289 errFile <<
"Invalid HDR file, the first line after the header didn't have the expected format:" << line;
293 if ((match.captured(1).at(1) !=
u'Y') || (match.captured(3).at(1) !=
u'X')) {
294 errFile <<
"Unsupported image orientation in HDR file.";
298 const int width = match.captured(4).toInt();
299 const int height = match.captured(2).toInt();
301 dbgFile <<
"RGBE image information:";
302 dbgFile <<
"Program info:" << headerInfo;
303 if (!rawGamma.isEmpty()) {
306 dbgFile <<
"No gamma metadata provided";
308 if (!rawExposure.isEmpty()) {
309 dbgFile <<
"Exposure:" <<
d.m_exposure;
311 dbgFile <<
"No exposure metadata provided";
313 dbgFile <<
"Dimension:" << width <<
"x" << height;
326 image =
new KisImage(document->createUndoStore(), width, height,
d.cs,
"RGBE image");
330 QDataStream stream(io);
334 errFile <<
"Error loading HDR file.";
341 document->setCurrentImage(image);