-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdrawingimage.go
107 lines (89 loc) · 2.46 KB
/
drawingimage.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
package main
import (
. "gopaint/reza"
"image"
"image/color"
"strconv"
"github.com/shahfarhadreza/go-gdiplus"
"github.com/fogleman/gg"
win "github.com/lxn/win"
)
type DrawingImage struct {
BGRA
hbitmap win.HBITMAP
memdc win.HDC
context *gdiplus.Graphics
context2 *gg.Context
context3 *Graphics
filepath string
sizeOnDisk int64 // in bytes
lastSaved string
}
func NewDrawingImage(width, height int) *DrawingImage {
this := &DrawingImage{}
this.Rect = image.Rect(0, 0, width, height)
this.Stride = 4 * width // 4 bytes per pixel
this.Pix = make([]uint8, this.Stride*height)
this.hbitmap, this.Pix = CreateHBitmap(width, height)
hScreenDC := win.GetDC(0)
this.memdc = win.CreateCompatibleDC(hScreenDC)
win.SelectObject(this.memdc, win.HGDIOBJ(this.hbitmap))
win.ReleaseDC(0, hScreenDC)
this.context = gdiplus.NewGraphicsFromHDC(gdiplus.HDC(this.memdc))
this.context.SetTextRenderingHint(gdiplus.TextRenderingHintAntiAlias)
//this.context.SetSmoothingMode(gdiplus.SmoothingModeAntiAlias)
rgba := (*image.RGBA)(&this.BGRA)
this.context2 = gg.NewContextForRGBA(rgba)
this.context3 = NewGraphics(this.memdc)
return this
}
func (image *DrawingImage) Dispose() {
image.context3 = nil
image.context2 = nil
if image.context != nil {
image.context.Dispose()
}
if image.memdc != 0 {
win.DeleteDC(image.memdc)
}
DeleteHBitmap(image.hbitmap)
image.hbitmap = 0
image.Pix = nil
}
func (di *DrawingImage) Width() int {
return di.Bounds().Dx()
}
func (di *DrawingImage) Height() int {
return di.Bounds().Dy()
}
func (di *DrawingImage) GetColorAt(x, y int) Color {
r, g, b, a := di.At(x, y).RGBA()
return Color{RGBA: color.RGBA{R: uint8(r), G: uint8(g), B: uint8(b), A: uint8(a)}}
}
func (image *DrawingImage) HasFilePath() bool {
return len(image.filepath) > 0
}
func (image *DrawingImage) Clear(color *gdiplus.Color) {
image.context.Clear(color)
}
func (image *DrawingImage) SizeOnDisk() (asString string, available bool) {
if image.sizeOnDisk > 0 {
if image.sizeOnDisk < 1024 {
return strconv.FormatInt(image.sizeOnDisk, 10) + " Bytes", true
}
kb := float64(image.sizeOnDisk) / 1024.0
if kb > 1024 {
mb := kb / 1024.0
return strconv.FormatFloat(mb, 'f', 1, 64) + "MB", true
} else {
return strconv.FormatFloat(kb, 'f', 1, 64) + "KB", true
}
}
return "", false
}
func (image *DrawingImage) LastSaved() (asString string, available bool) {
if len(image.lastSaved) > 0 {
return image.lastSaved, true
}
return "", false
}