~zjm/Moon3D

8431bad35d550f1efba80f0b36c712d29c42cc48 — Zack Michener 4 months ago 901d49d
add atkinson dithering
2 files changed, 28 insertions(+), 0 deletions(-)

M src/3d/dither.c
M src/3d/dither.h
M src/3d/dither.c => src/3d/dither.c +27 -0
@@ 12,6 12,7 @@ void Dither(Viewport frame)

	// Ordered(output, frame.width, frame.height);
	FloydSteinberg(output, frame.width, frame.height);
	// Atkinson(output, frame.width, frame.height);

	for (int y = 0; y < frame.height; y++) {
		for (int x = 0; x < frame.width; x++) {


@@ 42,6 43,32 @@ void FloydSteinberg(Color *output, int w, int h)
	}
}

void Atkinson(Color *output, int w, int h)
{
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			Color oldPixel = output[y*w + x];
			Color newPixel = ClosestColor(oldPixel);
			output[y*w + x] = newPixel;
			double error = oldPixel - newPixel;

			if (y < h-1 || x < w-1)
		        output[y*w+x+1] 	+= error * 1.f/8.f;
			if (y < h-1 || x < w-2)
		        output[y*w+x+2] 	+= error * 1.f/8.f;

		    if (y < h-1) {
		        output[(y+1)*w+x-1] += error * 1.f/8.f;
		        output[(y+1)*w+x] 	+= error * 1.f/8.f;
		        output[(y+1)*w+x+1] += error * 1.f/8.f;
		    }

		    if (y < h-2)
		        output[(y+2)*w+x] += error * 1.f/8.f;
		}
	}
}

void Ordered(Color *output, int w, int h)
{
	double map[4][4];

M src/3d/dither.h => src/3d/dither.h +1 -0
@@ 5,5 5,6 @@

void Dither(Viewport frame);
void FloydSteinberg(Color *output, int w, int h);
void Atkinson(Color *output, int w, int h);
void Ordered(Color *output, int w, int h);
Color ClosestColor(Color p);