| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 * Loads and resizes an image. | 6 * Loads and resizes an image. |
| 7 * @constructor | 7 * @constructor |
| 8 */ | 8 */ |
| 9 function ImageLoader() { | 9 function ImageLoader() { |
| 10 /** | 10 /** |
| 11 * Persistent cache object. | 11 * Persistent cache object. |
| 12 * @type {Cache} | 12 * @type {Cache} |
| 13 * @private | 13 * @private |
| 14 */ | 14 */ |
| 15 this.cache_ = new Cache(); | 15 this.cache_ = new Cache(); |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * Manages pending requests and runs them in order of priorities. | 18 * Manages pending requests and runs them in order of priorities. |
| 19 * @type {Scheduler} | 19 * @type {Scheduler} |
| 20 * @private | 20 * @private |
| 21 */ | 21 */ |
| 22 this.scheduler_ = new Scheduler(); | 22 this.scheduler_ = new Scheduler(); |
| 23 | 23 |
| 24 /** |
| 25 * Piex loader for RAW images. |
| 26 * @private {!PiexLoader} |
| 27 */ |
| 28 this.piexLoader_ = new PiexLoader(); |
| 29 |
| 24 // Grant permissions to all volumes, initialize the cache and then start the | 30 // Grant permissions to all volumes, initialize the cache and then start the |
| 25 // scheduler. | 31 // scheduler. |
| 26 chrome.fileManagerPrivate.getVolumeMetadataList(function(volumeMetadataList) { | 32 chrome.fileManagerPrivate.getVolumeMetadataList(function(volumeMetadataList) { |
| 27 // Listen for mount events, and grant permissions to volumes being mounted. | 33 // Listen for mount events, and grant permissions to volumes being mounted. |
| 28 chrome.fileManagerPrivate.onMountCompleted.addListener( | 34 chrome.fileManagerPrivate.onMountCompleted.addListener( |
| 29 function(event) { | 35 function(event) { |
| 30 if (event.eventType === 'mount' && event.status === 'success') { | 36 if (event.eventType === 'mount' && event.status === 'success') { |
| 31 chrome.fileSystem.requestFileSystem( | 37 chrome.fileSystem.requestFileSystem( |
| 32 {volumeId: event.volumeMetadata.volumeId}, function() {}); | 38 {volumeId: event.volumeMetadata.volumeId}, function() {}); |
| 33 } | 39 } |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 * @private | 98 * @private |
| 93 */ | 99 */ |
| 94 ImageLoader.prototype.onMessage_ = function(senderId, request, callback) { | 100 ImageLoader.prototype.onMessage_ = function(senderId, request, callback) { |
| 95 var requestId = senderId + ':' + request.taskId; | 101 var requestId = senderId + ':' + request.taskId; |
| 96 if (request.cancel) { | 102 if (request.cancel) { |
| 97 // Cancel a task. | 103 // Cancel a task. |
| 98 this.scheduler_.remove(requestId); | 104 this.scheduler_.remove(requestId); |
| 99 return false; // No callback calls. | 105 return false; // No callback calls. |
| 100 } else { | 106 } else { |
| 101 // Create a request task and add it to the scheduler (queue). | 107 // Create a request task and add it to the scheduler (queue). |
| 102 var requestTask = new Request(requestId, this.cache_, request, callback); | 108 var requestTask = new Request( |
| 109 requestId, this.cache_, this.piexLoader_, request, callback); |
| 103 this.scheduler_.add(requestTask); | 110 this.scheduler_.add(requestTask); |
| 104 return true; // Request will call the callback. | 111 return true; // Request will call the callback. |
| 105 } | 112 } |
| 106 }; | 113 }; |
| 107 | 114 |
| 108 /** | 115 /** |
| 109 * Returns the singleton instance. | 116 * Returns the singleton instance. |
| 110 * @return {ImageLoader} ImageLoader object. | 117 * @return {ImageLoader} ImageLoader object. |
| 111 */ | 118 */ |
| 112 ImageLoader.getInstance = function() { | 119 ImageLoader.getInstance = function() { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 * Performs resizing of the source image into the target canvas. | 203 * Performs resizing of the source image into the target canvas. |
| 197 * | 204 * |
| 198 * @param {HTMLCanvasElement|Image} source Source image or canvas. | 205 * @param {HTMLCanvasElement|Image} source Source image or canvas. |
| 199 * @param {HTMLCanvasElement} target Target canvas. | 206 * @param {HTMLCanvasElement} target Target canvas. |
| 200 * @param {Object} options Resizing options as a hash array. | 207 * @param {Object} options Resizing options as a hash array. |
| 201 */ | 208 */ |
| 202 ImageLoader.resize = function(source, target, options) { | 209 ImageLoader.resize = function(source, target, options) { |
| 203 var targetDimensions = ImageLoader.resizeDimensions( | 210 var targetDimensions = ImageLoader.resizeDimensions( |
| 204 source.width, source.height, options); | 211 source.width, source.height, options); |
| 205 | 212 |
| 206 target.width = targetDimensions.width; | |
| 207 target.height = targetDimensions.height; | |
| 208 | |
| 209 // Default orientation is 0deg. | 213 // Default orientation is 0deg. |
| 210 var orientation = options.orientation || 0; | 214 var orientation = options.orientation || new ImageOrientation(1, 0, 0, 1); |
| 211 | 215 var size = orientation.getSizeAfterCancelling( |
| 212 // For odd orientation values: 1 (90deg) and 3 (270deg) flip dimensions. | 216 targetDimensions.width, targetDimensions.height); |
| 213 var drawImageWidth; | 217 target.width = size.width; |
| 214 var drawImageHeight; | 218 target.height = size.height; |
| 215 if (orientation % 2) { | |
| 216 drawImageWidth = target.height; | |
| 217 drawImageHeight = target.width; | |
| 218 } else { | |
| 219 drawImageWidth = target.width; | |
| 220 drawImageHeight = target.height; | |
| 221 } | |
| 222 | 219 |
| 223 var targetContext = target.getContext('2d'); | 220 var targetContext = target.getContext('2d'); |
| 224 targetContext.save(); | 221 targetContext.save(); |
| 225 targetContext.translate(target.width / 2, target.height / 2); | 222 orientation.cancelImageOrientation( |
| 226 targetContext.rotate(orientation * Math.PI / 2); | 223 targetContext, targetDimensions.width, targetDimensions.height); |
| 227 targetContext.drawImage( | 224 targetContext.drawImage( |
| 228 source, | 225 source, |
| 229 0, 0, | 226 0, 0, source.width, source.height, |
| 230 source.width, source.height, | 227 0, 0, targetDimensions.width, targetDimensions.height); |
| 231 -drawImageWidth / 2, -drawImageHeight / 2, | |
| 232 drawImageWidth, drawImageHeight); | |
| 233 targetContext.restore(); | 228 targetContext.restore(); |
| 234 }; | 229 }; |
| OLD | NEW |