Commit 46987249 authored by Halla Rempt's avatar Halla Rempt

Also add support for r8 heightmap files

Note: the common extension for 8 bit raw heightmap files is .raw, however
that conflicts with panasonic .raw raw camera files. Since panasonic
registered the mimetype for .raw and the games people didn't, the games
people lose and have to mess about with another extension, i.e., .r8.
parent 1421157d
......@@ -26,4 +26,11 @@
</magic>
<glob pattern="*.r16"/>
</mime-type>
<mime-type type="image/x-r8">
<comment xml:lang="en">r8 heightmap</comment>
<magic priority="50">
<match type="string" value="r8heightmap" offset="40"/>
</magic>
<glob pattern="*.r8"/>
</mime-type>
</mime-info>
......@@ -59,7 +59,7 @@ KisHeightMapExport::~KisHeightMapExport()
KoFilter::ConversionStatus KisHeightMapExport::convert(const QByteArray& from, const QByteArray& to)
{
dbgFile << "HeightMap export! From:" << from << ", To:" << to << "";
dbgFile << "HeightMap export! From:" << from << ", To:" << to;
if (from != "application/x-krita")
return KoFilter::NotImplemented;
......@@ -85,11 +85,6 @@ KoFilter::ConversionStatus KisHeightMapExport::convert(const QByteArray& from, c
return KoFilter::WrongFormat;
}
if (inputDoc->image()->colorSpace()->colorDepthId() != Integer16BitsColorDepthID) {
inputDoc->setErrorMessage(i18n("Cannot export this image to a heightmap: it is not 16 bits integer"));
return KoFilter::WrongFormat;
}
KDialog* kdb = new KDialog(0);
kdb->setWindowTitle(i18n("HeightMap Export Options"));
kdb->setButtons(KDialog::Ok | KDialog::Cancel);
......@@ -137,6 +132,15 @@ KoFilter::ConversionStatus KisHeightMapExport::convert(const QByteArray& from, c
}
KisConfig().setExportConfiguration("HeightMap", cfg);
bool downscale = false;
if (to == "image/x-r8" && image->colorSpace()->colorDepthId() == Integer16BitsColorDepthID) {
downscale = (QMessageBox::question(0,
i18n("Downscale Image"),
i18n("You specified the .r8 extension for a 16 bit/channel image. Do you want to save as 8 bit? Your image data will not be changed."),
QMessageBox::Yes | QMessageBox::No)
== QMessageBox::Yes);
}
image->refreshGraph();
image->lock();
......@@ -149,11 +153,16 @@ KoFilter::ConversionStatus KisHeightMapExport::convert(const QByteArray& from, c
s.setByteOrder(bo);
KisRandomConstAccessorSP it = pd->createRandomConstAccessorNG(0, 0);
bool r16 = ((image->colorSpace()->colorDepthId() == Integer16BitsColorDepthID) && !downscale);
for (int i = 0; i < image->height(); ++i) {
for (int j = 0; j < image->width(); ++j) {
it->moveTo(i, j);
s << KoGrayU16Traits::gray(const_cast<quint8*>(it->rawDataConst()));
if (r16) {
s << KoGrayU16Traits::gray(const_cast<quint8*>(it->rawDataConst()));
}
else {
s << KoGrayU8Traits::gray(const_cast<quint8*>(it->rawDataConst()));
}
}
}
......
......@@ -65,17 +65,29 @@ KisHeightMapImport::~KisHeightMapImport()
KoFilter::ConversionStatus KisHeightMapImport::convert(const QByteArray& from, const QByteArray& to)
{
Q_UNUSED(from);
dbgFile << "Importing using HeightMapImport!";
if (to != "application/x-krita")
return KoFilter::BadMimeType;
KisDoc2 * doc = dynamic_cast<KisDoc2*>(m_chain -> outputDocument());
if (!doc)
if (!doc) {
return KoFilter::NoDocumentCreated;
}
KoID depthId;
if (from == "image/x-r8") {
depthId = Integer8BitsColorDepthID;
}
else if (from == "image/x-r16") {
depthId = Integer16BitsColorDepthID;
}
else {
doc->setErrorMessage(i18n("The file is not 8 or 16 bits raw"));
return KoFilter::WrongFormat;
}
dbgFile << "Importing using HeightMapImport!";
if (to != "application/x-krita") {
return KoFilter::BadMimeType;
}
QString filename = m_chain -> inputFile();
......@@ -142,7 +154,7 @@ KoFilter::ConversionStatus KisHeightMapImport::convert(const QByteArray& from, c
w = h = optionsHeightMap.intSize->value();
if ((w * h * 2) != f.size()) {
if ((w * h * (from == "image/x-r16" ? 2 : 1)) != f.size()) {
doc->setErrorMessage(i18n("Source file is not the right size for the specified width and height."));
return KoFilter::WrongFormat;
}
......@@ -161,18 +173,28 @@ KoFilter::ConversionStatus KisHeightMapImport::convert(const QByteArray& from, c
QDataStream s(&f);
s.setByteOrder(bo);
const KoColorSpace *colorSpace = KoColorSpaceRegistry::instance()->colorSpace(GrayAColorModelID.id(), Integer16BitsColorDepthID.id(), 0);
const KoColorSpace *colorSpace = KoColorSpaceRegistry::instance()->colorSpace(GrayAColorModelID.id(), depthId.id(), 0);
KisImageSP image = new KisImage(doc->createUndoStore(), w, h, colorSpace, "imported heightmap");
KisPaintLayerSP layer = new KisPaintLayer(image, image->nextLayerName(), 255);
KisRandomAccessorSP it = layer->paintDevice()->createRandomAccessorNG(0, 0);
bool r16 = (depthId == Integer16BitsColorDepthID);
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
it->moveTo(i, j);
quint16 pixel;
s >> pixel;
KoGrayU16Traits::setGray(it->rawData(), pixel);
KoGrayU16Traits::setOpacity(it->rawData(), OPACITY_OPAQUE_F, 1);
if (r16) {
quint16 pixel;
s >> pixel;
KoGrayU16Traits::setGray(it->rawData(), pixel);
KoGrayU16Traits::setOpacity(it->rawData(), OPACITY_OPAQUE_F, 1);
}
else {
quint8 pixel;
s >> pixel;
KoGrayU8Traits::setGray(it->rawData(), pixel);
KoGrayU8Traits::setOpacity(it->rawData(), OPACITY_OPAQUE_F, 1);
}
}
}
......
......@@ -6,11 +6,44 @@
<rect>
<x>0</x>
<y>0</y>
<width>242</width>
<height>122</height>
<width>177</width>
<height>100</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Size:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="KIntNumInput" name="intSize"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Endianness:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
</widget>
</item>
<item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
......@@ -44,39 +77,6 @@
</item>
</layout>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Size:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="KIntNumInput" name="intSize"/>
</item>
<item row="2" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Endianness:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
......
......@@ -10,7 +10,7 @@ StartupNotify=true
Terminal=false
Type=Application
X-KDE-ServiceTypes=Calligra/Application
X-Calligra-DefaultMimeTypes=image/x-r16
X-Calligra-DefaultMimeTypes=image/x-r16,image/x-8
X-DBUS-ServiceName=org.krita.krita
X-DBUS-StartupType=multi
X-KDE-SubstituteUID=false
......
......@@ -3,7 +3,7 @@ Icon=
Name=Krita HeightMap Export Filter
X-KDE-ServiceTypes=Calligra/Filter
Type=Service
X-KDE-Export=image/x-r16
X-KDE-Export=image/x-r16,image/x-r8
X-KDE-Import=application/x-krita
X-KDE-Library=kritaheightmapexport
X-KDE-Weight=1
......
......@@ -4,7 +4,7 @@ Name=Krita HeightMap Import Filter
X-KDE-ServiceTypes=Calligra/Filter
Type=Service
X-KDE-Export=application/x-krita
X-KDE-Import=image/x-r16
X-KDE-Import=image/x-r16,image/x-r8
X-KDE-Library=kritaheightmapimport
X-KDE-Weight=1
NoDisplay=true
......@@ -69,7 +69,7 @@ inline quint16 correctIndian(quint16 v)
KoFilter::ConversionStatus KisRawImport::convert(const QByteArray& from, const QByteArray& to)
{
dbgFile << from << " " << to;
dbgFile << from << " " << to << "";
if (/*from != "image/x-raw" || */to != "application/x-krita") { // too many from to check, and I don't think it can happen an unsupported from
return KoFilter::NotImplemented;
}
......@@ -122,7 +122,7 @@ KoFilter::ConversionStatus KisRawImport::convert(const QByteArray& from, const Q
if (image.isNull()) return KoFilter::CreationError;
KisPaintLayerSP layer = new KisPaintLayer(image, image->nextLayerName(), quint8_MAX);
KisTransaction("", layer -> paintDevice());
KisTransaction("", layer->paintDevice());
image->addNode(layer, image->rootLayer());
if (layer.isNull()) return KoFilter::CreationError;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment