Faster way of drawing a byte to the screen ?

edited June 2017 in Questions about Code

Hey guys! I've made a Program that basically emulates a 256x192 (monochrome)screen in processing. The byte-array, in which i store the pixel-data, has 6kb. (8pixel per byte, to a total of 6144bytes). 1 drawing cycle of the screen (if every pixel is a 1==white) takes the Program around 6 seconds.

So my question is, is there a faster way of drawing a byte to the screen than my approach?

Code of the drawing "algorithm":

for (int I=0; I<6144; I++){ int x,y = I/32; for(int X=0; X<32; X++) { x = X*8; // checks if the bit is == 1 and then draws the point if ( (DataIn[I] & 0x01) > 0) point(x,y); if ( (DataIn[I] & 0x02) > 0) point(x+1,y); if ( (DataIn[I] & 0x04) > 0) point(x+2,y); if ( (DataIn[I] & 0x08) > 0) point(x+3,y); if ( (DataIn[I] & 0x10) > 0) point(x+4,y); if ( (DataIn[I] & 0x20) > 0) point(x+5,y); if ( (DataIn[I] & 0x40) > 0) point(x+6,y); if ( (DataIn[I] & 0x80) > 0) point(x+7,y); } }

also sorry about my bad english

Answers

  • Answer ✓

    using pixels[] instead of point?

  • edited June 2017 Answer ✓
    byte[] DataIn = new byte[6144];
    void setup(){
      size(256,192);
      loadPixels();
    }
    void drawing(){
      for (int I=0, x=0; I<6144; I++, x+=8){
          for (int j=0; j<8; j++){
          pixels[x+j] = (DataIn[I] & (1 << j)) == 1 ? #ffffff : #000000;
          }
        }
      }
    
    void draw(){
      drawing();
      updatePixels();
    }
    
  • edited June 2017

    thanks alot! the whole display takes now ~2ms to render

  • edited June 2017 Answer ✓

    glad you got it to work but i made a big error in my previous code

    byte[] DataIn = new byte[6144];
    void setup(){
      size(256,192);
      loadPixels();
    }
    void drawing(){
      for (int I=0, x=0; I<6144; I++, x+=8){
          for (int j=0; j<8; j++){
          pixels[x+j] = ((char(DataIn[I]) & (0x80 >> j)) > 0 ) ? #ffffff : #000000;
          }
        }
      }
    byte ubyte(int i){
      if(i>=128){i-=256;}
      return byte(i);
    }
    void on(int x, int y){
      DataIn[x/8+y*32]|=(0x80>>x%8);
    }
    void draw(){
      on(constrain(mouseX,0,width),constrain(mouseY-10,0,height));
      drawing();
      updatePixels();
    }
    
Sign In or Register to comment.