1 module pxv.lib;
2 
3 import std.algorithm : canFind, clamp, countUntil, find, max, min, startsWith;
4 import std.conv : to;
5 import std.file;
6 import std.format : format;
7 import std.getopt;
8 import std.math : floor;
9 
10 import sily.color;
11 import sily.bashfmt: BG, FG, FM, FR;
12 import sily.vector: ivec2;
13 import sily.terminal;
14 // import speedy.stdio : write, writef, writefln, writeln;
15 
16 import pxv.lib.image;
17 // cls && /g/pxv/bin/pxv ~/Pictures/20407219.png ~/Pictures/roofline-girl-1s-3840x2400.jpg ~/Pictures/anime-wallpaper.jpg
18 // cls && ./bin/pxv ~/Pictures/20407219.png
19 
20 private string brightChars = r" .`^,:;!-~=+<>[]{}*JS?AX#%@";
21 
22 private const int alphaThreshold = 64;
23 
24 private alias fFloor = (T) => floor(to!float(T));
25 private alias fFloorToInt = (T) => to!int(fFloor(T));
26 private alias fClamp = (T, M, A) => clamp(to!float(T), to!float(M), to!float(A));
27 private alias fClampToInt = (T, M, A) => to!int(fClamp(T, M, A));
28 
29 private string upperBlock = "\u2580";
30 private string lowerBlock = "\u2584";
31 
32 enum MatchSize {
33     none, width, height, fit
34 }
35 
36 enum ColorType {
37     ansi8, ansi256, truecolor
38 }
39 
40 struct Config {
41     int width = 0;
42     int height = 0;
43     MatchSize size = MatchSize.none;
44     ColorType color = ColorType.truecolor;
45     bool grayscale = false;
46     bool lowres = false;
47     bool useAscii = false;
48     string asciiPalette = "";
49     bool hideBackground = false;
50     bool useUnicode = false;
51     bool loopOnce = false;
52     bool still = false;
53     int frame = -1;
54 }
55 
56 private Config conf;
57 private string _out = "";
58 
59 string getImageString(Config p_conf, Image img) {
60     _out = "";
61     conf = p_conf;
62 
63     if (conf.lowres && conf.hideBackground && !conf.useAscii) {
64         conf.hideBackground = false;
65     }
66 
67     int trows = conf.height;
68     int tcolumns = conf.width;
69 
70     if (conf.asciiPalette != "") {
71         brightChars = conf.asciiPalette;
72     }
73 
74     import std.conv: to;
75     // writeln(conf.color.to!string);
76 
77     int height = img.h;
78 
79     int frame = 0;
80     // _out ~= "\033[?25l";
81     if (conf.frame != -1 && conf.frame < img.frameCount) {
82         frame = conf.frame;
83     }
84     // for (; frame < img.frameCount; ++frame) {
85         // fwrite(FR.fullreset);
86         // cursorMoveTo(1);
87     _out ~= "\033[1G";
88     for (int y = 0; y < trows; ++y) {
89         for (int x = 0; x < tcolumns / (conf.lowres ? 2 : 1); ++x) {
90 
91             Pixel upperPix = img.opIndex(x * (conf.lowres ? 2 : 1), y * 2, frame);
92             Color upperCol = upperPix.pixelColor();
93             if (conf.grayscale) upperCol = Color(upperCol.luminance);
94             if (!conf.lowres) {
95                 if (y * 2 < height - 1) {
96                     Pixel lowerPix = img.opIndex(x, y * 2 + 1, frame);
97                     Color lowerCol = lowerPix.pixelColor();
98                     if (conf.grayscale) lowerCol = Color(lowerCol.luminance);
99 
100                     if (upperPix.transparent) {
101                         if (lowerPix.transparent) {
102                             write(FR.fullreset ~ " ");
103                         } else {
104                             if (conf.useAscii) {
105                                 char c = lowerCol.getChar();
106                                 writePixel!true(BG.reset, lowerCol, [c]);
107                             } else {
108                                 writePixel!false(BG.reset, lowerCol, lowerBlock);
109                             }
110                         }
111                     } else { // upperPix.transparent
112                         if (lowerPix.transparent) {
113                             if (conf.useAscii) {
114                                 writePixel!true(upperCol, FG.reset, " ");
115                             } else {
116                                 writePixel!false(BG.reset, upperCol, upperBlock);
117                             }
118                         } else {
119                             if (conf.useAscii) {
120                                 char c = lowerCol.getChar();
121                                 writePixel(upperCol, lowerCol, [c]);
122                             } else {
123                                 writePixel(upperCol, lowerCol, lowerBlock);
124                             }
125                         }
126                     }
127                 } else { // y * 2 < height - 1
128                     if (upperPix.transparent) {
129                         write(FR.fullreset ~ " ");
130                     } else {
131                         if (conf.useAscii) {
132                             writePixel!true(upperCol, FG.reset, " ");
133                         } else {
134                             writePixel!false(BG.reset, upperCol, upperBlock);
135                         }
136                     }
137                 }
138             } else { // chalf
139                 if (y * 2 < height - 1) {
140                     Pixel lowerPix = img.opIndex(x * 2, y * 2 + 1, frame);
141                     Color lowerCol = lowerPix.pixelColor();
142                     if (conf.grayscale) lowerCol = Color(lowerCol.luminance);
143 
144                     if (upperPix.transparent) {
145                         if (lowerPix.transparent) {
146                             write(FR.fullreset ~ "  ");
147                         } else {
148                             if (conf.useAscii) {
149                                 char c = lowerCol.getChar();
150                                 writePixel!true(BG.reset, lowerCol, [c, c]);
151                             } else {
152                                 writePixel!false(BG.reset, lowerCol, "  ");
153                             }
154                         }
155                     } else { // upperPix.transparent
156                         if (lowerPix.transparent) {
157                             if (conf.useAscii) {
158                                 writePixel!true(upperCol, FG.reset, "  ");
159                             } else {
160                                 writePixel!false(BG.reset, upperCol, "  ");
161                             }
162                         } else {
163                             if (conf.useAscii) {
164                                 char c = lowerCol.getChar();
165                                 writePixel(upperCol, lowerCol, [c, c]);
166                             } else {
167                                 writePixel(upperCol, lowerCol, "  ");
168                             }
169                         }
170                     }
171                 } else { // y * 2 < height - 1
172                     if (upperPix.transparent) {
173                         fwrite(FR.fullreset);
174                     } else {
175                         if (conf.useAscii) {
176                             writePixel!true(upperCol, FG.reset, "  ");
177                         } else {
178                             writePixel!true(BG.reset, upperCol, "  ");
179                         }
180                     }
181                 }
182             }
183 
184         } // x
185         if (img.isGif && y == trows - 1) {
186             fwrite(FR.fullreset);
187         } else {
188             fwriteln(FR.fullreset);
189         }
190     } // y
191 
192 
193     return _out;
194 }
195 
196 private void write(string t) {
197     _out ~= t;
198 }
199 
200 private void writeln(string t) {
201     _out ~= t ~ "\n";
202 }
203 
204 private void fwrite(A...)(A args) {
205     foreach (arg; args) {
206         write(cast(string) arg);
207     }
208 }
209 
210 private void fwriteln(A...)(A args) {
211     foreach (arg; args) {
212         write(cast(string) arg);
213     }
214     write("\n");
215 }
216 
217 private void writePixel(bool fgOnly = false)(BG bg, Color fg, string c) {
218     if (fgOnly || !conf.hideBackground) {
219         if (conf.color == ColorType.ansi8)
220             fwrite(fg.toAnsi8String(), c);
221         else if (conf.color == ColorType.truecolor)
222             fwrite(fg.toTrueColorString(), c);
223         else
224             fwrite(fg.toAnsiString(), c);
225     } else {
226         if (conf.color == ColorType.ansi8)
227             fwrite(bg, fg.toAnsi8String(), c);
228         else if (conf.color == ColorType.truecolor)
229             fwrite(bg, fg.toTrueColorString(), c);
230         else
231             fwrite(bg, fg.toAnsiString(), c);
232     }
233 }
234 
235 private void writePixel(bool bgOnly = false)(Color bg, FG fg, string c) {
236     if (conf.hideBackground) {
237         write(c);
238         return;
239     }
240     if (bgOnly) {
241         if (conf.color == ColorType.ansi8)
242             fwrite(bg.toAnsi8String(true), c);
243         else if (conf.color == ColorType.truecolor)
244             fwrite(bg.toTrueColorString(true), c);
245         else
246             fwrite(bg.toAnsiString(true), c);
247     } else {
248         if (conf.color == ColorType.ansi8)
249             fwrite(fg, bg.toAnsi8String(true), c);
250         else if (conf.color == ColorType.truecolor)
251             fwrite(fg, bg.toTrueColorString(true), c);
252         else
253             fwrite(fg, bg.toAnsiString(true), c);
254     }
255 }
256 
257 private void writePixel(Color bg, Color fg, string c) {
258     if (!conf.hideBackground) {
259         if (conf.color == ColorType.ansi8)
260             fwrite(bg.toAnsi8String(true), fg.toAnsi8String(), c);
261         else if (conf.color == ColorType.truecolor)
262             fwrite(bg.toTrueColorString(true), fg.toTrueColorString(), c);
263         else
264             fwrite(bg.toAnsiString(true), fg.toAnsiString(), c);
265     } else {
266         if (conf.color == ColorType.ansi8)
267             fwrite(fg.toAnsi8String(), c);
268         else if (conf.color == ColorType.truecolor)
269             fwrite(fg.toTrueColorString(), c);
270         else
271             fwrite(fg.toAnsiString(), c);
272     }
273 }
274 
275 private Color pixelColor(Pixel p) {
276     return Color(p.r / 255.0, p.g / 255.0, p.b / 255.0);
277 }
278 
279 
280 private char getChar(Color _col) {
281     int maxPos = brightChars.length.to!int - 1;
282     int p = clamp( _col.luminance() * maxPos, 0, maxPos).to!int;
283     return brightChars[p];
284 }