1 /*
2 Copyright (c) 2017-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.texture;
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.texture;
50 import dagon.graphics.containerimage;
51 import dagon.resource.asset;
52 import dagon.resource.stbi;
53 import dagon.resource.dds;
54 
55 class TextureAsset: Asset
56 {
57     UnmanagedImageFactory imageFactory;
58     UnmanagedHDRImageFactory hdrImageFactory;
59     Texture texture;
60 
61     this(UnmanagedImageFactory imgfac, UnmanagedHDRImageFactory hdrImgFac, Owner o)
62     {
63         super(o);
64         imageFactory = imgfac;
65         hdrImageFactory = hdrImgFac;
66         texture = New!Texture(this);
67     }
68 
69     ~this()
70     {
71         release();
72     }
73 
74     override bool loadThreadSafePart(string filename, InputStream istrm, ReadOnlyFileSystem fs, AssetManager mngr)
75     {
76         string errMsg;
77 
78         if (filename.extension == ".hdr" ||
79             filename.extension == ".HDR")
80         {
81             Compound!(SuperHDRImage, string) res;
82             res = loadHDR(istrm, hdrImageFactory);
83             texture.image = res[0];
84             errMsg = res[1];
85         }
86         else if (filename.extension == ".dds" ||
87                  filename.extension == ".DDS")
88         {
89             Compound!(ContainerImage, string) res;
90             res = loadDDS(istrm);
91             texture.image = res[0];
92             errMsg = res[1];
93         }
94         else
95         {
96             Compound!(SuperImage, string) res;
97 
98             switch(filename.extension)
99             {
100                 case ".bmp", ".BMP",
101                      ".jpg", ".JPG", ".jpeg", ".JPEG",
102                      ".png", ".PNG",
103                      ".tga", ".TGA",
104                      ".gif", ".GIF",
105                      ".psd", ".PSD":
106                     res = loadImageSTB(istrm, imageFactory);
107                     break;
108                 default:
109                     return false;
110             }
111 
112             texture.image = res[0];
113             errMsg = res[1];
114         }
115 
116         if (texture.image !is null)
117         {
118             return true;
119         }
120         else
121         {
122             writeln(errMsg);
123             return false;
124         }
125     }
126 
127     override bool loadThreadUnsafePart()
128     {
129         if (texture.image !is null)
130         {
131             texture.createFromImage(texture.image);
132             if (texture.valid)
133             {
134                 return true;
135             }
136             else
137                 return false;
138         }
139         else
140         {
141             return false;
142         }
143     }
144 
145     override void release()
146     {
147         if (texture)
148             texture.release();
149     }
150 }
151 
152 TextureAsset textureAsset(AssetManager assetManager, string filename)
153 {
154     TextureAsset asset;
155     if (assetManager.assetExists(filename))
156     {
157         asset = cast(TextureAsset)assetManager.getAsset(filename);
158     }
159     else
160     {
161         asset = New!TextureAsset(assetManager.imageFactory, assetManager.hdrImageFactory, assetManager);
162         assetManager.preloadAsset(asset, filename);
163     }
164     return asset;
165 }