KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Random JavaDoc;
34
35 /**
36  * Implements different PNG predictor algorithms that is used in PDF files.
37  *
38  * @author xylifyx@yahoo.co.uk
39  * @version $Revision: 1.3 $
40  * @see <a HREF="http://www.w3.org/TR/PNG-Filters.html">PNG Filters</a>
41  */

42 public abstract class PredictorAlgorithm
43 {
44     private int width;
45
46     private int height;
47
48     private int bpp;
49
50     /**
51      * check that buffer sizes matches width,height,bpp. This implementation is
52      * used by most of the filters, but not Uptimum.
53      *
54      * @param src The source buffer.
55      * @param dest The destination buffer.
56      */

57     public void checkBufsiz(byte[] src, byte[] dest)
58     {
59         if (src.length != dest.length)
60         {
61             throw new IllegalArgumentException JavaDoc("src.length != dest.length");
62         }
63         if (src.length != getWidth() * getHeight() * getBpp())
64         {
65             throw new IllegalArgumentException JavaDoc(
66                     "src.length != width * height * bpp");
67         }
68     }
69
70     /**
71      * encode line of pixel data in src from srcOffset and width*bpp bytes
72      * forward, put the decoded bytes into dest.
73      *
74      * @param src
75      * raw image data
76      * @param dest
77      * encoded data
78      * @param srcDy
79      * byte offset between lines
80      * @param srcOffset
81      * beginning of line data
82      * @param destDy
83      * byte offset between lines
84      * @param destOffset
85      * beginning of line data
86      */

87     public abstract void encodeLine(byte[] src, byte[] dest, int srcDy,
88             int srcOffset, int destDy, int destOffset);
89
90     /**
91      * decode line of pixel data in src from src_offset and width*bpp bytes
92      * forward, put the decoded bytes into dest.
93      *
94      * @param src
95      * encoded image data
96      * @param dest
97      * raw data
98      * @param srcDy
99      * byte offset between lines
100      * @param srcOffset
101      * beginning of line data
102      * @param destDy
103      * byte offset between lines
104      * @param destOffset
105      * beginning of line data
106      */

107     public abstract void decodeLine(byte[] src, byte[] dest, int srcDy,
108             int srcOffset, int destDy, int destOffset);
109
110     /**
111      * Simple command line program to test the algorithm.
112      *
113      * @param args The command line arguments.
114      */

115     public static void main(String JavaDoc[] args)
116     {
117         Random JavaDoc rnd = new Random JavaDoc();
118         int width = 5;
119         int height = 5;
120         int bpp = 3;
121         byte[] raw = new byte[width * height * bpp];
122         rnd.nextBytes(raw);
123         System.out.println("raw: ");
124         dump(raw);
125         for (int i = 10; i < 15; i++)
126         {
127             byte[] decoded = new byte[width * height * bpp];
128             byte[] encoded = new byte[width * height * bpp];
129
130             PredictorAlgorithm filter = PredictorAlgorithm.getFilter(i);
131             filter.setWidth(width);
132             filter.setHeight(height);
133             filter.setBpp(bpp);
134             filter.encode(raw, encoded);
135             filter.decode(encoded, decoded);
136             System.out.println(filter.getClass().getName());
137             dump(decoded);
138         }
139     }
140
141     /**
142      * Get the left pixel from the buffer.
143      *
144      * @param buf The buffer.
145      * @param offset The offset into the buffer.
146      * @param dy The dy value.
147      * @param x The x value.
148      *
149      * @return The left pixel.
150      */

151     public int leftPixel(byte[] buf, int offset, int dy, int x)
152     {
153         return x >= getBpp() ? buf[offset + x - getBpp()] : 0;
154     }
155
156     /**
157      * Get the above pixel from the buffer.
158      *
159      * @param buf The buffer.
160      * @param offset The offset into the buffer.
161      * @param dy The dy value.
162      * @param x The x value.
163      *
164      * @return The above pixel.
165      */

166     public int abovePixel(byte[] buf, int offset, int dy, int x)
167     {
168         return offset >= dy ? buf[offset + x - dy] : 0;
169     }
170
171     /**
172      * Get the above-left pixel from the buffer.
173      *
174      * @param buf The buffer.
175      * @param offset The offset into the buffer.
176      * @param dy The dy value.
177      * @param x The x value.
178      *
179      * @return The above-left pixel.
180      */

181     public int aboveLeftPixel(byte[] buf, int offset, int dy, int x)
182     {
183         return offset >= dy && x >= getBpp() ? buf[offset + x - dy - getBpp()]
184                 : 0;
185     }
186     
187     /**
188      * Simple helper to print out a buffer.
189      *
190      * @param raw The bytes to print out.
191      */

192     private static void dump(byte[] raw)
193     {
194         for (int i = 0; i < raw.length; i++)
195         {
196             System.out.print(raw[i] + " ");
197         }
198         System.out.println();
199     }
200
201     /**
202      * @return Returns the bpp.
203      */

204     public int getBpp()
205     {
206         return bpp;
207     }
208
209     /**
210      * @param newBpp
211      * The bpp to set.
212      */

213     public void setBpp(int newBpp)
214     {
215         bpp = newBpp;
216     }
217
218     /**
219      * @return Returns the height.
220      */

221     public int getHeight()
222     {
223         return height;
224     }
225
226     /**
227      * @param newHeight
228      * The height to set.
229      */

230     public void setHeight(int newHeight)
231     {
232         height = newHeight;
233     }
234
235     /**
236      * @return Returns the width.
237      */

238     public int getWidth()
239     {
240         return width;
241     }
242
243     /**
244      * @param newWidth
245      * The width to set.
246      */

247     public void setWidth(int newWidth)
248     {
249         this.width = newWidth;
250     }
251     
252
253     /**
254      * encode a byte array full of image data using the filter that this object
255      * implements.
256      *
257      * @param src
258      * buffer
259      * @param dest
260      * buffer
261      */

262     public void encode(byte[] src, byte[] dest)
263     {
264         checkBufsiz(dest, src);
265         int dy = getWidth()*getBpp();
266         for (int y = 0; y < height; y++)
267         {
268             int yoffset = y * dy;
269             encodeLine(src, dest, dy, yoffset, dy, yoffset);
270         }
271     }
272
273     /**
274      * decode a byte array full of image data using the filter that this object
275      * implements.
276      *
277      * @param src
278      * buffer
279      * @param dest
280      * buffer
281      */

282     public void decode(byte[] src, byte[] dest)
283     {
284         checkBufsiz(src, dest);
285         int dy = width * bpp;
286         for (int y = 0; y < height; y++)
287         {
288             int yoffset = y * dy;
289             decodeLine(src, dest, dy, yoffset, dy, yoffset);
290         }
291     }
292
293     /**
294      * @param predictor
295      * <ul>
296      * <li>1 No prediction (the default value)
297      * <li>2 TIFF Predictor 2
298      * <li>10 PNG prediction (on encoding, PNG None on all rows)
299      * <li>11 PNG prediction (on encoding, PNG Sub on all rows)
300      * <li>12 PNG prediction (on encoding, PNG Up on all rows)
301      * <li>13 PNG prediction (on encoding, PNG Average on all rows)
302      * <li>14 PNG prediction (on encoding, PNG Paeth on all rows)
303      * <li>15 PNG prediction (on encoding, PNG optimum)
304      * </ul>
305      *
306      * @return The predictor class based on the predictor code.
307      */

308     public static PredictorAlgorithm getFilter(int predictor)
309     {
310         PredictorAlgorithm filter;
311         switch (predictor)
312         {
313             case 10:
314                 filter = new None();
315                 break;
316             case 11:
317                 filter = new Sub();
318                 break;
319             case 12:
320                 filter = new Up();
321                 break;
322             case 13:
323                 filter = new Average();
324                 break;
325             case 14:
326                 filter = new Paeth();
327                 break;
328             case 15:
329                 filter = new Uptimum();
330                 break;
331             default:
332                 filter = new None();
333         }
334         return filter;
335     }
336 }
337
Popular Tags