diff --git a/libs/brush/CMakeLists.txt b/libs/brush/CMakeLists.txt index a3982cb194abce306260d0f445498afacf3c39d7..b82699773434c61e0ac2ae825f952d240506c73d 100644 --- a/libs/brush/CMakeLists.txt +++ b/libs/brush/CMakeLists.txt @@ -20,6 +20,7 @@ set(kritalibbrush_LIB_SRCS kis_png_brush.cpp kis_svg_brush.cpp kis_vectoranimated_brush.cpp + kis_vectorbrush_parasite.cpp kis_qimage_pyramid.cpp KisSharedQImagePyramid.cpp kis_text_brush.cpp diff --git a/libs/brush/kis_animated_brushes.cpp b/libs/brush/kis_animated_brushes.cpp deleted file mode 100644 index 0b6990c4a4d6b3ffa2fe8199b17c125d7f34b166..0000000000000000000000000000000000000000 --- a/libs/brush/kis_animated_brushes.cpp +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright (c) 2012 Dmitry Kazakov - * - * 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. - */ - -#include - -#include -#include -#include - - - -KisAnimatedBrushes::KisAnimatedBrushes() - : m_isInitialized(false) -{ -} - - - - -int KisAnimatedBrushes::chooseNextBrush(const KisPaintInformation &info) { - int index; - if (!m_isInitialized) { - index = 0; - updateBrushIndexes(info, 0); - m_isInitialized = true; - } else { - index = KisAnimatedBrushes::nextBrushSequence(m_currentIndex, m_numberOfImages) - } - - m_currentIndex = index; - return index; -} - -*/ - -int KisAnimatedBrushes::nextBrushSequence(int currentIndex, int numberOfImages) { - - if (currentIndex == numberOfImages) { - return 0; - } else { - return currentIndex++; - } - -} - -int KisAnimatedBrushes::nextBrushRandom(int currentIndex, int numberOfImages) { - int index = rand() % numberOfImages + 1; - if (currentIndex == index) { - index = rand() % numberOfImages + 1; - } - return index; - -} - - - - - -void updateBrushIndexes(const KisPaintInformation& info, int seqNo) override { - for (int i = 0; i < m_parasite.dim; i++) { - m_parasite.index[i] = selectPost(m_parasite.selection[i], - m_parasite.index[i], - m_parasite.rank[i], - info, - seqNo); - } - - - - diff --git a/libs/brush/kis_animated_brushes.h b/libs/brush/kis_animated_brushes.h deleted file mode 100644 index 810a2efb42bb1347b02c14ca99537501cdae5d7e..0000000000000000000000000000000000000000 --- a/libs/brush/kis_animated_brushes.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2012 Dmitry Kazakov - * - * 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. - */ - -#ifndef KIS_ANIMATED_BRUSHES_ -#define KIS_ANIMATED_BRUSHES_ - -#include -#include - - -class BRUSH_EXPORT KisAnimatedBrushes : public KisBrushesPipe -{ -public: - KisAnimatedBrushes(); - - // <-----------------Index Functions -------------------> - // KisPaintInformation -> int - // Returns and index for the next brush - int chooseNextBrush(const KisPaintInformation& info) override; - - // int -> int - // helper for chooseNextBrush - // Returns next index in sequence - int nextBrushSequence(int currentIndex, int numberOfImages); - - // int -> int - // helper for chooseNextBrush - // Returns a random index - int nextBrushRandom(int currentIndex, int numberOfImages); - - - void updateBrushIndexes(const KisPaintInformation& info, int seqNo); - - - - -private: - bool m_isInitialized; - int m_currentIndex; - int m_numberOfImages; - QString m_typeOfBrush; - struct Private; - Private * const m_d; - - -}; - -#endif /* __KIS_ANIMATED_BRUSHES_ */ diff --git a/libs/brush/kis_vectoranimated_brush.cpp b/libs/brush/kis_vectoranimated_brush.cpp index 771b5b1593122c4cfbfcfb6ed978db00a7920397..04816c59eaa6f87e90aea9939ea4af7d923dbc64 100644 --- a/libs/brush/kis_vectoranimated_brush.cpp +++ b/libs/brush/kis_vectoranimated_brush.cpp @@ -1,9 +1,9 @@ -/* - * Copyright (c) 2010 Cyrille Berger +/* + * Copyright (c) 2019 Alberto Eleuterio Flores Guerrero * * 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 + * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, @@ -30,34 +30,26 @@ #include #include +#include + #include KisVectorAnimatedBrush::KisVectorAnimatedBrush(const QString& filename) - : KisScalingSizeBrush(filename) - /* , m_imageNumber(0) - , m_isInitialized(false) */ + : KisScalingSizeBrush(filename) { - setBrushType(INVALID); + setBrushType(PIPE_MASK); setSpacing(0.25); setHasColor(false); - /* - if (!m_isInitialized) { - if (m_imageNumber == m_numberOfImages) { - m_imageNumber = 0; - qDebug() << "m_imageNumber" << m_imageNumber; - } else { - m_imageNumber ++; - qDebug() << "m_imageNumber" << m_imageNumber; - } - } */ + } KisVectorAnimatedBrush::KisVectorAnimatedBrush(const KisVectorAnimatedBrush& rhs) : KisScalingSizeBrush(rhs) - , m_svg(rhs.m_svg) + , m_svg(rhs.m_svg) { } + KisBrush* KisVectorAnimatedBrush::clone() const { return new KisVectorAnimatedBrush(*this); @@ -78,106 +70,16 @@ bool KisVectorAnimatedBrush::load() return res; } +KisVectorBrushParasite KisVectorAnimatedBrush::m_parasite; + bool KisVectorAnimatedBrush::loadFromDevice(QIODevice *dev) { m_svg = dev->readAll(); - KoXmlDocument documento = SvgParser::createDocumentFromSvg(m_svg); - // qDebug() << "ejemplo" << documento.toString(); - - KoXmlElement elementos = documento.documentElement(); - - /* qDebug() << documento.toString(); - qDebug() << elementos.text(); */ - - KoDocumentResourceManager manager; - SvgParser parser(&manager); - parser.setResolution(QRectF(0,0,100,100), 72); // initialize with default values - QSizeF fragmentSize; - - // QList shapes = parser.parseSvg(documento.documentElement(), &fragmentSize); - QList list = parser.parseSvg(elementos, &fragmentSize); - - /* // According to documentation it is better to use const_iterator if we won't change the QList elements - QList::const_iterator i; - for (i = list.constBegin(); i != list.constEnd(); ++i) - qDebug() << "figura" << *i; - // qDebug() << list.at(*i) ; - - for (int i = 0; i != list.size(); ++i) { - qDebug() << "figura" << i; - qDebug() << list.at(i); -} -*/ - m_imageNumber = 0; - qDebug() << "Numero:" << m_imageNumber; - - QList single; - single.append(list.at(m_imageNumber)); - - qDebug() << "lista" << list.count(); - qDebug() << "figura" << m_imageNumber << list.at(m_imageNumber); - -/* - if (m_imageNumber == list.size()) { - m_imageNumber = 0; - } - else { - m_imageNumber ++; - } - */ - m_numberOfImages = list.size(); - m_isInitialized = true; - // qDebug() << "numberImages" << m_numberOfImages << "initialized" << m_isInitialized << "numero" << m_imageNumber ; - - KoShapePainter painter; - painter.setShapes(single); - - QImage theImage(1000, 1000, QImage::Format_ARGB32); //fix size - { - QPainter p(&theImage); - p.fillRect(0, 0, theImage.width(), theImage.height(), Qt::white); - - } - - painter.paint(theImage); - - theImage.save("my_testImage", "PNG"); //should save only one image - - /* qDebug() << "shapes" << list[0]; */ - - - - // ------------------------------------------- old ------------------------ - - /* QSvgRenderer renderer(m_svg); - QRect box = renderer.viewBox(); - if (box.isEmpty()) return false; - - QImage image_(1000, (1000 * box.height()) / box.width(), QImage::Format_ARGB32); - { - QPainter p(&image_); - p.fillRect(0, 0, image_.width(), image_.height(), Qt::white); - renderer.render(&p); - } - */ - - // ------------------------------------------- old ------------------------ + m_parasite.parseSvg(m_svg); + m_parasite.setIndex(0); + setBrushTipImage(m_parasite.nextBrushTip()); - QVector table; - for (int i = 0; i < 256; ++i) table.push_back(qRgb(i, i, i)); - theImage = theImage.convertToFormat(QImage::Format_Indexed8, table); - - setBrushTipImage(theImage); - // Blackbeard TODO!! - // KoCanvasControllerWidgetViewport_p.cpp - // KoSvgSymbolCollectionResource.cpp - // KoShapePainter.cpp --> boud told me to check this library - // SvgWriter.cpp line 256 - // KoMarker.cpp line 99 - // setBrushTipImage(theImage); - // > couldn't you just set a single shape with void setShapes(const - // QList &shapes); ? setValid(true); @@ -196,9 +98,10 @@ bool KisVectorAnimatedBrush::loadFromDevice(QIODevice *dev) QFileInfo fi(filename()); setName(fi.baseName()); - return !brushTipImage().isNull() && valid(); + return !brushTipImage().isNull() && valid(); } + bool KisVectorAnimatedBrush::save() { QFile f(filename()); @@ -229,43 +132,61 @@ void KisVectorAnimatedBrush::toXML(QDomDocument& d, QDomElement& e) const } - -/* -// <---------------------- Index Functions --------------------> - -void KisAnimatedBrushes::notifyStrokeStarted() { - m_isInitialized = false; +void KisVectorAnimatedBrush::prepareForSeqNo(const KisPaintInformation &info, int seqNo) +{ + Q_UNUSED(info) + Q_UNUSED(seqNo) + m_parasite.nextIndex(); + newbrushTip(); } -void KisAnimatedBrushes::notifyCachedDabPainted(const KisPaintInformation& info) +void KisVectorAnimatedBrush::newbrushTip() { - m_d->brushesPipe.notifyCachedDabPainted(info); -} + QImage image = m_parasite.nextBrushTip(); -void KisAnimatedBrushes::prepareForSeqNo(const KisPaintInformation &info, int seqNo) -{ - m_d->brushesPipe.prepareForSeqNo(info, seqNo); -} + setBrushTipImage(image); + setValid(true); + // Well for now, always true + if (brushTipImage().isGrayscale()) { + setBrushType(MASK); + setHasColor(false); + } + else { + setBrushType(IMAGE); + setHasColor(true); + } -enumBrushType KisAnimatedBrushes::brushType() const -{ - return !hasColor() || useColorAsMask() ? PIPE_MASK : PIPE_IMAGE; -} + setWidth(brushTipImage().width()); + setHeight(brushTipImage().height()); -bool KisAnimatedBrushes::hasColor() const -{ - return m_d->brushesPipe.hasColor(); } -quint32 KisAnimatedBrushes::brushIndex(const KisPaintInformation& info) const +/* +KisFixedPaintDeviceSP KisVectorAnimatedBrush::paintDevice(const KoColorSpace *colorSpace, + const KisDabShape &, + const KisPaintInformation &info, + double subPixelX, double subPixelY) const { - return m_d->brushesPipe.brushIndex(info); -} + Q_ASSERT(valid()); + Q_UNUSED(info) + Q_UNUSED(subPixelX) + Q_UNUSED(subPixelY) + qDebug() << m_imageNumber; + nextIndex(); + m_imageNumber++; + QImage outputImage = m_brushTip; + KisFixedPaintDeviceSP dab = new KisFixedPaintDevice(colorSpace); + Q_CHECK_PTR(dab); + dab->convertFromQImage(outputImage, ""); + return dab; + + } + */ diff --git a/libs/brush/kis_vectoranimated_brush.h b/libs/brush/kis_vectoranimated_brush.h index 89aeadf28b88468bae99dcb80b57134007488db0..d0960d90e874f15385384962abc52e35fed417f1 100644 --- a/libs/brush/kis_vectoranimated_brush.h +++ b/libs/brush/kis_vectoranimated_brush.h @@ -1,9 +1,9 @@ /* - * Copyright (c) 2010 Cyrille Berger + * Copyright (c) 2019 Alberto Eleuterio Flores Guerrero * * 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 + * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, @@ -25,11 +25,15 @@ #include #include #include +#include +// class BRUSH_EXPORT KisVectorAnimatedBrush : public KisScalingSizeBrush { public: + + /// Construct brush to load filename later as brush KisVectorAnimatedBrush(const QString& filename); KisVectorAnimatedBrush(const KisVectorAnimatedBrush& rhs); @@ -42,144 +46,24 @@ public: QString defaultFileExtension() const override; void toXML(QDomDocument& d, QDomElement& e) const override; -/* -// <------------------------------------------------- Draft Data --------------------------------------------------------> - -//type of Brush -namespace KisAnimated -{ -enum SelectionMode { - Sequence, - Random -}; -} - -// State of the brush - bool m_isInitialized; - - // style of brush - // "Sequence" - // "Random" - QString m_brushStyle; - - //Needed to parse the svg - KoXmlDocument m_document; - KoXmlElement m_elements; - - // QList with all the brush tips - QList m_listOfTips; - - // The index of the QList to get the proper QImage - quint32 m_listIndex; - - // Current Size of the list of QList - int m_listSize; - - // Current brush tip - QImage m_brushTip; - - - - -// <------------------------------------------------- Draft Functions --------------------------------------------------------> - - // <------------------------ Initialize Functions -----------------------------> - - // bool -> bool - // tells if brush is Initialized - bool isBrushInitialized(); - - - // <------------------------ Metadata Functions -----------------------------> - // svg -> QString - // Returns the style of the brush - QString getBrushStyle(); - - // KoXmlDocument -> QList - //helper for getBrushStyle - // returns the metadata of the brush in a QList - QList getMetadata(); - - // QList -> QString - // helper for getBrushStyle - // Returns Style of the brush : "Random", "Sequence" - QString getMetadataStyle(); - - - // <------------------------ List of Shapes Functions -----------------------------> - // svg -> QList - // Returns the shapes of the SVG - QList getBrushShapes(); - - // <------------------------ Brush Tip Functions -----------------------------> + void newbrushTip(); - // QList int -> QImage - // Returns the proper QImage - QImage getBrushStamp(); - - // QList int -> QImage - // Returns the proper QImage - QImage setBrushStamp(); - - - - void setAngle(qreal angle) override; - void setScale(qreal scale) override; - void setSpacing(double spacing) override; - - - - // <------------------------ Index Functions -----------------------------> - //KisPaintInformation -> quint32 - // Returns current Index - quint32 brushIndex(const KisPaintInformation& info) const override; - - // KisPaintInformation -> int - // Returns and index for the next brush - int chooseNextBrush(const KisPaintInformation& info) override; - - // int -> int - // helper for chooseNextBrush - // Returns next index in sequence - int nextBrushSequence(); - - // int -> int - // helper for chooseNextBrush - // Returns a random index - int nextBrushRandom(); - - - - // qint32 maskWidth(KisDabShape const&, double subPixelX, double subPixelY, const KisPaintInformation& info) const override; - // qint32 maskHeight(KisDabShape const&, double subPixelX, double subPixelY, const KisPaintInformation& info) const override; - - void notifyStrokeStarted() override; - void notifyCachedDabPainted(const KisPaintInformation& info) override; + // void notifyStrokeStarted() override; + // void notifyCachedDabPainted(const KisPaintInformation& info) override; void prepareForSeqNo(const KisPaintInformation& info, int seqNo) override; - - - -protected: - void setBrushType(enumBrushType type) override; - - -// private: - // friend class KisAnimatedBrushesTest; - - // KisVectorAnimatedBrush* testingGetCurrentBrush(const KisPaintInformation& info) const; - // void testingSelectNextBrush(const KisPaintInformation& info) const; - - - +/* + // Asks for the new image + KisFixedPaintDeviceSP paintDevice(const KoColorSpace * colorSpace, + KisDabShape const&, + const KisPaintInformation& info, + double subPixelX = 0, double subPixelY = 0) const override; */ - + private: QByteArray m_svg; - int m_imageNumber; - int m_numberOfImages; - bool m_isInitialized; + static KisVectorBrushParasite m_parasite; /* struct Private; Private * const m_d; */ diff --git a/libs/brush/kis_vectorbrush_parasite.cpp b/libs/brush/kis_vectorbrush_parasite.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bfc0b291d69e680ef1af6777b5fba0262876ff5d --- /dev/null +++ b/libs/brush/kis_vectorbrush_parasite.cpp @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2019 Alberto Eleuterio Flores Guerrero + * + * 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 3 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. + */ +#include "kis_vectorbrush_parasite.h" + +#include + + +#include +#include +#include +#include + +#include + +KisVectorBrushParasite::KisVectorBrushParasite() +{ +} + +void KisVectorBrushParasite::parseSvg(QByteArray svg) +{ + m_shapes= svgToKoShapes(svg); + m_maxIndex = m_shapes.size(); +} + + +QList KisVectorBrushParasite::svgToKoShapes(QByteArray svg) +{ + KoXmlDocument document = SvgParser::createDocumentFromSvg(svg); + // qDebug() << "document" << documento.toString(); + + KoXmlElement elements = document.documentElement(); + // qDebug() << element.text(); + + + // Necessary to parse the elemnts + KoDocumentResourceManager manager; + SvgParser parser(&manager); + parser.setResolution(QRectF(0,0,100,100), 72); // initialize with default values + QSizeF fragmentSize; + + QList shapes = parser.parseSvg(elements, &fragmentSize); + + return shapes; +} + + +// Blackbeard TODO!! +// KoCanvasControllerWidgetViewport_p.cpp +// KoSvgSymbolCollectionResource.cpp +// KoShapePainter.cpp --> boud told me to check this library +// SvgWriter.cpp line 256 +// KoMarker.cpp line 99 +// setBrushTipImage(theImage); +// > couldn't you just set a single shape with void setShapes(const +// QList &shapes); ? +// this is too expensive, should be done with KoShapeManager +QImage KisVectorBrushParasite::shapeToQImage(QList shapes, int index) +{ + QList single_list; + single_list.append(shapes.at(index)); + + KoShapePainter painter; + painter.setShapes(single_list); + + QImage theImage(1000, 1000, QImage::Format_ARGB32); //fix size + { + QPainter p(&theImage); + p.fillRect(0, 0, theImage.width(), theImage.height(), Qt::white); + + } + painter.paint(theImage); + + QVector table; + for (int i = 0; i < 256; ++i) table.push_back(qRgb(i, i, i)); + theImage = theImage.convertToFormat(QImage::Format_Indexed8, table); + + return theImage; +} + + +QImage KisVectorBrushParasite::nextBrushTip() +{ + return shapeToQImage(m_shapes, m_index); +} + + +int KisVectorBrushParasite::maxIndex() +{ + return m_maxIndex; +} + + +void KisVectorBrushParasite::nextIndex() +{ + qDebug() << "index" << m_index; + if (m_index == m_maxIndex){ + m_index = 0; + } else { + m_index++; + } +} + + +void KisVectorBrushParasite::setIndex(int index) +{ + m_index = index; +} + diff --git a/libs/brush/kis_vectorbrush_parasite.h b/libs/brush/kis_vectorbrush_parasite.h new file mode 100644 index 0000000000000000000000000000000000000000..abe61fcbccf11197502f0dd921386789bf31f8c4 --- /dev/null +++ b/libs/brush/kis_vectorbrush_parasite.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2019 Alberto Eleuterio Flores Guerrero + * + * 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 3 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. + */ +#ifndef KIS_VECTOR_BRUSH_P_ +#define KIS_VECTOR_BRUSH_P_ + +#include +#include +#include + + +class KisVectorBrushParasite +{ +public: + KisVectorBrushParasite(); + + /* + * This function gets the SVG and + * sets m_listOfImages + * */ + void parseSvg(QByteArray svg); + QList svgToKoShapes(QByteArray svg); + + QImage shapeToQImage(QList shapes, int index); + + QImage nextBrushTip(); + + + void setMaxIndex(int max); + void setIndex(int index); + void nextIndex(); + + int maxIndex(); + + +private: + QList m_shapes; + QImage m_brushTip; + int m_maxIndex; + int m_index; + + +}; + + +#endif