-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathimage.go
69 lines (60 loc) · 1.76 KB
/
image.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
// Copyright 2020 Frederik Zipp. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// The API doc comments are based on the MDN Web Docs for the [Canvas API]
// by Mozilla Contributors and are licensed under [CC-BY-SA 2.5].
//
// [Canvas API]: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
// [CC-BY-SA 2.5]: https://creativecommons.org/licenses/by-sa/2.5/
package canvas
import (
"image"
"image/draw"
)
// ImageData represents the underlying pixel data of an image. It is
// created using the Context.CreateImageData and Context.GetImageData methods.
// It can also be used to set a part of the canvas by using
// Context.PutImageData, Context.PutImageDataDirty, Context.DrawImage and
// Context.DrawImageScaled.
//
// The image data should be released with the Release method when it is no
// longer needed.
type ImageData struct {
id uint32
ctx *Context
width int
height int
released bool
}
// Width returns the actual width, in pixels, of the image.
func (m *ImageData) Width() int {
return m.width
}
// Height returns the actual height, in pixels, of the image.
func (m *ImageData) Height() int {
return m.height
}
// Release releases the image data on the client side.
func (m *ImageData) Release() {
if m.released {
return
}
m.ctx.buf.addByte(bReleaseImageData)
m.ctx.buf.addUint32(m.id)
m.released = true
}
func (m *ImageData) checkUseAfterRelease() {
if m.released {
panic("ImageData: use after release")
}
}
func ensureRGBA(img image.Image) *image.RGBA {
switch im := img.(type) {
case *image.RGBA:
return im
default:
rgba := image.NewRGBA(im.Bounds())
draw.Draw(rgba, im.Bounds(), im, image.Pt(0, 0), draw.Src)
return rgba
}
}