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