2D Image Effect Manager - Part 3

Xử lý hiệu ứng ảnh cơ bản phần 3 với Bitwise.

Byte <-> Array

12345678910111213141516
void ByteArrayToInteger(unsigned int &des, const unsigned char* src)
{
	des = 	(src[0] << 24) |
			(src[1] << 16) |
			(src[2] << 8) |
			(src[3] << 0);
}

void IntegerToByteArray(unsigned char* des, const unsigned int & src)
{
	des[0] = src >> 24;
	des[1] = (src & 0x00ff0000) >> 16;
	des[2] = (src & 0x0000ff00) >> 8;
	des[3] = src & 0x000000ff;
}

Compress RGB888 <-> RGB565

12345678910111213141516171819202122232425void BGR888ToBGR565(unsigned char* color565, const unsigned char* color888)
{
	color565[0] = (color888[0] & 0xf8) | (color888[1] >> 5);
	color565[1] = ((color888[1] & 0x1c) << 3) | (color888[2] >> 3);
}

void BGR565ToBGR888(unsigned char* color888, const unsigned char* color565)
{
	color888[0] = color565[0] & 0xf8;
	color888[1] = (color565[0] << 5) | (color565[1] >> 5);
	color888[2] = color565[1] << 3;
}

void BGR888ToBGR444(unsigned char* color444, const unsigned char* color888)
{
	color444[0] = (color888[0] & 0xf0) | (color888[1] >> 4);
	color444[1] = color888[2] & 0xf0;
}

void BGR444ToBGR888(unsigned char* color888, const unsigned char* color444)
{
	color888[0] = color444[0] & 0xf0;
	color888[1] = color444[0] << 4;
	color888[2] = color444[1] & 0xf0;
}

Invert effect

1234567891011121314void InvertImage(int width, int height, int byte_per_pixel, byte *& data)
{
	int resolution = width * height;
	int i = 0;
	
	for (int index = 0; index < resolution; index++)
	{
		i = index * byte_per_pixel;
		
		*(data + i + 0) = ~*(data + i + 0);
		*(data + i + 1) = ~*(data + i + 1);
		*(data + i + 2) = ~*(data + i + 2);
	}		
}

Lossy compress

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123/////////////////////////////////////////////

void CompressImage(int width, int height, int byte_per_pixel, byte *& data)
{
	byte_per_pixel--;
	
	byte *dataTemp = new byte[width * height * (byte_per_pixel - 1)];

	int resolution = width * height;
	int i = 0;
	
	for (int index = 0; index < resolution; index++)
	{
		i = index * byte_per_pixel;
		BGR888ToBGR444(dataTemp + i, data + i);
	}
	
	delete []data;
	data = dataTemp;
}

void DecompressImage(int width, int height, int byte_per_pixel, byte *& data)
{
	byte *dataTemp = new byte[width * height * byte_per_pixel];

	int resolution = width * height;
	int i = 0;
	
	for (int index = 0; index < resolution; index++)
	{
		i = index * byte_per_pixel;
		BGR444ToBGR888(dataTemp + i, data + i);
	}
	
	delete []data;
	data = dataTemp;
}

/////////////////////////////////////////////

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

	byte* compressedImage = new byte[width * height * 2];

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

		compressedImage[jndex + 0] = (data[index + 0] & 0xf0) | (data[index + 1] >> 4);
		compressedImage[jndex + 1] = data[index + 2] & 0xf0;
	}

	delete[] data;
	data = compressedImage;
}

void DecompressImage(int width, int height, int byte_per_pixel, byte* &data, double value)
{
	int resolution = width * height;

	int index = 0;
	int jndex = 0;

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

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

		decompressedImage[index + 0] = data[jndex + 0] & 0xf0;
		decompressedImage[index + 1] = data[jndex + 0] << 4;
		decompressedImage[index + 2] = data[jndex + 1] & 0xf0;
	}

	delete[] data;
	data = decompressedImage;
}

/////////////////////////////////////////////

void CompressImage(int width, int height, int byte_per_pixel, byte*& data, double value)
{
	int resolution = width * height;
	int dataIndex = 0;
	int compressedIndex = 0;

	byte * compressedData = new byte[width * height * (byte_per_pixel - 1)];

	for (int i = 0; i < resolution; i++)
	{
		dataIndex = i * byte_per_pixel;
		compressedIndex = i * (byte_per_pixel - 1);

		compressedData[compressedIndex + 0] = data[dataIndex + 0] & 0xf0;
		compressedData[compressedIndex + 0] = compressedData[compressedIndex + 0] | (data[dataIndex + 1] >> 4);
		compressedData[compressedIndex + 1] = data[dataIndex + 2] & 0xf0;

		if (byte_per_pixel == 4)
			compressedData[compressedIndex + 2] = data[dataIndex + 3];
	}

	for (int i = 0; i < resolution; i++)
	{
		dataIndex = i * byte_per_pixel;
		compressedIndex = i * (byte_per_pixel - 1);

		data[dataIndex + 0] = compressedData[compressedIndex + 0] & 0xf0;
		data[dataIndex + 1] = compressedData[compressedIndex + 0] << 4;
		data[dataIndex + 2] = compressedData[compressedIndex + 1] & 0xf0;

		if (byte_per_pixel == 4)
			data[dataIndex + 3] = compressedData[compressedIndex + 2];
	}

	delete compressedData;
}

Encrypt image

12345678910111213141516void SimpleEncryptImage(int width, int height, int byte_per_pixel, byte *& data)
{
	int resolution = width * height;
	int i = 0;
	
	byte color[3] = {0, 136, 255};
	
	for (int index = 0; index < resolution; index++)
	{
		i = index * byte_per_pixel;
		
		*(data + i + 0) = *(data + i + 0) ^ color[0];
		*(data + i + 1) = *(data + i + 1) ^ color[1];
		*(data + i + 2) = *(data + i + 2) ^ color[2];
	}	
}