Krita Source Code Documentation
Loading...
Searching...
No Matches
psd_colormode_block.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2009 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
7
8#include <psd_utils.h>
9#include <QColor>
10
12 : blocksize(0)
13 , colormode(colormode)
14{
15}
16
17bool PSDColorModeBlock::read(QIODevice &io)
18{
19 // get length
20 psdread(io, blocksize);
21
22 if (blocksize == 0) {
23 if (colormode == Indexed || colormode == DuoTone) {
24 error = "Blocksize of 0 and Indexed or DuoTone colormode";
25 return false;
26 }
27 else {
28 return true;
29 }
30 }
31
32 if (colormode == Indexed && blocksize != 768) {
33 error = QString("Indexed mode, but block size is %1.").arg(blocksize);
34 return false;
35 }
36
37 data = io.read(blocksize);
38 if ((quint32)data.size() != blocksize) return false;
39
40 if (colormode == Indexed) {
41 int i = 0;
42 while (i <= 767) {
43 colormap.append(qRgb(data[i],data[i + 1],data[i + 2]));
44 i += 3;
45 }
46 }
47 else {
49 }
50 return valid();
51}
52
53bool PSDColorModeBlock::write(QIODevice &io)
54{
55 if (!valid()) {
56 error = "Cannot write an invalid Color Mode Block";
57 return false;
58 }
59 if (colormap.size() > 0 && colormode == Indexed) {
60 error = "Cannot write indexed color data";
61 return false;
62 }
63 else if (duotoneSpecification.size() > 0 && colormode == DuoTone) {
64 quint32 blocksize = duotoneSpecification.size();
66 if (io.write(duotoneSpecification.constData(), blocksize) != blocksize) {
67 error = "Failed to write duotone specification";
68 return false;
69 }
70 }
71 else {
72 quint32 i = 0;
73 psdwrite(io, i);
74 }
75 return true;
76}
77
79{
80 if (blocksize == 0 && (colormode == Indexed || colormode == DuoTone)) {
81 error = "Blocksize of 0 and Indexed or DuoTone colormode";
82 return false;
83 }
84 if (colormode == Indexed && blocksize != 768) {
85 error = QString("Indexed mode, but block size is %1.").arg(blocksize);
86 return false;
87 }
88 if (colormode == DuoTone && blocksize == 0) {
89 error = QString("DuoTone mode, but data block is empty");
90 return false;
91 }
92 if ((quint32)data.size() != blocksize) {
93 error = QString("Data size is %1, but block size is %2").arg(data.size()).arg(blocksize);
94 return false;
95 }
96 return true;
97}
QByteArray duotoneSpecification
bool write(QIODevice &io)
psd_color_mode colormode
QList< QColor > colormap
bool read(QIODevice &io)
PSDColorModeBlock(psd_color_mode colormode)
psd_color_mode
Definition psd.h:50
@ DuoTone
Definition psd.h:57
@ Indexed
Definition psd.h:53
std::enable_if_t< std::is_arithmetic< T >::value, bool > psdread(QIODevice &io, T &v)
Definition psd_utils.h:397
std::enable_if_t< std::is_arithmetic< T >::value, bool > psdwrite(QIODevice &io, T v)
Definition psd_utils.h:170