Selaa lähdekoodia

Fix Image format RGB565 conversion

BlueCube3310 6 kuukautta sitten
vanhempi
commit
638c6a5659

+ 4 - 4
core/io/image.cpp

@@ -3200,9 +3200,9 @@ Color Image::_get_color_at_ofs(const uint8_t *ptr, uint32_t ofs) const {
 		}
 		case FORMAT_RGB565: {
 			uint16_t u = ((uint16_t *)ptr)[ofs];
-			float r = (u & 0x1F) / 31.0;
+			float r = ((u >> 11) & 0x1F) / 31.0;
 			float g = ((u >> 5) & 0x3F) / 63.0;
-			float b = ((u >> 11) & 0x1F) / 31.0;
+			float b = (u & 0x1F) / 31.0;
 			return Color(r, g, b, 1.0);
 		}
 		case FORMAT_RF: {
@@ -3299,9 +3299,9 @@ void Image::_set_color_at_ofs(uint8_t *ptr, uint32_t ofs, const Color &p_color)
 		case FORMAT_RGB565: {
 			uint16_t rgba = 0;
 
-			rgba = uint16_t(CLAMP(p_color.r * 31.0, 0, 31));
+			rgba = uint16_t(CLAMP(p_color.r * 31.0, 0, 31)) << 11;
 			rgba |= uint16_t(CLAMP(p_color.g * 63.0, 0, 63)) << 5;
-			rgba |= uint16_t(CLAMP(p_color.b * 31.0, 0, 31)) << 11;
+			rgba |= uint16_t(CLAMP(p_color.b * 31.0, 0, 31));
 
 			((uint16_t *)ptr)[ofs] = rgba;
 		} break;

+ 1 - 1
modules/dds/dds_enums.h

@@ -195,7 +195,7 @@ static const DDSFormatInfo dds_format_info[DDS_MAX] = {
 	{ "BGRA8", false, 1, 4, Image::FORMAT_RGBA8 },
 	{ "BGRX8", false, 1, 4, Image::FORMAT_RGB8 },
 	{ "BGR5A1", false, 1, 2, Image::FORMAT_RGBA8 },
-	{ "BGR565", false, 1, 2, Image::FORMAT_RGB8 },
+	{ "BGR565", false, 1, 2, Image::FORMAT_RGB565 },
 	{ "B2GR3", false, 1, 1, Image::FORMAT_RGB8 },
 	{ "B2GR3A8", false, 1, 2, Image::FORMAT_RGBA8 },
 	{ "BGR10A2", false, 1, 4, Image::FORMAT_RGBA8 },

+ 1 - 18
modules/dds/image_saver_dds.cpp

@@ -366,7 +366,7 @@ Vector<uint8_t> save_dds_buffer(const Ref<Image> &p_img) {
 		stream_buffer->put_32(b_mask);
 		stream_buffer->put_32(a_mask);
 
-		if (image->get_format() == Image::FORMAT_RGBA4444 || image->get_format() == Image::FORMAT_RGB565 || image->get_format() == Image::FORMAT_RGB8) {
+		if (image->get_format() == Image::FORMAT_RGBA4444 || image->get_format() == Image::FORMAT_RGB8) {
 			needs_pixeldata_swap = true;
 		}
 	} else if (format_type == DDFT_FOURCC) {
@@ -442,23 +442,6 @@ Vector<uint8_t> save_dds_buffer(const Ref<Image> &p_img) {
 					wb[data_i + 1] = ((ar & 0x0F) << 4) | ((gb & 0xF0) >> 4);
 					wb[data_i + 0] = ((ar & 0xF0) >> 4) | ((gb & 0x0F) << 4);
 				}
-			} else if (mip_image->get_format() == Image::FORMAT_RGB565) {
-				// RGB565 to BGR565
-				const int64_t data_size = data.size();
-				uint8_t *wb = data.ptrw();
-
-				for (int64_t data_i = 0; data_i < data_size; data_i += 2) {
-					uint16_t px = wb[data_i] | (wb[data_i + 1] << 8);
-
-					uint8_t r = (px >> 11) & 0x1F;
-					uint8_t g = (px >> 5) & 0x3F;
-					uint8_t b = px & 0x1F;
-
-					uint16_t out_px = (b << 11) | (g << 5) | r;
-
-					wb[data_i + 0] = out_px & 0xFF;
-					wb[data_i + 1] = (out_px >> 8) & 0xFF;
-				}
 			} else if (mip_image->get_format() == Image::FORMAT_RGB8) {
 				// RGB8 to BGR8
 				const int64_t data_size = data.size();

+ 0 - 22
modules/dds/texture_loader_dds.cpp

@@ -172,10 +172,6 @@ static Ref<Image> _dds_load_layer(Ref<FileAccess> p_file, DDSFormat p_dds_format
 
 		// Calculate the space these formats will take up after decoding.
 		switch (p_dds_format) {
-			case DDS_BGR565:
-				size = size * 3 / 2;
-				break;
-
 			case DDS_BGR5A1:
 			case DDS_B2GR3A8:
 			case DDS_LUMINANCE_ALPHA_4:
@@ -214,24 +210,6 @@ static Ref<Image> _dds_load_layer(Ref<FileAccess> p_file, DDSFormat p_dds_format
 					wb[dst_ofs + 3] = a ? 255 : 0;
 				}
 
-			} break;
-			case DDS_BGR565: {
-				// To RGB8.
-				int colcount = size / 3;
-
-				for (int i = colcount - 1; i >= 0; i--) {
-					int src_ofs = i * 2;
-					int dst_ofs = i * 3;
-
-					uint8_t b = wb[src_ofs] & 0x1F;
-					uint8_t g = (wb[src_ofs] >> 5) | ((wb[src_ofs + 1] & 0x7) << 3);
-					uint8_t r = wb[src_ofs + 1] >> 3;
-
-					wb[dst_ofs + 0] = r << 3;
-					wb[dst_ofs + 1] = g << 2;
-					wb[dst_ofs + 2] = b << 3;
-				}
-
 			} break;
 			case DDS_BGRA4: {
 				// To RGBA4.