Happy to share my source
but it's all C#, so if you need it in another language better convert it. I'll comment it for you too just wait a minute
Edit:
Here you go
Code:
class SobelFilter
{
public float scale = 1.0f;
Bitmap b, b1;
public Image apply(Image im)
{
float dx; //Current x pixel
float dy; //Current y pixel
int[] pix = { 0, 0, 0 }; //Color of the pixel
float nx; //normal X
float ny; //normal Y
float nz; //normal Z
float length; //length
b = (Bitmap)im; //Source bitmap
Bitmap b1 = new Bitmap(im); //Target bitmap
for (int y = 1; y < b.Height - 1; y++) // loop for the image pixels height
{
for (int x = 1; x < b.Width - 1; x++) // loop for image pixels width
{
pix[0] = b.GetPixel((x - 1 + b.Width) % b.Width, (y + 1) % b.Height).R; //Get R value
pix[1] = b.GetPixel((x - 1 + b.Width) % b.Width, (y + 1) % b.Height).G; //Get G value
pix[2] = b.GetPixel((x - 1 + b.Width) % b.Width, (y + 1) % b.Height).B; //Get B value
dy = pix[0] / 225.0f * -1.0f; //Calculate value of R color
//Same here for other pixels
pix[0] = b.GetPixel(x % b.Width, (y + 1) % b.Height).R;
pix[1] = b.GetPixel(x % b.Width, (y + 1) % b.Height).G;
pix[2] = b.GetPixel(x % b.Width, (y + 1) % b.Height).B;
dy += pix[0] / 225.0f * -2.0f;
pix[0] = b.GetPixel((x + 1) % b.Width, (y + 1) % b.Height).R;
pix[1] = b.GetPixel((x + 1) % b.Width, (y + 1) % b.Height).G;
pix[2] = b.GetPixel((x + 1) % b.Width, (y + 1) % b.Height).B;
dy += pix[0] / 225.0f * -1.0f;
pix[0] = b.GetPixel((x - 1 + b.Width) % b.Width, (y - 1 + b.Height) % b.Height).R;
pix[1] = b.GetPixel((x + 1) % b.Width, (y + 1) % b.Height).G;
pix[2] = b.GetPixel((x + 1) % b.Width, (y + 1) % b.Height).B;
dy += pix[0] / 225.0f * 1.0f;
pix[0] = b.GetPixel(x % b.Width, (y - 1 + b.Height) % b.Height).R;
pix[1] = b.GetPixel(x % b.Width, (y - 1 + b.Height) % b.Height).G;
pix[2] = b.GetPixel(x % b.Width, (y - 1 + b.Height) % b.Height).B;
dy += pix[0] / 225.0f * 2.0f;
pix[0] = b.GetPixel((x + 1) % b.Width, (y - 1 + b.Height) % b.Height).R;
pix[1] = b.GetPixel(x % b.Width, (y - 1 + b.Height) % b.Height).G;
pix[2] = b.GetPixel(x % b.Width, (y - 1 + b.Height) % b.Height).B;
dy += pix[0] / 225.0f * 1.0f;
//x filter
pix[0] = b.GetPixel((x - 1 + b.Height) % b.Width, (y - 1 + b.Height) % b.Height).R;
pix[1] = b.GetPixel((x - 1 + b.Height) % b.Width, (y - 1 + b.Height) % b.Height).G;
pix[2] = b.GetPixel((x - 1 + b.Height) % b.Width, (y - 1 + b.Height) % b.Height).B;
dx = pix[0] / 225.0f * -1.0f;
pix[0] = b.GetPixel((x - 1 + b.Width) % b.Width, y % b.Width).R;
pix[1] = b.GetPixel((x - 1 + b.Width) % b.Width, y % b.Width).G;
pix[2] = b.GetPixel((x - 1 + b.Width) % b.Width, y % b.Width).B;
dx += pix[0] / 225.0f * -2.0f;
pix[0] = b.GetPixel((x - 1 + b.Width) % b.Width, (y + 1) % b.Height).R;
pix[1] = b.GetPixel((x - 1 + b.Width) % b.Width, (y + 1) % b.Height).G;
pix[2] = b.GetPixel((x - 1 + b.Width) % b.Width, (y + 1) % b.Height).B;
dx += pix[0] / 225.0f * -1.0f;
pix[0] = b.GetPixel((x + 1) % b.Width, (y - 1 + b.Height) % b.Height).R;
pix[1] = b.GetPixel((x + 1) % b.Width, (y - 1 + b.Height) % b.Height).G;
pix[2] = b.GetPixel((x + 1) % b.Width, (y - 1 + b.Height) % b.Height).B;
dx += pix[0] / 225.0f * 1.0f;
pix[0] = b.GetPixel((x + 1) % b.Width, y % b.Height).R;
pix[1] = b.GetPixel((x + 1) % b.Width, y % b.Height).G;
pix[2] = b.GetPixel((x + 1) % b.Width, y % b.Height).B;
dx += pix[0] / 225.0f * 2.0f;
pix[0] = b.GetPixel((x + 1) % b.Width, (y + 1) % b.Height).R;
pix[1] = b.GetPixel((x + 1) % b.Width, (y + 1) % b.Height).G;
pix[2] = b.GetPixel((x + 1) % b.Width, (y + 1) % b.Height).B;
dx += pix[0] / 225.0f * 1.0f;
//Cross product of values
nx = dx;
ny = dy;
nz = 1 / this.scale;
//Normalize
length = 1.0f;
nx *= length;
ny *= length;
nz *= length;
//convert to float
pix[0] = floatToByte(nx);
pix[1] = floatToByte(ny);
pix[2] = floatToByte(nz);
//make sure we have no negative values or values above 255
if (pix[0] > 255)
pix[0] = 255;
if (pix[1] > 255)
pix[1] = 255;
if (pix[2] > 255)
pix[2] = 255;
if (pix[0] < 0)
pix[0] = 0;
if (pix[1] < 0)
pix[1] = 0;
if (pix[2] < 0)
pix[2] = 0;
//create the color for the pixel
Color c = Color.FromArgb(pix[0],pix[1],pix[2]);
//write the color for the pixel
b1.SetPixel(x,y,c);
}
}
return (Image)b1; //return generated image
}
private int floatToByte(float inValue) {
return (int)((inValue + 1.0f) / 2.0f * 255.0f);
}
}