Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_heightmap_import.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2014 Boudewijn Rempt <boud@valdyas.org>
3 * SPDX-FileCopyrightText: 2017 Victor Wåhlström <victor.wahlstrom@initiali.se>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-or-later
6 */
7
9
10#include <ctype.h>
11
12#include <QApplication>
13#include <qendian.h>
14
15#include <kpluginfactory.h>
16#include <KoDialog.h>
17
21#include <KoColorSpace.h>
22#include <KoColorSpaceTraits.h>
23
24#include <kis_debug.h>
25#include <KisDocument.h>
26#include <kis_group_layer.h>
27#include <kis_image.h>
28#include <kis_paint_layer.h>
29#include <kis_paint_device.h>
30#include <kis_transaction.h>
31#include <kis_iterator_ng.h>
33#include <kis_config.h>
35
37#include "kis_heightmap_utils.h"
38
39K_PLUGIN_FACTORY_WITH_JSON(HeightMapImportFactory, "krita_heightmap_import.json", registerPlugin<KisHeightMapImport>();)
40
41template<typename T>
42void fillData(KisPaintDeviceSP pd, int w, int h, QDataStream &stream) {
44
45 T pixel;
46
47 for (int i = 0; i < h; ++i) {
49 do {
50 stream >> pixel;
51 KoGrayTraits<T>::setGray(it->rawData(), pixel);
53 } while(it->nextPixel());
54 }
55}
56
57KisHeightMapImport::KisHeightMapImport(QObject *parent, const QVariantList &) : KisImportExportFilter(parent)
58{
59}
60
64
66{
67 Q_UNUSED(configuration);
69 if (depthId.id().isNull()) {
70 document->setErrorMessage(i18n("Unknown file type"));
72 }
73
74 int w = 0;
75 int h = 0;
76
77 KIS_ASSERT(io->isOpen());
78 const quint64 size = io->size();
79 if (size == 0) {
81 }
82
83 QDataStream::ByteOrder bo = QDataStream::LittleEndian;
84
85 if (!batchMode()) {
86 KisCursorOverrideHijacker cursorHijacker;
87
88 KoDialog* kdb = new KoDialog(qApp->activeWindow());
89 kdb->setWindowTitle(i18n("Heightmap Import Options"));
91
93
94 kdb->setMainWidget(wdg);
95
96 connect(wdg, SIGNAL(statusUpdated(bool)), kdb, SLOT(enableButtonOk(bool)));
97
98 KisConfig config(true);
99
100 QString filterConfig = config.importConfiguration(mimeType());
102 cfg->fromXML(filterConfig);
103
104
105 int endianness = cfg->getInt("endianness", 1);
106 if (endianness == 0) {
107 wdg->radioBig->setChecked(true);
108 }
109 else {
110 wdg->radioLittle->setChecked(true);
111 }
112
113 wdg->fileSizeLabel->setText(QString::number(size));
114
115 if(depthId == Integer8BitsColorDepthID) {
116 wdg->bppLabel->setText(QString::number(8));
117 wdg->typeLabel->setText("Integer");
118 }
119 else if(depthId == Integer16BitsColorDepthID) {
120 wdg->bppLabel->setText(QString::number(16));
121 wdg->typeLabel->setText("Integer");
122 }
123 else if(depthId == Float32BitsColorDepthID) {
124 wdg->bppLabel->setText(QString::number(32));
125 wdg->typeLabel->setText("Float");
126 }
127 else {
130 }
131
132 if (kdb->exec() == QDialog::Rejected) {
134 }
135
136 cfg->setProperty("endianness", wdg->radioBig->isChecked() ? 0 : 1);
137
138 config.setImportConfiguration(mimeType(), cfg);
139
140 w = wdg->widthInput->value();
141 h = wdg->heightInput->value();
142
143 bo = QDataStream::LittleEndian;
144 cfg->setProperty("endianness", 1);
145 if (wdg->radioBig->isChecked()) {
146 bo = QDataStream::BigEndian;
147 cfg->setProperty("endianness", 0);
148 }
150
151 } else {
152 const int pixelSize =
153 depthId == Float32BitsColorDepthID ? 4 :
154 depthId == Integer16BitsColorDepthID ? 2 : 1;
155
156 const int numPixels = size / pixelSize;
157
158 w = std::sqrt(numPixels);
159 h = numPixels / w;
160 bo = QDataStream::LittleEndian;
161 }
162
163
164 QDataStream s(io);
165 s.setByteOrder(bo);
166 // needed for 32bit float data
167 s.setFloatingPointPrecision(QDataStream::SinglePrecision);
168
169 const KoColorSpace *colorSpace = KoColorSpaceRegistry::instance()->colorSpace(GrayAColorModelID.id(), depthId.id(), "Gray-D50-elle-V2-srgbtrc.icc");
170 KisImageSP image = new KisImage(document->createUndoStore(), w, h, colorSpace, "imported heightmap");
171 KisPaintLayerSP layer = new KisPaintLayer(image, image->nextLayerName(), 255);
172
173 if (depthId == Float32BitsColorDepthID) {
174 fillData<float>(layer->paintDevice(), w, h, s);
175 }
176 else if (depthId == Integer16BitsColorDepthID) {
177 fillData<quint16>(layer->paintDevice(), w, h, s);
178 }
179 else if (depthId == Integer8BitsColorDepthID) {
180 fillData<quint8>(layer->paintDevice(), w, h, s);
181 }
182 else {
185 }
186
187 image->addNode(layer.data(), image->rootLayer().data());
188 document->setCurrentImage(image);
190}
191
192#include "kis_heightmap_import.moc"
const KoID Float32BitsColorDepthID("F32", ki18n("32-bit float/channel"))
const KoID GrayAColorModelID("GRAYA", ki18n("Grayscale/Alpha"))
const KoID Integer8BitsColorDepthID("U8", ki18n("8-bit integer/channel"))
const KoID Integer16BitsColorDepthID("U16", ki18n("16-bit integer/channel"))
const qreal OPACITY_OPAQUE_F
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
QString importConfiguration(const QString &filterId, bool defaultValue=false) const
void setImportConfiguration(const QString &filterId, KisPropertiesConfigurationSP properties) const
void setExportConfiguration(const QString &filterId, KisPropertiesConfigurationSP properties) const
The KisCursorOverrideHijacker class stores all override cursors in a stack, and resets them back afte...
KisImportExportErrorCode convert(KisDocument *document, QIODevice *io, KisPropertiesConfigurationSP configuration=0) override
KisHeightMapImport(QObject *parent, const QVariantList &)
KisGroupLayerSP rootLayer() const
QString nextLayerName(const QString &baseName="") const
Definition kis_image.cc:715
The base class for import and export filters.
KisHLineIteratorSP createHLineIteratorNG(qint32 x, qint32 y, qint32 w)
A dialog base class with standard buttons and predefined layouts.
Definition KoDialog.h:116
void setMainWidget(QWidget *widget)
Definition KoDialog.cpp:354
void setButtons(ButtonCodes buttonMask)
Definition KoDialog.cpp:195
@ Ok
Show Ok button. (this button accept()s the dialog; result set to QDialog::Accepted)
Definition KoDialog.h:127
@ Cancel
Show Cancel-button. (this button reject()s the dialog; result set to QDialog::Rejected)
Definition KoDialog.h:130
Definition KoID.h:30
QString id() const
Definition KoID.cpp:63
K_PLUGIN_FACTORY_WITH_JSON(KritaASCCDLFactory, "kritaasccdl.json", registerPlugin< KritaASCCDL >();) KritaASCCDL
#define KIS_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:85
#define KIS_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:75
#define KIS_ASSERT(cond)
Definition kis_assert.h:33
void fillData(KisPaintDeviceSP pd, int w, int h, QDataStream &stream)
KoID mimeTypeToKoID(const class QByteArray &mimeType)
bool addNode(KisNodeSP node, KisNodeSP parent=KisNodeSP(), KisNodeAdditionFlags flags=KisNodeAdditionFlag::None)
KisPaintDeviceSP paintDevice
const KoColorSpace * colorSpace(const QString &colorModelId, const QString &colorDepthId, const KoColorProfile *profile)
static KoColorSpaceRegistry * instance()
static void setOpacity(quint8 *pixels, quint8 alpha, qint32 nPixels)
static void setGray(quint8 *data, channels_type nv)
Set the gray component.