package a2geek.apple2.image.encoder.encode; import a2geek.apple2.image.encoder.A2Image; /** * PackBits. The following text is from Wikipedia: *

* PackBits is a fast, simple compression scheme for run-length encoding of data. *

* Apple introduced the PackBits format with the release of MacPaint on the Macintosh computer. * This compression scheme is one of the types of compression that can be used in TIFF-files. *

* A PackBits data stream consists of packets of one byte of header followed by data. The header * is a signed byte; the data can be signed, unsigned, or packed (such as MacPaint pixels). *

* * * * * *
Header byteData
0 to 1271 + n literal bytes of data
0 to -127One byte of data, repeated 1 - n times in the decompressed output
-128No operation
*

* Note that interpreting 0 as positive or negative makes no difference in the output. Runs of * two bytes adjacent to non-runs are typically written as literal data. *

* Looking at the bits, setup is:
* * * * *
%0nnn nnnn1 + n literal bytes of data
%1nnn nnnnOne byte of data repeated 1 - n times.
%1000 0000No operation (used to flag END OF FILE; this is a variance)
* * @author a2geek@users.noreply.github.com (sort of) * @see http://en.wikipedia.org/wiki/PackBits * @see http://developer.apple.com/technotes/tn/tn1023.html * @see http://web.archive.org/web/20080705155158/http://developer.apple.com/technotes/tn/tn1023.html */ public class PackBitsEncoder extends A2Encoder { public String getTitle() { return "PackBits (Apple)"; } public void encode(A2Image image, int maxSize) { byte[] data = image.getBytes(); encode(data, maxSize); } public void encode(byte[] data, int maxSize) { reset(maxSize); for (int i=0; i 2) { // Compress run of repeated bytes: addByte(- runSize + 1); addByte(b); i+= runSize - 1; } else { // Gather up nonrepeating bytes: int count=0; for (count=1; i+count 2) break; } addByte(count - 1); for (int k=0; k