Skip to content

Commit

Permalink
Image: Add a method for detecting signed values
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueCube3310 committed Jun 5, 2024
1 parent 3ac9843 commit 34ecaaf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
27 changes: 27 additions & 0 deletions core/io/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3710,6 +3710,33 @@ void Image::bump_map_to_normal_map(float bump_scale) {
data = result_image;
}

bool Image::detect_signed(bool p_include_mips) const {
ERR_FAIL_COND_V(is_compressed(), false);

if (format >= Image::FORMAT_RH && format <= Image::FORMAT_RGBAH) {
const uint16_t *img_data = reinterpret_cast<const uint16_t *>(data.ptr());
const uint64_t img_size = p_include_mips ? (data.size() / 2) : (width * height * get_format_pixel_size(format) / 2);

for (uint64_t i = 0; i < img_size; i++) {
if ((img_data[i] & 0x8000) != 0 && (img_data[i] & 0x7fff) != 0) {
return true;
}
}

} else if (format >= Image::FORMAT_RF && format <= Image::FORMAT_RGBAF) {
const uint32_t *img_data = reinterpret_cast<const uint32_t *>(data.ptr());
const uint64_t img_size = p_include_mips ? (data.size() / 4) : (width * height * get_format_pixel_size(format) / 4);

for (uint64_t i = 0; i < img_size; i++) {
if ((img_data[i] & 0x80000000) != 0 && (img_data[i] & 0x7fffffff) != 0) {
return true;
}
}
}

return false;
}

void Image::srgb_to_linear() {
if (data.size() == 0) {
return;
Expand Down
2 changes: 2 additions & 0 deletions core/io/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ class Image : public Resource {
Ref<Image> get_image_from_mipmap(int p_mipamp) const;
void bump_map_to_normal_map(float bump_scale = 1.0);

bool detect_signed(bool p_include_mips = true) const;

void blit_rect(const Ref<Image> &p_src, const Rect2i &p_src_rect, const Point2i &p_dest);
void blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2i &p_src_rect, const Point2i &p_dest);
void blend_rect(const Ref<Image> &p_src, const Rect2i &p_src_rect, const Point2i &p_dest);
Expand Down
12 changes: 1 addition & 11 deletions modules/cvtt/image_compress_cvtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,7 @@ void image_compress_cvtt(Image *p_image, Image::UsedChannels p_channels) {
p_image->convert(Image::FORMAT_RGBH);
}

const uint8_t *rb = p_image->get_data().ptr();

const uint16_t *source_data = reinterpret_cast<const uint16_t *>(&rb[0]);
int pixel_element_count = w * h * 3;
for (int i = 0; i < pixel_element_count; i++) {
if ((source_data[i] & 0x8000) != 0 && (source_data[i] & 0x7fff) != 0) {
is_signed = true;
break;
}
}

is_signed = p_image->detect_signed();
target_format = is_signed ? Image::FORMAT_BPTC_RGBF : Image::FORMAT_BPTC_RGBFU;
} else {
p_image->convert(Image::FORMAT_RGBA8); //still uses RGBA to convert
Expand Down

0 comments on commit 34ecaaf

Please sign in to comment.