-
Notifications
You must be signed in to change notification settings - Fork 1
/
seam.js
435 lines (349 loc) · 10.9 KB
/
seam.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/**
* Seam carving -- refactored from
* http://labs.pimsworld.org/wp-content/uploads/2009/04/demo-content-aware-image-resizing-2/
*/
var SeamCarving = function(orgImgData, masks, out){
//original imaged
this.original = orgImgData;
//resized image
this.out = out;
this._currentWidth = this.original.width;
this._currentHeight = this.original.height;
this._currentdata = this.original.data; // current resize step RGBA
this._tmp = []; // current step gray + energy + sobel + seam
this._masks = masks.data
};
SeamCarving.prototype = {
_process: function(){
var tmp = this._tmp;
var l = this._currentdata.length;
//init tmp (black, full opacity)
for(var pixel = 0; pixel < l; pixel += 4){
tmp[pixel] = tmp[pixel + 1] = tmp[pixel + 2] = 0;
tmp[pixel + 3] = 255;
}
this._desaturate();
//poor idea, blows up contour detection
//this._equalize();
this._sobelAndEnergy();
},
/**
* Create grayscale image
*/
_desaturate: function(){
var pixel,
outImgData = this._tmp,
srcImgData = this._currentdata,
l = srcImgData.length;
for (pixel = 0; pixel < l; pixel += 4)
outImgData[pixel] = Math.round((299 * srcImgData[pixel] + 587 * srcImgData[pixel + 1] + 114 * srcImgData[pixel + 2]) / 1000);
},
/**
* build sobel and energy map
*/
_sobelAndEnergy: function(){
var width = this._currentWidth,
height = this.__currentHeight,
data = this._tmp,
masks = this._masks,
sobelPixelH, sobelPixelV, sobelResult,
topLeft, topMiddle, topRight, bottomLeft, bottomMiddle, bottomRight, leftMiddle, rightMiddle,
pixel,
minEnergy,
p,
l = data.length;
for (pixel = 0; pixel < l; pixel += 4) {
topLeft = pixel - (width + 1) * 4;
topMiddle = pixel - (width ) * 4;
topRight = pixel - (width - 1) * 4;
bottomLeft = pixel + (width - 1) * 4;
bottomMiddle = pixel + (width ) * 4;
bottomRight = pixel + (width + 1) * 4;
leftMiddle = pixel - 4;
rightMiddle = pixel + 4;
// simple discrete derivative
/*
sobelResult = Math.abs(-1 * (data[topMiddle] || 0) + 1 * (data[bottomMiddle] || 0))
+ Math.abs(-1 * (data[leftMiddle] || 0) + 1 * (data[rightMiddle] || 0));
sobelResult /= 40;
*/
// Vertical Sobel
sobelPixelV =
+ 1 * (data[topLeft] || 0)
+ 2 * (data[topMiddle] || 0)
+ 1 * (data[topRight] || 0)
- 1 * (data[bottomLeft] || 0)
- 2 * (data[bottomMiddle]|| 0)
- 1 * (data[bottomRight] || 0);
//Horizontal Sobel
sobelPixelH =
+ 1 * (data[topLeft] || 0)
+ 2 * (data[leftMiddle] || 0)
+ 1 * (data[bottomLeft] || 0)
- 1 * (data[topRight] || 0)
- 2 * (data[rightMiddle] || 0)
- 1 * (data[bottomRight] || 0);
sobelResult =
Math.round(Math.sqrt((sobelPixelV* sobelPixelV)+(sobelPixelH * sobelPixelH)) / 40);
sobelResult = Math.max(sobelResult, masks[pixel + 1]);
if(!this._ignoreRED)
sobelResult = Math.min(sobelResult, masks[pixel]);
minEnergy = Math.min(data[topLeft+1], data[topMiddle+1], data[topRight+1]);
minEnergy = isNaN(minEnergy) ? sobelResult : minEnergy + sobelResult;
//force the minimal seam to be in the red area
if(!this._ignoreRED)
minEnergy += masks[pixel];
// Should be done by the canvas implementation at the browser level
// http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-canvaspixelarray-set
// if (minEnergy > 255) minEnergy = 255;
// if (minEnergy < 0) minEnergy = 0;
data[pixel + 1] = Math.round(minEnergy);
data[pixel + 2] = sobelResult * 20; //random coef
}
},
// Copy intermediate image to dest
// 0 = grayscale,
// 1 = energy,
// 2 = Sobel,
// 3 = seam
debug: function(dest, mod){
var tmp = this._tmp,
ddata = dest.data,
l = tmp.length;
for(var pixel = 0; pixel < l; pixel += 4){
ddata[pixel] = ddata[pixel + 1] = ddata[pixel + 2] = tmp[pixel + mod];
ddata[pixel + 3] = tmp[pixel + 3];
}
return dest;
},
_seamMap:function(seam){
var data = seam.pixels,
tmp = this._tmp,
l = data.length;
for(var i = 0; i < l; i++)
tmp[data[i] + 3] = 0;
},
//restrict == take only seam crossing the red area
_getSeam: function(from, restrict) {
var CANAL = 1;
var minEnergyPosition,
tmpEnergy = Number.MAX_VALUE,
currentWidth = this._currentWidth,
currentHeight = this._currentHeight,
pseudoImgData = this._tmp,
masks = this._masks,
topLeftPosition, topMiddlePosition, topRightPosition,topLeftValue, topMiddleValue, topRightValue,
minEnergyValue,
pixel,
pixels = [],
x, y,
pos = 0,
hasRED = false;
var seamPixels = this._seamsPixels;
minEnergyPosition = from;
tmpEnergy = pseudoImgData[from + CANAL];
pixels[pos++] = minEnergyPosition;
for (y = currentHeight - 1; y > 0 ; y--) {
topLeftPosition = minEnergyPosition - currentWidth * 4 - 4;
topMiddlePosition = minEnergyPosition - currentWidth * 4;
topRightPosition = minEnergyPosition - currentWidth * 4 + 4;
//avoid overlapping as much as we can
topLeftValue = seamPixels[topLeftPosition + 3] > 0 ? pseudoImgData[topLeftPosition + CANAL] : Number.MAX_VALUE;
topMiddleValue = seamPixels[topMiddlePosition + 3] > 0 ? pseudoImgData[topMiddlePosition + CANAL] : Number.MAX_VALUE;
topRightValue = seamPixels[topRightPosition + 3] > 0 ? pseudoImgData[topRightPosition + CANAL] : Number.MAX_VALUE;
minEnergyValue = topMiddleValue;
minEnergyPosition = topMiddlePosition;
//Low energy + detect the image left and right borders
if (topLeftValue < minEnergyValue && ((topMiddlePosition % (currentWidth)) > 0)){
minEnergyValue = topMiddleValue;
minEnergyPosition = topLeftPosition;
}
if (topRightValue < minEnergyValue && (((topMiddlePosition + 4) % (currentWidth)) > 0)){
minEnergyValue = topRightValue;
minEnergyPosition = topRightPosition;
}
tmpEnergy += minEnergyValue;
pixels[pos++] = minEnergyPosition;
if(masks[minEnergyPosition] == 0)
hasRED = true;
// We assume that this function is called with low energy pixels first
if(minEnergyValue == Number.MAX_VALUE){
//ignore overlapping seams
//console.warn("overlapping seam");
return null;
}
//store seams position
seamPixels[minEnergyPosition + 3] = 0;
}
if(restrict && !hasRED)
return null;
return { 'pixels' : pixels, 'energy' : tmpEnergy };
},
_seamsList: function(restrict){
this._seamsPixels = [];
var currentL = this._currentdata.length;
for(var i = 0; i < currentL; i++)
this._seamsPixels[i] = 255;
var currentWidth = this._currentWidth,
currentHeight = this._currentHeight,
pixel = 0,
//sort bottom pixels by energy
sortedPixels = [],
tmp = this._tmp,
x = this._currentWidth,
pos = 0;
while(--x){ // for some reason we got better results if we start by the end of the image...
pixel = ((currentHeight - 1) * currentWidth + x) * 4;
sortedPixels[pos++] = {'index': pixel, 'energy': tmp[pixel + 1]}
}
sortedPixels.sort(function(p1, p2){
if(p1.energy < p2.energy) return -1;
else return 1;
});
var seams = [],
l = sortedPixels.length;
pos = 0;
for(var x = 0; x < l ; x++) {
pixel = sortedPixels[x];
var s = this._getSeam(pixel.index, restrict);
if(s != null)
seams[pos++] = s;
}
//sort seams by energy
return seams.sort(function(s1, s2){
if(s1.energy < s2.energy) return -1;
if(s1.energy)
return 1;
});
},
_sliceSeam: function(){
var srcImgDataData = this._currentdata,
srcSeamMapData = this._tmp,
srcMasks = this._masks,
resultMasks = [],
resultImageData = [];
var pos = 0, l = srcImgDataData.length;
for(var i = 0; i < l; i+=4){
if(srcSeamMapData[i + 3] > 0){
for(var j = 0; j < 4; j++){
resultImageData[pos] = srcImgDataData[i + j];
resultMasks[pos] = srcMasks[i + j];
pos++;
}
}
}
this._currentWidth--;
this._currentdata = resultImageData;
this._masks = resultMasks;
},
_addSeam: function(){
var srcImgData = this._currentdata,
srcSeamMapData = this._tmp,
srcMasks = this._masks,
resultMasks = [],
resultImageData = [],
left,
right,
m,
ind,
pos = 0,
l = srcImgData.length;
for(var i = 0; i < l; i+=4){
//We found a SEAM!
if(!(srcSeamMapData[i + 3] > 0)){
for(var j = 0; j < 3; j++){
ind = i + j;
left = (ind - 4) > 0 ? ind - 4 : ind;
right = (ind + 4) < srcImgData.length ? ind + 4 : ind;
m = (srcImgData[left] + srcImgData[right] + srcImgData[ind]) / 3;
resultImageData[pos] = m;
resultMasks[pos++] = (srcMasks[left] + srcMasks[right] + srcMasks[ind]) / 3;
}
resultImageData[pos++] = 255; //alpha
}
for(var j = 0; j < 4; j++){
resultImageData[pos] = srcImgData[i + j];
resultMasks[pos++] = srcMasks[i + j];
}
}
this._currentWidth = (resultImageData.length) / 4 / this._currentHeight;
this._currentdata = resultImageData;
this._masks = resultMasks;
},
_equalize: function(){
//http://en.wikipedia.org/wiki/Histogram_equalization
var tmp = this._tmp, pmap = [];
for(var i = 0; i < 256; i++) pmap[i] = 0;
for(var i = 0; i < tmp.length; i+=4)
pmap[Math.round(tmp[i])]++;
var cdfmap = [], current = 0;
for(var i = 0; i < pmap.length; i++)
cdfmap[i] = (current += pmap[i]);
var h = this._currentHeight,
w = this._currentWidth;
var min = Math.min.apply(Math,cdfmap);
for(var p = 0; p < tmp.length; p += 4){
var v = Math.round(tmp[p]);
var r = cdfmap[v] - min;
r /= (h * w - min);
r *= 255;
tmp[p] = r;
}
},
resize: function(){
//invert red
var masks = this._masks,
l = masks.length;
this._ignoreRED = true;
for(var i = 0; i < l; i+=4){
if(masks[i] > 0)
masks[i] = 0;
else
masks[i] = 255;
}
//smaller ?
while(this._currentdata.length > this.out.data.length){
this._process();
var l = this._seamsList();
this._seamMap(l[0]);
this._sliceSeam();
}
//bigger
while(this._currentWidth < this.out.width){
var diff = this.out.width - this._currentWidth;
this._process();
var l = this._seamsList();
for(var i = 0; (i < 35) && (i < diff) && (i < l.length); i++)
this._seamMap(l[i]);
this._addSeam();
}
//copy result
l = this.out.data.length;
for(var i = 0; i < l; i++)
this.out.data[i] = this._currentdata[i];
},
erase: function(){
var masks = this._masks,
currentX = 0,
maxX = 0,
minX = Number.MAX_VALUE,
width = this.original.width;
//invert mask and count slices to delete
for(var i = 0; i < masks.length; i+=4){
if(masks[i] > 0)
masks[i] = 0
else
masks[i] = 255;
}
while(true){
this._process();
var l = this._seamsList(true);
if(l.length < 1) break; //nothing else to deletes
this._seamMap(l[0]);
this._sliceSeam();
}
this._ignoreRED = true;
//bigger
this.resize();
}
}