Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Extract RGBE functions into a static library
Dmitry Kazakov36931106580b
diff --git a/plugins/impex/rgbe/CMakeLists.txt b/plugins/impex/rgbe/CMakeLists.txtindex 8e7630f858..f0d616e6ab 100644--- a/plugins/impex/rgbe/CMakeLists.txt+++ b/plugins/impex/rgbe/CMakeLists.txt@@ -1,11 +1,14 @@add_subdirectory(tests)+add_library(kritargbeimportutils STATIC RGBEImportUtils.cpp)+target_link_libraries(kritargbeimportutils PUBLIC kritaimage)+set(kritargbeimport_SOURCESRGBEImport.cpp)add_library(kritargbeimport MODULE ${kritargbeimport_SOURCES})-target_link_libraries(kritargbeimport kritaui)+target_link_libraries(kritargbeimport kritaui kritargbeimportutils)install(TARGETS kritargbeimport DESTINATION ${KRITA_PLUGIN_INSTALL_DIR})diff --git a/plugins/impex/rgbe/RGBEImport.cpp b/plugins/impex/rgbe/RGBEImport.cppindex 2072a7d707..fa261f185d 100644--- a/plugins/impex/rgbe/RGBEImport.cpp+++ b/plugins/impex/rgbe/RGBEImport.cpp@@ -35,9 +35,12 @@#include <kis_sequential_iterator.h>#include "RGBEImport.h"+#include "RGBEImportUtils.h"K_PLUGIN_FACTORY_WITH_JSON(KisRGBEImportFactory, "krita_rgbe_import.json", registerPlugin<RGBEImport>();)+#define MAXLINE 1024+class Q_DECL_HIDDEN RGBEImportData{public:@@ -49,174 +52,6 @@ public:const KoColorSpace *cs = nullptr;};-namespace RGBEIMPORT-{-#define MAXLINE 1024-#define MINELEN 8 // minimum scanline length for encoding-#define MAXELEN 0x7fff // maximum scanline length for encoding--// read an old style line from the hdr image file-// if 'first' is true the first byte is already read-static bool ReadOldLine(quint8 *image, int width, QDataStream &s)-{- int rshift = 0;- int i;-- while (width > 0) {- s >> image[0];- s >> image[1];- s >> image[2];- s >> image[3];-- if (s.atEnd()) {- return false;- }-- if ((image[0] == 1) && (image[1] == 1) && (image[2] == 1)) {- const int length = image[3] << rshift;- if (length > width) {- dbgFile << "Broken file detected: cannot duplicate pixels past image bounds!";- return false;- }- for (i = length; i > 0; i--) {- memcpy(image, image-4, 4);- image += 4;- width--;- }- rshift += 8;- } else {- image += 4;- width--;- rshift = 0;- }- }- return true;-}--static void RGBEToPaintDevice(quint8 *image, int width, KisSequentialIterator &it)-{- for (int j = 0; j < width; j++) {- it.nextPixel();- auto *dst = reinterpret_cast<float *>(it.rawData());-- if (image[3]) {- const float v = std::ldexp(1.0f, int(image[3]) - (128 + 8));- const float pixelData[4] = {float(image[0]) * v,- float(image[1]) * v,- float(image[2]) * v,- 1.0f};- memcpy(dst, pixelData, 4 * sizeof(float));- } else {- // Zero exponent handle- const float pixelData[4] = {0.0f, 0.0f, 0.0f, 1.0f};- memcpy(dst, pixelData, 4 * sizeof(float));- }-- image += 4;- }-}--// Load the HDR image.-static bool LoadHDR(QDataStream &s, const int width, const int height, KisSequentialIterator &it)-{- quint8 val;- quint8 code;-- QByteArray lineArray;- lineArray.resize(4 * width);- quint8 *image = (quint8 *)lineArray.data();-- for (int cline = 0; cline < height; cline++) {- // determine scanline type- if ((width < MINELEN) || (MAXELEN < width)) {- if (!ReadOldLine(image, width, s)) {- return false;- }- RGBEToPaintDevice(image, width, it);- continue;- }-- s >> val;-- if (s.atEnd()) {- return true;- }-- if (val != 2) {- s.device()->ungetChar(val);- if (!ReadOldLine(image, width, s)) {- return false;- }- RGBEToPaintDevice(image, width, it);- continue;- }-- s >> image[1];- s >> image[2];- s >> image[3];-- if (s.atEnd()) {- return true;- }-- if ((image[1] != 2) || (image[2] & 128)) {- image[0] = 2;- if (!ReadOldLine(image + 4, width - 1, s)) {- return false;- }- RGBEToPaintDevice(image, width, it);- continue;- }-- if ((image[2] << 8 | image[3]) != width) {- dbgFile << "Line of pixels had width" << (image[2] << 8 | image[3]) << "instead of" << width;- return false;- }-- // read each component- for (int i = 0; i < 4; i++) {- for (int j = 0; j < width;) {- s >> code;- if (s.atEnd()) {- dbgFile << "Truncated HDR file";- return false;- }- if (code > 128) {- // run- code &= 127;- s >> val;- if (j + code - 1 >= width) {- dbgFile << "Broken file detected: cannot duplicate data past image bounds!";- return false;- }- while (code != 0) {- image[i + j * 4] = val;- j++;- code--;- }- } else {- // non-run- if (j + code - 1 >= width) {- dbgFile << "Broken file detected: cannot extract data past image bounds!";- return false;- }- while (code != 0) {- s >> image[i + j * 4];- j++;- code--;- }- }- }- }-- RGBEToPaintDevice(image, width, it);- }-- return true;-}--} // namespace RGBEIMPORT-RGBEImport::RGBEImport(QObject *parent, const QVariantList &): KisImportExportFilter(parent){diff --git a/plugins/impex/rgbe/RGBEImportUtils.cpp b/plugins/impex/rgbe/RGBEImportUtils.cppnew file mode 100644index 0000000000..6730f88326--- /dev/null+++ b/plugins/impex/rgbe/RGBEImportUtils.cpp@@ -0,0 +1,183 @@+/*+ * SPDX-FileCopyrightText: 2023 Rasyuqa A. H. <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ *+ * Based on KImageFormats Radiance HDR loader+ *+ * SPDX-FileCopyrightText: 2005 Christoph Hormann <[email protected]>+ * SPDX-FileCopyrightText: 2005 Ignacio Castaño <[email protected]>+ *+ * SPDX-License-Identifier: LGPL-2.0-or-later+ */++#include <QDataStream>+#include <QIODevice>+#include <kis_sequential_iterator.h>++namespace RGBEIMPORT+{+#define MINELEN 8 // minimum scanline length for encoding+#define MAXELEN 0x7fff // maximum scanline length for encoding++// read an old style line from the hdr image file+// if 'first' is true the first byte is already read+bool ReadOldLine(quint8 *image, int width, QDataStream &s)+{+ int rshift = 0;+ int i;++ while (width > 0) {+ s >> image[0];+ s >> image[1];+ s >> image[2];+ s >> image[3];++ if (s.atEnd()) {+ return false;+ }++ if ((image[0] == 1) && (image[1] == 1) && (image[2] == 1)) {+ const int length = image[3] << rshift;+ if (length > width) {+ dbgFile << "Broken file detected: cannot duplicate pixels past image bounds!";+ return false;+ }+ for (i = length; i > 0; i--) {+ memcpy(image, image-4, 4);+ image += 4;+ width--;+ }+ rshift += 8;+ } else {+ image += 4;+ width--;+ rshift = 0;+ }+ }+ return true;+}++void RGBEToPaintDevice(quint8 *image, int width, KisSequentialIterator &it)+{+ for (int j = 0; j < width; j++) {+ it.nextPixel();+ auto *dst = reinterpret_cast<float *>(it.rawData());++ if (image[3]) {+ const float v = std::ldexp(1.0f, int(image[3]) - (128 + 8));+ const float pixelData[4] = {float(image[0]) * v,+ float(image[1]) * v,+ float(image[2]) * v,+ 1.0f};+ memcpy(dst, pixelData, 4 * sizeof(float));+ } else {+ // Zero exponent handle+ const float pixelData[4] = {0.0f, 0.0f, 0.0f, 1.0f};+ memcpy(dst, pixelData, 4 * sizeof(float));+ }++ image += 4;+ }+}++// Load the HDR image.+bool LoadHDR(QDataStream &s, const int width, const int height, KisSequentialIterator &it)+{+ quint8 val;+ quint8 code;++ QByteArray lineArray;+ lineArray.resize(4 * width);+ quint8 *image = (quint8 *)lineArray.data();++ for (int cline = 0; cline < height; cline++) {+ // determine scanline type+ if ((width < MINELEN) || (MAXELEN < width)) {+ if (!ReadOldLine(image, width, s)) {+ return false;+ }+ RGBEToPaintDevice(image, width, it);+ continue;+ }++ s >> val;++ if (s.atEnd()) {+ return true;+ }++ if (val != 2) {+ s.device()->ungetChar(val);+ if (!ReadOldLine(image, width, s)) {+ return false;+ }+ RGBEToPaintDevice(image, width, it);+ continue;+ }++ s >> image[1];+ s >> image[2];+ s >> image[3];++ if (s.atEnd()) {+ return true;+ }++ if ((image[1] != 2) || (image[2] & 128)) {+ image[0] = 2;+ if (!ReadOldLine(image + 4, width - 1, s)) {+ return false;+ }+ RGBEToPaintDevice(image, width, it);+ continue;+ }++ if ((image[2] << 8 | image[3]) != width) {+ dbgFile << "Line of pixels had width" << (image[2] << 8 | image[3]) << "instead of" << width;+ return false;+ }++ // read each component+ for (int i = 0; i < 4; i++) {+ for (int j = 0; j < width;) {+ s >> code;+ if (s.atEnd()) {+ dbgFile << "Truncated HDR file";+ return false;+ }+ if (code > 128) {+ // run+ code &= 127;+ s >> val;+ if (j + code - 1 >= width) {+ dbgFile << "Broken file detected: cannot duplicate data past image bounds!";+ return false;+ }+ while (code != 0) {+ image[i + j * 4] = val;+ j++;+ code--;+ }+ } else {+ // non-run+ if (j + code - 1 >= width) {+ dbgFile << "Broken file detected: cannot extract data past image bounds!";+ return false;+ }+ while (code != 0) {+ s >> image[i + j * 4];+ j++;+ code--;+ }+ }+ }+ }++ RGBEToPaintDevice(image, width, it);+ }++ return true;+}++} // namespace RGBEIMPORTdiff --git a/plugins/impex/rgbe/RGBEImportUtils.h b/plugins/impex/rgbe/RGBEImportUtils.hnew file mode 100644index 0000000000..e509fa1519--- /dev/null+++ b/plugins/impex/rgbe/RGBEImportUtils.h@@ -0,0 +1,18 @@+/*+ * SPDX-FileCopyrightText: 2023 Rasyuqa A. H. <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */++#ifndef RGBE_IMPORT_UTILS_H_+#define RGBE_IMPORT_UTILS_H_++#include <kis_sequential_iterator.h>++class QDataStream;++namespace RGBEIMPORT {+ bool LoadHDR(QDataStream &s, const int width, const int height, KisSequentialIterator &it);+}++#endif // RGBE_IMPORT_UTILS_H_