/* Transform image pixels: p' = 255*log(p+1) */ void logTransform( image img ) { int r, c, p ; for ( r=0; r<img.nrows; r++ ) for ( c=0; c<img.ncols; c++ ) { p = getPixel( img, r, c ) + 1 ; p = 255*log((double)p) / log(256.0); setPixel( img, r, c, (unsigned char)p ); } }
Comments:
The function can be made faster by computing the common
factor 255/log(256.0)
only once, outside of the loop.
But an optimizing compiler will do this anyway so it does not hurt
to leave it were it is.
Another way to optimize is to realize that there are only 256 values being transformed, so the transformation can be implemented as an array
int transformed[256];
indexed from 0 to 255.
The array element transformed[p]
gives the value that gray level p
is mapped into.
The array can be initialized in a loop outside of the main image
scanning loop.
Now each pixel is transformed using an array access,
not by computing a log function.
This is much faster.