001: /**
002: * Copyright (c) 2005, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: */package org.pdfbox.pdmodel.graphics.predictor;
030:
031: /**
032: * From http://www.w3.org/TR/PNG-Filters.html: The Paeth filter computes a
033: * simple linear function of the three neighboring pixels (left, above, upper
034: * left), then chooses as predictor the neighboring pixel closest to the
035: * computed value. This technique is due to Alan W. Paeth [PAETH].
036: *
037: * To compute the Paeth filter, apply the following formula to each byte of the
038: * scanline:
039: *
040: * <code>Paeth(i,j) = Raw(i,j) - PaethPredictor(Raw(i-1,j), Raw(i,j-1), Raw(i-1,j-1))</code>
041: *
042: * To decode the Paeth filter
043: *
044: * <code>Raw(i,j) = Paeth(i,j) - PaethPredictor(Raw(i-1,j), Raw(i,j-1), Raw(i-1,j-1))</code>
045: *
046: * @author xylifyx@yahoo.co.uk
047: * @version $Revision: 1.3 $
048: */
049: public class Paeth extends PredictorAlgorithm {
050: /**
051: * The paeth predictor function.
052: *
053: * This function is taken almost directly from the PNG definition on
054: * http://www.w3.org/TR/PNG-Filters.html
055: *
056: * @param a
057: * left
058: * @param b
059: * above
060: * @param c
061: * upper left
062: * @return The result of the paeth predictor.
063: */
064: public int paethPredictor(int a, int b, int c) {
065: int p = a + b - c; // initial estimate
066: int pa = Math.abs(p - a); // distances to a, b, c
067: int pb = Math.abs(p - b);
068: int pc = Math.abs(p - c);
069: // return nearest of a,b,c,
070: // breaking ties in order a,b,c.
071: if (pa <= pb && pa <= pc) {
072: return a;
073: } else if (pb <= pc) {
074: return b;
075: } else {
076: return c;
077: }
078: }
079:
080: /**
081: * {@inheritDoc}
082: */
083: public void encodeLine(byte[] src, byte[] dest, int srcDy,
084: int srcOffset, int destDy, int destOffset) {
085: int bpl = getWidth() * getBpp();
086: for (int x = 0; x < bpl; x++) {
087: dest[x + destOffset] = (byte) (src[x + srcOffset] - paethPredictor(
088: leftPixel(src, srcOffset, srcDy, x), abovePixel(
089: src, srcOffset, srcDy, x), aboveLeftPixel(
090: src, srcOffset, srcDy, x)));
091: }
092: }
093:
094: /**
095: * {@inheritDoc}
096: */
097: public void decodeLine(byte[] src, byte[] dest, int srcDy,
098: int srcOffset, int destDy, int destOffset) {
099: int bpl = getWidth() * getBpp();
100: for (int x = 0; x < bpl; x++) {
101: dest[x + destOffset] = (byte) (src[x + srcOffset] + paethPredictor(
102: leftPixel(dest, destOffset, destDy, x), abovePixel(
103: dest, destOffset, destDy, x),
104: aboveLeftPixel(dest, destOffset, destDy, x)));
105: }
106: }
107: }
|