abhishek2301 0 Light Poster

Hello,

I am trying to understand the exact procedure of the IMG_FILTER_CONTRAST function available in the GD library. After delving through the source code, I found the following function which performs the computation:

int gdImageContrast(gdImagePtr src, double contrast)
{
	int x, y;
	int r,g,b,a;
	double rf,gf,bf;
	int new_pxl, pxl;
	typedef int (*FuncPtr)(gdImagePtr, int, int);

	FuncPtr f;
	f = GET_PIXEL_FUNCTION(src);

	if (src==NULL) {
		return 0;
	}

	contrast = (double)(100.0-contrast)/100.0;
	contrast = contrast*contrast;

	for (y=0; y<src->sy; ++y) {
		for (x=0; x<src->sx; ++x) {
			pxl = f(src, x, y);

			r = gdImageRed(src, pxl);
			g = gdImageGreen(src, pxl);
			b = gdImageBlue(src, pxl);
			a = gdImageAlpha(src, pxl);

			rf = (double)r/255.0;
			rf = rf-0.5;
			rf = rf*contrast;
			rf = rf+0.5;
			rf = rf*255.0;

			bf = (double)b/255.0;
			bf = bf-0.5;
			bf = bf*contrast;
			bf = bf+0.5;
			bf = bf*255.0;

			gf = (double)g/255.0;
			gf = gf-0.5;
			gf = gf*contrast;
			gf = gf+0.5;
			gf = gf*255.0;

			rf = (rf > 255.0)? 255.0 : ((rf < 0.0)? 0.0:rf);
			gf = (gf > 255.0)? 255.0 : ((gf < 0.0)? 0.0:gf);
			bf = (bf > 255.0)? 255.0 : ((bf < 0.0)? 0.0:bf);

			new_pxl = gdImageColorAllocateAlpha(src, (int)rf, (int)gf, (int)bf, a);
			if (new_pxl == -1) {
				new_pxl = gdImageColorClosestAlpha(src, (int)rf, (int)gf, (int)bf, a);
			}
			gdImageSetPixel (src, x, y, new_pxl);
		}
	}
	return 1;
}

According to the specification, the contrast value argument to be passed with the IMG_FILTER_CONTRAST in imagefilter() is [-100,100].

Analysing the above code:
Line: contrast = (double)(100.0-contrast)/100.0; normalizes the [-100,100] to [2,0].
Line: contrast = contrast*contrast; stretches the [2,0] to [4,0].
Line: rf = rf*contrast; each pixel intensity value is multiplied with the above contrast value.

I would like to know whether my above analysis is correct and what is the significance of this contrast adjustment function on the final image.
According to my knowledge, contrast adjustments are generally performed to uniformly distribute the pixel intensity values using histogram equalization (non-linear mapping) or contrast stretching (linear mapping).
I am trying to understand the nature of transfer function applied in this case ?

Any help is appreciated!
Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.