STDIO Training

45 bundles
4 files8 months ago
0

2D Image Effect Manager - Part 4

2D Image Effect Manager - Part 4

Hiệu ứng ảnh nâng cao phần 4.

Make blur

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384void BlurImage(int width, int height, int byte_per_pixel, byte* &data, double value)
{
	int radius = (int)value;

	if (radius == 0)
		return;

	byte* temp = data;

	int resolution = width * height;

	data = new byte[resolution * byte_per_pixel];

	int r0;
	int c0;

	int hA;
	int hB;
	int wA;
	int wB;
	
	int color[3];
	int count;

	unsigned int radius2 = radius * radius;

	for (int index0 = 0; index0 < resolution; index0++)
	{
		r0 = index0 / width;
		c0 = index0 % width;

		hA = r0 - radius >= 0 ? r0 - radius : 0;
		hB = r0 + radius <= height ? r0 + radius : height;

		wA = c0 - radius >= 0 ? c0 - radius : 0;
		wB = c0 + radius <= width ? c0 + radius : width;

		color[0] = color[1] = color[2] = count = 0;

		unsigned int distance2;
		int index;

		for (int r = hA; r < hB; r++)
		{
			for (int c = wA; c < wB; c++)
			{
				distance2 = (c - c0) *  (c - c0) + (r - r0) * (r - r0);

				if (c0 >= width / 2)
				{
					if (distance2 < radius2)
					{
						index = (r * width + c) * byte_per_pixel;

						color[0] += temp[index + 0];
						color[1] += temp[index + 1];
						color[2] += temp[index + 2];

						count++;
					}
				}
				else
				{
					index = (r * width + c) * byte_per_pixel;

					color[0] += temp[index + 0];
					color[1] += temp[index + 1];
					color[2] += temp[index + 2];

					count++;
				}
			}
		}

		data[index0 * byte_per_pixel + 0] = color[0] / count;
		data[index0 * byte_per_pixel + 1] = color[1] / count;
		data[index0 * byte_per_pixel + 2] = color[2] / count;

		if (byte_per_pixel == 4)
			data[index0 * byte_per_pixel + 3] = temp[index0 * byte_per_pixel + 3];
	}

	delete temp;
}

Softlight

123456789101112131415161718192021222324252627282930313233void SoftlightImage(int width, int height, int byte_per_pixel, byte *& data, double value)
{
	int resolution = width * height;
	int index = -1;

	byte* result = new byte[width * height * byte_per_pixel];

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

		if (data[index + 0] > 128 ||
			data[index + 1] > 128 ||
			data[index + 2] > 128)
		{
			result[index + 0] = data[index + 0];
			result[index + 1] = data[index + 1];
			result[index + 2] = data[index + 2];
		}
		else
		{
			result[index + 0] = 0;
			result[index + 1] = 0;
			result[index + 2] = 0;
		}

		if (byte_per_pixel == 4)
			result[index + 3] = data[index + 3];
	}

	delete[] data;
	data = result;
}

Bloom

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647void BloomImage(int width, int height, int byte_per_pixel, byte*& data, double value)
{
	byte* original = data;

	int imageSize = width * height * byte_per_pixel;

	data = new byte[imageSize];
	for (int i = 0; i < imageSize; i++)
		data[i] = original[i];

	SoftlightImage(width, height, byte_per_pixel, data, value);
	IncreaseBrightnessOfImage(width, height, byte_per_pixel, data, value);
	BlurImage(width, height, byte_per_pixel, data, value);

	int component = 0;
	int index = 0;

	float factor = 1.0;

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

			component = data[index + 0] * factor + original[index + 0];
			if (component > 255)
				component = 255;
			data[index + 0] = component;

			component = data[index + 1] * factor + original[index + 1];
			if (component > 255)
				component = 255;
			data[index + 1] = component;

			component = data[index + 2] * factor + original[index + 2];
			if (component > 255)
				component = 255;
			data[index + 2] = component;

			if (byte_per_pixel == 4)
				data[index + 3] = original[index + 3];
		}
	}

	delete[] original;
}

Other external

1234https://www.bugs.vn/io/QOTo6VlXaPiZBM2N

Matrix4 https://bugs.vn/io/1kwDthlXv8pIU4Wq