Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / ui / file_manager / file_manager / foreground / js / image_editor / image_util.js
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.
4
5 'use strict';
6
7
8 // Namespace object for the utilities.
9 function ImageUtil() {}
10
11 /**
12  * Performance trace.
13  */
14 ImageUtil.trace = (function() {
15   function PerformanceTrace() {
16     this.lines_ = {};
17     this.timers_ = {};
18     this.container_ = null;
19   }
20
21   PerformanceTrace.prototype.bindToDOM = function(container) {
22     this.container_ = container;
23   };
24
25   PerformanceTrace.prototype.report = function(key, value) {
26     if (!(key in this.lines_)) {
27       if (this.container_) {
28         var div = this.lines_[key] = document.createElement('div');
29         this.container_.appendChild(div);
30       } else {
31         this.lines_[key] = {};
32       }
33     }
34     this.lines_[key].textContent = key + ': ' + value;
35     if (ImageUtil.trace.log) this.dumpLine(key);
36   };
37
38   PerformanceTrace.prototype.resetTimer = function(key) {
39     this.timers_[key] = Date.now();
40   };
41
42   PerformanceTrace.prototype.reportTimer = function(key) {
43     this.report(key, (Date.now() - this.timers_[key]) + 'ms');
44   };
45
46   PerformanceTrace.prototype.dump = function() {
47     for (var key in this.lines_)
48       this.dumpLine(key);
49   };
50
51   PerformanceTrace.prototype.dumpLine = function(key) {
52     console.log('trace.' + this.lines_[key].textContent);
53   };
54
55   return new PerformanceTrace();
56 })();
57
58 /**
59  * @param {number} min Minimum value.
60  * @param {number} value Value to adjust.
61  * @param {number} max Maximum value.
62  * @return {number} The closest to the |value| number in span [min, max].
63  */
64 ImageUtil.clamp = function(min, value, max) {
65   return Math.max(min, Math.min(max, value));
66 };
67
68 /**
69  * @param {number} min Minimum value.
70  * @param {number} value Value to check.
71  * @param {number} max Maximum value.
72  * @return {boolean} True if value is between.
73  */
74 ImageUtil.between = function(min, value, max) {
75   return (value - min) * (value - max) <= 0;
76 };
77
78 /**
79  * Rectangle class.
80  */
81
82 /**
83  * Rectangle constructor takes 0, 1, 2 or 4 arguments.
84  * Supports following variants:
85  *   new Rect(left, top, width, height)
86  *   new Rect(width, height)
87  *   new Rect(rect)         // anything with left, top, width, height properties
88  *   new Rect(bounds)       // anything with left, top, right, bottom properties
89  *   new Rect(canvas|image) // anything with width and height properties.
90  *   new Rect()             // empty rectangle.
91  * @constructor
92  */
93 function Rect() {
94   switch (arguments.length) {
95     case 4:
96       this.left = arguments[0];
97       this.top = arguments[1];
98       this.width = arguments[2];
99       this.height = arguments[3];
100       return;
101
102     case 2:
103       this.left = 0;
104       this.top = 0;
105       this.width = arguments[0];
106       this.height = arguments[1];
107       return;
108
109     case 1: {
110       var source = arguments[0];
111       if ('left' in source && 'top' in source) {
112         this.left = source.left;
113         this.top = source.top;
114         if ('right' in source && 'bottom' in source) {
115           this.width = source.right - source.left;
116           this.height = source.bottom - source.top;
117           return;
118         }
119       } else {
120         this.left = 0;
121         this.top = 0;
122       }
123       if ('width' in source && 'height' in source) {
124         this.width = source.width;
125         this.height = source.height;
126         return;
127       }
128       break; // Fall through to the error message.
129     }
130
131     case 0:
132       this.left = 0;
133       this.top = 0;
134       this.width = 0;
135       this.height = 0;
136       return;
137   }
138   console.error('Invalid Rect constructor arguments:',
139        Array.apply(null, arguments));
140 }
141
142 /**
143  * @param {number} factor Factor to scale.
144  * @return {Rect} A rectangle with every dimension scaled.
145  */
146 Rect.prototype.scale = function(factor) {
147   return new Rect(
148       this.left * factor,
149       this.top * factor,
150       this.width * factor,
151       this.height * factor);
152 };
153
154 /**
155  * @param {number} dx Difference in X.
156  * @param {number} dy Difference in Y.
157  * @return {Rect} A rectangle shifted by (dx,dy), same size.
158  */
159 Rect.prototype.shift = function(dx, dy) {
160   return new Rect(this.left + dx, this.top + dy, this.width, this.height);
161 };
162
163 /**
164  * @param {number} x Coordinate of the left top corner.
165  * @param {number} y Coordinate of the left top corner.
166  * @return {Rect} A rectangle with left==x and top==y, same size.
167  */
168 Rect.prototype.moveTo = function(x, y) {
169   return new Rect(x, y, this.width, this.height);
170 };
171
172 /**
173  * @param {number} dx Difference in X.
174  * @param {number} dy Difference in Y.
175  * @return {Rect} A rectangle inflated by (dx, dy), same center.
176  */
177 Rect.prototype.inflate = function(dx, dy) {
178   return new Rect(
179       this.left - dx, this.top - dy, this.width + 2 * dx, this.height + 2 * dy);
180 };
181
182 /**
183  * @param {number} x Coordinate of the point.
184  * @param {number} y Coordinate of the point.
185  * @return {boolean} True if the point lies inside the rectangle.
186  */
187 Rect.prototype.inside = function(x, y) {
188   return this.left <= x && x < this.left + this.width &&
189          this.top <= y && y < this.top + this.height;
190 };
191
192 /**
193  * @param {Rect} rect Rectangle to check.
194  * @return {boolean} True if this rectangle intersects with the |rect|.
195  */
196 Rect.prototype.intersects = function(rect) {
197   return (this.left + this.width) > rect.left &&
198          (rect.left + rect.width) > this.left &&
199          (this.top + this.height) > rect.top &&
200          (rect.top + rect.height) > this.top;
201 };
202
203 /**
204  * @param {Rect} rect Rectangle to check.
205  * @return {boolean} True if this rectangle containing the |rect|.
206  */
207 Rect.prototype.contains = function(rect) {
208   return (this.left <= rect.left) &&
209          (rect.left + rect.width) <= (this.left + this.width) &&
210          (this.top <= rect.top) &&
211          (rect.top + rect.height) <= (this.top + this.height);
212 };
213
214 /**
215  * @return {boolean} True if rectangle is empty.
216  */
217 Rect.prototype.isEmpty = function() {
218   return this.width === 0 || this.height === 0;
219 };
220
221 /**
222  * Clamp the rectangle to the bounds by moving it.
223  * Decrease the size only if necessary.
224  * @param {Rect} bounds Bounds.
225  * @return {Rect} Calculated rectangle.
226  */
227 Rect.prototype.clamp = function(bounds) {
228   var rect = new Rect(this);
229
230   if (rect.width > bounds.width) {
231     rect.left = bounds.left;
232     rect.width = bounds.width;
233   } else if (rect.left < bounds.left) {
234     rect.left = bounds.left;
235   } else if (rect.left + rect.width >
236              bounds.left + bounds.width) {
237     rect.left = bounds.left + bounds.width - rect.width;
238   }
239
240   if (rect.height > bounds.height) {
241     rect.top = bounds.top;
242     rect.height = bounds.height;
243   } else if (rect.top < bounds.top) {
244     rect.top = bounds.top;
245   } else if (rect.top + rect.height >
246              bounds.top + bounds.height) {
247     rect.top = bounds.top + bounds.height - rect.height;
248   }
249
250   return rect;
251 };
252
253 /**
254  * @return {string} String representation.
255  */
256 Rect.prototype.toString = function() {
257   return '(' + this.left + ',' + this.top + '):' +
258          '(' + (this.left + this.width) + ',' + (this.top + this.height) + ')';
259 };
260 /*
261  * Useful shortcuts for drawing (static functions).
262  */
263
264 /**
265  * Draw the image in context with appropriate scaling.
266  * @param {CanvasRenderingContext2D} context Context to draw.
267  * @param {Image} image Image to draw.
268  * @param {Rect=} opt_dstRect Rectangle in the canvas (whole canvas by default).
269  * @param {Rect=} opt_srcRect Rectangle in the image (whole image by default).
270  */
271 Rect.drawImage = function(context, image, opt_dstRect, opt_srcRect) {
272   opt_dstRect = opt_dstRect || new Rect(context.canvas);
273   opt_srcRect = opt_srcRect || new Rect(image);
274   if (opt_dstRect.isEmpty() || opt_srcRect.isEmpty())
275     return;
276   context.drawImage(image,
277       opt_srcRect.left, opt_srcRect.top, opt_srcRect.width, opt_srcRect.height,
278       opt_dstRect.left, opt_dstRect.top, opt_dstRect.width, opt_dstRect.height);
279 };
280
281 /**
282  * Draw a box around the rectangle.
283  * @param {CanvasRenderingContext2D} context Context to draw.
284  * @param {Rect} rect Rectangle.
285  */
286 Rect.outline = function(context, rect) {
287   context.strokeRect(
288       rect.left - 0.5, rect.top - 0.5, rect.width + 1, rect.height + 1);
289 };
290
291 /**
292  * Fill the rectangle.
293  * @param {CanvasRenderingContext2D} context Context to draw.
294  * @param {Rect} rect Rectangle.
295  */
296 Rect.fill = function(context, rect) {
297   context.fillRect(rect.left, rect.top, rect.width, rect.height);
298 };
299
300 /**
301  * Fills the space between the two rectangles.
302  * @param {CanvasRenderingContext2D} context Context to draw.
303  * @param {Rect} inner Inner rectangle.
304  * @param {Rect} outer Outer rectangle.
305  */
306 Rect.fillBetween = function(context, inner, outer) {
307   var innerRight = inner.left + inner.width;
308   var innerBottom = inner.top + inner.height;
309   var outerRight = outer.left + outer.width;
310   var outerBottom = outer.top + outer.height;
311   if (inner.top > outer.top) {
312     context.fillRect(
313         outer.left, outer.top, outer.width, inner.top - outer.top);
314   }
315   if (inner.left > outer.left) {
316     context.fillRect(
317         outer.left, inner.top, inner.left - outer.left, inner.height);
318   }
319   if (inner.width < outerRight) {
320     context.fillRect(
321         innerRight, inner.top, outerRight - innerRight, inner.height);
322   }
323   if (inner.height < outerBottom) {
324     context.fillRect(
325         outer.left, innerBottom, outer.width, outerBottom - innerBottom);
326   }
327 };
328
329 /**
330  * Circle class.
331  * @param {number} x X coordinate of circle center.
332  * @param {number} y Y coordinate of circle center.
333  * @param {number} r Radius.
334  * @constructor
335  */
336 function Circle(x, y, r) {
337   this.x = x;
338   this.y = y;
339   this.squaredR = r * r;
340 }
341
342 /**
343  * Check if the point is inside the circle.
344  * @param {number} x X coordinate of the point.
345  * @param {number} y Y coordinate of the point.
346  * @return {boolean} True if the point is inside.
347  */
348 Circle.prototype.inside = function(x, y) {
349   x -= this.x;
350   y -= this.y;
351   return x * x + y * y <= this.squaredR;
352 };
353
354 /**
355  * Copy an image applying scaling and rotation.
356  *
357  * @param {HTMLCanvasElement} dst Destination.
358  * @param {HTMLCanvasElement|HTMLImageElement} src Source.
359  * @param {number} scaleX Y scale transformation.
360  * @param {number} scaleY X scale transformation.
361  * @param {number} angle (in radians).
362  */
363 ImageUtil.drawImageTransformed = function(dst, src, scaleX, scaleY, angle) {
364   var context = dst.getContext('2d');
365   context.save();
366   context.translate(context.canvas.width / 2, context.canvas.height / 2);
367   context.rotate(angle);
368   context.scale(scaleX, scaleY);
369   context.drawImage(src, -src.width / 2, -src.height / 2);
370   context.restore();
371 };
372
373 /**
374  * Adds or removes an attribute to/from an HTML element.
375  * @param {HTMLElement} element To be applied to.
376  * @param {string} attribute Name of attribute.
377  * @param {boolean} on True if add, false if remove.
378  */
379 ImageUtil.setAttribute = function(element, attribute, on) {
380   if (on)
381     element.setAttribute(attribute, '');
382   else
383     element.removeAttribute(attribute);
384 };
385
386 /**
387  * Adds or removes CSS class to/from an HTML element.
388  * @param {HTMLElement} element To be applied to.
389  * @param {string} className Name of CSS class.
390  * @param {boolean} on True if add, false if remove.
391  */
392 ImageUtil.setClass = function(element, className, on) {
393   var cl = element.classList;
394   if (on)
395     cl.add(className);
396   else
397     cl.remove(className);
398 };
399
400 /**
401  * ImageLoader loads an image from a given Entry into a canvas in two steps:
402  * 1. Loads the image into an HTMLImageElement.
403  * 2. Copies pixels from HTMLImageElement to HTMLCanvasElement. This is done
404  *    stripe-by-stripe to avoid freezing up the UI. The transform is taken into
405  *    account.
406  *
407  * @param {HTMLDocument} document Owner document.
408  * @param {MetadataCache=} opt_metadataCache Metadata cache. Required for
409  *     caching. If not passed, caching will be disabled.
410  * @constructor
411  */
412 ImageUtil.ImageLoader = function(document, opt_metadataCache) {
413   this.document_ = document;
414   this.metadataCache_ = opt_metadataCache || null;
415   this.image_ = new Image();
416   this.generation_ = 0;
417 };
418
419 /**
420  * Loads an image.
421  * TODO(mtomasz): Simplify, or even get rid of this class and merge with the
422  * ThumbnaiLoader class.
423  *
424  * @param {FileEntry} entry Image entry to be loaded.
425  * @param {function(function(object))} transformFetcher function to get
426  *     the image transform (which we need for the image orientation).
427  * @param {function(HTMLCanvasElement, string=)} callback Callback to be
428  *     called when loaded. The second optional argument is an error identifier.
429  * @param {number=} opt_delay Load delay in milliseconds, useful to let the
430  *     animations play out before the computation heavy image loading starts.
431  */
432 ImageUtil.ImageLoader.prototype.load = function(
433     entry, transformFetcher, callback, opt_delay) {
434   this.cancel();
435
436   this.entry_ = entry;
437   this.callback_ = callback;
438
439   // The transform fetcher is not cancellable so we need a generation counter.
440   var generation = ++this.generation_;
441   var onTransform = function(image, transform) {
442     if (generation === this.generation_) {
443       this.convertImage_(
444           image, transform || { scaleX: 1, scaleY: 1, rotate90: 0});
445     }
446   };
447
448   var onError = function(opt_error) {
449     this.image_.onerror = null;
450     this.image_.onload = null;
451     var tmpCallback = this.callback_;
452     this.callback_ = null;
453     var emptyCanvas = this.document_.createElement('canvas');
454     emptyCanvas.width = 0;
455     emptyCanvas.height = 0;
456     tmpCallback(emptyCanvas, opt_error);
457   }.bind(this);
458
459   var loadImage = function(opt_metadata) {
460     ImageUtil.metrics.startInterval(ImageUtil.getMetricName('LoadTime'));
461     this.timeout_ = null;
462
463     this.image_.onload = function(e) {
464       this.image_.onerror = null;
465       this.image_.onload = null;
466
467       transformFetcher(entry, onTransform.bind(this, e.target));
468     }.bind(this);
469
470     // The error callback has an optional error argument, which in case of a
471     // general error should not be specified
472     this.image_.onerror = onError.bind(this, 'GALLERY_IMAGE_ERROR');
473
474     // Load the image directly. The query parameter is workaround for
475     // crbug.com/379678, which force to update the contents of the image.
476     this.image_.src = entry.toURL() + "?nocache=" + Date.now();
477   }.bind(this);
478
479   // Loads the image. If already loaded, then forces a reload.
480   var startLoad = this.resetImage_.bind(this, function() {
481     // Fetch metadata to detect last modification time for the caching purpose.
482     if (this.metadataCache_)
483       this.metadataCache_.getOne(entry, 'filesystem', loadImage);
484     else
485       loadImage();
486   }.bind(this), onError);
487
488   if (opt_delay) {
489     this.timeout_ = setTimeout(startLoad, opt_delay);
490   } else {
491     startLoad();
492   }
493 };
494
495 /**
496  * Resets the image by forcing the garbage collection and clearing the src
497  * attribute.
498  *
499  * @param {function()} onSuccess Success callback.
500  * @param {function(opt_string)} onError Failure callback with an optional
501  *     error identifier.
502  * @private
503  */
504 ImageUtil.ImageLoader.prototype.resetImage_ = function(onSuccess, onError) {
505   var clearSrc = function() {
506     this.image_.onload = onSuccess;
507     this.image_.onerror = onSuccess;
508     this.image_.src = '';
509   }.bind(this);
510
511   var emptyImage = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAA' +
512       'AAABAAEAAAICTAEAOw==';
513
514   if (this.image_.src !== emptyImage) {
515     // Load an empty image, then clear src.
516     this.image_.onload = clearSrc;
517     this.image_.onerror = onError.bind(this, 'GALLERY_IMAGE_ERROR');
518     this.image_.src = emptyImage;
519   } else {
520     // Empty image already loaded, so clear src immediately.
521     clearSrc();
522   }
523 };
524
525 /**
526  * @return {boolean} True if an image is loading.
527  */
528 ImageUtil.ImageLoader.prototype.isBusy = function() {
529   return !!this.callback_;
530 };
531
532 /**
533  * @param {Entry} entry Image entry.
534  * @return {boolean} True if loader loads this image.
535  */
536 ImageUtil.ImageLoader.prototype.isLoading = function(entry) {
537   return this.isBusy() && util.isSameEntry(this.entry_, entry);
538 };
539
540 /**
541  * @param {function} callback To be called when the image loaded.
542  */
543 ImageUtil.ImageLoader.prototype.setCallback = function(callback) {
544   this.callback_ = callback;
545 };
546
547 /**
548  * Stops loading image.
549  */
550 ImageUtil.ImageLoader.prototype.cancel = function() {
551   if (!this.callback_) return;
552   this.callback_ = null;
553   if (this.timeout_) {
554     clearTimeout(this.timeout_);
555     this.timeout_ = null;
556   }
557   if (this.image_) {
558     this.image_.onload = function() {};
559     this.image_.onerror = function() {};
560     this.image_.src = '';
561   }
562   this.generation_++;  // Silence the transform fetcher if it is in progress.
563 };
564
565 /**
566  * @param {HTMLImageElement} image Image to be transformed.
567  * @param {Object} transform transformation description to apply to the image.
568  * @private
569  */
570 ImageUtil.ImageLoader.prototype.convertImage_ = function(image, transform) {
571   var canvas = this.document_.createElement('canvas');
572
573   if (transform.rotate90 & 1) {  // Rotated +/-90deg, swap the dimensions.
574     canvas.width = image.height;
575     canvas.height = image.width;
576   } else {
577     canvas.width = image.width;
578     canvas.height = image.height;
579   }
580
581   var context = canvas.getContext('2d');
582   context.save();
583   context.translate(canvas.width / 2, canvas.height / 2);
584   context.rotate(transform.rotate90 * Math.PI / 2);
585   context.scale(transform.scaleX, transform.scaleY);
586
587   var stripCount = Math.ceil(image.width * image.height / (1 << 21));
588   var step = Math.max(16, Math.ceil(image.height / stripCount)) & 0xFFFFF0;
589
590   this.copyStrip_(context, image, 0, step);
591 };
592
593 /**
594  * @param {CanvasRenderingContext2D} context Context to draw.
595  * @param {HTMLImageElement} image Image to draw.
596  * @param {number} firstRow Number of the first pixel row to draw.
597  * @param {number} rowCount Count of pixel rows to draw.
598  * @private
599  */
600 ImageUtil.ImageLoader.prototype.copyStrip_ = function(
601     context, image, firstRow, rowCount) {
602   var lastRow = Math.min(firstRow + rowCount, image.height);
603
604   context.drawImage(
605       image, 0, firstRow, image.width, lastRow - firstRow,
606       -image.width / 2, firstRow - image.height / 2,
607       image.width, lastRow - firstRow);
608
609   if (lastRow === image.height) {
610     context.restore();
611     if (this.entry_.toURL().substr(0, 5) !== 'data:') {  // Ignore data urls.
612       ImageUtil.metrics.recordInterval(ImageUtil.getMetricName('LoadTime'));
613     }
614     try {
615       setTimeout(this.callback_, 0, context.canvas);
616     } catch (e) {
617       console.error(e);
618     }
619     this.callback_ = null;
620   } else {
621     var self = this;
622     this.timeout_ = setTimeout(
623         function() {
624           self.timeout_ = null;
625           self.copyStrip_(context, image, lastRow, rowCount);
626         }, 0);
627   }
628 };
629
630 /**
631  * @param {HTMLElement} element To remove children from.
632  */
633 ImageUtil.removeChildren = function(element) {
634   element.textContent = '';
635 };
636
637 /**
638  * @param {string} name File name (with extension).
639  * @return {string} File name without extension.
640  */
641 ImageUtil.getDisplayNameFromName = function(name) {
642   var index = name.lastIndexOf('.');
643   if (index !== -1)
644     return name.substr(0, index);
645   else
646     return name;
647 };
648
649 /**
650  * @param {string} name File name.
651  * @return {string} File extension.
652  */
653 ImageUtil.getExtensionFromFullName = function(name) {
654   var index = name.lastIndexOf('.');
655   if (index !== -1)
656     return name.substring(index);
657   else
658     return '';
659 };
660
661 /**
662  * Metrics (from metrics.js) itnitialized by the File Manager from owner frame.
663  * @type {Object?}
664  */
665 ImageUtil.metrics = null;
666
667 /**
668  * @param {string} name Local name.
669  * @return {string} Full name.
670  */
671 ImageUtil.getMetricName = function(name) {
672   return 'PhotoEditor.' + name;
673 };
674
675 /**
676  * Used for metrics reporting, keep in sync with the histogram description.
677  */
678 ImageUtil.FILE_TYPES = ['jpg', 'png', 'gif', 'bmp', 'webp'];