Bug related to PImage::copy() of images with transparency?

Hello all I’d like to report what seems to be a bug related to PImage::copy() of images with transparency.

There’s an easy hack-fix and I don’t have time to further investigate so I’m just posting this in case this is somehow meant to work that way or in case others have a similar issue.

Background:
I start with a PImage object where every pixel is transparent. I draw a simple grid with rect() and the PImage on top of it. Since the PImage it’s transparent you can see the grid (fig.1).
image
fig.1

I can then add images (with or without transparency) to this PImage to specific coordinates, which I do with:

  void addImageGrid(PImage newImg, int x, int y, int w, int h) {
    if (x < 0 || y < 0 || x >= this.gridX || y >= this.gridY) {
      println("ERROR: addImageGrid coordinate out of range");
      return;
    }
    if (w < 1 || h < 1 || x + w > this.gridX || y + h > this.gridY) {
      println("ERROR: addImageGrid coordinate our of range");
      return;
    }
    this.img.copy(newImg, 0, 0, newImg.width, newImg.height,
      round(this.img.width * (float(x) / this.gridX)),
      round(this.img.height * (float(y) / this.gridY)),
      w * (this.img.width / this.gridX), h * (this.img.height / this.gridY));
  }

For example I can add the same image of a bush to many grid coordinates (fig.2).
image

Problem:
If I add an image with transparency to the 0, 0 coordinate all transparency is lost in the PImage (fig.3).
image

Workaround
This seems to be an issue with the pixel at index 0 so if I skip that pixel it works fine.

I assume if I wrote my own PImage::copy() function I could avoid this issue so I think it is a bug in PImage::copy(), however I haven’t had time to test that theory. If I do I’ll post the results!

1 Like

Hello @yodan,

This is a related topic:

https://processing.org/reference/PImage_copy_.html states:

No alpha information is used in the process, however if the source image has an alpha channel set, it will be copied as well.

Another topic that is related:
https://discourse.processing.org/t/pixel-processing-grabbing-the-canvas-and-wheres-the-alpha/7162

:)

1 Like