I’m working on a project where I’ve been asked to work with 48bit (16bits per channel RGB) .PNG files. I’ll need to open, read and manipulate pixel data, and save to 48bit .PNG. Is this possible with Processing 4?
Hi and welcome
Here is some links
https://docs.oracle.com/javase/8/docs/api/java/awt/image/BufferedImage.html#TYPE_USHORT_565_RGB
1 Like
Thanks for this. Just incase folks are wondering how to get up and running, here’s what I was able to come up with:
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
void setup() {
String path = "/absolute/path/to/your/file/";
File inputFile = new File(path+"test.png");
File outputFile = new File(path+"saved.png");
ColorModel cm = null;
WritableRaster wr = null;
BufferedImage img = null;
// open a file
try {
img = ImageIO.read(inputFile);
}
catch (IOException e) {
e.printStackTrace();
}
// print what we got and hang on to the ColorModel and Raster
if (img != null) {
println("Image Type: "+ img.getType());
println(img.toString());
println("width: "+img.getWidth()+", height: "+img.getHeight());
cm = img.getColorModel();
wr = img.getRaster();
//mess with the pixels
int[] pixel = new int[3], sample = new int[3];
for (int y = 0; y < img.getHeight(); y++) {
for (int x = 0; x < img.getWidth(); x++) {
wr.getPixel(x, y, sample);
pixel[0] = sample[1];
pixel[1] = int(pow(2, 16) - sample[2]);
pixel[2] = sample[0];
wr.setPixel(x, y, pixel);
}
}
//create new BufferedImage and set it to the pixels we messed with
BufferedImage output = new BufferedImage(cm, wr, false, null);
//write the BufferedImage to a new file
try {
ImageIO.write(output, "png", outputFile);
}
catch (IOException e) {
}
println(cm.toString());
println(cm.getComponentSize());
}
exit();
}
3 Likes