Home » pdfbox-1.1.0-src » org.apache.pdfbox.pdmodel.graphics.predictor » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.pdfbox.pdmodel.graphics.predictor;
   18   
   19   /**
   20    * From http://www.w3.org/TR/PNG-Filters.html: The Paeth filter computes a
   21    * simple linear function of the three neighboring pixels (left, above, upper
   22    * left), then chooses as predictor the neighboring pixel closest to the
   23    * computed value. This technique is due to Alan W. Paeth [PAETH].
   24    *
   25    * To compute the Paeth filter, apply the following formula to each byte of the
   26    * scanline:
   27    *
   28    * <code>Paeth(i,j) = Raw(i,j) - PaethPredictor(Raw(i-1,j), Raw(i,j-1), Raw(i-1,j-1))</code>
   29    *
   30    * To decode the Paeth filter
   31    *
   32    * <code>Raw(i,j) = Paeth(i,j) - PaethPredictor(Raw(i-1,j), Raw(i,j-1), Raw(i-1,j-1))</code>
   33    *
   34    * @author xylifyx@yahoo.co.uk
   35    * @version $Revision: 1.3 $
   36    */
   37   public class Paeth extends PredictorAlgorithm
   38   {
   39       /**
   40        * The paeth predictor function.
   41        *
   42        * This function is taken almost directly from the PNG definition on
   43        * http://www.w3.org/TR/PNG-Filters.html
   44        *
   45        * @param a
   46        *            left
   47        * @param b
   48        *            above
   49        * @param c
   50        *            upper left
   51        * @return The result of the paeth predictor.
   52        */
   53       public int paethPredictor(int a, int b, int c)
   54       {
   55           int p = a + b - c; // initial estimate
   56           int pa = Math.abs(p - a); // distances to a, b, c
   57           int pb = Math.abs(p - b);
   58           int pc = Math.abs(p - c);
   59           // return nearest of a,b,c,
   60           // breaking ties in order a,b,c.
   61           if (pa <= pb && pa <= pc)
   62           {
   63               return a;
   64           }
   65           else if (pb <= pc)
   66           {
   67               return b;
   68           }
   69           else
   70           {
   71               return c;
   72           }
   73       }
   74   
   75       /**
   76        * {@inheritDoc}
   77        */
   78       public void encodeLine(byte[] src, byte[] dest, int srcDy, int srcOffset,
   79               int destDy, int destOffset)
   80       {
   81           int bpl = getWidth() * getBpp();
   82           for (int x = 0; x < bpl; x++)
   83           {
   84               dest[x + destOffset] = (byte) (src[x + srcOffset] - paethPredictor(
   85                       leftPixel(src, srcOffset, srcDy, x), abovePixel(src,
   86                               srcOffset, srcDy, x), aboveLeftPixel(src,
   87                               srcOffset, srcDy, x)));
   88           }
   89       }
   90   
   91       /**
   92        * {@inheritDoc}
   93        */
   94       public void decodeLine(byte[] src, byte[] dest, int srcDy, int srcOffset,
   95               int destDy, int destOffset)
   96       {
   97           int bpl = getWidth() * getBpp();
   98           for (int x = 0; x < bpl; x++)
   99           {
  100               dest[x + destOffset] = (byte) (src[x + srcOffset] + paethPredictor(
  101                       leftPixel(dest, destOffset, destDy, x), abovePixel(dest,
  102                               destOffset, destDy, x), aboveLeftPixel(dest,
  103                               destOffset, destDy, x)));
  104           }
  105       }
  106   }

Home » pdfbox-1.1.0-src » org.apache.pdfbox.pdmodel.graphics.predictor » [javadoc | source]