Skip to content

Instantly share code, notes, and snippets.

@chrissearle
Created November 25, 2021 08:43
Show Gist options
  • Save chrissearle/05234866d50794e5d69a8172ebc82706 to your computer and use it in GitHub Desktop.
Save chrissearle/05234866d50794e5d69a8172ebc82706 to your computer and use it in GitHub Desktop.
Convert png to rgb565
public class Convert {
public static void main(String[] args) {
String pngFile = args[0];
String datFile = args[1];
try {
BufferedImage image = ImageIO.read(new File(pngFile));
try (
LittleEndianDataOutputStream outputStream = new LittleEndianDataOutputStream(new FileOutputStream(datFile))
) {
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
int pixel = image.getRGB(x, y);
short rgb565 = (short) (((pixel & 0xf80000) >> 8) | ((pixel & 0xfc00) >> 5) | ((pixel & 0xf8) >> 3));
outputStream.writeShort(rgb565);
}
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
fun main(args: Array<String>) {
val (pngFile, datFile) = args
try {
val image = ImageIO.read(File(pngFile))
LittleEndianDataOutputStream(FileOutputStream(datFile)).use { outputStream ->
for (y in 0 until image.height) {
for (x in 0 until image.width) {
val pixel = image.getRGB(x, y)
outputStream.writeShort(
((pixel and 0xf80000 shr 8) or
(pixel and 0xfc00 shr 5) or
(pixel and 0xf8 shr 3))
)
}
}
}
} catch (e: Exception) {
System.err.println(e.message)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment