1 /*
2 Copyright (c) 2019-2020 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 Permission is hereby granted, free of charge, to any person or organization
6 obtaining a copy of the software and accompanying documentation covered by
7 this license (the "Software") to use, reproduce, display, distribute,
8 execute, and transmit the Software, and to prepare derivative works of the
9 Software, and to permit third-parties to whom the Software is furnished to
10 do so, all subject to the following:
11 
12 The copyright notices in the Software and this entire statement, including
13 the above license grant, this restriction and the following disclaimer,
14 must be included in all copies of the Software, in whole or in part, and
15 all derivative works of the Software, unless such copies or derivative
16 works are solely in the form of machine-executable object code generated by
17 a source language processor.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 */
27 
28 module dagon.resource.image;
29 
30 import std.stdio;
31 import std.path;
32 
33 import dlib.core.memory;
34 import dlib.core.ownership;
35 import dlib.core.stream;
36 import dlib.core.compound;
37 import dlib.image.image;
38 /*
39 import dlib.image.io.bmp;
40 import dlib.image.io.png;
41 import dlib.image.io.tga;
42 import dlib.image.io.jpeg;
43 */
44 import dlib.image.io.hdr;
45 import dlib.image.unmanaged;
46 import dlib.image.hdri;
47 import dlib.filesystem.filesystem;
48 
49 import dagon.graphics.containerimage;
50 import dagon.resource.asset;
51 import dagon.resource.stbi;
52 import dagon.resource.dds;
53 
54 class ImageAsset: Asset
55 {
56     UnmanagedImageFactory imageFactory;
57     UnmanagedHDRImageFactory hdrImageFactory;
58     SuperImage image;
59 
60     this(UnmanagedImageFactory imgfac, UnmanagedHDRImageFactory hdrImgFac, Owner o)
61     {
62         super(o);
63         imageFactory = imgfac;
64         hdrImageFactory = hdrImgFac;
65     }
66 
67     ~this()
68     {
69         release();
70     }
71 
72     override bool loadThreadSafePart(string filename, InputStream istrm, ReadOnlyFileSystem fs, AssetManager mngr)
73     {
74         string errMsg;
75 
76         if (filename.extension == ".hdr" ||
77             filename.extension == ".HDR")
78         {
79             Compound!(SuperHDRImage, string) res;
80             res = loadHDR(istrm, hdrImageFactory);
81             image = res[0];
82             errMsg = res[1];
83         }
84         else if (filename.extension == ".dds" ||
85                  filename.extension == ".DDS")
86         {
87             Compound!(ContainerImage, string) res;
88             res = loadDDS(istrm);
89             image = res[0];
90             errMsg = res[1];
91         }
92         else
93         {
94             Compound!(SuperImage, string) res;
95 
96             switch(filename.extension)
97             {
98                 case ".bmp", ".BMP",
99                      ".jpg", ".JPG", ".jpeg", ".JPEG",
100                      ".png", ".PNG",
101                      ".tga", ".TGA",
102                      ".gif", ".GIF",
103                      ".psd", ".PSD":
104                     res = loadImageSTB(istrm, imageFactory);
105                     break;
106                 default:
107                     return false;
108             }
109 
110             image = res[0];
111             errMsg = res[1];
112         }
113 
114         if (image !is null)
115         {
116             return true;
117         }
118         else
119         {
120             writeln(errMsg);
121             return false;
122         }
123     }
124 
125     override bool loadThreadUnsafePart()
126     {
127         if (image !is null)
128             return true;
129         else
130             return false;
131     }
132 
133     override void release()
134     {
135         if (image)
136             Delete(image);
137     }
138 }
139 
140 ImageAsset imageAsset(AssetManager assetManager, string filename)
141 {
142     ImageAsset asset;
143     if (assetManager.assetExists(filename))
144     {
145         asset = cast(ImageAsset)assetManager.getAsset(filename);
146     }
147     else
148     {
149         asset = New!ImageAsset(assetManager.imageFactory, assetManager.hdrImageFactory, assetManager);
150         assetManager.preloadAsset(asset, filename);
151     }
152     return asset;
153 }