Skip to content

Instantly share code, notes, and snippets.

@tianhaoz95
Last active February 27, 2020 04:14
Show Gist options
  • Save tianhaoz95/5b21d38630684366407ef5dadaa0afe6 to your computer and use it in GitHub Desktop.
Save tianhaoz95/5b21d38630684366407ef5dadaa0afe6 to your computer and use it in GitHub Desktop.
Simplified code for encoding byte messages into images
/// Steps for encoding the message
Uint16List expandedMsg = expandMsg(msg2bytes(msg));
Uint16List paddedMsg = padMsg(getEncoderCapacity(img), expandedMsg);
/// Helper function to convert string to bytes
Uint16List msg2bytes(String msg) {
return Uint16List.fromList(msg.codeUnits);
}
/// Helper function to expand byte messages to bit messages
Uint16List expandMsg(Uint16List msg) {
Uint16List expanded = Uint16List(msg.length * dataLength);
for (int i = 0; i < msg.length; ++i) {
int msgByte = msg[i];
for (int j = 0; j < dataLength; ++j) {
int lastBit = msgByte & 1;
expanded[i * dataLength + (dataLength - j - 1)] = lastBit;
msgByte = msgByte >> 1;
}
}
return expanded;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment