Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 3906B200B4B for ; Thu, 21 Jul 2016 18:10:05 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 379F5160A73; Thu, 21 Jul 2016 16:10:05 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 5B73B160A72 for ; Thu, 21 Jul 2016 18:10:04 +0200 (CEST) Received: (qmail 20632 invoked by uid 500); 21 Jul 2016 16:10:03 -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 20623 invoked by uid 99); 21 Jul 2016 16:10:03 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 21 Jul 2016 16:10:03 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id 2B5C4C0AA0 for ; Thu, 21 Jul 2016 16:10:03 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.513 X-Spam-Level: X-Spam-Status: No, score=0.513 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RP_MATCHES_RCVD=-1.287] autolearn=disabled Received: from mx2-lw-us.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id D20VUeamllE5 for ; Thu, 21 Jul 2016 16:10:02 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx2-lw-us.apache.org (ASF Mail Server at mx2-lw-us.apache.org) with ESMTP id 1E08E60CC4 for ; Thu, 21 Jul 2016 16:10:01 +0000 (UTC) Received: from svn01-us-west.apache.org (svn.apache.org [10.41.0.6]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id F386FE043F for ; Thu, 21 Jul 2016 16:09:58 +0000 (UTC) Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id 23F233A0B72 for ; Thu, 21 Jul 2016 16:09:58 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1753707 - in /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox: cos/COSStream.java pdmodel/graphics/image/LosslessFactory.java Date: Thu, 21 Jul 2016 16:09:57 -0000 To: commits@pdfbox.apache.org From: tilman@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20160721160958.23F233A0B72@svn01-us-west.apache.org> archived-at: Thu, 21 Jul 2016 16:10:05 -0000 Author: tilman Date: Thu Jul 21 16:09:57 2016 New Revision: 1753707 URL: http://svn.apache.org/viewvc?rev=1753707&view=rev Log: PDFBOX-3433: add optimizations by Michael Doswald Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/LosslessFactory.java Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java?rev=1753707&r1=1753706&r2=1753707&view=diff ============================================================================== --- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java (original) +++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSStream.java Thu Jul 21 16:09:57 2016 @@ -24,7 +24,6 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.pdfbox.filter.Filter; @@ -213,6 +212,12 @@ public class COSStream extends COSDictio return new FilterOutputStream(cosOut) { @Override + public void write(byte[] b, int off, int len) throws IOException + { + this.out.write(b, off, len); + } + + @Override public void close() throws IOException { super.close(); @@ -256,6 +261,12 @@ public class COSStream extends COSDictio return new FilterOutputStream(out) { @Override + public void write(byte[] b, int off, int len) throws IOException + { + this.out.write(b, off, len); + } + + @Override public void close() throws IOException { super.close(); Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/LosslessFactory.java URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/LosslessFactory.java?rev=1753707&r1=1753706&r2=1753707&view=diff ============================================================================== --- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/LosslessFactory.java (original) +++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/LosslessFactory.java Thu Jul 21 16:09:57 2016 @@ -15,7 +15,6 @@ */ package org.apache.pdfbox.pdmodel.graphics.image; -import java.awt.Color; import java.awt.Transparency; import java.awt.image.BufferedImage; import java.awt.image.WritableRaster; @@ -58,50 +57,59 @@ public final class LosslessFactory int bpc; PDDeviceColorSpace deviceColorSpace; - ByteArrayOutputStream bos = new ByteArrayOutputStream(); int height = image.getHeight(); int width = image.getWidth(); + int[] rgbLineBuffer = new int[width]; + byte[] imageData; if ((image.getType() == BufferedImage.TYPE_BYTE_GRAY && image.getColorModel().getPixelSize() <= 8) || (image.getType() == BufferedImage.TYPE_BYTE_BINARY && image.getColorModel().getPixelSize() == 1)) { - MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(bos); - // grayscale images need one color per sample bpc = image.getColorModel().getPixelSize(); deviceColorSpace = PDDeviceGray.INSTANCE; + + ByteArrayOutputStream bos = new ByteArrayOutputStream((width*bpc/8)+(width*bpc%8 != 0 ? 1:0)*height); + MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(bos); + for (int y = 0; y < height; ++y) { - for (int x = 0; x < width; ++x) + for (int pixel : image.getRGB(0, y, width, 1, rgbLineBuffer, 0, width)) { - mcios.writeBits(image.getRGB(x, y) & 0xFF, bpc); + mcios.writeBits(pixel & 0xFF, bpc); } - while (mcios.getBitOffset() != 0) + + int bitOffset = mcios.getBitOffset(); + if (bitOffset != 0) { - mcios.writeBit(0); + mcios.writeBits(0, 8-bitOffset); } } mcios.flush(); mcios.close(); + + imageData = bos.toByteArray(); } else { // RGB bpc = 8; deviceColorSpace = PDDeviceRGB.INSTANCE; + imageData = new byte[width*height*3]; + int byteIdx = 0; + for (int y = 0; y < height; ++y) { - for (int x = 0; x < width; ++x) + for (int pixel : image.getRGB(0, y, width, 1, rgbLineBuffer, 0, width)) { - Color color = new Color(image.getRGB(x, y)); - bos.write(color.getRed()); - bos.write(color.getGreen()); - bos.write(color.getBlue()); + imageData[byteIdx++] = (byte)((pixel >> 16) & 0xFF); + imageData[byteIdx++] = (byte)((pixel >> 8) & 0xFF); + imageData[byteIdx++] = (byte)(pixel & 0xFF); } } } - PDImageXObject pdImage = prepareImageXObject(document, bos.toByteArray(), + PDImageXObject pdImage = prepareImageXObject(document, imageData, image.getWidth(), image.getHeight(), bpc, deviceColorSpace); // alpha -> soft mask @@ -248,7 +256,8 @@ public final class LosslessFactory byte [] byteArray, int width, int height, int bitsPerComponent, PDColorSpace initColorSpace) throws IOException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); + //pre-size the output stream to half of the input + ByteArrayOutputStream baos = new ByteArrayOutputStream(byteArray.length/2); Filter filter = FilterFactory.INSTANCE.getFilter(COSName.FLATE_DECODE); filter.encode(new ByteArrayInputStream(byteArray), baos, new COSDictionary(), 0);