Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/bkcore.coffee/ImageData.coffee
4626 views
1
###
2
Loads an image and gives access to pixel data.
3
4
@class bkcore.ImageData
5
@author Thibaut 'BKcore' Despoulain <http://bkcore.com>
6
###
7
class ImageData
8
9
###
10
Creates a new ImageData object
11
12
@param path string The path of the image
13
@param callback function A callback function to be called
14
once th eimage is loaded
15
###
16
constructor: (path, callback)->
17
18
@image = new Image
19
@pixels = null
20
@canvas = null
21
@loaded = false
22
23
@image.onload = ()=>
24
25
@canvas = document.createElement('canvas')
26
@canvas.width = @image.width
27
@canvas.height = @image.height
28
29
context = @canvas.getContext('2d')
30
context.drawImage(@image, 0, 0)
31
32
@pixels = context.getImageData(0,0, @canvas.width, @canvas.height)
33
@loaded = true
34
35
context = null
36
@canvas = null
37
@image = null
38
39
callback?.call(@)
40
41
@image.crossOrigin = "anonymous"
42
@image.src = path
43
44
###
45
Gets pixel RGBA data at given index
46
47
@param x int In pixels
48
@param y int In pixels
49
@return Object{r,g,b,a}
50
###
51
getPixel: (x, y)->
52
53
if !@pixels? or x < 0 or y < 0 or x >= @pixels.width or y >= @pixels.height
54
return {r: 0, g: 0, b:0, a:0}
55
56
i = (y*@pixels.width + x) * 4
57
58
return {
59
r: @pixels.data[i]
60
g: @pixels.data[i+1]
61
b: @pixels.data[i+2]
62
a: @pixels.data[i+3]
63
}
64
65
###
66
Gets pixel RGBA data at given float index using bilinear interpolation
67
68
@param x float In subpixels
69
@param y float In subpixels
70
@return Object{r,g,b,a}
71
###
72
getPixelBilinear: (fx, fy)->
73
74
x = Math.floor(fx)
75
y = Math.floor(fy)
76
rx = fx - x - .5
77
ry = fy - y - .5
78
ax = Math.abs(rx)
79
ay = Math.abs(ry)
80
dx = if rx < 0 then -1 else 1
81
dy = if ry < 0 then -1 else 1
82
83
c = @getPixel(x, y)
84
cx = @getPixel(x+dx, y)
85
cy = @getPixel(x, y+dy)
86
cxy = @getPixel(x+dx, y+dy)
87
88
cf1 = [
89
(1-ax) * c.r + ax * cx.r
90
(1-ax) * c.g + ax * cx.g
91
(1-ax) * c.b + ax * cx.b
92
(1-ax) * c.a + ax * cx.a
93
]
94
95
cf2 = [
96
(1-ax) * cy.r + ax * cxy.r
97
(1-ax) * cy.g + ax * cxy.g
98
(1-ax) * cy.b + ax * cxy.b
99
(1-ax) * cy.a + ax * cxy.a
100
]
101
102
return {
103
r: (1-ay) * cf1[0] + ay * cf2[0]
104
g: (1-ay) * cf1[1] + ay * cf2[1]
105
b: (1-ay) * cf1[2] + ay * cf2[2]
106
a: (1-ay) * cf1[3] + ay * cf2[3]
107
}
108
109
###
110
Gets pixel data at given index
111
as 3-bytes integer (for floating-point textures erzats, from RGB values)
112
113
@param x int In pixels
114
@param y int In pixels
115
@return int (R + G*255 + B*255*255)
116
###
117
getPixelF: (x, y)->
118
119
c = @getPixel(x, y)
120
return c.r + c.g * 255 + c.b * 255 * 255
121
122
###
123
Gets pixel data at given float index using bilinear interpolationas
124
as 3-bytes integer (for floating-point textures erzats, from RGB values)
125
126
@param x float In subpixels
127
@param y float In subpixels
128
@return Object{r,g,b,a}
129
###
130
getPixelFBilinear: (fx, fy)->
131
132
c = @getPixelBilinear(fx, fy)
133
return c.r + c.g * 255 + c.b * 255 * 255
134
135
###
136
Exports
137
@package bkcore
138
###
139
exports = exports ? @
140
exports.bkcore ||= {}
141
exports.bkcore.ImageData = ImageData
142