We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Array Maximums (Read 652 times)
Array Maximums
Sep 24th, 2009, 6:24am
 
Hey Guys,

So I am trying to make a sketch that outputs an html file. The sketch takes an image and converts it to a series of div block elements, one div for each different coloured pixel section of the image. The program works fine for small images. Takes no time at all. However, when I start using images around about 200x200 pixels it grinds the program to a halt nearly. And anything bigger than that doesnt get past the initialisation stage. I think it has something to do with the amount of memory/max size of an array.. Is there any way to increase this? or should I create multiple arrays to handle my data and recollate it at the end? perhaps use a class somewhere? any suggestions.. source code is below:

Code:
String output = "";
PImage inputImage;
String hexData[] = {};
int colourLengths[] = {};
String combinedArray[] = {};
int x, y;
int colourLength = 1;
int addition;
char colon = ':';
char semi = ';';
char lsquig = '{';
char rsquig = '}';
String fileName = "index.html";

void setup() {
 inputImage = loadImage("test.png"); //image to be converted
 size(inputImage.width,inputImage.height);
 image(inputImage,0,0);
 InitialData();
 println("Initialised");
 CreateArray();
 println("Arrays Created");
 FindLengths();
 println("Found div Lengths");
 CombineArrays();
 println("Arrays Combined");
 CreateData();
 println("divs Created");
 EndData();
 println("Preping for file write");
 WriteFile();
 println("COMPLETE");
}

void draw(){

}

void InitialData(){ //creates the heading part of the html document and the start of the container div
String headData;
headData = "<html><head><style>.box "+lsquig+"height"+colon+"1px"+semi+"overflow"+colon+"hidden"+semi+"float"+colon+"left"+semi+rsquig+"</style></head><body>";
output = headData + "<div id=\"container\" style=\"width"+ colon + inputImage.width + semi +" height" + colon + inputImage.height +"px" + semi+"\">"; //create opening container
}

void CreateArray(){
 for(y=0; y<inputImage.height;y++){
  for(x=0; x<inputImage.width;x++){
   color Colour = get(x,y); //find the colour of the pixel
   String hexColour = hex(Colour); //convert to hexidecimal
   hexColour = hexColour.substring(2); //delete the first two Alpha chanel bits of the hex colour
   hexData = append(hexData, hexColour); //write the hexcode to the array
  }
 hexData = append(hexData, " "); //creates a break at the end of each line
 }
}

void FindLengths(){ //creates an array of colour widths to save on div numbers
  for(int i = 0; i < hexData.length-1; i++){
    if(hexData[i].equals(hexData[i+1]) == true){ //compare values if same add 1 to colourLength
     colourLength++;
    } else if (colourLength != 1){ //when they are no longer the same add the colourLength to the array
     colourLengths = append(colourLengths, colourLength);
     colourLength = 1; //reset colourLength to 1
    } else {
      colourLengths = append(colourLengths, colourLength); //if the length is only 1
    }
  }
  for(int i = 0; i < colourLengths.length; i++){ //adds all lengths to find last length
   addition = addition + colourLengths[i];
  }
  colourLengths = append(colourLengths, hexData.length - addition); //finds last length
}

void CombineArrays(){ //creates an array that combines the colour and length arrays
 int placement = 0;
 for(int i = 0; i < colourLengths.length; i++){
   placement = placement + colourLengths[i]; //finds the beginning of each colour
   combinedArray = append(combinedArray, hexData[placement-1]); //adds the colours data
   if (hexData[placement-1].equals(" ") == true){ //finds the end line breaks
   }else {
   combinedArray = append(combinedArray, str(colourLengths[i])); //adds the length data
   }
 }
}

void CreateData(){ //creates the html code for each div box
 for(int i = 0; i < combinedArray.length;){
   if(combinedArray[i].equals(" ") == true){
    i= i-1; //if end of line dont count as half a count
   } else {
   String divBox;
   divBox = "<div class=\"box\" style=\"background-color" + colon + "#" + combinedArray[i] + semi +" width"+colon + combinedArray[i+1] + "px" +semi+"\"></div>";
   output = output + divBox;
   }
  i=i+2;
 }
}

void EndData(){//adds the end of html document data
output = output + "</div></body></html>";
}

void WriteFile(){ //write to the html document
 String[] saveOutput = {output};
 saveStrings(fileName, saveOutput);
}


Thanks Guys!
Re: Array Maximums
Reply #1 - Sep 24th, 2009, 6:34am
 
Perhaps PDE > File > Preferences > Increase maximum available memory to...
Re: Array Maximums
Reply #2 - Sep 25th, 2009, 5:53am
 
Awesome Cheesy now set to much higher.. thanks so much.. dont know why I didnt think to look there in the first place..

I think its still just a case of as Ace Ventura would put it.. "If I'm not back in 5 minutes.... Just wait longer..."
Page Index Toggle Pages: 1