java - Looping through image pixels is crashing my program -


i started work little image processing software. need set image black & white , loop through pixels , generate report of counter, coordinates , color of each pixel (#,x,y,color).

it works fine tiny images create test, when use real picture, takes several minutes or crash software. code inside loop seems simple crashing.

any tip on how can improve it? in advance!

void processimage(bufferedimage image) {         exportstr = "#,x,y,color"+ newline;               color c = new color(0);         int imgw = image.getwidth();         int imgh = image.getheight();         matrix = new int[imgw][imgh];           int currentpx = 1;          for(int x=0; x < imgw; x++)          {             for(int y=0; y < imgh; y++)              {                 c = new color(image.getrgb(x, y));                  if(c.equals(color.white))                 {                      matrix[x][y] = 1;                                    }                  string color = matrix[x][y]==1 ? "w" : "b";                 exportstr += formatstringexport(currentpx, x, y, color); // concatenate values                 currentpx++;             }         }          return img; } 

this problem

exportstr += formatstringexport(currentpx, x, y, color); 

use stringbuilder instead:

stringbuilder sb = new stringbuilder(imgw  * imgh * <string size per pixel>);  ....  sb.append(formatstringexport(currentpx, x, y, color)); 

check stringbuilder documentation details. additionally, try reduce number of objects being created. instance replace:

c = new color(image.getrgb(x, y));  string color = matrix[x][y]==1 ? "w" : "b"; 

by ...

if(image.getrgb(x, y).equals(...))  sb.append(formatstringexport(currentpx, x, y, (matrix[x][y]==1 ? "w" : "b"))); 

good luck! :)


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -