OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * A namespace for image filter utilities. | 6 * A namespace for image filter utilities. |
7 */ | 7 */ |
8 var filter = {}; | 8 var filter = {}; |
9 | 9 |
10 /** | 10 /** |
11 * Create a filter from name and options. | 11 * Create a filter from name and options. |
12 * | 12 * |
13 * @param {string} name Maps to a filter method name. | 13 * @param {string} name Maps to a filter method name. |
14 * @param {!Object} options A map of filter-specific options. | 14 * @param {!Object} options A map of filter-specific options. |
15 * @return {function(!ImageData,!ImageData,number,number)} created function. | 15 * @return {function(!ImageData,!ImageData,number,number)} created function. |
16 */ | 16 */ |
17 filter.create = function(name, options) { | 17 filter.create = function(name, options) { |
18 var filterFunc = filter[name](options); | 18 var filterFunc = filter[name](options); |
19 return function() { | 19 return function() { |
20 var time = Date.now(); | 20 var time = Date.now(); |
21 filterFunc.apply(null, arguments); | 21 filterFunc.apply(null, arguments); |
22 var dst = arguments[0]; | 22 var dst = arguments[0]; |
23 var mPixPerSec = dst.width * dst.height / 1000 / (Date.now() - time); | 23 var mPixPerSec = dst.width * dst.height / 1000 / (Date.now() - time); |
24 ImageUtil.trace.report(name, Math.round(mPixPerSec * 10) / 10 + 'Mps'); | 24 ImageUtil.trace.report(name, Math.round(mPixPerSec * 10) / 10 + 'Mps'); |
25 } | 25 } |
26 }; | 26 }; |
27 | 27 |
28 /** | 28 /** |
29 * Ensures argument is canvas. If it's not, creates new canvas and copy. | |
30 * | |
31 * @param {!HTMLCanvasElement|!HTMLImageElement} imgOrCanvas | |
32 * imageOrCanvas | |
33 * @return {!HTMLCanvasElement} canvas. | |
yawano
2016/01/20 06:23:09
nit: @private
ryoh
2016/01/21 03:19:55
Done.
| |
34 */ | |
35 filter.ensureCanvas_ = function(imgOrCanvas) { | |
36 if(imgOrCanvas.tagName === 'canvas') { | |
37 return assertInstanceof(imgOrCanvas, HTMLCanvasElement); | |
38 } | |
39 var canvas = assertInstanceof(document.createElement('canvas'), | |
40 HTMLCanvasElement); | |
41 canvas.width = imgOrCanvas.width; | |
42 canvas.height = imgOrCanvas.height; | |
43 var context = canvas.getContext('2d'); | |
44 context.drawImage(imgOrCanvas, 0, 0); | |
45 return canvas; | |
46 }; | |
47 | |
48 /** | |
29 * Apply a filter to a image by splitting it into strips. | 49 * Apply a filter to a image by splitting it into strips. |
30 * | 50 * |
31 * To be used with large images to avoid freezing up the UI. | 51 * To be used with large images to avoid freezing up the UI. |
32 * | 52 * |
33 * @param {!HTMLCanvasElement} dstCanvas Destination canvas. | 53 * @param {!HTMLCanvasElement} dstCanvas Destination canvas. |
34 * @param {!HTMLCanvasElement} srcCanvas Source canvas. | 54 * @param {!HTMLCanvasElement|!HTMLImageElement} srcImage Source image. |
35 * @param {function(!ImageData,!ImageData,number,number)} filterFunc Filter. | 55 * @param {function(!ImageData,!ImageData,number,number)} filterFunc Filter. |
36 * @param {function(number, number)} progressCallback Progress callback. | 56 * @param {function(number, number)} progressCallback Progress callback. |
37 * @param {number=} opt_maxPixelsPerStrip Pixel number to process at once. | 57 * @param {number=} opt_maxPixelsPerStrip Pixel number to process at once. |
38 */ | 58 */ |
39 filter.applyByStrips = function( | 59 filter.applyByStrips = function( |
40 dstCanvas, srcCanvas, filterFunc, progressCallback, opt_maxPixelsPerStrip) { | 60 dstCanvas, srcImage, filterFunc, progressCallback, opt_maxPixelsPerStrip) { |
41 // 1 Mpix is a reasonable default. | 61 // 1 Mpix is a reasonable default. |
42 var maxPixelsPerStrip = opt_maxPixelsPerStrip || 1000000; | 62 var maxPixelsPerStrip = opt_maxPixelsPerStrip || 1000000; |
43 | 63 |
44 var dstContext = dstCanvas.getContext('2d'); | 64 var dstContext = dstCanvas.getContext('2d'); |
45 var srcContext = srcCanvas.getContext('2d'); | 65 var srcContext = filter.ensureCanvas_(srcImage).getContext('2d'); |
46 var source = srcContext.getImageData(0, 0, srcCanvas.width, srcCanvas.height); | 66 var source = srcContext.getImageData(0, 0, srcImage.width, srcImage.height); |
47 | 67 |
48 var stripCount = Math.ceil(srcCanvas.width * srcCanvas.height / | 68 var stripCount = Math.ceil(srcImage.width * srcImage.height / |
49 maxPixelsPerStrip); | 69 maxPixelsPerStrip); |
50 | 70 |
51 var strip = srcContext.getImageData(0, 0, | 71 var strip = srcContext.getImageData(0, 0, |
52 srcCanvas.width, Math.ceil(srcCanvas.height / stripCount)); | 72 srcImage.width, Math.ceil(srcImage.height / stripCount)); |
53 | 73 |
54 var offset = 0; | 74 var offset = 0; |
55 | 75 |
56 function filterStrip() { | 76 function filterStrip() { |
57 // If the strip overlaps the bottom of the source image we cannot shrink it | 77 // If the strip overlaps the bottom of the source image we cannot shrink it |
58 // and we cannot fill it partially (since canvas.putImageData always draws | 78 // and we cannot fill it partially (since canvas.putImageData always draws |
59 // the entire buffer). | 79 // the entire buffer). |
60 // Instead we move the strip up several lines (converting those lines | 80 // Instead we move the strip up several lines (converting those lines |
61 // twice is a small price to pay). | 81 // twice is a small price to pay). |
62 if (offset > source.height - strip.height) { | 82 if (offset > source.height - strip.height) { |
(...skipping 547 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
610 /** | 630 /** |
611 * @param {{r: !Array<number>, g: !Array<number>, b: !Array<number>}} | 631 * @param {{r: !Array<number>, g: !Array<number>, b: !Array<number>}} |
612 * histogram | 632 * histogram |
613 * @return {boolean} True if the autofix would make a visible difference. | 633 * @return {boolean} True if the autofix would make a visible difference. |
614 */ | 634 */ |
615 filter.autofix.isApplicable = function(histogram) { | 635 filter.autofix.isApplicable = function(histogram) { |
616 return filter.autofix.needsStretching(histogram.r) || | 636 return filter.autofix.needsStretching(histogram.r) || |
617 filter.autofix.needsStretching(histogram.g) || | 637 filter.autofix.needsStretching(histogram.g) || |
618 filter.autofix.needsStretching(histogram.b); | 638 filter.autofix.needsStretching(histogram.b); |
619 }; | 639 }; |
OLD | NEW |