Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_abr_brush_collection.cpp File Reference
#include <QtEndian>
#include "kis_abr_brush_collection.h"
#include "kis_abr_brush.h"
#include <QDomElement>
#include <QFile>
#include <QImage>
#include <QPoint>
#include <QColor>
#include <QByteArray>
#include <kis_debug.h>
#include <QString>
#include <QBuffer>
#include <QFileInfo>
#include <KoMD5Generator.h>
#include <klocalizedstring.h>
#include <KoColor.h>

Go to the source code of this file.

Classes

struct  AbrInfo
 

Functions

static bool abr_reach_8BIM_section (QDataStream &abr, const QString name)
 
static bool abr_read_content (QDataStream &abr, AbrInfo *abr_hdr)
 
static QString abr_read_ucs2_text (QDataStream &abr)
 
static bool abr_supported_content (AbrInfo *abr_hdr)
 
static QString abr_v1_brush_name (const QString filename, qint32 id)
 
static QImage convertToQImage (char *buffer, qint32 width, qint32 height)
 save the QImages as png files to directory image_tests
 
static qint32 find_sample_count_v6 (QDataStream &abr, AbrInfo *abr_info)
 
static qint32 rle_decode (QDataStream &abr, char *buffer, qint32 height)
 

Function Documentation

◆ abr_reach_8BIM_section()

static bool abr_reach_8BIM_section ( QDataStream & abr,
const QString name )
static

Definition at line 138 of file kis_abr_brush_collection.cpp.

139{
140 char tag[4];
141 char tagname[5];
142 qint32 section_size = 0;
143 int r;
144
145 // find 8BIMname section
146 while (!abr.atEnd()) {
147 r = abr.readRawData(tag, 4);
148
149 if (r != 4) {
150 warnKrita << "Error: Cannot read 8BIM tag ";
151 return false;
152 }
153
154 if (strncmp(tag, "8BIM", 4)) {
155 warnKrita << "Error: Start tag not 8BIM but " << (int)tag[0] << (int)tag[1] << (int)tag[2] << (int)tag[3] << " at position " << abr.device()->pos();
156 return false;
157 }
158
159 r = abr.readRawData(tagname, 4);
160
161 if (r != 4) {
162 warnKrita << "Error: Cannot read 8BIM tag name";
163 return false;
164 }
165 tagname[4] = '\0';
166
167 QString s1 = QString::fromLatin1(tagname, 4);
168
169 if (!s1.compare(name)) {
170 return true;
171 }
172
173 // long
174 abr >> section_size;
175 abr.device()->seek(abr.device()->pos() + section_size);
176 }
177 return true;
178}
QPointF s1
#define warnKrita
Definition kis_debug.h:87

References s1, and warnKrita.

◆ abr_read_content()

static bool abr_read_content ( QDataStream & abr,
AbrInfo * abr_hdr )
static

Definition at line 237 of file kis_abr_brush_collection.cpp.

238{
239
240 abr >> abr_hdr->version;
241 abr_hdr->subversion = 0;
242 abr_hdr->count = 0;
243
244 switch (abr_hdr->version) {
245 case 1:
246 case 2:
247 abr >> abr_hdr->count;
248 break;
249 case 6:
250 abr >> abr_hdr->subversion;
251 abr_hdr->count = find_sample_count_v6(abr, abr_hdr);
252 break;
253 default:
254 // unknown versions
255 break;
256 }
257 // next bytes in abr are samples data
258
259 return true;
260}
static qint32 find_sample_count_v6(QDataStream &abr, AbrInfo *abr_info)

References AbrInfo::count, find_sample_count_v6(), AbrInfo::subversion, and AbrInfo::version.

◆ abr_read_ucs2_text()

static QString abr_read_ucs2_text ( QDataStream & abr)
static

Definition at line 263 of file kis_abr_brush_collection.cpp.

264{
265 quint32 name_size;
266 quint32 buf_size;
267 uint i;
268 /* two-bytes characters encoded (UCS-2)
269 * format:
270 * long : size - number of characters in string
271 * data : zero terminated UCS-2 string
272 */
273
274 // long
275 abr >> name_size;
276 if (name_size == 0) {
277 return QString();
278 }
279
280 //buf_size = name_size * 2;
281 buf_size = name_size;
282
283 //name_ucs2 = (char*) malloc (buf_size * sizeof (char));
284 //name_ucs2 = new char[buf_size];
285
286 ushort * name_ucs2 = new ushort[buf_size];
287 for (i = 0; i < buf_size ; i++) {
288 //* char*/
289 //abr >> name_ucs2[i];
290
291 // I will use ushort as that is input to fromUtf16
292 abr >> name_ucs2[i];
293 }
294 QString name_utf8 = QString::fromUtf16(name_ucs2, buf_size);
295 delete [] name_ucs2;
296
297 return name_utf8;
298}
unsigned int uint

◆ abr_supported_content()

static bool abr_supported_content ( AbrInfo * abr_hdr)
static

Definition at line 123 of file kis_abr_brush_collection.cpp.

124{
125 switch (abr_hdr->version) {
126 case 1:
127 case 2:
128 return true;
129 break;
130 case 6:
131 if (abr_hdr->subversion == 1 || abr_hdr->subversion == 2)
132 return true;
133 break;
134 }
135 return false;
136}

References AbrInfo::subversion, and AbrInfo::version.

◆ abr_v1_brush_name()

static QString abr_v1_brush_name ( const QString filename,
qint32 id )
static

Definition at line 114 of file kis_abr_brush_collection.cpp.

115{
116 QString result = filename;
117 int pos = filename.lastIndexOf('.');
118 result.remove(pos, 4);
119 QTextStream(&result) << "_" << id;
120 return result;
121}

◆ convertToQImage()

static QImage convertToQImage ( char * buffer,
qint32 width,
qint32 height )
static

save the QImages as png files to directory image_tests

Definition at line 38 of file kis_abr_brush_collection.cpp.

39{
40 // create 8-bit indexed image
41 QImage img(width, height, QImage::Format_RGB32);
42 int pos = 0;
43 int value = 0;
44 for (int y = 0; y < height; y++) {
45 QRgb *pixel = reinterpret_cast<QRgb *>(img.scanLine(y));
46 for (int x = 0; x < width; x++, pos++) {
47 value = 255 - buffer[pos];
48 pixel[x] = qRgb(value, value , value);
49 }
50
51 }
52
53 return img;
54}
float value(const T *src, size_t ch)
unsigned int QRgb

References value().

◆ find_sample_count_v6()

static qint32 find_sample_count_v6 ( QDataStream & abr,
AbrInfo * abr_info )
static

Definition at line 180 of file kis_abr_brush_collection.cpp.

181{
182 qint64 origin;
183 qint32 sample_section_size;
184 qint32 sample_section_end;
185 qint32 samples = 0;
186 qint32 data_start;
187
188 qint32 brush_size;
189 qint32 brush_end;
190
191 if (!abr_supported_content(abr_info))
192 return 0;
193
194 origin = abr.device()->pos();
195
196 if (!abr_reach_8BIM_section(abr, "samp")) {
197 // reset to origin
198 abr.device()->seek(origin);
199 return 0;
200 }
201
202 // long
203 abr >> sample_section_size;
204 sample_section_end = sample_section_size + abr.device()->pos();
205
206 if(sample_section_end < 0 || sample_section_end > abr.device()->size())
207 return 0;
208
209 data_start = abr.device()->pos();
210
211 while ((!abr.atEnd()) && (abr.device()->pos() < sample_section_end)) {
212 // read long
213 abr >> brush_size;
214 brush_end = brush_size;
215 // complement to 4
216 while (brush_end % 4 != 0) brush_end++;
217
218 qint64 newPos = abr.device()->pos() + brush_end;
219 if(newPos > 0 && newPos < abr.device()->size()) {
220 abr.device()->seek(newPos);
221 }
222 else
223 return 0;
224
225 samples++;
226 }
227
228 // set stream to samples data
229 abr.device()->seek(data_start);
230
231 //dbgKrita <<"samples : "<< samples;
232 return samples;
233}
static bool abr_reach_8BIM_section(QDataStream &abr, const QString name)
static bool abr_supported_content(AbrInfo *abr_hdr)

References abr_reach_8BIM_section(), and abr_supported_content().

◆ rle_decode()

static qint32 rle_decode ( QDataStream & abr,
char * buffer,
qint32 height )
static

Definition at line 56 of file kis_abr_brush_collection.cpp.

57{
58 qint32 n;
59 char ptmp;
60 char ch;
61 int i, j, c;
62 short *cscanline_len;
63 char *data = buffer;
64
65 // read compressed size foreach scanline
66 cscanline_len = new short[ height ];
67 for (i = 0; i < height; i++) {
68 // short
69 abr >> cscanline_len[i];
70 }
71
72 // unpack each scanline data
73 for (i = 0; i < height; i++) {
74 for (j = 0; j < cscanline_len[i];) {
75 // char
76 if (!abr.device()->getChar(&ptmp)) {
77 break;
78 }
79 n = ptmp;
80
81 j++;
82 if (n >= 128) // force sign
83 n -= 256;
84 if (n < 0) { // copy the following char -n + 1 times
85 if (n == -128) // it's a nop
86 continue;
87 n = -n + 1;
88 // char
89 if (!abr.device()->getChar(&ch)) {
90 break;
91 }
92
93 j++;
94 for (c = 0; c < n; c++, data++) {
95 *data = ch;
96 }
97 }
98 else {
99 // read the following n + 1 chars (no compr)
100 for (c = 0; c < n + 1; c++, j++, data++) {
101 // char
102 if (!abr.device()->getChar(data)) {
103 break;
104 }
105 }
106 }
107 }
108 }
109 delete [] cscanline_len;
110 return 0;
111}