1 /**
2  * ae.demo.libpng.pngtobmp
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <vladimir@thecybershadow.net>
12  */
13 
14 module ae.demo.libpng.pngtobmp;
15 
16 import std.exception;
17 import std.file;
18 import std.path;
19 
20 import ae.utils.funopt;
21 import ae.utils.graphics.color;
22 import ae.utils.graphics.image;
23 import ae.utils.graphics.libpng;
24 import ae.utils.main;
25 import ae.utils.meta;
26 
27 void pngtobmp(bool strict, bool alpha, bool padding, bool gray, bool rgb, string[] files)
28 {
29 	enforce(files.length, "No PNG files specified");
30 
31 	void cv3(COLOR)()
32 	{
33 		static if (is(typeof(Image!COLOR.init.toBMP)))
34 		{
35 			foreach (fn; files)
36 			{
37 				auto i = decodePNG!COLOR(cast(ubyte[])read(fn), strict);
38 				write(fn.setExtension(".bmp"), i.toBMP);
39 			}
40 		}
41 		else
42 			throw new Exception("Invalid format options");
43 	}
44 
45 	void cv1(string[] colorChannels)()
46 	{
47 		if (alpha)
48 		{
49 			enforce(!padding, "Can't use --alpha with --padding");
50 			cv3!(Color!(ubyte, ArrayToTuple!(colorChannels ~ "a")))();
51 		}
52 		else
53 		if (padding)
54 			cv3!(Color!(ubyte, ArrayToTuple!(colorChannels ~ "x")))();
55 		else
56 			cv3!(Color!(ubyte, ArrayToTuple!colorChannels))();
57 	}
58 
59 	if (gray)
60 	{
61 		enforce(!rgb, "--rgb meaningless with --gray");
62 		cv1!(["l"])();
63 	}
64 	else
65 	if (rgb)
66 		cv1!(["r", "g", "b"])();
67 	else
68 		cv1!(["b", "g", "r"])();
69 }
70 
71 mixin main!(funopt!pngtobmp);