Skip to content

Instantly share code, notes, and snippets.

@lite1979
Created October 10, 2016 20:28
Show Gist options
  • Save lite1979/cdda9842248413bd690189a8461a543e to your computer and use it in GitHub Desktop.
Save lite1979/cdda9842248413bd690189a8461a543e to your computer and use it in GitHub Desktop.
private void appendStarSlices(ByteArrayBuilder builder) {
/* Loop through and convert format */
int widthBytes = (int)((getWidth() + 7) / 8);
int heightBytes = (int)((getHeight() + 7) / 8);
int x = 0;
int y = 0;
int bit = 0;
int byteVal = 0;
boolean[] imgData = getImageAsBooleanArray();
StringBuilder b = new StringBuilder();
for (int i = 0; i < widthBytes * getHeight(); i++)
b.append(new char[0]);
while(true) {
byteVal |= (imgData[y * getWidth() + x] ? 1 : 0) << 7 - bit;
x++;
bit++;
if (x >= getWidth()) {
x = 0;
y++;
bit = 8;
if (y > getHeight()) {
builder.append(new byte[] { (byte)byteVal } );
break;
}
}
if (bit >= 8) {
builder.append(new byte[] { (byte)byteVal } );
byteVal = 0;
bit = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment