2D Image Effect Manager - Part 1

Lập trình hiệu ứng cho ảnh 2D cơ bản phần 1.

Shine one component

123456789101112131415void ShineOneComponentInImage(int width, int height, int byte_per_pixel, byte *& data, double value)
{
	int off_position[2] = {0, 1};

	int resolution = width * height;
	int i = 0;

	for (int index = 0; index < resolution; index++)
	{
		i = index * byte_per_pixel;

		*(data + i + off_position[0]) = 0;
		*(data + i + off_position[1]) = 0;
	}
}

Make grey

12345678910111213141516void MakeBGRToGreyImage(int width, int height, int byte_per_pixel, byte *& data, double value)
{
	int resolution = width * height;
	byte component = 0;

	int i = 0;
	for (int index = 0; index < resolution; index++)
	{
		i = index * byte_per_pixel;
		component = (*(data + i + 0) + *(data + i + 1) + *(data + i + 2)) / 3;
		
		*(data + i + 0) = component;
		*(data + i + 1) = component;
		*(data + i + 2) = component;
	}
}

Grayscale

1234567891011121314void GrayscaleImage(int width, int height, int byte_per_pixel, byte *& data, double value)
{
	int componentIndexes[] = {0, 1, 2};

	int resolution = width * height;
	int index = 0;

	for (int i = 0; i < resolution; i++)
	{
		index = i * byte_per_pixel;
		*(data + index + componentIndexes[1]) = *(data + index + componentIndexes[0]);
		*(data + index + componentIndexes[2]) = *(data + index + componentIndexes[0]);
	}
}

Brightness

1234567891011121314151617181920void IncreaseBrightnessOfImage(int width, int height, int byte_per_pixel, byte* &data, double value)
{
	int resolution = width * height;
	int index = 0;
	int component = 0;

	for (int i = 0; i < resolution; i++)
	{
		index = i * byte_per_pixel;

		component = *(data + index + 0) * value;
		*(data + index + 0) = component > 255 ? 255 : component;

		component = *(data + index + 1) * value;
		*(data + index + 1) = component > 255 ? 255 : component;

		component = *(data + index + 2) * value;
		*(data + index + 2) = component > 255 ? 255 : component;
	}
}

Brightness a part

12345678910111213141516171819202122232425262728293031323334353637void IncreaseBrightnessAPartOfImage(int width, int height, int byte_per_pixel, byte* &data, double value)
{
	int X = 0, Y = 1, W = 2, H = 3;

	// Rectangle: X, Y, W, H
	int rect[4] = { 100, 100, 200, 150 };

	// Recompute rectangle
	if (rect[X] < 0)
		rect[X] = 0;
	if (rect[X] + rect[W] > width)
		rect[W] = width - rect[X];
	if (rect[Y] < 0)
		rect[Y] = 0;
	if (rect[Y] + rect[H] > height)
		rect[H] = height - rect[Y];

	int index = 0;
	int component = 0;

	for (int r = rect[Y]; r < rect[Y] + rect[H]; r++)
	{
		for (int c = rect[X]; c < rect[X] + rect[W]; c++)
		{
			index = (r * width + c) * byte_per_pixel;

			component = *(data + index + 0) * value;
			*(data + index + 0) = component > 255 ? 255 : component;

			component = *(data + index + 1) * value;
			*(data + index + 1) = component > 255 ? 255 : component;

			component = *(data + index + 2) * value;
			*(data + index + 2) = component > 255 ? 255 : component;
		}
	}
}

Brightness "art"

1234567891011121314151617181920212223242526272829303132void IncreaseBrightnessArtOfImage(int width, int height, int byte_per_pixel, byte* &data, double value)
{
	int repeatTime = 4;

	int repeatWidth = width;
	int index = 0;
	int component = 0;

	for (int time = 0; time < repeatTime; time++)
	{
		repeatWidth /= 2;
		if (repeatWidth == 1)
			break;

		for (int r = 0; r < height; r++)
		{
			for (int c = 0; c < repeatWidth; c++)
			{
				index = (r * width + c) * byte_per_pixel;

				component = *(data + index + 0) * value;
				*(data + index + 0) = component > 255 ? 255 : component;

				component = *(data + index + 1) * value;
				*(data + index + 1) = component > 255 ? 255 : component;

				component = *(data + index + 2) * value;
				*(data + index + 2) = component > 255 ? 255 : component;
			}
		}
	}
}

Consrast (Brightness)

123456789101112131415161718192021222324252627282930void IncreaseContrastOfImage(int width, int height, int byte_per_pixel, byte* &data, double value)
{
	int resolution = width * height;
	int index = 0;
	int component = 0;
	double intensity = 0.0;

	int border = 128;

	for (int i = 0; i < resolution; i++)
	{
		index = i * byte_per_pixel;

		if (*(data + index + 0) > border ||
			*(data + index + 1) > border ||
			*(data + index + 2) > border)
			intensity = value;
		else
			intensity = 1.0 / value;

		component = *(data + index + 0) * intensity;
		*(data + index + 0) = component > 255 ? 255 : component;

		component = *(data + index + 1) * intensity;
		*(data + index + 1) = component > 255 ? 255 : component;

		component = *(data + index + 2) * intensity;
		*(data + index + 2) = component > 255 ? 255 : component;
	}
}

Border

12345678910111213141516171819202122232425262728293031323334353637383940414243444546void BoundingBoxImage(int width, int height, int byte_per_pixel, byte *& data, double value)
{
	int borderWidth = (int)value;

	byte color[3] = { 0, 136, 255 };

	int index = 0;

	// ooooo
	// x   x
	// ooooo
	for (int r = 0; r < borderWidth; r++)
	{
		for (int c = 0; c < width; c++)
		{
			index = (r * width + c) * byte_per_pixel;
			*(data + index + 0) = color[0];
			*(data + index + 1) = color[1];
			*(data + index + 2) = color[2];

			index = ((height - r - 1) * width + c) * byte_per_pixel;
			*(data + index + 0) = color[0];
			*(data + index + 1) = color[1];
			*(data + index + 2) = color[2];
		}
	}

	// oxxxo
	// o   o
	// oxxxo
	for (int r = 0; r < height; r++)
	{
		for (int c = 0; c < borderWidth; c++)
		{
			index = (r * width + c) * byte_per_pixel;
			*(data + index + 0) = color[0];
			*(data + index + 1) = color[1];
			*(data + index + 2) = color[2];

			index = (r * width + (width - c - 1)) * byte_per_pixel;
			*(data + index + 0) = color[0];
			*(data + index + 1) = color[1];
			*(data + index + 2) = color[2];
		}
	}
}