Commit 1912514d authored by Boudewijn Rempt's avatar Boudewijn Rempt

Some more work on loading patterns

parent 307de363
......@@ -2,6 +2,7 @@ set(libkispsd_LIB_SRCS
psd_utils.cpp
psd.cpp
compression.cpp
psd_pattern.cpp
)
kde4_add_library(libkispsd SHARED ${libkispsd_LIB_SRCS} )
......
......@@ -192,6 +192,7 @@ struct psd_gradient_color {
};
struct psd_pattern {
psd_color_mode color_mode; // The image mode of the file.
quint8 height; // Point: vertical, 2 bytes and horizontal, 2 bytes
quint8 width;
......
/*
* Copyright (c) 2014 Boudewijn Rempt <boud@valdyas.org>
*
* 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 "psd_pattern.h"
#include "psd_utils.h"
#include <QImage>
#include <QDebug>
struct PsdPattern::Private
{
KoPattern *patternResource;
};
PsdPattern::PsdPattern()
: d(new Private())
{
d->patternResource = 0;
}
PsdPattern::~PsdPattern()
{
delete d;
}
void PsdPattern::setPattern(KoPattern *pattern)
{
d->patternResource = pattern;
}
KoPattern *PsdPattern::pattern() const
{
return d->patternResource;
}
bool psd_write_pattern(QIODevice *io)
{
return false;
}
bool psd_read_pattern(QIODevice *io)
{
quint32 pattern_length;
psd_pattern pattern;
memset(&pattern, 0, sizeof(psd_pattern));
psdread(io, &pattern_length);
pattern_length = (pattern_length + 3) & ~3;
psdread(io, &pattern.version);
if (pattern.version != 1) return false;
psdread(io, (quint32*)&pattern.color_mode);
psdread(io, &pattern.height);
psdread(io, &pattern.width);
psdread_unicodestring(io, pattern.name);
psdread_pascalstring(io, pattern.uuid, 2);
if (pattern.color_mode == Indexed) {
pattern.color_table.reserve(256);
quint8 r;
quint8 g;
quint8 b;
for (int i = 0; i < 256; ++i) {
psdread(io, &r);
psdread(io, &g);
psdread(io, &b);
pattern.color_table.append(qRgb(r, g, b));
}
}
// Now load the virtual memory array
psdread(io, &pattern.version);
if (!pattern.version == 3) return false;
quint32 vm_array_length;
psdread(io, &vm_array_length);
psdread(io, &pattern.top);
psdread(io, &pattern.left);
psdread(io, &pattern.bottom);
psdread(io, &pattern.right);
QImage img;
if (pattern.color_mode == Indexed) {
img = QImage(pattern.width, pattern.height, QImage::Format_Indexed8);
img.setColorTable(pattern.color_table);
}
else {
img = QImage(pattern.width, pattern.height, QImage::Format_ARGB32);
}
qint32 max_channels;
psdread(io, &max_channels);
for (int i = 0; i < max_channels; ++i) {
quint32 written;
qint32 len;
quint32 pixel_depth1;
quint32 top;
quint32 left;
quint32 bottom;
quint32 right;
quint16 pixel_depth2;
quint8 compression_mode;
psdread(io, &written);
psdread(io, &len);
len -= 4 + 4 * 4 + 2 + 1;
if (len < 0) {
continue;
}
if (written > 0) {
if (pattern.channel_number == 0) {
psdread(io, &pixel_depth1);
}
else {
quint32 d;
psdread(io, &d);
Q_ASSERT(d == pixel_depth1);
}
}
else {
quint32 d;
psdread(io, &d);
}
psdread(io, &top);
psdread(io, &left);
psdread(io, &bottom);
psdread(io, &right);
psdread(io, &pixel_depth2);
psdread(io, &compression_mode);
quint32 per_channel_length = 0;
quint8 *temp_channel_data = new quint8(len);
if (written > 0) {
if (pattern.channel_number == 0) {
qint32 pixels;
qint32 length;
pixels = length = pattern.width * pattern.height;
switch(pixel_depth1) {
case 1:
length = (pattern.width + 7) / 8 * pattern.height;
break;
case 8:
break;
case 16:
length *= 2;
pixels *= 2;
default:
qDebug() << "Wrong depth for pattern";
return false;
}
quint32 per_channel_length = length;
switch(pattern.color_mode) {
case Bitmap:
case Indexed:
break;
case Grayscale:
case DuoTone:
length *= 2;
break;
case RGB:
length *= 4;
break;
case CMYK:
length *= 5;
break;
case Lab:
length *= 4;
break;
case MultiChannel:
length *= 4;
default:
qDebug() << "Impossible color mode" << pattern.color_mode;
delete temp_channel_data;
return false;
}
}
}
delete temp_channel_data;
}
return true;
}
/*
* Copyright (c) 2014 Boudewijn Rempt <boud@valdyas.org>
*
* 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.
*/
#ifndef PSD_PATTERN_H
#define PSD_PATTERN_H
#include "psd.h"
#include <QIODevice>
#include <KoPattern.h>
class LIBKISPSD_EXPORT PsdPattern
{
public:
PsdPattern();
~PsdPattern();
void setPattern(KoPattern *pattern);
KoPattern *pattern() const;
bool psd_write_pattern(QIODevice* io);
bool psd_read_pattern(QIODevice* io);
private:
struct Private;
Private * const d;
};
#endif // PSD_PATTERN_H
......@@ -233,53 +233,6 @@ bool psd_read_blendmode(QIODevice *io, QString &blendModeKey)
}
bool psd_write_pattern(QIODevice *io, KoPattern *pattern)
{
return false;
}
bool psd_read_pattern(QIODevice *io, KoPattern *pattern)
{
quint32 length;
psd_pattern pat;
psdread(io, &length);
psdread(io, &pat.version);
if (pat.version != 1) return false;
psdread(io, (quint32*)&pat.color_mode);
psdread(io, &pat.height);
psdread(io, &pat.width);
psdread_unicodestring(io, pat.name);
psdread_pascalstring(io, pat.uuid, 2);
if (pat.color_mode == Indexed) {
pat.color_table.reserve(256);
quint8 r;
quint8 g;
quint8 b;
for (int i = 0; i < 256; ++i) {
psdread(io, &r);
psdread(io, &g);
psdread(io, &b);
pat.color_table.append(qRgb(r, g, b));
}
}
// Now load the virtual memory array
psdread(io, &pat.version);
if (!pat.version == 3) return false;
psdread(io, &length);
psdread(io, &pat.top);
psdread(io, &pat.left);
psdread(io, &pat.bottom);
psdread(io, &pat.right);
quint32 max_channels;
psdread(io, &max_channels);
return true;
}
bool psdread_unicodestring(QIODevice *io, QString &s)
......
......@@ -37,8 +37,6 @@ bool LIBKISPSD_EXPORT psdwrite_pascalstring(QIODevice* io, const QString &s);
bool LIBKISPSD_EXPORT psdwrite_pascalstring(QIODevice* io, const QString &s, int padding);
bool LIBKISPSD_EXPORT psdpad(QIODevice* io, quint32 padding);
bool LIBKISPSD_EXPORT psd_write_pattern(QIODevice* io, KoPattern *pattern);
bool LIBKISPSD_EXPORT psdread(QIODevice* io, quint8* v);
bool LIBKISPSD_EXPORT psdread(QIODevice* io, quint16* v);
bool LIBKISPSD_EXPORT psdread(QIODevice* io, qint16* v);
......@@ -49,6 +47,5 @@ bool LIBKISPSD_EXPORT psdread_pascalstring(QIODevice* io, QString& s, int paddin
bool LIBKISPSD_EXPORT psdread_unicodestring(QIODevice* io, QString &s);
bool LIBKISPSD_EXPORT psd_read_blendmode(QIODevice* io, QString &blendModeKey);
bool LIBKISPSD_EXPORT psd_read_pattern(QIODevice* io, KoPattern *pattern);
#endif // PSD_UTILS_H
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