Skip to content

Instantly share code, notes, and snippets.

@kartben
Created June 4, 2012 22:17
Show Gist options
  • Save kartben/2871147 to your computer and use it in GitHub Desktop.
Save kartben/2871147 to your computer and use it in GitHub Desktop.
Convert BMP to 12-bit RGB values
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Main {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File f = new File("/Users/kartben/Downloads/gas.bmp");
BufferedImage image = ImageIO.read(f);
int i = 0;
short convertedPixel = 0;
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
int pixel = image.getRGB(x, y);
byte red = (byte) (((pixel >> 16) & 0xff) / 16);
byte green = (byte) (((pixel >> 8) & 0xff) / 16);
byte blue = (byte) (((pixel) & 0xff) / 16);
convertedPixel = (short) (red * 256 + green * 16 + blue);
System.out.printf("0x%02X, ", convertedPixel);
}
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment