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