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
3 // found in the LICENSE file.
8 * The base class for simple filters that only modify the image content
9 * but do not modify the image dimensions.
11 * @extends ImageEditor.Mode
13 ImageEditor.Mode.Adjust = function() {
14 ImageEditor.Mode.apply(this, arguments);
15 this.implicitCommit = true;
16 this.doneMessage_ = null;
17 this.viewportGeneration_ = 0;
20 ImageEditor.Mode.Adjust.prototype = {__proto__: ImageEditor.Mode.prototype};
23 ImageEditor.Mode.Adjust.prototype.getCommand = function() {
24 if (!this.filter_) return null;
26 return new Command.Filter(this.name, this.filter_, this.doneMessage_);
30 ImageEditor.Mode.Adjust.prototype.cleanUpUI = function() {
31 ImageEditor.Mode.prototype.cleanUpUI.apply(this, arguments);
38 ImageEditor.Mode.Adjust.prototype.hidePreview = function() {
40 this.canvas_.parentNode.removeChild(this.canvas_);
48 ImageEditor.Mode.Adjust.prototype.cleanUpCaches = function() {
50 this.previewImageData_ = null;
56 ImageEditor.Mode.Adjust.prototype.reset = function() {
57 ImageEditor.Mode.prototype.reset.call(this);
64 * @param {Object} options // TODO(JSDOC).
66 ImageEditor.Mode.Adjust.prototype.update = function(options) {
67 ImageEditor.Mode.prototype.update.apply(this, arguments);
69 // We assume filter names are used in the UI directly.
70 // This will have to change with i18n.
71 this.filter_ = this.createFilter(options);
72 this.updatePreviewImage();
73 ImageUtil.trace.resetTimer('preview');
74 this.filter_(this.previewImageData_, this.originalImageData, 0, 0);
75 ImageUtil.trace.reportTimer('preview');
76 this.canvas_.getContext('2d').putImageData(
77 this.previewImageData_, 0, 0);
81 * Copy the source image data for the preview.
82 * Use the cached copy if the viewport has not changed.
84 ImageEditor.Mode.Adjust.prototype.updatePreviewImage = function() {
85 if (!this.previewImageData_ ||
86 this.viewportGeneration_ != this.getViewport().getCacheGeneration()) {
87 this.viewportGeneration_ = this.getViewport().getCacheGeneration();
90 this.canvas_ = this.getImageView().createOverlayCanvas();
93 this.getImageView().setupDeviceBuffer(this.canvas_);
95 this.originalImageData = this.getImageView().copyScreenImageData();
96 this.previewImageData_ = this.getImageView().copyScreenImageData();
106 * @param {Object} options // TODO(JSDOC).
107 * @return {function(ImageData,ImageData,number,number)} Created function.
109 ImageEditor.Mode.Adjust.prototype.createFilter = function(options) {
110 return filter.create(this.name, options);
114 * A base class for color filters that are scale independent.
117 ImageEditor.Mode.ColorFilter = function() {
118 ImageEditor.Mode.Adjust.apply(this, arguments);
121 ImageEditor.Mode.ColorFilter.prototype =
122 {__proto__: ImageEditor.Mode.Adjust.prototype};
126 * @return {{r: Array.<number>, g: Array.<number>, b: Array.<number>}}
129 ImageEditor.Mode.ColorFilter.prototype.getHistogram = function() {
130 return filter.getHistogram(this.getImageView().getThumbnail());
134 * Exposure/contrast filter.
137 ImageEditor.Mode.Exposure = function() {
138 ImageEditor.Mode.ColorFilter.call(this, 'exposure', 'GALLERY_EXPOSURE');
141 ImageEditor.Mode.Exposure.prototype =
142 {__proto__: ImageEditor.Mode.ColorFilter.prototype};
146 * @param {ImageEditor.Toolbar} toolbar The toolbar to populate.
148 ImageEditor.Mode.Exposure.prototype.createTools = function(toolbar) {
149 toolbar.addRange('brightness', 'GALLERY_BRIGHTNESS', -1, 0, 1, 100);
150 toolbar.addRange('contrast', 'GALLERY_CONTRAST', -1, 0, 1, 100);
157 ImageEditor.Mode.Autofix = function() {
158 ImageEditor.Mode.ColorFilter.call(this, 'autofix', 'GALLERY_AUTOFIX');
159 this.doneMessage_ = 'GALLERY_FIXED';
162 ImageEditor.Mode.Autofix.prototype =
163 {__proto__: ImageEditor.Mode.ColorFilter.prototype};
167 * @param {ImageEditor.Toolbar} toolbar The toolbar to populate.
169 ImageEditor.Mode.Autofix.prototype.createTools = function(toolbar) {
171 toolbar.addButton('Apply', this.apply.bind(this));
176 * @return {boolean} // TODO(JSDOC).
178 ImageEditor.Mode.Autofix.prototype.isApplicable = function() {
179 return this.getImageView().hasValidImage() &&
180 filter.autofix.isApplicable(this.getHistogram());
186 ImageEditor.Mode.Autofix.prototype.apply = function() {
187 this.update({histogram: this.getHistogram()});
194 ImageEditor.Mode.InstantAutofix = function() {
195 ImageEditor.Mode.Autofix.apply(this, arguments);
199 ImageEditor.Mode.InstantAutofix.prototype =
200 {__proto__: ImageEditor.Mode.Autofix.prototype};
205 ImageEditor.Mode.InstantAutofix.prototype.setUp = function() {
206 ImageEditor.Mode.Autofix.prototype.setUp.apply(this, arguments);
214 ImageEditor.Mode.Blur = function() {
215 ImageEditor.Mode.Adjust.call(this, 'blur');
218 ImageEditor.Mode.Blur.prototype =
219 {__proto__: ImageEditor.Mode.Adjust.prototype};
223 * @param {ImageEditor.Toolbar} toolbar The toolbar to populate.
225 ImageEditor.Mode.Blur.prototype.createTools = function(toolbar) {
226 toolbar.addRange('strength', 'GALLERY_STRENGTH', 0, 0, 1, 100);
227 toolbar.addRange('radius', 'GALLERY_RADIUS', 1, 1, 3);
234 ImageEditor.Mode.Sharpen = function() {
235 ImageEditor.Mode.Adjust.call(this, 'sharpen');
238 ImageEditor.Mode.Sharpen.prototype =
239 {__proto__: ImageEditor.Mode.Adjust.prototype};
243 * @param {ImageEditor.Toolbar} toolbar The toolbar to populate.
245 ImageEditor.Mode.Sharpen.prototype.createTools = function(toolbar) {
246 toolbar.addRange('strength', 'GALLERY_STRENGTH', 0, 0, 1, 100);
247 toolbar.addRange('radius', 'GALLERY_RADIUS', 1, 1, 3);