1 module skia.SKImageInfo;
2 
3 
4 import skia.Definitions;
5 import skia.MathTypes;
6 import skia.SkiaApi;
7 import skia.SKColorSpace;
8 import skia.EnumMappings;
9 
10 import std.concurrency : initOnce;
11 
12 
13 /**
14  * 
15  */
16 struct SKImageInfo // : IEquatable<SKImageInfo>
17 {
18 	enum SKImageInfo Empty = SKImageInfo(0, 0, SKColorType.Unknown);
19 
20 	private __gshared bool _isInitialized = false;
21 
22 	private __gshared int _platformColorAlphaShift;
23 	private __gshared int _platformColorRedShift;
24 	private __gshared int _platformColorGreenShift;
25 	private __gshared int _platformColorBlueShift;
26 	
27 	private static bool initializePlatformColor ()
28 	{
29 		int* a = &_platformColorAlphaShift;
30 		int* r = &_platformColorRedShift;
31 		int* g = &_platformColorGreenShift;
32 		int* b = &_platformColorBlueShift;
33 
34 		SkiaApi.sk_color_get_bit_shift (a, r, g, b);
35 
36 		return true;
37 	}
38 	
39 	static SKColorType PlatformColorType() {
40 		__gshared SKColorType inst;
41 		return initOnce!inst(SkiaApi.sk_colortype_get_default_8888 ().FromNative ());
42 	}
43 
44 	static int PlatformColorAlphaShift() {
45 		initOnce!_isInitialized(initializePlatformColor());
46 		return _platformColorAlphaShift;
47 	}
48 
49 	static int PlatformColorRedShift() {
50 		initOnce!_isInitialized(initializePlatformColor());
51 		return _platformColorRedShift;
52 	}
53 
54 	static int PlatformColorGreenShift() {
55 		initOnce!_isInitialized(initializePlatformColor());
56 		return _platformColorGreenShift;
57 	}
58 
59 	static int PlatformColorBlueShift() {
60 		initOnce!_isInitialized(initializePlatformColor());
61 		return _platformColorBlueShift;
62 	}
63 
64 	int Width; // { get; set; }
65 
66 	int Height; // { get; set; }
67 
68 	SKColorType ColorType; // { get; set; }
69 
70 	SKAlphaType AlphaType; // { get; set; }
71 
72 	SKColorSpace ColorSpace; // { get; set; }
73 
74 	this (int width, int height)
75 	{
76 		Width = width;
77 		Height = height;
78 		ColorType = PlatformColorType;
79 		AlphaType = SKAlphaType.Premul;
80 		ColorSpace = null;
81 	}
82 
83 	this (int width, int height, SKColorType colorType)
84 	{
85 		Width = width;
86 		Height = height;
87 		ColorType = colorType;
88 		AlphaType = SKAlphaType.Premul;
89 		ColorSpace = null;
90 	}
91 
92 	this (int width, int height, SKColorType colorType, SKAlphaType alphaType)
93 	{
94 		Width = width;
95 		Height = height;
96 		ColorType = colorType;
97 		AlphaType = alphaType;
98 		ColorSpace = null;
99 	}
100 
101 	this (int width, int height, SKColorType colorType, SKAlphaType alphaType, SKColorSpace colorspace)
102 	{
103 		Width = width;
104 		Height = height;
105 		ColorType = colorType;
106 		AlphaType = alphaType;
107 		ColorSpace = colorspace;
108 	}
109 
110 	int BytesPerPixel()
111 	{
112 		return ColorType.GetBytesPerPixel ();
113 	}
114 
115 	int BitsPerPixel() { return BytesPerPixel * 8; }
116 
117 	int BytesSize() { return Width * Height * BytesPerPixel; }
118 
119 	long BytesSize64() { return cast(long)Width * cast(long)Height * cast(long)BytesPerPixel; }
120 
121 	int RowBytes() { return Width * BytesPerPixel; }
122 
123 	long RowBytes64() { return cast(long)Width * cast(long)BytesPerPixel; }
124 
125 	bool IsEmpty() { return Width <= 0 || Height <= 0; }
126 
127 	bool IsOpaque() { return AlphaType == SKAlphaType.Opaque; }
128 
129 	SKSizeI Size() { return SKSizeI (Width, Height); }
130 
131 	SKRectI Rect() { return SKRectI.Create (Width, Height); }
132 
133 	SKImageInfo WithSize (SKSizeI size)
134 	{
135 		return WithSize (size.Width, size.Height);
136 	}
137 
138 	SKImageInfo WithSize (int width, int height)
139 	{
140 		SKImageInfo copy = this;
141 		copy.Width = width;
142 		copy.Height = height;
143 		return copy;
144 	}
145 
146 	SKImageInfo WithColorType (SKColorType newColorType)
147 	{
148 		SKImageInfo copy = this;
149 		copy.ColorType = newColorType;
150 		return copy;
151 	}
152 
153 	SKImageInfo WithColorSpace (SKColorSpace newColorSpace)
154 	{
155 		SKImageInfo copy = this;
156 		copy.ColorSpace = newColorSpace;
157 		return copy;
158 	}
159 
160 	SKImageInfo WithAlphaType (SKAlphaType newAlphaType)
161 	{
162 		SKImageInfo copy = this;
163 		copy.AlphaType = newAlphaType;
164 		return copy;
165 	}
166 
167 	// bool Equals (SKImageInfo obj) =>
168 	// 	ColorSpace == obj.ColorSpace &&
169 	// 	Width == obj.Width &&
170 	// 	Height == obj.Height &&
171 	// 	ColorType == obj.ColorType &&
172 	// 	AlphaType == obj.AlphaType;
173 
174 	// override bool Equals (object obj) =>
175 	// 	obj is SKImageInfo f && Equals (f);
176 
177 	// static bool operator == (SKImageInfo left, SKImageInfo right) =>
178 	// 	left.Equals (right);
179 
180 	// static bool operator != (SKImageInfo left, SKImageInfo right) =>
181 	// 	!left.Equals (right);
182 
183 	// override int GetHashCode ()
184 	// {
185 	// 	var hash = new HashCode ();
186 	// 	hash.Add (ColorSpace);
187 	// 	hash.Add (Width);
188 	// 	hash.Add (Height);
189 	// 	hash.Add (ColorType);
190 	// 	hash.Add (AlphaType);
191 	// 	return hash.ToHashCode ();
192 	// }
193 
194 
195   	static void UpdateNative (ref SKImageInfo managed, ref SKImageInfoNative native)
196 	{
197 		SKColorSpace colorSpace = managed.ColorSpace;
198 		if(colorSpace !is null && colorSpace.Handle() !is null) {
199 			native.colorspace = cast(sk_colorspace_t*)colorSpace.Handle();
200 		} else {
201 			native.colorspace = null;
202 		}
203 
204 		// native.colorspace = managed.ColorSpace?.Handle ?? null;
205 		native.width = managed.Width;
206 		native.height = managed.Height;
207 		native.colorType = managed.ColorType.ToNative ();
208 		native.alphaType = managed.AlphaType;
209 	}
210 
211 	static SKImageInfoNative FromManaged (ref SKImageInfo managed)
212 	{
213 		SKImageInfoNative imageInfo;
214 
215 		SKColorSpace colorSpace = managed.ColorSpace;
216 		if(colorSpace !is null && colorSpace.Handle() !is null) {
217 			imageInfo.colorspace = cast(sk_colorspace_t*)colorSpace.Handle();
218 		} else {
219 			imageInfo.colorspace = null;
220 		}
221 		imageInfo.width = managed.Width;
222 		imageInfo.height = managed.Height;
223 		imageInfo.colorType = managed.ColorType.ToNative ();
224 		imageInfo.alphaType = managed.AlphaType;
225 
226 		return imageInfo;
227 	}
228 
229 	static SKImageInfo ToManaged (ref SKImageInfoNative native)
230 	{
231 		SKImageInfo imageInfo;
232 
233 		imageInfo.ColorSpace = SKColorSpace.GetObject (native.colorspace);
234 		imageInfo.Width = native.width;
235 		imageInfo.Height = native.height;
236 		imageInfo.ColorType = native.colorType.FromNative ();
237 		imageInfo.AlphaType = native.alphaType;
238 
239 		return imageInfo;
240 	}
241 
242 }