J
jw
Hi All,
I've got one idea I'm currently trying to get this code to better use
its int arrays, but as written below the system runs out of memory
after about 6 hours of crunching. The code below is in a loop called
hundreds of thousands of times, and I'm 97% sure the imgdata int array
is not getting properly cleaned up. While I'm waiting to see if my
idea fixed the issue I thought I'd ask if anyone here has a good idea.
This code splits up a large image into smaller images and saves each
subimage. The problem code is designed to inspect every 3rd pixel of
an image and if they are all the same, we assume the image is just a
solid color so instead of saving the entire image, we check a cache of
1x1 images for a 1x1 image of the matching color or else we scale it
to 1x1, put it into the cache and in either case and we set it up to
save the 1x1 image further below. When I commented out the code that
states "//begin area of code that is the issue" the system did not run
out of memory. Anyway, an alternate question is: Is there a better way
to detect if the image is just a solid color?
Thanks in advance for your help!
BufferedImage subImage;
ByteArrayOutputStream baos;
int[] imgdata; //variable that is the issue
for(int i = 0; i < w.tcnt; i++) {
for (int j = 0; j < w.tcnt; j++) {
//begin area of code that is the issue
subImage = w.img.getSubimage(i * tilesize, j * tilesize, tilesize,
tilesize);
imgdata =
((DataBufferInt)subImage.getRaster().getDataBuffer()).getData();
if(imgdata.length>0) {
int firstpixel = imgdata[0];
int length = imgdata.length;
while((length=length-3)>0) {
if(firstpixel!=imgdata[length]) {
break;
}
}
if(length<4) {
if(images1x1.containsKey(firstpixel)) {
subImage = images1x1.get(firstpixel);
}
else {
subImage = subImage.getSubimage(0,0,1,1);
images1x1.put(firstpixel, subImage);
}
}
}
//end code that is the issue
baos = new ByteArrayOutputStream();
try {
ImageIO.write(subImage, "PNG", baos);
//internal call to save the image
} catch (IOException e) {
System.err.println("Error converting tile!");
}
}
}
subImage = null;
baos = null;
imgdata = null;
w.img = null;
I've got one idea I'm currently trying to get this code to better use
its int arrays, but as written below the system runs out of memory
after about 6 hours of crunching. The code below is in a loop called
hundreds of thousands of times, and I'm 97% sure the imgdata int array
is not getting properly cleaned up. While I'm waiting to see if my
idea fixed the issue I thought I'd ask if anyone here has a good idea.
This code splits up a large image into smaller images and saves each
subimage. The problem code is designed to inspect every 3rd pixel of
an image and if they are all the same, we assume the image is just a
solid color so instead of saving the entire image, we check a cache of
1x1 images for a 1x1 image of the matching color or else we scale it
to 1x1, put it into the cache and in either case and we set it up to
save the 1x1 image further below. When I commented out the code that
states "//begin area of code that is the issue" the system did not run
out of memory. Anyway, an alternate question is: Is there a better way
to detect if the image is just a solid color?
Thanks in advance for your help!
BufferedImage subImage;
ByteArrayOutputStream baos;
int[] imgdata; //variable that is the issue
for(int i = 0; i < w.tcnt; i++) {
for (int j = 0; j < w.tcnt; j++) {
//begin area of code that is the issue
subImage = w.img.getSubimage(i * tilesize, j * tilesize, tilesize,
tilesize);
imgdata =
((DataBufferInt)subImage.getRaster().getDataBuffer()).getData();
if(imgdata.length>0) {
int firstpixel = imgdata[0];
int length = imgdata.length;
while((length=length-3)>0) {
if(firstpixel!=imgdata[length]) {
break;
}
}
if(length<4) {
if(images1x1.containsKey(firstpixel)) {
subImage = images1x1.get(firstpixel);
}
else {
subImage = subImage.getSubimage(0,0,1,1);
images1x1.put(firstpixel, subImage);
}
}
}
//end code that is the issue
baos = new ByteArrayOutputStream();
try {
ImageIO.write(subImage, "PNG", baos);
//internal call to save the image
} catch (IOException e) {
System.err.println("Error converting tile!");
}
}
}
subImage = null;
baos = null;
imgdata = null;
w.img = null;