Uses blob URL instead of data when possible

This commit is contained in:
Yury Delendik 2013-11-11 22:25:03 -06:00 committed by Yury
parent 4ce6cb8b0f
commit c8af2565f1
3 changed files with 34 additions and 8 deletions

View File

@ -15,7 +15,7 @@
* limitations under the License.
*/
/* globals bytesToString, ColorSpace, Dict, EOF, error, info, Jbig2Image,
JpegImage, JpxImage, Lexer, Util */
JpegImage, JpxImage, Lexer, Util, PDFJS */
'use strict';
@ -832,7 +832,7 @@ var JpegStream = (function JpegStreamClosure() {
}
};
JpegStream.prototype.getIR = function JpegStream_getIR() {
return bytesToString(this.bytes);
return PDFJS.createObjectURL(this.bytes, 'image/jpeg');
};
/**
* Checks if the image can be decoded and displayed by the browser without any

View File

@ -815,7 +815,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
}, this);
messageHandler.on('JpegDecode', function(data, promise) {
var imageData = data[0];
var imageUrl = data[0];
var components = data[1];
if (components != 3 && components != 1)
error('Only 3 component or 1 component can be returned');
@ -845,8 +845,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
}
promise.resolve({ data: buf, width: width, height: height});
}).bind(this);
var src = 'data:image/jpeg;base64,' + window.btoa(imageData);
img.src = src;
img.src = imageUrl;
});
},

View File

@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals Cmd, ColorSpace, Dict, MozBlobBuilder, Name, PDFJS, Ref */
/* globals Cmd, ColorSpace, Dict, MozBlobBuilder, Name, PDFJS, Ref, URL */
'use strict';
@ -1100,6 +1100,33 @@ PDFJS.createBlob = function createBlob(data, contentType) {
return bb.getBlob(contentType);
};
PDFJS.createObjectURL = (function createObjectURLClosure() {
if (typeof URL !== 'undefined' && URL.createObjectURL) {
return function createObjectURL(data, contentType) {
var blob = PDFJS.createBlob(data, contentType);
return URL.createObjectURL(blob);
};
}
// Blob/createObjectURL is not available, falling back to data schema.
var digits =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
return function createObjectURL(data, contentType) {
var buffer = 'data:' + contentType + ';base64,';
for (var i = 0, ii = data.length; i < ii; i += 3) {
var b1 = data[i] & 0xFF;
var b2 = data[i + 1] & 0xFF;
var b3 = data[i + 2] & 0xFF;
var d1 = b1 >> 2, d2 = ((b1 & 3) << 4) | (b2 >> 4);
var d3 = i + 1 < ii ? ((b2 & 0xF) << 2) | (b3 >> 6) : 64;
var d4 = i + 2 < ii ? (b3 & 0x3F) : 64;
buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4];
}
return buffer;
};
})();
function MessageHandler(name, comObj) {
this.name = name;
this.comObj = comObj;
@ -1191,10 +1218,10 @@ MessageHandler.prototype = {
}
};
function loadJpegStream(id, imageData, objs) {
function loadJpegStream(id, imageUrl, objs) {
var img = new Image();
img.onload = (function loadJpegStream_onloadClosure() {
objs.resolve(id, img);
});
img.src = 'data:image/jpeg;base64,' + window.btoa(imageData);
img.src = imageUrl;
}