From 6025b6016517c6d898d8957d1d7e03ba71431912 Mon Sep 17 00:00:00 2001 From: tknall Date: Fri, 1 Dec 2006 12:20:24 +0000 Subject: Initial import of release 2.2. git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@4 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../pdfbox/pdmodel/graphics/predictor/Average.java | 81 +++++ .../pdfbox/pdmodel/graphics/predictor/None.java | 104 +++++++ .../pdfbox/pdmodel/graphics/predictor/Paeth.java | 121 ++++++++ .../graphics/predictor/PredictorAlgorithm.java | 336 +++++++++++++++++++++ .../org/pdfbox/pdmodel/graphics/predictor/Sub.java | 86 ++++++ .../org/pdfbox/pdmodel/graphics/predictor/Up.java | 100 ++++++ .../pdfbox/pdmodel/graphics/predictor/Uptimum.java | 153 ++++++++++ .../pdfbox/pdmodel/graphics/predictor/package.html | 10 + 8 files changed, 991 insertions(+) create mode 100644 src/main/java/org/pdfbox/pdmodel/graphics/predictor/Average.java create mode 100644 src/main/java/org/pdfbox/pdmodel/graphics/predictor/None.java create mode 100644 src/main/java/org/pdfbox/pdmodel/graphics/predictor/Paeth.java create mode 100644 src/main/java/org/pdfbox/pdmodel/graphics/predictor/PredictorAlgorithm.java create mode 100644 src/main/java/org/pdfbox/pdmodel/graphics/predictor/Sub.java create mode 100644 src/main/java/org/pdfbox/pdmodel/graphics/predictor/Up.java create mode 100644 src/main/java/org/pdfbox/pdmodel/graphics/predictor/Uptimum.java create mode 100644 src/main/java/org/pdfbox/pdmodel/graphics/predictor/package.html (limited to 'src/main/java/org/pdfbox/pdmodel/graphics/predictor') diff --git a/src/main/java/org/pdfbox/pdmodel/graphics/predictor/Average.java b/src/main/java/org/pdfbox/pdmodel/graphics/predictor/Average.java new file mode 100644 index 0000000..46e65dc --- /dev/null +++ b/src/main/java/org/pdfbox/pdmodel/graphics/predictor/Average.java @@ -0,0 +1,81 @@ +/** + * Copyright (c) 2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.pdmodel.graphics.predictor; + +/** + * We can use raw on the right hand side of + * the decoding formula because it is already decoded. + * + * average(i,j) = raw(i,j) + (raw(i-1,j)+raw(i,j-1)/2 + * + * decoding + * + * raw(i,j) = avarage(i,j) - (raw(i-1,j)+raw(i,j-1)/2 + * + * @author xylifyx@yahoo.co.uk + * @version $Revision: 1.2 $ + */ +public class Average extends PredictorAlgorithm +{ + /** + * Not an optimal version, but close to the def. + * + * @see org.pdfbox.pdmodel.graphics.predictor.PredictorAlgorithm#encodeLine(byte[], byte[], + * int, int, int, int) + */ + public void encodeLine(byte[] src, byte[] dest, int srcDy, int srcOffset, + int destDy, int destOffset) + { + int bpl = getWidth() * getBpp(); + for (int x = 0; x < bpl; x++) + { + dest[x + destOffset] = (byte) (src[x + srcOffset] - ((leftPixel( + src, srcOffset, srcDy, x) + abovePixel(src, srcOffset, + srcDy, x)) >>> 2)); + } + } + + /** + * @see org.pdfbox.pdmodel.graphics.predictor.PredictorAlgorithm#decodeLine(byte[], byte[], + * int, int, int, int) + */ + public void decodeLine(byte[] src, byte[] dest, int srcDy, int srcOffset, + int destDy, int destOffset) + { + int bpl = getWidth() * getBpp(); + for (int x = 0; x < bpl; x++) + { + dest[x + destOffset] = (byte) (src[x + srcOffset] + ((leftPixel( + dest, destOffset, destDy, x) + abovePixel(dest, + destOffset, destDy, x)) >>> 2)); + } + } +} diff --git a/src/main/java/org/pdfbox/pdmodel/graphics/predictor/None.java b/src/main/java/org/pdfbox/pdmodel/graphics/predictor/None.java new file mode 100644 index 0000000..20ec815 --- /dev/null +++ b/src/main/java/org/pdfbox/pdmodel/graphics/predictor/None.java @@ -0,0 +1,104 @@ +/** + * Copyright (c) 2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.pdmodel.graphics.predictor; + +/** + * The none algorithm. + * + * None(i,j) = Raw(i,j) + * + * Raw(i,j) = None(i,j) + * + * @author xylifyx@yahoo.co.uk + * @version $Revision: 1.2 $ + */ +public class None extends PredictorAlgorithm +{ + /** + * encode a byte array full of image data using the filter that this object + * implements. + * + * @param src + * buffer + * @param dest + * buffer + */ + public void encode(byte[] src, byte[] dest) + { + checkBufsiz(dest, src); + System.arraycopy(src,0,dest,0,src.length); + } + + /** + * decode a byte array full of image data using the filter that this object + * implements. + * + * @param src + * buffer + * @param dest + * buffer + */ + public void decode(byte[] src, byte[] dest) + { + System.arraycopy(src,0,dest,0,src.length); + } + + + + /** + * @see org.pdfbox.pdmodel.graphics.predictor.PredictorAlgorithm#encodeLine(byte[], byte[], + * int, int, int, int) + */ + public void encodeLine(byte[] src, byte[] dest, int srcDy, int srcOffset, + int destDy, int destOffset) + { + int bpl = getWidth() * getBpp(); + for (int x = 0; x < bpl; x++) + { + dest[destOffset + x] = src[srcOffset + x]; + } + } + + /** + * @see org.pdfbox.pdmodel.graphics.predictor.PredictorAlgorithm#decodeLine(byte[], byte[], + * int, int, int, int) + */ + public void decodeLine(byte[] src, byte[] dest, int srcDy, int srcOffset, + int destDy, int destOffset) + { + int bpl = getWidth() * getBpp(); + for (int x = 0; x < bpl; x++) + { + dest[destOffset + x] = src[srcOffset + x]; + } + } + +} diff --git a/src/main/java/org/pdfbox/pdmodel/graphics/predictor/Paeth.java b/src/main/java/org/pdfbox/pdmodel/graphics/predictor/Paeth.java new file mode 100644 index 0000000..1fddef0 --- /dev/null +++ b/src/main/java/org/pdfbox/pdmodel/graphics/predictor/Paeth.java @@ -0,0 +1,121 @@ +/** + * Copyright (c) 2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + */ +package org.pdfbox.pdmodel.graphics.predictor; + +/** + * From http://www.w3.org/TR/PNG-Filters.html: The Paeth filter computes a + * simple linear function of the three neighboring pixels (left, above, upper + * left), then chooses as predictor the neighboring pixel closest to the + * computed value. This technique is due to Alan W. Paeth [PAETH]. + * + * To compute the Paeth filter, apply the following formula to each byte of the + * scanline: + * + * Paeth(i,j) = Raw(i,j) - PaethPredictor(Raw(i-1,j), Raw(i,j-1), Raw(i-1,j-1)) + * + * To decode the Paeth filter + * + * Raw(i,j) = Paeth(i,j) - PaethPredictor(Raw(i-1,j), Raw(i,j-1), Raw(i-1,j-1)) + * + * @author xylifyx@yahoo.co.uk + * @version $Revision: 1.2 $ + */ +public class Paeth extends PredictorAlgorithm +{ + /** + * The paeth predictor function. + * + * This function is taken almost directly from the PNG definition on + * http://www.w3.org/TR/PNG-Filters.html + * + * @param a + * left + * @param b + * above + * @param c + * upper left + * @return The result of the paeth predictor. + */ + public int paethPredictor(int a, int b, int c) + { + int p = a + b - c; // initial estimate + int pa = Math.abs(p - a); // distances to a, b, c + int pb = Math.abs(p - b); + int pc = Math.abs(p - c); + // return nearest of a,b,c, + // breaking ties in order a,b,c. + if (pa <= pb && pa <= pc) + { + return a; + } + else if (pb <= pc) + { + return b; + } + else + { + return c; + } + } + + /** + * @see org.pdfbox.pdmodel.graphics.predictor.PredictorAlgorithm#encodeLine(byte[], byte[], + * int, int, int, int) + */ + public void encodeLine(byte[] src, byte[] dest, int srcDy, int srcOffset, + int destDy, int destOffset) + { + int bpl = getWidth() * getBpp(); + for (int x = 0; x < bpl; x++) + { + dest[x + destOffset] = (byte) (src[x + srcOffset] - paethPredictor( + leftPixel(src, srcOffset, srcDy, x), abovePixel(src, + srcOffset, srcDy, x), aboveLeftPixel(src, + srcOffset, srcDy, x))); + } + } + + /** + * @see org.pdfbox.pdmodel.graphics.predictor.PredictorAlgorithm#decodeLine(byte[], byte[], + * int, int, int, int) + */ + public void decodeLine(byte[] src, byte[] dest, int srcDy, int srcOffset, + int destDy, int destOffset) + { + int bpl = getWidth() * getBpp(); + for (int x = 0; x < bpl; x++) + { + dest[x + destOffset] = (byte) (src[x + srcOffset] + paethPredictor( + leftPixel(dest, destOffset, destDy, x), abovePixel(dest, + destOffset, destDy, x), aboveLeftPixel(dest, + destOffset, destDy, x))); + } + } +} diff --git a/src/main/java/org/pdfbox/pdmodel/graphics/predictor/PredictorAlgorithm.java b/src/main/java/org/pdfbox/pdmodel/graphics/predictor/PredictorAlgorithm.java new file mode 100644 index 0000000..11f60f9 --- /dev/null +++ b/src/main/java/org/pdfbox/pdmodel/graphics/predictor/PredictorAlgorithm.java @@ -0,0 +1,336 @@ +/** + * Copyright (c) 2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.pdmodel.graphics.predictor; + +import java.util.Random; + +/** + * Implements different PNG predictor algorithms that is used in PDF files. + * + * @author xylifyx@yahoo.co.uk + * @version $Revision: 1.2 $ + * @see http://www.w3.org/TR/PNG-Filters.html + */ +public abstract class PredictorAlgorithm +{ + private int width; + + private int height; + + private int bpp; + + /** + * check that buffer sizes matches width,height,bpp. This implementation is + * used by most of the filters, but not Uptimum. + * + * @param src The source buffer. + * @param dest The destination buffer. + */ + public void checkBufsiz(byte[] src, byte[] dest) + { + if (src.length != dest.length) + { + throw new IllegalArgumentException("src.length != dest.length"); + } + if (src.length != getWidth() * getHeight() * getBpp()) + { + throw new IllegalArgumentException( + "src.length != width * height * bpp"); + } + } + + /** + * encode line of pixel data in src from srcOffset and width*bpp bytes + * forward, put the decoded bytes into dest. + * + * @param src + * raw image data + * @param dest + * encoded data + * @param srcDy + * byte offset between lines + * @param srcOffset + * beginning of line data + * @param destDy + * byte offset between lines + * @param destOffset + * beginning of line data + */ + public abstract void encodeLine(byte[] src, byte[] dest, int srcDy, + int srcOffset, int destDy, int destOffset); + + /** + * decode line of pixel data in src from src_offset and width*bpp bytes + * forward, put the decoded bytes into dest. + * + * @param src + * encoded image data + * @param dest + * raw data + * @param srcDy + * byte offset between lines + * @param srcOffset + * beginning of line data + * @param destDy + * byte offset between lines + * @param destOffset + * beginning of line data + */ + public abstract void decodeLine(byte[] src, byte[] dest, int srcDy, + int srcOffset, int destDy, int destOffset); + + /** + * Simple command line program to test the algorithm. + * + * @param args The command line arguments. + */ + public static void main(String[] args) + { + Random rnd = new Random(); + int width = 5; + int height = 5; + int bpp = 3; + byte[] raw = new byte[width * height * bpp]; + rnd.nextBytes(raw); + System.out.println("raw: "); + dump(raw); + for (int i = 10; i < 15; i++) + { + byte[] decoded = new byte[width * height * bpp]; + byte[] encoded = new byte[width * height * bpp]; + + PredictorAlgorithm filter = PredictorAlgorithm.getFilter(i); + filter.setWidth(width); + filter.setHeight(height); + filter.setBpp(bpp); + filter.encode(raw, encoded); + filter.decode(encoded, decoded); + System.out.println(filter.getClass().getName()); + dump(decoded); + } + } + + /** + * Get the left pixel from the buffer. + * + * @param buf The buffer. + * @param offset The offset into the buffer. + * @param dy The dy value. + * @param x The x value. + * + * @return The left pixel. + */ + public int leftPixel(byte[] buf, int offset, int dy, int x) + { + return x >= getBpp() ? buf[offset + x - getBpp()] : 0; + } + + /** + * Get the above pixel from the buffer. + * + * @param buf The buffer. + * @param offset The offset into the buffer. + * @param dy The dy value. + * @param x The x value. + * + * @return The above pixel. + */ + public int abovePixel(byte[] buf, int offset, int dy, int x) + { + return offset >= dy ? buf[offset + x - dy] : 0; + } + + /** + * Get the above-left pixel from the buffer. + * + * @param buf The buffer. + * @param offset The offset into the buffer. + * @param dy The dy value. + * @param x The x value. + * + * @return The above-left pixel. + */ + public int aboveLeftPixel(byte[] buf, int offset, int dy, int x) + { + return offset >= dy && x >= getBpp() ? buf[offset + x - dy - getBpp()] + : 0; + } + + /** + * Simple helper to print out a buffer. + * + * @param raw The bytes to print out. + */ + private static void dump(byte[] raw) + { + for (int i = 0; i < raw.length; i++) + { + System.out.print(raw[i] + " "); + } + System.out.println(); + } + + /** + * @return Returns the bpp. + */ + public int getBpp() + { + return bpp; + } + + /** + * @param newBpp + * The bpp to set. + */ + public void setBpp(int newBpp) + { + bpp = newBpp; + } + + /** + * @return Returns the height. + */ + public int getHeight() + { + return height; + } + + /** + * @param newHeight + * The height to set. + */ + public void setHeight(int newHeight) + { + height = newHeight; + } + + /** + * @return Returns the width. + */ + public int getWidth() + { + return width; + } + + /** + * @param newWidth + * The width to set. + */ + public void setWidth(int newWidth) + { + this.width = newWidth; + } + + + /** + * encode a byte array full of image data using the filter that this object + * implements. + * + * @param src + * buffer + * @param dest + * buffer + */ + public void encode(byte[] src, byte[] dest) + { + checkBufsiz(dest, src); + int dy = getWidth()*getBpp(); + for (int y = 0; y < height; y++) + { + int yoffset = y * dy; + encodeLine(src, dest, dy, yoffset, dy, yoffset); + } + } + + /** + * decode a byte array full of image data using the filter that this object + * implements. + * + * @param src + * buffer + * @param dest + * buffer + */ + public void decode(byte[] src, byte[] dest) + { + checkBufsiz(src, dest); + int dy = width * bpp; + for (int y = 0; y < height; y++) + { + int yoffset = y * dy; + decodeLine(src, dest, dy, yoffset, dy, yoffset); + } + } + + /** + * @param predictor + * + * + * @return The predictor class based on the predictor code. + */ + public static PredictorAlgorithm getFilter(int predictor) + { + PredictorAlgorithm filter; + switch (predictor) + { + case 10: + filter = new None(); + break; + case 11: + filter = new Sub(); + break; + case 12: + filter = new Up(); + break; + case 13: + filter = new Average(); + break; + case 14: + filter = new Paeth(); + break; + case 15: + filter = new Uptimum(); + break; + default: + filter = new None(); + } + return filter; + } +} diff --git a/src/main/java/org/pdfbox/pdmodel/graphics/predictor/Sub.java b/src/main/java/org/pdfbox/pdmodel/graphics/predictor/Sub.java new file mode 100644 index 0000000..3959dbe --- /dev/null +++ b/src/main/java/org/pdfbox/pdmodel/graphics/predictor/Sub.java @@ -0,0 +1,86 @@ +/** + * Copyright (c) 2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.pdmodel.graphics.predictor; + +/** + * The sub algorithm. + * + * Sub(i,j) = Raw(i,j) - Raw(i-1,j) + * + * Raw(i,j) = Sub(i,j) + Raw(i-1,j) + * + * @author xylifyx@yahoo.co.uk + * @version $Revision: 1.2 $ + */ +public class Sub extends PredictorAlgorithm +{ + /** + * @see org.pdfbox.pdmodel.graphics.predictor.PredictorAlgorithm#encodeLine(byte[], byte[], int, int, int, int) + */ + public void encodeLine(byte[] src, byte[] dest, int srcDy, int srcOffset, + int destDy, int destOffset) + { + int bpl = getWidth()*getBpp(); + int bpp = getBpp(); + // case: x < bpp + for (int x = 0; x < bpl && x < bpp; x++) + { + dest[x + destOffset] = src[x + srcOffset]; + } + // otherwise + for (int x = getBpp(); x < bpl; x++) + { + dest[x + destOffset] = (byte) (src[x + srcOffset] - src[x + + srcOffset - bpp]); + } + } + + /** + * @see org.pdfbox.pdmodel.graphics.predictor.PredictorAlgorithm#decodeLine(byte[], byte[], int, int, int, int) + */ + public void decodeLine(byte[] src, byte[] dest, int srcDy, int srcOffset, + int destDy, int destOffset) + { + int bpl = getWidth()*getBpp(); + int bpp = getBpp(); + // case: x < bpp + for (int x = 0; x < bpl && x < bpp; x++) + { + dest[x + destOffset] = src[x + srcOffset]; + } + // otherwise + for (int x = getBpp(); x < bpl; x++) + { + dest[x + destOffset] = (byte) (src[x + srcOffset] + dest[x + + destOffset - bpp]); + } + } +} diff --git a/src/main/java/org/pdfbox/pdmodel/graphics/predictor/Up.java b/src/main/java/org/pdfbox/pdmodel/graphics/predictor/Up.java new file mode 100644 index 0000000..f1932b4 --- /dev/null +++ b/src/main/java/org/pdfbox/pdmodel/graphics/predictor/Up.java @@ -0,0 +1,100 @@ +/** + * Copyright (c) 2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.pdmodel.graphics.predictor; + +/** + * The up algorithm. + * + * Up(i,j) = Raw(i,j) - Raw(i,j-1) + * + * Raw(i,j) = Up(i,j) + Raw(i,j-1) + * + * @author xylifyx@yahoo.co.uk + * @version $Revision: 1.2 $ + */ +public class Up extends PredictorAlgorithm +{ + /** + * @see org.pdfbox.pdmodel.graphics.predictor.PredictorAlgorithm#encodeLine(byte[], byte[], int, int, int, int) + */ + public void encodeLine(byte[] src, byte[] dest, int srcDy, int srcOffset, + int destDy, int destOffset) + { + int bpl = getWidth()*getBpp(); + // case: y = 0; + if (srcOffset - srcDy < 0) + { + if (0 < getHeight()) + { + for (int x = 0; x < bpl; x++) + { + dest[destOffset + x] = src[srcOffset + x]; + } + } + } + else + { + for (int x = 0; x < bpl; x++) + { + dest[destOffset + x] = (byte) (src[srcOffset + x] - src[srcOffset + + x - srcDy]); + } + } + } + + /** + * @see org.pdfbox.pdmodel.graphics.predictor.PredictorAlgorithm#decodeLine(byte[], byte[], int, int, int, int) + */ + public void decodeLine(byte[] src, byte[] dest, int srcDy, int srcOffset, + int destDy, int destOffset) + { + // case: y = 0; + int bpl = getWidth()*getBpp(); + if (destOffset - destDy < 0) + { + if (0 < getHeight()) + { + for (int x = 0; x < bpl; x++) + { + dest[destOffset + x] = src[srcOffset + x]; + } + } + } + else + { + for (int x = 0; x < bpl; x++) + { + dest[destOffset + x] = (byte) (src[srcOffset + x] + dest[destOffset + + x - destDy]); + } + } + } +} diff --git a/src/main/java/org/pdfbox/pdmodel/graphics/predictor/Uptimum.java b/src/main/java/org/pdfbox/pdmodel/graphics/predictor/Uptimum.java new file mode 100644 index 0000000..ac03162 --- /dev/null +++ b/src/main/java/org/pdfbox/pdmodel/graphics/predictor/Uptimum.java @@ -0,0 +1,153 @@ +/** + * Copyright (c) 2005, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.pdmodel.graphics.predictor; + +/** + * + * + * In an Uptimum encoded image, each line takes up width*bpp+1 bytes. The first + * byte holds a number that signifies which algorithm encoded the line. + * + * @author xylifyx@yahoo.co.uk + * @version $Revision: 1.2 $ + */ +public class Uptimum extends PredictorAlgorithm +{ + /** + * @see PredictorAlgorithm#checkBufsiz(byte[], byte[]) + */ + public void checkBufsiz(byte[] filtered, byte[] raw) + { + if (filtered.length != (getWidth() * getBpp() + 1) * getHeight()) + { + + throw new IllegalArgumentException( + "filtered.length != (width*bpp + 1) * height, " + + filtered.length + " " + + (getWidth() * getBpp() + 1) * getHeight() + + "w,h,bpp=" + getWidth() + "," + getHeight() + "," + + getBpp()); + } + if (raw.length != getWidth() * getHeight() * getBpp()) + { + throw new IllegalArgumentException( + "raw.length != width * height * bpp, raw.length=" + + raw.length + " w,h,bpp=" + getWidth() + "," + + getHeight() + "," + getBpp()); + } + } + + /** + * @see org.pdfbox.pdmodel.graphics.predictor.PredictorAlgorithm#encodeLine(byte[], byte[], + * int, int, int, int) + */ + public void encodeLine(byte[] src, byte[] dest, int srcDy, int srcOffset, + int destDy, int destOffset) + { + throw new UnsupportedOperationException("encodeLine"); + } + + /** + * @see org.pdfbox.pdmodel.graphics.predictor.PredictorAlgorithm#decodeLine(byte[], byte[], + * int, int, int, int) + */ + public void decodeLine(byte[] src, byte[] dest, int srcDy, int srcOffset, + int destDy, int destOffset) + { + throw new UnsupportedOperationException("decodeLine"); + } + + /** + * @see PredictorAlgorithm#encode(byte[], byte[]) + */ + public void encode(byte[] src, byte[] dest) + { + checkBufsiz(dest, src); + throw new UnsupportedOperationException("encode"); + } + + /** + * filter indexed by byte code. + */ + PredictorAlgorithm[] filter = { new None(), new Sub(), new Up(), new Average(), + new Paeth() }; + + /** + * @see org.pdfbox.pdmodel.graphics.predictor.PredictorAlgorithm#setBpp(int) + */ + public void setBpp(int bpp) + { + super.setBpp(bpp); + for (int i = 0; i < filter.length; i++) + { + filter[i].setBpp(bpp); + } + } + /** + * @see org.pdfbox.pdmodel.graphics.predictor.PredictorAlgorithm#setHeight(int) + */ + public void setHeight(int height) + { + super.setHeight(height); + for (int i = 0; i < filter.length; i++) + { + filter[i].setHeight(height); + } + } + + /** + * @see org.pdfbox.pdmodel.graphics.predictor.PredictorAlgorithm#setWidth(int) + */ + public void setWidth(int width) + { + super.setWidth(width); + for (int i = 0; i < filter.length; i++) + { + filter[i].setWidth(width); + } + } + + /** + * @see PredictorAlgorithm#decode(byte[], byte[]) + */ + public void decode(byte[] src, byte[] dest) + { + checkBufsiz(src, dest); + int bpl = getWidth() * getBpp(); + int srcDy = bpl + 1; + for (int y = 0; y < getHeight(); y++) + { + PredictorAlgorithm f = filter[src[y * srcDy]]; + int srcOffset = y * srcDy + 1; + f.decodeLine(src, dest, srcDy, srcOffset, bpl, y * bpl); + } + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/pdmodel/graphics/predictor/package.html b/src/main/java/org/pdfbox/pdmodel/graphics/predictor/package.html new file mode 100644 index 0000000..127d2e0 --- /dev/null +++ b/src/main/java/org/pdfbox/pdmodel/graphics/predictor/package.html @@ -0,0 +1,10 @@ + + + + + + +The predictor package contains code for different PNG predictor algorithms that +are present in PDF documents. These classes are used internally by PDFBox. + + -- cgit v1.2.3