To use this library in your code:
import bitmapdest-byte = new-byte, dest-byte |= new-byte, dest-byte &= new-byte, dest-byte ^= new-byte, and dest-byte += new-byte respectively, where the addition is saturating and unsigned 8 bit, clamped to 0xff. ADD-16-LE treats the destination and the subsequent byte as a little-endian 16 bit integer, and does a saturating 16 bit addition, clamped to 0xffff.
// Byte-swap the 16 bit values in byte-array from little-endian
// to big-endian (or vice versa).
tmp := ByteArray byte-array.size
blit byte-array tmp[1..] byte-array.size / 2 --source-pixel-stride=2 --destination-pixel-stride=2
blit byte-array[1..] tmp byte-array.size / 2 --source-pixel-stride=2 --destination-pixel-stride=2
byte-array.replace 0 tmp
// Take three byte-arrays of red, green, and blue pixels, and create a
// single byte-array with the pixels interleaved in r, g, b order.
output := ByteArray red.size * 3
blit red output red.size --destination-pixel-stride=3
blit green output[1..] red.size --destination-pixel-stride=3
blit blue output[2..] red.size --destination-pixel-stride=3
// Extract the red pixels from a 30x20 square in a 100x100 rgb-interleaved 24
// bit image at position (42,13)
x := 42
y := 13
w := 30
h := 20
red-extract := ByteArray: w * h
first-pixel := (x + 100 * y) * 3
blit image[first-pixel..] red-extract w --source-pixel-stride=3 --source-line-stride=300
REVERSED ::= ByteArray 0x100: 0
| (it & 0x01) << 7
| (it & 0x02) << 5
| (it & 0x04) << 3
| (it & 0x08) << 1
| (it & 0x10) >> 1
| (it & 0x20) >> 3
| (it & 0x40) >> 5
| (it & 0x80) >> 7
/// Reverse the bits in each byte.
reverse byte-array/ByteArray -> none:
lut byte-array byte-array REVERSED
ROT13 ::= ByteArray 0x100:
result := it
if 'n' <= it <= 'z' or 'N' <= it <= 'Z': result -= 13
else if 'a' <= it <= 'm' or 'A' <= it <= 'M': result += 13
result // Final value in the block is used to initialize the ByteArray.
/// Rot13 encode/decode a string.
rot13 str/string -> string:
byte-array := ByteArray: str.size
lut byte-array str ROT13
return byte-array.to-string