0
|
1 // Written in the D programming language
|
|
2 // www.digitalmars.com/d/
|
|
3
|
|
4 /***************************************************************************************************
|
|
5 Types and functions common to both the encode and decoder of lodepng, as well as image format
|
|
6 conversion routines
|
|
7
|
|
8 License:
|
|
9 Copyright (c) 2005-2007 Lode Vandevenne
|
|
10 All rights reserved.
|
|
11
|
|
12 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
13
|
|
14 - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.<br>
|
|
15 - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.<br>
|
|
16 - Neither the name of Lode Vandevenne nor the names of his contributors may be used to endorse or promote products derived from this software without specific prior written permission.<br>
|
|
17
|
|
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
19
|
|
20 Authors: Lode Vandevenne (original version in C++), Lutger Blijdestijn (D version) : lutger dot blijdestijn at gmail dot com,
|
|
21 Stewart Gordon (modifications)
|
|
22
|
|
23 Date: August 7, 2007
|
|
24
|
|
25 References:
|
|
26 $(LINK2 http://members.gamedev.net/lode/projects/LodePNG/, Original lodepng) <br>
|
|
27 $(LINK2 http://www.w3.org/TR/PNG/, PNG Specification) <br>
|
|
28 $(LINK2 http://www.libpng.org/pub/png/pngsuite.html, PNG Suite: set of test images) <br>
|
|
29 $(LINK2 http://optipng.sourceforge.net/, OptiPNG: tool to experimentally optimize png images)
|
|
30 ***************************************************************************************************/
|
|
31 module dynamin.lodepng.common;
|
|
32 import dynamin.lodepng.util;
|
|
33 import dynamin.lodepng.zlib_codec;
|
|
34
|
|
35 /***************************************************************************************************
|
|
36 Png specific exception.
|
|
37
|
|
38 Instead of errors codes, this port of lodepng makes use of exceptions.
|
|
39 At the moment, the decoder is very strict and will tolerate no errors whatsoever even
|
|
40 if they could safely be ignored. CRC checking is always done.
|
|
41 This might be slightly relaxed in a future release to be more in line with the
|
|
42 recommendations of the specification.
|
|
43
|
|
44 ***************************************************************************************************/
|
|
45 class PngException : Exception
|
|
46 {
|
|
47 this(char[] msg)
|
|
48 {
|
|
49 super(msg);
|
|
50 }
|
|
51 }
|
|
52
|
|
53 package alias _enforce!(PngException) pngEnforce;
|
|
54
|
|
55 /***************************************************************************************************
|
|
56 An enumeration of the color types supported by the png format.
|
|
57
|
|
58 see the $(LINK2 http://www.w3.org/TR/PNG/index-noobject.html#6Colour-values, png specification)
|
|
59 for details.
|
|
60
|
|
61 ***************************************************************************************************/
|
|
62 enum ColorType : ubyte
|
|
63 {
|
|
64 Greyscale = 0, /// allowed bit depths: 1, 2, 4, 8 and 16
|
|
65 RGB = 2, /// allowed bit depths: 8 and 16
|
|
66 Palette = 3, /// allowed bit depths: 1, 2, 4 and 8
|
|
67 GreyscaleAlpha = 4, /// allowed bit depths: 8 and 16
|
|
68 RGBA = 6, /// allowed bit depths: 8 and 16
|
|
69 Any = 7, /// one of the above
|
|
70 }
|
|
71
|
|
72 /***************************************************************************************************
|
|
73 Convert source pixels from the color type described by info the color type destColorType.
|
|
74
|
|
75 Conversion can be from any color type supported by the png specification to
|
|
76 24 / 32 bit RGB(A). If RGBA is specified and the info has a colorkey, transparency is applied.
|
|
77
|
|
78 If a buffer is given, it may be used to store the result and a slice from it can be returned.
|
|
79
|
|
80 Returns: converted image in RGB(A) format, pixels are from left to right, top to bottom
|
|
81 ***************************************************************************************************/
|
|
82 ubyte[] convert(in ubyte[] source, /+const+/ ref PngInfo info, ColorType destColorType = ColorType.RGBA)
|
|
83 {
|
|
84 ubyte[] buffer;
|
|
85 return convert(source, info, buffer, destColorType);
|
|
86 }
|
|
87
|
|
88 /// ditto
|
|
89 ubyte[] convert(in ubyte[] source, /+const+/in PngInfo info, ref ubyte[] buffer, ColorType destColorType = ColorType.RGBA)
|
|
90 {
|
|
91 if (!(destColorType == ColorType.RGBA || destColorType == ColorType.RGB))
|
|
92 {
|
|
93 destColorType = ColorType.RGBA;
|
|
94 assert(false, "destColorType should be one of: RGB, RGBA");
|
|
95 }
|
|
96 return _convert(source, info, buffer, destColorType);
|
|
97 }
|
|
98
|
|
99 /***************************************************************************************************
|
|
100 Description of the image.
|
|
101
|
|
102 ***************************************************************************************************/
|
|
103 struct PngImage
|
|
104 {
|
|
105 /// constructor
|
|
106 static PngImage opCall(uint w, uint h, ubyte bd, ColorType ct)
|
|
107 {
|
|
108 PngImage result;
|
|
109 with (result)
|
|
110 {
|
|
111 width = w;
|
|
112 height = h;
|
|
113 bitDepth = bd;
|
|
114 colorType = ct;
|
|
115 bpp = bitDepth * numChannels(colorType);
|
|
116 }
|
|
117 return result;
|
|
118 }
|
|
119 uint width; /// in pixels
|
|
120 uint height; /// in pixels
|
|
121 ubyte bitDepth; /// bits per color channel, see also: ColorType
|
|
122 ubyte bpp; /// bits per pixel
|
|
123 ColorType colorType; /// the color format, see also: ColorType
|
|
124 }
|
|
125
|
|
126 /***************************************************************************************************
|
|
127 Png file and image description.
|
|
128
|
|
129 A simple data type describing the png file. Usually the image field will contain all the
|
|
130 required information.
|
|
131
|
|
132 There is one member that behaves a bit different: parseText(bool). This is used to tell
|
|
133 the decoder whether to ignore textual metadata (which it does by default).
|
|
134
|
|
135 ***************************************************************************************************/
|
|
136 struct PngInfo
|
|
137 {
|
|
138
|
|
139 PngImage image; /// Information related to the image, see also: PngImage.
|
|
140
|
|
141 /***********************************************************************************************
|
|
142 Recommended background color or empty if none is given.
|
|
143
|
|
144 Interpretation of the array depends on the color type:
|
|
145
|
|
146 $(DL
|
|
147 $(DT palette)
|
|
148 $(DD palette[backgroundColor[0]] is the background color)
|
|
149 $(DT greyscale (8 bit or less) )
|
|
150 $(DD backgroundColor[0] is used)
|
|
151 $(DT RGB(A) (8 bit or less))
|
|
152 $(DD backgroundColor[0..3] is the rgb triplet used)
|
|
153 $(DT 16-bit greyscale or RGB(A))
|
|
154 $(DD same as above, but the color/greyscale channels are 2 bytes wide))
|
|
155
|
|
156 ***********************************************************************************************/
|
|
157 ubyte[] backgroundColor;
|
|
158
|
|
159 /***********************************************************************************************
|
|
160 Palette colors or empty if this image does not make use of it.
|
|
161
|
|
162 Each palette entry is a 32-bit RGBA pixel represented as an ubyte[4].
|
|
163 When a colorkey is specified, it is automatically applied to the palette by the decoder.
|
|
164
|
|
165 ***********************************************************************************************/
|
|
166 ubyte[4][] palette;
|
|
167
|
|
168 /***********************************************************************************************
|
|
169 Whether there is a colorkey transparency associated with the png image.
|
|
170
|
|
171 Note that this is applicable only when the image has no seperate alpha channel, and
|
|
172 the image format is not ColorType.Palette. In the latter case, transparency is always
|
|
173 stored in the palette by the decoder.
|
|
174 The transparent color is stored in keyR, keyG and keyB, for brevity greyscale is in keyR.
|
|
175 ***********************************************************************************************/
|
|
176 bool colorKey = false;
|
|
177 ushort keyR; ///
|
|
178 ushort keyG; ///
|
|
179 ushort keyB; ///
|
|
180
|
|
181 /// By default, lodepng will not parse textual metadata, set to true if this is desired.
|
|
182 void parseText(bool flag)
|
|
183 {
|
|
184 parseTextChunks = flag;
|
|
185 }
|
|
186
|
|
187 /// Whether parsing of metadata is enabled.
|
|
188 bool parseText()
|
|
189 {
|
|
190 return parseTextChunks;
|
|
191 }
|
|
192
|
|
193 /***********************************************************************************************
|
|
194 Retrieve the dictionary of textual metadata.
|
|
195
|
|
196 When no text is read, because it was set to be ignored or there wasn't any,
|
|
197 this returns null so check the return value.
|
|
198
|
|
199 In the case that a PngInfo instance is passed more than once to the api,
|
|
200 an existing dictionary is not reused. Instead a new one will be
|
|
201 created. It is therefore not necessary to copy anything, the strings they are all yours.
|
|
202
|
|
203 ***********************************************************************************************/
|
|
204 PngText text()
|
|
205 {
|
|
206 return textual;
|
|
207 }
|
|
208
|
|
209 /// Returns whether any unicode text has been stored.
|
|
210 bool hasUnicodeText()
|
|
211 {
|
|
212 return textual !is null && textual.unicodeText.length > 0;
|
|
213 }
|
|
214
|
|
215 /// Returns whether any latin-1 text has been stored.
|
|
216 bool hasLatin1Text()
|
|
217 {
|
|
218 return textual !is null && textual.latin1Text.length > 0;
|
|
219 }
|
|
220
|
|
221 package
|
|
222 {
|
|
223 ubyte interlace;
|
|
224 PngText textual;
|
|
225 }
|
|
226
|
|
227 private bool parseTextChunks = true;
|
|
228 }
|
|
229
|
|
230 /// Dictionary of key-value textual metadata in utf-8 and / or latin-1 encoding.
|
|
231 class PngText
|
|
232 {
|
|
233 ///
|
|
234 this()
|
|
235 {
|
|
236 }
|
|
237
|
|
238 ///
|
|
239 this(char[][char[]] unicodeText)
|
|
240 {
|
|
241 this.unicodeText = unicodeText;
|
|
242 }
|
|
243
|
|
244 ///
|
|
245 this(ubyte[][ubyte[]] latin1Text)
|
|
246 {
|
|
247 this.latin1Text = latin1Text;
|
|
248 }
|
|
249
|
|
250 /// Visit utf-8 dictionary
|
|
251 int opApply(int delegate(ref char[] keyword, ref char[] contents) dg)
|
|
252 {
|
|
253 int result = 0;
|
|
254 foreach(index, value; unicodeText)
|
|
255 {
|
|
256 result = dg(index, value);
|
|
257 if (result)
|
|
258 return result;
|
|
259 }
|
|
260 return 0;
|
|
261 }
|
|
262
|
|
263 /// Visit latin-1 dictionary
|
|
264 int opApply(int delegate(ref ubyte[] keyword, ref ubyte[] contents) dg)
|
|
265 {
|
|
266 int result = 0;
|
|
267 foreach(index, value; latin1Text)
|
|
268 {
|
|
269 result = dg(index, value);
|
|
270 if (result)
|
|
271 return result;
|
|
272 }
|
|
273 return 0;
|
|
274 }
|
|
275
|
|
276 /// Assign key-value pair
|
|
277 PngText opIndexAssign(ubyte[] value, ubyte[] keyword)
|
|
278 {
|
|
279 latin1Text[value] = keyword;
|
|
280 return this;
|
|
281 }
|
|
282
|
|
283 /// ditto
|
|
284 PngText opIndexAssign(char[] value, char[] keyword)
|
|
285 {
|
|
286 unicodeText[value] = keyword;
|
|
287 return this;
|
|
288 }
|
|
289
|
|
290 package
|
|
291 {
|
|
292 char[][char[]] unicodeText;
|
|
293 ubyte[][ubyte[]] latin1Text;
|
|
294 }
|
|
295 }
|
|
296
|
|
297 /// Number of color or alpha channels associated with this color type.
|
|
298 uint numChannels(ColorType colorType)
|
|
299 in
|
|
300 {
|
|
301 assert(colorType != ColorType.Any, "cannot determine number of channels with ColorType.Any");
|
|
302 }
|
|
303 body
|
|
304 {
|
|
305 return [ 0 : 1, 2 : 3, 3 : 1, 4 : 2, 6 : 4, 7 : 0] [colorType];
|
|
306 }
|
|
307
|
|
308 ///
|
|
309 bool isGreyscale(ColorType colorType)
|
|
310 {
|
|
311 return colorType == ColorType.Greyscale || colorType == ColorType.GreyscaleAlpha;
|
|
312 }
|
|
313
|
|
314 ///
|
|
315 bool hasAlphaChannel(ColorType colorType)
|
|
316 {
|
|
317 return colorType == ColorType.RGBA || colorType == ColorType.GreyscaleAlpha;
|
|
318 }
|
|
319
|
|
320 /// Whether the image contains any alpha information (channel / colorkey / palette)
|
|
321 bool hasAlpha(/+const+/ ref PngInfo info)
|
|
322 {
|
|
323 if (hasAlphaChannel(info.image.colorType) || info.colorKey)
|
|
324 return true;
|
|
325 else if (info.image.colorType == ColorType.Palette)
|
|
326 foreach(color; info.palette)
|
|
327 if (color[3] != 255)
|
|
328 return true;
|
|
329 return false;
|
|
330 }
|
|
331
|
|
332 /* ************************************************************************************************
|
|
333 PACKAGE SECTION, not so interesting stuff
|
|
334
|
|
335 ************************************************************************************************* */
|
|
336 package
|
|
337 {
|
|
338 const IHDR = toUint('I', 'H', 'D', 'R');
|
|
339 const IDAT = toUint('I', 'D', 'A', 'T');
|
|
340 const PLTE = toUint('P', 'L', 'T', 'E');
|
|
341 const tRNS = toUint('t', 'R', 'N', 'S');
|
|
342 const bKGD = toUint('b', 'K', 'G', 'D');
|
|
343 const IEND = toUint('I', 'E', 'N', 'D');
|
|
344 const tEXt = toUint('t', 'E', 'X', 't');
|
|
345 const iTXt = toUint('i', 'T', 'X', 't');
|
|
346 const zTXt = toUint('z', 'T', 'X', 't');
|
|
347 const cHRM = toUint('c', 'H', 'R', 'M');
|
|
348 const gAMA = toUint('g', 'A', 'M', 'A');
|
|
349 const hIST = toUint('h', 'I', 'S', 'T');
|
|
350 const iCCP = toUint('i', 'C', 'C', 'P');
|
|
351 const oFFs = toUint('o', 'F', 'F', 's');
|
|
352 const pCAL = toUint('p', 'C', 'A', 'L');
|
|
353 const pHYs = toUint('p', 'H', 'Y', 's');
|
|
354 const sBIT = toUint('s', 'B', 'I', 'T');
|
|
355 const sCAL = toUint('s', 'C', 'A', 'L');
|
|
356 const sPLT = toUint('s', 'P', 'L', 'T');
|
|
357 const sRGB = toUint('s', 'R', 'G', 'B');
|
|
358
|
|
359 //return type is a LodePNG error code
|
|
360 bool checkColorValidity(uint colorType, uint bitDepth)
|
|
361 {
|
|
362 alias bitDepth bd;
|
|
363 switch(colorType)
|
|
364 {
|
|
365 case 0: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 || bd == 16)) //grey
|
|
366 return false;
|
|
367 break;
|
|
368 case 2: if(!(bd == 8 || bd == 16)) //RGB
|
|
369 return false;
|
|
370 break;
|
|
371 case 3: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 )) //palette
|
|
372 return false;
|
|
373 break;
|
|
374 case 4: if(!(bd == 8 || bd == 16)) //greyalpha
|
|
375 return false;
|
|
376 break;
|
|
377 case 6: if(!(bd == 8 || bd == 16)) //RGBA
|
|
378 return false;
|
|
379 break;
|
|
380 default:
|
|
381 mixin(pngEnforce(`false`, "invalid png color format"));
|
|
382 }
|
|
383 return true;
|
|
384 }
|
|
385
|
|
386 // Paeth predicter, used by one of the PNG filter types
|
|
387 int paethPredictor(int a, int b, int c)
|
|
388 {
|
|
389 int p = a + b - c;
|
|
390 int pa = p > a ? p - a : a - p;
|
|
391 int pb = p > b ? p - b : b - p;
|
|
392 int pc = p > c ? p - c : c - p;
|
|
393
|
|
394 if(pa <= pb && pa <= pc) return a;
|
|
395 else if(pb <= pc) return b;
|
|
396 return c;
|
|
397 }
|
|
398
|
|
399 uint readBitFromStreamReversed(ref size_t bitpointer, ubyte[] bitstream)
|
|
400 {
|
|
401 return (bitstream[bitpointer / 8] >> (7 - bitpointer++ & 0x7)) & 1;
|
|
402 }
|
|
403 }
|
|
404
|
|
405 bool checkCRC(uint crc, ubyte[] data)
|
|
406 {
|
|
407 return crc == czlib.crc32(0, cast(ubyte *)data, data.length);
|
|
408 }
|
|
409
|
|
410 bool checkCRC(ubyte[] crc, ubyte[] data)
|
|
411 {
|
|
412 return toUint(crc) == czlib.crc32(0, cast(ubyte *)data, data.length);
|
|
413 }
|
|
414
|
|
415 uint createCRC(ubyte[] data)
|
|
416 {
|
|
417 return czlib.crc32(0, cast(ubyte *)data, data.length);
|
|
418 }
|
|
419
|
|
420 struct Chunk
|
|
421 {
|
|
422 static Chunk fromStream(ubyte[] byteStream)
|
|
423 in
|
|
424 {
|
|
425 assert(byteStream.length >= 12);
|
|
426 }
|
|
427 body
|
|
428 {
|
|
429 Chunk result;
|
|
430 uint dataLength = toUint(byteStream);
|
|
431 result.type = toUint(byteStream[4 .. 8]);
|
|
432 if (dataLength)
|
|
433 result.data = byteStream[8 .. 8 + dataLength];
|
|
434 mixin(pngEnforce("checkCRC(byteStream[8 + dataLength..12 + dataLength], byteStream[4..8 + dataLength])",
|
|
435 "CRC check failed"));
|
|
436 return result;
|
|
437 }
|
|
438
|
|
439 int opCmp(Chunk other)
|
|
440 {
|
|
441 int result = chunkOrder(type, other.type);
|
|
442
|
|
443 /* Setting of the afterIDAT member is not yet implemented. It would
|
|
444 * be used to prevent unknown ancillary chunks from being moved from
|
|
445 * before the IDAT chunk to after it, or vice versa.
|
|
446
|
|
447 if (result == 0) {
|
|
448 result = cast(int) afterIDAT - cast(int) other.afterIDAT;
|
|
449 }
|
|
450 if (result == 0) {
|
|
451 // if one of them is IDAT, the other is before IDAT
|
|
452 if (type == IDAT) return 1;
|
|
453 if (other.type == IDAT) return -1;
|
|
454 }
|
|
455 */
|
|
456 return result;
|
|
457 }
|
|
458
|
|
459 uint length() { return data.length + 12; }
|
|
460
|
|
461 uint type;
|
|
462 ubyte[] data;
|
|
463 //bool afterIDAT;
|
|
464 }
|
|
465
|
|
466
|
|
467 private int chunkOrder(uint type1, uint type2) {
|
|
468 if (type1 == type2) return 0;
|
|
469
|
|
470 // sort out IHDR and IEND first
|
|
471 if (type1 == IHDR) return -1;
|
|
472 if (type1 == IEND) return 1;
|
|
473 if (type2 == IHDR) return 1;
|
|
474 if (type2 == IEND) return -1;
|
|
475
|
|
476 // now sort out PLTE and IDAT
|
|
477 switch (type1) {
|
|
478 case PLTE:
|
|
479 switch (type2) {
|
|
480 case gAMA: case cHRM: case sRGB: case iCCP: case sBIT:
|
|
481 return 1;
|
|
482
|
|
483 case tRNS: case bKGD: case hIST: case IDAT:
|
|
484 return -1;
|
|
485
|
|
486 default:
|
|
487 return 0;
|
|
488 }
|
|
489
|
|
490 case IDAT:
|
|
491 switch (type2) {
|
|
492 case gAMA: case cHRM: case sRGB: case iCCP: case sBIT:
|
|
493 case PLTE: case tRNS: case bKGD: case hIST: case pHYs:
|
|
494 case sPLT: case oFFs: case pCAL: case sCAL:
|
|
495 return 1;
|
|
496
|
|
497 default:
|
|
498 return 0;
|
|
499 }
|
|
500
|
|
501 default:
|
|
502 if (type2 == PLTE || type2 == IDAT) {
|
|
503 return -chunkOrder(type2, type1);
|
|
504 }
|
|
505 }
|
|
506
|
|
507 // if we reach this point, they're both ancillary chunks
|
|
508 switch (type1) {
|
|
509 case gAMA: case cHRM: case sRGB: case iCCP: case sBIT:
|
|
510 switch (type2) {
|
|
511 case tRNS: case bKGD: case hIST: case IDAT:
|
|
512 return -1;
|
|
513
|
|
514 default:
|
|
515 return 0;
|
|
516 }
|
|
517
|
|
518 case tRNS: case bKGD: case hIST: case IDAT:
|
|
519 switch (type2) {
|
|
520 case gAMA: case cHRM: case sRGB: case iCCP: case sBIT:
|
|
521 return 1;
|
|
522
|
|
523 default:
|
|
524 return 0;
|
|
525 }
|
|
526
|
|
527 default:
|
|
528 return 0;
|
|
529 }
|
|
530 }
|
|
531
|
|
532
|
|
533 uint toUint(ubyte a, ubyte b, ubyte c, ubyte d) { return a << 24 | b << 16 | c << 8 | d; }
|
|
534 uint toUint(ubyte[] source)
|
|
535 in { assert(source.length >= 4); }
|
|
536 body { return source[0] << 24 | source[1] << 16 | source[2] << 8 | source[3]; }
|
|
537
|
|
538 private ubyte[] _convert(in ubyte[] source, /+const+/ ref PngInfo info, ref ubyte[] buffer, ColorType destColorType)
|
|
539 {
|
|
540 bool alpha = hasAlphaChannel(destColorType);
|
|
541
|
|
542 if (destColorType == info.image.colorType)
|
|
543 {
|
|
544 if ((destColorType == ColorType.RGBA || destColorType == ColorType.RGB) && info.image.bitDepth == 8)
|
|
545 {
|
|
546 buffer = source;
|
|
547 return buffer;
|
|
548 }
|
|
549 }
|
|
550
|
|
551
|
|
552 uint w = info.image.width;
|
|
553 uint h = info.image.height;
|
|
554 ubyte[] res = buffer;
|
|
555 ubyte colors = alpha ? 4 : 3;
|
|
556
|
|
557 res.length = w * h * colors;
|
|
558
|
|
559 assert(source.length >= (w * (info.image.bpp / 8 ) * h));
|
|
560
|
|
561 if (!info.colorKey && alpha)
|
|
562 for(size_t i = 0; i < w * h; i++)
|
|
563 res[4 * i + 3] = 255;
|
|
564
|
|
565 switch(info.image.colorType)
|
|
566 {
|
|
567 case 0: //greyscale color
|
|
568 {
|
|
569 if(info.image.bitDepth == 8)
|
|
570 {
|
|
571 foreach(i, pixel; source[0 .. w * h])
|
|
572 res[colors * i + 0] = res[colors * i + 1] = res[colors * i + 2] = pixel;
|
|
573 if (info.colorKey && alpha)
|
|
574 foreach(i, pixel; source[0 .. w * h])
|
|
575 res[colors * i + 3] = (pixel == info.keyR) ? 0 : 255;
|
|
576 }
|
|
577 else if (info.image.bitDepth == 8)
|
|
578 {
|
|
579 for(size_t i = 0; i < w * h; i++)
|
|
580 res[colors * i + 0] = res[colors * i + 1] = res[colors * i + 2] = source[i * 2];
|
|
581 if (info.colorKey && alpha)
|
|
582 for(size_t i = 0; i < w * h; i++)
|
|
583 res[colors * i + 3] = (256U * source[i] + source[i + 1] == info.keyR) ? 0 : 255;
|
|
584 }
|
|
585 else
|
|
586 {
|
|
587 if (!info.colorKey)
|
|
588 {
|
|
589 for(size_t i = 0; i < w * h; i++)
|
|
590 {
|
|
591 size_t bp = info.image.bitDepth * i;
|
|
592 uint value = 0;
|
|
593 uint pot = 1 << info.image.bitDepth; //power of two
|
|
594 for(size_t j = 0; j < info.image.bitDepth; j++)
|
|
595 {
|
|
596 pot /= 2;
|
|
597 uint _bit = readBitFromStreamReversed(bp, source);
|
|
598 value += pot * _bit;
|
|
599 }
|
|
600 //scale value from 0 to 255
|
|
601 value = (value * 255) / ((1 << info.image.bitDepth) - 1);
|
|
602 res[colors * i + 0] = res[colors * i + 1] = res[colors * i + 2] = cast(ubyte)(value);
|
|
603 }
|
|
604 }
|
|
605 else if (alpha)
|
|
606 {
|
|
607 for(size_t i = 0; i < w * h; i++)
|
|
608 {
|
|
609 size_t bp = info.image.bitDepth * i;
|
|
610 uint value = 0;
|
|
611 uint pot = 1 << info.image.bitDepth; //power of two
|
|
612 for(size_t j = 0; j < info.image.bitDepth; j++)
|
|
613 {
|
|
614 pot /= 2;
|
|
615 uint _bit = readBitFromStreamReversed(bp, source);
|
|
616 value += pot * _bit;
|
|
617 }
|
|
618 res[colors * i + 3] = ( value && ((1U << info.image.bitDepth) - 1U) ==
|
|
619 info.keyR && ((1U << info.image.bitDepth) - 1U)) ? 0 : 255;
|
|
620 value = (value * 255) / ((1 << info.image.bitDepth) - 1);
|
|
621 res[colors * i + 0] = res[colors * i + 1] = res[colors * i + 2] = cast(ubyte)(value);
|
|
622 }
|
|
623 }
|
|
624 }
|
|
625 }
|
|
626 break;
|
|
627 case 2: //RGB color
|
|
628 {
|
|
629 if(info.image.bitDepth == 8)
|
|
630 {
|
|
631 for(size_t i = 0; i < w * h; i++)
|
|
632 for(size_t c = 0; c < 3; c++)
|
|
633 res[colors * i + c] =
|
|
634 source[3 * i + c];
|
|
635 if (info.colorKey && alpha)
|
|
636 for(size_t i = 0; i < w * h; i++)
|
|
637 res[colors * i + 3] = (source[3 * i + 0] == info.keyR && source[3 * i + 1] ==
|
|
638 info.keyG && source[3 * i + 2] == info.keyB) ? 0 : 255;
|
|
639 }
|
|
640 else // 16 bit
|
|
641 {
|
|
642 for(size_t i = 0; i < w * h; i++)
|
|
643 for(size_t c = 0; c < 3; c++)
|
|
644 res[colors * i + c] = source[6 * i + 2 * c];
|
|
645 if (info.colorKey && alpha)
|
|
646 for(size_t i = 0; i < w * h; i++)
|
|
647 res[colors * i + 3] = (256U * source[6 * i + 0] + source[6 * i + 1] ==
|
|
648 info.keyR && 256U * source[6 * i + 2] + source[6 * i + 3] ==
|
|
649 info.keyG && 256U * source[6 * i + 4] + source[6 * i + 5] ==
|
|
650 info.keyB) ? 0 : 255;
|
|
651 }
|
|
652 break;
|
|
653 }
|
|
654 case 3: //indexed color (palette)
|
|
655 {
|
|
656 if(info.image.bitDepth == 8)
|
|
657 for(size_t i = 0; i < w * h; i++)
|
|
658 res[colors * i..colors * i + colors] = info.palette[source[i]][0..colors];
|
|
659 else if(info.image.bitDepth < 8)
|
|
660 {
|
|
661 for(size_t i = 0; i < w * h; i++)
|
|
662 {
|
|
663 size_t bp = info.image.bitDepth * i;
|
|
664 uint value = 0;
|
|
665 uint pot = 1 << info.image.bitDepth; //power of two
|
|
666 for(size_t j = 0; j < info.image.bitDepth; j++)
|
|
667 {
|
|
668 pot /= 2;
|
|
669 uint _bit = readBitFromStreamReversed(bp, source);
|
|
670 value += pot * _bit;
|
|
671 }
|
|
672 res[colors * i.. colors * i + colors] = info.palette[value][0..colors];
|
|
673 }
|
|
674 }
|
|
675 break;
|
|
676 }
|
|
677 case 4: //greyscale with alpha
|
|
678 {
|
|
679 if(info.image.bitDepth == 8)
|
|
680 {
|
|
681 for(size_t i = 0; i < w * h; i++)
|
|
682 {
|
|
683 res[colors * i + 0] = res[colors * i + 1] = res[colors * i + 2] = source[i * 2 + 0];
|
|
684 if (alpha)
|
|
685 res[colors * i + 3] = source[i * 2 + 1];
|
|
686 }
|
|
687 }
|
|
688 else if(info.image.bitDepth == 16)
|
|
689 {
|
|
690 for(size_t i = 0; i < w * h; i++)
|
|
691 {
|
|
692 //most significant byte
|
|
693 res[colors * i + 0] = res[colors * i + 1] = res[colors * i + 2] = source[4 * i];
|
|
694 if (alpha)
|
|
695 res[colors * i + 3] = source[4 * i + 2];
|
|
696 }
|
|
697 }
|
|
698 break;
|
|
699 }
|
|
700 case 6: //RGB with alpha
|
|
701 {
|
|
702 if (alpha && info.image.bitDepth == 16)
|
|
703 {
|
|
704 for(size_t i = 0; i < w * h; i++)
|
|
705 res[colors * i .. colors * i + colors] =
|
|
706 [ source[8 * i], source[8 * i + 2], source[8 * i + 4], source[8 * i + 6] ];
|
|
707 }
|
|
708 else if (info.image.bitDepth == 16)
|
|
709 {
|
|
710 for(size_t i = 0; i < w * h; i++)
|
|
711 res[colors * i .. colors * i + 3] =
|
|
712 [ source[8 * i], source[8 * i + 2], source[8 * i + 4] ];
|
|
713 }
|
|
714 else // must be RGB
|
|
715 for(size_t i = 0; i < w * h; i++)
|
|
716 res[colors * i .. colors * i + 3] = source[4 * i .. 4 * i + 3];
|
|
717 break;
|
|
718 }
|
|
719 default:
|
|
720 mixin(pngEnforce("false", "invalid conversion"));
|
|
721 break;
|
|
722
|
|
723 }
|
|
724 return res;
|
|
725 }
|
|
726
|
|
727 //package const char[] constructor = "static typeof(*this) create() { typeof(*this) instance; return instance; }";
|
|
728 debug package char[] chunkTypeToString(uint chunkType)
|
|
729 {
|
|
730 char[] result = new char[4];
|
|
731 result[0] = chunkType >> 24 ;
|
|
732 result[1] = chunkType >> 16;
|
|
733 result[2] = chunkType >> 8;
|
|
734 result[3] = chunkType;
|
|
735 return result;
|
|
736 }
|