forked from jimp-dev/jimp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resize.js
374 lines (352 loc) · 16.7 KB
/
resize.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// JavaScript Image Resizer (c) 2012 - Grant Galitz
// Released to public domain 29 July 2013: https://github.com/grantgalitz/JS-Image-Resizer/issues/4
function Resize (widthOriginal, heightOriginal, targetWidth, targetHeight, blendAlpha, interpolationPass, resizeCallback) {
this.widthOriginal = Math.abs(Math.floor(widthOriginal) || 0);
this.heightOriginal = Math.abs(Math.floor(heightOriginal) || 0);
this.targetWidth = Math.abs(Math.floor(targetWidth) || 0);
this.targetHeight = Math.abs(Math.floor(targetHeight) || 0);
this.colorChannels = (blendAlpha) ? 4 : 3;
this.interpolationPass = Boolean(interpolationPass);
this.resizeCallback = (typeof resizeCallback === "function") ? resizeCallback : function () {};
this.targetWidthMultipliedByChannels = this.targetWidth * this.colorChannels;
this.originalWidthMultipliedByChannels = this.widthOriginal * this.colorChannels;
this.originalHeightMultipliedByChannels = this.heightOriginal * this.colorChannels;
this.widthPassResultSize = this.targetWidthMultipliedByChannels * this.heightOriginal;
this.finalResultSize = this.targetWidthMultipliedByChannels * this.targetHeight;
this.initialize();
}
Resize.prototype.initialize = function () {
// Perform some checks:
if (this.widthOriginal > 0 && this.heightOriginal > 0 && this.targetWidth > 0 && this.targetHeight > 0) {
this.configurePasses();
} else {
throw (new Error("Invalid settings specified for the resizer."));
}
}
Resize.prototype.configurePasses = function () {
if (this.widthOriginal === this.targetWidth) {
// Bypass the width resizer pass:
this.resizeWidth = this.bypassResizer;
} else {
// Setup the width resizer pass:
this.ratioWeightWidthPass = this.widthOriginal / this.targetWidth;
if (this.ratioWeightWidthPass < 1 && this.interpolationPass) {
this.initializeFirstPassBuffers(true);
this.resizeWidth = (this.colorChannels === 4) ? this.resizeWidthInterpolatedRGBA : this.resizeWidthInterpolatedRGB;
} else {
this.initializeFirstPassBuffers(false);
this.resizeWidth = (this.colorChannels === 4) ? this.resizeWidthRGBA : this.resizeWidthRGB;
}
}
if (this.heightOriginal === this.targetHeight) {
// Bypass the height resizer pass:
this.resizeHeight = this.bypassResizer;
} else {
// Setup the height resizer pass:
this.ratioWeightHeightPass = this.heightOriginal / this.targetHeight;
if (this.ratioWeightHeightPass < 1 && this.interpolationPass) {
this.initializeSecondPassBuffers(true);
this.resizeHeight = this.resizeHeightInterpolated;
} else {
this.initializeSecondPassBuffers(false);
this.resizeHeight = (this.colorChannels === 4) ? this.resizeHeightRGBA : this.resizeHeightRGB;
}
}
}
Resize.prototype._resizeWidthInterpolatedRGBChannels = function (buffer, fourthChannel) {
var channelsNum = fourthChannel ? 4 : 3;
var ratioWeight = this.ratioWeightWidthPass;
var weight = 0;
var finalOffset = 0;
var pixelOffset = 0;
var firstWeight = 0;
var secondWeight = 0;
var outputBuffer = this.widthBuffer;
// Handle for only one interpolation input being valid for start calculation:
for (var targetPosition = 0; weight < 1 / 3; targetPosition += channelsNum, weight += ratioWeight) {
for (finalOffset = targetPosition, pixelOffset = 0; finalOffset < this.widthPassResultSize; pixelOffset += this.originalWidthMultipliedByChannels, finalOffset += this.targetWidthMultipliedByChannels) {
outputBuffer[finalOffset] = buffer[pixelOffset];
outputBuffer[finalOffset + 1] = buffer[pixelOffset + 1];
outputBuffer[finalOffset + 2] = buffer[pixelOffset + 2];
if (fourthChannel)
outputBuffer[finalOffset + 3] = buffer[pixelOffset + 3];
}
}
// Adjust for overshoot of the last pass's counter:
weight -= 1 / 3;
for (var interpolationWidthSourceReadStop = this.widthOriginal - 1; weight < interpolationWidthSourceReadStop; targetPosition += channelsNum, weight += ratioWeight) {
// Calculate weightings:
secondWeight = weight % 1;
firstWeight = 1 - secondWeight;
// Interpolate:
for (finalOffset = targetPosition, pixelOffset = Math.floor(weight) * channelsNum;
finalOffset < this.widthPassResultSize;
pixelOffset += this.originalWidthMultipliedByChannels,
finalOffset += this.targetWidthMultipliedByChannels) {
outputBuffer[finalOffset + 0] = (buffer[pixelOffset + 0] * firstWeight) +
(buffer[pixelOffset + channelsNum + 0] * secondWeight);
outputBuffer[finalOffset + 1] = (buffer[pixelOffset + 1] * firstWeight) +
(buffer[pixelOffset + channelsNum + 1] * secondWeight);
outputBuffer[finalOffset + 2] = (buffer[pixelOffset + 2] * firstWeight) +
(buffer[pixelOffset + channelsNum + 2] * secondWeight);
if (fourthChannel)
outputBuffer[finalOffset + 3] = (buffer[pixelOffset + 3] * firstWeight) +
(buffer[pixelOffset + channelsNum + 3] * secondWeight);
}
}
// Handle for only one interpolation input being valid for end calculation:
for (interpolationWidthSourceReadStop = this.originalWidthMultipliedByChannels - channelsNum; targetPosition < this.targetWidthMultipliedByChannels; targetPosition += channelsNum) {
for (finalOffset = targetPosition, pixelOffset = interpolationWidthSourceReadStop; finalOffset < this.widthPassResultSize; pixelOffset += this.originalWidthMultipliedByChannels, finalOffset += this.targetWidthMultipliedByChannels) {
outputBuffer[finalOffset] = buffer[pixelOffset];
outputBuffer[finalOffset + 1] = buffer[pixelOffset + 1];
outputBuffer[finalOffset + 2] = buffer[pixelOffset + 2];
if (fourthChannel)
outputBuffer[finalOffset + 3] = buffer[pixelOffset + 3];
}
}
return outputBuffer;
}
Resize.prototype._resizeWidthRGBChannels = function (buffer, fourthChannel) {
var channelsNum = fourthChannel ? 4 : 3;
var ratioWeight = this.ratioWeightWidthPass;
var ratioWeightDivisor = 1 / ratioWeight;
var weight = 0;
var amountToNext = 0;
var actualPosition = 0;
var currentPosition = 0;
var line = 0;
var pixelOffset = 0;
var outputOffset = 0;
var nextLineOffsetOriginalWidth = this.originalWidthMultipliedByChannels - channelsNum + 1;
var nextLineOffsetTargetWidth = this.targetWidthMultipliedByChannels - channelsNum + 1;
var output = this.outputWidthWorkBench;
var outputBuffer = this.widthBuffer;
var trustworthyColorsCount = this.outputWidthWorkBenchOpaquePixelsCount;
var multiplier = 1;
var r = 0;
var g = 0;
var b = 0;
var a = 0;
do {
for (line = 0; line < this.originalHeightMultipliedByChannels;) {
output[line++] = 0;
output[line++] = 0;
output[line++] = 0;
if (fourthChannel) {
output[line++] = 0;
trustworthyColorsCount[line / channelsNum - 1] = 0;
}
}
weight = ratioWeight;
do {
amountToNext = 1 + actualPosition - currentPosition;
multiplier = Math.min(weight, amountToNext);
for (line = 0, pixelOffset = actualPosition; line < this.originalHeightMultipliedByChannels; pixelOffset += nextLineOffsetOriginalWidth) {
r = buffer[pixelOffset];
g = buffer[++pixelOffset];
b = buffer[++pixelOffset];
a = fourthChannel ? buffer[++pixelOffset] : 255;
// Ignore RGB values if pixel is completely transparent
output[line++] += (a ? r : 0) * multiplier;
output[line++] += (a ? g : 0) * multiplier;
output[line++] += (a ? b : 0) * multiplier;
if (fourthChannel) {
output[line++] += a * multiplier;
trustworthyColorsCount[line / channelsNum - 1] += (a ? multiplier : 0);
}
}
if (weight >= amountToNext) {
currentPosition = actualPosition = actualPosition + channelsNum;
weight -= amountToNext;
} else {
currentPosition += weight;
break;
}
} while (weight > 0 && actualPosition < this.originalWidthMultipliedByChannels);
for (line = 0, pixelOffset = outputOffset; line < this.originalHeightMultipliedByChannels; pixelOffset += nextLineOffsetTargetWidth) {
weight = fourthChannel ? trustworthyColorsCount[line / channelsNum] : 1;
multiplier = fourthChannel ? (weight ? 1 / weight : 0) : ratioWeightDivisor;
outputBuffer[pixelOffset] = output[line++] * multiplier;
outputBuffer[++pixelOffset] = output[line++] * multiplier;
outputBuffer[++pixelOffset] = output[line++] * multiplier;
if (fourthChannel)
outputBuffer[++pixelOffset] = output[line++] * ratioWeightDivisor;
}
outputOffset += channelsNum;
} while (outputOffset < this.targetWidthMultipliedByChannels);
return outputBuffer;
}
Resize.prototype._resizeHeightRGBChannels = function (buffer, fourthChannel) {
var ratioWeight = this.ratioWeightHeightPass;
var ratioWeightDivisor = 1 / ratioWeight;
var weight = 0;
var amountToNext = 0;
var actualPosition = 0;
var currentPosition = 0;
var pixelOffset = 0;
var outputOffset = 0;
var output = this.outputHeightWorkBench;
var outputBuffer = this.heightBuffer;
var trustworthyColorsCount = this.outputHeightWorkBenchOpaquePixelsCount;
var caret = 0;
var multiplier = 1;
var r = 0;
var g = 0;
var b = 0;
var a = 0;
do {
for (pixelOffset = 0; pixelOffset < this.targetWidthMultipliedByChannels;) {
output[pixelOffset++] = 0;
output[pixelOffset++] = 0;
output[pixelOffset++] = 0;
if (fourthChannel) {
output[pixelOffset++] = 0;
trustworthyColorsCount[pixelOffset / 4 - 1] = 0;
}
}
weight = ratioWeight;
do {
amountToNext = 1 + actualPosition - currentPosition;
multiplier = Math.min(weight, amountToNext);
caret = actualPosition;
for (pixelOffset = 0; pixelOffset < this.targetWidthMultipliedByChannels;) {
r = buffer[caret++];
g = buffer[caret++];
b = buffer[caret++];
a = fourthChannel ? buffer[caret++] : 255;
// Ignore RGB values if pixel is completely transparent
output[pixelOffset++] += (a ? r : 0) * multiplier;
output[pixelOffset++] += (a ? g : 0) * multiplier;
output[pixelOffset++] += (a ? b : 0) * multiplier;
if (fourthChannel) {
output[pixelOffset++] += a * multiplier;
trustworthyColorsCount[pixelOffset / 4 - 1] += (a ? multiplier : 0);
}
}
if (weight >= amountToNext) {
currentPosition = actualPosition = caret;
weight -= amountToNext;
} else {
currentPosition += weight;
break;
}
} while (weight > 0 && actualPosition < this.widthPassResultSize);
for (pixelOffset = 0; pixelOffset < this.targetWidthMultipliedByChannels;) {
weight = fourthChannel ? trustworthyColorsCount[pixelOffset / 4] : 1;
multiplier = fourthChannel ? (weight ? 1 / weight : 0) : ratioWeightDivisor;
outputBuffer[outputOffset++] = Math.round(output[pixelOffset++] * multiplier);
outputBuffer[outputOffset++] = Math.round(output[pixelOffset++] * multiplier);
outputBuffer[outputOffset++] = Math.round(output[pixelOffset++] * multiplier);
if (fourthChannel)
outputBuffer[outputOffset++] = Math.round(output[pixelOffset++] * ratioWeightDivisor);
}
} while (outputOffset < this.finalResultSize);
return outputBuffer;
}
Resize.prototype.resizeWidthInterpolatedRGB = function (buffer) {
return this._resizeWidthInterpolatedRGBChannels(buffer, false);
}
Resize.prototype.resizeWidthInterpolatedRGBA = function (buffer) {
return this._resizeWidthInterpolatedRGBChannels(buffer, true);
}
Resize.prototype.resizeWidthRGB = function (buffer) {
return this._resizeWidthRGBChannels(buffer, false);
}
Resize.prototype.resizeWidthRGBA = function (buffer) {
return this._resizeWidthRGBChannels(buffer, true);
}
Resize.prototype.resizeHeightInterpolated = function (buffer) {
var ratioWeight = this.ratioWeightHeightPass;
var weight = 0;
var finalOffset = 0;
var pixelOffset = 0;
var pixelOffsetAccumulated = 0;
var pixelOffsetAccumulated2 = 0;
var firstWeight = 0;
var secondWeight = 0;
var outputBuffer = this.heightBuffer;
// Handle for only one interpolation input being valid for start calculation:
for (; weight < 1 / 3; weight += ratioWeight) {
for (pixelOffset = 0; pixelOffset < this.targetWidthMultipliedByChannels;) {
outputBuffer[finalOffset++] = Math.round(buffer[pixelOffset++]);
}
}
// Adjust for overshoot of the last pass's counter:
weight -= 1 / 3;
for (var interpolationHeightSourceReadStop = this.heightOriginal - 1; weight < interpolationHeightSourceReadStop; weight += ratioWeight) {
// Calculate weightings:
secondWeight = weight % 1;
firstWeight = 1 - secondWeight;
// Interpolate:
pixelOffsetAccumulated = Math.floor(weight) * this.targetWidthMultipliedByChannels;
pixelOffsetAccumulated2 = pixelOffsetAccumulated + this.targetWidthMultipliedByChannels;
for (pixelOffset = 0; pixelOffset < this.targetWidthMultipliedByChannels; ++pixelOffset) {
outputBuffer[finalOffset++] = Math.round((buffer[pixelOffsetAccumulated++] * firstWeight) + (buffer[pixelOffsetAccumulated2++] * secondWeight));
}
}
// Handle for only one interpolation input being valid for end calculation:
while (finalOffset < this.finalResultSize) {
for (pixelOffset = 0, pixelOffsetAccumulated = interpolationHeightSourceReadStop * this.targetWidthMultipliedByChannels; pixelOffset < this.targetWidthMultipliedByChannels; ++pixelOffset) {
outputBuffer[finalOffset++] = Math.round(buffer[pixelOffsetAccumulated++]);
}
}
return outputBuffer;
}
Resize.prototype.resizeHeightRGB = function (buffer) {
return this._resizeHeightRGBChannels(buffer, false);
}
Resize.prototype.resizeHeightRGBA = function (buffer) {
return this._resizeHeightRGBChannels(buffer, true);
}
Resize.prototype.resize = function (buffer) {
this.resizeCallback(this.resizeHeight(this.resizeWidth(buffer)));
}
Resize.prototype.bypassResizer = function (buffer) {
// Just return the buffer passsed:
return buffer;
}
Resize.prototype.initializeFirstPassBuffers = function (BILINEARAlgo) {
// Initialize the internal width pass buffers:
this.widthBuffer = this.generateFloatBuffer(this.widthPassResultSize);
if (!BILINEARAlgo) {
this.outputWidthWorkBench = this.generateFloatBuffer(this.originalHeightMultipliedByChannels);
if (this.colorChannels > 3) {
this.outputWidthWorkBenchOpaquePixelsCount = this.generateFloat64Buffer(this.heightOriginal);
}
}
}
Resize.prototype.initializeSecondPassBuffers = function (BILINEARAlgo) {
// Initialize the internal height pass buffers:
this.heightBuffer = this.generateUint8Buffer(this.finalResultSize);
if (!BILINEARAlgo) {
this.outputHeightWorkBench = this.generateFloatBuffer(this.targetWidthMultipliedByChannels);
if (this.colorChannels > 3) {
this.outputHeightWorkBenchOpaquePixelsCount = this.generateFloat64Buffer(this.targetWidth);
}
}
}
Resize.prototype.generateFloatBuffer = function (bufferLength) {
// Generate a float32 typed array buffer:
try {
return new Float32Array(bufferLength);
} catch (error) {
return [];
}
}
Resize.prototype.generateFloat64Buffer = function (bufferLength) {
// Generate a float64 typed array buffer:
try {
return new Float64Array(bufferLength);
} catch (error) {
return [];
}
}
Resize.prototype.generateUint8Buffer = function (bufferLength) {
// Generate a uint8 typed array buffer:
try {
return new Uint8Array(bufferLength);
} catch (error) {
return [];
}
}
module.exports = Resize;