Commit 065ac9ba authored by Blackbeard (alberto flores)'s avatar Blackbeard (alberto flores) 🚢

Added a parasite helper class.

parent 7e087bd6
...@@ -20,6 +20,7 @@ set(kritalibbrush_LIB_SRCS ...@@ -20,6 +20,7 @@ set(kritalibbrush_LIB_SRCS
kis_png_brush.cpp kis_png_brush.cpp
kis_svg_brush.cpp kis_svg_brush.cpp
kis_vectoranimated_brush.cpp kis_vectoranimated_brush.cpp
kis_vectorbrush_parasite.cpp
kis_qimage_pyramid.cpp kis_qimage_pyramid.cpp
KisSharedQImagePyramid.cpp KisSharedQImagePyramid.cpp
kis_text_brush.cpp kis_text_brush.cpp
......
/*
* Copyright (c) 2012 Dmitry Kazakov <dimula73@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.
*/
#include <QRandomGenerator>
#include <kis_animated_brushes.h>
#include <kis_brushes_pipe.h>
#include <kis_vectoranimated_brush.h>
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);
}
/* /*
* Copyright (c) 2010 Cyrille Berger <cberger@cberger.net> * Copyright (c) 2019 Alberto Eleuterio Flores Guerrero <barbanegra+bugs@posteo.mx>
* *
* This program is free software; you can redistribute it and/or modify * 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 * 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. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
...@@ -30,34 +30,26 @@ ...@@ -30,34 +30,26 @@
#include <KoShapePainter.h> #include <KoShapePainter.h>
#include <kis_brushes_pipe.h> #include <kis_brushes_pipe.h>
#include <kis_vectorbrush_parasite.h>
#include <QDebug> #include <QDebug>
KisVectorAnimatedBrush::KisVectorAnimatedBrush(const QString& filename) KisVectorAnimatedBrush::KisVectorAnimatedBrush(const QString& filename)
: KisScalingSizeBrush(filename) : KisScalingSizeBrush(filename)
/* , m_imageNumber(0)
, m_isInitialized(false) */
{ {
setBrushType(INVALID); setBrushType(PIPE_MASK);
setSpacing(0.25); setSpacing(0.25);
setHasColor(false); 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) KisVectorAnimatedBrush::KisVectorAnimatedBrush(const KisVectorAnimatedBrush& rhs)
: KisScalingSizeBrush(rhs) : KisScalingSizeBrush(rhs)
, m_svg(rhs.m_svg) , m_svg(rhs.m_svg)
{ {
} }
KisBrush* KisVectorAnimatedBrush::clone() const KisBrush* KisVectorAnimatedBrush::clone() const
{ {
return new KisVectorAnimatedBrush(*this); return new KisVectorAnimatedBrush(*this);
...@@ -78,106 +70,16 @@ bool KisVectorAnimatedBrush::load() ...@@ -78,106 +70,16 @@ bool KisVectorAnimatedBrush::load()
return res; return res;
} }
KisVectorBrushParasite KisVectorAnimatedBrush::m_parasite;
bool KisVectorAnimatedBrush::loadFromDevice(QIODevice *dev) bool KisVectorAnimatedBrush::loadFromDevice(QIODevice *dev)
{ {
m_svg = dev->readAll(); m_svg = dev->readAll();
KoXmlDocument documento = SvgParser::createDocumentFromSvg(m_svg); m_parasite.parseSvg(m_svg);
// qDebug() << "ejemplo" << documento.toString(); m_parasite.setIndex(0);
setBrushTipImage(m_parasite.nextBrushTip());
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<KoShape*> shapes = parser.parseSvg(documento.documentElement(), &fragmentSize);
QList<KoShape*> list = parser.parseSvg(elementos, &fragmentSize);
/* // According to documentation it is better to use const_iterator if we won't change the QList elements
QList<KoShape*>::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<KoShape*> 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 ------------------------
QVector<QRgb> 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<KoShape*> &shapes); ?
setValid(true); setValid(true);
...@@ -196,9 +98,10 @@ bool KisVectorAnimatedBrush::loadFromDevice(QIODevice *dev) ...@@ -196,9 +98,10 @@ bool KisVectorAnimatedBrush::loadFromDevice(QIODevice *dev)
QFileInfo fi(filename()); QFileInfo fi(filename());
setName(fi.baseName()); setName(fi.baseName());
return !brushTipImage().isNull() && valid(); return !brushTipImage().isNull() && valid();
} }
bool KisVectorAnimatedBrush::save() bool KisVectorAnimatedBrush::save()
{ {
QFile f(filename()); QFile f(filename());
...@@ -229,43 +132,61 @@ void KisVectorAnimatedBrush::toXML(QDomDocument& d, QDomElement& e) const ...@@ -229,43 +132,61 @@ void KisVectorAnimatedBrush::toXML(QDomDocument& d, QDomElement& e) const
} }
void KisVectorAnimatedBrush::prepareForSeqNo(const KisPaintInformation &info, int seqNo)
/* {
// <---------------------- Index Functions --------------------> Q_UNUSED(info)
Q_UNUSED(seqNo)
void KisAnimatedBrushes::notifyStrokeStarted() { m_parasite.nextIndex();
m_isInitialized = false; 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) setBrushTipImage(image);
{ setValid(true);
m_d->brushesPipe.prepareForSeqNo(info, seqNo);
}
// Well for now, always true
if (brushTipImage().isGrayscale()) {
setBrushType(MASK);
setHasColor(false);
}
else {
setBrushType(IMAGE);
setHasColor(true);
}
enumBrushType KisAnimatedBrushes::brushType() const setWidth(brushTipImage().width());
{ setHeight(brushTipImage().height());
return !hasColor() || useColorAsMask() ? PIPE_MASK : PIPE_IMAGE;
}
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;
}
*/ */
/* /*
* Copyright (c) 2010 Cyrille Berger <cberger@cberger.net> * Copyright (c) 2019 Alberto Eleuterio Flores Guerrero <barbanegra+bugs@posteo.mx>
* *
* This program is free software; you can redistribute it and/or modify * 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 * 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. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
...@@ -25,11 +25,15 @@ ...@@ -25,11 +25,15 @@
#include <KoShapePainter.h> #include <KoShapePainter.h>
#include <SvgParser.h> #include <SvgParser.h>
#include <KoXmlReader.h> #include <KoXmlReader.h>
#include <kis_vectorbrush_parasite.h>
//
class BRUSH_EXPORT KisVectorAnimatedBrush : public KisScalingSizeBrush class BRUSH_EXPORT KisVectorAnimatedBrush : public KisScalingSizeBrush
{ {
public: public:
/// Construct brush to load filename later as brush /// Construct brush to load filename later as brush
KisVectorAnimatedBrush(const QString& filename); KisVectorAnimatedBrush(const QString& filename);
KisVectorAnimatedBrush(const KisVectorAnimatedBrush& rhs); KisVectorAnimatedBrush(const KisVectorAnimatedBrush& rhs);
...@@ -42,144 +46,24 @@ public: ...@@ -42,144 +46,24 @@ public:
QString defaultFileExtension() const override; QString defaultFileExtension() const override;
void toXML(QDomDocument& d, QDomElement& e) 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<KoShape*> 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<QString> getMetadata();
// QList -> QString
// helper for getBrushStyle
// Returns Style of the brush : "Random", "Sequence"
QString getMetadataStyle();
// <------------------------ List of Shapes Functions ----------------------------->
// svg -> QList<KoShape*>
// Returns the shapes of the SVG
QList<KoShape*> getBrushShapes();
// <------------------------ Brush Tip Functions -----------------------------> void newbrushTip();
// QList<KoShape*> int -> QImage // void notifyStrokeStarted() override;
// Returns the proper QImage // void notifyCachedDabPainted(const KisPaintInformation& info) override;
QImage getBrushStamp();
// QList<KoShape*> 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 prepareForSeqNo(const KisPaintInformation& info, int seqNo) override; void prepareForSeqNo(const KisPaintInformation& info, int seqNo) override;
/*
// Asks for the new image
KisFixedPaintDeviceSP paintDevice(const KoColorSpace * colorSpace,
protected: KisDabShape const&,
void setBrushType(enumBrushType type) override; const KisPaintInformation& info,
double subPixelX = 0, double subPixelY = 0) const override;
// private:
// friend class KisAnimatedBrushesTest;
// KisVectorAnimatedBrush* testingGetCurrentBrush(const KisPaintInformation& info) const;
// void testingSelectNextBrush(const KisPaintInformation& info) const;
*/ */
private: private:
QByteArray m_svg; QByteArray m_svg;
int m_imageNumber; static KisVectorBrushParasite m_parasite;
int m_numberOfImages;
bool m_isInitialized;
/* struct Private; /* struct Private;
Private * const m_d; */ Private * const m_d; */
......
/*
* Copyright (c) 2019 Alberto Eleuterio Flores Guerrero <barbanegra+bugs@posteo.mx>
*
* 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 <KoShapeManager.h>
#include <SvgParser.h>
#include <KoDocumentResourceManager.h>
#include <KoXmlReader.h>
#include <KoShapePainter.h>
#include <QDebug>
KisVectorBrushParasite::KisVectorBrushParasite()
{
}
void KisVectorBrushParasite::parseSvg(QByteArray svg)
{
m_shapes= svgToKoShapes(svg);
m_maxIndex = m_shapes.size();
}
QList<KoShape*> 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<KoShape*> 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<KoShape*> &shapes); ?
// this is too expensive, should be done with KoShapeManager
QImage KisVectorBrushParasite::shapeToQImage(QList<KoShape *> shapes, int index)
{
QList<KoShape*> 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<QRgb> 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;
}
/* /*
* Copyright (c) 2012 Dmitry Kazakov <dimula73@gmail.com> * Copyright (c) 2019 Alberto Eleuterio Flores Guerrero <barbanegra+bugs@posteo.mx>
* *
* This program is free software; you can redistribute it and/or modify * 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 * 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. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
...@@ -15,49 +15,46 @@ ...@@ -15,49 +15,46 @@
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.