-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.js
313 lines (250 loc) · 7.27 KB
/
core.js
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
'use strict'
// inspired by Jimp, imagejs
const glur = require('glur')
const blend = require('./lib/blend')
const MIME = require('./mime-types.json')
const unsharp = require(/* pica */'./lib/unsharp')
const resizeArray = require(/* pica */'./lib/resize-array')
const { hsvToRgb, hslToRgb, rgbToHsl, rgbToHsv } = require('./lib/color-conversion-algorithms')
const INTERPOLATION = {
lanczos3: resizeArray.lanczos3,
lanczos2: resizeArray.lanczos2,
hamming: resizeArray.hamming,
nearest: resizeArray.nearest
}
function clamp (n, min, max) {
if (n < min) n = min
else if (n > max) n = max
return n
}
function def (object, properties) {
const descriptors = {}
for (let name in properties) {
descriptors[name] = {
enumerable: true,
configurable: false,
writable: false,
value: properties[name]
}
}
return Object.defineProperties(object, descriptors)
}
const NO_DATA = {}
const AUTO = {}
class PixMap {
constructor (width, height) {
if (width === NO_DATA) return
width = Math.round(width | 0)
height = Math.round(height | 0)
if (width <= 0 || height <= 0) throw new Error('width and height must be > 0')
def(this, {
width: width,
height: height,
data: new Uint8ClampedArray(width * height * 4)
})
}
rect (x = 0, y = 0, w = this.width - x, h = this.height - y) {
const height = this.height
const width = this.width
const x2 = clamp(x + w, 0, width)
const y2 = clamp(y + h, 0, height)
x = clamp(x, 0, width)
y = clamp(y, 0, height)
if (x === x2 || y === y2) return null
const rect = [[], []]
if (x < x2) {
rect[0][0] = x
rect[1][0] = x2
} else if (x > x2) {
rect[0][0] = x2
rect[1][0] = x
}
if (y < y2) {
rect[0][1] = y
rect[1][1] = y2
} else if (y > y2) {
rect[0][1] = y2
rect[1][1] = y
}
return rect
}
getPixelOffset (x, y) {
const width = this.width
const height = this.height
if (x < 0 || x >= width || y < 0 || y >= height) return null
return (width * y + x) * 4
}
setPixel/* RGB(A) */ (x, y, src, offset = 0) {
const off = this.getPixelOffset(x, y)
if (off == null) return null
const data = this.data
const len = Math.min(src.length - offset, 4) // cutoff at 4
for (let i = 0; i < len; i++) data[off + i] = src[offset + i]
return this
}
setPixelHsv (x, y, src, offset = 0) {
const off = this.getPixelOffset(x, y)
if (off == null) return null
hsvToRgb(src, offset, this.data, off)
return this
}
setPixelHsl (x, y, src, offset = 0) {
const off = this.getPixelOffset(x, y)
if (off == null) return null
hslToRgb(src, offset, this.data, off)
return this
}
blendPixel (x, y, src, offset = 0, blendMode, amount) {
const off = this.getPixelOffset(x, y)
if (off == null) return null
blend(src, offset, this.data, off, blendMode || blend.MODES.normal, amount)
return this
}
scan (handle, dx, dy, w, h) {
const rect = this.rect(dx, dy, w, h)
if (!rect) return this
const [ [sx, sy], [ex, ey] ] = rect
const data = this.data
const width = this.width
let broken
for (let y = sy; y < ey; y++) {
for (let x = sx; x < ex; x++) {
const index = (width * y + x) * 4
const res = handle.call(this, x, y, index, data)
if (res === false) {
broken = true
break
}
}
if (broken) break
}
return this
}
crop (dx, dy, w, h) {
const rect = this.rect(dx, dy, w, h)
if (!rect) return this.clone()
const [ [sx, sy], [ex, ey] ] = rect
const width = ex - sx
const height = ey - sy
const pix = new PixMap(width, height)
const tw = this.width
const pd = pix.data
const td = this.data
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const tidx = (tw * (y + sy) + (x + sx)) * 4
const pidx = (width * y + x) * 4
for (let i = 0; i < 4; i++) pd[pidx + i] = td[tidx + i]
}
}
return pix
}
mask (pix, dx, dy) {
const pw = pix.width
const ph = pix.height
const w = this.width
const rect = this.rect(dx, dy, pw, ph)
if (!rect) return this
const [ [sx, sy], [ex, ey] ] = rect
const sdata = pix.data
const ddata = this.data
for (let y = sy; y < ey; y++) {
for (let x = sx; x < ex; x++) {
const delta1 = (pw * (y - sy) + (x - sx)) * 4
const delta2 = (w * y + x) * 4
const avg = (sdata[ delta1 ] + sdata[delta1 + 1] + sdata[delta1 + 2]) / 3
const f = avg / 255
ddata[delta2 + 3] *= f
}
}
return this
}
clone () {
const pix = new PixMap(NO_DATA)
def(pix, {
width: this.width,
height: this.height,
data: Uint8ClampedArray.from(this.data)
})
return pix
}
blend (pix, dx, dy, blendMode, amount) {
const pw = pix.width
const ph = pix.height
const w = this.width
const rect = this.rect(dx, dy, pw, ph)
if (!rect) return this
const [ [sx, sy], [ex, ey] ] = rect
if (!blendMode) blendMode = blend.MODES.normal
const src = pix.data
const dst = this.data
for (let y = sy; y < ey; y++) {
for (let x = sx; x < ex; x++) {
const ds = (pw * (y - sy) + (x - sx)) * 4
const dd = (w * y + x) * 4
blend(src, ds, dst, dd, blendMode, amount)
}
}
return this
}
copy (pix, dx, dy) {
const pw = pix.width
const ph = pix.height
const w = this.width
const rect = this.rect(dx, dy, pw, ph)
if (!rect) return this
const [ [sx, sy], [ex, ey] ] = rect
const src = pix.data
const dst = this.data
for (let y = sy; y < ey; y++) {
for (let x = sx; x < ex; x++) {
const ds = (pw * (y - sy) + (x - sx)) * 4
const dd = (w * y + x) * 4
for (let i = 0; i < 4; i++) dst[dd + i] = src[ds + i]
}
}
return this
}
blur (radius) {
glur(this.data, this.width, this.height, radius)
return this
}
unsharp (amount, radius, threshold) {
unsharp(this.data, this.width, this.height, amount, radius, threshold)
return this
}
resize (width, height, interpolation) {
if (width === AUTO) {
let ratio = this.width / this.height
width = Math.round(height * ratio)
} else if (height === AUTO) {
let ratio = this.height / this.width
height = Math.round(width * ratio)
}
const pix = new PixMap(width, height)
let interpolator = interpolation || INTERPOLATION.lanczos3
interpolator(this.data, pix.data, this.width, this.height, width, height)
return pix
}
}
PixMap.fromView = function (width, height, data) {
if (!ArrayBuffer.isView(data)) throw new Error(`${data} must be an ArrayBuffer view`)
if (data.length !== width * height * 4) throw new Error(`data must be in 32bpp`)
const pix = new PixMap(NO_DATA)
return def(pix, {
width: width,
height: height,
// create a clamped view from the same underlying ArrayBuffer
data: new Uint8ClampedArray(data.buffer, data.byteOffset, data.length)
})
}
// convenience
PixMap.hsvToRgb = hsvToRgb
PixMap.hslToRgb = hslToRgb
PixMap.rgbToHsl = rgbToHsl
PixMap.rgbToHsv = rgbToHsv
PixMap.MIME = MIME
PixMap.BLEND = blend.MODES
PixMap.INTERPOLATION = INTERPOLATION
PixMap.AUTO = AUTO
module.exports = PixMap