23 #include <tqdatastream.h>
27 typedef TQ_UINT32 uint;
28 typedef TQ_UINT16 ushort;
29 typedef TQ_UINT8 uchar;
55 static TQDataStream & operator>> ( TQDataStream & s, PSDHeader & header )
57 s >> header.signature;
59 for(
int i = 0; i < 6; i++ ) {
60 s >> header.reserved[i];
62 s >> header.channel_count;
66 s >> header.color_mode;
69 static bool seekBy(TQDataStream& s,
unsigned int bytes)
73 unsigned int num= TQMIN(bytes,
sizeof(buf));
75 s.readRawBytes(buf, l);
84 static bool IsValid(
const PSDHeader & header )
86 if( header.signature != 0x38425053 ) {
93 static bool IsSupported(
const PSDHeader & header )
95 if( header.version != 1 ) {
98 if( header.channel_count > 16 ) {
101 if( header.depth != 8 ) {
104 if( header.color_mode != CM_RGB ) {
111 static bool LoadPSD( TQDataStream & s,
const PSDHeader & header, TQImage & img )
114 if( !img.create( header.width, header.height, 32 )) {
122 s.device()->at( s.device()->at() + tmp );
126 s.device()->at( s.device()->at() + tmp );
130 s.device()->at( s.device()->at() + tmp );
139 if( compression > 1 ) {
144 uint channel_num = header.channel_count;
147 if( channel_num < 4 ) {
148 img.fill(tqRgba(0, 0, 0, 0xFF));
152 img.setAlphaBuffer(
true );
158 const uint pixel_count = header.height * header.width;
160 static const uint components[4] = {2, 1, 0, 3};
165 if(!seekBy(s, header.height*header.channel_count*
sizeof(ushort)))
169 for(uint channel = 0; channel < channel_num; channel++) {
171 uchar * ptr = img.bits() + components[channel];
174 while( count < pixel_count ) {
185 if ( count > pixel_count )
194 else if( len > 128 ) {
200 if(s.atEnd() || count > pixel_count)
210 else if( len == 128 ) {
221 for(uint channel = 0; channel < channel_num; channel++) {
223 uchar * ptr = img.bits() + components[channel];
226 uint count = pixel_count;
227 while( count != 0 ) {
241 void kimgio_psd_read( TQImageIO *io )
243 TQDataStream s( io->ioDevice() );
244 s.setByteOrder( TQDataStream::BigEndian );
250 if( s.atEnd() || !IsValid( header ) ) {
251 kdDebug(399) <<
"This PSD file is not valid." << endl;
252 io->setImage( TQImage() );
258 if( !IsSupported( header ) ) {
259 kdDebug(399) <<
"This PSD file is not supported." << endl;
260 io->setImage( TQImage() );
266 if( !LoadPSD(s, header, img) ) {
267 kdDebug(399) <<
"Error loading PSD file." << endl;
268 io->setImage( TQImage() );
278 void kimgio_psd_write( TQImageIO * )