1 /*
2 Copyright (c) 2015-2021 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 /**
30  * GC-free SuperImage implementation
31  *
32  * Copyright: Timur Gafarov 2015-2021.
33  * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0).
34  * Authors: Timur Gafarov
35  */
36 module dlib.image.unmanaged;
37 
38 import dlib.image.image;
39 import dlib.core.memory;
40 
41 /**
42  * Image that uses dlib.core.memory instead of GC
43  */
44 class UnmanagedImage(IntegerPixelFormat fmt): Image!(fmt)
45 {
46     override @property SuperImage dup()
47     {
48         auto res = New!(UnmanagedImage!(fmt))(_width, _height);
49         res.data[] = data[];
50         return res;
51     }
52 
53     override SuperImage createSameFormat(uint w, uint h)
54     {
55         return New!(UnmanagedImage!(fmt))(w, h);
56     }
57 
58     this(uint w, uint h)
59     {
60         super(w, h);
61     }
62 
63     ~this()
64     {
65         Delete(_data);
66     }
67 
68     protected override void allocateData()
69     {
70         _data = New!(ubyte[])(_width * _height * _pixelSize);
71     }
72 
73     override void free()
74     {
75         Delete(this);
76     }
77 }
78 
79 /// Specialization of UnmanagedImage for 8-bit luminance pixel format
80 alias UnmanagedImageL8 = UnmanagedImage!(IntegerPixelFormat.L8);
81 /// Specialization of UnmanagedImage for 8-bit luminance-alpha pixel format
82 alias UnmanagedImageLA8 = UnmanagedImage!(IntegerPixelFormat.LA8);
83 /// Specialization of UnmanagedImage for 8-bit RGB pixel format
84 alias UnmanagedImageRGB8 = UnmanagedImage!(IntegerPixelFormat.RGB8);
85 /// Specialization of UnmanagedImage for 8-bit RGBA pixel format
86 alias UnmanagedImageRGBA8 = UnmanagedImage!(IntegerPixelFormat.RGBA8);
87 
88 /// Specialization of UnmanagedImage for 16-bit luminance pixel format
89 alias UnmanagedImageL16 = UnmanagedImage!(IntegerPixelFormat.L16);
90 /// Specialization of UnmanagedImage for 16-bit luminance-alpha pixel format
91 alias UnmanagedImageLA16 = UnmanagedImage!(IntegerPixelFormat.LA16);
92 /// Specialization of UnmanagedImage for 16-bit RGB pixel format
93 alias UnmanagedImageRGB16 = UnmanagedImage!(IntegerPixelFormat.RGB16);
94 /// Specialization of UnmanagedImage for 16-bit RGBA pixel format
95 alias UnmanagedImageRGBA16 = UnmanagedImage!(IntegerPixelFormat.RGBA16);
96 
97 /**
98  * UnmanagedImage factory class
99  */
100 class UnmanagedImageFactory: SuperImageFactory
101 {
102     SuperImage createImage(uint w, uint h, uint channels, uint bitDepth, uint numFrames = 1)
103     {
104         return unmanagedImage(w, h, channels, bitDepth);
105     }
106 }
107 
108 /**
109  * UnmanagedImage factory function
110  */
111 SuperImage unmanagedImage(uint w, uint h, uint channels = 3, uint bitDepth = 8)
112 in
113 {
114     assert(channels > 0 && channels <= 4);
115     assert(bitDepth == 8 || bitDepth == 16);
116 }
117 do
118 {
119     switch(channels)
120     {
121         case 1:
122         {
123             if (bitDepth == 8)
124                 return New!UnmanagedImageL8(w, h);
125             else
126                 return New!UnmanagedImageL16(w, h);
127         }
128         case 2:
129         {
130             if (bitDepth == 8)
131                 return New!UnmanagedImageLA8(w, h);
132             else
133                 return New!UnmanagedImageLA16(w, h);
134         }
135         case 3:
136         {
137             if (bitDepth == 8)
138                 return New!UnmanagedImageRGB8(w, h);
139             else
140                 return New!UnmanagedImageRGB16(w, h);
141         }
142         case 4:
143         {
144             if (bitDepth == 8)
145                 return New!UnmanagedImageRGBA8(w, h);
146             else
147                 return New!UnmanagedImageRGBA16(w, h);
148         }
149         default:
150             assert(0);
151     }
152 }