Krita Source Code Documentation
Loading...
Searching...
No Matches
KoPattern Class Reference

Write API docs here. More...

#include <KoPattern.h>

+ Inheritance diagram for KoPattern:

Public Member Functions

KoResourceSP clone () const override
 
KoPatternSP cloneWithoutAlpha () const
 
QString defaultFileExtension () const override
 
bool hasAlpha () const
 
qint32 height () const
 
 KoPattern (const KoPattern &rhs)
 
 KoPattern (const QImage &image, const QString &name, const QString &filename)
 
 KoPattern (const QString &filename)
 
bool loadFromDevice (QIODevice *dev, KisResourcesInterfaceSP resourcesInterface) override
 
bool loadPatFromDevice (QIODevice *dev)
 
KoPatternoperator= (const KoPattern &rhs)=delete
 
QImage pattern () const
 pattern the actual pattern image
 
QPair< QString, QString > resourceType () const override
 
bool savePatToDevice (QIODevice *dev) const
 
bool saveToDevice (QIODevice *dev) const override
 
qint32 width () const
 
 ~KoPattern () override
 
- Public Member Functions inherited from KoResource
bool active () const
 
void addMetaData (QString key, QVariant value)
 store the given key, value pair in the resource
 
virtual void clearSideLoadedResources ()
 
virtual QList< KoResourceLoadResultembeddedResources (KisResourcesInterfaceSP globalResourcesInterface) const
 
QString filename () const
 
QImage image () const
 
bool isDirty () const
 
virtual bool isEphemeral () const
 
virtual bool isSerializable () const
 
 KoResource ()
 
 KoResource (const KoResource &rhs)
 
 KoResource (const QString &filename)
 
virtual QList< KoResourceLoadResultlinkedResources (KisResourcesInterfaceSP globalResourcesInterface) const
 
bool load (KisResourcesInterfaceSP resourcesInterface)
 
QString md5Sum (bool generateIfEmpty=true) const
 
QMap< QString, QVariant > metadata () const
 get a map with all the metadata
 
virtual QString name () const
 
KoResourceoperator= (const KoResource &rhs)=delete
 
bool permanent () const
 
virtual QList< int > requiredCanvasResources () const
 
QList< KoResourceLoadResultrequiredResources (KisResourcesInterfaceSP globalResourcesInterface) const
 
int resourceId () const
 
bool save ()
 
void setActive (bool active)
 
void setDirty (bool value)
 Mark the preset as modified but not saved.
 
void setFilename (const QString &filename)
 
void setImage (const QImage &image)
 
void setMD5Sum (const QString &md5sum)
 Set the md5sum of this resource. It must be in hex-encoded string format.
 
void setName (const QString &name)
 
void setPermanent (bool permanent)
 
void setResourceId (int id)
 
void setStorageLocation (const QString &location)
 
void setValid (bool valid)
 
void setVersion (int version)
 
virtual QList< KoResourceLoadResultsideLoadedResources (KisResourcesInterfaceSP globalResourcesInterface) const
 
KoResourceSignature signature () const
 
QString storageLocation () const
 
QList< KoResourceLoadResulttakeSideLoadedResources (KisResourcesInterfaceSP globalResourcesInterface)
 
virtual QImage thumbnail () const
 thumbnail the thumbnail image to use in resource selectors
 
virtual QString thumbnailPath () const
 thumbnailPath returns the path to a separate thumbnail image, outside the actual resource file itself. If the path is relative, it is supposed start in the same location as the resource itself. If it's absolute, that is, it starts with "/", it is from the root of the storage.
 
virtual void updateThumbnail ()
 updateThumbnail updates the thumbnail for this resource. Reimplement if your thumbnail is something else than the image set with setImage.
 
bool valid () const
 
int version () const
 Get the version of the resource.
 
virtual ~KoResource ()
 
- Public Member Functions inherited from Private
 Private (KisCanvas2 *c)
 

Private Member Functions

void checkForAlpha (const QImage &image)
 
void setPatternImage (const QImage &image)
 

Private Attributes

bool m_hasAlpha = false
 
QImage m_pattern
 

Additional Inherited Members

- Public Attributes inherited from KoResource
bool active {true}
 
QString filename
 
QImage image
 
QString md5sum
 
QMap< QString, QVariant > metadata
 
bool modified {false}
 
QString name
 
bool permanent {false}
 
int resourceId {-1}
 
QString storageLocation
 
bool valid {false}
 
int version {-1}
 
- Public Attributes inherited from Private
KisCanvas2canvas
 
int displayedFrame
 
int intendedFrame
 

Detailed Description

Write API docs here.

Definition at line 20 of file KoPattern.h.

Constructor & Destructor Documentation

◆ KoPattern() [1/3]

KoPattern::KoPattern ( const QString & filename)
explicit

Creates a new KoPattern object using filename. No file is opened in the constructor, you have to call load.

Parameters
filenamethe file name to save and load from.

Definition at line 50 of file KoPattern.cpp.

51 : KoResource(file)
52{
53}

◆ KoPattern() [2/3]

KoPattern::KoPattern ( const QImage & image,
const QString & name,
const QString & filename )

Create a new pattern from scratch, without loading it from a file

Parameters
imagethe pattern
namethe name of the pattern
filenamethe filename of the pattern (note that this filename does not need to exist)

Definition at line 55 of file KoPattern.cpp.

56 : KoResource(QString())
57{
61}
void setPatternImage(const QImage &image)
void setName(const QString &name)
QImage image
void setFilename(const QString &filename)
QString filename
QString name

References KoResource::filename, KoResource::image, KoResource::name, KoResource::setFilename(), KoResource::setName(), and setPatternImage().

◆ ~KoPattern()

KoPattern::~KoPattern ( )
override

Definition at line 64 of file KoPattern.cpp.

65{
66}

◆ KoPattern() [3/3]

KoPattern::KoPattern ( const KoPattern & rhs)

Definition at line 68 of file KoPattern.cpp.

69 : KoResource(rhs)
70 , m_pattern(rhs.m_pattern)
71{
72}
QImage m_pattern
Definition KoPattern.h:88

Member Function Documentation

◆ checkForAlpha()

void KoPattern::checkForAlpha ( const QImage & image)
private

Definition at line 359 of file KoPattern.cpp.

359 {
360 m_hasAlpha = false;
361 for (int y = 0; y < image.height(); y++) {
362 for (int x = 0; x < image.width(); x++) {
363 if (qAlpha(image.pixel(x, y)) != 255) {
364 m_hasAlpha = true;
365 break;
366 }
367 }
368 }
369}
bool m_hasAlpha
Definition KoPattern.h:89

References KoResource::image, and m_hasAlpha.

◆ clone()

KoResourceSP KoPattern::clone ( ) const
overridevirtual

Implements KoResource.

Definition at line 74 of file KoPattern.cpp.

75{
76 return KoResourceSP(new KoPattern(*this));
77}
KoPattern(const QString &filename)
Definition KoPattern.cpp:50
QSharedPointer< KoResource > KoResourceSP

References KoPattern().

◆ cloneWithoutAlpha()

KoPatternSP KoPattern::cloneWithoutAlpha ( ) const

Create a copy of this pattern removing all the transparency from it. The fully transparent color becomes 100% black. The name and the filename of the new pattern are kept the same.

If hasAlpha() is false, the function just returns a simple clone of this pattern.

Definition at line 376 of file KoPattern.cpp.

377{
378 if (!hasAlpha()) return clone().dynamicCast<KoPattern>();
379
380 QImage image = this->image();
381
382 for (int y = 0; y < image.height(); ++y) {
383 QRgb *ptr = reinterpret_cast<QRgb*>(image.scanLine(y));
384
385 for (int x = 0; x < image.width(); ++x) {
386 const qreal coeff = qAlpha(*ptr) / 255.0;
387 *ptr = qRgba(qRound(coeff * qRed(*ptr)), qRound(coeff * qGreen(*ptr)), qRound(coeff * qBlue(*ptr)), 255);
388 ptr++;
389 }
390 }
391
392 KoPatternSP flattenedPattern =
393 toQShared(new KoPattern(image, this->name(), this->filename()));
394
395 return flattenedPattern;
396}
Write API docs here.
Definition KoPattern.h:21
bool hasAlpha() const
KoResourceSP clone() const override
Definition KoPattern.cpp:74
unsigned int QRgb
QSharedPointer< T > toQShared(T *ptr)

References clone(), KoResource::filename, hasAlpha(), KoResource::image, KoPattern(), KoResource::name, and toQShared().

◆ defaultFileExtension()

QString KoPattern::defaultFileExtension ( ) const
overridevirtual
Returns
the default file extension which should be used when saving the resource

Reimplemented from KoResource.

Definition at line 348 of file KoPattern.cpp.

349{
350 return QString(".pat");
351}

◆ hasAlpha()

bool KoPattern::hasAlpha ( ) const

Definition at line 371 of file KoPattern.cpp.

372{
373 return m_hasAlpha;
374}

References m_hasAlpha.

◆ height()

qint32 KoPattern::height ( ) const

Definition at line 334 of file KoPattern.cpp.

335{
336 return m_pattern.height();
337}

References m_pattern.

◆ loadFromDevice()

bool KoPattern::loadFromDevice ( QIODevice * dev,
KisResourcesInterfaceSP resourcesInterface )
overridevirtual

Implements KoResource.

Definition at line 283 of file KoPattern.cpp.

284{
285 Q_UNUSED(resourcesInterface);
286
287 QByteArray ba = dev->readAll();
288
289 QBuffer buf(&ba);
290 buf.open(QBuffer::ReadOnly);
291
292 bool result = false;
293
294 if (QImageReader::supportedMimeTypes().contains(KisMimeDatabase::mimeTypeForData(ba).toLatin1())) {
295 QFileInfo fi(filename());
296 QImage image;
297 result = image.load(&buf, fi.suffix().toUpper().toLatin1());
299 }
300 else {
301 result = loadPatFromDevice(&buf);
302 }
303
304 return result;
305
306}
static QString mimeTypeForData(const QByteArray ba)
bool loadPatFromDevice(QIODevice *dev)
Definition KoPattern.cpp:79

References KoResource::filename, KoResource::image, loadPatFromDevice(), KisMimeDatabase::mimeTypeForData(), and setPatternImage().

◆ loadPatFromDevice()

bool KoPattern::loadPatFromDevice ( QIODevice * dev)

Definition at line 79 of file KoPattern.cpp.

80{
81 QByteArray bytes = dev->readAll();
82 int dataSize = bytes.size();
83 const char* data = bytes.constData();
84
85 // load Gimp patterns
86 GimpPatternHeader bh;
87 qint32 k;
88 char* name;
89
90 if ((int)sizeof(GimpPatternHeader) > dataSize) {
91 return false;
92 }
93
94 memcpy(&bh, data, sizeof(GimpPatternHeader));
95 bh.header_size = qFromBigEndian(bh.header_size);
96 bh.version = qFromBigEndian(bh.version);
97 bh.width = qFromBigEndian(bh.width);
98 bh.height = qFromBigEndian(bh.height);
99 bh.bytes = qFromBigEndian(bh.bytes);
100 bh.magic_number = qFromBigEndian(bh.magic_number);
101
102 if (bytes.mid(20, 4) != "GPAT") {
103 qWarning() << filename() << "is not a .pat pattern file";
104 return false;
105 }
106
107 if ((int)bh.header_size > dataSize || bh.header_size == 0) {
108 return false;
109 }
110 int size = bh.header_size - sizeof(GimpPatternHeader);
111 name = new char[size];
112 memcpy(name, data + sizeof(GimpPatternHeader), size);
113
114 if (name[size - 1]) {
115 delete[] name;
116 return false;
117 }
118
119 // size -1 so we don't add the end 0 to the QString...
120 QString newName = QString::fromUtf8(name, size - 1);
121 if (!newName.isEmpty()) { // if it's empty, it's better to leave the name that was there before (based on filename)
122 setName(newName);
123 }
124 delete[] name;
125
126 if (bh.width == 0 || bh.height == 0) {
127 return false;
128 }
129
130 QImage::Format imageFormat;
131
132 if (bh.bytes == 1 || bh.bytes == 3) {
133 imageFormat = QImage::Format_RGB32;
134 } else {
135 imageFormat = QImage::Format_ARGB32;
136 }
137
138 QImage pattern = QImage(bh.width, bh.height, imageFormat);
139 if (pattern.isNull()) {
140 return false;
141 }
142 k = bh.header_size;
143
144 if (bh.bytes == 1) {
145 // Grayscale
146 qint32 val;
147 for (quint32 y = 0; y < bh.height; ++y) {
148 QRgb* pixels = reinterpret_cast<QRgb*>( pattern.scanLine(y) );
149 for (quint32 x = 0; x < bh.width; ++x, ++k) {
150 if (k > dataSize) {
151 qWarning() << "failed to load grayscale pattern" << filename();
152 return false;
153 }
154
155 val = data[k];
156 pixels[x] = qRgb(val, val, val);
157 }
158 }
159 // It was grayscale, so make the pattern as small as possible
160 // by converting it to Indexed8
161 pattern.convertTo(QImage::Format_Indexed8);
162 }
163 else if (bh.bytes == 2) {
164 // Grayscale + A
165 qint32 val;
166 qint32 alpha;
167 for (quint32 y = 0; y < bh.height; ++y) {
168 QRgb* pixels = reinterpret_cast<QRgb*>( pattern.scanLine(y) );
169 for (quint32 x = 0; x < bh.width; ++x, ++k) {
170 if (k + 2 > dataSize) {
171 qWarning() << "failed to load grayscale +_ alpha pattern" << filename();
172 return false;
173 }
174
175 val = data[k];
176 alpha = data[k++];
177 pixels[x] = qRgba(val, val, val, alpha);
178 }
179 }
180 }
181 else if (bh.bytes == 3) {
182 // RGB without alpha
183 for (quint32 y = 0; y < bh.height; ++y) {
184 QRgb* pixels = reinterpret_cast<QRgb*>( pattern.scanLine(y) );
185 for (quint32 x = 0; x < bh.width; ++x) {
186 if (k + 3 > dataSize) {
187 qWarning() << "failed to load RGB pattern" << filename();
188 return false;
189 }
190 pixels[x] = qRgb(data[k],
191 data[k + 1],
192 data[k + 2]);
193 k += 3;
194 }
195 }
196 } else if (bh.bytes == 4) {
197 // Has alpha
198 for (quint32 y = 0; y < bh.height; ++y) {
199 QRgb* pixels = reinterpret_cast<QRgb*>( pattern.scanLine(y) );
200 for (quint32 x = 0; x < bh.width; ++x) {
201 if (k + 4 > dataSize) {
202 qWarning() << "failed to load RGB + Alpha pattern" << filename();
203 return false;
204 }
205
206 pixels[x] = qRgba(data[k],
207 data[k + 1],
208 data[k + 2],
209 data[k + 3]);
210 k += 4;
211 }
212 }
213 } else {
214 return false;
215 }
216
217 if (pattern.isNull()) {
218 return false;
219 }
220
222 setValid(true);
223
224 return true;
225
226}
QImage pattern() const
pattern the actual pattern image
int size(const Forest< T > &forest)
Definition KisForest.h:1232
void setValid(bool valid)

References KoResource::filename, KoResource::name, pattern(), KoResource::setName(), setPatternImage(), and KoResource::setValid().

◆ operator=()

KoPattern & KoPattern::operator= ( const KoPattern & rhs)
delete

◆ pattern()

QImage KoPattern::pattern ( ) const

pattern the actual pattern image

Returns
a valid QImage. There are no guarantees to the image format.

Definition at line 354 of file KoPattern.cpp.

355{
356 return m_pattern;
357}

References m_pattern.

◆ resourceType()

QPair< QString, QString > KoPattern::resourceType ( ) const
inlineoverridevirtual
Returns
the resource type

Implements KoResource.

Definition at line 60 of file KoPattern.h.

60 {
61 return QPair<QString, QString>(ResourceType::Patterns, "");
62 }
const QString Patterns

References ResourceType::Patterns.

◆ savePatToDevice()

bool KoPattern::savePatToDevice ( QIODevice * dev) const

Definition at line 228 of file KoPattern.cpp.

229{
230 // Header: header_size (24+name length),version,width,height,colordepth of brush,magic,name
231 // depth: 1 = greyscale, 2 = greyscale + A, 3 = RGB, 4 = RGBA
232 // magic = "GPAT", as a single uint32, the docs are wrong here!
233 // name is UTF-8 (\0-terminated! The docs say nothing about this!)
234 // _All_ data in network order, it seems! (not mentioned in gimp-2.2.8/devel-docs/pat.txt!!)
235 // We only save RGBA at the moment
236 // Version is 1 for now...
237
238
239
240 GimpPatternHeader ph;
241 QByteArray utf8Name = name().toUtf8();
242 char const* name = utf8Name.data();
243 int nameLength = qstrlen(name);
244
245 ph.header_size = qToBigEndian((quint32)sizeof(GimpPatternHeader) + nameLength + 1); // trailing 0
246 ph.version = qToBigEndian((quint32)1);
247 ph.width = qToBigEndian((quint32)width());
248 ph.height = qToBigEndian((quint32)height());
249 ph.bytes = qToBigEndian((quint32)4);
250 ph.magic_number = qToBigEndian((quint32)GimpPatternMagic);
251
252 QByteArray bytes = QByteArray::fromRawData(reinterpret_cast<char*>(&ph), sizeof(GimpPatternHeader));
253 int wrote = dev->write(bytes);
254 bytes.clear();
255
256 if (wrote == -1)
257 return false;
258
259 wrote = dev->write(name, nameLength + 1); // Trailing 0 apparently!
260 if (wrote == -1)
261 return false;
262
263 int k = 0;
264 bytes.resize(width() * height() * 4);
265 for (qint32 y = 0; y < height(); ++y) {
266 for (qint32 x = 0; x < width(); ++x) {
267 // RGBA only
268 QRgb pixel = m_pattern.pixel(x, y);
269 bytes[k++] = static_cast<char>(qRed(pixel));
270 bytes[k++] = static_cast<char>(qGreen(pixel));
271 bytes[k++] = static_cast<char>(qBlue(pixel));
272 bytes[k++] = static_cast<char>(qAlpha(pixel));
273 }
274 }
275
276 wrote = dev->write(bytes);
277 if (wrote == -1)
278 return false;
279
280 return true;
281}
qint32 width() const
qint32 height() const

References height(), m_pattern, KoResource::name, and width().

◆ saveToDevice()

bool KoPattern::saveToDevice ( QIODevice * dev) const
overridevirtual

Reimplemented from KoResource.

Definition at line 308 of file KoPattern.cpp.

309{
310 QFileInfo fi(filename());
311 QString fileExtension = fi.suffix().toUpper();
312
313 bool result = false;
314
315 if (fileExtension == "PAT") {
316 result = savePatToDevice(dev);
317 }
318 else {
319 if (fileExtension.isEmpty()) {
320 fileExtension = "PNG";
321 }
322 result = m_pattern.save(dev, fileExtension.toLatin1());
323 }
324
325 return result;
326}
bool savePatToDevice(QIODevice *dev) const
QLatin1String fileExtension(RecorderFormat format)

References KoResource::filename, m_pattern, and savePatToDevice().

◆ setPatternImage()

void KoPattern::setPatternImage ( const QImage & image)
private

Definition at line 339 of file KoPattern.cpp.

340{
344 setValid(true);
345}
void checkForAlpha(const QImage &image)
void setImage(const QImage &image)

References checkForAlpha(), KoResource::image, m_pattern, KoResource::setImage(), and KoResource::setValid().

◆ width()

qint32 KoPattern::width ( ) const

Definition at line 329 of file KoPattern.cpp.

330{
331 return m_pattern.width();
332}

References m_pattern.

Member Data Documentation

◆ m_hasAlpha

bool KoPattern::m_hasAlpha = false
private

Definition at line 89 of file KoPattern.h.

◆ m_pattern

QImage KoPattern::m_pattern
private

Definition at line 88 of file KoPattern.h.


The documentation for this class was generated from the following files: