return closest;
}
private void DitherDirection(BufferedImage img,int y,C3[] error,C3[] nexterror,int direction) throws IOException {
int w = stepw;
C3 oldPixel = new C3(0,0,0);
C3 newPixel = new C3(0,0,0);
C3 quant_error = new C3(0,0,0);
int start, end, x;
for(x=0;x<w;++x) nexterror[x].set(0,0,0);
if(direction>0) {
start=0;
end=w;
} else {
start=w-1;
end=-1;
}
// @TODO: make this a parameter
boolean draw_filled=false;
// for each x from left to right
for(x=start;x!=end;x+=direction) {
// oldpixel := pixel[x][y]
//oldPixel.set( new C3(img.getRGB(x, y)).add(error[x]) );
oldPixel.set( new C3(TakeImageSampleBlock(img,x*step4,y*step4,x*step4+step4,y*step4+step4)).add(error[x]) );
// newpixel := find_closest_palette_color(oldpixel)
newPixel = QuantizeColor(oldPixel);
// pixel[x][y] := newpixel
if(newPixel.diff(palette[palette_mask])==0) {
// draw a circle. the diameter is relative to the intensity.
if(draw_filled) {
MoveTo(out,x*step4+step2-step2,y*step4+step2-step2,true);
MoveTo(out,x*step4+step2+step2,y*step4+step2-step2,false);
MoveTo(out,x*step4+step2+step2,y*step4+step2+step2,false);
MoveTo(out,x*step4+step2-step2,y*step4+step2+step2,false);
MoveTo(out,x*step4+step2-step2,y*step4+step2-step2,false);
MoveTo(out,x*step4+step2+step1,y*step4+step2-step1,false);
MoveTo(out,x*step4+step2+step1,y*step4+step2+step1,false);
MoveTo(out,x*step4+step2-step1,y*step4+step2+step1,false);
MoveTo(out,x*step4+step2-step1,y*step4+step2-step1,false);
MoveTo(out,x*step4+step2 ,y*step4+step2 ,false);
MoveTo(out,x*step4+step2 ,y*step4+step2 ,true);
} else {
MoveTo(out,x*step4+step2-step1,y*step4+step2-step1,true);
MoveTo(out,x*step4+step2+step1,y*step4+step2-step1,false);
MoveTo(out,x*step4+step2+step1,y*step4+step2+step1,false);
MoveTo(out,x*step4+step2-step1,y*step4+step2+step1,false);
MoveTo(out,x*step4+step2-step1,y*step4+step2-step1,false);
MoveTo(out,x*step4+step2-step1,y*step4+step2-step1,true);
}
}
// quant_error := oldpixel - newpixel
quant_error.set( oldPixel.sub( newPixel ) );
// pixel[x+1][y ] += 7/16 * quant_error
// pixel[x-1][y+1] += 3/16 * quant_error
// pixel[x ][y+1] += 5/16 * quant_error
// pixel[x+1][y+1] += 1/16 * quant_error
nexterror[x ].add(quant_error.mul(5.0/16.0));
if(x+direction>=0 && x+direction < w) {
error[x+direction].add(quant_error.mul(7.0/16.0));
nexterror[x+direction].add(quant_error.mul(1.0/16.0));
}
if(x-direction>=0 && x-direction < w) {
nexterror[x-direction].add(quant_error.mul(3.0/16.0));
}
}
}