1
// Copyright (C) 2002-2011 Nikolaus Gebhardt
2
// This file is part of the "Irrlicht Engine".
3
// For conditions of distribution and use, see copyright notice in irrlicht.h
5
#include "CImageLoaderPSD.h"
7
#ifdef _IRR_COMPILE_WITH_PSD_LOADER_
12
#include "irrString.h"
22
CImageLoaderPSD::CImageLoaderPSD()
25
setDebugName("CImageLoaderPSD");
30
//! returns true if the file maybe is able to be loaded by this class
31
//! based on the file extension (e.g. ".tga")
32
bool CImageLoaderPSD::isALoadableFileExtension(const io::path& filename) const
34
return core::hasFileExtension ( filename, "psd" );
39
//! returns true if the file maybe is able to be loaded by this class
40
bool CImageLoaderPSD::isALoadableFileFormat(io::IReadFile* file) const
46
file->read(&type, sizeof(u8)*3);
47
return (type[2]==2); // we currently only handle tgas of type 2.
52
//! creates a surface from the file
53
IImage* CImageLoaderPSD::loadImage(io::IReadFile* file) const
58
file->read(&header, sizeof(PsdHeader));
60
#ifndef __BIG_ENDIAN__
61
header.version = os::Byteswap::byteswap(header.version);
62
header.channels = os::Byteswap::byteswap(header.channels);
63
header.height = os::Byteswap::byteswap(header.height);
64
header.width = os::Byteswap::byteswap(header.width);
65
header.depth = os::Byteswap::byteswap(header.depth);
66
header.mode = os::Byteswap::byteswap(header.mode);
69
if (header.signature[0] != '8' ||
70
header.signature[1] != 'B' ||
71
header.signature[2] != 'P' ||
72
header.signature[3] != 'S')
75
if (header.version != 1)
77
os::Printer::log("Unsupported PSD file version", file->getFileName(), ELL_ERROR);
81
if (header.mode != 3 || header.depth != 8)
83
os::Printer::log("Unsupported PSD color mode or depth.\n", file->getFileName(), ELL_ERROR);
87
// skip color mode data
90
file->read(&l, sizeof(u32));
91
#ifndef __BIG_ENDIAN__
92
l = os::Byteswap::byteswap(l);
94
if (!file->seek(l, true))
96
os::Printer::log("Error seeking file pos to image resources.\n", file->getFileName(), ELL_ERROR);
100
// skip image resources
102
file->read(&l, sizeof(u32));
103
#ifndef __BIG_ENDIAN__
104
l = os::Byteswap::byteswap(l);
106
if (!file->seek(l, true))
108
os::Printer::log("Error seeking file pos to layer and mask.\n", file->getFileName(), ELL_ERROR);
114
file->read(&l, sizeof(u32));
115
#ifndef __BIG_ENDIAN__
116
l = os::Byteswap::byteswap(l);
118
if (!file->seek(l, true))
120
os::Printer::log("Error seeking file pos to image data section.\n", file->getFileName(), ELL_ERROR);
127
file->read(&compressionType, sizeof(u16));
128
#ifndef __BIG_ENDIAN__
129
compressionType = os::Byteswap::byteswap(compressionType);
132
if (compressionType != 1 && compressionType != 0)
134
os::Printer::log("Unsupported psd compression mode.\n", file->getFileName(), ELL_ERROR);
138
// create image data block
140
imageData = new u32[header.width * header.height];
144
if (compressionType == 0)
145
res = readRawImageData(file, header, imageData); // RAW image data
147
res = readRLEImageData(file, header, imageData); // RLE compressed data
149
video::IImage* image = 0;
154
image = new CImage(ECF_A8R8G8B8,
155
core::dimension2d<u32>(header.width, header.height), imageData);
166
bool CImageLoaderPSD::readRawImageData(io::IReadFile* file, const PsdHeader& header, u32* imageData) const
168
u8* tmpData = new u8[header.width * header.height];
170
for (s32 channel=0; channel<header.channels && channel < 3; ++channel)
172
if (!file->read(tmpData, sizeof(c8) * header.width * header.height))
174
os::Printer::log("Error reading color channel\n", file->getFileName(), ELL_ERROR);
178
s16 shift = getShiftFromChannel((c8)channel, header);
181
u32 mask = 0xff << shift;
183
for (u32 x=0; x<header.width; ++x)
185
for (u32 y=0; y<header.height; ++y)
187
s32 index = x + y*header.width;
188
imageData[index] = ~(~imageData[index] | mask);
189
imageData[index] |= tmpData[index] << shift;
201
bool CImageLoaderPSD::readRLEImageData(io::IReadFile* file, const PsdHeader& header, u32* imageData) const
203
/* If the compression code is 1, the image data
204
starts with the byte counts for all the scan lines in the channel
205
(LayerBottom LayerTop), with each count stored as a two
206
byte value. The RLE compressed data follows, with each scan line
207
compressed separately. The RLE compression is the same compres-sion
208
algorithm used by the Macintosh ROM routine PackBits, and
210
If the Layer's Size, and therefore the data, is odd, a pad byte will
211
be inserted at the end of the row.
215
A pseudo code fragment to unpack might look like this:
217
Loop until you get the number of unpacked bytes you are expecting:
218
Read the next source byte into n.
219
If n is between 0 and 127 inclusive, copy the next n+1 bytes literally.
220
Else if n is between -127 and -1 inclusive, copy the next byte -n+1
222
Else if n is -128, noop.
225
In the inverse routine, it is best to encode a 2-byte repeat run as a replicate run
226
except when preceded and followed by a literal run. In that case, it is best to merge
227
the three runs into one literal run. Always encode 3-byte repeats as replicate runs.
228
That is the essence of the algorithm. Here are some additional rules:
229
- Pack each row separately. Do not compress across row boundaries.
230
- The number of uncompressed bytes per row is defined to be (ImageWidth + 7)
231
/ 8. If the uncompressed bitmap is required to have an even number of bytes per
232
row, decompress into word-aligned buffers.
233
- If a run is larger than 128 bytes, encode the remainder of the run as one or more
234
additional replicate runs.
235
When PackBits data is decompressed, the result should be interpreted as per com-pression
236
type 1 (no compression).
239
u8* tmpData = new u8[header.width * header.height];
240
u16 *rleCount= new u16 [header.height * header.channels];
244
for (u32 y=0; y<header.height * header.channels; ++y)
246
if (!file->read(&rleCount[y], sizeof(u16)))
250
os::Printer::log("Error reading rle rows\n", file->getFileName(), ELL_ERROR);
254
#ifndef __BIG_ENDIAN__
255
rleCount[y] = os::Byteswap::byteswap(rleCount[y]);
260
s8 *buf = new s8[size];
261
if (!file->read(buf, size))
266
os::Printer::log("Error reading rle rows\n", file->getFileName(), ELL_ERROR);
270
u16 *rcount=rleCount;
277
// decompress packbit rle
279
for (s32 channel=0; channel<header.channels; channel++)
281
for (u32 y=0; y<header.height; ++y, ++rcount)
284
dest = &tmpData[y*header.width];
286
while (bytesRead < *rcount)
319
s16 shift = getShiftFromChannel((c8)channel, header);
323
u32 mask = 0xff << shift;
325
for (u32 x=0; x<header.width; ++x)
326
for (u32 y=0; y<header.height; ++y)
328
s32 index = x + y*header.width;
329
imageData[index] = ~(~imageData[index] | mask);
330
imageData[index] |= tmpData[index] << shift;
343
s16 CImageLoaderPSD::getShiftFromChannel(c8 channelNr, const PsdHeader& header) const
354
return header.channels == 4 ? 24 : -1; // ?
364
//! creates a loader which is able to load tgas
365
IImageLoader* createImageLoaderPSD()
367
return new CImageLoaderPSD();
371
} // end namespace video
372
} // end namespace irr