Implements different PNG predictor algorithms that is used in PDF files.
Method from org.apache.pdfbox.pdmodel.graphics.predictor.PredictorAlgorithm Detail: |
public int aboveLeftPixel(byte[] buf,
int offset,
int dy,
int x) {
return offset >= dy && x >= getBpp() ? buf[offset + x - dy - getBpp()]
: 0;
}
Get the above-left pixel from the buffer. |
public int abovePixel(byte[] buf,
int offset,
int dy,
int x) {
return offset >= dy ? buf[offset + x - dy] : 0;
}
Get the above pixel from the buffer. |
public void checkBufsiz(byte[] src,
byte[] dest) {
if (src.length != dest.length)
{
throw new IllegalArgumentException("src.length != dest.length");
}
if (src.length != getWidth() * getHeight() * getBpp())
{
throw new IllegalArgumentException(
"src.length != width * height * bpp");
}
}
check that buffer sizes matches width,height,bpp. This implementation is
used by most of the filters, but not Uptimum. |
public void decode(byte[] src,
byte[] dest) {
checkBufsiz(src, dest);
int dy = width * bpp;
for (int y = 0; y < height; y++)
{
int yoffset = y * dy;
decodeLine(src, dest, dy, yoffset, dy, yoffset);
}
}
decode a byte array full of image data using the filter that this object
implements. |
abstract public void decodeLine(byte[] src,
byte[] dest,
int srcDy,
int srcOffset,
int destDy,
int destOffset)
decode line of pixel data in src from src_offset and width*bpp bytes
forward, put the decoded bytes into dest. |
public void encode(byte[] src,
byte[] dest) {
checkBufsiz(dest, src);
int dy = getWidth()*getBpp();
for (int y = 0; y < height; y++)
{
int yoffset = y * dy;
encodeLine(src, dest, dy, yoffset, dy, yoffset);
}
}
encode a byte array full of image data using the filter that this object
implements. |
abstract public void encodeLine(byte[] src,
byte[] dest,
int srcDy,
int srcOffset,
int destDy,
int destOffset)
encode line of pixel data in src from srcOffset and width*bpp bytes
forward, put the decoded bytes into dest. |
public int getBpp() {
return bpp;
}
|
public static PredictorAlgorithm getFilter(int predictor) {
PredictorAlgorithm filter;
switch (predictor)
{
case 10:
filter = new None();
break;
case 11:
filter = new Sub();
break;
case 12:
filter = new Up();
break;
case 13:
filter = new Average();
break;
case 14:
filter = new Paeth();
break;
case 15:
filter = new Optimum();
break;
default:
filter = new None();
}
return filter;
}
|
public int getHeight() {
return height;
}
|
public int getWidth() {
return width;
}
|
public int leftPixel(byte[] buf,
int offset,
int dy,
int x) {
return x >= getBpp() ? buf[offset + x - getBpp()] : 0;
}
Get the left pixel from the buffer. |
public static void main(String[] args) {
Random rnd = new Random();
int width = 5;
int height = 5;
int bpp = 3;
byte[] raw = new byte[width * height * bpp];
rnd.nextBytes(raw);
System.out.println("raw: ");
dump(raw);
for (int i = 10; i < 15; i++)
{
byte[] decoded = new byte[width * height * bpp];
byte[] encoded = new byte[width * height * bpp];
PredictorAlgorithm filter = PredictorAlgorithm.getFilter(i);
filter.setWidth(width);
filter.setHeight(height);
filter.setBpp(bpp);
filter.encode(raw, encoded);
filter.decode(encoded, decoded);
System.out.println(filter.getClass().getName());
dump(decoded);
}
}
Simple command line program to test the algorithm. |
public void setBpp(int newBpp) {
bpp = newBpp;
}
|
public void setHeight(int newHeight) {
height = newHeight;
}
|
public void setWidth(int newWidth) {
this.width = newWidth;
}
|