STDIO Training

45 bundles
6 files8 months ago
1

2D Image Effect Manager - Part 2

Opacity adjustment

12345678910111213141516void DecreaseOpacityOfHalfImage(int width, int height, int byte_per_pixel, byte* &data, double value)
{
	if (byte_per_pixel == 4)
	{
		int index = 0;

		for (int r = 0; r < height; r++)
		{
			for (int c = width / 2; c < width; c++)
			{
				index = (r * width + c) * byte_per_pixel;
				*(data + index + 3) *= value; // keep value from 0.0 to 1.0
			}
		}
	}
}

Split and swap

12345678910111213141516171819202122232425262728void SplitingAndSortingImage(int width, int height, int byte_per_pixel, byte* &data, double value)
{
	int index[4] = { 0 };
	byte component = 0;

	int halfWidth = width / 2;
	int halfHeight = height / 2;

	for (int r = 0; r < halfHeight; r++)
	{
		for (int c = 0; c < halfWidth; c++)
		{
			index[0] = (r * width + c) * byte_per_pixel;
			index[1] = ((r + height / 2) * width + c) * byte_per_pixel;
			index[2] = ((r + height / 2) * width + c + width / 2) * byte_per_pixel;
			index[3] = (r * width + c + width / 2) * byte_per_pixel;

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

Blending (Alpha)

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758void BlendingAreasOfImage(int width, int height, int byte_per_pixel, byte*& data, double value)
{
	int X = 0, Y = 1, W = 2, H = 3;
	int M[4] = { 100, 100, 150, 200 };
 
	if (M[X] < 0)
		M[X] = 0;
	if (M[X] + M[W] > width)
		M[W] = width - M[X];
	if (M[Y] < 0)
		M[Y] = 0;
	if (M[Y] + M[H] > height)
		M[H] = height - M[Y];
 
	byte * layer = new byte[width * height * 4];
 
	int i;
	int r;
	int c;

	int iData;
	int iLayer;
	int alpha;

	for (int index = 0; index < width * height; index++)
	{
		i = index * 4;
 
		r = index / width;
		c = index % width;
 
		*(layer + i + 0) = 0;
		*(layer + i + 1) = 0;
		*(layer + i + 2) = 255;
 
		if (c < M[X] ||
			c > M[X] + M[W] ||
			r < M[Y] ||
			r > M[Y] + M[H])
			*(layer + i + 3) = (int)value;
		else
			*(layer + i + 3) = 0;
	}
 
	for (int index = 0; index < width * height; index++)
	{
		iData = index * byte_per_pixel;
		iLayer = index * 4;
 
		alpha = layer[iLayer + 3];
 
		data[iData + 0] = (data[iData + 0] * (255 - alpha) + layer[iLayer + 0] * alpha) / 255;
		data[iData + 1] = (data[iData + 1] * (255 - alpha) + layer[iLayer + 1] * alpha) / 255;
		data[iData + 2] = (data[iData + 2] * (255 - alpha) + layer[iLayer + 2] * alpha) / 255;
	}
 
	delete layer;
}

Replace

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051void ReplaceAnAreaOfImage(int width, int height, int byte_per_pixel, byte* &data, double value)
{
	int X = 0, Y = 1, W = 2, H = 3;
	int rect[4] = { 100, 100, 100, 150 };
 
	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 dataIndex = 0;
	int tempIndex = 0;
 
	byte* imgTemp = new byte[rect[W] * rect[H] * byte_per_pixel];
 
	for (int r = 0; r < rect[H]; r++)
	{
		for (int c = 0; c < rect[W]; c++)
		{
			tempIndex = (r * rect[W] + c) * byte_per_pixel;
			dataIndex = ((r + rect[Y]) * width + c + rect[X]) * byte_per_pixel;
 
			*(imgTemp + tempIndex + 0) = *(data + dataIndex + 0);
			*(imgTemp + tempIndex + 1) = *(data + dataIndex + 1);
			*(imgTemp + tempIndex + 2) = *(data + dataIndex + 2);
			if (byte_per_pixel == 4)
				*(imgTemp + tempIndex + 3) = *(data + dataIndex + 3);
		}
	}
 
	for (int r = 0; r < rect[H]; r++)
	{
		for (int c = 0; c < rect[W]; c++)
		{
			tempIndex = (r * rect[W] + c) * byte_per_pixel;
			dataIndex = ((r + rect[Y]) * width + c + rect[X]) * byte_per_pixel;
 
			*(data + dataIndex + 0) = *(imgTemp + tempIndex + 0);
			*(data + dataIndex + 1) = *(imgTemp + tempIndex + 1);
			*(data + dataIndex + 2) = *(imgTemp + tempIndex + 2);
			if (byte_per_pixel == 4)
				*(data + dataIndex + 3) = *(imgTemp + tempIndex + 3);
		}
	}
 
	delete[] imgTemp;
}

Rotate

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576void RotateAnAreaOfImage(int width, int height, int byte_per_pixel, byte* &data, double value)
{
	// Rectangle: X, Y, W, H
	int rect[4] = { 100, 100, 100, 150 };
	int vec[2] = { 50, 60 };

	// Recompute rectangle
	if (rect[0] < 0)
		rect[0] = 0;
	if (rect[0] + rect[2] > width)
		rect[2] = width - rect[0];
	if (rect[1] < 0)
		rect[1] = 0;
	if (rect[1] + rect[3] > height)
		rect[3] = height - rect[1];

	int dataIndex = 0;
	int tempIndex = 0;
	int rotatedIndex = 0;

	byte* imgTemp = new byte[rect[2] * rect[3] * byte_per_pixel];

	for (int r = 0; r < rect[3]; r++)
	{
		for (int c = 0; c < rect[2]; c++)
		{
			tempIndex = (r * rect[2] + c) * byte_per_pixel;
			dataIndex = ((r + rect[1]) * width + c + rect[0]) * byte_per_pixel;

			*(imgTemp + tempIndex + 0) = *(data + dataIndex + 0);
			*(imgTemp + tempIndex + 1) = *(data + dataIndex + 1);
			*(imgTemp + tempIndex + 2) = *(data + dataIndex + 2);
			if (byte_per_pixel == 4)
				*(imgTemp + tempIndex + 3) = *(data + dataIndex + 3);
		}
	}

	byte* rotatedTemp = new byte[rect[2] * rect[3] * byte_per_pixel];

	for (int r = 0; r < rect[3]; r++)
	{
		for (int c = 0; c < rect[2]; c++)
		{
			tempIndex = (r * rect[2] + c) * byte_per_pixel;
			rotatedIndex = (c * rect[3] + rect[3] - r - 1) * byte_per_pixel;

			*(rotatedTemp + rotatedIndex + 0) = *(imgTemp + tempIndex + 0);
			*(rotatedTemp + rotatedIndex + 1) = *(imgTemp + tempIndex + 1);
			*(rotatedTemp + rotatedIndex + 2) = *(imgTemp + tempIndex + 2);
			if (byte_per_pixel == 4)
				*(rotatedTemp + rotatedIndex + 3) = *(imgTemp + tempIndex + 3);
		}
	}

	int temp = rect[2];
	rect[2] = rect[3];
	rect[3] = temp;

	for (int r = 0; r < rect[3]; r++)
	{
		for (int c = 0; c < rect[2]; c++)
		{
			rotatedIndex = (r * rect[2] + c) * byte_per_pixel;
			dataIndex = ((r + vec[1]) * width + c + vec[0]) * byte_per_pixel;

			*(data + dataIndex + 0) = *(rotatedTemp + rotatedIndex + 0);
			*(data + dataIndex + 1) = *(rotatedTemp + rotatedIndex + 1);
			*(data + dataIndex + 2) = *(rotatedTemp + rotatedIndex + 2);
			if (byte_per_pixel == 4)
				*(data + dataIndex + 3) = *(rotatedTemp + rotatedIndex + 3);
		}
	}

	delete[] imgTemp;
	delete[] rotatedTemp;
}

Scale (Zoom)

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374void ZoomOutAnAreaOfImage(int width, int height, int byte_per_pixel, byte* &data, double value)
{
	int rectX = 0;
	int rectY = 0;
	int rectW = 100;
	int rectH = 100;

	byte* image = new byte[rectW * rectH * byte_per_pixel];

	int indexImage = 0;
	int index = 0;

	for (int r = 0; r < rectH; r++)
	{
		for (int c = 0; c < rectW; c++)
		{
			indexImage = (r * rectW + c) * byte_per_pixel;
			index = ((r + rectY) * width + (c + rectX)) * byte_per_pixel;

			image[indexImage + 0] = data[index + 0];
			image[indexImage + 1] = data[index + 1];
			image[indexImage + 2] = data[index + 2];

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

	float scaleHorizontal = value;
	float scaleVerticle = value;

	int destW = rectW * scaleHorizontal;
	int destH = rectH * scaleVerticle;

	byte* destImage = new byte[destW * destH * byte_per_pixel];

	for (int r = 0; r < destH; r++)
	{
		for (int c = 0; c < destW; c++)
		{
			int srcR = r / scaleVerticle;
			int srcC = c / scaleHorizontal;

			int desIndex = (r * destW + c) * byte_per_pixel;
			int srcIndex = (srcR * rectW + srcC) * byte_per_pixel;

			destImage[desIndex + 0] = image[srcIndex + 0];
			destImage[desIndex + 1] = image[srcIndex + 1];
			destImage[desIndex + 2] = image[srcIndex + 2];
	
			if (byte_per_pixel == 4)
				destImage[desIndex + 3] = image[srcIndex + 3];
		}
	}

	delete[] image;

	for (int r = 0; r < destH; r++)
	{
		for (int c = 0; c < destW; c++)
		{
			int index = (r * destW + c) * byte_per_pixel;
			int originalIndex = ((r + rectY) * width + (c + rectX)) * byte_per_pixel;

			data[originalIndex + 0] = destImage[index + 0];
			data[originalIndex + 1] = destImage[index + 1];
			data[originalIndex + 2] = destImage[index + 2];
			if (byte_per_pixel == 4)
				data[originalIndex + 3] = destImage[index + 3];
		}
	}

	delete[] destImage;
}