kis_import_qmic_processing_visitor.cpp 3.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *  Copyright (c) 2013 Dmitry Kazakov <dimula73@gmail.com>
 *  Copyright (c) 2013 Lukáš Tvrdý <lukast.dev@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

20

21 22 23

#include <kis_transaction.h>
#include <kis_paint_device.h>
Lukáš Tvrdý's avatar
Lukáš Tvrdý committed
24
#include <kis_debug.h>
25

26
#include "kis_qmic_simple_convertor.h"
27 28
#include <kis_node.h>
#include <kis_painter.h>
29 30
#include <commands/kis_image_layer_add_command.h>
#include <kis_paint_layer.h>
31
#include <KoCompositeOpRegistry.h>
32 33
#include <kis_selection.h>
#include <kis_types.h>
34
#include <QtCore/QRegularExpression>
35

36 37
#include "kis_import_qmic_processing_visitor.h"
#include "gmic.h"
38

39
KisImportQmicProcessingVisitor::KisImportQmicProcessingVisitor(const KisNodeListSP nodes, QVector<gmic_image<float> *> images, const QRect &dstRect, KisSelectionSP selection)
40
    : m_nodes(nodes),
41 42 43
      m_images(images),
      m_dstRect(dstRect),
      m_selection(selection)
44
{
Boudewijn Rempt's avatar
Boudewijn Rempt committed
45
    dbgPlugins << "KisImportQmicProcessingVisitor";
46 47
}

48 49 50 51 52

void KisImportQmicProcessingVisitor::gmicImageToPaintDevice(gmic_image<float>& srcGmicImage,
                                                            KisPaintDeviceSP dst, KisSelectionSP selection, const QRect &dstRect)
{

Boudewijn Rempt's avatar
Boudewijn Rempt committed
53
    dbgPlugins << "KisImportQmicProcessingVisitor::gmicImageToPaintDevice();";
54 55 56 57 58
    if (selection) {
        KisPaintDeviceSP src = new KisPaintDevice(dst->colorSpace());
        KisQmicSimpleConvertor::convertFromGmicFast(srcGmicImage, src, 255.0f);

        KisPainter painter(dst, selection);
59
        painter.setCompositeOp(COMPOSITE_COPY);
60 61 62 63 64
        painter.bitBlt(dstRect.topLeft(), src, QRect(QPoint(0,0),dstRect.size()));
    }
    else {
        KisQmicSimpleConvertor::convertFromGmicFast(srcGmicImage, dst, 255.0f);
    }
65 66 67 68 69 70 71 72 73 74

    // Some GMic filters encode layer position into the layer name.
    // E.g. from extract foreground: "name([unnamed] [foreground]),pos(55,35)"
    const QRegularExpression positionPattern(R"(\Wpos\((\d+),(\d+)\))");
    const QRegularExpressionMatch match = positionPattern.match(srcGmicImage.name);
    if (match.hasMatch()) {
        int x = match.captured(1).toInt();
        int y = match.captured(2).toInt();
        dst->moveTo(x, y);
    }
75 76 77 78 79
}



void KisImportQmicProcessingVisitor::visitNodeWithPaintDevice(KisNode *node, KisUndoAdapter *undoAdapter)
80
{
81
    int index = m_nodes->indexOf(node);
82
    if (index >= 0 && index < m_images.size()) {
83
        gmic_image<float> *gimg = m_images[index];
Boudewijn Rempt's avatar
Boudewijn Rempt committed
84
        dbgPlugins << "Importing layer index" << index << "Size: "<< gimg->_width << "x" << gimg->_height << "colorchannels: " << gimg->_spectrum;
85

86
        KisPaintDeviceSP dst = node->paintDevice();
87 88 89 90

        const KisLayer *layer = dynamic_cast<KisLayer*>(node);
        const KisSelectionSP selection = layer ? layer->selection() : m_selection;
        
91
        KisTransaction transaction(dst);
92
        KisImportQmicProcessingVisitor::gmicImageToPaintDevice(*gimg, dst, selection, m_dstRect);
93
        if (undoAdapter) {
94 95 96 97 98 99
            transaction.commit(undoAdapter);
            node->setDirty(m_dstRect);
        }
    }
}

100
void KisImportQmicProcessingVisitor::visitExternalLayer(KisExternalLayer *layer, KisUndoAdapter *undoAdapter)
101 102 103 104
{
    Q_UNUSED(layer);
    Q_UNUSED(undoAdapter);
}
105

106
void KisImportQmicProcessingVisitor::visitColorizeMask(KisColorizeMask *mask, KisUndoAdapter *undoAdapter)
107 108 109 110
{
    Q_UNUSED(mask);
    Q_UNUSED(undoAdapter);
}