1 module pxv.lib.image;
2 
3 public import stb.image: STBImage = Image;
4 public import stb.image: Pixel = Color;
5 
6 version (Have_speedy_stdio)
7     import speedy.stdio: writeln;
8 else
9     import std.stdio: writeln;
10 
11 import std.conv: to;
12 import std.algorithm.comparison: max;
13 
14 import core.stdc.stdlib: exit;
15 const int alphaThreshold = 64;
16 
17 Image loadImage(string filepath) {
18     STBImage stbimg;
19     try {
20         stbimg = new STBImage(filepath);
21     } catch (Exception e) {
22         writeln(e.msg);
23         exit(1);
24     }
25     Image img = Image(
26         stbimg.opSlice, stbimg.w, stbimg.h,
27         stbimg.frames, cast(uint[]) (stbimg.delays[0..stbimg.frames]),
28         stbimg.frames > 2);
29     return img;
30 }
31 
32 Image loadImageURL(string url) {
33     import std.net.curl;
34     import std.file: tempDir;
35     import std.path: dirSeparator;
36     download(url, tempDir ~ "pxv-tempFile");
37     // writeln(tempDir ~ "pxv-tempFile");
38     return loadImage(tempDir ~ "pxv-tempFile");
39     // STBImage stbimg;
40     // HTTP http = HTTP();
41     // // http.verbose(true);
42     // string content = cast(string) get!(HTTP, ubyte)(url, http);
43     // writeln(content);
44     // try {
45     //     stbimg = new STBImage(cast(void[]) content);
46     // } catch (Exception e) {
47     //     writeln(e.msg);
48     //     exit(1);
49     // }
50     // Image img = Image(
51     //     stbimg.opSlice, stbimg.w, stbimg.h,
52     //     stbimg.frames, cast(uint[]) (stbimg.delays[0..stbimg.frames]),
53     //     stbimg.frames > 2);
54     // return img;
55 }
56 
57 
58 struct Image {
59     Pixel[] data;
60     uint w;
61     uint h;
62     uint frameCount;
63     uint[] delays;
64     bool isGif;
65 }
66 
67 Pixel opIndex(Image img, uint x, uint y) {
68     return img.data[y * img.w + x];
69 }
70 
71 Pixel opIndex(Image img, uint x, uint y, uint frame) {
72     return img.data[y * img.w + x + frame * img.w * img.h];
73 }
74 
75 void resizeImage(ref Image img, uint newWitdth, uint newHeight) {
76     uint w = newWitdth;
77     uint h = newHeight;
78 
79     Pixel[] pix = new Pixel[](newWitdth * newHeight * img.frameCount);
80     float xsc = img.w.to!float / w / 1f;
81     float ysc = img.h.to!float / h / 1f;
82     uint xsci = max(1, cast(int) (img.w / w / 1f));
83     uint ysci = max(1, cast(int) (img.h / h / 1f));
84     uint wh = xsci * ysci;
85 
86     for (uint f = 0; f < img.frameCount; ++f) {
87         ulong offset = f * img.w * img.h;
88         for (uint y = 0; y < h; ++y) {
89             for (uint x = 0; x < w; ++x) {
90                 uint r = 0;
91                 uint g = 0;
92                 uint b = 0;
93                 uint a = 0;
94 
95                 uint srcx = cast(int) (xsc * x);
96                 uint srcy = cast(int) (ysc * y);
97                 for (uint yi = 0; yi < ysci; ++yi) {
98                     for (uint xi = 0; xi < xsci; ++xi) {
99                         ulong ipos = (srcy + yi) * img.w + srcx + xi + offset;
100 
101                         r += img.data[ipos].r;
102                         g += img.data[ipos].g;
103                         b += img.data[ipos].b;
104                         a += img.data[ipos].a;
105                     }
106                 }
107                 uint pos = (y * w + x) + w * h * f;
108 
109                 pix[pos].r = to!ubyte(r / wh);
110                 pix[pos].g = to!ubyte(g / wh);
111                 pix[pos].b = to!ubyte(b / wh);
112                 pix[pos].a = to!ubyte(a / wh);
113 
114             }
115         }
116     }
117 
118     img.w = w;
119     img.h = h;
120     img.data = pix;
121 }
122 
123 bool transparent(Pixel p) {
124     return p.a < alphaThreshold;
125 }
126