summaryrefslogtreecommitdiffstats
path: root/kimgio/psd.cpp
blob: c70919d0bf41d3cbde13e564c25b3f6c52e99045 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* This file is part of the KDE project
   Copyright (C) 2003 Ignacio Castaņo <castano@ludicon.com>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser 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 code is based on Thacher Ulrich PSD loading code released
   on public domain. See: http://tulrich.com/geekstuff/
*/

/* this code supports:
 * reading:
 *     rle and raw psd files
 * writing:
 *     not supported
 */

#include "psd.h"

#include <tqimage.h>
#include <tqdatastream.h>

#include <kdebug.h>

typedef TQ_UINT32 uint;
typedef TQ_UINT16 ushort;
typedef TQ_UINT8 uchar;

namespace {	// Private.

	enum ColorMode {
		CM_BITMAP = 0,
		CM_GRAYSCALE = 1,
		CM_INDEXED = 2,
		CM_RGB = 3,
		CM_CMYK = 4,
		CM_MULTICHANNEL = 7,
		CM_DUOTONE = 8,
		CM_LABCOLOR = 9
	};

	struct PSDHeader {
		uint signature;
		ushort version;
		uchar reserved[6];
		ushort channel_count;
		uint height;
		uint width;
		ushort depth;
		ushort color_mode;
	};
	
	static TQDataStream & operator>> ( TQDataStream & s, PSDHeader & header )
	{
		s >> header.signature;
		s >> header.version;
		for( int i = 0; i < 6; i++ ) {
			s >> header.reserved[i];
		}
		s >> header.channel_count;
		s >> header.height;
		s >> header.width;
		s >> header.depth;
		s >> header.color_mode;
		return s;
	}
        static bool seekBy(TQDataStream& s, unsigned int bytes)
        {
                char buf[4096];
                while (bytes) {
                        unsigned int num= TQMIN(bytes,sizeof(buf));
                        unsigned int l = num;
                        s.readRawBytes(buf, l);
                        if(l != num)
                          return false;
                        bytes -= num;
                }
                return true;
        }

	// Check that the header is a valid PSD.
	static bool IsValid( const PSDHeader & header )
	{
		if( header.signature != 0x38425053 ) {	// '8BPS'
			return false;
		}
		return true;
	}

	// Check that the header is supported.
	static bool IsSupported( const PSDHeader & header )
	{
		if( header.version != 1 ) {
			return false;
		}
		if( header.channel_count > 16 ) {
			return false;
		}
		if( header.depth != 8 ) {
			return false;
		}
		if( header.color_mode != CM_RGB ) {
			return false;
		}
		return true;
	}

	// Load the PSD image.
	static bool LoadPSD( TQDataStream & s, const PSDHeader & header, TQImage & img )
	{
		// Create dst image.
		if( !img.create( header.width, header.height, 32 )) {
			return false;
		}
	
		uint tmp;

		// Skip mode data.
		s >> tmp;
		s.device()->at( s.device()->at() + tmp );

		// Skip image resources.
		s >> tmp;
		s.device()->at( s.device()->at() + tmp );

		// Skip the reserved data.
		s >> tmp;
		s.device()->at( s.device()->at() + tmp );
		
		// Find out if the data is compressed.
		// Known values:
		//   0: no compression
		//   1: RLE compressed
		ushort compression;
		s >> compression;
		
		if( compression > 1 ) {
			// Unknown compression type.
			return false;
		}

		uint channel_num = header.channel_count;
		
		// Clear the image.
		if( channel_num < 4 ) {
			img.fill(tqRgba(0, 0, 0, 0xFF));
		}
		else {
			// Enable alpha.		
			img.setAlphaBuffer( true );
			
			// Ignore the other channels.
			channel_num = 4;			
		}
		
		const uint pixel_count = header.height * header.width;
		
		static const uint components[4] = {2, 1, 0, 3}; // @@ Is this endian dependant?
		
		if( compression ) {
		
			// Skip row lengths.
                        if(!seekBy(s, header.height*header.channel_count*sizeof(ushort)))
                                return false;

			// Read RLE data.						
			for(uint channel = 0; channel < channel_num; channel++) {
			
				uchar * ptr = img.bits() + components[channel];
				
				uint count = 0;
				while( count < pixel_count ) {
					uchar c;
                                        if(s.atEnd())
                                                return false;
					s >> c;
					uint len = c;
					
					if( len < 128 ) {
						// Copy next len+1 bytes literally.
						len++;
						count += len;
                                                if ( count > pixel_count )
                                                        return false;

						while( len != 0 ) {
							s >> *ptr;
							ptr += 4;
							len--;
						}
					} 
					else if( len > 128 ) {
						// Next -len+1 bytes in the dest are replicated from next source byte.
						// (Interpret len as a negative 8-bit int.)
						len ^= 0xFF;
						len += 2;
						count += len;
                                                if(s.atEnd() || count > pixel_count)
                                                        return false;
						uchar val;
						s >> val;
						while( len != 0 ) {
							*ptr = val;
							ptr += 4;
							len--;
						}
					}
					else if( len == 128 ) {
						// No-op.
					}
				}
			}
		}
		else {
			// We're at the raw image data.  It's each channel in order (Red, Green, Blue, Alpha, ...)
			// where each channel consists of an 8-bit value for each pixel in the image.

			// Read the data by channel.
			for(uint channel = 0; channel < channel_num; channel++) {

				uchar * ptr = img.bits() + components[channel];
			
				// Read the data.
				uint count = pixel_count;
				while( count != 0 ) {
					s >> *ptr;
					ptr += 4;
					count--;
				}
			}
		}

		return true;
	}

} // Private


void kimgio_psd_read( TQImageIO *io )
{
	TQDataStream s( io->ioDevice() );
	s.setByteOrder( TQDataStream::BigEndian );

	PSDHeader header;
	s >> header;

	// Check image file format.
	if( s.atEnd() || !IsValid( header ) ) {
		kdDebug(399) << "This PSD file is not valid." << endl;
		io->setImage( TQImage() );
		io->seStatus( -1 );
		return;
	}

	// Check if it's a supported format.
	if( !IsSupported( header ) ) {
		kdDebug(399) << "This PSD file is not supported." << endl;
		io->setImage( TQImage() );
		io->seStatus( -1 );
		return;
	}

	TQImage img;
	if( !LoadPSD(s, header, img) ) {
		kdDebug(399) << "Error loading PSD file." << endl;
		io->setImage( TQImage() );
		io->seStatus( -1 );
		return;
	}

    io->setImage( img );
    io->seStatus( 0 );
}


void kimgio_psd_write( TQImageIO * )
{
	// TODO Stub!
}