]> git.parisson.com Git - pdf.js.git/commitdiff
Decode more jpegs using the browser if possible.
authorBrendan Dahl <brendan.dahl@gmail.com>
Tue, 3 Jan 2012 22:26:19 +0000 (14:26 -0800)
committerBrendan Dahl <brendan.dahl@gmail.com>
Tue, 3 Jan 2012 22:26:19 +0000 (14:26 -0800)
src/evaluator.js
src/image.js
src/stream.js

index 2905565daee80df75a75b095d7f55a0c8328623a..3daf97da904621ac47f4020414ba13ce3be6af0e 100644 (file)
@@ -211,7 +211,8 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
         args = [objId, w, h];
 
         var softMask = dict.get('SMask', 'IM') || false;
-        if (!softMask && image instanceof JpegStream && image.isNative) {
+        if (!softMask && image instanceof JpegStream &&
+            image.isNativelySupported(xref, resources)) {
           // These JPEGs don't need any more processing so we can just send it.
           fn = 'paintJpegXObject';
           handler.send('obj', [objId, 'JpegStream', image.getIR()]);
index 29bad4d8a240ff64babbecc1df7a34bfeb672089..6e7ab2020a1d8194effda3ed9de7b66a09624163 100644 (file)
@@ -9,7 +9,7 @@ var PDFImage = (function PDFImageClosure() {
    * when the image data is ready.
    */
   function handleImageData(handler, xref, res, image, promise) {
-    if (image instanceof JpegStream && image.isNative) {
+    if (image instanceof JpegStream && image.isNativelyDecodable(xref, res)) {
       // For natively supported jpegs send them to the main thread for decoding.
       var dict = image.dict;
       var colorSpace = dict.get('ColorSpace', 'CS');
index d996f5c91b43b7fd06e121fcc9cd5e3fb63dae9a..3367636b490967637fc4123c05eea8b04d5c6809 100644 (file)
@@ -803,35 +803,23 @@ var JpegStream = (function JpegStreamClosure() {
     // need to be removed
     this.dict = dict;
 
-    // Flag indicating wether the image can be natively loaded.
-    this.isNative = true;
-
-    this.colorTransform = -1;
+    this.colorTransform = dict.get('ColorTransform') || -1;
+    this.isAdobeImage = false;
 
     if (isAdobeImage(bytes)) {
-      // when bug 674619 land, let's check if browser can do
-      // normal cmyk and then we won't have to the following
-      var cs = xref.fetchIfRef(dict.get('ColorSpace'));
-
-      // DeviceRGB and DeviceGray are the only Adobe images that work natively
-      if (isName(cs) && (cs.name === 'DeviceRGB' || cs.name === 'DeviceGray')) {
-        bytes = fixAdobeImage(bytes);
-        this.src = bytesToString(bytes);
-      } else {
-        this.colorTransform = dict.get('ColorTransform');
-        this.isNative = false;
-        this.bytes = bytes;
-      }
-    } else {
-      this.src = bytesToString(bytes);
+      this.isAdobeImage = true;
+      bytes = fixAdobeImage(bytes);
     }
 
+    this.bytes = bytes;
+
     DecodeStream.call(this);
   }
 
   JpegStream.prototype = Object.create(DecodeStream.prototype);
 
   JpegStream.prototype.ensureBuffer = function jpegStreamEnsureBuffer(req) {
+    // todo make sure this isn't called on natively supported jpegs
     if (this.bufferLength)
       return;
     var jpegImage = new JpegImage();
@@ -844,11 +832,36 @@ var JpegStream = (function JpegStreamClosure() {
     this.bufferLength = data.length;
   };
   JpegStream.prototype.getIR = function jpegStreamGetIR() {
-    return this.src;
+    return bytesToString(this.bytes);
   };
   JpegStream.prototype.getChar = function jpegStreamGetChar() {
       error('internal error: getChar is not valid on JpegStream');
   };
+  /**
+   * Checks if the image can be decoded and displayed by the browser without any
+   * further processing such as color space conversions.
+   */
+  JpegStream.prototype.isNativelySupported = function isNativelySupported(xref,
+                                                                          res) {
+    var cs = ColorSpace.parse(this.dict.get('ColorSpace'), xref, res);
+    if (cs.name === 'DeviceGray' || cs.name === 'DeviceRGB')
+      return true;
+    if (cs.name === 'DeviceCMYK' && !this.isAdobeImage &&
+        this.colorTransform < 1)
+      return true;
+    return false;
+  };
+  /**
+   * Checks if the image can be decoded by the browser.
+   */
+  JpegStream.prototype.isNativelyDecodable = function isNativelyDecodable(xref,
+                                                                          res) {
+    var cs = ColorSpace.parse(this.dict.get('ColorSpace'), xref, res);
+    if (cs.numComps == 1 || cs.numComps == 3)
+      return true;
+
+    return false;
+  };
 
   return JpegStream;
 })();