Sign in to follow this  
Puyover

Sobel operator and 8 bit paletted image

Recommended Posts

Hi! I'm having few problem applying a Sobel filter to an image that I have loaded into the NDS (I'm programming it at libnds). I have to do this in indexed color (paletted 8 bit image). Here is the code I use:
void imageToSobel(u8* bmp, u16* palette, int len, int weight, int height) {
	int i, j;
	int x, y;
	u8 p1, p2, p3, p4, p6, p7, p8, p9;
	u8 R, G, B, gray;

	for(i = 0; i < len/2; i++) {
		R = palette[i] & 0x1F;
		G = (palette[i] >> 5) & 0x1F;
		B = (palette[i] >> 10) & 0x1F;
		gray = div32(30*R, 100) + div32(59*G, 100) + div32(11*B, 100);
		BG_PALETTE[i] = RGB15(gray, gray, gray);
	}
	
	for(i = 0; i < height; i++) {
		for(j = 0; j < weight; j++) {
			p1 = bmp[(i-1)*weight+(j-1)];
			p2 = bmp[(i-1)*weight+j];
			p3 = bmp[(i-1)*weight+(j+1)];
			p4 = bmp[i*weight+(j-1)];
			p6 = bmp[i*weight+(j+1)];
			p7 = bmp[(i+1)*weight+(j-1)];
			p8 = bmp[(i+1)*weight+j];
			p9 = bmp[(i+1)*weight+(j+1)];
			
			x = (p1+(p2+p2)+p3-p7-(p8+p8)-p9);
			y = (p3+(p6+p6)+p9-p1-(p4+p4)-p7);
			BG_GFX[i*weight+j] = sqrt32((x*x) + (y*y));
		}
	}
}

The first for loop converts the image to gray scale, and do it fine. But in the next for is the problem; it's seem that doesn't do correctly... I try the same code but loading a 16 bit image and works fine, so the algorithm is correct... Could somebody help me? I don't know where is the mistake :( Thank you! PD: Sorry about my bad English.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this