Exemple #1
0
unsigned int Texture::compareTGA(IAllocator& allocator,
								 FS::IFile* file1,
								 FS::IFile* file2,
								 int difference)
{
	TGAHeader header1, header2;
	file1->read(&header1, sizeof(header1));
	file2->read(&header2, sizeof(header2));

	if (header1.bitsPerPixel != header2.bitsPerPixel ||
		header1.width != header2.width || header1.height != header2.height ||
		header1.dataType != header2.dataType ||
		header1.imageDescriptor != header2.imageDescriptor)
	{
		g_log_error.log("Renderer")
			<< "Trying to compare textures with different formats";
		return 0;
	}

	int color_mode = header1.bitsPerPixel / 8;
	if (header1.dataType != 2)
	{
		g_log_error.log("Renderer") << "Unsupported texture format";
		return 0;
	}

	int different_pixel_count = 0;
	size_t pixel_count = header1.width * header1.height;
	uint8* img1 = (uint8*)allocator.allocate(pixel_count * color_mode);
	uint8* img2 = (uint8*)allocator.allocate(pixel_count * color_mode);

	file1->read(img1, pixel_count * color_mode);
	file2->read(img2, pixel_count * color_mode);

	for (size_t i = 0; i < pixel_count * color_mode; i += color_mode)
	{
		for (int j = 0; j < color_mode; ++j)
		{
			if (Math::abs(img1[i + j] - img2[i + j]) > difference)
			{
				++different_pixel_count;
				break;
			}
		}
	}

	allocator.deallocate(img1);
	allocator.deallocate(img2);

	return different_pixel_count;
}
bool Texture::saveTGA(IAllocator& allocator,
					  FS::IFile* file,
					  int width,
					  int height,
					  int bytes_per_pixel,
					  const uint8_t* image_dest,
					  const Path& path)
{
	if (bytes_per_pixel != 4)
	{
		g_log_error.log("renderer")
			<< "Texture " << path.c_str()
			<< " could not be saved, unsupported TGA format";
		return false;
	}

	uint8_t* data = (uint8_t*)allocator.allocate(width * height * 4);

	TGAHeader header;
	memset(&header, 0, sizeof(header));
	header.bitsPerPixel = (char)(bytes_per_pixel * 8);
	header.height = (short)height;
	header.width = (short)width;
	header.dataType = 2;

	file->write(&header, sizeof(header));

	for (long y = 0; y < header.height; y++)
	{
		long read_index = y * header.width * 4;
		long write_index = ((header.imageDescriptor & 32) != 0)
							   ? read_index
							   : y * header.width * 4;
		for (long x = 0; x < header.width; x++)
		{
			data[write_index + 0] = image_dest[write_index + 2];
			data[write_index + 1] = image_dest[write_index + 1];
			data[write_index + 2] = image_dest[write_index + 0];
			data[write_index + 3] = image_dest[write_index + 3];
			write_index += 4;
		}
	}

	file->write(data, width * height * 4);

	allocator.deallocate(data);

	return true;
}