]> git.parisson.com Git - pdf.js.git/commitdiff
Implements RunLengthDecode filter
authornotmasteryet <async.processingjs@yahoo.com>
Sat, 14 Jan 2012 19:47:14 +0000 (13:47 -0600)
committernotmasteryet <async.processingjs@yahoo.com>
Sat, 14 Jan 2012 19:47:14 +0000 (13:47 -0600)
src/parser.js
src/stream.js

index e50b12b9b2dcfbfac85be4856ef6883ad5748e6e..0474160277df6082c2198ee4bf95acd441cd3032 100644 (file)
@@ -249,6 +249,9 @@ var Parser = (function ParserClosure() {
       if (name == 'CCITTFaxDecode' || name == 'CCF') {
         return new CCITTFaxStream(stream, params);
       }
+      if (name == 'RunLengthDecode') {
+        return new RunLengthStream(stream);
+      }
       warn('filter "' + name + '" not supported yet');
       return stream;
     }
index 69748b5f26f559cdee05bf2d75abe8c0d551a83d..ded6c37ef4103650fb2ae4238155e9801a21dcda 100644 (file)
@@ -1041,6 +1041,51 @@ var AsciiHexStream = (function AsciiHexStreamClosure() {
   return AsciiHexStream;
 })();
 
+var RunLengthStream = (function RunLengthStreamClosure() {
+  function RunLengthStream(str) {
+    this.str = str;
+    this.dict = str.dict;
+
+    DecodeStream.call(this);
+  }
+
+  RunLengthStream.prototype = Object.create(DecodeStream.prototype);
+
+  RunLengthStream.prototype.readBlock = function runLengthStreamReadBlock() {
+    // The repeatHeader has following format. The first byte defines type of run
+    // and amount of bytes to repeat/copy: n = 0 through 127 - copy next n bytes
+    // (in addition to the second byte from the header), n = 129 through 255 -
+    // duplicate the second byte from the header (257 - n) times, n = 128 - end.
+    var repeatHeader = this.str.getBytes(2);
+    if (!repeatHeader || repeatHeader.length < 2 || repeatHeader[0] == 128) {
+      this.eof = true;
+      return;
+    }
+
+    var bufferLength = this.bufferLength;
+    var n = repeatHeader[0];
+    if (n < 128) {
+      // copy n bytes
+      var buffer = this.ensureBuffer(bufferLength + n + 1);
+      buffer[bufferLength++] = repeatHeader[1];
+      if (n > 0) {
+        var source = this.str.getBytes(n);
+        buffer.set(source, bufferLength);
+        bufferLength += n;
+      }
+    } else {
+      n = 257 - n;
+      var b = repeatHeader[1];
+      var buffer = this.ensureBuffer(bufferLength + n + 1);
+      for (var i = 0; i < n; i++)
+        buffer[bufferLength++] = b;
+    }
+    this.bufferLength = bufferLength;
+  };
+
+  return RunLengthStream;
+})();
+
 var CCITTFaxStream = (function CCITTFaxStreamClosure() {
 
   var ccittEOL = -2;