Please see my comment to the question.
Here is the problem: even though you can create an image with any predefined gradient, easily,
you cannot generally ask a question "what is a gradient of a given image?". It takes some understanding, what a gradient is. A gradient cannot be a property of an image. It is a property of some class of functions (
differentiable at least in some points) at
some given point of the function domain. In case of images, this is a property of one given location. Moreover, such property may or may not exist. First of all, the bitmap images are
discrete, they can be considered only as approximation of some smooth functions, and only in the vicinities of some points of the image, not at any arbitrary location.
To have such approximate gradient defined, you need some smooth function of color change is some reasonably big vicinity of some point (say, not one pixel). In this vicinity, you can build some smooth function by
interpolation of pixels colors. This function can have some partial derivatives of the colors on two directions. You should find out the direction where the derivative is maximum. The obtained vector will be the
gradient in the point. Naturally, in some other point the gradient will be different. And in the points of sharp contrast, the notion of gradient should be considered as not making real sense.
This is pretty well explained here, and for the images, too:
http://en.wikipedia.org/wiki/Gradient[
^].
From the equations shown, it's not too difficult to develop the algorithm for calculation of the gradient in a given point. It's basically reduced to the simple task of numeric computation of
partial derivatives by the discrete points in some point's vicinity.
Pay attention for the very first and especially the third picture in this article. They illustrate the cases when the gradient for the image cannot be defined, as it is different for every point of the image. The case when you could talk about "gradient of the image" is only the second picture, which happens to have uniform gradient оver the whole image.
—SA