Browse Source

Fix loading of 24-bit TGA with flipped y-axis, see #851

Michael Ragazzon 2 months ago
parent
commit
b6a26d41d8

+ 3 - 2
Backends/RmlUi_Renderer_GL2.cpp

@@ -275,12 +275,13 @@ Rml::TextureHandle RenderInterface_GL2::LoadTexture(Rml::Vector2i& texture_dimen
 	const byte* image_src = buffer.get() + sizeof(TGAHeader);
 	Rml::UniquePtr<byte[]> image_dest_buffer(new byte[image_size]);
 	byte* image_dest = image_dest_buffer.get();
+	const bool top_to_bottom_order = ((header.imageDescriptor & 32) != 0);
 
-	// Targa is BGR, swap to RGB, flip Y axis, and convert to premultiplied alpha.
+	// Targa is BGR, swap to RGB, flip Y axis as necessary, and convert to premultiplied alpha.
 	for (long y = 0; y < header.height; y++)
 	{
 		long read_index = y * header.width * color_mode;
-		long write_index = ((header.imageDescriptor & 32) != 0) ? read_index : (header.height - y - 1) * header.width * 4;
+		long write_index = top_to_bottom_order ? (y * header.width * 4) : (header.height - y - 1) * header.width * 4;
 		for (long x = 0; x < header.width; x++)
 		{
 			image_dest[write_index] = image_src[read_index + 2];

+ 3 - 2
Backends/RmlUi_Renderer_GL3.cpp

@@ -1262,12 +1262,13 @@ Rml::TextureHandle RenderInterface_GL3::LoadTexture(Rml::Vector2i& texture_dimen
 	const byte* image_src = buffer.get() + sizeof(TGAHeader);
 	Rml::UniquePtr<byte[]> image_dest_buffer(new byte[image_size]);
 	byte* image_dest = image_dest_buffer.get();
+	const bool top_to_bottom_order = ((header.imageDescriptor & 32) != 0);
 
-	// Targa is BGR, swap to RGB, flip Y axis, and convert to premultiplied alpha.
+	// Targa is BGR, swap to RGB, flip Y axis as necessary, and convert to premultiplied alpha.
 	for (long y = 0; y < header.height; y++)
 	{
 		long read_index = y * header.width * color_mode;
-		long write_index = ((header.imageDescriptor & 32) != 0) ? read_index : (header.height - y - 1) * header.width * 4;
+		long write_index = top_to_bottom_order ? (y * header.width * 4) : (header.height - y - 1) * header.width * 4;
 		for (long x = 0; x < header.width; x++)
 		{
 			image_dest[write_index] = image_src[read_index + 2];

+ 3 - 2
Backends/RmlUi_Renderer_VK.cpp

@@ -451,12 +451,13 @@ Rml::TextureHandle RenderInterface_VK::LoadTexture(Rml::Vector2i& texture_dimens
 	const byte* image_src = buffer.get() + sizeof(TGAHeader);
 	Rml::UniquePtr<byte[]> image_dest_buffer(new byte[image_size]);
 	byte* image_dest = image_dest_buffer.get();
+	const bool top_to_bottom_order = ((header.imageDescriptor & 32) != 0);
 
-	// Targa is BGR, swap to RGB, flip Y axis, and convert to premultiplied alpha.
+	// Targa is BGR, swap to RGB, flip Y axis as necessary, and convert to premultiplied alpha.
 	for (long y = 0; y < header.height; y++)
 	{
 		long read_index = y * header.width * color_mode;
-		long write_index = ((header.imageDescriptor & 32) != 0) ? read_index : (header.height - y - 1) * header.width * 4;
+		long write_index = top_to_bottom_order ? (y * header.width * 4) : (header.height - y - 1) * header.width * 4;
 		for (long x = 0; x < header.width; x++)
 		{
 			image_dest[write_index] = image_src[read_index + 2];