Return-Path: X-Original-To: apmail-pdfbox-commits-archive@www.apache.org Delivered-To: apmail-pdfbox-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B7A7EC41A for ; Wed, 12 Mar 2014 17:06:13 +0000 (UTC) Received: (qmail 48973 invoked by uid 500); 12 Mar 2014 17:06:01 -0000 Delivered-To: apmail-pdfbox-commits-archive@pdfbox.apache.org Received: (qmail 48762 invoked by uid 500); 12 Mar 2014 17:05:54 -0000 Mailing-List: contact commits-help@pdfbox.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@pdfbox.apache.org Delivered-To: mailing list commits@pdfbox.apache.org Received: (qmail 48733 invoked by uid 99); 12 Mar 2014 17:05:53 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 12 Mar 2014 17:05:53 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 12 Mar 2014 17:05:50 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 45A5C238899C; Wed, 12 Mar 2014 17:05:29 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1576821 - /pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java Date: Wed, 12 Mar 2014 17:05:29 -0000 To: commits@pdfbox.apache.org From: tilman@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140312170529.45A5C238899C@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: tilman Date: Wed Mar 12 17:05:28 2014 New Revision: 1576821 URL: http://svn.apache.org/r1576821 Log: PDFBOX-1977: Check whether chunk needs adjusting before writing EOD Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java?rev=1576821&r1=1576820&r2=1576821&view=diff ============================================================================== --- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java (original) +++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/filter/LZWFilter.java Wed Mar 12 17:05:28 2014 @@ -105,22 +105,8 @@ public class LZWFilter implements Filter result.write(newData); codeTable.add(newData); } - if (codeTable.size() >= 2047) - { - chunk = 12; - } - else if (codeTable.size() >= 1023) - { - chunk = 11; - } - else if (codeTable.size() >= 511) - { - chunk = 10; - } - else - { - chunk = 9; - } + + chunk = calculateChunk(codeTable.size()); prevCommand = nextCommand; } } @@ -168,6 +154,7 @@ public class LZWFilter implements Filter if (newFoundCode == -1) { // use previous + chunk = calculateChunk(codeTable.size() - 1); out.writeBits(foundCode, chunk); // create new table entry codeTable.add(inputPattern); @@ -191,29 +178,22 @@ public class LZWFilter implements Filter foundCode = newFoundCode; } } - if (codeTable.size() - 1 >= 2047) - { - chunk = 12; - } - else if (codeTable.size() - 1 >= 1023) - { - chunk = 11; - } - else if (codeTable.size() - 1 >= 511) - { - chunk = 10; - } - else - { - chunk = 9; - } } if (foundCode != -1) { + chunk = calculateChunk(codeTable.size() - 1); out.writeBits(foundCode, chunk); } + + // PPDFBOX-1977: the decoder wouldn't know that the encoder would output + // an EOD as code, so he would have increased his own code table and + // possibly adjusted the chunk. Therefore, the encoder must behave as + // if the code table had just grown and thus it must be checked it is + // needed to adjust the chunk, based on an increased table size parameter + chunk = calculateChunk(codeTable.size()); + out.writeBits(EOD, chunk); - out.writeBits(0, 7); + out.writeBits(0, 7); // pad with 0 out.flush(); // must do or file will be empty :-( codeTable.clear(); } @@ -275,4 +255,27 @@ public class LZWFilter implements Filter codeTable.add(null); // 257 CLEAR_TABLE } + /** + * Calculate the appropriate chunk size + * + * @param tabSize the size of the code table + * + * @return a value between 9 and 12 + */ + private int calculateChunk(int tabSize) + { + if (tabSize >= 2047) + { + return 12; + } + if (tabSize >= 1023) + { + return 11; + } + if (tabSize >= 511) + { + return 10; + } + return 9; + } }