OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 ByteReader = function(arrayBuffer) { | 5 ByteReader = function(arrayBuffer) { |
6 this.buf_ = arrayBuffer; | 6 this.buf_ = arrayBuffer; |
7 this.view_ = new DataView(arrayBuffer); | 7 this.view_ = new DataView(arrayBuffer); |
8 this.pos_ = 0; | 8 this.pos_ = 0; |
9 this.seekStack_ = []; | 9 this.seekStack_ = []; |
10 this.setByteOrder(ByteReader.BIG_ENDIAN); | 10 this.setByteOrder(ByteReader.BIG_ENDIAN); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 | 117 |
118 return rv.join(''); | 118 return rv.join(''); |
119 }; | 119 }; |
120 | 120 |
121 /** | 121 /** |
122 * Read as an image encoded in a data url. | 122 * Read as an image encoded in a data url. |
123 * | 123 * |
124 * This is a static utility function. There is a member function with the | 124 * This is a static utility function. There is a member function with the |
125 * same name which side-effects the current read position. | 125 * same name which side-effects the current read position. |
126 */ | 126 */ |
127 ByteReader.readImage = function(dataView, pos, size, | 127 ByteReader.readImage = function(dataView, pos, size, opt_end) { |
128 opt_littleEndian, opt_end) { | |
129 opt_end = opt_end || dataView.byteLength; | 128 opt_end = opt_end || dataView.byteLength; |
130 ByteReader.validateRead(pos, size, opt_end); | 129 ByteReader.validateRead(pos, size, opt_end); |
131 | 130 |
132 var format; | 131 var format; |
133 if (ByteReader.readString(dataView, pos, 4, opt_end) == '\x89PNG') { | 132 if (ByteReader.readString(dataView, pos, 4, opt_end) == '\x89PNG') { |
134 format = 'png'; | 133 format = 'png'; |
135 } else if (dataView.getUint16(pos, opt_littleEndian) == 0xFFD8) { | 134 } else if (dataView.getUint16(pos, false) == 0xFFD8) { // Always big endian. |
136 format = 'jpeg'; | 135 format = 'jpeg'; |
137 } else { | 136 } else { |
138 format = 'unknown'; | 137 format = 'unknown'; |
139 } | 138 } |
140 | 139 |
141 var b64 = ByteReader.readBase64(dataView, pos, size, opt_end); | 140 var b64 = ByteReader.readBase64(dataView, pos, size, opt_end); |
142 return 'data:image/' + format + ';base64,' + b64; | 141 return 'data:image/' + format + ';base64,' + b64; |
143 }; | 142 }; |
144 | 143 |
145 // Instance methods. | 144 // Instance methods. |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 return rv; | 282 return rv; |
284 }; | 283 }; |
285 | 284 |
286 /** | 285 /** |
287 * Read an image returning it as a data url. | 286 * Read an image returning it as a data url. |
288 * | 287 * |
289 * Adjusts the current position on success. Throws an exception if the | 288 * Adjusts the current position on success. Throws an exception if the |
290 * read would go past the end of the buffer. | 289 * read would go past the end of the buffer. |
291 */ | 290 */ |
292 ByteReader.prototype.readImage = function(size, opt_end) { | 291 ByteReader.prototype.readImage = function(size, opt_end) { |
293 var rv = ByteReader.readImage( | 292 var rv = ByteReader.readImage(this.view_, this.pos_, size, opt_end); |
294 this.view_, this.pos_, size, this.littleEndian_, opt_end); | |
295 this.pos_ += size; | 293 this.pos_ += size; |
296 return rv; | 294 return rv; |
297 }; | 295 }; |
298 | 296 |
299 /** | 297 /** |
300 * Seek to a give position relative to opt_seekStart. | 298 * Seek to a give position relative to opt_seekStart. |
301 */ | 299 */ |
302 ByteReader.prototype.seek = function(pos, opt_seekStart, opt_end) { | 300 ByteReader.prototype.seek = function(pos, opt_seekStart, opt_end) { |
303 opt_end = opt_end || this.view_.byteLength; | 301 opt_end = opt_end || this.view_.byteLength; |
304 | 302 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 ByteReader.prototype.popSeek = function() { | 334 ByteReader.prototype.popSeek = function() { |
337 this.seek(this.seekStack_.pop()); | 335 this.seek(this.seekStack_.pop()); |
338 }; | 336 }; |
339 | 337 |
340 /** | 338 /** |
341 * Return the current read position. | 339 * Return the current read position. |
342 */ | 340 */ |
343 ByteReader.prototype.tell = function() { | 341 ByteReader.prototype.tell = function() { |
344 return this.pos_; | 342 return this.pos_; |
345 }; | 343 }; |
OLD | NEW |