How Does Unity Create Normal Maps from Grayscale?

The question is all in the title, whenever you import textures into Unity and set the texture type to normal map, you will be given the option to create a normal map from grayscale. Anybody have any idea on how this works and how to write virtually the same functionality to be executed at runtime?

Found some code that works very well:

for (y=0; y < bumpTexture.height; y++) {
    for (x=0; x < bumpTexture.width; x++) {
             xLeft = bumpSource.GetPixel(x-1,y).grayscale;
             xRight = bumpSource.GetPixel(x+1,y).grayscale;
             yUp = bumpSource.GetPixel(x,y-1).grayscale;
             yDown = bumpSource.GetPixel(x,y+1).grayscale;
             xDelta = ((xLeft-xRight)+1)*0.5;
             yDelta = ((yUp-yDown)+1)*0.5;
        bumpTexture.SetPixel(x,y,new Color(xDelta,yDelta,1.0,1.0));



Python Version:

normalmap = np.ones(shape=(disp.shape[0], disp.shape[1], 3), dtype=np.float32)
h, w = disp.shape
scale = 6
normalmap[:,:,2] = (np.pad(disp[:,:w-2] - disp[:,2:], ((0,0),(1,1)))*scale +1)*0.5
normalmap[:,:,1] = 1.0 - (np.pad( disp[0:h-2,:] - disp[2:,:], ((1,1),(0,0)))*scale +1)*0.5

normalmap = normalmap*255