KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > graphics > predictor > Uptimum


1 /**
2  * Copyright (c) 2005, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdmodel.graphics.predictor;
32
33 /**
34  *
35  *
36  * In an Uptimum encoded image, each line takes up width*bpp+1 bytes. The first
37  * byte holds a number that signifies which algorithm encoded the line.
38  *
39  * @author xylifyx@yahoo.co.uk
40  * @version $Revision: 1.3 $
41  */

42 public class Uptimum extends PredictorAlgorithm
43 {
44     /**
45      * {@inheritDoc}
46      */

47     public void checkBufsiz(byte[] filtered, byte[] raw)
48     {
49         if (filtered.length != (getWidth() * getBpp() + 1) * getHeight())
50         {
51
52             throw new IllegalArgumentException JavaDoc(
53                     "filtered.length != (width*bpp + 1) * height, "
54                             + filtered.length + " "
55                             + (getWidth() * getBpp() + 1) * getHeight()
56                             + "w,h,bpp=" + getWidth() + "," + getHeight() + ","
57                             + getBpp());
58         }
59         if (raw.length != getWidth() * getHeight() * getBpp())
60         {
61             throw new IllegalArgumentException JavaDoc(
62                     "raw.length != width * height * bpp, raw.length="
63                             + raw.length + " w,h,bpp=" + getWidth() + ","
64                             + getHeight() + "," + getBpp());
65         }
66     }
67
68     /**
69      * {@inheritDoc}
70      */

71     public void encodeLine(byte[] src, byte[] dest, int srcDy, int srcOffset,
72             int destDy, int destOffset)
73     {
74         throw new UnsupportedOperationException JavaDoc("encodeLine");
75     }
76
77     /**
78      * {@inheritDoc}
79      */

80     public void decodeLine(byte[] src, byte[] dest, int srcDy, int srcOffset,
81             int destDy, int destOffset)
82     {
83         throw new UnsupportedOperationException JavaDoc("decodeLine");
84     }
85
86     /**
87      * {@inheritDoc}
88      */

89     public void encode(byte[] src, byte[] dest)
90     {
91         checkBufsiz(dest, src);
92         throw new UnsupportedOperationException JavaDoc("encode");
93     }
94
95     /**
96      * Filter indexed by byte code.
97      */

98     PredictorAlgorithm[] filter = { new None(), new Sub(), new Up(), new Average(),
99             new Paeth() };
100
101     /**
102      * {@inheritDoc}
103      */

104     public void setBpp(int bpp)
105     {
106         super.setBpp(bpp);
107         for (int i = 0; i < filter.length; i++)
108         {
109             filter[i].setBpp(bpp);
110         }
111     }
112     /**
113      * {@inheritDoc}
114      */

115     public void setHeight(int height)
116     {
117         super.setHeight(height);
118         for (int i = 0; i < filter.length; i++)
119         {
120             filter[i].setHeight(height);
121         }
122     }
123
124     /**
125      * {@inheritDoc}
126      */

127     public void setWidth(int width)
128     {
129         super.setWidth(width);
130         for (int i = 0; i < filter.length; i++)
131         {
132             filter[i].setWidth(width);
133         }
134     }
135
136     /**
137      * {@inheritDoc}
138      */

139     public void decode(byte[] src, byte[] dest)
140     {
141         checkBufsiz(src, dest);
142         int bpl = getWidth() * getBpp();
143         int srcDy = bpl + 1;
144         for (int y = 0; y < getHeight(); y++)
145         {
146             PredictorAlgorithm f = filter[src[y * srcDy]];
147             int srcOffset = y * srcDy + 1;
148             f.decodeLine(src, dest, srcDy, srcOffset, bpl, y * bpl);
149         }
150     }
151 }
Popular Tags