]> git.lizzy.rs Git - irrlicht.git/blob - source/Irrlicht/CImageLoaderPSD.cpp
CIrrDeviceWin32: drop all video mode code
[irrlicht.git] / source / Irrlicht / CImageLoaderPSD.cpp
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt\r
2 // This file is part of the "Irrlicht Engine".\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h\r
4 \r
5 #include "CImageLoaderPSD.h"\r
6 \r
7 #ifdef _IRR_COMPILE_WITH_PSD_LOADER_\r
8 \r
9 #include "IReadFile.h"\r
10 #include "os.h"\r
11 #include "CImage.h"\r
12 #include "irrString.h"\r
13 \r
14 \r
15 namespace irr\r
16 {\r
17 namespace video\r
18 {\r
19 \r
20 \r
21 //! constructor\r
22 CImageLoaderPSD::CImageLoaderPSD()\r
23 {\r
24         #ifdef _DEBUG\r
25         setDebugName("CImageLoaderPSD");\r
26         #endif\r
27 }\r
28 \r
29 \r
30 //! returns true if the file maybe is able to be loaded by this class\r
31 //! based on the file extension (e.g. ".tga")\r
32 bool CImageLoaderPSD::isALoadableFileExtension(const io::path& filename) const\r
33 {\r
34         return core::hasFileExtension ( filename, "psd" );\r
35 }\r
36 \r
37 \r
38 \r
39 //! returns true if the file maybe is able to be loaded by this class\r
40 bool CImageLoaderPSD::isALoadableFileFormat(io::IReadFile* file) const\r
41 {\r
42         if (!file)\r
43                 return false;\r
44 \r
45         u8 type[3];\r
46         file->read(&type, sizeof(u8)*3);\r
47         return (type[2]==2); // we currently only handle tgas of type 2.\r
48 }\r
49 \r
50 \r
51 \r
52 //! creates a surface from the file\r
53 IImage* CImageLoaderPSD::loadImage(io::IReadFile* file) const\r
54 {\r
55         u32* imageData = 0;\r
56 \r
57         PsdHeader header;\r
58         file->read(&header, sizeof(PsdHeader));\r
59 \r
60 #ifndef __BIG_ENDIAN__\r
61         header.version = os::Byteswap::byteswap(header.version);\r
62         header.channels = os::Byteswap::byteswap(header.channels);\r
63         header.height = os::Byteswap::byteswap(header.height);\r
64         header.width = os::Byteswap::byteswap(header.width);\r
65         header.depth = os::Byteswap::byteswap(header.depth);\r
66         header.mode = os::Byteswap::byteswap(header.mode);\r
67 #endif\r
68 \r
69         if (header.signature[0] != '8' ||\r
70                 header.signature[1] != 'B' ||\r
71                 header.signature[2] != 'P' ||\r
72                 header.signature[3] != 'S')\r
73                 return 0;\r
74 \r
75         if (header.version != 1)\r
76         {\r
77                 os::Printer::log("Unsupported PSD file version", file->getFileName(), ELL_ERROR);\r
78                 return 0;\r
79         }\r
80 \r
81         if (header.mode != 3 || header.depth != 8)\r
82         {\r
83                 os::Printer::log("Unsupported PSD color mode or depth.\n", file->getFileName(), ELL_ERROR);\r
84                 return 0;\r
85         }\r
86 \r
87         // skip color mode data\r
88 \r
89         u32 l;\r
90         file->read(&l, sizeof(u32));\r
91 #ifndef __BIG_ENDIAN__\r
92         l = os::Byteswap::byteswap(l);\r
93 #endif\r
94         if (!file->seek(l, true))\r
95         {\r
96                 os::Printer::log("Error seeking file pos to image resources.\n", file->getFileName(), ELL_ERROR);\r
97                 return 0;\r
98         }\r
99 \r
100         // skip image resources\r
101 \r
102         file->read(&l, sizeof(u32));\r
103 #ifndef __BIG_ENDIAN__\r
104         l = os::Byteswap::byteswap(l);\r
105 #endif\r
106         if (!file->seek(l, true))\r
107         {\r
108                 os::Printer::log("Error seeking file pos to layer and mask.\n", file->getFileName(), ELL_ERROR);\r
109                 return 0;\r
110         }\r
111 \r
112         // skip layer & mask\r
113 \r
114         file->read(&l, sizeof(u32));\r
115 #ifndef __BIG_ENDIAN__\r
116         l = os::Byteswap::byteswap(l);\r
117 #endif\r
118         if (!file->seek(l, true))\r
119         {\r
120                 os::Printer::log("Error seeking file pos to image data section.\n", file->getFileName(), ELL_ERROR);\r
121                 return 0;\r
122         }\r
123 \r
124         // read image data\r
125 \r
126         u16 compressionType;\r
127         file->read(&compressionType, sizeof(u16));\r
128 #ifndef __BIG_ENDIAN__\r
129         compressionType = os::Byteswap::byteswap(compressionType);\r
130 #endif\r
131 \r
132         if (compressionType != 1 && compressionType != 0)\r
133         {\r
134                 os::Printer::log("Unsupported psd compression mode.\n", file->getFileName(), ELL_ERROR);\r
135                 return 0;\r
136         }\r
137 \r
138         // create image data block\r
139 \r
140         imageData = new u32[header.width * header.height];\r
141 \r
142         bool res = false;\r
143 \r
144         if (compressionType == 0)\r
145                 res = readRawImageData(file, header, imageData); // RAW image data\r
146         else\r
147                 res = readRLEImageData(file, header, imageData); // RLE compressed data\r
148 \r
149         video::IImage* image = 0;\r
150 \r
151         if (res)\r
152         {\r
153                 // create surface\r
154                 image = new CImage(ECF_A8R8G8B8,\r
155                         core::dimension2d<u32>(header.width, header.height), imageData);\r
156         }\r
157 \r
158         if (!image)\r
159                 delete [] imageData;\r
160         imageData = 0;\r
161 \r
162         return image;\r
163 }\r
164 \r
165 \r
166 bool CImageLoaderPSD::readRawImageData(io::IReadFile* file, const PsdHeader& header, u32* imageData) const\r
167 {\r
168         u8* tmpData = new u8[header.width * header.height];\r
169 \r
170         for (s32 channel=0; channel<header.channels && channel < 3; ++channel)\r
171         {\r
172                 if (!file->read(tmpData, sizeof(c8) * header.width * header.height))\r
173                 {\r
174                         os::Printer::log("Error reading color channel\n", file->getFileName(), ELL_ERROR);\r
175                         break;\r
176                 }\r
177 \r
178                 s16 shift = getShiftFromChannel((c8)channel, header);\r
179                 if (shift != -1)\r
180                 {\r
181                         u32 mask = 0xff << shift;\r
182 \r
183                         for (u32 x=0; x<header.width; ++x)\r
184                         {\r
185                                 for (u32 y=0; y<header.height; ++y)\r
186                                 {\r
187                                         s32 index = x + y*header.width;\r
188                                         imageData[index] = ~(~imageData[index] | mask);\r
189                                         imageData[index] |= tmpData[index] << shift;\r
190                                 }\r
191                         }\r
192                 }\r
193 \r
194         }\r
195 \r
196         delete [] tmpData;\r
197         return true;\r
198 }\r
199 \r
200 \r
201 bool CImageLoaderPSD::readRLEImageData(io::IReadFile* file, const PsdHeader& header, u32* imageData) const\r
202 {\r
203         /*      If the compression code is 1, the image data\r
204                 starts with the byte counts for all the scan lines in the channel\r
205                 (LayerBottom LayerTop), with each count stored as a two\r
206                 byte value. The RLE compressed data follows, with each scan line\r
207                 compressed separately. The RLE compression is the same compres-sion\r
208                 algorithm used by the Macintosh ROM routine PackBits, and\r
209                 the TIFF standard.\r
210                 If the Layer's Size, and therefore the data, is odd, a pad byte will\r
211                 be inserted at the end of the row.\r
212         */\r
213 \r
214         /*\r
215         A pseudo code fragment to unpack might look like this:\r
216 \r
217         Loop until you get the number of unpacked bytes you are expecting:\r
218                 Read the next source byte into n.\r
219                 If n is between 0 and 127 inclusive, copy the next n+1 bytes literally.\r
220                 Else if n is between -127 and -1 inclusive, copy the next byte -n+1\r
221                 times.\r
222                 Else if n is -128, noop.\r
223         Endloop\r
224 \r
225         In the inverse routine, it is best to encode a 2-byte repeat run as a replicate run\r
226         except when preceded and followed by a literal run. In that case, it is best to merge\r
227         the three runs into one literal run. Always encode 3-byte repeats as replicate runs.\r
228         That is the essence of the algorithm. Here are some additional rules:\r
229         - Pack each row separately. Do not compress across row boundaries.\r
230         - The number of uncompressed bytes per row is defined to be (ImageWidth + 7)\r
231         / 8. If the uncompressed bitmap is required to have an even number of bytes per\r
232         row, decompress into word-aligned buffers.\r
233         - If a run is larger than 128 bytes, encode the remainder of the run as one or more\r
234         additional replicate runs.\r
235         When PackBits data is decompressed, the result should be interpreted as per com-pression\r
236         type 1 (no compression).\r
237         */\r
238 \r
239         u8* tmpData = new u8[header.width * header.height];\r
240         u16 *rleCount= new u16 [header.height * header.channels];\r
241 \r
242         s32 size=0;\r
243 \r
244         for (u32 y=0; y<header.height * header.channels; ++y)\r
245         {\r
246                 if (!file->read(&rleCount[y], sizeof(u16)))\r
247                 {\r
248                         delete [] tmpData;\r
249                         delete [] rleCount;\r
250                         os::Printer::log("Error reading rle rows\n", file->getFileName(), ELL_ERROR);\r
251                         return false;\r
252                 }\r
253 \r
254 #ifndef __BIG_ENDIAN__\r
255                 rleCount[y] = os::Byteswap::byteswap(rleCount[y]);\r
256 #endif\r
257                 size += rleCount[y];\r
258         }\r
259 \r
260         s8 *buf = new s8[size];\r
261         if (!file->read(buf, size))\r
262         {\r
263                 delete [] rleCount;\r
264                 delete [] buf;\r
265                 delete [] tmpData;\r
266                 os::Printer::log("Error reading rle rows\n", file->getFileName(), ELL_ERROR);\r
267                 return false;\r
268         }\r
269 \r
270         u16 *rcount=rleCount;\r
271 \r
272         s8 rh;\r
273         u16 bytesRead;\r
274         u8 *dest;\r
275         s8 *pBuf = buf;\r
276 \r
277         // decompress packbit rle\r
278 \r
279         for (s32 channel=0; channel<header.channels; channel++)\r
280         {\r
281                 for (u32 y=0; y<header.height; ++y, ++rcount)\r
282                 {\r
283                         bytesRead=0;\r
284                         dest = &tmpData[y*header.width];\r
285 \r
286                         while (bytesRead < *rcount)\r
287                         {\r
288                                 rh = *pBuf++;\r
289                                 ++bytesRead;\r
290 \r
291                                 if (rh >= 0)\r
292                                 {\r
293                                         ++rh;\r
294 \r
295                                         while (rh--)\r
296                                         {\r
297                                                 *dest = *pBuf++;\r
298                                                 ++bytesRead;\r
299                                                 ++dest;\r
300                                         }\r
301                                 }\r
302                                 else\r
303                                 if (rh > -128)\r
304                                 {\r
305                                         rh = -rh +1;\r
306 \r
307                                         while (rh--)\r
308                                         {\r
309                                                 *dest = *pBuf;\r
310                                                 ++dest;\r
311                                         }\r
312 \r
313                                         ++pBuf;\r
314                                         ++bytesRead;\r
315                                 }\r
316                         }\r
317                 }\r
318 \r
319                 s16 shift = getShiftFromChannel((c8)channel, header);\r
320 \r
321                 if (shift != -1)\r
322                 {\r
323                         u32 mask = 0xff << shift;\r
324 \r
325                         for (u32 x=0; x<header.width; ++x)\r
326                                 for (u32 y=0; y<header.height; ++y)\r
327                                 {\r
328                                         s32 index = x + y*header.width;\r
329                                         imageData[index] = ~(~imageData[index] | mask);\r
330                                         imageData[index] |= tmpData[index] << shift;\r
331                                 }\r
332                 }\r
333         }\r
334 \r
335         delete [] rleCount;\r
336         delete [] buf;\r
337         delete [] tmpData;\r
338 \r
339         return true;\r
340 }\r
341 \r
342 \r
343 s16 CImageLoaderPSD::getShiftFromChannel(c8 channelNr, const PsdHeader& header) const\r
344 {\r
345         switch(channelNr)\r
346         {\r
347         case 0:\r
348                 return 16;  // red\r
349         case 1:\r
350                 return 8;   // green\r
351         case 2:\r
352                 return 0;   // blue\r
353         case 3:\r
354                 return header.channels == 4 ? 24 : -1;  // ?\r
355         case 4:\r
356                 return 24;  // alpha\r
357         default:\r
358                 return -1;\r
359         }\r
360 }\r
361 \r
362 \r
363 \r
364 //! creates a loader which is able to load tgas\r
365 IImageLoader* createImageLoaderPSD()\r
366 {\r
367         return new CImageLoaderPSD();\r
368 }\r
369 \r
370 \r
371 } // end namespace video\r
372 } // end namespace irr\r
373 \r
374 #endif\r
375 \r