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.graphics.containerimage;
29 
30 import std.stdio;
31 
32 import dlib.core.memory;
33 import dlib.image.color;
34 import dlib.image.image;
35 import dlib.image.hdri;
36 
37 enum ContainerImageFormat: uint
38 {
39     R8 = IntegerPixelFormat.L8,
40     RG8 = IntegerPixelFormat.LA8,
41     RGB8 = IntegerPixelFormat.RGB8,
42     RGBA8 = IntegerPixelFormat.RGBA8,
43     RGBAF32 = FloatPixelFormat.RGBAF32,
44     RGBAF16 = 9, //FloatPixelFormat.RGBAF16
45     
46     S3TC_RGB_DXT1 = 256,
47     S3TC_RGBA_DXT3 = 257,
48     S3TC_RGBA_DXT5 = 258,
49 
50     RGTC1_R = 259,
51     RGTC1_R_S = 260,
52     RGTC2_RG = 261,
53     RGTC2_RG_S = 262,
54 
55     BPTC_RGBA_UNORM = 263,
56     BPTC_SRGBA_UNORM = 264,
57     BPTC_RGB_SF = 265,
58     BPTC_RGB_UF = 266
59 }
60 
61 class ContainerImage: SuperImage
62 {
63     uint _width;
64     uint _height;
65     ContainerImageFormat _containerImageFormat;
66     uint _mipLevels;
67     bool _isCubemap;
68     size_t _bufferSize;
69     ubyte[] _data;
70 
71     this(uint w, uint h, ContainerImageFormat format, uint mipmaps, size_t bufferSize, bool cubemap = false)
72     {
73         _width = w;
74         _height = h;
75         _containerImageFormat = format;
76         _mipLevels = mipmaps;
77         _bufferSize = bufferSize;
78         _isCubemap = cubemap;
79         _data = New!(ubyte[])(_bufferSize);
80     }
81 
82     ~this()
83     {
84         if (data.length)
85             Delete(_data);
86     }
87 
88     void free()
89     {
90         Delete(this);
91     }
92 
93     @property uint width()
94     {
95         return _width;
96     }
97 
98     @property uint height()
99     {
100         return _height;
101     }
102 
103     @property uint bitDepth()
104     {
105         return 0;
106     }
107 
108     @property uint channels()
109     {
110         return 0;
111     }
112 
113     @property uint pixelSize()
114     {
115         return 0;
116     }
117 
118     @property uint pixelFormat()
119     {
120         return _containerImageFormat;
121     }
122 
123     @property uint mipLevels()
124     {
125         return _mipLevels;
126     }
127     
128     @property bool isCubemap()
129     {
130         return _isCubemap;
131     }
132 
133     @property ubyte[] data()
134     {
135         return _data;
136     }
137 
138     @property SuperImage dup()
139     {
140         auto res = New!ContainerImage(_width, _height, _containerImageFormat, _mipLevels, _bufferSize, _isCubemap);
141         res.data[] = data[];
142         return res;
143     }
144 
145     SuperImage createSameFormat(uint w, uint h)
146     {
147         return null;
148     }
149 
150     Color4f opIndex(int x, int y)
151     {
152         return Color4f(0.0f, 0.0f, 0.0f, 0.0f);
153     }
154 
155     Color4f opIndexAssign(Color4f c, int x, int y)
156     {
157         return Color4f(0.0f, 0.0f, 0.0f, 0.0f);
158     }
159 }