# Working with 48bit image formats. How to load, process, save?

**URL:** <https://discourse.processing.org/t/working-with-48bit-image-formats-how-to-load-process-save/36652>\
**Category:** Coding Questions\
**Created:** [April 29, 2022, 9:26pm UTC](https://discourse.processing.org/t/working-with-48bit-image-formats-how-to-load-process-save/36652 "2022-04-29T21:26:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phillipstearns](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/phillipstearns/32/16140_2.png) [@phillipstearns](https://discourse.processing.org/u/phillipstearns)\
**Post date:** [April 29, 2022, 9:26pm UTC](https://discourse.processing.org/t/working-with-48bit-image-formats-how-to-load-process-save/36652/1 "2022-04-29T21:26:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [April 29, 2022, 10:56pm UTC](https://discourse.processing.org/t/working-with-48bit-image-formats-how-to-load-process-save/36652/2 "2022-04-29T22:56:27Z")

</div>

Hi and welcome

Here is some links

[https://docs.oracle.com/javase/8/docs/api/java/awt/image/BufferedImage.html#TYPE\_USHORT\_565\_RGB](https://docs.oracle.com/javase/8/docs/api/java/awt/image/BufferedImage.html#TYPE_USHORT_565_RGB)

> **[Java 48 Bit TIFF Image Processing](https://www.codeproject.com/Articles/456525/Java-48-Bit-TIFF-Image-Processing)**
>
> 48 Bit Image Processing using JAI(Java Advanced Imaging Library)

---

<div class="post-metadata">

**Author:** ![phillipstearns](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/phillipstearns/32/16140_2.png) [@phillipstearns](https://discourse.processing.org/u/phillipstearns)\
**Post date:** [April 30, 2022, 2:21am UTC](https://discourse.processing.org/t/working-with-48bit-image-formats-how-to-load-process-save/36652/3 "2022-04-30T02:21:46Z")

</div>

Thanks for this. Just incase folks are wondering how to get up and running, here’s what I was able to come up with:

```auto
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();
}

```
