-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGDI32.lua
403 lines (295 loc) · 9.53 KB
/
GDI32.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
local ffi = require "ffi"
local GDILib = ffi.load("gdi32");
local gdi32_ffi = require "gdi32_ffi"
ffi.cdef[[
typedef struct {
HDC Handle;
} DeviceContext;
]]
DeviceContext = ffi.typeof("DeviceContext");
local GDI32 = {
FFI = gdi32_ffi,
CreateDC = function(lpszDriver, lpszDevice, lpszOutput, lpInitData)
return DeviceContext(GDILib.CreateDCA(lpszDriver, lpszDevice, lpszOutput, lpInitData));
end,
CreateCompatibleDC = function(hdc)
return DeviceContext(GDILib.CreateCompatibleDC(hdc));
end,
CreateDCForDefaultDisplay = function(self)
return DeviceContext(GDILib.CreateDCA("DISPLAY", nil, nil, nil));
end,
CreateDCForMemory = function()
local displayDC = GDILib.CreateDCA("DISPLAY", nil, nil, nil)
return GDIContext(GDILib.CreateCompatibleDC(displayDC))
end,
GetDC = function(hWnd)
return DeviceContext(GDILib.GetDC(hWnd));
end,
SaveDC = function(hdc)
return GDILib.SaveDC(hdc);
end,
RestoreDC = function(hdc, nSaveDC)
return GDILib.RestoreDC(hdc, nSavedDC);
end,
-- Object Management
GetObject = function(hgdiobj, cbBuffer, lpvObject)
return GDILib.GetObjectA(hgdiobj, cbBuffer, lpvObject);
end,
GetStockObject = function(fnObject)
return GDILib.GetStockObject(fnObject);
end,
-- Bitmaps
CreateCompatibleBitmap = function(hdc, nWidth, nHeight)
return GDILib.CreateCompatibleBitmap(hdc, nWidth, nHeight);
end,
CreateDIBSection = function(hdc, pbmi, iUsage, ppvBits, hSection, dwOffset)
return GDILib.CreateDIBSection(hdc, pbmi, iUsage, ppvBits, hSection, dwOffset);
end,
}
DeviceContext_mt = {
__tostring = function(self)
return string.format("DeviceContext(0x%s)", tostring(self.Handle))
end,
__index = {
TypeName = "DeviceContext",
Size = ffi.sizeof("DeviceContext"),
CreateCompatibleDC = function(self)
return DeviceContext(GDILib.CreateCompatibleDC(self.Handle))
end,
CreateCompatibleBitmap = function(self, width, height)
local bm = GDIBitmap(GDILib.CreateCompatibleBitmap(self.Handle,width,height));
bm:Init(self.Handle)
return bm
end,
-- Device Context State
Flush = function(self)
return GDILib.GdiFlush()
end,
-- Object Management
SelectObject = function(self, gdiobj)
GDILib.SelectObject(self.Handle, gdiobj.Handle)
end,
SelectStockObject = function(self, objectIndex)
-- First get a handle on the object
local objHandle = GDILib.GetStockObject(objectIndex);
-- Then select it into the device context
return GDILib.SelectObject(self.Handle, objHandle);
end,
-- Drawing Attributes
UseDCBrush = function(self)
self:SelectStockObject(gdi_ffi.DC_BRUSH)
end,
UseDCPen = function(self)
self:SelectStockObject(gdi_ffi.DC_PEN)
end,
SetDCBrushColor = function(self, color)
return GDILib.SetDCBrushColor(self.Handle, color)
end,
SetDCPenColor = function(self, color)
return GDILib.SetDCPenColor(self.Handle, color)
end,
-- Drawing routines
MoveTo = function(self, x, y)
local result = GDILib.MoveToEx(self.Handle, x, y, nil);
return result
end,
MoveToEx = function(self, x, y, lpPoint)
return GDILib.MoveToEx(self.Handle, X, Y, lpPoint);
end,
SetPixel = function(self, x, y, color)
return GDILib.SetPixel(self.Handle, x, y, color);
end,
SetPixelV = function(self, x, y, crColor)
return GDILib.SetPixelV(self.Handle, X, Y, crColor);
end,
LineTo = function(self, xend, yend)
local result = GDILib.LineTo(self.Handle, xend, yend);
return result
end,
Ellipse = function(self, nLeftRect, nTopRect, nRightRect, nBottomRect)
return GDILib.Ellipse(self.Handle,nLeftRect,nTopRect,nRightRect,nBottomRect);
end,
Rectangle = function(self, left, top, right, bottom)
return GDILib.Rectangle(self.Handle, left, top, right, bottom);
end,
RoundRect = function(self, left, top, right, bottom, width, height)
return GDILib.RoundRect(self.Handle, left, top, right, bottom, width, height);
end,
-- Text Drawing
Text = function(self, txt, x, y)
x = x or 0
y = y or 0
return C.TextOutA(self.Handle, x, y, txt, string.len(txt));
end,
-- Bitmap drawing
BitBlt = function(self, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, dwRop)
return GDILib.BitBlt(self.Handle,nXDest,nYDest,nWidth,nHeight,hdcSrc,nXSrc,nYSrc,dwRop);
end,
StretchDIBits = function(self, XDest, YDest, nDestWidth, nDestHeight, XSrc, YSrc, nSrcWidth, nSrcHeight, lpBits, lpBitsInfo, iUsage, dwRop)
XDest = XDest or 0
YDest = YDest or 0
iUsage = iUsage or 0
dwRop = dwRop or gdi32_ffi.SRCCOPY;
return GDILib.StretchDIBits(hdc,XDest,YDest,nDestWidth,nDestHeight,XSrc,YSrc,nSrcWidth,nSrcHeight,lpBits,lpBitsInfo,iUsage,dwRop);
end,
GetDIBits = function(self, hbmp, uStartScan, cScanLines, lpvBits, lpbi, uUsage)
return GDILib.GetDIBits(self.Handle,hbmp,uStartScan,cScanLines,lpvBits,lpbi,uUsage);
end,
StretchBlt = function(self, img, XDest, YDest,DestWidth,DestHeight)
XDest = XDest or 0
YDest = YDest or 0
DestWidth = DestWidth or img.Width
DestHeight = DestHeight or img.Height
-- Draw a pixel buffer
local bmInfo = BITMAPINFO();
bmInfo.bmiHeader.biWidth = img.Width;
bmInfo.bmiHeader.biHeight = img.Height;
bmInfo.bmiHeader.biPlanes = 1;
bmInfo.bmiHeader.biBitCount = img.BitsPerElement;
bmInfo.bmiHeader.biClrImportant = 0;
bmInfo.bmiHeader.biClrUsed = 0;
bmInfo.bmiHeader.biCompression = 0;
self:StretchDIBits(XDest,YDest,DestWidth,DestHeight,
0,0,img.Width, img.Height,
img.Data,
bmInfo);
end,
}
}
DeviceContext = ffi.metatype(DeviceContext, DeviceContext_mt)
ffi.cdef[[
typedef struct {
void * Handle;
BITMAP Bitmap;
unsigned char * Pixels;
} GDIBitmap;
]]
-- For Color
-- 0x00bbggrr
function RGB(byRed, byGreen, byBlue)
local acolor = lshift(byBlue,16) + lshift(byGreen,8) + byRed;
return acolor;
end
function GetRValue(c)
return band(c, 0xff)
end
function GetGValue(c)
return band(rshift(c,8), 0xff)
end
function GetBValue(c)
return band(rshift(c,16), 0xff)
end
--
-- This function answers the question:
-- Given:
-- We know the size of the byte boundary we want
-- to align to.
-- Question:
-- How many bytes need to be allocated to ensure we
-- will align to that boundary.
-- Discussion:
-- This comes up in cases where you're allocating a bitmap image
-- for example. It may be a 24-bit image, but you need to ensure
-- that each row can align to a 32-bit boundary. So, we need to
-- essentially scale up the number of bits to match the alignment.
--
local function GetAlignedByteCount(width, bitsperpixel, alignment)
local bytesperpixel = bitsperpixel / 8;
local stride = band((width * bytesperpixel + (alignment - 1)), bnot(alignment - 1));
return stride;
end
BITMAP = ffi.typeof("BITMAP")
GDIBitmap = nil
GDIBitmap_mt = {
__tostring = function(self) return string.format("GDIBitmap(0x%s)", tostring(self.Handle)) end,
__index = {
TypeName = "BITMAP",
Size = ffi.sizeof("GDIBitmap"),
Init = function(self, hdc)
local bmap = ffi.new("BITMAP[1]")
local bmapsize = ffi.sizeof("BITMAP");
C.GetObjectA(self.Handle, bmapsize, bmap)
self.Bitmap = bmap[0]
end,
Print = function(self)
print(string.format("Bitmap"))
print(string.format(" type: %d", self.Bitmap.bmType))
print(string.format(" width: %d", self.Bitmap.bmWidth))
print(string.format(" height: %d", self.Bitmap.bmHeight))
print(string.format(" Width Bytes: %d", self.Bitmap.bmWidthBytes))
print(string.format(" Planes: %d", self.Bitmap.bmPlanes))
print(string.format("BitsPerPixel: %d", self.Bitmap.bmBitsPixel));
end,
}
}
GDIBitmap = ffi.metatype("GDIBitmap", GDIBitmap_mt)
--
-- GDIDIBSection_mt
--
ffi.cdef[[
typedef struct {
void *Handle;
DeviceContext hDC;
int Width;
int Height;
int BitsPerPixel;
char * Pixels;
BITMAPINFO Info;
} GDIDIBSection;
]]
GDIDIBSection = nil
GDIDIBSection_mt = {
__index = {
TypeName = "GDIDIBSection",
Size = ffi.sizeof("GDIDIBSection"),
Init = function(self, width, height, bitsperpixel, alignment)
alignment = alignment or 2
bitsperpixel = bitsperpixel or 32
self.Width = width
self.Height = height
self.BitsPerPixel = bitsperpixel
-- Need to construct a BITMAPINFO structure
-- to describe the image we'll be creating
local bytesPerRow = GetAlignedByteCount(width, bitsperpixel, alignment)
local info = BITMAPINFO();
info.bmiHeader.biWidth = width
info.bmiHeader.biHeight = height
info.bmiHeader.biPlanes = 1
info.bmiHeader.biBitCount = bitsperpixel
info.bmiHeader.biSizeImage = bytesPerRow * height
info.bmiHeader.biClrImportant = 0
info.bmiHeader.biClrUsed = 0
info.bmiHeader.biCompression = 0 -- GDI32.BI_RGB
self.Info = info
-- Create the DIBSection, using the screen as
-- the source DC
local ddc = GDIContext():CreateForDefaultDisplay().Handle
local DIB_RGB_COLORS = 0
local pixelP = ffi.new("uint8_t *[1]")
self.Handle = GDILib.CreateDIBSection(ddc,
info,
DIB_RGB_COLORS,
pixelP,
nil,
0);
--print("GDIDIBSection Handle: ", self.Handle)
--self.Pixels = ffi.cast("Ppixel_BGRA_b", pixelP[0])
self.Pixels = pixelP[0]
-- Create a memory device context
-- and select the DIBSecton handle into it
self.hDC = GDIContext():CreateForMemory()
local selected = GDILib.SelectObject(self.hDC.Handle, self.Handle)
return self
end,
Print = function(self)
print("Bits Per Pixel: ", self.BitsPerPixel)
print("Size: ", self.Width, self.Height)
print("Pixels: ", self.Pixels)
end,
}
}
GDIDIBSection = ffi.metatype("GDIDIBSection", GDIDIBSection_mt)
--[[
Function Implementations
--]]
return GDI32