81 QByteArray bytes = dev->readAll();
82 int dataSize = bytes.size();
83 const char* data = bytes.constData();
90 if ((
int)
sizeof(GimpPatternHeader) > dataSize) {
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);
102 if (bytes.mid(20, 4) !=
"GPAT") {
103 qWarning() <<
filename() <<
"is not a .pat pattern file";
107 if ((
int)bh.header_size > dataSize || bh.header_size == 0) {
110 int size = bh.header_size -
sizeof(GimpPatternHeader);
111 name =
new char[size];
112 memcpy(
name, data +
sizeof(GimpPatternHeader), size);
114 if (
name[size - 1]) {
120 QString newName = QString::fromUtf8(
name, size - 1);
121 if (!newName.isEmpty()) {
126 if (bh.width == 0 || bh.height == 0) {
130 QImage::Format imageFormat;
132 if (bh.bytes == 1 || bh.bytes == 3) {
133 imageFormat = QImage::Format_RGB32;
135 imageFormat = QImage::Format_ARGB32;
138 QImage
pattern = QImage(bh.width, bh.height, imageFormat);
147 for (quint32 y = 0; y < bh.height; ++y) {
149 for (quint32 x = 0; x < bh.width; ++x, ++k) {
151 qWarning() <<
"failed to load grayscale pattern" <<
filename();
156 pixels[x] = qRgb(val, val, val);
161 pattern.convertTo(QImage::Format_Indexed8);
163 else if (bh.bytes == 2) {
167 for (quint32 y = 0; y < bh.height; ++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();
177 pixels[x] = qRgba(val, val, val, alpha);
181 else if (bh.bytes == 3) {
183 for (quint32 y = 0; y < bh.height; ++y) {
185 for (quint32 x = 0; x < bh.width; ++x) {
186 if (k + 3 > dataSize) {
187 qWarning() <<
"failed to load RGB pattern" <<
filename();
190 pixels[x] = qRgb(data[k],
196 }
else if (bh.bytes == 4) {
198 for (quint32 y = 0; y < bh.height; ++y) {
200 for (quint32 x = 0; x < bh.width; ++x) {
201 if (k + 4 > dataSize) {
202 qWarning() <<
"failed to load RGB + Alpha pattern" <<
filename();
206 pixels[x] = qRgba(data[k],
240 GimpPatternHeader ph;
241 QByteArray utf8Name =
name().toUtf8();
242 char const*
name = utf8Name.data();
243 int nameLength = qstrlen(
name);
245 ph.header_size = qToBigEndian((quint32)
sizeof(GimpPatternHeader) + nameLength + 1);
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);
252 QByteArray bytes = QByteArray::fromRawData(
reinterpret_cast<char*
>(&ph),
sizeof(GimpPatternHeader));
253 int wrote = dev->write(bytes);
259 wrote = dev->write(
name, nameLength + 1);
265 for (qint32 y = 0; y <
height(); ++y) {
266 for (qint32 x = 0; x <
width(); ++x) {
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));
276 wrote = dev->write(bytes);