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
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
......
/*
* 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
* 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,26 +30,17 @@
#include <KoShapePainter.h>
#include <kis_brushes_pipe.h>
#include <kis_vectorbrush_parasite.h>
#include <QDebug>
KisVectorAnimatedBrush::KisVectorAnimatedBrush(const QString& filename)
: KisScalingSizeBrush(filename)
/* , m_imageNumber(0)
, m_isInitialized(false) */
{
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)
......@@ -58,6 +49,7 @@ KisVectorAnimatedBrush::KisVectorAnimatedBrush(const KisVectorAnimatedBrush& rhs
{
}
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<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;
m_parasite.parseSvg(m_svg);
m_parasite.setIndex(0);
setBrushTipImage(m_parasite.nextBrushTip());
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);
......@@ -199,6 +101,7 @@ bool KisVectorAnimatedBrush::loadFromDevice(QIODevice *dev)
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;
}
*/
/*
* 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
* 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 <KoShapePainter.h>
#include <SvgParser.h>
#include <KoXmlReader.h>
#include <kis_vectorbrush_parasite.h>
//
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<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();
void newbrushTip();
// <------------------------ Brush Tip Functions ----------------------------->
// QList<KoShape*> int -> QImage
// Returns the proper QImage
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 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; */
......
/*
* 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
* 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,
......@@ -15,49 +15,46 @@
* 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_
#ifndef KIS_ANIMATED_BRUSHES_
#define KIS_ANIMATED_BRUSHES_
#include <KoShapePainter.h>
#include <SvgParser.h>
#include <KoXmlReader.h>
#include <kis_brushes_pipe.h>
#include <kis_vectoranimated_brush.h>
class BRUSH_EXPORT KisAnimatedBrushes : public KisBrushesPipe<KisVectorAnimatedBrush>
class KisVectorBrushParasite
{
public:
KisAnimatedBrushes();
// <-----------------Index Functions ------------------->
// KisPaintInformation -> int
// Returns and index for the next brush
int chooseNextBrush(const KisPaintInformation& info) override;
KisVectorBrushParasite();
// int -> int
// helper for chooseNextBrush
// Returns next index in sequence
int nextBrushSequence(int currentIndex, int numberOfImages);
/*
* This function gets the SVG and
* sets m_listOfImages
* */
void parseSvg(QByteArray svg);
QList<KoShape*> svgToKoShapes(QByteArray svg);
// int -> int
// helper for chooseNextBrush
// Returns a random index
int nextBrushRandom(int currentIndex, int numberOfImages);
QImage shapeToQImage(QList<KoShape*> shapes, int index);
QImage nextBrushTip();
void updateBrushIndexes(const KisPaintInformation& info, int seqNo);
void setMaxIndex(int max);
void setIndex(int index);
void nextIndex();
int maxIndex();
private:
bool m_isInitialized;
int m_currentIndex;
int m_numberOfImages;
QString m_typeOfBrush;
struct Private;
Private * const m_d;
QList<KoShape*> m_shapes;
QImage m_brushTip;
int m_maxIndex;
int m_index;
};
#endif /* __KIS_ANIMATED_BRUSHES_ */
#endif
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