22#include "kis_pattern.h"
25#include <netinet/in.h>
33#include <tqvaluevector.h>
42#include "kis_paint_device.h"
45 struct GimpPatternHeader {
46 TQ_UINT32 header_size;
51 TQ_UINT32 magic_number;
55 TQ_UINT32
const GimpPatternMagic = ((
'G' << 24) + (
'P' << 16) + (
'A' << 8) + (
'T' << 0));
58KisPattern::KisPattern(
const TQString& file) : super(file), m_hasFile(true)
62KisPattern::KisPattern(
KisPaintDevice* image,
int x,
int y,
int w,
int h)
63 : super(
""), m_hasFile(false)
68 setName(image->name());
71KisPattern::~KisPattern()
80 TQFile file(filename());
81 file.open(IO_ReadOnly);
82 TQByteArray data = file.readAll();
83 if (!data.isEmpty()) {
84 TQ_INT32 startPos = m_data.size();
86 m_data.resize(m_data.size() + data.count());
87 memcpy(&m_data[startPos], data.data(), data.count());
95 TQFile file(filename());
96 file.open(IO_WriteOnly | IO_Truncate);
98 TQTextStream stream(&file);
107 GimpPatternHeader ph;
108 TQCString utf8Name = name().utf8();
109 char const* name = utf8Name.data();
110 int nameLength = tqstrlen(name);
112 ph.header_size = htonl(
sizeof(GimpPatternHeader) + nameLength + 1);
113 ph.version = htonl(1);
114 ph.width = htonl(width());
115 ph.height = htonl(height());
117 ph.magic_number = htonl(GimpPatternMagic);
120 bytes.setRawData(
reinterpret_cast<char*
>(&ph),
sizeof(GimpPatternHeader));
121 int wrote = file.writeBlock(bytes);
122 bytes.resetRawData(
reinterpret_cast<char*
>(&ph),
sizeof(GimpPatternHeader));
127 wrote = file.writeBlock(name, nameLength + 1);
132 bytes.resize(width() * height() * 4);
133 for (TQ_INT32 y = 0; y < height(); y++) {
134 for (TQ_INT32 x = 0; x < width(); x++) {
136 TQRgb pixel = m_img.pixel(x,y);
137 bytes[k++] =
static_cast<char>(tqRed(pixel));
138 bytes[k++] =
static_cast<char>(tqGreen(pixel));
139 bytes[k++] =
static_cast<char>(tqBlue(pixel));
140 bytes[k++] =
static_cast<char>(tqAlpha(pixel));
144 wrote = file.writeBlock(bytes);
158bool KisPattern::init()
161 GimpPatternHeader bh;
163 TQValueVector<char> name;
165 if (
sizeof(GimpPatternHeader) > m_data.size()) {
169 memcpy(&bh, &m_data[0],
sizeof(GimpPatternHeader));
170 bh.header_size = ntohl(bh.header_size);
171 bh.version = ntohl(bh.version);
172 bh.width = ntohl(bh.width);
173 bh.height = ntohl(bh.height);
174 bh.bytes = ntohl(bh.bytes);
175 bh.magic_number = ntohl(bh.magic_number);
177 if (bh.header_size > m_data.size() || bh.header_size == 0) {
181 name.resize(bh.header_size -
sizeof(GimpPatternHeader));
182 memcpy(&name[0], &m_data[
sizeof(GimpPatternHeader)], name.size());
184 if (name[name.size() - 1]) {
188 setName(i18n(&name[0]));
190 if (bh.width == 0 || bh.height == 0 || !m_img.create(bh.width, bh.height, 32)) {
200 for (TQ_UINT32 y = 0; y < bh.height; y++) {
201 for (TQ_UINT32 x = 0; x < bh.width; x++, k++) {
202 if (
static_cast<TQ_UINT32
>(k) > m_data.size()) {
203 kdDebug(DBG_AREA_FILE) <<
"failed in gray\n";
208 m_img.setPixel(x, y, tqRgb(val, val, val));
209 m_img.setAlphaBuffer(
false);
212 }
else if (bh.bytes == 2) {
216 for (TQ_UINT32 y = 0; y < bh.height; y++) {
217 for (TQ_UINT32 x = 0; x < bh.width; x++, k++) {
218 if (
static_cast<TQ_UINT32
>(k + 2) > m_data.size()) {
219 kdDebug(DBG_AREA_FILE) <<
"failed in grayA\n";
225 m_img.setPixel(x, y, tqRgba(val, val, val, alpha));
226 m_img.setAlphaBuffer(
true);
229 }
else if (bh.bytes == 3) {
231 for (TQ_UINT32 y = 0; y < bh.height; y++) {
232 for (TQ_UINT32 x = 0; x < bh.width; x++) {
233 if (
static_cast<TQ_UINT32
>(k + 3) > m_data.size()) {
234 kdDebug(DBG_AREA_FILE) <<
"failed in RGB\n";
238 m_img.setPixel(x, y, tqRgb(m_data[k],
242 m_img.setAlphaBuffer(
false);
245 }
else if (bh.bytes == 4) {
247 for (TQ_UINT32 y = 0; y < bh.height; y++) {
248 for (TQ_UINT32 x = 0; x < bh.width; x++) {
249 if (
static_cast<TQ_UINT32
>(k + 4) > m_data.size()) {
250 kdDebug(DBG_AREA_FILE) <<
"failed in RGBA\n";
254 m_img.setPixel(x, y, tqRgba(m_data[k],
259 m_img.setAlphaBuffer(
true);
266 if (m_img.isNull()) {
270 setWidth(m_img.width());
271 setHeight(m_img.height());
278KisPaintDeviceSP KisPattern::image(
KisColorSpace * colorSpace) {
280 TQMap<TQString, KisPaintDeviceSP>::const_iterator it = m_colorspaces.find(colorSpace->
id().id());
281 if (it != m_colorspaces.end())
285 KisPaintDeviceSP layer =
new KisPaintDevice(colorSpace,
"pattern");
289 layer->convertFromTQImage(m_img,
"");
291 m_colorspaces[colorSpace->
id().id()] = layer;
295TQ_INT32 KisPattern::width()
const
300void KisPattern::setWidth(TQ_INT32 w)
305TQ_INT32 KisPattern::height()
const
310void KisPattern::setHeight(TQ_INT32 h)
315void KisPattern::setImage(
const TQImage& img)
321 setWidth(img.width());
322 setHeight(img.height());
327KisPattern* KisPattern::clone()
const
329 KisPattern* pattern =
new KisPattern(
"");
330 pattern->setImage(m_img);
331 pattern->setName(name());
335#include "kis_pattern.moc"
A colorspace is the definition of a certain color model in Chalk.
virtual KisID id() const =0
Chalk definition for use in .kra files and internally: unchanging name + i18n'able description.
A paint device contains the actual pixel data and offers methods to read and write pixels.
virtual TQImage convertToTQImage(KisProfile *dstProfile, TQ_INT32 x, TQ_INT32 y, TQ_INT32 w, TQ_INT32 h, float exposure=0.0f)
Create an RGBA TQImage from a rectangle in the paint device.
virtual TQImage img()=0
Returns a TQImage representing this resource.
virtual bool save()=0
Save this resource asynchronously.
virtual bool load()=0
Load this resource.