Skip to content

Commit 395b0f0

Browse files
committed
Merge pull request #103635 from BlueCube3310/rgb565-fix
Fix Image format RGB565 conversion and rendering
2 parents ce330e6 + 638c6a5 commit 395b0f0

File tree

4 files changed

+6
-45
lines changed

4 files changed

+6
-45
lines changed

core/io/image.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3200,9 +3200,9 @@ Color Image::_get_color_at_ofs(const uint8_t *ptr, uint32_t ofs) const {
32003200
}
32013201
case FORMAT_RGB565: {
32023202
uint16_t u = ((uint16_t *)ptr)[ofs];
3203-
float r = (u & 0x1F) / 31.0;
3203+
float r = ((u >> 11) & 0x1F) / 31.0;
32043204
float g = ((u >> 5) & 0x3F) / 63.0;
3205-
float b = ((u >> 11) & 0x1F) / 31.0;
3205+
float b = (u & 0x1F) / 31.0;
32063206
return Color(r, g, b, 1.0);
32073207
}
32083208
case FORMAT_RF: {
@@ -3299,9 +3299,9 @@ void Image::_set_color_at_ofs(uint8_t *ptr, uint32_t ofs, const Color &p_color)
32993299
case FORMAT_RGB565: {
33003300
uint16_t rgba = 0;
33013301

3302-
rgba = uint16_t(CLAMP(p_color.r * 31.0, 0, 31));
3302+
rgba = uint16_t(CLAMP(p_color.r * 31.0, 0, 31)) << 11;
33033303
rgba |= uint16_t(CLAMP(p_color.g * 63.0, 0, 63)) << 5;
3304-
rgba |= uint16_t(CLAMP(p_color.b * 31.0, 0, 31)) << 11;
3304+
rgba |= uint16_t(CLAMP(p_color.b * 31.0, 0, 31));
33053305

33063306
((uint16_t *)ptr)[ofs] = rgba;
33073307
} break;

modules/dds/dds_enums.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ static const DDSFormatInfo dds_format_info[DDS_MAX] = {
195195
{ "BGRA8", false, 1, 4, Image::FORMAT_RGBA8 },
196196
{ "BGRX8", false, 1, 4, Image::FORMAT_RGB8 },
197197
{ "BGR5A1", false, 1, 2, Image::FORMAT_RGBA8 },
198-
{ "BGR565", false, 1, 2, Image::FORMAT_RGB8 },
198+
{ "BGR565", false, 1, 2, Image::FORMAT_RGB565 },
199199
{ "B2GR3", false, 1, 1, Image::FORMAT_RGB8 },
200200
{ "B2GR3A8", false, 1, 2, Image::FORMAT_RGBA8 },
201201
{ "BGR10A2", false, 1, 4, Image::FORMAT_RGBA8 },

modules/dds/image_saver_dds.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ Vector<uint8_t> save_dds_buffer(const Ref<Image> &p_img) {
366366
stream_buffer->put_32(b_mask);
367367
stream_buffer->put_32(a_mask);
368368

369-
if (image->get_format() == Image::FORMAT_RGBA4444 || image->get_format() == Image::FORMAT_RGB565 || image->get_format() == Image::FORMAT_RGB8) {
369+
if (image->get_format() == Image::FORMAT_RGBA4444 || image->get_format() == Image::FORMAT_RGB8) {
370370
needs_pixeldata_swap = true;
371371
}
372372
} else if (format_type == DDFT_FOURCC) {
@@ -442,23 +442,6 @@ Vector<uint8_t> save_dds_buffer(const Ref<Image> &p_img) {
442442
wb[data_i + 1] = ((ar & 0x0F) << 4) | ((gb & 0xF0) >> 4);
443443
wb[data_i + 0] = ((ar & 0xF0) >> 4) | ((gb & 0x0F) << 4);
444444
}
445-
} else if (mip_image->get_format() == Image::FORMAT_RGB565) {
446-
// RGB565 to BGR565
447-
const int64_t data_size = data.size();
448-
uint8_t *wb = data.ptrw();
449-
450-
for (int64_t data_i = 0; data_i < data_size; data_i += 2) {
451-
uint16_t px = wb[data_i] | (wb[data_i + 1] << 8);
452-
453-
uint8_t r = (px >> 11) & 0x1F;
454-
uint8_t g = (px >> 5) & 0x3F;
455-
uint8_t b = px & 0x1F;
456-
457-
uint16_t out_px = (b << 11) | (g << 5) | r;
458-
459-
wb[data_i + 0] = out_px & 0xFF;
460-
wb[data_i + 1] = (out_px >> 8) & 0xFF;
461-
}
462445
} else if (mip_image->get_format() == Image::FORMAT_RGB8) {
463446
// RGB8 to BGR8
464447
const int64_t data_size = data.size();

modules/dds/texture_loader_dds.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,6 @@ static Ref<Image> _dds_load_layer(Ref<FileAccess> p_file, DDSFormat p_dds_format
172172

173173
// Calculate the space these formats will take up after decoding.
174174
switch (p_dds_format) {
175-
case DDS_BGR565:
176-
size = size * 3 / 2;
177-
break;
178-
179175
case DDS_BGR5A1:
180176
case DDS_B2GR3A8:
181177
case DDS_LUMINANCE_ALPHA_4:
@@ -214,24 +210,6 @@ static Ref<Image> _dds_load_layer(Ref<FileAccess> p_file, DDSFormat p_dds_format
214210
wb[dst_ofs + 3] = a ? 255 : 0;
215211
}
216212

217-
} break;
218-
case DDS_BGR565: {
219-
// To RGB8.
220-
int colcount = size / 3;
221-
222-
for (int i = colcount - 1; i >= 0; i--) {
223-
int src_ofs = i * 2;
224-
int dst_ofs = i * 3;
225-
226-
uint8_t b = wb[src_ofs] & 0x1F;
227-
uint8_t g = (wb[src_ofs] >> 5) | ((wb[src_ofs + 1] & 0x7) << 3);
228-
uint8_t r = wb[src_ofs + 1] >> 3;
229-
230-
wb[dst_ofs + 0] = r << 3;
231-
wb[dst_ofs + 1] = g << 2;
232-
wb[dst_ofs + 2] = b << 3;
233-
}
234-
235213
} break;
236214
case DDS_BGRA4: {
237215
// To RGBA4.

0 commit comments

Comments
 (0)