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 // Namespace object for the utilities. | 5 // Namespace object for the utilities. |
6 var ImageUtil = {}; | 6 var ImageUtil = {}; |
7 | 7 |
8 /** | 8 /** |
9 * Performance trace. | 9 * Performance trace. |
10 */ | 10 */ |
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
433 */ | 433 */ |
434 ImageUtil.ImageLoader = function(document, metadataModel) { | 434 ImageUtil.ImageLoader = function(document, metadataModel) { |
435 this.document_ = document; | 435 this.document_ = document; |
436 | 436 |
437 /** | 437 /** |
438 * @private {!MetadataModel} | 438 * @private {!MetadataModel} |
439 * @const | 439 * @const |
440 */ | 440 */ |
441 this.metadataModel_ = metadataModel; | 441 this.metadataModel_ = metadataModel; |
442 | 442 |
443 this.image_ = new Image(); | |
444 this.generation_ = 0; | 443 this.generation_ = 0; |
445 | 444 |
446 /** | 445 /** |
447 * @type {number} | 446 * @type {number} |
448 * @private | 447 * @private |
449 */ | 448 */ |
450 this.timeout_ = 0; | 449 this.timeout_ = 0; |
451 | 450 |
452 /** | 451 /** |
453 * @type {?function(!HTMLCanvasElement, string=)} | 452 * @type {?function(!HTMLCanvasElement, string=)} |
(...skipping 19 matching lines...) Expand all Loading... | |
473 * @param {number=} opt_delay Load delay in milliseconds, useful to let the | 472 * @param {number=} opt_delay Load delay in milliseconds, useful to let the |
474 * animations play out before the computation heavy image loading starts. | 473 * animations play out before the computation heavy image loading starts. |
475 */ | 474 */ |
476 ImageUtil.ImageLoader.prototype.load = function(item, callback, opt_delay) { | 475 ImageUtil.ImageLoader.prototype.load = function(item, callback, opt_delay) { |
477 var entry = item.getEntry(); | 476 var entry = item.getEntry(); |
478 | 477 |
479 this.cancel(); | 478 this.cancel(); |
480 this.entry_ = entry; | 479 this.entry_ = entry; |
481 this.callback_ = callback; | 480 this.callback_ = callback; |
482 | 481 |
483 var targetImage = this.image_; | 482 var targetImage = assertInstanceof(this.document_.createElement('img'), |
483 HTMLImageElement); | |
484 // The transform fetcher is not cancellable so we need a generation counter. | 484 // The transform fetcher is not cancellable so we need a generation counter. |
485 var generation = ++this.generation_; | 485 var generation = ++this.generation_; |
486 | 486 |
487 /** | 487 /** |
488 * @param {!HTMLImageElement} image Image to be transformed. | 488 * @param {!HTMLImageElement} image Image to be transformed. |
489 * @param {Object=} opt_transform Transformation. | 489 * @param {Object=} opt_transform Transformation. |
490 */ | 490 */ |
491 var onTransform = function(image, opt_transform) { | 491 var onTransform = function(image, opt_transform) { |
492 if (generation === this.generation_) { | 492 if (generation === this.generation_) { |
493 this.convertImage_( | 493 this.convertImage_( |
494 image, opt_transform || { scaleX: 1, scaleY: 1, rotate90: 0}); | 494 image, opt_transform); |
yawano
2016/01/20 06:41:33
nit: this line also fits in one line.
ryoh
2016/01/21 03:19:55
Done.
| |
495 } | 495 } |
496 }; | 496 }; |
497 onTransform = onTransform.bind(this); | 497 onTransform = onTransform.bind(this); |
498 | 498 |
499 /** | 499 /** |
500 * @param {string=} opt_error Error. | 500 * @param {string=} opt_error Error. |
501 */ | 501 */ |
502 var onError = function(opt_error) { | 502 var onError = function(opt_error) { |
503 targetImage.onerror = null; | 503 targetImage.onerror = null; |
504 targetImage.onload = null; | 504 targetImage.onload = null; |
(...skipping 28 matching lines...) Expand all Loading... | |
533 targetImage.onerror = onError.bind(null, 'GALLERY_IMAGE_ERROR'); | 533 targetImage.onerror = onError.bind(null, 'GALLERY_IMAGE_ERROR'); |
534 | 534 |
535 targetImage.src = url; | 535 targetImage.src = url; |
536 }.bind(this); | 536 }.bind(this); |
537 | 537 |
538 // Loads the image. If already loaded, then forces a reload. | 538 // Loads the image. If already loaded, then forces a reload. |
539 var startLoad = function() { | 539 var startLoad = function() { |
540 if (generation !== this.generation_) | 540 if (generation !== this.generation_) |
541 return; | 541 return; |
542 | 542 |
543 // Target current image. | |
544 targetImage = this.image_; | |
545 | |
546 // Obtain target URL. | 543 // Obtain target URL. |
547 if (FileType.isRaw(entry)) { | 544 if (FileType.isRaw(entry)) { |
548 var timestamp = | 545 var timestamp = |
549 item.getMetadataItem() && | 546 item.getMetadataItem() && |
550 item.getMetadataItem().modificationTime && | 547 item.getMetadataItem().modificationTime && |
551 item.getMetadataItem().modificationTime.getTime(); | 548 item.getMetadataItem().modificationTime.getTime(); |
552 ImageLoaderClient.getInstance().load(entry.toURL(), function(result) { | 549 ImageLoaderClient.getInstance().load(entry.toURL(), function(result) { |
553 if (generation !== this.generation_) | 550 if (generation !== this.generation_) |
554 return; | 551 return; |
555 if (result.status === 'success') | 552 if (result.status === 'success') |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
603 * Stops loading image. | 600 * Stops loading image. |
604 */ | 601 */ |
605 ImageUtil.ImageLoader.prototype.cancel = function() { | 602 ImageUtil.ImageLoader.prototype.cancel = function() { |
606 if (!this.callback_) | 603 if (!this.callback_) |
607 return; | 604 return; |
608 this.callback_ = null; | 605 this.callback_ = null; |
609 if (this.timeout_) { | 606 if (this.timeout_) { |
610 clearTimeout(this.timeout_); | 607 clearTimeout(this.timeout_); |
611 this.timeout_ = 0; | 608 this.timeout_ = 0; |
612 } | 609 } |
613 if (this.image_) { | |
614 this.image_.onload = function() {}; | |
615 this.image_.onerror = function() {}; | |
616 // Force to free internal image by assigning empty image. | |
617 this.image_.src = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAA' + | |
618 'AAABAAEAAAICTAEAOw=='; | |
619 this.image_ = document.createElement('img'); | |
620 } | |
621 this.generation_++; // Silence the transform fetcher if it is in progress. | 610 this.generation_++; // Silence the transform fetcher if it is in progress. |
622 }; | 611 }; |
623 | 612 |
624 /** | 613 /** |
625 * @param {!HTMLImageElement} image Image to be transformed. | 614 * @param {!HTMLImageElement} image Image to be transformed. |
626 * @param {!Object} transform transformation description to apply to the image. | 615 * @param {!Object} transform transformation description to apply to the image. |
627 * @private | 616 * @private |
628 */ | 617 */ |
629 ImageUtil.ImageLoader.prototype.convertImage_ = function(image, transform) { | 618 ImageUtil.ImageLoader.prototype.convertImage_ = function(image, transform) { |
619 if (!transform || | |
620 (transform.rotate90 === 0 && | |
621 transform.scaleX === 1 && | |
622 transform.scaleY === 1)) { | |
623 try { | |
hirono
2016/01/20 06:07:09
Why is this try closure needed?
ryoh
2016/01/21 03:19:55
It's copied from here:
https://code.google.com/p/c
| |
624 setTimeout(this.callback_, 0, image); | |
625 } catch (e) { | |
626 console.error(e); | |
627 } | |
628 this.callback_ = null; | |
629 return; | |
630 } | |
630 var canvas = this.document_.createElement('canvas'); | 631 var canvas = this.document_.createElement('canvas'); |
631 | 632 |
632 if (transform.rotate90 & 1) { // Rotated +/-90deg, swap the dimensions. | 633 if (transform.rotate90 & 1) { // Rotated +/-90deg, swap the dimensions. |
633 canvas.width = image.height; | 634 canvas.width = image.height; |
634 canvas.height = image.width; | 635 canvas.height = image.width; |
635 } else { | 636 } else { |
636 canvas.width = image.width; | 637 canvas.width = image.width; |
637 canvas.height = image.height; | 638 canvas.height = image.height; |
638 } | 639 } |
639 | 640 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
730 ImageUtil.getMetricName = function(name) { | 731 ImageUtil.getMetricName = function(name) { |
731 return 'PhotoEditor.' + name; | 732 return 'PhotoEditor.' + name; |
732 }; | 733 }; |
733 | 734 |
734 /** | 735 /** |
735 * Used for metrics reporting, keep in sync with the histogram description. | 736 * Used for metrics reporting, keep in sync with the histogram description. |
736 * @type {Array<string>} | 737 * @type {Array<string>} |
737 * @const | 738 * @const |
738 */ | 739 */ |
739 ImageUtil.FILE_TYPES = ['jpg', 'png', 'gif', 'bmp', 'webp']; | 740 ImageUtil.FILE_TYPES = ['jpg', 'png', 'gif', 'bmp', 'webp']; |
OLD | NEW |