How to draw bitmap array in Processing?

Hi,

I think you are misleading here at this line :

pixels[i+BMP] = pixels[i];
  • Saying i + BMP makes no sense because you can’t add an integer (i) and an array of char.
  • On the right side of the assign statement, writing pixels[i] means that you are affecting the pixel it’s value which is useless.

It depends on the size of the bitmap image that is stored in your array but I assume that pixels are stored linearly (as pixels) so you can say :

pixels[i] = color(BMP[i]);

This means that we are creating a color with the char value (0 means black and 255 means white) at the i location then assigning it to the pixels of the screen.

This is the output :

void setup() {
  size(128, 64);
  
  println(BMP.length);
}

void draw() {
  background(0);
  
  loadPixels();
  for (int i = 0; i < 1024; i++) {
    pixels[i] = color(BMP[i]);
  }
  updatePixels();
  
  noLoop();
} 

Capture d’écran de 2020-06-15 01-21-59
(Note that the minimal size of a Processing window is around 100px on each directions so that’s why there’s some grey bands…)

But the image looks crunched, to scale it you can use a PImage to render it then scale it :

PImage bitmap;

void setup() {
  // 10x resolution
  size(1280, 640);
  
  // Create a PImage with RGB colors
  bitmap = createImage(128, 64, RGB);
}

void draw() {
  background(0);
  
  bitmap.loadPixels();
  for (int i = 0; i < 1024; i++) {
    bitmap.pixels[i] = color(BMP[i]);
  }
  bitmap.updatePixels();
  
  // Scale then display the image (scale more on y axis)
  scale(10, 80);
  image(bitmap, 0, 0);
  
  noLoop();
} 

Java syntax is close to C type languages so it’s not difficult to adapt sometimes. The big difference is that it’s object oriented.

Hope it helps! :wink:

3 Likes