-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitmap.go
269 lines (214 loc) · 6.06 KB
/
bitmap.go
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
// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package walk
import (
"fmt"
"image"
"syscall"
"unsafe"
)
import . "github.com/lxn/go-winapi"
func withCompatibleDC(f func(hdc HDC) error) error {
hdc := CreateCompatibleDC(0)
if hdc == 0 {
return newError("CreateCompatibleDC failed")
}
defer DeleteDC(hdc)
return f(hdc)
}
func hPackedDIBFromHBITMAP(hBmp HBITMAP) (HGLOBAL, error) {
var dib DIBSECTION
if GetObject(HGDIOBJ(hBmp), unsafe.Sizeof(dib), unsafe.Pointer(&dib)) == 0 {
return 0, newError("GetObject failed")
}
bmihSize := uintptr(unsafe.Sizeof(dib.DsBmih))
pixelsSize := uintptr(
int32(dib.DsBmih.BiBitCount) * dib.DsBmih.BiWidth * dib.DsBmih.BiHeight)
totalSize := bmihSize + pixelsSize
hPackedDIB := GlobalAlloc(GHND, totalSize)
dest := GlobalLock(hPackedDIB)
defer GlobalUnlock(hPackedDIB)
src := unsafe.Pointer(&dib.DsBmih)
MoveMemory(dest, src, bmihSize)
dest = unsafe.Pointer(uintptr(dest) + bmihSize)
src = unsafe.Pointer(uintptr(src) + bmihSize)
MoveMemory(dest, src, pixelsSize)
return hPackedDIB, nil
}
func hBitmapFromImage(im image.Image) (HBITMAP, error) {
var bi BITMAPV5HEADER
bi.BiSize = uint32(unsafe.Sizeof(bi))
bi.BiWidth = int32(im.Bounds().Dx())
bi.BiHeight = -int32(im.Bounds().Dy())
bi.BiPlanes = 1
bi.BiBitCount = 32
bi.BiCompression = BI_BITFIELDS
// The following mask specification specifies a supported 32 BPP
// alpha format for Windows XP.
bi.BV4RedMask = 0x00FF0000
bi.BV4GreenMask = 0x0000FF00
bi.BV4BlueMask = 0x000000FF
bi.BV4AlphaMask = 0xFF000000
hdc := GetDC(0)
defer ReleaseDC(0, hdc)
var lpBits unsafe.Pointer
// Create the DIB section with an alpha channel.
hBitmap := CreateDIBSection(hdc, &bi.BITMAPINFOHEADER, DIB_RGB_COLORS, &lpBits, 0, 0)
switch hBitmap {
case 0, ERROR_INVALID_PARAMETER:
return 0, newError("CreateDIBSection failed")
}
// Fill the image
bitmap_array := (*[1 << 30]byte)(unsafe.Pointer(lpBits))
i := 0
for y := im.Bounds().Min.Y; y != im.Bounds().Max.Y; y++ {
for x := im.Bounds().Min.X; x != im.Bounds().Max.X; x++ {
r, g, b, a := im.At(x, y).RGBA()
bitmap_array[i+3] = byte(a >> 8)
bitmap_array[i+2] = byte(r >> 8)
bitmap_array[i+1] = byte(g >> 8)
bitmap_array[i+0] = byte(b >> 8)
i += 4
}
}
return hBitmap, nil
}
type Bitmap struct {
hBmp HBITMAP
hPackedDIB HGLOBAL
size Size
}
func newBitmapFromHBITMAP(hBmp HBITMAP) (bmp *Bitmap, err error) {
var dib DIBSECTION
if GetObject(HGDIOBJ(hBmp), unsafe.Sizeof(dib), unsafe.Pointer(&dib)) == 0 {
return nil, newError("GetObject failed")
}
bmih := &dib.DsBmih
bmihSize := uintptr(unsafe.Sizeof(*bmih))
pixelsSize := uintptr(int32(bmih.BiBitCount)*bmih.BiWidth*bmih.BiHeight) / 8
totalSize := uintptr(bmihSize + pixelsSize)
hPackedDIB := GlobalAlloc(GHND, totalSize)
dest := GlobalLock(hPackedDIB)
defer GlobalUnlock(hPackedDIB)
src := unsafe.Pointer(&dib.DsBmih)
MoveMemory(dest, src, bmihSize)
dest = unsafe.Pointer(uintptr(dest) + bmihSize)
src = dib.DsBm.BmBits
MoveMemory(dest, src, pixelsSize)
return &Bitmap{
hBmp: hBmp,
hPackedDIB: hPackedDIB,
size: Size{
int(bmih.BiWidth),
int(bmih.BiHeight),
},
}, nil
}
func NewBitmap(size Size) (bmp *Bitmap, err error) {
var hdr BITMAPINFOHEADER
hdr.BiSize = uint32(unsafe.Sizeof(hdr))
hdr.BiBitCount = 24
hdr.BiCompression = BI_RGB
hdr.BiPlanes = 1
hdr.BiWidth = int32(size.Width)
hdr.BiHeight = int32(size.Height)
err = withCompatibleDC(func(hdc HDC) error {
hBmp := CreateDIBSection(hdc, &hdr, DIB_RGB_COLORS, nil, 0, 0)
switch hBmp {
case 0, ERROR_INVALID_PARAMETER:
return newError("CreateDIBSection failed")
}
bmp, err = newBitmapFromHBITMAP(hBmp)
return err
})
return
}
func NewBitmapFromFile(filePath string) (*Bitmap, error) {
var si GdiplusStartupInput
si.GdiplusVersion = 1
if status := GdiplusStartup(&si, nil); status != Ok {
return nil, newError(fmt.Sprintf("GdiplusStartup failed with status '%s'", status))
}
defer GdiplusShutdown()
var gpBmp *GpBitmap
if status := GdipCreateBitmapFromFile(syscall.StringToUTF16Ptr(filePath), &gpBmp); status != Ok {
return nil, newError(fmt.Sprintf("GdipCreateBitmapFromFile failed with status '%s' for file '%s'", status, filePath))
}
defer GdipDisposeImage((*GpImage)(gpBmp))
var hBmp HBITMAP
if status := GdipCreateHBITMAPFromBitmap(gpBmp, &hBmp, 0); status != Ok {
return nil, newError(fmt.Sprintf("GdipCreateHBITMAPFromBitmap failed with status '%s' for file '%s'", status, filePath))
}
return newBitmapFromHBITMAP(hBmp)
}
func NewBitmapFromImage(im image.Image) (*Bitmap, error) {
hBmp, err := hBitmapFromImage(im)
if err != nil {
return nil, err
}
return newBitmapFromHBITMAP(hBmp)
}
func (bmp *Bitmap) withSelectedIntoMemDC(f func(hdcMem HDC) error) error {
return withCompatibleDC(func(hdcMem HDC) error {
hBmpOld := SelectObject(hdcMem, HGDIOBJ(bmp.hBmp))
if hBmpOld == 0 {
return newError("SelectObject failed")
}
defer SelectObject(hdcMem, hBmpOld)
return f(hdcMem)
})
}
func (bmp *Bitmap) draw(hdc HDC, location Point) error {
return bmp.withSelectedIntoMemDC(func(hdcMem HDC) error {
size := bmp.Size()
if !BitBlt(
hdc,
int32(location.X),
int32(location.Y),
int32(size.Width),
int32(size.Height),
hdcMem,
0,
0,
SRCCOPY) {
return lastError("BitBlt")
}
return nil
})
}
func (bmp *Bitmap) drawStretched(hdc HDC, bounds Rectangle) error {
return bmp.withSelectedIntoMemDC(func(hdcMem HDC) error {
size := bmp.Size()
if !StretchBlt(
hdc,
int32(bounds.X),
int32(bounds.Y),
int32(bounds.Width),
int32(bounds.Height),
hdcMem,
0,
0,
int32(size.Width),
int32(size.Height),
SRCCOPY) {
return newError("StretchBlt failed")
}
return nil
})
}
func (bmp *Bitmap) handle() HBITMAP {
return bmp.hBmp
}
func (bmp *Bitmap) Dispose() {
if bmp.hBmp != 0 {
DeleteObject(HGDIOBJ(bmp.hBmp))
GlobalUnlock(bmp.hPackedDIB)
GlobalFree(bmp.hPackedDIB)
bmp.hPackedDIB = 0
bmp.hBmp = 0
}
}
func (bmp *Bitmap) Size() Size {
return bmp.size
}