1 31 package org.pdfbox.pdmodel.graphics.predictor; 32 33 import java.util.Random ; 34 35 42 public abstract class PredictorAlgorithm 43 { 44 private int width; 45 46 private int height; 47 48 private int bpp; 49 50 57 public void checkBufsiz(byte[] src, byte[] dest) 58 { 59 if (src.length != dest.length) 60 { 61 throw new IllegalArgumentException ("src.length != dest.length"); 62 } 63 if (src.length != getWidth() * getHeight() * getBpp()) 64 { 65 throw new IllegalArgumentException ( 66 "src.length != width * height * bpp"); 67 } 68 } 69 70 87 public abstract void encodeLine(byte[] src, byte[] dest, int srcDy, 88 int srcOffset, int destDy, int destOffset); 89 90 107 public abstract void decodeLine(byte[] src, byte[] dest, int srcDy, 108 int srcOffset, int destDy, int destOffset); 109 110 115 public static void main(String [] args) 116 { 117 Random rnd = new Random (); 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 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 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 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 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 204 public int getBpp() 205 { 206 return bpp; 207 } 208 209 213 public void setBpp(int newBpp) 214 { 215 bpp = newBpp; 216 } 217 218 221 public int getHeight() 222 { 223 return height; 224 } 225 226 230 public void setHeight(int newHeight) 231 { 232 height = newHeight; 233 } 234 235 238 public int getWidth() 239 { 240 return width; 241 } 242 243 247 public void setWidth(int newWidth) 248 { 249 this.width = newWidth; 250 } 251 252 253 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 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 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 |