Skip to content

Commit

Permalink
Make ImageLoader take bit field flags
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriSizov committed Aug 23, 2022
1 parent d560650 commit 672e9d6
Show file tree
Hide file tree
Showing 20 changed files with 54 additions and 34 deletions.
6 changes: 3 additions & 3 deletions core/io/image_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ bool ImageFormatLoader::recognize(const String &p_extension) const {
return false;
}

Error ImageLoader::load_image(String p_file, Ref<Image> p_image, Ref<FileAccess> p_custom, bool p_force_linear, float p_scale) {
Error ImageLoader::load_image(String p_file, Ref<Image> p_image, Ref<FileAccess> p_custom, uint32_t p_flags, float p_scale) {
ERR_FAIL_COND_V_MSG(p_image.is_null(), ERR_INVALID_PARAMETER, "It's not a reference to a valid Image object.");

Ref<FileAccess> f = p_custom;
Expand All @@ -60,7 +60,7 @@ Error ImageLoader::load_image(String p_file, Ref<Image> p_image, Ref<FileAccess>
if (!loader[i]->recognize(extension)) {
continue;
}
Error err = loader[i]->load_image(p_image, f, p_force_linear, p_scale);
Error err = loader[i]->load_image(p_image, f, p_flags, p_scale);
if (err != OK) {
ERR_PRINT("Error loading image: " + p_file);
}
Expand Down Expand Up @@ -152,7 +152,7 @@ Ref<Resource> ResourceFormatLoaderImage::load(const String &p_path, const String
Ref<Image> image;
image.instantiate();

Error err = ImageLoader::loader[idx]->load_image(image, f, false, 1.0);
Error err = ImageLoader::loader[idx]->load_image(image, f);

if (err != OK) {
if (r_error) {
Expand Down
9 changes: 7 additions & 2 deletions core/io/image_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ class ImageFormatLoader {
friend class ResourceFormatLoaderImage;

protected:
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> p_fileaccess, bool p_force_linear, float p_scale) = 0;
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> p_fileaccess, uint32_t p_flags = (uint32_t)FLAG_NONE, float p_scale = 1.0) = 0;
virtual void get_recognized_extensions(List<String> *p_extensions) const = 0;
bool recognize(const String &p_extension) const;

public:
enum LoaderFlags {
FLAG_NONE = 0,
FLAG_FORCE_LINEAR = 1,
};

virtual ~ImageFormatLoader() {}
};

Expand All @@ -58,7 +63,7 @@ class ImageLoader {

protected:
public:
static Error load_image(String p_file, Ref<Image> p_image, Ref<FileAccess> p_custom = Ref<FileAccess>(), bool p_force_linear = false, float p_scale = 1.0);
static Error load_image(String p_file, Ref<Image> p_image, Ref<FileAccess> p_custom = Ref<FileAccess>(), uint32_t p_flags = (uint32_t)ImageFormatLoader::FLAG_NONE, float p_scale = 1.0);
static void get_recognized_extensions(List<String> *p_extensions);
static ImageFormatLoader *recognize(const String &p_extension);

Expand Down
4 changes: 2 additions & 2 deletions drivers/png/image_loader_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

#include <string.h>

Error ImageLoaderPNG::load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_force_linear, float p_scale) {
Error ImageLoaderPNG::load_image(Ref<Image> p_image, Ref<FileAccess> f, uint32_t p_flags, float p_scale) {
const uint64_t buffer_size = f->get_length();
Vector<uint8_t> file_buffer;
Error err = file_buffer.resize(buffer_size);
Expand All @@ -48,7 +48,7 @@ Error ImageLoaderPNG::load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_f
f->get_buffer(writer, buffer_size);
}
const uint8_t *reader = file_buffer.ptr();
return PNGDriverCommon::png_to_image(reader, buffer_size, p_force_linear, p_image);
return PNGDriverCommon::png_to_image(reader, buffer_size, p_flags & FLAG_FORCE_LINEAR, p_image);
}

void ImageLoaderPNG::get_recognized_extensions(List<String> *p_extensions) const {
Expand Down
2 changes: 1 addition & 1 deletion drivers/png/image_loader_png.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ImageLoaderPNG : public ImageFormatLoader {
static Ref<Image> load_mem_png(const uint8_t *p_png, int p_size);

public:
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_force_linear, float p_scale);
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> f, uint32_t p_flags, float p_scale);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderPNG();
};
Expand Down
2 changes: 1 addition & 1 deletion editor/import/resource_importer_layered_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ Error ResourceImporterLayeredTexture::import(const String &p_source_file, const

Ref<Image> image;
image.instantiate();
Error err = ImageLoader::load_image(p_source_file, image, nullptr, false, 1.0);
Error err = ImageLoader::load_image(p_source_file, image);
if (err != OK) {
return err;
}
Expand Down
29 changes: 22 additions & 7 deletions editor/import/resource_importer_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,41 +409,56 @@ void ResourceImporterTexture::_save_ctex(const Ref<Image> &p_image, const String
}

Error ResourceImporterTexture::import(const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
// Parse import options.
int32_t loader_flags = ImageFormatLoader::FLAG_NONE;

// Compression.
CompressMode compress_mode = CompressMode(int(p_options["compress/mode"]));
const float lossy = p_options["compress/lossy_quality"];
const int pack_channels = p_options["compress/channel_pack"];
const int normal = p_options["compress/normal_map"];
const int hdr_compression = p_options["compress/hdr_compression"];
const int bptc_ldr = p_options["compress/bptc_ldr"];

// Mipmaps.
const bool mipmaps = p_options["mipmaps/generate"];
const uint32_t mipmap_limit = mipmaps ? uint32_t(p_options["mipmaps/limit"]) : uint32_t(-1);

// Roughness.
const int roughness = p_options["roughness/mode"];
const String normal_map = p_options["roughness/src_normal"];

// Processing.
const bool fix_alpha_border = p_options["process/fix_alpha_border"];
const bool premult_alpha = p_options["process/premult_alpha"];
const bool normal_map_invert_y = p_options["process/normal_map_invert_y"];
// Support for texture streaming is not implemented yet.
const bool stream = false;
const int size_limit = p_options["process/size_limit"];
const bool hdr_as_srgb = p_options["process/hdr_as_srgb"];
if (hdr_as_srgb) {
loader_flags |= ImageFormatLoader::FLAG_FORCE_LINEAR;
}
const bool hdr_clamp_exposure = p_options["process/hdr_clamp_exposure"];
const int normal = p_options["compress/normal_map"];
const int hdr_compression = p_options["compress/hdr_compression"];
const int bptc_ldr = p_options["compress/bptc_ldr"];
const int roughness = p_options["roughness/mode"];
const String normal_map = p_options["roughness/src_normal"];

float scale = 1.0;
// SVG-specific options.
if (p_options.has("svg/scale")) {
scale = p_options["svg/scale"];
}

Ref<Image> normal_image;
Image::RoughnessChannel roughness_channel = Image::ROUGHNESS_CHANNEL_R;

if (mipmaps && roughness > 1 && FileAccess::exists(normal_map)) {
normal_image.instantiate();
if (ImageLoader::load_image(normal_map, normal_image) == OK) {
roughness_channel = Image::RoughnessChannel(roughness - 2);
}
}

Ref<Image> image;
image.instantiate();
Error err = ImageLoader::load_image(p_source_file, image, nullptr, hdr_as_srgb, scale);
Error err = ImageLoader::load_image(p_source_file, image, nullptr, loader_flags, scale);
if (err != OK) {
return err;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/bmp/image_loader_bmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ Error ImageLoaderBMP::convert_to_image(Ref<Image> p_image,
return err;
}

Error ImageLoaderBMP::load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_force_linear, float p_scale) {
Error ImageLoaderBMP::load_image(Ref<Image> p_image, Ref<FileAccess> f, uint32_t p_flags, float p_scale) {
bmp_header_s bmp_header;
Error err = ERR_INVALID_DATA;

Expand Down
2 changes: 1 addition & 1 deletion modules/bmp/image_loader_bmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class ImageLoaderBMP : public ImageFormatLoader {
const bmp_header_s &p_header);

public:
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_force_linear, float p_scale);
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> f, uint32_t p_flags, float p_scale);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderBMP();
};
Expand Down
4 changes: 2 additions & 2 deletions modules/hdr/image_loader_hdr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "core/os/os.h"
#include "core/string/print_string.h"

Error ImageLoaderHDR::load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_force_linear, float p_scale) {
Error ImageLoaderHDR::load_image(Ref<Image> p_image, Ref<FileAccess> f, uint32_t p_flags, float p_scale) {
String header = f->get_token();

ERR_FAIL_COND_V_MSG(header != "#?RADIANCE" && header != "#?RGBE", ERR_FILE_UNRECOGNIZED, "Unsupported header information in HDR: " + header + ".");
Expand Down Expand Up @@ -131,7 +131,7 @@ Error ImageLoaderHDR::load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_f
ptr[1] * exp / 255.0,
ptr[2] * exp / 255.0);

if (p_force_linear) {
if (p_flags & FLAG_FORCE_LINEAR) {
c = c.srgb_to_linear();
}

Expand Down
2 changes: 1 addition & 1 deletion modules/hdr/image_loader_hdr.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

class ImageLoaderHDR : public ImageFormatLoader {
public:
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_force_linear, float p_scale);
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> f, uint32_t p_flags, float p_scale);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderHDR();
};
Expand Down
2 changes: 1 addition & 1 deletion modules/jpg/image_loader_jpegd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Error jpeg_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p
return OK;
}

Error ImageLoaderJPG::load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_force_linear, float p_scale) {
Error ImageLoaderJPG::load_image(Ref<Image> p_image, Ref<FileAccess> f, uint32_t p_flags, float p_scale) {
Vector<uint8_t> src_image;
uint64_t src_image_len = f->get_length();
ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
Expand Down
2 changes: 1 addition & 1 deletion modules/jpg/image_loader_jpegd.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

class ImageLoaderJPG : public ImageFormatLoader {
public:
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_force_linear, float p_scale);
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> f, uint32_t p_flags, float p_scale);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderJPG();
};
Expand Down
4 changes: 2 additions & 2 deletions modules/svg/image_loader_svg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ void ImageLoaderSVG::get_recognized_extensions(List<String> *p_extensions) const
p_extensions->push_back("svg");
}

Error ImageLoaderSVG::load_image(Ref<Image> p_image, Ref<FileAccess> p_fileaccess, bool p_force_linear, float p_scale) {
Error ImageLoaderSVG::load_image(Ref<Image> p_image, Ref<FileAccess> p_fileaccess, uint32_t p_flags, float p_scale) {
String svg = p_fileaccess->get_as_utf8_string();
create_image_from_string(p_image, svg, p_scale, false, false);
ERR_FAIL_COND_V(p_image->is_empty(), FAILED);
if (p_force_linear) {
if (p_flags & FLAG_FORCE_LINEAR) {
p_image->srgb_to_linear();
}
return OK;
Expand Down
2 changes: 1 addition & 1 deletion modules/svg/image_loader_svg.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ImageLoaderSVG : public ImageFormatLoader {
void set_replace_colors(Dictionary p_replace_colors) { replace_colors = p_replace_colors; }
void create_image_from_string(Ref<Image> p_image, String p_string, float p_scale, bool p_upsample, bool p_convert_color);

virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> p_fileaccess, bool p_force_linear, float p_scale) override;
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> p_fileaccess, uint32_t p_flags, float p_scale) override;
virtual void get_recognized_extensions(List<String> *p_extensions) const override;
};

Expand Down
2 changes: 1 addition & 1 deletion modules/tga/image_loader_tga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ Error ImageLoaderTGA::convert_to_image(Ref<Image> p_image, const uint8_t *p_buff
return OK;
}

Error ImageLoaderTGA::load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_force_linear, float p_scale) {
Error ImageLoaderTGA::load_image(Ref<Image> p_image, Ref<FileAccess> f, uint32_t p_flags, float p_scale) {
Vector<uint8_t> src_image;
uint64_t src_image_len = f->get_length();
ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
Expand Down
2 changes: 1 addition & 1 deletion modules/tga/image_loader_tga.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ImageLoaderTGA : public ImageFormatLoader {
static Error convert_to_image(Ref<Image> p_image, const uint8_t *p_buffer, const tga_header_s &p_header, const uint8_t *p_palette, const bool p_is_monochrome, size_t p_input_size);

public:
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_force_linear, float p_scale);
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> f, uint32_t p_flags, float p_scale);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderTGA();
};
Expand Down
6 changes: 3 additions & 3 deletions modules/tinyexr/image_loader_tinyexr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

#include "thirdparty/tinyexr/tinyexr.h"

Error ImageLoaderTinyEXR::load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_force_linear, float p_scale) {
Error ImageLoaderTinyEXR::load_image(Ref<Image> p_image, Ref<FileAccess> f, uint32_t p_flags, float p_scale) {
Vector<uint8_t> src_image;
uint64_t src_image_len = f->get_length();
ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
Expand Down Expand Up @@ -229,7 +229,7 @@ Error ImageLoaderTinyEXR::load_image(Ref<Image> p_image, Ref<FileAccess> f, bool
color.a = *a_channel++;
}

if (p_force_linear) {
if (p_flags & FLAG_FORCE_LINEAR) {
color = color.srgb_to_linear();
}

Expand Down Expand Up @@ -260,7 +260,7 @@ Error ImageLoaderTinyEXR::load_image(Ref<Image> p_image, Ref<FileAccess> f, bool
color.a = *a_channel++;
}

if (p_force_linear) {
if (p_flags & FLAG_FORCE_LINEAR) {
color = color.srgb_to_linear();
}

Expand Down
2 changes: 1 addition & 1 deletion modules/tinyexr/image_loader_tinyexr.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

class ImageLoaderTinyEXR : public ImageFormatLoader {
public:
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_force_linear, float p_scale);
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> f, uint32_t p_flags, float p_scale);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderTinyEXR();
};
Expand Down
2 changes: 1 addition & 1 deletion modules/webp/image_loader_webp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static Ref<Image> _webp_mem_loader_func(const uint8_t *p_png, int p_size) {
return img;
}

Error ImageLoaderWebP::load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_force_linear, float p_scale) {
Error ImageLoaderWebP::load_image(Ref<Image> p_image, Ref<FileAccess> f, uint32_t p_flags, float p_scale) {
Vector<uint8_t> src_image;
uint64_t src_image_len = f->get_length();
ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
Expand Down
2 changes: 1 addition & 1 deletion modules/webp/image_loader_webp.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

class ImageLoaderWebP : public ImageFormatLoader {
public:
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_force_linear, float p_scale);
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> f, uint32_t p_flags, float p_scale);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderWebP();
};
Expand Down

0 comments on commit 672e9d6

Please sign in to comment.