1 /*
2 Copyright (c) 2019 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.imageasset;
29 
30 import std.stdio;
31 import std.path;
32 
33 import dlib.core.memory;
34 import dlib.core.stream;
35 import dlib.core.compound;
36 import dlib.image.image;
37 import dlib.image.io.bmp;
38 import dlib.image.io.png;
39 import dlib.image.io.tga;
40 import dlib.image.io.jpeg;
41 import dlib.image.io.hdr;
42 import dlib.image.unmanaged;
43 import dlib.image.hdri;
44 import dlib.filesystem.filesystem;
45 
46 import dagon.core.ownership;
47 import dagon.resource.asset;
48 
49 class ImageAsset: Asset
50 {
51     UnmanagedImageFactory imageFactory;
52     UnmanagedHDRImageFactory hdrImageFactory;
53     SuperImage image;
54 
55     this(UnmanagedImageFactory imgfac, UnmanagedHDRImageFactory hdrImgFac, Owner o)
56     {
57         super(o);
58         imageFactory = imgfac;
59         hdrImageFactory = hdrImgFac;
60     }
61 
62     ~this()
63     {
64         release();
65     }
66 
67     override bool loadThreadSafePart(string filename, InputStream istrm, ReadOnlyFileSystem fs, AssetManager mngr)
68     {
69         string errMsg;
70 
71         if (filename.extension == ".hdr" ||
72             filename.extension == ".HDR")
73         {
74             Compound!(SuperHDRImage, string) res;
75             res = loadHDR(istrm, hdrImageFactory);
76             image = res[0];
77             errMsg = res[1];
78         }
79         else
80         {
81             Compound!(SuperImage, string) res;
82 
83             switch(filename.extension)
84             {
85                 case ".bmp", ".BMP":
86                     res = loadBMP(istrm, imageFactory);
87                     break;
88                 case ".jpg", ".JPG", ".jpeg", ".JPEG":
89                     res = loadJPEG(istrm, imageFactory);
90                     break;
91                 case ".png", ".PNG":
92                     res = loadPNG(istrm, imageFactory);
93                     break;
94                 case ".tga", ".TGA":
95                     res = loadTGA(istrm, imageFactory);
96                     break;
97                 default:
98                     return false;
99             }
100 
101             image = res[0];
102             errMsg = res[1];
103         }
104 
105         if (image !is null)
106         {
107             return true;
108         }
109         else
110         {
111             writeln(errMsg);
112             return false;
113         }
114     }
115 
116     override bool loadThreadUnsafePart()
117     {
118         if (image !is null)
119             return true;
120         else
121             return false;
122     }
123 
124     override void release()
125     {
126         if (image)
127             Delete(image);
128     }
129 }
130 
131 ImageAsset imageAsset(AssetManager assetManager, string filename)
132 {
133     ImageAsset asset;
134     if (assetManager.assetExists(filename))
135     {
136         asset = cast(ImageAsset)assetManager.getAsset(filename);
137     }
138     else
139     {
140         asset = New!ImageAsset(assetManager.imageFactory, assetManager.hdrImageFactory, assetManager);
141         assetManager.preloadAsset(asset, filename);
142     }
143     return asset;
144 }