I want to create down-scaled Bitmaps ( original Bitmap loaded from asset) with high quality comparable to the quality of bitmaps scaled with professional software like Paint.net (in Paint.NET the algorithm for scaling is choosen in a field called Interpolation. The highest setting uses supersampling)?
I understand how to implement supersampling for anti-aliasing. For anti-aliasing-purpose the original image is rendered in higher resolution and then gets downsampled. For Example to get a target image of 100x100 you would render the scene to 200x200 and then downsampling it with a 2x2 grid.
But, how the algorithm can handle downsampling from let's say 400x400 to 175x175 for scaling-purpose. The grid must be ~ 2.285x2.285 in this case. So how can supersampling be implemented for scaling-purpose?
thx
EDIT: My current algorithm looks like this:
private Bitmap downscale(Bitmap src, int targetWidth, int targetHeight){
Bitmap target = Bitmap.createBitmap(targetWidth, targetHeight, Config.ARGB_8888);
float w = src.getWidth()/(float)targetWidth;
float s = src.getHeight()/(float)targetHeight;
int color = 0;
int g = 0;
for(int i=0;i<target.getWidth();++i){
for(int j=0;j<target.getHeight();++j){
color = 0;
g = 0;
for(float k =i*w;k<roundUp((i+1)*w);++k){
for(float l=j*w;l<roundUp((j+1)*s);++l){
++g;
color+=src.getPixel((int)k, (int)l);
}
}
target.setPixel(i, j, color/g);
}
}
return target;
}
The image shows the same 100x100 bitmap scaled to 54x54. The left one is scaled by Paint.Net and the right one by my algorithm. Doesn't look good... How can I improve my code?
To calculate the average color you have to calculate each a-,r-,g-,b-value seperately
I changed my code and it works pretty good now.