Problem looping through a 4D array

got it I think

press any key to toggle between two views:

  • debug view to see all permuts
  • real view of animated graphic

Explanation

the 3x4 grid holds 12 bits, 4096 permutations.

The number of bits that are 1 decide the brightness of the 3x4 grid. If more bits are ON, the grid appears more bright. So there are 12 levels of brightness.

Therefore I use these 12 brightness instead of 256 levels of brightness/gray scales. I use map() command to change from 255 to 12 scale.

The 12 brightnesses each have a different number of permuts (the different grids), between 1 and 220 different grids.

The animation loops between the different grids

I hope this helps.

Chrisir

// list uses gray scales as index. The list contains the
//different permuts for this gray scale (number of permuts can vary).
ListOfPermuts[] permuts = new ListOfPermuts[256];  // ONLY 12 used !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 

PixelRepresentation[][] myList;    // image !!!!!!!!

PImage img;

int state = 1; 

void setup() {
  size(1900, 800);
  background(0) ; 
  noSmooth();
  noStroke();
  frameRate(13);

  img = loadImage("process1.png"); // "fashion_MNist_01.png");
  img.resize (200, 200);

  // fill image grid
  myList = new PixelRepresentation[img.width][img.height];

  // fill permuts 
  for (int i=0; i<256; i++) {
    permuts[i] = new  ListOfPermuts( i );
  }//for
  fillpermuts () ;

  for (int x = 0; x < img.width; x++) {
    for (int y = 0; y < img.height; y++ ) {
      // Calculate the 1D pixel location
      int loc = x + y*img.width;
      color c1=img.pixels[loc]; 
      myList[x][y] = new PixelRepresentation (c1, x*5, y*5);
    }// for
  }// for
}// setup

void draw() {

  background(0) ; 

  switch(state) {
  case 0: 
    for (int x = 0; x < img.width; x++) {
      for (int y = 0; y < img.height; y++ ) {
        myList[x][y].display();
      }// for
    }// for
    break; 

  case 1: 
    // for debug / press any key to toggle 
    background(111); 
    for (int i=0; i<256; i++) {
      fill(0); 
      text(i+"   "+permuts[i].listOfPermutations.size(), 
        17, i*20+22);
      fill(255); 
      text("Permutations Overview.\nTable shows number of gray scale and the number of permutations for that number and the permuts as pixels. \nHit any key to toggly view", 
        140, 299);
      permuts[i].displayLong(i);
    }
    break;
  }//switch
}// draw

// ----------------------------------------------------------------

void keyPressed() {
  //toggle
  if (state==0) 
    state=1; 
  else 
  state=0;
}

// ----------------------------------------------------------------

void fillpermuts () {
  // solution by RichardDL
  int num;
  int count3 = 0;
  for (num  = 0; num < 4096; num++) {
    // RUN ALWAYS, the if is not working / is always true 
    //if (true||is3bits(num)) {
    count3++; 
    // println(String.format("%4d %s %4d", num, binary(num, 12), count3), count(num)  );
    // println(count(num));  
    permuts[count(num)].listOfPermutations.add  ( new Field ( binary(num, 12) ));
    //}//if
  }//for
  //
}

// ================================================================

class PixelRepresentation {

  // represents the entire image 
  color col;
  float x, y;
  int brightnessOnScale12 ; 

  PixelRepresentation (color col_, 
    float x_, float y_) {
    col=col_;
    brightnessOnScale12 = int(map(brightness(col), 0, 255, 0, 12)); 
    x=x_;
    y=y_;
  }

  void display() {
    permuts[brightnessOnScale12].display(x, y);
  }
}//class
//
//================================================================

class ListOfPermuts {

  // class holds all permutations for ONE brighntness 

  // length can vary (therefore ArrayList)
  ArrayList<Field> listOfPermutations = new ArrayList();

  // counter or permut for display as an animation
  int permutNum;

  //constr 
  ListOfPermuts (int brightnessOfField_) {
    //
  } // constr ---------

  void display(float x_, float y_) {
    // Animation 
    Field f1 = listOfPermutations.get(permutNum%listOfPermutations.size()); 
    f1.display(x_, y_);
    permutNum++;
    //
  } // func

  void displayLong(int i_) {
    int xoffset = 70;
    int yoffset = i_*20+20;

    // make a horizontal list - just to get an overview
    for (Field f1 : listOfPermutations) {
      f1.display(xoffset, yoffset); 
      xoffset += 20;
    }//for
  }//method
  //
} //class 

//================================================================

class Field {

  // ONE 3x4 Field 

  boolean [][] field = new boolean [3][4];  

  //constr 
  Field(String s1_) {

    // reads a String: binary data

    int counter=0; 
    // 
    for (int x = 0; x < field.length; x++) {
      for (int y = 0; y < field[0].length; y++ ) {
        field[x][y] = false;

        if (s1_.charAt(counter)=='1') {
          field[x][y] = true;
        }
        counter++;
      }
    }
  }//constr 

  void display( float  xoffset, float   yoffset) {
    //
    for (int x = 0; x < field.length; x++) {
      for (int y = 0; y < field[0].length; y++ ) {
        //
        fill(0);
        stroke(0); 
        if (field[x][y]) {
          fill(255); // WHITE
          stroke(255);
        }
        //rect(xoffset + x*5, yoffset +  y*5, 4, 4);
        point(xoffset +x, yoffset + y);
      }
    }
  }//func
  //
}//class

// ================================================================

// RichardDL's code for finding 12 bit binary numbers with 3 bits ON - function

//boolean is3bits(int no)
//{
//  // return true if given number has 3 bits set.
//  int ix;
//  int tot = 0;
//  for (ix = 0; ix < 12; ix++) {
//    if ((no & 1) == 1) tot++;
//    no = no >> 1;
//  }
//  return tot == 3;
//}

int count(int no)
{
  // return from  given number the  bits set Number
  int ix;
  int tot = 0;
  for (ix = 0; ix < 12; ix++)
  {
    if ((no & 1) == 1) {
      tot++;
    }
    no = no >> 1;
  }
  return tot ;
}
1 Like