jimp 0.2.27 → 0.2.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +14 -0
- package/README.md +53 -10
- package/browser/.editorconfig +2 -0
- package/browser/README.md +2 -2
- package/browser/browserify-build.sh +2 -2
- package/browser/examples/dice.png +0 -0
- package/browser/examples/example1.html +3 -3
- package/browser/examples/jimp-worker.js +1 -1
- package/browser/examples/test.html +43 -0
- package/browser/lib/jimp.js +1 -3854
- package/browser/lib/jimp.min.js +2 -12
- package/index.js +372 -119
- package/jimp.d.ts +133 -0
- package/omggif.js +782 -0
- package/package.json +7 -4
- package/resize.js +120 -185
- package/resize2.js +15 -8
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jimp",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.28",
|
|
4
4
|
"description": "An image processing library written entirely in JavaScript (i.e. zero external or native dependencies).",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"types": "./jimp.d.ts",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"test": "./test/tests.sh",
|
|
8
9
|
"prepublish": "./browser/browserify-build.sh",
|
|
@@ -24,13 +25,14 @@
|
|
|
24
25
|
"license": "MIT",
|
|
25
26
|
"dependencies": {
|
|
26
27
|
"bignumber.js": "^2.1.0",
|
|
27
|
-
"bmp-js": "0.0.
|
|
28
|
+
"bmp-js": "0.0.3",
|
|
28
29
|
"es6-promise": "^3.0.2",
|
|
29
30
|
"exif-parser": "^0.1.9",
|
|
30
31
|
"file-type": "^3.1.0",
|
|
31
32
|
"jpeg-js": "^0.2.0",
|
|
32
33
|
"load-bmfont": "^1.2.3",
|
|
33
34
|
"mime": "^1.3.4",
|
|
35
|
+
"mkdirp": "0.5.1",
|
|
34
36
|
"pixelmatch": "^4.0.0",
|
|
35
37
|
"pngjs": "^3.0.0",
|
|
36
38
|
"read-chunk": "^1.0.1",
|
|
@@ -44,7 +46,7 @@
|
|
|
44
46
|
"babel-cli": "^6.0.0",
|
|
45
47
|
"babel-preset-es2015": "^6.0.14",
|
|
46
48
|
"babel-preset-stage-0": "^6.0.14",
|
|
47
|
-
"browserify": "^
|
|
49
|
+
"browserify": "^14.3.0",
|
|
48
50
|
"envify": "^3.4.0",
|
|
49
51
|
"express": "^4.13.4",
|
|
50
52
|
"uglify-js": "^2.6.1",
|
|
@@ -57,5 +59,6 @@
|
|
|
57
59
|
"bugs": {
|
|
58
60
|
"url": "https://github.com/oliver-moran/jimp/issues"
|
|
59
61
|
},
|
|
60
|
-
"tonicExampleFilename": "example.js"
|
|
62
|
+
"tonicExampleFilename": "example.js",
|
|
63
|
+
"browser": "browser/lib/jimp.js"
|
|
61
64
|
}
|
package/resize.js
CHANGED
|
@@ -57,58 +57,8 @@ Resize.prototype.configurePasses = function () {
|
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
Resize.prototype.
|
|
61
|
-
var
|
|
62
|
-
var ratioWeightDivisor = 1 / ratioWeight;
|
|
63
|
-
var weight = 0;
|
|
64
|
-
var amountToNext = 0;
|
|
65
|
-
var actualPosition = 0;
|
|
66
|
-
var currentPosition = 0;
|
|
67
|
-
var line = 0;
|
|
68
|
-
var pixelOffset = 0;
|
|
69
|
-
var outputOffset = 0;
|
|
70
|
-
var nextLineOffsetOriginalWidth = this.originalWidthMultipliedByChannels - 2;
|
|
71
|
-
var nextLineOffsetTargetWidth = this.targetWidthMultipliedByChannels - 2;
|
|
72
|
-
var output = this.outputWidthWorkBench;
|
|
73
|
-
var outputBuffer = this.widthBuffer;
|
|
74
|
-
do {
|
|
75
|
-
for (line = 0; line < this.originalHeightMultipliedByChannels;) {
|
|
76
|
-
output[line++] = 0;
|
|
77
|
-
output[line++] = 0;
|
|
78
|
-
output[line++] = 0;
|
|
79
|
-
}
|
|
80
|
-
weight = ratioWeight;
|
|
81
|
-
do {
|
|
82
|
-
amountToNext = 1 + actualPosition - currentPosition;
|
|
83
|
-
if (weight >= amountToNext) {
|
|
84
|
-
for (line = 0, pixelOffset = actualPosition; line < this.originalHeightMultipliedByChannels; pixelOffset += nextLineOffsetOriginalWidth) {
|
|
85
|
-
output[line++] += buffer[pixelOffset++] * amountToNext;
|
|
86
|
-
output[line++] += buffer[pixelOffset++] * amountToNext;
|
|
87
|
-
output[line++] += buffer[pixelOffset] * amountToNext;
|
|
88
|
-
}
|
|
89
|
-
currentPosition = actualPosition = actualPosition + 3;
|
|
90
|
-
weight -= amountToNext;
|
|
91
|
-
} else {
|
|
92
|
-
for (line = 0, pixelOffset = actualPosition; line < this.originalHeightMultipliedByChannels; pixelOffset += nextLineOffsetOriginalWidth) {
|
|
93
|
-
output[line++] += buffer[pixelOffset++] * weight;
|
|
94
|
-
output[line++] += buffer[pixelOffset++] * weight;
|
|
95
|
-
output[line++] += buffer[pixelOffset] * weight;
|
|
96
|
-
}
|
|
97
|
-
currentPosition += weight;
|
|
98
|
-
break;
|
|
99
|
-
}
|
|
100
|
-
} while (weight > 0 && actualPosition < this.originalWidthMultipliedByChannels);
|
|
101
|
-
for (line = 0, pixelOffset = outputOffset; line < this.originalHeightMultipliedByChannels; pixelOffset += nextLineOffsetTargetWidth) {
|
|
102
|
-
outputBuffer[pixelOffset++] = output[line++] * ratioWeightDivisor;
|
|
103
|
-
outputBuffer[pixelOffset++] = output[line++] * ratioWeightDivisor;
|
|
104
|
-
outputBuffer[pixelOffset] = output[line++] * ratioWeightDivisor;
|
|
105
|
-
}
|
|
106
|
-
outputOffset += 3;
|
|
107
|
-
} while (outputOffset < this.targetWidthMultipliedByChannels);
|
|
108
|
-
return outputBuffer;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
Resize.prototype.resizeWidthInterpolatedRGB = function (buffer) {
|
|
60
|
+
Resize.prototype._resizeWidthInterpolatedRGBChannels = function (buffer, fourthChannel) {
|
|
61
|
+
var channelsNum = fourthChannel ? 4 : 3;
|
|
112
62
|
var ratioWeight = this.ratioWeightWidthPass;
|
|
113
63
|
var weight = 0;
|
|
114
64
|
var finalOffset = 0;
|
|
@@ -117,38 +67,45 @@ Resize.prototype.resizeWidthInterpolatedRGB = function (buffer) {
|
|
|
117
67
|
var secondWeight = 0;
|
|
118
68
|
var outputBuffer = this.widthBuffer;
|
|
119
69
|
//Handle for only one interpolation input being valid for start calculation:
|
|
120
|
-
for (var targetPosition = 0; weight < 1 / 3; targetPosition +=
|
|
70
|
+
for (var targetPosition = 0; weight < 1 / 3; targetPosition += channelsNum, weight += ratioWeight) {
|
|
121
71
|
for (finalOffset = targetPosition, pixelOffset = 0; finalOffset < this.widthPassResultSize; pixelOffset += this.originalWidthMultipliedByChannels, finalOffset += this.targetWidthMultipliedByChannels) {
|
|
122
72
|
outputBuffer[finalOffset] = buffer[pixelOffset];
|
|
123
73
|
outputBuffer[finalOffset + 1] = buffer[pixelOffset + 1];
|
|
124
74
|
outputBuffer[finalOffset + 2] = buffer[pixelOffset + 2];
|
|
75
|
+
if (!fourthChannel) continue;
|
|
76
|
+
outputBuffer[finalOffset + 3] = buffer[pixelOffset + 3];
|
|
125
77
|
}
|
|
126
78
|
}
|
|
127
79
|
//Adjust for overshoot of the last pass's counter:
|
|
128
80
|
weight -= 1 / 3;
|
|
129
|
-
for (var interpolationWidthSourceReadStop = this.widthOriginal - 1; weight < interpolationWidthSourceReadStop; targetPosition +=
|
|
81
|
+
for (var interpolationWidthSourceReadStop = this.widthOriginal - 1; weight < interpolationWidthSourceReadStop; targetPosition += channelsNum, weight += ratioWeight) {
|
|
130
82
|
//Calculate weightings:
|
|
131
83
|
secondWeight = weight % 1;
|
|
132
84
|
firstWeight = 1 - secondWeight;
|
|
133
85
|
//Interpolate:
|
|
134
|
-
for (finalOffset = targetPosition, pixelOffset = Math.floor(weight) *
|
|
135
|
-
outputBuffer[finalOffset] = (buffer[pixelOffset] * firstWeight) + (buffer[pixelOffset +
|
|
136
|
-
outputBuffer[finalOffset + 1] = (buffer[pixelOffset + 1] * firstWeight) + (buffer[pixelOffset +
|
|
137
|
-
outputBuffer[finalOffset + 2] = (buffer[pixelOffset + 2] * firstWeight) + (buffer[pixelOffset +
|
|
86
|
+
for (finalOffset = targetPosition, pixelOffset = Math.floor(weight) * channelsNum; finalOffset < this.widthPassResultSize; pixelOffset += this.originalWidthMultipliedByChannels, finalOffset += this.targetWidthMultipliedByChannels) {
|
|
87
|
+
outputBuffer[finalOffset] = (buffer[pixelOffset] * firstWeight) + (buffer[pixelOffset + channelsNum] * secondWeight);
|
|
88
|
+
outputBuffer[finalOffset + 1] = (buffer[pixelOffset + 1] * firstWeight) + (buffer[pixelOffset + channelsNum + 1] * secondWeight);
|
|
89
|
+
outputBuffer[finalOffset + 2] = (buffer[pixelOffset + 2] * firstWeight) + (buffer[pixelOffset + channelsNum + 2] * secondWeight);
|
|
90
|
+
if (!fourthChannel) continue;
|
|
91
|
+
outputBuffer[finalOffset + 3] = (buffer[pixelOffset + 3] * firstWeight) + (buffer[pixelOffset + channelsNum + 3] * secondWeight);
|
|
138
92
|
}
|
|
139
93
|
}
|
|
140
94
|
//Handle for only one interpolation input being valid for end calculation:
|
|
141
|
-
for (interpolationWidthSourceReadStop = this.originalWidthMultipliedByChannels -
|
|
95
|
+
for (interpolationWidthSourceReadStop = this.originalWidthMultipliedByChannels - channelsNum; targetPosition < this.targetWidthMultipliedByChannels; targetPosition += channelsNum) {
|
|
142
96
|
for (finalOffset = targetPosition, pixelOffset = interpolationWidthSourceReadStop; finalOffset < this.widthPassResultSize; pixelOffset += this.originalWidthMultipliedByChannels, finalOffset += this.targetWidthMultipliedByChannels) {
|
|
143
97
|
outputBuffer[finalOffset] = buffer[pixelOffset];
|
|
144
98
|
outputBuffer[finalOffset + 1] = buffer[pixelOffset + 1];
|
|
145
99
|
outputBuffer[finalOffset + 2] = buffer[pixelOffset + 2];
|
|
100
|
+
if (!fourthChannel) continue;
|
|
101
|
+
outputBuffer[finalOffset + 3] = buffer[pixelOffset + 3];
|
|
146
102
|
}
|
|
147
103
|
}
|
|
148
104
|
return outputBuffer;
|
|
149
105
|
}
|
|
150
106
|
|
|
151
|
-
Resize.prototype.
|
|
107
|
+
Resize.prototype._resizeWidthRGBChannels = function (buffer, fourthChannel) {
|
|
108
|
+
var channelsNum = fourthChannel ? 4 : 3;
|
|
152
109
|
var ratioWeight = this.ratioWeightWidthPass;
|
|
153
110
|
var ratioWeightDivisor = 1 / ratioWeight;
|
|
154
111
|
var weight = 0;
|
|
@@ -158,95 +115,65 @@ Resize.prototype.resizeWidthRGBA = function (buffer) {
|
|
|
158
115
|
var line = 0;
|
|
159
116
|
var pixelOffset = 0;
|
|
160
117
|
var outputOffset = 0;
|
|
161
|
-
var nextLineOffsetOriginalWidth = this.originalWidthMultipliedByChannels -
|
|
162
|
-
var nextLineOffsetTargetWidth = this.targetWidthMultipliedByChannels -
|
|
118
|
+
var nextLineOffsetOriginalWidth = this.originalWidthMultipliedByChannels - channelsNum + 1;
|
|
119
|
+
var nextLineOffsetTargetWidth = this.targetWidthMultipliedByChannels - channelsNum + 1;
|
|
163
120
|
var output = this.outputWidthWorkBench;
|
|
164
121
|
var outputBuffer = this.widthBuffer;
|
|
122
|
+
var trustworthyColorsCount = this.outputWidthWorkBenchOpaquePixelsCount;
|
|
123
|
+
var multiplier = 1;
|
|
124
|
+
var r = 0;
|
|
125
|
+
var g = 0;
|
|
126
|
+
var b = 0;
|
|
127
|
+
var a = 0;
|
|
165
128
|
do {
|
|
166
129
|
for (line = 0; line < this.originalHeightMultipliedByChannels;) {
|
|
167
130
|
output[line++] = 0;
|
|
168
131
|
output[line++] = 0;
|
|
169
132
|
output[line++] = 0;
|
|
133
|
+
if (!fourthChannel) continue;
|
|
170
134
|
output[line++] = 0;
|
|
135
|
+
trustworthyColorsCount[line / channelsNum - 1] = 0;
|
|
171
136
|
}
|
|
172
137
|
weight = ratioWeight;
|
|
173
138
|
do {
|
|
174
139
|
amountToNext = 1 + actualPosition - currentPosition;
|
|
140
|
+
multiplier = Math.min(weight, amountToNext);
|
|
141
|
+
for (line = 0, pixelOffset = actualPosition; line < this.originalHeightMultipliedByChannels; pixelOffset += nextLineOffsetOriginalWidth) {
|
|
142
|
+
r = buffer[pixelOffset];
|
|
143
|
+
g = buffer[++pixelOffset];
|
|
144
|
+
b = buffer[++pixelOffset];
|
|
145
|
+
a = fourthChannel ? buffer[++pixelOffset] : 255;
|
|
146
|
+
// Ignore RGB values if pixel is completely transparent
|
|
147
|
+
output[line++] += (a ? r : 0) * multiplier;
|
|
148
|
+
output[line++] += (a ? g : 0) * multiplier;
|
|
149
|
+
output[line++] += (a ? b : 0) * multiplier;
|
|
150
|
+
if (!fourthChannel) continue;
|
|
151
|
+
output[line++] += a * multiplier;
|
|
152
|
+
trustworthyColorsCount[line / channelsNum - 1] += (a ? multiplier : 0);
|
|
153
|
+
}
|
|
175
154
|
if (weight >= amountToNext) {
|
|
176
|
-
|
|
177
|
-
output[line++] += buffer[pixelOffset++] * amountToNext;
|
|
178
|
-
output[line++] += buffer[pixelOffset++] * amountToNext;
|
|
179
|
-
output[line++] += buffer[pixelOffset++] * amountToNext;
|
|
180
|
-
output[line++] += buffer[pixelOffset] * amountToNext;
|
|
181
|
-
}
|
|
182
|
-
currentPosition = actualPosition = actualPosition + 4;
|
|
155
|
+
currentPosition = actualPosition = actualPosition + channelsNum;
|
|
183
156
|
weight -= amountToNext;
|
|
184
157
|
} else {
|
|
185
|
-
for (line = 0, pixelOffset = actualPosition; line < this.originalHeightMultipliedByChannels; pixelOffset += nextLineOffsetOriginalWidth) {
|
|
186
|
-
output[line++] += buffer[pixelOffset++] * weight;
|
|
187
|
-
output[line++] += buffer[pixelOffset++] * weight;
|
|
188
|
-
output[line++] += buffer[pixelOffset++] * weight;
|
|
189
|
-
output[line++] += buffer[pixelOffset] * weight;
|
|
190
|
-
}
|
|
191
158
|
currentPosition += weight;
|
|
192
159
|
break;
|
|
193
160
|
}
|
|
194
161
|
} while (weight > 0 && actualPosition < this.originalWidthMultipliedByChannels);
|
|
195
162
|
for (line = 0, pixelOffset = outputOffset; line < this.originalHeightMultipliedByChannels; pixelOffset += nextLineOffsetTargetWidth) {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
outputBuffer[pixelOffset
|
|
199
|
-
outputBuffer[pixelOffset] = output[line++] *
|
|
163
|
+
weight = fourthChannel ? trustworthyColorsCount[line / channelsNum] : 1;
|
|
164
|
+
multiplier = fourthChannel ? (weight ? 1 / weight : 0) : ratioWeightDivisor;
|
|
165
|
+
outputBuffer[pixelOffset] = output[line++] * multiplier;
|
|
166
|
+
outputBuffer[++pixelOffset] = output[line++] * multiplier;
|
|
167
|
+
outputBuffer[++pixelOffset] = output[line++] * multiplier;
|
|
168
|
+
if (!fourthChannel) continue;
|
|
169
|
+
outputBuffer[++pixelOffset] = output[line++] * ratioWeightDivisor;
|
|
200
170
|
}
|
|
201
|
-
outputOffset +=
|
|
171
|
+
outputOffset += channelsNum;
|
|
202
172
|
} while (outputOffset < this.targetWidthMultipliedByChannels);
|
|
203
173
|
return outputBuffer;
|
|
204
174
|
}
|
|
205
175
|
|
|
206
|
-
Resize.prototype.
|
|
207
|
-
var ratioWeight = this.ratioWeightWidthPass;
|
|
208
|
-
var weight = 0;
|
|
209
|
-
var finalOffset = 0;
|
|
210
|
-
var pixelOffset = 0;
|
|
211
|
-
var firstWeight = 0;
|
|
212
|
-
var secondWeight = 0;
|
|
213
|
-
var outputBuffer = this.widthBuffer;
|
|
214
|
-
//Handle for only one interpolation input being valid for start calculation:
|
|
215
|
-
for (var targetPosition = 0; weight < 1 / 3; targetPosition += 4, weight += ratioWeight) {
|
|
216
|
-
for (finalOffset = targetPosition, pixelOffset = 0; finalOffset < this.widthPassResultSize; pixelOffset += this.originalWidthMultipliedByChannels, finalOffset += this.targetWidthMultipliedByChannels) {
|
|
217
|
-
outputBuffer[finalOffset] = buffer[pixelOffset];
|
|
218
|
-
outputBuffer[finalOffset + 1] = buffer[pixelOffset + 1];
|
|
219
|
-
outputBuffer[finalOffset + 2] = buffer[pixelOffset + 2];
|
|
220
|
-
outputBuffer[finalOffset + 3] = buffer[pixelOffset + 3];
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
//Adjust for overshoot of the last pass's counter:
|
|
224
|
-
weight -= 1 / 3;
|
|
225
|
-
for (var interpolationWidthSourceReadStop = this.widthOriginal - 1; weight < interpolationWidthSourceReadStop; targetPosition += 4, weight += ratioWeight) {
|
|
226
|
-
//Calculate weightings:
|
|
227
|
-
secondWeight = weight % 1;
|
|
228
|
-
firstWeight = 1 - secondWeight;
|
|
229
|
-
//Interpolate:
|
|
230
|
-
for (finalOffset = targetPosition, pixelOffset = Math.floor(weight) * 4; finalOffset < this.widthPassResultSize; pixelOffset += this.originalWidthMultipliedByChannels, finalOffset += this.targetWidthMultipliedByChannels) {
|
|
231
|
-
outputBuffer[finalOffset] = (buffer[pixelOffset] * firstWeight) + (buffer[pixelOffset + 4] * secondWeight);
|
|
232
|
-
outputBuffer[finalOffset + 1] = (buffer[pixelOffset + 1] * firstWeight) + (buffer[pixelOffset + 5] * secondWeight);
|
|
233
|
-
outputBuffer[finalOffset + 2] = (buffer[pixelOffset + 2] * firstWeight) + (buffer[pixelOffset + 6] * secondWeight);
|
|
234
|
-
outputBuffer[finalOffset + 3] = (buffer[pixelOffset + 3] * firstWeight) + (buffer[pixelOffset + 7] * secondWeight);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
//Handle for only one interpolation input being valid for end calculation:
|
|
238
|
-
for (interpolationWidthSourceReadStop = this.originalWidthMultipliedByChannels - 4; targetPosition < this.targetWidthMultipliedByChannels; targetPosition += 4) {
|
|
239
|
-
for (finalOffset = targetPosition, pixelOffset = interpolationWidthSourceReadStop; finalOffset < this.widthPassResultSize; pixelOffset += this.originalWidthMultipliedByChannels, finalOffset += this.targetWidthMultipliedByChannels) {
|
|
240
|
-
outputBuffer[finalOffset] = buffer[pixelOffset];
|
|
241
|
-
outputBuffer[finalOffset + 1] = buffer[pixelOffset + 1];
|
|
242
|
-
outputBuffer[finalOffset + 2] = buffer[pixelOffset + 2];
|
|
243
|
-
outputBuffer[finalOffset + 3] = buffer[pixelOffset + 3];
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
return outputBuffer;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
Resize.prototype.resizeHeightRGB = function (buffer) {
|
|
176
|
+
Resize.prototype._resizeHeightRGBChannels = function(buffer, fourthChannel) {
|
|
250
177
|
var ratioWeight = this.ratioWeightHeightPass;
|
|
251
178
|
var ratioWeightDivisor = 1 / ratioWeight;
|
|
252
179
|
var weight = 0;
|
|
@@ -257,42 +184,78 @@ Resize.prototype.resizeHeightRGB = function (buffer) {
|
|
|
257
184
|
var outputOffset = 0;
|
|
258
185
|
var output = this.outputHeightWorkBench;
|
|
259
186
|
var outputBuffer = this.heightBuffer;
|
|
187
|
+
var trustworthyColorsCount = this.outputHeightWorkBenchOpaquePixelsCount;
|
|
188
|
+
var caret = 0;
|
|
189
|
+
var multiplier = 1;
|
|
190
|
+
var r = 0;
|
|
191
|
+
var g = 0;
|
|
192
|
+
var b = 0;
|
|
193
|
+
var a = 0;
|
|
260
194
|
do {
|
|
261
195
|
for (pixelOffset = 0; pixelOffset < this.targetWidthMultipliedByChannels;) {
|
|
262
196
|
output[pixelOffset++] = 0;
|
|
263
197
|
output[pixelOffset++] = 0;
|
|
264
198
|
output[pixelOffset++] = 0;
|
|
199
|
+
if (!fourthChannel) continue;
|
|
200
|
+
output[pixelOffset++] = 0;
|
|
201
|
+
trustworthyColorsCount[pixelOffset / 4 - 1] = 0;
|
|
265
202
|
}
|
|
266
203
|
weight = ratioWeight;
|
|
267
204
|
do {
|
|
268
205
|
amountToNext = 1 + actualPosition - currentPosition;
|
|
206
|
+
multiplier = Math.min(weight, amountToNext);
|
|
207
|
+
caret = actualPosition;
|
|
208
|
+
for (pixelOffset = 0; pixelOffset < this.targetWidthMultipliedByChannels;) {
|
|
209
|
+
r = buffer[caret++];
|
|
210
|
+
g = buffer[caret++];
|
|
211
|
+
b = buffer[caret++];
|
|
212
|
+
a = fourthChannel ? buffer[caret++] : 255;
|
|
213
|
+
// Ignore RGB values if pixel is completely transparent
|
|
214
|
+
output[pixelOffset++] += (a ? r : 0) * multiplier;
|
|
215
|
+
output[pixelOffset++] += (a ? g : 0) * multiplier;
|
|
216
|
+
output[pixelOffset++] += (a ? b : 0) * multiplier;
|
|
217
|
+
if (!fourthChannel) continue;
|
|
218
|
+
output[pixelOffset++] += a * multiplier;
|
|
219
|
+
trustworthyColorsCount[pixelOffset / 4 - 1] += (a ? multiplier : 0);
|
|
220
|
+
}
|
|
269
221
|
if (weight >= amountToNext) {
|
|
270
|
-
|
|
271
|
-
output[pixelOffset++] += buffer[actualPosition++] * amountToNext;
|
|
272
|
-
output[pixelOffset++] += buffer[actualPosition++] * amountToNext;
|
|
273
|
-
output[pixelOffset++] += buffer[actualPosition++] * amountToNext;
|
|
274
|
-
}
|
|
275
|
-
currentPosition = actualPosition;
|
|
222
|
+
currentPosition = actualPosition = caret;
|
|
276
223
|
weight -= amountToNext;
|
|
277
224
|
} else {
|
|
278
|
-
for (pixelOffset = 0, amountToNext = actualPosition; pixelOffset < this.targetWidthMultipliedByChannels;) {
|
|
279
|
-
output[pixelOffset++] += buffer[amountToNext++] * weight;
|
|
280
|
-
output[pixelOffset++] += buffer[amountToNext++] * weight;
|
|
281
|
-
output[pixelOffset++] += buffer[amountToNext++] * weight;
|
|
282
|
-
}
|
|
283
225
|
currentPosition += weight;
|
|
284
226
|
break;
|
|
285
227
|
}
|
|
286
228
|
} while (weight > 0 && actualPosition < this.widthPassResultSize);
|
|
287
229
|
for (pixelOffset = 0; pixelOffset < this.targetWidthMultipliedByChannels;) {
|
|
288
|
-
|
|
289
|
-
|
|
230
|
+
weight = fourthChannel ? trustworthyColorsCount[pixelOffset / 4] : 1;
|
|
231
|
+
multiplier = fourthChannel ? (weight ? 1 / weight : 0) : ratioWeightDivisor;
|
|
232
|
+
outputBuffer[outputOffset++] = Math.round(output[pixelOffset++] * multiplier);
|
|
233
|
+
outputBuffer[outputOffset++] = Math.round(output[pixelOffset++] * multiplier);
|
|
234
|
+
outputBuffer[outputOffset++] = Math.round(output[pixelOffset++] * multiplier);
|
|
235
|
+
if (!fourthChannel) continue;
|
|
290
236
|
outputBuffer[outputOffset++] = Math.round(output[pixelOffset++] * ratioWeightDivisor);
|
|
291
237
|
}
|
|
292
238
|
} while (outputOffset < this.finalResultSize);
|
|
239
|
+
|
|
293
240
|
return outputBuffer;
|
|
294
241
|
}
|
|
295
242
|
|
|
243
|
+
Resize.prototype.resizeWidthInterpolatedRGB = function (buffer) {
|
|
244
|
+
return this._resizeWidthInterpolatedRGBChannels(buffer, false);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
Resize.prototype.resizeWidthInterpolatedRGBA = function (buffer) {
|
|
248
|
+
return this._resizeWidthInterpolatedRGBChannels(buffer, true);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
Resize.prototype.resizeWidthRGB = function (buffer) {
|
|
252
|
+
return this._resizeWidthRGBChannels(buffer, false);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
Resize.prototype.resizeWidthRGBA = function (buffer) {
|
|
256
|
+
return this._resizeWidthRGBChannels(buffer, true);
|
|
257
|
+
}
|
|
258
|
+
|
|
296
259
|
Resize.prototype.resizeHeightInterpolated = function (buffer) {
|
|
297
260
|
var ratioWeight = this.ratioWeightHeightPass;
|
|
298
261
|
var weight = 0;
|
|
@@ -331,55 +294,12 @@ Resize.prototype.resizeHeightInterpolated = function (buffer) {
|
|
|
331
294
|
return outputBuffer;
|
|
332
295
|
}
|
|
333
296
|
|
|
297
|
+
Resize.prototype.resizeHeightRGB = function (buffer) {
|
|
298
|
+
return this._resizeHeightRGBChannels(buffer, false);
|
|
299
|
+
}
|
|
300
|
+
|
|
334
301
|
Resize.prototype.resizeHeightRGBA = function (buffer) {
|
|
335
|
-
|
|
336
|
-
var ratioWeightDivisor = 1 / ratioWeight;
|
|
337
|
-
var weight = 0;
|
|
338
|
-
var amountToNext = 0;
|
|
339
|
-
var actualPosition = 0;
|
|
340
|
-
var currentPosition = 0;
|
|
341
|
-
var pixelOffset = 0;
|
|
342
|
-
var outputOffset = 0;
|
|
343
|
-
var output = this.outputHeightWorkBench;
|
|
344
|
-
var outputBuffer = this.heightBuffer;
|
|
345
|
-
do {
|
|
346
|
-
for (pixelOffset = 0; pixelOffset < this.targetWidthMultipliedByChannels;) {
|
|
347
|
-
output[pixelOffset++] = 0;
|
|
348
|
-
output[pixelOffset++] = 0;
|
|
349
|
-
output[pixelOffset++] = 0;
|
|
350
|
-
output[pixelOffset++] = 0;
|
|
351
|
-
}
|
|
352
|
-
weight = ratioWeight;
|
|
353
|
-
do {
|
|
354
|
-
amountToNext = 1 + actualPosition - currentPosition;
|
|
355
|
-
if (weight >= amountToNext) {
|
|
356
|
-
for (pixelOffset = 0; pixelOffset < this.targetWidthMultipliedByChannels;) {
|
|
357
|
-
output[pixelOffset++] += buffer[actualPosition++] * amountToNext;
|
|
358
|
-
output[pixelOffset++] += buffer[actualPosition++] * amountToNext;
|
|
359
|
-
output[pixelOffset++] += buffer[actualPosition++] * amountToNext;
|
|
360
|
-
output[pixelOffset++] += buffer[actualPosition++] * amountToNext;
|
|
361
|
-
}
|
|
362
|
-
currentPosition = actualPosition;
|
|
363
|
-
weight -= amountToNext;
|
|
364
|
-
} else {
|
|
365
|
-
for (pixelOffset = 0, amountToNext = actualPosition; pixelOffset < this.targetWidthMultipliedByChannels;) {
|
|
366
|
-
output[pixelOffset++] += buffer[amountToNext++] * weight;
|
|
367
|
-
output[pixelOffset++] += buffer[amountToNext++] * weight;
|
|
368
|
-
output[pixelOffset++] += buffer[amountToNext++] * weight;
|
|
369
|
-
output[pixelOffset++] += buffer[amountToNext++] * weight;
|
|
370
|
-
}
|
|
371
|
-
currentPosition += weight;
|
|
372
|
-
break;
|
|
373
|
-
}
|
|
374
|
-
} while (weight > 0 && actualPosition < this.widthPassResultSize);
|
|
375
|
-
for (pixelOffset = 0; pixelOffset < this.targetWidthMultipliedByChannels;) {
|
|
376
|
-
outputBuffer[outputOffset++] = Math.round(output[pixelOffset++] * ratioWeightDivisor);
|
|
377
|
-
outputBuffer[outputOffset++] = Math.round(output[pixelOffset++] * ratioWeightDivisor);
|
|
378
|
-
outputBuffer[outputOffset++] = Math.round(output[pixelOffset++] * ratioWeightDivisor);
|
|
379
|
-
outputBuffer[outputOffset++] = Math.round(output[pixelOffset++] * ratioWeightDivisor);
|
|
380
|
-
}
|
|
381
|
-
} while (outputOffset < this.finalResultSize);
|
|
382
|
-
return outputBuffer;
|
|
302
|
+
return this._resizeHeightRGBChannels(buffer, true);
|
|
383
303
|
}
|
|
384
304
|
|
|
385
305
|
Resize.prototype.resize = function (buffer) {
|
|
@@ -396,6 +316,9 @@ Resize.prototype.initializeFirstPassBuffers = function (BILINEARAlgo) {
|
|
|
396
316
|
this.widthBuffer = this.generateFloatBuffer(this.widthPassResultSize);
|
|
397
317
|
if (!BILINEARAlgo) {
|
|
398
318
|
this.outputWidthWorkBench = this.generateFloatBuffer(this.originalHeightMultipliedByChannels);
|
|
319
|
+
if (this.colorChannels > 3) {
|
|
320
|
+
this.outputWidthWorkBenchOpaquePixelsCount = this.generateFloat64Buffer(this.heightOriginal);
|
|
321
|
+
}
|
|
399
322
|
}
|
|
400
323
|
}
|
|
401
324
|
|
|
@@ -404,6 +327,9 @@ Resize.prototype.initializeSecondPassBuffers = function (BILINEARAlgo) {
|
|
|
404
327
|
this.heightBuffer = this.generateUint8Buffer(this.finalResultSize);
|
|
405
328
|
if (!BILINEARAlgo) {
|
|
406
329
|
this.outputHeightWorkBench = this.generateFloatBuffer(this.targetWidthMultipliedByChannels);
|
|
330
|
+
if (this.colorChannels > 3) {
|
|
331
|
+
this.outputHeightWorkBenchOpaquePixelsCount = this.generateFloat64Buffer(this.targetWidth);
|
|
332
|
+
}
|
|
407
333
|
}
|
|
408
334
|
}
|
|
409
335
|
|
|
@@ -416,6 +342,15 @@ Resize.prototype.generateFloatBuffer = function (bufferLength) {
|
|
|
416
342
|
}
|
|
417
343
|
}
|
|
418
344
|
|
|
345
|
+
Resize.prototype.generateFloat64Buffer = function (bufferLength) {
|
|
346
|
+
//Generate a float64 typed array buffer:
|
|
347
|
+
try {
|
|
348
|
+
return new Float64Array(bufferLength);
|
|
349
|
+
} catch (error) {
|
|
350
|
+
return [];
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
419
354
|
Resize.prototype.generateUint8Buffer = function (bufferLength) {
|
|
420
355
|
//Generate a uint8 typed array buffer:
|
|
421
356
|
try {
|
package/resize2.js
CHANGED
|
@@ -201,22 +201,29 @@ module.exports = {
|
|
|
201
201
|
var g = 0;
|
|
202
202
|
var b = 0;
|
|
203
203
|
var a = 0;
|
|
204
|
+
var realColors = 0;
|
|
204
205
|
for (var y = 0; y < hM; y++) {
|
|
205
206
|
var yPos = i * hM + y;
|
|
206
207
|
for (var x = 0; x < wM; x++) {
|
|
207
208
|
var xPos = j * wM + x;
|
|
208
209
|
var xyPos = (yPos * wDst2 + xPos) * 4;
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
210
|
+
var pixelAplha = buf2[xyPos+3];
|
|
211
|
+
|
|
212
|
+
if (pixelAplha) {
|
|
213
|
+
r += buf2[xyPos];
|
|
214
|
+
g += buf2[xyPos+1];
|
|
215
|
+
b += buf2[xyPos+2];
|
|
216
|
+
realColors++;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
a += pixelAplha;
|
|
213
220
|
}
|
|
214
221
|
}
|
|
215
222
|
|
|
216
223
|
var pos = (i*wDst + j) * 4;
|
|
217
|
-
bufDst[pos] = Math.round(r /
|
|
218
|
-
bufDst[pos+1] = Math.round(g /
|
|
219
|
-
bufDst[pos+2] = Math.round(b /
|
|
224
|
+
bufDst[pos] = realColors ? Math.round(r / realColors) : 0;
|
|
225
|
+
bufDst[pos+1] = realColors ? Math.round(g / realColors) : 0;
|
|
226
|
+
bufDst[pos+2] = realColors ? Math.round(b / realColors) : 0;
|
|
220
227
|
bufDst[pos+3] = Math.round(a / m);
|
|
221
228
|
}
|
|
222
229
|
}
|
|
@@ -274,4 +281,4 @@ module.exports = {
|
|
|
274
281
|
}
|
|
275
282
|
return this._interpolate2D(src, dst, options, interpolateBezier);
|
|
276
283
|
}
|
|
277
|
-
}
|
|
284
|
+
}
|