You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.9 KiB
49 lines
1.9 KiB
package com.jotuntech.sketcher.common;
|
|
|
|
import java.util.zip.DataFormatException;
|
|
import java.util.zip.Inflater;
|
|
|
|
public class DiffUnpacker {
|
|
Inflater inflater;
|
|
private byte[] encodedDiff;
|
|
|
|
public DiffUnpacker() {
|
|
inflater = new Inflater(true);
|
|
}
|
|
|
|
public void unpack(byte[] input, int ofs, int len, short[] outputAlpha, short[] outputRed, short[] outputGreen, short[] outputBlue) {
|
|
if(encodedDiff == null || encodedDiff.length < input.length * 8) {
|
|
encodedDiff = new byte[outputAlpha.length * 8];
|
|
}
|
|
|
|
inflater.reset();
|
|
inflater.setInput(input, ofs, len);
|
|
|
|
int encodedLength = 0;
|
|
try {
|
|
while(!inflater.finished() && encodedLength < encodedDiff.length) {
|
|
encodedLength += inflater.inflate(encodedDiff, encodedLength, encodedDiff.length - encodedLength);
|
|
}
|
|
} catch(DataFormatException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
|
|
if(encodedLength < outputAlpha.length * 8) {
|
|
Log.error("needsInput = " + inflater.needsInput());
|
|
throw new RuntimeException("Encoded length " + encodedLength + " is shorter than output length " + outputAlpha.length * 8 + ".");
|
|
}
|
|
|
|
short alpha = 0, red = 0, green = 0, blue = 0;
|
|
for(int outputOffset = 0, alphaOffset = 0, redOffset = outputAlpha.length * 2, greenOffset = outputAlpha.length * 4, blueOffset = outputAlpha.length * 6; outputOffset < outputAlpha.length; outputOffset++, alphaOffset++, redOffset++, greenOffset++, blueOffset++) {
|
|
alpha += (short) ((encodedDiff[alphaOffset++] << 8) | (encodedDiff[alphaOffset] & 0xff));
|
|
red += (short) ((encodedDiff[redOffset++] << 8) | (encodedDiff[redOffset] & 0xff));
|
|
green += (short) ((encodedDiff[greenOffset++] << 8) | (encodedDiff[greenOffset] & 0xff));
|
|
blue += (short) ((encodedDiff[blueOffset++] << 8) | (encodedDiff[blueOffset] & 0xff));
|
|
|
|
outputAlpha[outputOffset] = alpha;
|
|
outputRed[outputOffset] = red;
|
|
outputGreen[outputOffset] = green;
|
|
outputBlue[outputOffset] = blue;
|
|
}
|
|
}
|
|
}
|
|
|