summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--core/extension/extension_api_dump.cpp42
-rw-r--r--core/input/input.cpp15
-rw-r--r--core/object/undo_redo.cpp43
-rw-r--r--core/object/undo_redo.h2
-rw-r--r--editor/animation_track_editor.cpp7
-rw-r--r--editor/code_editor.cpp15
-rw-r--r--editor/code_editor.h3
-rw-r--r--editor/debugger/editor_debugger_node.cpp63
-rw-r--r--editor/debugger/editor_debugger_node.h5
-rw-r--r--editor/plugins/animation_player_editor_plugin.cpp4
-rw-r--r--editor/plugins/mesh_instance_3d_editor_plugin.cpp85
-rw-r--r--editor/plugins/script_editor_plugin.cpp4
-rw-r--r--editor/plugins/shader_editor_plugin.cpp15
-rw-r--r--editor/plugins/shader_editor_plugin.h1
-rw-r--r--editor/scene_tree_dock.cpp17
-rw-r--r--editor/scene_tree_dock.h2
-rw-r--r--modules/mono/csharp_script.cpp14
-rw-r--r--modules/mono/editor/GodotTools/GodotTools/Export/ExportPlugin.cs15
-rw-r--r--modules/mono/mono_gd/gd_mono.cpp15
-rw-r--r--modules/mono/mono_gd/gd_mono.h6
-rw-r--r--modules/svg/SCsub5
-rw-r--r--modules/text_server_adv/gdextension_build/SConstruct9
-rw-r--r--modules/text_server_fb/gdextension_build/SConstruct9
-rw-r--r--platform/android/doc_classes/EditorExportPlatformAndroid.xml2
-rw-r--r--platform/android/java/lib/src/org/godotengine/godot/Godot.kt18
-rw-r--r--scene/2d/camera_2d.cpp6
-rw-r--r--scene/2d/navigation_region_2d.cpp26
-rw-r--r--scene/2d/physics_body_2d.cpp2
-rw-r--r--scene/2d/remote_transform_2d.cpp23
-rw-r--r--scene/3d/navigation_region_3d.cpp12
-rw-r--r--scene/3d/remote_transform_3d.cpp25
-rw-r--r--scene/animation/animation_mixer.cpp32
-rw-r--r--scene/gui/tab_bar.cpp2
-rw-r--r--scene/gui/text_edit.cpp31
-rw-r--r--scene/gui/text_edit.h4
-rw-r--r--scene/main/instance_placeholder.cpp1
-rw-r--r--servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.cpp2
-rw-r--r--servers/rendering_server.cpp4
-rw-r--r--tests/core/variant/test_variant.h1033
-rw-r--r--thirdparty/README.md3
-rw-r--r--thirdparty/thorvg/inc/config.h4
-rw-r--r--thirdparty/thorvg/patches/loader_jpg-fix_regression_bug.patch13
-rw-r--r--thirdparty/thorvg/src/loaders/external_png/tvgPngLoader.cpp120
-rw-r--r--thirdparty/thorvg/src/loaders/external_png/tvgPngLoader.h47
-rw-r--r--thirdparty/thorvg/src/loaders/jpg/tvgJpgLoader.cpp143
-rw-r--r--thirdparty/thorvg/src/loaders/jpg/tvgJpgLoader.h52
-rw-r--r--thirdparty/thorvg/src/loaders/jpg/tvgJpgd.cpp3026
-rw-r--r--thirdparty/thorvg/src/loaders/jpg/tvgJpgd.h35
-rw-r--r--thirdparty/thorvg/src/loaders/png/tvgLodePng.cpp2628
-rw-r--r--thirdparty/thorvg/src/loaders/png/tvgLodePng.h174
-rw-r--r--thirdparty/thorvg/src/loaders/png/tvgPngLoader.cpp185
-rw-r--r--thirdparty/thorvg/src/loaders/png/tvgPngLoader.h55
-rw-r--r--thirdparty/thorvg/src/loaders/svg/tvgSvgSceneBuilder.cpp8
-rw-r--r--thirdparty/thorvg/src/renderer/sw_engine/tvgSwCommon.h1
-rw-r--r--thirdparty/thorvg/src/renderer/sw_engine/tvgSwShape.cpp58
-rw-r--r--thirdparty/thorvg/src/renderer/sw_engine/tvgSwStroke.cpp1
-rwxr-xr-xthirdparty/thorvg/update-thorvg.sh9
57 files changed, 7957 insertions, 224 deletions
diff --git a/core/extension/extension_api_dump.cpp b/core/extension/extension_api_dump.cpp
index 58cb51245a..f3e988633c 100644
--- a/core/extension/extension_api_dump.cpp
+++ b/core/extension/extension_api_dump.cpp
@@ -502,7 +502,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
if (p_include_docs) {
for (const DocData::ConstantDoc &constant_doc : global_scope_doc->constants) {
if (constant_doc.name == name) {
- d["documentation"] = fix_doc_description(constant_doc.description);
+ d["description"] = fix_doc_description(constant_doc.description);
break;
}
}
@@ -521,7 +521,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
if (p_include_docs) {
const DocData::EnumDoc *enum_doc = global_scope_doc->enums.getptr(E.key);
if (enum_doc) {
- d1["documentation"] = fix_doc_description(enum_doc->description);
+ d1["description"] = fix_doc_description(enum_doc->description);
}
}
Array values;
@@ -532,7 +532,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
if (p_include_docs) {
for (const DocData::ConstantDoc &constant_doc : global_scope_doc->constants) {
if (constant_doc.name == F.first) {
- d2["documentation"] = fix_doc_description(constant_doc.description);
+ d2["description"] = fix_doc_description(constant_doc.description);
break;
}
}
@@ -596,7 +596,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
if (p_include_docs) {
for (const DocData::MethodDoc &method_doc : global_scope_doc->methods) {
if (method_doc.name == name) {
- func["documentation"] = fix_doc_description(method_doc.description);
+ func["description"] = fix_doc_description(method_doc.description);
break;
}
}
@@ -647,7 +647,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
if (p_include_docs) {
for (const DocData::PropertyDoc &property_doc : builtin_doc->properties) {
if (property_doc.name == member_name) {
- d2["documentation"] = fix_doc_description(property_doc.description);
+ d2["description"] = fix_doc_description(property_doc.description);
break;
}
}
@@ -673,7 +673,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
if (p_include_docs) {
for (const DocData::ConstantDoc &constant_doc : builtin_doc->constants) {
if (constant_doc.name == constant_name) {
- d2["documentation"] = fix_doc_description(constant_doc.description);
+ d2["description"] = fix_doc_description(constant_doc.description);
break;
}
}
@@ -706,7 +706,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
if (p_include_docs) {
for (const DocData::ConstantDoc &constant_doc : builtin_doc->constants) {
if (constant_doc.name == enumeration) {
- values_dict["documentation"] = fix_doc_description(constant_doc.description);
+ values_dict["description"] = fix_doc_description(constant_doc.description);
break;
}
}
@@ -717,7 +717,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
if (p_include_docs) {
const DocData::EnumDoc *enum_doc = builtin_doc->enums.getptr(enum_name);
if (enum_doc) {
- enum_dict["documentation"] = fix_doc_description(enum_doc->description);
+ enum_dict["description"] = fix_doc_description(enum_doc->description);
}
}
@@ -750,7 +750,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
if (p_include_docs && builtin_doc != nullptr) {
for (const DocData::MethodDoc &operator_doc : builtin_doc->operators) {
if (operator_doc.name == "operator " + operator_name) {
- d2["documentation"] = fix_doc_description(operator_doc.description);
+ d2["description"] = fix_doc_description(operator_doc.description);
break;
}
}
@@ -805,7 +805,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
if (p_include_docs) {
for (const DocData::MethodDoc &method_doc : builtin_doc->methods) {
if (method_doc.name == method_name) {
- d2["documentation"] = fix_doc_description(method_doc.description);
+ d2["description"] = fix_doc_description(method_doc.description);
break;
}
}
@@ -853,7 +853,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
}
}
if (constructor_found) {
- d2["documentation"] = fix_doc_description(constructor_doc.description);
+ d2["description"] = fix_doc_description(constructor_doc.description);
}
}
}
@@ -871,7 +871,8 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
}
if (p_include_docs && builtin_doc != nullptr) {
- d["documentation"] = fix_doc_description(builtin_doc->description);
+ d["brief_description"] = fix_doc_description(builtin_doc->brief_description);
+ d["description"] = fix_doc_description(builtin_doc->description);
}
builtins.push_back(d);
@@ -933,7 +934,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
if (p_include_docs) {
for (const DocData::ConstantDoc &constant_doc : class_doc->constants) {
if (constant_doc.name == F) {
- d2["documentation"] = fix_doc_description(constant_doc.description);
+ d2["description"] = fix_doc_description(constant_doc.description);
break;
}
}
@@ -967,7 +968,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
if (p_include_docs) {
for (const DocData::ConstantDoc &constant_doc : class_doc->constants) {
if (constant_doc.name == G->get()) {
- d3["documentation"] = fix_doc_description(constant_doc.description);
+ d3["description"] = fix_doc_description(constant_doc.description);
break;
}
}
@@ -981,7 +982,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
if (p_include_docs) {
const DocData::EnumDoc *enum_doc = class_doc->enums.getptr(F);
if (enum_doc) {
- d2["documentation"] = fix_doc_description(enum_doc->description);
+ d2["description"] = fix_doc_description(enum_doc->description);
}
}
@@ -1039,7 +1040,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
if (p_include_docs) {
for (const DocData::MethodDoc &method_doc : class_doc->methods) {
if (method_doc.name == method_name) {
- d2["documentation"] = fix_doc_description(method_doc.description);
+ d2["description"] = fix_doc_description(method_doc.description);
break;
}
}
@@ -1116,7 +1117,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
if (p_include_docs) {
for (const DocData::MethodDoc &method_doc : class_doc->methods) {
if (method_doc.name == method_name) {
- d2["documentation"] = fix_doc_description(method_doc.description);
+ d2["description"] = fix_doc_description(method_doc.description);
break;
}
}
@@ -1159,7 +1160,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
if (p_include_docs) {
for (const DocData::MethodDoc &signal_doc : class_doc->signals) {
if (signal_doc.name == signal_name) {
- d2["documentation"] = fix_doc_description(signal_doc.description);
+ d2["description"] = fix_doc_description(signal_doc.description);
break;
}
}
@@ -1208,7 +1209,7 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
if (p_include_docs) {
for (const DocData::PropertyDoc &property_doc : class_doc->properties) {
if (property_doc.name == property_name) {
- d2["documentation"] = fix_doc_description(property_doc.description);
+ d2["description"] = fix_doc_description(property_doc.description);
break;
}
}
@@ -1223,7 +1224,8 @@ Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {
}
if (p_include_docs && class_doc != nullptr) {
- d["documentation"] = fix_doc_description(class_doc->description);
+ d["brief_description"] = fix_doc_description(class_doc->brief_description);
+ d["description"] = fix_doc_description(class_doc->description);
}
classes.push_back(d);
diff --git a/core/input/input.cpp b/core/input/input.cpp
index 19ea8c7317..f3f5940118 100644
--- a/core/input/input.cpp
+++ b/core/input/input.cpp
@@ -697,29 +697,28 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em
for (const KeyValue<StringName, InputMap::Action> &E : InputMap::get_singleton()->get_action_map()) {
if (InputMap::get_singleton()->event_is_action(p_event, E.key)) {
Action &action = action_state[E.key];
+ bool is_joypad_axis = jm.is_valid();
bool is_pressed = false;
-
if (!p_event->is_echo()) {
if (p_event->is_action_pressed(E.key)) {
- if (jm.is_valid()) {
- // If axis is already pressed, don't increase the pressed counter.
+ bool is_joypad_axis_valid_zone_enter = false;
+ if (is_joypad_axis) {
if (!action.axis_pressed) {
+ is_joypad_axis_valid_zone_enter = true;
action.pressed++;
action.axis_pressed = true;
}
} else {
action.pressed++;
}
-
- is_pressed = true;
- if (action.pressed == 1) {
+ if (action.pressed == 1 && (is_joypad_axis_valid_zone_enter || !is_joypad_axis)) {
action.pressed_physics_frame = Engine::get_singleton()->get_physics_frames();
action.pressed_process_frame = Engine::get_singleton()->get_process_frames();
}
+ is_pressed = true;
} else {
bool is_released = true;
- if (jm.is_valid()) {
- // Same as above. Don't release axis when not pressed.
+ if (is_joypad_axis) {
if (action.axis_pressed) {
action.axis_pressed = false;
} else {
diff --git a/core/object/undo_redo.cpp b/core/object/undo_redo.cpp
index 2435ab48ac..a8f2ac5bfe 100644
--- a/core/object/undo_redo.cpp
+++ b/core/object/undo_redo.cpp
@@ -71,9 +71,7 @@ bool UndoRedo::_redo(bool p_execute) {
}
current_action++;
- if (p_execute) {
- _process_operation_list(actions.write[current_action].do_ops.front());
- }
+ _process_operation_list(actions.write[current_action].do_ops.front(), p_execute);
version++;
emit_signal(SNAME("version_changed"));
@@ -321,7 +319,7 @@ void UndoRedo::commit_action(bool p_execute) {
}
}
-void UndoRedo::_process_operation_list(List<Operation>::Element *E) {
+void UndoRedo::_process_operation_list(List<Operation>::Element *E, bool p_execute) {
const int PREALLOCATE_ARGS_COUNT = 16;
LocalVector<const Variant *> args;
@@ -337,18 +335,20 @@ void UndoRedo::_process_operation_list(List<Operation>::Element *E) {
switch (op.type) {
case Operation::TYPE_METHOD: {
- Callable::CallError ce;
- Variant ret;
- op.callable.callp(nullptr, 0, ret, ce);
- if (ce.error != Callable::CallError::CALL_OK) {
- ERR_PRINT("Error calling UndoRedo method operation '" + String(op.name) + "': " + Variant::get_call_error_text(obj, op.name, nullptr, 0, ce));
- }
+ if (p_execute) {
+ Callable::CallError ce;
+ Variant ret;
+ op.callable.callp(nullptr, 0, ret, ce);
+ if (ce.error != Callable::CallError::CALL_OK) {
+ ERR_PRINT("Error calling UndoRedo method operation '" + String(op.name) + "': " + Variant::get_call_error_text(obj, op.name, nullptr, 0, ce));
+ }
#ifdef TOOLS_ENABLED
- Resource *res = Object::cast_to<Resource>(obj);
- if (res) {
- res->set_edited(true);
- }
+ Resource *res = Object::cast_to<Resource>(obj);
+ if (res) {
+ res->set_edited(true);
+ }
#endif
+ }
if (method_callback) {
Vector<Variant> binds;
@@ -373,13 +373,16 @@ void UndoRedo::_process_operation_list(List<Operation>::Element *E) {
}
} break;
case Operation::TYPE_PROPERTY: {
- obj->set(op.name, op.value);
+ if (p_execute) {
+ obj->set(op.name, op.value);
#ifdef TOOLS_ENABLED
- Resource *res = Object::cast_to<Resource>(obj);
- if (res) {
- res->set_edited(true);
- }
+ Resource *res = Object::cast_to<Resource>(obj);
+ if (res) {
+ res->set_edited(true);
+ }
#endif
+ }
+
if (property_callback) {
property_callback(prop_callback_ud, obj, op.name, op.value);
}
@@ -400,7 +403,7 @@ bool UndoRedo::undo() {
if (current_action < 0) {
return false; //nothing to redo
}
- _process_operation_list(actions.write[current_action].undo_ops.front());
+ _process_operation_list(actions.write[current_action].undo_ops.front(), true);
current_action--;
version--;
emit_signal(SNAME("version_changed"));
diff --git a/core/object/undo_redo.h b/core/object/undo_redo.h
index 389d8714f7..74a6bea732 100644
--- a/core/object/undo_redo.h
+++ b/core/object/undo_redo.h
@@ -85,7 +85,7 @@ private:
uint64_t version = 1;
void _pop_history_tail();
- void _process_operation_list(List<Operation>::Element *E);
+ void _process_operation_list(List<Operation>::Element *E, bool p_execute);
void _discard_redo();
bool _redo(bool p_execute);
diff --git a/editor/animation_track_editor.cpp b/editor/animation_track_editor.cpp
index c555ca2109..a2011144ce 100644
--- a/editor/animation_track_editor.cpp
+++ b/editor/animation_track_editor.cpp
@@ -330,6 +330,7 @@ bool AnimationTrackKeyEdit::_set(const StringName &p_name, const Variant &p_valu
undo_redo->commit_action();
setting = false;
+ notify_change(); // To update limits for `start_offset`/`end_offset` sliders (they depend on the stream length).
return true;
}
@@ -586,8 +587,10 @@ void AnimationTrackKeyEdit::_get_property_list(List<PropertyInfo> *p_list) const
} break;
case Animation::TYPE_AUDIO: {
p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("stream"), PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"));
- p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("start_offset"), PROPERTY_HINT_RANGE, "0,3600,0.0001,or_greater"));
- p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("end_offset"), PROPERTY_HINT_RANGE, "0,3600,0.0001,or_greater"));
+ Ref<AudioStream> audio_stream = animation->audio_track_get_key_stream(track, key);
+ String hint_string = vformat("0,%.4f,0.0001,or_greater", audio_stream.is_valid() ? audio_stream->get_length() : 3600.0);
+ p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("start_offset"), PROPERTY_HINT_RANGE, hint_string));
+ p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("end_offset"), PROPERTY_HINT_RANGE, hint_string));
} break;
case Animation::TYPE_ANIMATION: {
diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp
index f85d177eef..b6d90a5bd2 100644
--- a/editor/code_editor.cpp
+++ b/editor/code_editor.cpp
@@ -134,6 +134,13 @@ void FindReplaceBar::unhandled_input(const Ref<InputEvent> &p_event) {
}
}
+void FindReplaceBar::_focus_lost() {
+ if (Input::get_singleton()->is_action_pressed(SNAME("ui_cancel"))) {
+ // Unfocused after pressing Escape, so hide the bar.
+ _hide_bar(true);
+ }
+}
+
bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) {
if (!preserve_cursor) {
text_editor->remove_secondary_carets();
@@ -499,8 +506,8 @@ bool FindReplaceBar::search_next() {
return _search(flags, line, col);
}
-void FindReplaceBar::_hide_bar() {
- if (replace_text->has_focus() || search_text->has_focus()) {
+void FindReplaceBar::_hide_bar(bool p_force_focus) {
+ if (replace_text->has_focus() || search_text->has_focus() || p_force_focus) {
text_editor->grab_focus();
}
@@ -698,6 +705,7 @@ FindReplaceBar::FindReplaceBar() {
search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
search_text->connect("text_changed", callable_mp(this, &FindReplaceBar::_search_text_changed));
search_text->connect("text_submitted", callable_mp(this, &FindReplaceBar::_search_text_submitted));
+ search_text->connect("focus_exited", callable_mp(this, &FindReplaceBar::_focus_lost));
matches_label = memnew(Label);
hbc_button_search->add_child(matches_label);
@@ -732,6 +740,7 @@ FindReplaceBar::FindReplaceBar() {
vbc_lineedit->add_child(replace_text);
replace_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
replace_text->connect("text_submitted", callable_mp(this, &FindReplaceBar::_replace_text_submitted));
+ replace_text->connect("focus_exited", callable_mp(this, &FindReplaceBar::_focus_lost));
replace = memnew(Button);
hbc_button_replace->add_child(replace);
@@ -773,7 +782,7 @@ void CodeTextEditor::input(const Ref<InputEvent> &event) {
}
if (!text_editor->has_focus()) {
- if ((find_replace_bar != nullptr && find_replace_bar->is_visible()) && (find_replace_bar->has_focus() || find_replace_bar->is_ancestor_of(get_viewport()->gui_get_focus_owner()))) {
+ if ((find_replace_bar != nullptr && find_replace_bar->is_visible()) && (find_replace_bar->has_focus() || (get_viewport()->gui_get_focus_owner() && find_replace_bar->is_ancestor_of(get_viewport()->gui_get_focus_owner())))) {
if (ED_IS_SHORTCUT("script_text_editor/find_next", key_event)) {
find_replace_bar->search_next();
accept_event();
diff --git a/editor/code_editor.h b/editor/code_editor.h
index 43173bd475..911b7193f5 100644
--- a/editor/code_editor.h
+++ b/editor/code_editor.h
@@ -97,7 +97,7 @@ class FindReplaceBar : public HBoxContainer {
void _update_matches_label();
void _show_search(bool p_focus_replace = false, bool p_show_only = false);
- void _hide_bar();
+ void _hide_bar(bool p_force_focus = false);
void _editor_text_changed();
void _search_options_changed(bool p_pressed);
@@ -108,6 +108,7 @@ class FindReplaceBar : public HBoxContainer {
protected:
void _notification(int p_what);
virtual void unhandled_input(const Ref<InputEvent> &p_event) override;
+ void _focus_lost();
bool _search(uint32_t p_flags, int p_from_line, int p_from_col);
diff --git a/editor/debugger/editor_debugger_node.cpp b/editor/debugger/editor_debugger_node.cpp
index a327fd778b..8229abb5a0 100644
--- a/editor/debugger/editor_debugger_node.cpp
+++ b/editor/debugger/editor_debugger_node.cpp
@@ -107,6 +107,8 @@ ScriptEditorDebugger *EditorDebuggerNode::_add_debugger() {
node->connect("remote_object_updated", callable_mp(this, &EditorDebuggerNode::_remote_object_updated).bind(id));
node->connect("remote_object_property_updated", callable_mp(this, &EditorDebuggerNode::_remote_object_property_updated).bind(id));
node->connect("remote_object_requested", callable_mp(this, &EditorDebuggerNode::_remote_object_requested).bind(id));
+ node->connect("set_breakpoint", callable_mp(this, &EditorDebuggerNode::_breakpoint_set_in_tree).bind(id));
+ node->connect("clear_breakpoints", callable_mp(this, &EditorDebuggerNode::_breakpoints_cleared_in_tree).bind(id));
node->connect("errors_cleared", callable_mp(this, &EditorDebuggerNode::_update_errors));
if (tabs->get_tab_count() > 0) {
@@ -168,6 +170,27 @@ void EditorDebuggerNode::_text_editor_stack_goto(const ScriptEditorDebugger *p_d
stack_script.unref(); // Why?!?
}
+void EditorDebuggerNode::_text_editor_stack_clear(const ScriptEditorDebugger *p_debugger) {
+ String file = p_debugger->get_stack_script_file();
+ if (file.is_empty()) {
+ return;
+ }
+ if (file.is_resource_file()) {
+ stack_script = ResourceLoader::load(file);
+ } else {
+ // If the script is built-in, it can be opened only if the scene is loaded in memory.
+ int i = file.find("::");
+ int j = file.rfind("(", i);
+ if (j > -1) { // If the script is named, the string is "name (file)", so we need to extract the path.
+ file = file.substr(j + 1, file.find(")", i) - j - 1);
+ }
+ Ref<PackedScene> ps = ResourceLoader::load(file.get_slice("::", 0));
+ stack_script = ResourceLoader::load(file);
+ }
+ emit_signal(SNAME("clear_execution"), stack_script);
+ stack_script.unref(); // Why?!?
+}
+
void EditorDebuggerNode::_bind_methods() {
// LiveDebug.
ClassDB::bind_method("live_debug_create_node", &EditorDebuggerNode::live_debug_create_node);
@@ -183,6 +206,8 @@ void EditorDebuggerNode::_bind_methods() {
ADD_SIGNAL(MethodInfo("clear_execution", PropertyInfo("script")));
ADD_SIGNAL(MethodInfo("breaked", PropertyInfo(Variant::BOOL, "reallydid"), PropertyInfo(Variant::BOOL, "can_debug")));
ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::BOOL, "enabled")));
+ ADD_SIGNAL(MethodInfo("breakpoint_set_in_tree", PropertyInfo("script"), PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::BOOL, "enabled"), PropertyInfo(Variant::INT, "debugger")));
+ ADD_SIGNAL(MethodInfo("breakpoints_cleared_in_tree", PropertyInfo(Variant::INT, "debugger")));
}
void EditorDebuggerNode::register_undo_redo(UndoRedo *p_undo_redo) {
@@ -198,6 +223,10 @@ ScriptEditorDebugger *EditorDebuggerNode::get_debugger(int p_id) const {
return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(p_id));
}
+ScriptEditorDebugger *EditorDebuggerNode::get_previous_debugger() const {
+ return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(tabs->get_previous_tab()));
+}
+
ScriptEditorDebugger *EditorDebuggerNode::get_current_debugger() const {
return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(tabs->get_current_tab()));
}
@@ -436,12 +465,18 @@ void EditorDebuggerNode::_debugger_changed(int p_tab) {
// Hopefully, in the future, we will have one inspector per debugger.
EditorNode::get_singleton()->push_item(nullptr);
}
+
+ if (get_previous_debugger()) {
+ _text_editor_stack_clear(get_previous_debugger());
+ }
if (remote_scene_tree->is_visible_in_tree()) {
get_current_debugger()->request_remote_tree();
}
if (get_current_debugger()->is_breaked()) {
_text_editor_stack_goto(get_current_debugger());
}
+
+ _break_state_changed();
}
void EditorDebuggerNode::set_script_debug_button(MenuButton *p_button) {
@@ -533,7 +568,7 @@ void EditorDebuggerNode::_breaked(bool p_breaked, bool p_can_debug, String p_mes
}
bool EditorDebuggerNode::is_skip_breakpoints() const {
- return get_default_debugger()->is_skip_breakpoints();
+ return get_current_debugger()->is_skip_breakpoints();
}
void EditorDebuggerNode::set_breakpoint(const String &p_path, int p_line, bool p_enabled) {
@@ -565,23 +600,23 @@ void EditorDebuggerNode::reload_scripts() {
}
void EditorDebuggerNode::debug_next() {
- get_default_debugger()->debug_next();
+ get_current_debugger()->debug_next();
}
void EditorDebuggerNode::debug_step() {
- get_default_debugger()->debug_step();
+ get_current_debugger()->debug_step();
}
void EditorDebuggerNode::debug_break() {
- get_default_debugger()->debug_break();
+ get_current_debugger()->debug_break();
}
void EditorDebuggerNode::debug_continue() {
- get_default_debugger()->debug_continue();
+ get_current_debugger()->debug_continue();
}
String EditorDebuggerNode::get_var_value(const String &p_var) const {
- return get_default_debugger()->get_var_value(p_var);
+ return get_current_debugger()->get_var_value(p_var);
}
// LiveEdit/Inspector
@@ -655,6 +690,22 @@ void EditorDebuggerNode::_save_node_requested(ObjectID p_id, const String &p_fil
get_current_debugger()->save_node(p_id, p_file);
}
+void EditorDebuggerNode::_breakpoint_set_in_tree(Ref<RefCounted> p_script, int p_line, bool p_enabled, int p_debugger) {
+ if (p_debugger != tabs->get_current_tab()) {
+ return;
+ }
+
+ emit_signal(SNAME("breakpoint_set_in_tree"), p_script, p_line, p_enabled);
+}
+
+void EditorDebuggerNode::_breakpoints_cleared_in_tree(int p_debugger) {
+ if (p_debugger != tabs->get_current_tab()) {
+ return;
+ }
+
+ emit_signal(SNAME("breakpoints_cleared_in_tree"));
+}
+
// Remote inspector/edit.
void EditorDebuggerNode::_method_changeds(void *p_ud, Object *p_base, const StringName &p_name, const Variant **p_args, int p_argcount) {
if (!singleton) {
diff --git a/editor/debugger/editor_debugger_node.h b/editor/debugger/editor_debugger_node.h
index 8ca709dd9b..4338f144b8 100644
--- a/editor/debugger/editor_debugger_node.h
+++ b/editor/debugger/editor_debugger_node.h
@@ -138,11 +138,15 @@ protected:
void _remote_object_requested(ObjectID p_id, int p_debugger);
void _save_node_requested(ObjectID p_id, const String &p_file, int p_debugger);
+ void _breakpoint_set_in_tree(Ref<RefCounted> p_script, int p_line, bool p_enabled, int p_debugger);
+ void _breakpoints_cleared_in_tree(int p_debugger);
+
void _clear_execution(Ref<RefCounted> p_script) {
emit_signal(SNAME("clear_execution"), p_script);
}
void _text_editor_stack_goto(const ScriptEditorDebugger *p_debugger);
+ void _text_editor_stack_clear(const ScriptEditorDebugger *p_debugger);
void _stack_frame_selected(int p_debugger);
void _error_selected(const String &p_file, int p_line, int p_debugger);
void _breaked(bool p_breaked, bool p_can_debug, String p_message, bool p_has_stackdump, int p_debugger);
@@ -159,6 +163,7 @@ public:
static EditorDebuggerNode *get_singleton() { return singleton; }
void register_undo_redo(UndoRedo *p_undo_redo);
+ ScriptEditorDebugger *get_previous_debugger() const;
ScriptEditorDebugger *get_current_debugger() const;
ScriptEditorDebugger *get_default_debugger() const;
ScriptEditorDebugger *get_debugger(int p_debugger) const;
diff --git a/editor/plugins/animation_player_editor_plugin.cpp b/editor/plugins/animation_player_editor_plugin.cpp
index 75c8ac11d0..440496b948 100644
--- a/editor/plugins/animation_player_editor_plugin.cpp
+++ b/editor/plugins/animation_player_editor_plugin.cpp
@@ -1051,7 +1051,6 @@ void AnimationPlayerEditor::_ensure_dummy_player() {
}
// Make some options disabled.
- tool_anim->get_popup()->set_item_disabled(tool_anim->get_popup()->get_item_index(TOOL_EDIT_TRANSITIONS), dummy_exists);
onion_toggle->set_disabled(dummy_exists);
onion_skinning->set_disabled(dummy_exists);
int selected = animation->get_selected();
@@ -1365,9 +1364,6 @@ void AnimationPlayerEditor::_animation_tool_menu(int p_option) {
_animation_rename();
} break;
case TOOL_EDIT_TRANSITIONS: {
- if (is_dummy) {
- break;
- }
_animation_blend();
} break;
case TOOL_REMOVE_ANIM: {
diff --git a/editor/plugins/mesh_instance_3d_editor_plugin.cpp b/editor/plugins/mesh_instance_3d_editor_plugin.cpp
index 8d112446a3..eec375cbea 100644
--- a/editor/plugins/mesh_instance_3d_editor_plugin.cpp
+++ b/editor/plugins/mesh_instance_3d_editor_plugin.cpp
@@ -44,6 +44,7 @@
#include "scene/gui/spin_box.h"
#include "scene/resources/concave_polygon_shape_3d.h"
#include "scene/resources/convex_polygon_shape_3d.h"
+#include "scene/resources/primitive_meshes.h"
#include "scene/scene_string_names.h"
void MeshInstance3DEditor::_node_removed(Node *p_node) {
@@ -300,59 +301,67 @@ void MeshInstance3DEditor::_menu_option(int p_option) {
ur->commit_action();
} break;
case MENU_OPTION_CREATE_UV2: {
- Ref<ArrayMesh> mesh2 = node->get_mesh();
- if (!mesh2.is_valid()) {
- err_dialog->set_text(TTR("Contained Mesh is not of type ArrayMesh."));
- err_dialog->popup_centered();
- return;
- }
+ Ref<PrimitiveMesh> primitive_mesh = Object::cast_to<PrimitiveMesh>(*node->get_mesh());
+ if (primitive_mesh.is_valid()) {
+ EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
+ ur->create_action(TTR("Unwrap UV2"));
+ ur->add_do_method(*primitive_mesh, "set_add_uv2", true);
+ ur->add_undo_method(*primitive_mesh, "set_add_uv2", primitive_mesh->get_add_uv2());
+ ur->commit_action();
+ } else {
+ Ref<ArrayMesh> mesh2 = node->get_mesh();
+ if (!mesh2.is_valid()) {
+ err_dialog->set_text(TTR("Contained Mesh is not of type ArrayMesh."));
+ err_dialog->popup_centered();
+ return;
+ }
- String path = mesh2->get_path();
- int srpos = path.find("::");
- if (srpos != -1) {
- String base = path.substr(0, srpos);
- if (ResourceLoader::get_resource_type(base) == "PackedScene") {
- if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
- err_dialog->set_text(TTR("Mesh cannot unwrap UVs because it does not belong to the edited scene. Make it unique first."));
- err_dialog->popup_centered();
- return;
+ String path = mesh2->get_path();
+ int srpos = path.find("::");
+ if (srpos != -1) {
+ String base = path.substr(0, srpos);
+ if (ResourceLoader::get_resource_type(base) == "PackedScene") {
+ if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
+ err_dialog->set_text(TTR("Mesh cannot unwrap UVs because it does not belong to the edited scene. Make it unique first."));
+ err_dialog->popup_centered();
+ return;
+ }
+ } else {
+ if (FileAccess::exists(path + ".import")) {
+ err_dialog->set_text(TTR("Mesh cannot unwrap UVs because it belongs to another resource which was imported from another file type. Make it unique first."));
+ err_dialog->popup_centered();
+ return;
+ }
}
} else {
if (FileAccess::exists(path + ".import")) {
- err_dialog->set_text(TTR("Mesh cannot unwrap UVs because it belongs to another resource which was imported from another file type. Make it unique first."));
+ err_dialog->set_text(TTR("Mesh cannot unwrap UVs because it was imported from another file type. Make it unique first."));
err_dialog->popup_centered();
return;
}
}
- } else {
- if (FileAccess::exists(path + ".import")) {
- err_dialog->set_text(TTR("Mesh cannot unwrap UVs because it was imported from another file type. Make it unique first."));
+
+ Ref<ArrayMesh> unwrapped_mesh = mesh2->duplicate(false);
+
+ Error err = unwrapped_mesh->lightmap_unwrap(node->get_global_transform());
+ if (err != OK) {
+ err_dialog->set_text(TTR("UV Unwrap failed, mesh may not be manifold?"));
err_dialog->popup_centered();
return;
}
- }
-
- Ref<ArrayMesh> unwrapped_mesh = mesh2->duplicate(false);
-
- Error err = unwrapped_mesh->lightmap_unwrap(node->get_global_transform());
- if (err != OK) {
- err_dialog->set_text(TTR("UV Unwrap failed, mesh may not be manifold?"));
- err_dialog->popup_centered();
- return;
- }
-
- EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
- ur->create_action(TTR("Unwrap UV2"));
- ur->add_do_method(node, "set_mesh", unwrapped_mesh);
- ur->add_do_reference(node);
- ur->add_do_reference(mesh2.ptr());
+ EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
+ ur->create_action(TTR("Unwrap UV2"));
- ur->add_undo_method(node, "set_mesh", mesh2);
- ur->add_undo_reference(unwrapped_mesh.ptr());
+ ur->add_do_method(node, "set_mesh", unwrapped_mesh);
+ ur->add_do_reference(node);
+ ur->add_do_reference(mesh2.ptr());
- ur->commit_action();
+ ur->add_undo_method(node, "set_mesh", mesh2);
+ ur->add_undo_reference(unwrapped_mesh.ptr());
+ ur->commit_action();
+ }
} break;
case MENU_OPTION_DEBUG_UV1: {
Ref<Mesh> mesh2 = node->get_mesh();
diff --git a/editor/plugins/script_editor_plugin.cpp b/editor/plugins/script_editor_plugin.cpp
index 79e260ef35..16d281d037 100644
--- a/editor/plugins/script_editor_plugin.cpp
+++ b/editor/plugins/script_editor_plugin.cpp
@@ -4023,8 +4023,8 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
debugger->connect("set_execution", callable_mp(this, &ScriptEditor::_set_execution));
debugger->connect("clear_execution", callable_mp(this, &ScriptEditor::_clear_execution));
debugger->connect("breaked", callable_mp(this, &ScriptEditor::_breaked));
- debugger->get_default_debugger()->connect("set_breakpoint", callable_mp(this, &ScriptEditor::_set_breakpoint));
- debugger->get_default_debugger()->connect("clear_breakpoints", callable_mp(this, &ScriptEditor::_clear_breakpoints));
+ debugger->connect("breakpoint_set_in_tree", callable_mp(this, &ScriptEditor::_set_breakpoint));
+ debugger->connect("breakpoints_cleared_in_tree", callable_mp(this, &ScriptEditor::_clear_breakpoints));
menu_hb->add_spacer();
diff --git a/editor/plugins/shader_editor_plugin.cpp b/editor/plugins/shader_editor_plugin.cpp
index 9eebf958ff..019d62a4bf 100644
--- a/editor/plugins/shader_editor_plugin.cpp
+++ b/editor/plugins/shader_editor_plugin.cpp
@@ -64,6 +64,11 @@ void ShaderEditorPlugin::_update_shader_list() {
}
}
+ // When shader is deleted in filesystem dock, need this to correctly close shader editor.
+ if (!path.is_empty()) {
+ shader->set_meta("_edit_res_path", path);
+ }
+
bool unsaved = false;
if (edited_shader.shader_editor) {
unsaved = edited_shader.shader_editor->is_unsaved();
@@ -571,10 +576,20 @@ void ShaderEditorPlugin::_window_changed(bool p_visible) {
make_floating->set_visible(!p_visible);
}
+void ShaderEditorPlugin::_file_removed(const String &p_removed_file) {
+ for (uint32_t i = 0; i < edited_shaders.size(); i++) {
+ const Ref<Shader> &shader = edited_shaders[i].shader;
+ if (shader->get_meta("_edit_res_path") == p_removed_file) {
+ _close_shader(i);
+ }
+ }
+}
+
void ShaderEditorPlugin::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_READY: {
EditorNode::get_singleton()->connect("scene_closed", callable_mp(this, &ShaderEditorPlugin::_close_builtin_shaders_from_scene));
+ FileSystemDock::get_singleton()->connect("file_removed", callable_mp(this, &ShaderEditorPlugin::_file_removed));
} break;
}
}
diff --git a/editor/plugins/shader_editor_plugin.h b/editor/plugins/shader_editor_plugin.h
index fb7b283266..8b48140262 100644
--- a/editor/plugins/shader_editor_plugin.h
+++ b/editor/plugins/shader_editor_plugin.h
@@ -87,6 +87,7 @@ class ShaderEditorPlugin : public EditorPlugin {
void _resource_saved(Object *obj);
void _close_shader(int p_index);
void _close_builtin_shaders_from_scene(const String &p_scene);
+ void _file_removed(const String &p_removed_file);
void _shader_created(Ref<Shader> p_shader);
void _shader_include_created(Ref<ShaderInclude> p_shader_inc);
diff --git a/editor/scene_tree_dock.cpp b/editor/scene_tree_dock.cpp
index a38487635f..d1e2ba0fe0 100644
--- a/editor/scene_tree_dock.cpp
+++ b/editor/scene_tree_dock.cpp
@@ -1061,6 +1061,10 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
break;
}
+ if (!_validate_no_foreign()) {
+ break;
+ }
+
List<Node *> selection = editor_selection->get_selected_node_list();
List<Node *>::Element *e = selection.front();
if (e) {
@@ -1635,10 +1639,15 @@ bool SceneTreeDock::_update_node_path(Node *p_root_node, NodePath &r_node_path,
return false;
}
-bool SceneTreeDock::_check_node_path_recursive(Node *p_root_node, Variant &r_variant, HashMap<Node *, NodePath> *p_renames) const {
+bool SceneTreeDock::_check_node_path_recursive(Node *p_root_node, Variant &r_variant, HashMap<Node *, NodePath> *p_renames, bool p_inside_resource) const {
switch (r_variant.get_type()) {
case Variant::NODE_PATH: {
NodePath node_path = r_variant;
+ if (p_inside_resource && !p_root_node->has_node(node_path)) {
+ // Resources may have NodePaths to nodes that aren't on the scene, so skip them.
+ return false;
+ }
+
if (!node_path.is_empty() && _update_node_path(p_root_node, node_path, p_renames)) {
r_variant = node_path;
return true;
@@ -1650,7 +1659,7 @@ bool SceneTreeDock::_check_node_path_recursive(Node *p_root_node, Variant &r_var
bool updated = false;
for (int i = 0; i < a.size(); i++) {
Variant value = a[i];
- if (_check_node_path_recursive(p_root_node, value, p_renames)) {
+ if (_check_node_path_recursive(p_root_node, value, p_renames, p_inside_resource)) {
if (!updated) {
a = a.duplicate(); // Need to duplicate for undo-redo to work.
updated = true;
@@ -1669,7 +1678,7 @@ bool SceneTreeDock::_check_node_path_recursive(Node *p_root_node, Variant &r_var
bool updated = false;
for (int i = 0; i < d.size(); i++) {
Variant value = d.get_value_at_index(i);
- if (_check_node_path_recursive(p_root_node, value, p_renames)) {
+ if (_check_node_path_recursive(p_root_node, value, p_renames, p_inside_resource)) {
if (!updated) {
d = d.duplicate(); // Need to duplicate for undo-redo to work.
updated = true;
@@ -1699,7 +1708,7 @@ bool SceneTreeDock::_check_node_path_recursive(Node *p_root_node, Variant &r_var
String propertyname = E.name;
Variant old_variant = resource->get(propertyname);
Variant updated_variant = old_variant;
- if (_check_node_path_recursive(p_root_node, updated_variant, p_renames)) {
+ if (_check_node_path_recursive(p_root_node, updated_variant, p_renames, true)) {
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->add_do_property(resource, propertyname, updated_variant);
undo_redo->add_undo_property(resource, propertyname, old_variant);
diff --git a/editor/scene_tree_dock.h b/editor/scene_tree_dock.h
index 9c111bce71..3f02d2881d 100644
--- a/editor/scene_tree_dock.h
+++ b/editor/scene_tree_dock.h
@@ -281,7 +281,7 @@ class SceneTreeDock : public VBoxContainer {
static void _update_configuration_warning();
bool _update_node_path(Node *p_root_node, NodePath &r_node_path, HashMap<Node *, NodePath> *p_renames) const;
- bool _check_node_path_recursive(Node *p_root_node, Variant &r_variant, HashMap<Node *, NodePath> *p_renames) const;
+ bool _check_node_path_recursive(Node *p_root_node, Variant &r_variant, HashMap<Node *, NodePath> *p_renames, bool p_inside_resource = false) const;
bool _check_node_recursive(Variant &r_variant, Node *p_node, Node *p_by_node, const String type_hint, String &r_warn_message);
void _replace_node(Node *p_node, Node *p_by_node, bool p_keep_properties = true, bool p_remove_old = true);
diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp
index 23d0eb8b67..44bcd4cfe4 100644
--- a/modules/mono/csharp_script.cpp
+++ b/modules/mono/csharp_script.cpp
@@ -121,16 +121,16 @@ void CSharpLanguage::init() {
GLOBAL_DEF(PropertyInfo(Variant::INT, "dotnet/project/assembly_reload_attempts", PROPERTY_HINT_RANGE, "1,16,1,or_greater"), 3);
#endif
- gdmono = memnew(GDMono);
- gdmono->initialize();
-
#ifdef TOOLS_ENABLED
- if (gdmono->is_runtime_initialized()) {
- gdmono->initialize_load_assemblies();
- }
-
EditorNode::add_init_callback(&_editor_init_callback);
#endif
+
+ gdmono = memnew(GDMono);
+
+ // Initialize only if the project uses C#.
+ if (gdmono->should_initialize()) {
+ gdmono->initialize();
+ }
}
void CSharpLanguage::finish() {
diff --git a/modules/mono/editor/GodotTools/GodotTools/Export/ExportPlugin.cs b/modules/mono/editor/GodotTools/GodotTools/Export/ExportPlugin.cs
index 018298cb33..91e5118990 100644
--- a/modules/mono/editor/GodotTools/GodotTools/Export/ExportPlugin.cs
+++ b/modules/mono/editor/GodotTools/GodotTools/Export/ExportPlugin.cs
@@ -20,6 +20,19 @@ namespace GodotTools.Export
private List<string> _tempFolders = new List<string>();
+ private static bool ProjectContainsDotNet()
+ {
+ return File.Exists(GodotSharpDirs.ProjectSlnPath);
+ }
+
+ public override string[] _GetExportFeatures(EditorExportPlatform platform, bool debug)
+ {
+ if (!ProjectContainsDotNet())
+ return Array.Empty<string>();
+
+ return new string[] { "dotnet" };
+ }
+
public override Godot.Collections.Array<Godot.Collections.Dictionary> _GetExportOptions(EditorExportPlatform platform)
{
return new Godot.Collections.Array<Godot.Collections.Dictionary>()
@@ -119,7 +132,7 @@ namespace GodotTools.Export
{
_ = flags; // Unused.
- if (!File.Exists(GodotSharpDirs.ProjectSlnPath))
+ if (!ProjectContainsDotNet())
return;
if (!DeterminePlatformFromFeatures(features, out string platform))
diff --git a/modules/mono/mono_gd/gd_mono.cpp b/modules/mono/mono_gd/gd_mono.cpp
index 23f2f2ff13..ca2ad315a7 100644
--- a/modules/mono/mono_gd/gd_mono.cpp
+++ b/modules/mono/mono_gd/gd_mono.cpp
@@ -348,6 +348,15 @@ godot_plugins_initialize_fn try_load_native_aot_library(void *&r_aot_dll_handle)
} // namespace
+bool GDMono::should_initialize() {
+#ifdef TOOLS_ENABLED
+ // The editor always needs to initialize the .NET module for now.
+ return true;
+#else
+ return OS::get_singleton()->has_feature("dotnet");
+#endif
+}
+
static bool _on_core_api_assembly_loaded() {
if (!GDMonoCache::godot_api_cache_updated) {
return false;
@@ -435,11 +444,15 @@ void GDMono::initialize() {
_on_core_api_assembly_loaded();
+#ifdef TOOLS_ENABLED
+ _try_load_project_assembly();
+#endif
+
initialized = true;
}
#ifdef TOOLS_ENABLED
-void GDMono::initialize_load_assemblies() {
+void GDMono::_try_load_project_assembly() {
if (Engine::get_singleton()->is_project_manager_hint()) {
return;
}
diff --git a/modules/mono/mono_gd/gd_mono.h b/modules/mono/mono_gd/gd_mono.h
index c629ab2eff..530936cfb4 100644
--- a/modules/mono/mono_gd/gd_mono.h
+++ b/modules/mono/mono_gd/gd_mono.h
@@ -72,6 +72,7 @@ class GDMono {
#ifdef TOOLS_ENABLED
bool _load_project_assembly();
+ void _try_load_project_assembly();
#endif
uint64_t api_core_hash = 0;
@@ -149,10 +150,9 @@ public:
Error reload_project_assemblies();
#endif
+ bool should_initialize();
+
void initialize();
-#ifdef TOOLS_ENABLED
- void initialize_load_assemblies();
-#endif
GDMono();
~GDMono();
diff --git a/modules/svg/SCsub b/modules/svg/SCsub
index a99bc8df60..d0c6250b11 100644
--- a/modules/svg/SCsub
+++ b/modules/svg/SCsub
@@ -24,6 +24,9 @@ thirdparty_sources = [
"src/loaders/svg/tvgSvgUtil.cpp",
"src/loaders/svg/tvgXmlParser.cpp",
"src/loaders/raw/tvgRawLoader.cpp",
+ "src/loaders/external_png/tvgPngLoader.cpp",
+ "src/loaders/jpg/tvgJpgd.cpp",
+ "src/loaders/jpg/tvgJpgLoader.cpp",
# renderer common
"src/renderer/tvgAccessor.cpp",
# "src/renderer/tvgAnimation.cpp",
@@ -68,6 +71,8 @@ env_thirdparty.Prepend(
thirdparty_dir + "src/renderer",
thirdparty_dir + "src/renderer/sw_engine",
thirdparty_dir + "src/loaders/raw",
+ thirdparty_dir + "src/loaders/external_png",
+ thirdparty_dir + "src/loaders/jpg",
]
)
diff --git a/modules/text_server_adv/gdextension_build/SConstruct b/modules/text_server_adv/gdextension_build/SConstruct
index b95c35f80d..4093842650 100644
--- a/modules/text_server_adv/gdextension_build/SConstruct
+++ b/modules/text_server_adv/gdextension_build/SConstruct
@@ -55,6 +55,9 @@ if env["thorvg_enabled"] and env["freetype_enabled"]:
"src/loaders/svg/tvgSvgUtil.cpp",
"src/loaders/svg/tvgXmlParser.cpp",
"src/loaders/raw/tvgRawLoader.cpp",
+ "src/loaders/external_png/tvgPngLoader.cpp",
+ "src/loaders/jpg/tvgJpgd.cpp",
+ "src/loaders/jpg/tvgJpgLoader.cpp",
# renderer common
"src/renderer/tvgAccessor.cpp",
# "src/renderer/tvgAnimation.cpp",
@@ -88,10 +91,12 @@ if env["thorvg_enabled"] and env["freetype_enabled"]:
CPPPATH=[
"../../../thirdparty/thorvg/inc",
"../../../thirdparty/thorvg/src/common",
- "../../../thirdparty/thorvg/src/loaders/svg",
- "../../../thirdparty/thorvg/src/loaders/raw",
"../../../thirdparty/thorvg/src/renderer",
"../../../thirdparty/thorvg/src/renderer/sw_engine",
+ "../../../thirdparty/thorvg/src/loaders/svg",
+ "../../../thirdparty/thorvg/src/loaders/raw",
+ "../../../thirdparty/thorvg/src/loaders/external_png",
+ "../../../thirdparty/thorvg/src/loaders/jpg",
]
)
diff --git a/modules/text_server_fb/gdextension_build/SConstruct b/modules/text_server_fb/gdextension_build/SConstruct
index 846ac02cf1..0d2fbd97fd 100644
--- a/modules/text_server_fb/gdextension_build/SConstruct
+++ b/modules/text_server_fb/gdextension_build/SConstruct
@@ -50,6 +50,9 @@ if env["thorvg_enabled"] and env["freetype_enabled"]:
"src/loaders/svg/tvgSvgUtil.cpp",
"src/loaders/svg/tvgXmlParser.cpp",
"src/loaders/raw/tvgRawLoader.cpp",
+ "src/loaders/external_png/tvgPngLoader.cpp",
+ "src/loaders/jpg/tvgJpgd.cpp",
+ "src/loaders/jpg/tvgJpgLoader.cpp",
# renderer common
"src/renderer/tvgAccessor.cpp",
# "src/renderer/tvgAnimation.cpp",
@@ -83,10 +86,12 @@ if env["thorvg_enabled"] and env["freetype_enabled"]:
CPPPATH=[
"../../../thirdparty/thorvg/inc",
"../../../thirdparty/thorvg/src/common",
- "../../../thirdparty/thorvg/src/loaders/svg",
- "../../../thirdparty/thorvg/src/loaders/raw",
"../../../thirdparty/thorvg/src/renderer",
"../../../thirdparty/thorvg/src/renderer/sw_engine",
+ "../../../thirdparty/thorvg/src/loaders/svg",
+ "../../../thirdparty/thorvg/src/loaders/raw",
+ "../../../thirdparty/thorvg/src/loaders/external_png",
+ "../../../thirdparty/thorvg/src/loaders/jpg",
]
)
diff --git a/platform/android/doc_classes/EditorExportPlatformAndroid.xml b/platform/android/doc_classes/EditorExportPlatformAndroid.xml
index 0f1da54376..dae968378b 100644
--- a/platform/android/doc_classes/EditorExportPlatformAndroid.xml
+++ b/platform/android/doc_classes/EditorExportPlatformAndroid.xml
@@ -7,7 +7,7 @@
</description>
<tutorials>
<link title="Exporting for Android">$DOCS_URL/tutorials/export/exporting_for_android.html</link>
- <link title="Custom builds for Android">$DOCS_URL/tutorials/export/android_custom_build.html</link>
+ <link title="Gradle builds for Android">$DOCS_URL/tutorials/export/android_gradle_build.html</link>
<link title="Android plugins documentation index">$DOCS_URL/tutorials/platform/index.html</link>
</tutorials>
<members>
diff --git a/platform/android/java/lib/src/org/godotengine/godot/Godot.kt b/platform/android/java/lib/src/org/godotengine/godot/Godot.kt
index 04d196a6e9..857f239e96 100644
--- a/platform/android/java/lib/src/org/godotengine/godot/Godot.kt
+++ b/platform/android/java/lib/src/org/godotengine/godot/Godot.kt
@@ -389,9 +389,6 @@ class Godot(private val context: Context) : SensorEventListener {
GodotGLRenderView(host, this, xrMode, useDebugOpengl)
}
- renderView?.inputHandler?.enableLongPress(java.lang.Boolean.parseBoolean(GodotLib.getGlobal("input_devices/pointing/android/enable_long_press_as_right_click")))
- renderView?.inputHandler?.enablePanningAndScalingGestures(java.lang.Boolean.parseBoolean(GodotLib.getGlobal("input_devices/pointing/android/enable_pan_and_scale_gestures")))
-
if (host == primaryHost) {
renderView!!.startRenderer()
}
@@ -575,6 +572,19 @@ class Godot(private val context: Context) : SensorEventListener {
* Invoked on the render thread when the Godot setup is complete.
*/
private fun onGodotSetupCompleted() {
+ Log.d(TAG, "OnGodotSetupCompleted")
+
+ // These properties are defined after Godot setup completion, so we retrieve them here.
+ val longPressEnabled = java.lang.Boolean.parseBoolean(GodotLib.getGlobal("input_devices/pointing/android/enable_long_press_as_right_click"))
+ val panScaleEnabled = java.lang.Boolean.parseBoolean(GodotLib.getGlobal("input_devices/pointing/android/enable_pan_and_scale_gestures"))
+
+ runOnUiThread {
+ renderView?.inputHandler?.apply {
+ enableLongPress(longPressEnabled)
+ enablePanningAndScalingGestures(panScaleEnabled)
+ }
+ }
+
for (plugin in pluginRegistry.allPlugins) {
plugin.onGodotSetupCompleted()
}
@@ -585,6 +595,8 @@ class Godot(private val context: Context) : SensorEventListener {
* Invoked on the render thread when the Godot main loop has started.
*/
private fun onGodotMainLoopStarted() {
+ Log.d(TAG, "OnGodotMainLoopStarted")
+
for (plugin in pluginRegistry.allPlugins) {
plugin.onGodotMainLoopStarted()
}
diff --git a/scene/2d/camera_2d.cpp b/scene/2d/camera_2d.cpp
index 78987738a5..0f5fa6543e 100644
--- a/scene/2d/camera_2d.cpp
+++ b/scene/2d/camera_2d.cpp
@@ -209,8 +209,6 @@ Transform2D Camera2D::get_camera_transform() {
screen_rect.position += offset;
}
- camera_screen_center = screen_rect.get_center();
-
Transform2D xform;
xform.scale_basis(zoom_scale);
if (!ignore_rotation) {
@@ -218,7 +216,9 @@ Transform2D Camera2D::get_camera_transform() {
}
xform.set_origin(screen_rect.position);
- return (xform).affine_inverse();
+ camera_screen_center = xform.xform(0.5 * screen_size);
+
+ return xform.affine_inverse();
}
void Camera2D::_notification(int p_what) {
diff --git a/scene/2d/navigation_region_2d.cpp b/scene/2d/navigation_region_2d.cpp
index 706b26bd05..78733f04e4 100644
--- a/scene/2d/navigation_region_2d.cpp
+++ b/scene/2d/navigation_region_2d.cpp
@@ -497,20 +497,18 @@ void NavigationRegion2D::_region_enter_navigation_map() {
return;
}
- if (enabled) {
- if (map_override.is_valid()) {
- NavigationServer2D::get_singleton()->region_set_map(region, map_override);
- for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
- if (constrain_avoidance_obstacles[i].is_valid()) {
- NavigationServer2D::get_singleton()->obstacle_set_map(constrain_avoidance_obstacles[i], map_override);
- }
+ if (map_override.is_valid()) {
+ NavigationServer2D::get_singleton()->region_set_map(region, map_override);
+ for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
+ if (constrain_avoidance_obstacles[i].is_valid()) {
+ NavigationServer2D::get_singleton()->obstacle_set_map(constrain_avoidance_obstacles[i], map_override);
}
- } else {
- NavigationServer2D::get_singleton()->region_set_map(region, get_world_2d()->get_navigation_map());
- for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
- if (constrain_avoidance_obstacles[i].is_valid()) {
- NavigationServer2D::get_singleton()->obstacle_set_map(constrain_avoidance_obstacles[i], get_world_2d()->get_navigation_map());
- }
+ }
+ } else {
+ NavigationServer2D::get_singleton()->region_set_map(region, get_world_2d()->get_navigation_map());
+ for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
+ if (constrain_avoidance_obstacles[i].is_valid()) {
+ NavigationServer2D::get_singleton()->obstacle_set_map(constrain_avoidance_obstacles[i], get_world_2d()->get_navigation_map());
}
}
}
@@ -523,6 +521,8 @@ void NavigationRegion2D::_region_enter_navigation_map() {
}
}
+ NavigationServer2D::get_singleton()->region_set_enabled(region, enabled);
+
queue_redraw();
}
diff --git a/scene/2d/physics_body_2d.cpp b/scene/2d/physics_body_2d.cpp
index fa94712396..baa3b0bb90 100644
--- a/scene/2d/physics_body_2d.cpp
+++ b/scene/2d/physics_body_2d.cpp
@@ -75,7 +75,7 @@ Ref<KinematicCollision2D> PhysicsBody2D::_move(const Vector2 &p_motion, bool p_t
bool PhysicsBody2D::move_and_collide(const PhysicsServer2D::MotionParameters &p_parameters, PhysicsServer2D::MotionResult &r_result, bool p_test_only, bool p_cancel_sliding) {
if (is_only_update_transform_changes_enabled()) {
- ERR_PRINT("Move functions do not work together with 'sync to physics' option. Please read the documentation.");
+ ERR_PRINT("Move functions do not work together with 'sync to physics' option. See the documentation for details.");
}
bool colliding = PhysicsServer2D::get_singleton()->body_test_motion(get_rid(), p_parameters, &r_result);
diff --git a/scene/2d/remote_transform_2d.cpp b/scene/2d/remote_transform_2d.cpp
index cd0d9f3750..f7de4f3376 100644
--- a/scene/2d/remote_transform_2d.cpp
+++ b/scene/2d/remote_transform_2d.cpp
@@ -114,6 +114,7 @@ void RemoteTransform2D::_notification(int p_what) {
_update_cache();
} break;
+ case NOTIFICATION_LOCAL_TRANSFORM_CHANGED:
case NOTIFICATION_TRANSFORM_CHANGED: {
if (!is_inside_tree()) {
break;
@@ -127,6 +128,10 @@ void RemoteTransform2D::_notification(int p_what) {
}
void RemoteTransform2D::set_remote_node(const NodePath &p_remote_node) {
+ if (remote_node == p_remote_node) {
+ return;
+ }
+
remote_node = p_remote_node;
if (is_inside_tree()) {
_update_cache();
@@ -141,7 +146,13 @@ NodePath RemoteTransform2D::get_remote_node() const {
}
void RemoteTransform2D::set_use_global_coordinates(const bool p_enable) {
+ if (use_global_coordinates == p_enable) {
+ return;
+ }
+
use_global_coordinates = p_enable;
+ set_notify_transform(use_global_coordinates);
+ set_notify_local_transform(!use_global_coordinates);
_update_remote();
}
@@ -150,6 +161,9 @@ bool RemoteTransform2D::get_use_global_coordinates() const {
}
void RemoteTransform2D::set_update_position(const bool p_update) {
+ if (update_remote_position == p_update) {
+ return;
+ }
update_remote_position = p_update;
_update_remote();
}
@@ -159,6 +173,9 @@ bool RemoteTransform2D::get_update_position() const {
}
void RemoteTransform2D::set_update_rotation(const bool p_update) {
+ if (update_remote_rotation == p_update) {
+ return;
+ }
update_remote_rotation = p_update;
_update_remote();
}
@@ -168,6 +185,9 @@ bool RemoteTransform2D::get_update_rotation() const {
}
void RemoteTransform2D::set_update_scale(const bool p_update) {
+ if (update_remote_scale == p_update) {
+ return;
+ }
update_remote_scale = p_update;
_update_remote();
}
@@ -215,6 +235,7 @@ void RemoteTransform2D::_bind_methods() {
}
RemoteTransform2D::RemoteTransform2D() {
- set_notify_transform(true);
+ set_notify_transform(use_global_coordinates);
+ set_notify_local_transform(!use_global_coordinates);
set_hide_clip_children(true);
}
diff --git a/scene/3d/navigation_region_3d.cpp b/scene/3d/navigation_region_3d.cpp
index e03640f6cb..b376a4945e 100644
--- a/scene/3d/navigation_region_3d.cpp
+++ b/scene/3d/navigation_region_3d.cpp
@@ -356,17 +356,17 @@ void NavigationRegion3D::_region_enter_navigation_map() {
return;
}
- if (enabled) {
- if (map_override.is_valid()) {
- NavigationServer3D::get_singleton()->region_set_map(region, map_override);
- } else {
- NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
- }
+ if (map_override.is_valid()) {
+ NavigationServer3D::get_singleton()->region_set_map(region, map_override);
+ } else {
+ NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
}
current_global_transform = get_global_transform();
NavigationServer3D::get_singleton()->region_set_transform(region, current_global_transform);
+ NavigationServer3D::get_singleton()->region_set_enabled(region, enabled);
+
#ifdef DEBUG_ENABLED
if (NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
_update_debug_mesh();
diff --git a/scene/3d/remote_transform_3d.cpp b/scene/3d/remote_transform_3d.cpp
index fe309a82f8..add58da4ba 100644
--- a/scene/3d/remote_transform_3d.cpp
+++ b/scene/3d/remote_transform_3d.cpp
@@ -113,6 +113,7 @@ void RemoteTransform3D::_notification(int p_what) {
_update_cache();
} break;
+ case NOTIFICATION_LOCAL_TRANSFORM_CHANGED:
case NOTIFICATION_TRANSFORM_CHANGED: {
if (!is_inside_tree()) {
break;
@@ -126,6 +127,10 @@ void RemoteTransform3D::_notification(int p_what) {
}
void RemoteTransform3D::set_remote_node(const NodePath &p_remote_node) {
+ if (remote_node == p_remote_node) {
+ return;
+ }
+
remote_node = p_remote_node;
if (is_inside_tree()) {
_update_cache();
@@ -140,7 +145,15 @@ NodePath RemoteTransform3D::get_remote_node() const {
}
void RemoteTransform3D::set_use_global_coordinates(const bool p_enable) {
+ if (use_global_coordinates == p_enable) {
+ return;
+ }
+
use_global_coordinates = p_enable;
+
+ set_notify_transform(use_global_coordinates);
+ set_notify_local_transform(!use_global_coordinates);
+ _update_remote();
}
bool RemoteTransform3D::get_use_global_coordinates() const {
@@ -148,6 +161,9 @@ bool RemoteTransform3D::get_use_global_coordinates() const {
}
void RemoteTransform3D::set_update_position(const bool p_update) {
+ if (update_remote_position == p_update) {
+ return;
+ }
update_remote_position = p_update;
_update_remote();
}
@@ -157,6 +173,9 @@ bool RemoteTransform3D::get_update_position() const {
}
void RemoteTransform3D::set_update_rotation(const bool p_update) {
+ if (update_remote_rotation == p_update) {
+ return;
+ }
update_remote_rotation = p_update;
_update_remote();
}
@@ -166,6 +185,9 @@ bool RemoteTransform3D::get_update_rotation() const {
}
void RemoteTransform3D::set_update_scale(const bool p_update) {
+ if (update_remote_scale == p_update) {
+ return;
+ }
update_remote_scale = p_update;
_update_remote();
}
@@ -213,5 +235,6 @@ void RemoteTransform3D::_bind_methods() {
}
RemoteTransform3D::RemoteTransform3D() {
- set_notify_transform(true);
+ set_notify_transform(use_global_coordinates);
+ set_notify_local_transform(!use_global_coordinates);
}
diff --git a/scene/animation/animation_mixer.cpp b/scene/animation/animation_mixer.cpp
index cca1c6efbb..879c39eec9 100644
--- a/scene/animation/animation_mixer.cpp
+++ b/scene/animation/animation_mixer.cpp
@@ -871,21 +871,23 @@ bool AnimationMixer::_update_caches() {
// TODO: Currently, misc type cannot be blended. In the future,
// it should have a separate blend weight, just as bool is converted to 0 and 1.
// Then, it should provide the correct precedence value.
- switch (track_value->init_value.get_type()) {
- case Variant::NIL:
- case Variant::STRING_NAME:
- case Variant::NODE_PATH:
- case Variant::RID:
- case Variant::OBJECT:
- case Variant::CALLABLE:
- case Variant::SIGNAL:
- case Variant::DICTIONARY:
- case Variant::ARRAY: {
- WARN_PRINT_ONCE_ED("AnimationMixer: '" + String(E) + "', Value Track: '" + String(path) + "' uses a non-numeric type as key value with UpdateMode.UPDATE_CONTINUOUS. This will not be blended correctly, so it is forced to UpdateMode.UPDATE_DISCRETE.");
- track_value->is_continuous = false;
- break;
- }
- default: {
+ if (track_value->is_continuous) {
+ switch (track_value->init_value.get_type()) {
+ case Variant::NIL:
+ case Variant::STRING_NAME:
+ case Variant::NODE_PATH:
+ case Variant::RID:
+ case Variant::OBJECT:
+ case Variant::CALLABLE:
+ case Variant::SIGNAL:
+ case Variant::DICTIONARY:
+ case Variant::ARRAY: {
+ WARN_PRINT_ONCE_ED("AnimationMixer: '" + String(E) + "', Value Track: '" + String(path) + "' uses a non-numeric type as key value with UpdateMode.UPDATE_CONTINUOUS. This will not be blended correctly, so it is forced to UpdateMode.UPDATE_DISCRETE.");
+ track_value->is_continuous = false;
+ break;
+ }
+ default: {
+ }
}
}
diff --git a/scene/gui/tab_bar.cpp b/scene/gui/tab_bar.cpp
index 7e9b566438..8af73deefb 100644
--- a/scene/gui/tab_bar.cpp
+++ b/scene/gui/tab_bar.cpp
@@ -567,6 +567,8 @@ void TabBar::_draw_tab(Ref<StyleBox> &p_tab_style, Color &p_font_color, int p_in
rb->draw(ci, Point2i(rb_rect.position.x + style->get_margin(SIDE_LEFT), rb_rect.position.y + style->get_margin(SIDE_TOP)));
p_x = rtl ? rb_rect.position.x : rb_rect.position.x + rb_rect.size.width;
+ } else {
+ tabs.write[p_index].rb_rect = Rect2();
}
// Draw and calculate rect of the close button.
diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp
index 073f164bdd..1cef81305b 100644
--- a/scene/gui/text_edit.cpp
+++ b/scene/gui/text_edit.cpp
@@ -1685,10 +1685,10 @@ void TextEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
h_scroll->set_value(h_scroll->get_value() - (100 * mb->get_factor()));
} else if (mb->is_alt_pressed()) {
// Scroll 5 times as fast as normal (like in Visual Studio Code).
- _scroll_up(15 * mb->get_factor());
+ _scroll_up(15 * mb->get_factor(), true);
} else if (v_scroll->is_visible()) {
// Scroll 3 lines.
- _scroll_up(3 * mb->get_factor());
+ _scroll_up(3 * mb->get_factor(), true);
}
}
if (mb->get_button_index() == MouseButton::WHEEL_DOWN && !mb->is_command_or_control_pressed()) {
@@ -1696,10 +1696,10 @@ void TextEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
h_scroll->set_value(h_scroll->get_value() + (100 * mb->get_factor()));
} else if (mb->is_alt_pressed()) {
// Scroll 5 times as fast as normal (like in Visual Studio Code).
- _scroll_down(15 * mb->get_factor());
+ _scroll_down(15 * mb->get_factor(), true);
} else if (v_scroll->is_visible()) {
// Scroll 3 lines.
- _scroll_down(3 * mb->get_factor());
+ _scroll_down(3 * mb->get_factor(), true);
}
}
if (mb->get_button_index() == MouseButton::WHEEL_LEFT) {
@@ -1933,9 +1933,9 @@ void TextEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
if (pan_gesture.is_valid()) {
const real_t delta = pan_gesture->get_delta().y;
if (delta < 0) {
- _scroll_up(-delta);
+ _scroll_up(-delta, false);
} else {
- _scroll_down(delta);
+ _scroll_down(delta, false);
}
h_scroll->set_value(h_scroll->get_value() + pan_gesture->get_delta().x * 100);
if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) {
@@ -5468,7 +5468,7 @@ void TextEdit::set_line_as_last_visible(int p_line, int p_wrap_index) {
set_v_scroll(0);
return;
}
- set_v_scroll(get_scroll_pos_for_line(first_line, next_line.y) + _get_visible_lines_offset());
+ set_v_scroll(Math::round(get_scroll_pos_for_line(first_line, next_line.y) + _get_visible_lines_offset()));
}
int TextEdit::get_last_full_visible_line() const {
@@ -7357,11 +7357,6 @@ void TextEdit::_update_scrollbars() {
v_scroll->show();
v_scroll->set_max(total_rows + _get_visible_lines_offset());
v_scroll->set_page(visible_rows + _get_visible_lines_offset());
- if (smooth_scroll_enabled) {
- v_scroll->set_step(0.25);
- } else {
- v_scroll->set_step(1);
- }
set_v_scroll(get_v_scroll());
} else {
@@ -7455,7 +7450,7 @@ double TextEdit::_get_v_scroll_offset() const {
return CLAMP(val, 0, 1);
}
-void TextEdit::_scroll_up(real_t p_delta) {
+void TextEdit::_scroll_up(real_t p_delta, bool p_animate) {
if (scrolling && smooth_scroll_enabled && SIGN(target_v_scroll - v_scroll->get_value()) != SIGN(-p_delta)) {
scrolling = false;
minimap_clicked = false;
@@ -7471,7 +7466,7 @@ void TextEdit::_scroll_up(real_t p_delta) {
if (target_v_scroll <= 0) {
target_v_scroll = 0;
}
- if (Math::abs(target_v_scroll - v_scroll->get_value()) < 1.0) {
+ if (!p_animate || Math::abs(target_v_scroll - v_scroll->get_value()) < 1.0) {
v_scroll->set_value(target_v_scroll);
} else {
scrolling = true;
@@ -7482,7 +7477,7 @@ void TextEdit::_scroll_up(real_t p_delta) {
}
}
-void TextEdit::_scroll_down(real_t p_delta) {
+void TextEdit::_scroll_down(real_t p_delta, bool p_animate) {
if (scrolling && smooth_scroll_enabled && SIGN(target_v_scroll - v_scroll->get_value()) != SIGN(p_delta)) {
scrolling = false;
minimap_clicked = false;
@@ -7499,7 +7494,7 @@ void TextEdit::_scroll_down(real_t p_delta) {
if (target_v_scroll > max_v_scroll) {
target_v_scroll = max_v_scroll;
}
- if (Math::abs(target_v_scroll - v_scroll->get_value()) < 1.0) {
+ if (!p_animate || Math::abs(target_v_scroll - v_scroll->get_value()) < 1.0) {
v_scroll->set_value(target_v_scroll);
} else {
scrolling = true;
@@ -7605,9 +7600,9 @@ void TextEdit::_update_minimap_click() {
int first_line = row - next_line.x + 1;
double delta = get_scroll_pos_for_line(first_line, next_line.y) - get_v_scroll();
if (delta < 0) {
- _scroll_up(-delta);
+ _scroll_up(-delta, true);
} else {
- _scroll_down(delta);
+ _scroll_down(delta, true);
}
}
diff --git a/scene/gui/text_edit.h b/scene/gui/text_edit.h
index 56d5b67e0b..8a541b623b 100644
--- a/scene/gui/text_edit.h
+++ b/scene/gui/text_edit.h
@@ -500,8 +500,8 @@ private:
double _get_visible_lines_offset() const;
double _get_v_scroll_offset() const;
- void _scroll_up(real_t p_delta);
- void _scroll_down(real_t p_delta);
+ void _scroll_up(real_t p_delta, bool p_animate);
+ void _scroll_down(real_t p_delta, bool p_animate);
void _scroll_lines_up();
void _scroll_lines_down();
diff --git a/scene/main/instance_placeholder.cpp b/scene/main/instance_placeholder.cpp
index b59dad716c..fe23ca1800 100644
--- a/scene/main/instance_placeholder.cpp
+++ b/scene/main/instance_placeholder.cpp
@@ -93,6 +93,7 @@ Node *InstancePlaceholder::create_instance(bool p_replace, const Ref<PackedScene
return nullptr;
}
scene->set_name(get_name());
+ scene->set_multiplayer_authority(get_multiplayer_authority());
int pos = get_index();
for (const PropSet &E : stored_values) {
diff --git a/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.cpp b/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.cpp
index 9a3556ce35..b2c0d4b1bd 100644
--- a/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.cpp
+++ b/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.cpp
@@ -621,7 +621,7 @@ void SceneShaderForwardMobile::init(const String p_defines) {
actions.default_filter = ShaderLanguage::FILTER_LINEAR_MIPMAP;
actions.default_repeat = ShaderLanguage::REPEAT_ENABLE;
actions.global_buffer_array_variable = "global_shader_uniforms.data";
- actions.instance_uniform_index_variable = "instances.data[instance_index].instance_uniforms_ofs";
+ actions.instance_uniform_index_variable = "instances.data[draw_call.instance_index].instance_uniforms_ofs";
actions.apply_luminance_multiplier = true; // apply luminance multiplier to screen texture
actions.check_multiview_samplers = RendererCompositorRD::get_singleton()->is_xr_enabled(); // Make sure we check sampling multiview textures.
diff --git a/servers/rendering_server.cpp b/servers/rendering_server.cpp
index 137cbfa861..ccddaae131 100644
--- a/servers/rendering_server.cpp
+++ b/servers/rendering_server.cpp
@@ -3375,8 +3375,8 @@ void RenderingServer::init() {
GLOBAL_DEF("rendering/shading/overrides/force_lambert_over_burley", false);
GLOBAL_DEF("rendering/shading/overrides/force_lambert_over_burley.mobile", true);
- GLOBAL_DEF("rendering/driver/depth_prepass/enable", true);
- GLOBAL_DEF("rendering/driver/depth_prepass/disable_for_vendors", "PowerVR,Mali,Adreno,Apple");
+ GLOBAL_DEF_RST("rendering/driver/depth_prepass/enable", true);
+ GLOBAL_DEF_RST("rendering/driver/depth_prepass/disable_for_vendors", "PowerVR,Mali,Adreno,Apple");
GLOBAL_DEF_RST("rendering/textures/default_filters/use_nearest_mipmap_filter", false);
GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "rendering/textures/default_filters/anisotropic_filtering_level", PROPERTY_HINT_ENUM, String::utf8("Disabled (Fastest),2× (Faster),4× (Fast),8× (Average),16× (Slow)")), 2);
diff --git a/tests/core/variant/test_variant.h b/tests/core/variant/test_variant.h
index 66b58620d2..93ae3b4c43 100644
--- a/tests/core/variant/test_variant.h
+++ b/tests/core/variant/test_variant.h
@@ -126,7 +126,7 @@ TEST_CASE("[Variant] Writer and parser Variant::FLOAT") {
CHECK_MESSAGE(float_parsed == 1.0e+100, "Should match the double literal.");
}
-TEST_CASE("[Variant] Assignment To Bool from Int,Float,String,Vec2,Vec2i,Vec3,Vec3i and Color") {
+TEST_CASE("[Variant] Assignment To Bool from Int,Float,String,Vec2,Vec2i,Vec3,Vec3i,Vec4,Vec4i,Rect2,Rect2i,Trans2d,Trans3d,Color,Call,Plane,Basis,AABB,Quant,Proj,RID,and Object") {
Variant int_v = 0;
Variant bool_v = true;
int_v = bool_v; // int_v is now a bool
@@ -183,6 +183,54 @@ TEST_CASE("[Variant] Assignment To Bool from Int,Float,String,Vec2,Vec2i,Vec3,Ve
vec3i_v = bool_v;
CHECK(vec3i_v.get_type() == Variant::BOOL);
+ Variant vec4_v = Vector4(0, 0, 0, 0);
+ bool_v = true;
+ vec4_v = bool_v;
+ CHECK(vec4_v == Variant(true));
+ bool_v = false;
+ vec4_v = bool_v;
+ CHECK(vec4_v.get_type() == Variant::BOOL);
+
+ Variant vec4i_v = Vector4i(0, 0, 0, 0);
+ bool_v = true;
+ vec4i_v = bool_v;
+ CHECK(vec4i_v == Variant(true));
+ bool_v = false;
+ vec4i_v = bool_v;
+ CHECK(vec4i_v.get_type() == Variant::BOOL);
+
+ Variant rect2_v = Rect2();
+ bool_v = true;
+ rect2_v = bool_v;
+ CHECK(rect2_v == Variant(true));
+ bool_v = false;
+ rect2_v = bool_v;
+ CHECK(rect2_v.get_type() == Variant::BOOL);
+
+ Variant rect2i_v = Rect2i();
+ bool_v = true;
+ rect2i_v = bool_v;
+ CHECK(rect2i_v == Variant(true));
+ bool_v = false;
+ rect2i_v = bool_v;
+ CHECK(rect2i_v.get_type() == Variant::BOOL);
+
+ Variant transform2d_v = Transform2D();
+ bool_v = true;
+ transform2d_v = bool_v;
+ CHECK(transform2d_v == Variant(true));
+ bool_v = false;
+ transform2d_v = bool_v;
+ CHECK(transform2d_v.get_type() == Variant::BOOL);
+
+ Variant transform3d_v = Transform3D();
+ bool_v = true;
+ transform3d_v = bool_v;
+ CHECK(transform3d_v == Variant(true));
+ bool_v = false;
+ transform3d_v = bool_v;
+ CHECK(transform3d_v.get_type() == Variant::BOOL);
+
Variant col_v = Color(0.5f, 0.2f, 0.75f);
bool_v = true;
col_v = bool_v;
@@ -190,9 +238,74 @@ TEST_CASE("[Variant] Assignment To Bool from Int,Float,String,Vec2,Vec2i,Vec3,Ve
bool_v = false;
col_v = bool_v;
CHECK(col_v.get_type() == Variant::BOOL);
+
+ Variant call_v = Callable();
+ bool_v = true;
+ call_v = bool_v;
+ CHECK(call_v == Variant(true));
+ bool_v = false;
+ call_v = bool_v;
+ CHECK(call_v.get_type() == Variant::BOOL);
+
+ Variant plane_v = Plane();
+ bool_v = true;
+ plane_v = bool_v;
+ CHECK(plane_v == Variant(true));
+ bool_v = false;
+ plane_v = bool_v;
+ CHECK(plane_v.get_type() == Variant::BOOL);
+
+ Variant basis_v = Basis();
+ bool_v = true;
+ basis_v = bool_v;
+ CHECK(basis_v == Variant(true));
+ bool_v = false;
+ basis_v = bool_v;
+ CHECK(basis_v.get_type() == Variant::BOOL);
+
+ Variant aabb_v = AABB();
+ bool_v = true;
+ aabb_v = bool_v;
+ CHECK(aabb_v == Variant(true));
+ bool_v = false;
+ aabb_v = bool_v;
+ CHECK(aabb_v.get_type() == Variant::BOOL);
+
+ Variant quaternion_v = Quaternion();
+ bool_v = true;
+ quaternion_v = bool_v;
+ CHECK(quaternion_v == Variant(true));
+ bool_v = false;
+ quaternion_v = bool_v;
+ CHECK(quaternion_v.get_type() == Variant::BOOL);
+
+ Variant projection_v = Projection();
+ bool_v = true;
+ projection_v = bool_v;
+ CHECK(projection_v == Variant(true));
+ bool_v = false;
+ projection_v = bool_v;
+ CHECK(projection_v.get_type() == Variant::BOOL);
+
+ Variant rid_v = RID();
+ bool_v = true;
+ rid_v = bool_v;
+ CHECK(rid_v == Variant(true));
+ bool_v = false;
+ rid_v = bool_v;
+ CHECK(rid_v.get_type() == Variant::BOOL);
+
+ Object obj_one = Object();
+ Variant object_v = &obj_one;
+ bool_v = true;
+ object_v = bool_v;
+ CHECK(object_v == Variant(true));
+ bool_v = false;
+ object_v = bool_v;
+ CHECK(object_v.get_type() == Variant::BOOL);
}
-TEST_CASE("[Variant] Assignment To Int from Bool,Float,String,Vec2,Vec2i,Vec3,Vec3i and Color") {
+TEST_CASE("[Variant] Assignment To Int from Bool,Float,String,Vec2,Vec2i,Vec3,Vec3i Vec4,Vec4i,Rect2,Rect2i,Trans2d,Trans3d,Color,Call,Plane,Basis,AABB,Quant,Proj,RID,and Object") {
Variant bool_v = false;
Variant int_v = 2;
bool_v = int_v; // Now bool_v is int
@@ -249,6 +362,54 @@ TEST_CASE("[Variant] Assignment To Int from Bool,Float,String,Vec2,Vec2i,Vec3,Ve
vec3i_v = int_v;
CHECK(vec3i_v.get_type() == Variant::INT);
+ Variant vec4_v = Vector4(0, 0, 0, 0);
+ int_v = 2;
+ vec4_v = int_v;
+ CHECK(vec4_v == Variant(2));
+ int_v = -3;
+ vec4_v = int_v;
+ CHECK(vec4_v.get_type() == Variant::INT);
+
+ Variant vec4i_v = Vector4i(0, 0, 0, 0);
+ int_v = 2;
+ vec4i_v = int_v;
+ CHECK(vec4i_v == Variant(2));
+ int_v = -3;
+ vec4i_v = int_v;
+ CHECK(vec4i_v.get_type() == Variant::INT);
+
+ Variant rect2_v = Rect2();
+ int_v = 2;
+ rect2_v = int_v;
+ CHECK(rect2_v == Variant(2));
+ int_v = -3;
+ rect2_v = int_v;
+ CHECK(rect2_v.get_type() == Variant::INT);
+
+ Variant rect2i_v = Rect2i();
+ int_v = 2;
+ rect2i_v = int_v;
+ CHECK(rect2i_v == Variant(2));
+ int_v = -3;
+ rect2i_v = int_v;
+ CHECK(rect2i_v.get_type() == Variant::INT);
+
+ Variant transform2d_v = Transform2D();
+ int_v = 2;
+ transform2d_v = int_v;
+ CHECK(transform2d_v == Variant(2));
+ int_v = -3;
+ transform2d_v = int_v;
+ CHECK(transform2d_v.get_type() == Variant::INT);
+
+ Variant transform3d_v = Transform3D();
+ int_v = 2;
+ transform3d_v = int_v;
+ CHECK(transform3d_v == Variant(2));
+ int_v = -3;
+ transform3d_v = int_v;
+ CHECK(transform3d_v.get_type() == Variant::INT);
+
Variant col_v = Color(0.5f, 0.2f, 0.75f);
int_v = 2;
col_v = int_v;
@@ -256,9 +417,74 @@ TEST_CASE("[Variant] Assignment To Int from Bool,Float,String,Vec2,Vec2i,Vec3,Ve
int_v = -3;
col_v = int_v;
CHECK(col_v.get_type() == Variant::INT);
+
+ Variant call_v = Callable();
+ int_v = 2;
+ call_v = int_v;
+ CHECK(call_v == Variant(2));
+ int_v = -3;
+ call_v = int_v;
+ CHECK(call_v.get_type() == Variant::INT);
+
+ Variant plane_v = Plane();
+ int_v = 2;
+ plane_v = int_v;
+ CHECK(plane_v == Variant(2));
+ int_v = -3;
+ plane_v = int_v;
+ CHECK(plane_v.get_type() == Variant::INT);
+
+ Variant basis_v = Basis();
+ int_v = 2;
+ basis_v = int_v;
+ CHECK(basis_v == Variant(2));
+ int_v = -3;
+ basis_v = int_v;
+ CHECK(basis_v.get_type() == Variant::INT);
+
+ Variant aabb_v = AABB();
+ int_v = 2;
+ aabb_v = int_v;
+ CHECK(aabb_v == Variant(2));
+ int_v = -3;
+ aabb_v = int_v;
+ CHECK(aabb_v.get_type() == Variant::INT);
+
+ Variant quaternion_v = Quaternion();
+ int_v = 2;
+ quaternion_v = int_v;
+ CHECK(quaternion_v == Variant(2));
+ int_v = -3;
+ quaternion_v = int_v;
+ CHECK(quaternion_v.get_type() == Variant::INT);
+
+ Variant projection_v = Projection();
+ int_v = 2;
+ projection_v = int_v;
+ CHECK(projection_v == Variant(2));
+ int_v = -3;
+ projection_v = int_v;
+ CHECK(projection_v.get_type() == Variant::INT);
+
+ Variant rid_v = RID();
+ int_v = 2;
+ rid_v = int_v;
+ CHECK(rid_v == Variant(2));
+ bool_v = -3;
+ rid_v = int_v;
+ CHECK(rid_v.get_type() == Variant::INT);
+
+ Object obj_one = Object();
+ Variant object_v = &obj_one;
+ int_v = 2;
+ object_v = int_v;
+ CHECK(object_v == Variant(2));
+ int_v = -3;
+ object_v = int_v;
+ CHECK(object_v.get_type() == Variant::INT);
}
-TEST_CASE("[Variant] Assignment To Float from Bool,Int,String,Vec2,Vec2i,Vec3,Vec3i and Color") {
+TEST_CASE("[Variant] Assignment To Float from Bool,Int,String,Vec2,Vec2i,Vec3,Vec3i,Vec4,Vec4i,Rect2,Rect2i,Trans2d,Trans3d,Color,Call,Plane,Basis,AABB,Quant,Proj,RID,and Object") {
Variant bool_v = false;
Variant float_v = 1.5f;
bool_v = float_v; // Now bool_v is float
@@ -315,6 +541,54 @@ TEST_CASE("[Variant] Assignment To Float from Bool,Int,String,Vec2,Vec2i,Vec3,Ve
vec3i_v = float_v;
CHECK(vec3i_v.get_type() == Variant::FLOAT);
+ Variant vec4_v = Vector4(0, 0, 0, 0);
+ float_v = 1.5f;
+ vec4_v = float_v;
+ CHECK(vec4_v == Variant(1.5f));
+ float_v = -4.6f;
+ vec4_v = float_v;
+ CHECK(vec4_v.get_type() == Variant::FLOAT);
+
+ Variant vec4i_v = Vector4i(0, 0, 0, 0);
+ float_v = 1.5f;
+ vec4i_v = float_v;
+ CHECK(vec4i_v == Variant(1.5f));
+ float_v = -4.6f;
+ vec4i_v = float_v;
+ CHECK(vec4i_v.get_type() == Variant::FLOAT);
+
+ Variant rect2_v = Rect2();
+ float_v = 1.5f;
+ rect2_v = float_v;
+ CHECK(rect2_v == Variant(1.5f));
+ float_v = -4.6f;
+ rect2_v = float_v;
+ CHECK(rect2_v.get_type() == Variant::FLOAT);
+
+ Variant rect2i_v = Rect2i();
+ float_v = 1.5f;
+ rect2i_v = float_v;
+ CHECK(rect2i_v == Variant(1.5f));
+ float_v = -4.6f;
+ rect2i_v = float_v;
+ CHECK(rect2i_v.get_type() == Variant::FLOAT);
+
+ Variant transform2d_v = Transform2D();
+ float_v = 1.5f;
+ transform2d_v = float_v;
+ CHECK(transform2d_v == Variant(1.5f));
+ float_v = -4.6f;
+ transform2d_v = float_v;
+ CHECK(transform2d_v.get_type() == Variant::FLOAT);
+
+ Variant transform3d_v = Transform3D();
+ float_v = 1.5f;
+ transform3d_v = float_v;
+ CHECK(transform3d_v == Variant(1.5f));
+ float_v = -4.6f;
+ transform3d_v = float_v;
+ CHECK(transform2d_v.get_type() == Variant::FLOAT);
+
Variant col_v = Color(0.5f, 0.2f, 0.75f);
float_v = 1.5f;
col_v = float_v;
@@ -322,9 +596,74 @@ TEST_CASE("[Variant] Assignment To Float from Bool,Int,String,Vec2,Vec2i,Vec3,Ve
float_v = -4.6f;
col_v = float_v;
CHECK(col_v.get_type() == Variant::FLOAT);
+
+ Variant call_v = Callable();
+ float_v = 1.5f;
+ call_v = float_v;
+ CHECK(call_v == Variant(1.5f));
+ float_v = -4.6f;
+ call_v = float_v;
+ CHECK(call_v.get_type() == Variant::FLOAT);
+
+ Variant plane_v = Plane();
+ float_v = 1.5f;
+ plane_v = float_v;
+ CHECK(plane_v == Variant(1.5f));
+ float_v = -4.6f;
+ plane_v = float_v;
+ CHECK(plane_v.get_type() == Variant::FLOAT);
+
+ Variant basis_v = Basis();
+ float_v = 1.5f;
+ basis_v = float_v;
+ CHECK(basis_v == Variant(1.5f));
+ float_v = -4.6f;
+ basis_v = float_v;
+ CHECK(basis_v.get_type() == Variant::FLOAT);
+
+ Variant aabb_v = AABB();
+ float_v = 1.5f;
+ aabb_v = float_v;
+ CHECK(aabb_v == Variant(1.5f));
+ float_v = -4.6f;
+ aabb_v = float_v;
+ CHECK(aabb_v.get_type() == Variant::FLOAT);
+
+ Variant quaternion_v = Quaternion();
+ float_v = 1.5f;
+ quaternion_v = float_v;
+ CHECK(quaternion_v == Variant(1.5f));
+ float_v = -4.6f;
+ quaternion_v = float_v;
+ CHECK(quaternion_v.get_type() == Variant::FLOAT);
+
+ Variant projection_v = Projection();
+ float_v = 1.5f;
+ projection_v = float_v;
+ CHECK(projection_v == Variant(1.5f));
+ float_v = -4.6f;
+ projection_v = float_v;
+ CHECK(projection_v.get_type() == Variant::FLOAT);
+
+ Variant rid_v = RID();
+ float_v = 1.5f;
+ rid_v = float_v;
+ CHECK(rid_v == Variant(1.5f));
+ float_v = -4.6f;
+ rid_v = float_v;
+ CHECK(rid_v.get_type() == Variant::FLOAT);
+
+ Object obj_one = Object();
+ Variant object_v = &obj_one;
+ float_v = 1.5f;
+ object_v = float_v;
+ CHECK(object_v == Variant(1.5f));
+ float_v = -4.6f;
+ object_v = float_v;
+ CHECK(object_v.get_type() == Variant::FLOAT);
}
-TEST_CASE("[Variant] Assignment To String from Bool,Int,Float,Vec2,Vec2i,Vec3,Vec3i and Color") {
+TEST_CASE("[Variant] Assignment To String from Bool,Int,Float,Vec2,Vec2i,Vec3,Vec3i,Vec4,Vec4i,Rect2,Rect2i,Trans2d,Trans3d,Color,Call,Plane,Basis,AABB,Quant,Proj,RID,and Object") {
Variant bool_v = false;
Variant string_v = "Hello";
bool_v = string_v; // Now bool_v is string
@@ -381,6 +720,54 @@ TEST_CASE("[Variant] Assignment To String from Bool,Int,Float,Vec2,Vec2i,Vec3,Ve
vec3i_v = string_v;
CHECK(vec3i_v.get_type() == Variant::STRING);
+ Variant vec4_v = Vector4(0, 0, 0, 0);
+ string_v = "Hello";
+ vec4_v = string_v;
+ CHECK(vec4_v == Variant("Hello"));
+ string_v = "Hello there";
+ vec4_v = string_v;
+ CHECK(vec4_v.get_type() == Variant::STRING);
+
+ Variant vec4i_v = Vector4i(0, 0, 0, 0);
+ string_v = "Hello";
+ vec4i_v = string_v;
+ CHECK(vec4i_v == Variant("Hello"));
+ string_v = "Hello there";
+ vec4i_v = string_v;
+ CHECK(vec4i_v.get_type() == Variant::STRING);
+
+ Variant rect2_v = Rect2();
+ string_v = "Hello";
+ rect2_v = string_v;
+ CHECK(rect2_v == Variant("Hello"));
+ string_v = "Hello there";
+ rect2_v = string_v;
+ CHECK(rect2_v.get_type() == Variant::STRING);
+
+ Variant rect2i_v = Rect2i();
+ string_v = "Hello";
+ rect2i_v = string_v;
+ CHECK(rect2i_v == Variant("Hello"));
+ string_v = "Hello there";
+ rect2i_v = string_v;
+ CHECK(rect2i_v.get_type() == Variant::STRING);
+
+ Variant transform2d_v = Transform2D();
+ string_v = "Hello";
+ transform2d_v = string_v;
+ CHECK(transform2d_v == Variant("Hello"));
+ string_v = "Hello there";
+ transform2d_v = string_v;
+ CHECK(transform2d_v.get_type() == Variant::STRING);
+
+ Variant transform3d_v = Transform3D();
+ string_v = "Hello";
+ transform3d_v = string_v;
+ CHECK(transform3d_v == Variant("Hello"));
+ string_v = "Hello there";
+ transform3d_v = string_v;
+ CHECK(transform3d_v.get_type() == Variant::STRING);
+
Variant col_v = Color(0.5f, 0.2f, 0.75f);
string_v = "Hello";
col_v = string_v;
@@ -388,9 +775,74 @@ TEST_CASE("[Variant] Assignment To String from Bool,Int,Float,Vec2,Vec2i,Vec3,Ve
string_v = "Hello there";
col_v = string_v;
CHECK(col_v.get_type() == Variant::STRING);
+
+ Variant call_v = Callable();
+ string_v = "Hello";
+ call_v = string_v;
+ CHECK(call_v == Variant("Hello"));
+ string_v = "Hello there";
+ call_v = string_v;
+ CHECK(call_v.get_type() == Variant::STRING);
+
+ Variant plane_v = Plane();
+ string_v = "Hello";
+ plane_v = string_v;
+ CHECK(plane_v == Variant("Hello"));
+ string_v = "Hello there";
+ plane_v = string_v;
+ CHECK(plane_v.get_type() == Variant::STRING);
+
+ Variant basis_v = Basis();
+ string_v = "Hello";
+ basis_v = string_v;
+ CHECK(basis_v == Variant("Hello"));
+ string_v = "Hello there";
+ basis_v = string_v;
+ CHECK(basis_v.get_type() == Variant::STRING);
+
+ Variant aabb_v = AABB();
+ string_v = "Hello";
+ aabb_v = string_v;
+ CHECK(aabb_v == Variant("Hello"));
+ string_v = "Hello there";
+ aabb_v = string_v;
+ CHECK(aabb_v.get_type() == Variant::STRING);
+
+ Variant quaternion_v = Quaternion();
+ string_v = "Hello";
+ quaternion_v = string_v;
+ CHECK(quaternion_v == Variant("Hello"));
+ string_v = "Hello there";
+ quaternion_v = string_v;
+ CHECK(quaternion_v.get_type() == Variant::STRING);
+
+ Variant projection_v = Projection();
+ string_v = "Hello";
+ projection_v = string_v;
+ CHECK(projection_v == Variant("Hello"));
+ string_v = "Hello there";
+ projection_v = string_v;
+ CHECK(projection_v.get_type() == Variant::STRING);
+
+ Variant rid_v = RID();
+ string_v = "Hello";
+ rid_v = string_v;
+ CHECK(rid_v == Variant("Hello"));
+ string_v = "Hello there";
+ rid_v = string_v;
+ CHECK(rid_v.get_type() == Variant::STRING);
+
+ Object obj_one = Object();
+ Variant object_v = &obj_one;
+ string_v = "Hello";
+ object_v = string_v;
+ CHECK(object_v == Variant("Hello"));
+ string_v = "Hello there";
+ object_v = string_v;
+ CHECK(object_v.get_type() == Variant::STRING);
}
-TEST_CASE("[Variant] Assignment To Vec2 from Bool,Int,Float,String,Vec2i,Vec3,Vec3i and Color") {
+TEST_CASE("[Variant] Assignment To Vec2 from Bool,Int,Float,String,Vec2i,Vec3,Vec3i,Vec4,Vec4i,Rect2,Rect2i,Trans2d,Trans3d,Color,Call,Plane,Basis,AABB,Quant,Proj,RID,and Object") {
Variant bool_v = false;
Variant vec2_v = Vector2(2.2f, 3.5f);
bool_v = vec2_v; // Now bool_v is Vector2
@@ -447,6 +899,54 @@ TEST_CASE("[Variant] Assignment To Vec2 from Bool,Int,Float,String,Vec2i,Vec3,Ve
vec3i_v = vec2_v;
CHECK(vec3i_v.get_type() == Variant::VECTOR2);
+ Variant vec4_v = Vector4(0, 0, 0, 0);
+ vec2_v = Vector2(2.2f, 3.5f);
+ vec4_v = vec2_v;
+ CHECK(vec4_v == Variant(Vector2(2.2f, 3.5f)));
+ vec2_v = Vector2(-5.4f, -7.9f);
+ vec4_v = vec2_v;
+ CHECK(vec4_v.get_type() == Variant::VECTOR2);
+
+ Variant vec4i_v = Vector4i(0, 0, 0, 0);
+ vec2_v = Vector2(2.2f, 3.5f);
+ vec4i_v = vec2_v;
+ CHECK(vec4i_v == Variant(Vector2(2.2f, 3.5f)));
+ vec2_v = Vector2(-5.4f, -7.9f);
+ vec4i_v = vec2_v;
+ CHECK(vec4i_v.get_type() == Variant::VECTOR2);
+
+ Variant rect2_v = Rect2();
+ vec2_v = Vector2(2.2f, 3.5f);
+ rect2_v = vec2_v;
+ CHECK(rect2_v == Variant(Vector2(2.2f, 3.5f)));
+ vec2_v = Vector2(-5.4f, -7.9f);
+ rect2_v = vec2_v;
+ CHECK(rect2_v.get_type() == Variant::VECTOR2);
+
+ Variant rect2i_v = Rect2i();
+ vec2_v = Vector2(2.2f, 3.5f);
+ rect2i_v = vec2_v;
+ CHECK(rect2i_v == Variant(Vector2(2.2f, 3.5f)));
+ vec2_v = Vector2(-5.4f, -7.9f);
+ rect2i_v = vec2_v;
+ CHECK(rect2i_v.get_type() == Variant::VECTOR2);
+
+ Variant transform2d_v = Transform2D();
+ vec2_v = Vector2(2.2f, 3.5f);
+ transform2d_v = vec2_v;
+ CHECK(transform2d_v == Variant(Vector2(2.2f, 3.5f)));
+ vec2_v = Vector2(-5.4f, -7.9f);
+ transform2d_v = vec2_v;
+ CHECK(transform2d_v.get_type() == Variant::VECTOR2);
+
+ Variant transform3d_v = Transform3D();
+ vec2_v = Vector2(2.2f, 3.5f);
+ transform3d_v = vec2_v;
+ CHECK(transform3d_v == Variant(Vector2(2.2f, 3.5f)));
+ vec2_v = Vector2(-5.4f, -7.9f);
+ transform3d_v = vec2_v;
+ CHECK(transform3d_v.get_type() == Variant::VECTOR2);
+
Variant col_v = Color(0.5f, 0.2f, 0.75f);
vec2_v = Vector2(2.2f, 3.5f);
col_v = vec2_v;
@@ -454,9 +954,74 @@ TEST_CASE("[Variant] Assignment To Vec2 from Bool,Int,Float,String,Vec2i,Vec3,Ve
vec2_v = Vector2(-5.4f, -7.9f);
col_v = vec2_v;
CHECK(col_v.get_type() == Variant::VECTOR2);
+
+ Variant call_v = Callable();
+ vec2_v = Vector2(2.2f, 3.5f);
+ call_v = vec2_v;
+ CHECK(call_v == Variant(Vector2(2.2f, 3.5f)));
+ vec2_v = Vector2(-5.4f, -7.9f);
+ call_v = vec2_v;
+ CHECK(call_v.get_type() == Variant::VECTOR2);
+
+ Variant plane_v = Plane();
+ vec2_v = Vector2(2.2f, 3.5f);
+ plane_v = vec2_v;
+ CHECK(plane_v == Variant(Vector2(2.2f, 3.5f)));
+ vec2_v = Vector2(-5.4f, -7.9f);
+ plane_v = vec2_v;
+ CHECK(plane_v.get_type() == Variant::VECTOR2);
+
+ Variant basis_v = Basis();
+ vec2_v = Vector2(2.2f, 3.5f);
+ basis_v = vec2_v;
+ CHECK(basis_v == Variant(Vector2(2.2f, 3.5f)));
+ vec2_v = Vector2(-5.4f, -7.9f);
+ basis_v = vec2_v;
+ CHECK(basis_v.get_type() == Variant::VECTOR2);
+
+ Variant aabb_v = AABB();
+ string_v = "Hello";
+ aabb_v = string_v;
+ CHECK(aabb_v == Variant("Hello"));
+ string_v = "Hello there";
+ aabb_v = string_v;
+ CHECK(aabb_v.get_type() == Variant::STRING);
+
+ Variant quaternion_v = Quaternion();
+ string_v = "Hello";
+ quaternion_v = string_v;
+ CHECK(quaternion_v == Variant("Hello"));
+ string_v = "Hello there";
+ quaternion_v = string_v;
+ CHECK(quaternion_v.get_type() == Variant::STRING);
+
+ Variant projection_v = Projection();
+ string_v = "Hello";
+ projection_v = string_v;
+ CHECK(projection_v == Variant("Hello"));
+ string_v = "Hello there";
+ projection_v = string_v;
+ CHECK(projection_v.get_type() == Variant::STRING);
+
+ Variant rid_v = RID();
+ string_v = "Hello";
+ rid_v = string_v;
+ CHECK(rid_v == Variant("Hello"));
+ string_v = "Hello there";
+ rid_v = string_v;
+ CHECK(rid_v.get_type() == Variant::STRING);
+
+ Object obj_one = Object();
+ Variant object_v = &obj_one;
+ string_v = "Hello";
+ object_v = string_v;
+ CHECK(object_v == Variant("Hello"));
+ string_v = "Hello there";
+ object_v = string_v;
+ CHECK(object_v.get_type() == Variant::STRING);
}
-TEST_CASE("[Variant] Assignment To Vec2i from Bool,Int,Float,String,Vec2,Vec3,Vec3i and Color") {
+TEST_CASE("[Variant] Assignment To Vec2i from Bool,Int,Float,String,Vec2,Vec3,Vec3i,Vec4,Vec4i,Rect2,Rect2i,Trans2d,Trans3d,Color,Call,Plane,Basis,AABB,Quant,Proj,RID,and Object") {
Variant bool_v = false;
Variant vec2i_v = Vector2i(2, 3);
bool_v = vec2i_v; // Now bool_v is Vector2i
@@ -513,6 +1078,54 @@ TEST_CASE("[Variant] Assignment To Vec2i from Bool,Int,Float,String,Vec2,Vec3,Ve
vec3i_v = vec2i_v;
CHECK(vec3i_v.get_type() == Variant::VECTOR2I);
+ Variant vec4_v = Vector4(0, 0, 0, 0);
+ vec2i_v = Vector2i(2, 3);
+ vec4_v = vec2i_v;
+ CHECK(vec4_v == Variant(Vector2i(2, 3)));
+ vec2i_v = Vector2i(-5, -7);
+ vec4_v = vec2i_v;
+ CHECK(vec4_v.get_type() == Variant::VECTOR2I);
+
+ Variant vec4i_v = Vector4i(0, 0, 0, 0);
+ vec2i_v = Vector2i(2, 3);
+ vec4i_v = vec2i_v;
+ CHECK(vec4i_v == Variant(Vector2i(2, 3)));
+ vec2i_v = Vector2i(-5, -7);
+ vec4i_v = vec2i_v;
+ CHECK(vec4i_v.get_type() == Variant::VECTOR2I);
+
+ Variant rect2_v = Rect2();
+ vec2i_v = Vector2i(2, 3);
+ rect2_v = vec2i_v;
+ CHECK(rect2_v == Variant(Vector2i(2, 3)));
+ vec2i_v = Vector2i(-5, -7);
+ rect2_v = vec2i_v;
+ CHECK(rect2_v.get_type() == Variant::VECTOR2I);
+
+ Variant rect2i_v = Rect2i();
+ vec2i_v = Vector2i(2, 3);
+ rect2i_v = vec2i_v;
+ CHECK(rect2i_v == Variant(Vector2i(2, 3)));
+ vec2i_v = Vector2i(-5, -7);
+ rect2i_v = vec2i_v;
+ CHECK(rect2i_v.get_type() == Variant::VECTOR2I);
+
+ Variant transform2d_v = Transform2D();
+ vec2i_v = Vector2i(2, 3);
+ transform2d_v = vec2i_v;
+ CHECK(transform2d_v == Variant(Vector2i(2, 3)));
+ vec2i_v = Vector2i(-5, -7);
+ transform2d_v = vec2i_v;
+ CHECK(transform2d_v.get_type() == Variant::VECTOR2I);
+
+ Variant transform3d_v = Transform3D();
+ vec2i_v = Vector2i(2, 3);
+ transform3d_v = vec2i_v;
+ CHECK(transform3d_v == Variant(Vector2i(2, 3)));
+ vec2i_v = Vector2i(-5, -7);
+ transform3d_v = vec2i_v;
+ CHECK(transform3d_v.get_type() == Variant::VECTOR2I);
+
Variant col_v = Color(0.5f, 0.2f, 0.75f);
vec2i_v = Vector2i(2, 3);
col_v = vec2i_v;
@@ -520,9 +1133,74 @@ TEST_CASE("[Variant] Assignment To Vec2i from Bool,Int,Float,String,Vec2,Vec3,Ve
vec2i_v = Vector2i(-5, -7);
col_v = vec2i_v;
CHECK(col_v.get_type() == Variant::VECTOR2I);
+
+ Variant call_v = Callable();
+ vec2i_v = Vector2i(2, 3);
+ call_v = vec2i_v;
+ CHECK(call_v == Variant(Vector2i(2, 3)));
+ vec2i_v = Vector2i(-5, -7);
+ call_v = vec2i_v;
+ CHECK(call_v.get_type() == Variant::VECTOR2I);
+
+ Variant plane_v = Plane();
+ vec2i_v = Vector2i(2, 3);
+ plane_v = vec2i_v;
+ CHECK(plane_v == Variant(Vector2i(2, 3)));
+ vec2i_v = Vector2i(-5, -7);
+ plane_v = vec2i_v;
+ CHECK(plane_v.get_type() == Variant::VECTOR2I);
+
+ Variant basis_v = Basis();
+ vec2i_v = Vector2i(2, 3);
+ basis_v = vec2i_v;
+ CHECK(basis_v == Variant(Vector2i(2, 3)));
+ vec2i_v = Vector2i(-5, -7);
+ basis_v = vec2i_v;
+ CHECK(basis_v.get_type() == Variant::VECTOR2I);
+
+ Variant aabb_v = AABB();
+ vec2i_v = Vector2i(2, 3);
+ aabb_v = vec2i_v;
+ CHECK(aabb_v == Variant(Vector2i(2, 3)));
+ vec2i_v = Vector2i(-5, -7);
+ aabb_v = vec2i_v;
+ CHECK(aabb_v.get_type() == Variant::VECTOR2I);
+
+ Variant quaternion_v = Quaternion();
+ vec2i_v = Vector2i(2, 3);
+ quaternion_v = vec2i_v;
+ CHECK(quaternion_v == Variant(Vector2i(2, 3)));
+ vec2i_v = Vector2i(-5, -7);
+ quaternion_v = vec2i_v;
+ CHECK(quaternion_v.get_type() == Variant::VECTOR2I);
+
+ Variant projection_v = Projection();
+ vec2i_v = Vector2i(2, 3);
+ projection_v = vec2i_v;
+ CHECK(projection_v == Variant(Vector2i(2, 3)));
+ vec2i_v = Vector2i(-5, -7);
+ projection_v = vec2i_v;
+ CHECK(projection_v.get_type() == Variant::VECTOR2I);
+
+ Variant rid_v = RID();
+ vec2i_v = Vector2i(2, 3);
+ rid_v = vec2i_v;
+ CHECK(rid_v == Variant(Vector2i(2, 3)));
+ vec2i_v = Vector2i(-5, -7);
+ rid_v = vec2i_v;
+ CHECK(rid_v.get_type() == Variant::VECTOR2I);
+
+ Object obj_one = Object();
+ Variant object_v = &obj_one;
+ vec2i_v = Vector2i(2, 3);
+ object_v = vec2i_v;
+ CHECK(object_v == Variant(Vector2i(2, 3)));
+ vec2i_v = Vector2i(-5, -7);
+ object_v = vec2i_v;
+ CHECK(object_v.get_type() == Variant::VECTOR2I);
}
-TEST_CASE("[Variant] Assignment To Vec3 from Bool,Int,Float,String,Vec2,Vec2i,Vec3i and Color") {
+TEST_CASE("[Variant] Assignment To Vec3 from Bool,Int,Float,String,Vec2,Vec2i,Vec3i,Vec4,Vec4i,Rect2,Rect2i,Trans2d,Trans3d,Color,Call,Plane,Basis,AABB,Quant,Proj,RID,and Object") {
Variant bool_v = false;
Variant vec3_v = Vector3(2.2f, 3.5f, 5.3f);
bool_v = vec3_v; // Now bool_v is Vector3
@@ -579,6 +1257,54 @@ TEST_CASE("[Variant] Assignment To Vec3 from Bool,Int,Float,String,Vec2,Vec2i,Ve
vec3i_v = vec3_v;
CHECK(vec3i_v.get_type() == Variant::VECTOR3);
+ Variant vec4_v = Vector4(0, 0, 0, 0);
+ vec3_v = Vector3(2.2f, 3.5f, 5.3f);
+ vec4_v = vec3_v;
+ CHECK(vec4_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
+ vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
+ vec4_v = vec3_v;
+ CHECK(vec4_v.get_type() == Variant::VECTOR3);
+
+ Variant vec4i_v = Vector4i(0, 0, 0, 0);
+ vec3_v = Vector3(2.2f, 3.5f, 5.3f);
+ vec4i_v = vec3_v;
+ CHECK(vec4i_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
+ vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
+ vec4i_v = vec3_v;
+ CHECK(vec4i_v.get_type() == Variant::VECTOR3);
+
+ Variant rect2_v = Rect2();
+ vec3_v = Vector3(2.2f, 3.5f, 5.3f);
+ rect2_v = vec3_v;
+ CHECK(rect2_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
+ vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
+ rect2_v = vec3_v;
+ CHECK(rect2_v.get_type() == Variant::VECTOR3);
+
+ Variant rect2i_v = Rect2i();
+ vec3_v = Vector3(2.2f, 3.5f, 5.3f);
+ rect2i_v = vec3_v;
+ CHECK(rect2i_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
+ vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
+ rect2i_v = vec3_v;
+ CHECK(rect2i_v.get_type() == Variant::VECTOR3);
+
+ Variant transform2d_v = Transform2D();
+ vec3_v = Vector3(2.2f, 3.5f, 5.3f);
+ transform2d_v = vec3_v;
+ CHECK(transform2d_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
+ vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
+ transform2d_v = vec3_v;
+ CHECK(transform2d_v.get_type() == Variant::VECTOR3);
+
+ Variant transform3d_v = Transform3D();
+ vec3_v = Vector3(2.2f, 3.5f, 5.3f);
+ transform3d_v = vec3_v;
+ CHECK(transform3d_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
+ vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
+ transform3d_v = vec3_v;
+ CHECK(transform3d_v.get_type() == Variant::VECTOR3);
+
Variant col_v = Color(0.5f, 0.2f, 0.75f);
vec3_v = Vector3(2.2f, 3.5f, 5.3f);
col_v = vec3_v;
@@ -586,6 +1312,71 @@ TEST_CASE("[Variant] Assignment To Vec3 from Bool,Int,Float,String,Vec2,Vec2i,Ve
vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
col_v = vec3_v;
CHECK(col_v.get_type() == Variant::VECTOR3);
+
+ Variant call_v = Callable();
+ vec3_v = Vector3(2.2f, 3.5f, 5.3f);
+ call_v = vec3_v;
+ CHECK(call_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
+ vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
+ call_v = vec3_v;
+ CHECK(call_v.get_type() == Variant::VECTOR3);
+
+ Variant plane_v = Plane();
+ vec3_v = Vector3(2.2f, 3.5f, 5.3f);
+ plane_v = vec3_v;
+ CHECK(plane_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
+ vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
+ plane_v = vec3_v;
+ CHECK(plane_v.get_type() == Variant::VECTOR3);
+
+ Variant basis_v = Basis();
+ vec3_v = Vector3(2.2f, 3.5f, 5.3f);
+ basis_v = vec3_v;
+ CHECK(basis_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
+ vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
+ basis_v = vec3_v;
+ CHECK(basis_v.get_type() == Variant::VECTOR3);
+
+ Variant aabb_v = AABB();
+ vec3_v = Vector3(2.2f, 3.5f, 5.3f);
+ aabb_v = vec3_v;
+ CHECK(aabb_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
+ vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
+ aabb_v = vec3_v;
+ CHECK(aabb_v.get_type() == Variant::VECTOR3);
+
+ Variant quaternion_v = Quaternion();
+ vec3_v = Vector3(2.2f, 3.5f, 5.3f);
+ quaternion_v = vec3_v;
+ CHECK(quaternion_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
+ vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
+ quaternion_v = vec3_v;
+ CHECK(quaternion_v.get_type() == Variant::VECTOR3);
+
+ Variant projection_v = Projection();
+ vec3_v = Vector3(2.2f, 3.5f, 5.3f);
+ quaternion_v = vec3_v;
+ CHECK(quaternion_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
+ vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
+ quaternion_v = vec3_v;
+ CHECK(quaternion_v.get_type() == Variant::VECTOR3);
+
+ Variant rid_v = RID();
+ vec3_v = Vector3(2.2f, 3.5f, 5.3f);
+ rid_v = vec3_v;
+ CHECK(rid_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
+ vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
+ rid_v = vec3_v;
+ CHECK(rid_v.get_type() == Variant::VECTOR3);
+
+ Object obj_one = Object();
+ Variant object_v = &obj_one;
+ vec3_v = Vector3(2.2f, 3.5f, 5.3f);
+ object_v = vec3_v;
+ CHECK(object_v == Variant(Vector3(2.2f, 3.5f, 5.3f)));
+ vec3_v = Vector3(-5.4f, -7.9f, -2.1f);
+ object_v = vec3_v;
+ CHECK(object_v.get_type() == Variant::VECTOR3);
}
TEST_CASE("[Variant] Assignment To Vec3i from Bool,Int,Float,String,Vec2,Vec2i,Vec3 and Color") {
@@ -645,6 +1436,54 @@ TEST_CASE("[Variant] Assignment To Vec3i from Bool,Int,Float,String,Vec2,Vec2i,V
vec3_v = vec3i_v;
CHECK(vec3_v.get_type() == Variant::VECTOR3I);
+ Variant vec4_v = Vector4(0, 0, 0, 0);
+ vec3i_v = Vector3i(2, 3, 5);
+ vec4_v = vec3i_v;
+ CHECK(vec4_v == Variant(Vector3i(2, 3, 5)));
+ vec3i_v = Vector3i(-5, -7, -2);
+ vec4_v = vec3i_v;
+ CHECK(vec4_v.get_type() == Variant::VECTOR3I);
+
+ Variant vec4i_v = Vector4i(0, 0, 0, 0);
+ vec3i_v = Vector3i(2, 3, 5);
+ vec4i_v = vec3i_v;
+ CHECK(vec4i_v == Variant(Vector3i(2, 3, 5)));
+ vec3i_v = Vector3i(-5, -7, -2);
+ vec4i_v = vec3i_v;
+ CHECK(vec4i_v.get_type() == Variant::VECTOR3I);
+
+ Variant rect2_v = Rect2();
+ vec3i_v = Vector3i(2, 3, 5);
+ rect2_v = vec3i_v;
+ CHECK(rect2_v == Variant(Vector3i(2, 3, 5)));
+ vec3i_v = Vector3i(-5, -7, -2);
+ rect2_v = vec3i_v;
+ CHECK(rect2_v.get_type() == Variant::VECTOR3I);
+
+ Variant rect2i_v = Rect2i();
+ vec3i_v = Vector3i(2, 3, 5);
+ rect2i_v = vec3i_v;
+ CHECK(rect2i_v == Variant(Vector3i(2, 3, 5)));
+ vec3i_v = Vector3i(-5, -7, -2);
+ rect2i_v = vec3i_v;
+ CHECK(rect2i_v.get_type() == Variant::VECTOR3I);
+
+ Variant transform2d_v = Transform2D();
+ vec3i_v = Vector3i(2, 3, 5);
+ transform2d_v = vec3i_v;
+ CHECK(transform2d_v == Variant(Vector3i(2, 3, 5)));
+ vec3i_v = Vector3i(-5, -7, -2);
+ transform2d_v = vec3i_v;
+ CHECK(transform2d_v.get_type() == Variant::VECTOR3I);
+
+ Variant transform3d_v = Transform3D();
+ vec3i_v = Vector3i(2, 3, 5);
+ transform3d_v = vec3i_v;
+ CHECK(transform3d_v == Variant(Vector3i(2, 3, 5)));
+ vec3i_v = Vector3i(-5, -7, -2);
+ transform3d_v = vec3i_v;
+ CHECK(transform3d_v.get_type() == Variant::VECTOR3I);
+
Variant col_v = Color(0.5f, 0.2f, 0.75f);
vec3i_v = Vector3i(2, 3, 5);
col_v = vec3i_v;
@@ -652,9 +1491,74 @@ TEST_CASE("[Variant] Assignment To Vec3i from Bool,Int,Float,String,Vec2,Vec2i,V
vec3i_v = Vector3i(-5, -7, -2);
col_v = vec3i_v;
CHECK(col_v.get_type() == Variant::VECTOR3I);
+
+ Variant call_v = Callable();
+ vec3i_v = Vector3i(2, 3, 5);
+ call_v = vec3i_v;
+ CHECK(call_v == Variant(Vector3i(2, 3, 5)));
+ vec3i_v = Vector3i(-5, -7, -2);
+ call_v = vec3i_v;
+ CHECK(call_v.get_type() == Variant::VECTOR3I);
+
+ Variant plane_v = Plane();
+ vec3i_v = Vector3i(2, 3, 5);
+ plane_v = vec3i_v;
+ CHECK(plane_v == Variant(Vector3i(2, 3, 5)));
+ vec3i_v = Vector3i(-5, -7, -2);
+ plane_v = vec3i_v;
+ CHECK(plane_v.get_type() == Variant::VECTOR3I);
+
+ Variant basis_v = Basis();
+ vec3i_v = Vector3i(2, 3, 5);
+ basis_v = vec3i_v;
+ CHECK(basis_v == Variant(Vector3i(2, 3, 5)));
+ vec3i_v = Vector3i(-5, -7, -2);
+ basis_v = vec3i_v;
+ CHECK(basis_v.get_type() == Variant::VECTOR3I);
+
+ Variant aabb_v = AABB();
+ vec3i_v = Vector3i(2, 3, 5);
+ aabb_v = vec3i_v;
+ CHECK(aabb_v == Variant(Vector3i(2, 3, 5)));
+ vec3i_v = Vector3i(-5, -7, -2);
+ aabb_v = vec3i_v;
+ CHECK(aabb_v.get_type() == Variant::VECTOR3I);
+
+ Variant quaternion_v = Quaternion();
+ vec3i_v = Vector3i(2, 3, 5);
+ quaternion_v = vec3i_v;
+ CHECK(quaternion_v == Variant(Vector3i(2, 3, 5)));
+ vec3i_v = Vector3i(-5, -7, -2);
+ quaternion_v = vec3i_v;
+ CHECK(quaternion_v.get_type() == Variant::VECTOR3I);
+
+ Variant projection_v = Projection();
+ vec3i_v = Vector3i(2, 3, 5);
+ projection_v = vec3i_v;
+ CHECK(projection_v == Variant(Vector3i(2, 3, 5)));
+ vec3i_v = Vector3i(-5, -7, -2);
+ projection_v = vec3i_v;
+ CHECK(projection_v.get_type() == Variant::VECTOR3I);
+
+ Variant rid_v = RID();
+ vec3i_v = Vector3i(2, 3, 5);
+ rid_v = vec3i_v;
+ CHECK(rid_v == Variant(Vector3i(2, 3, 5)));
+ vec3i_v = Vector3i(-5, -7, -2);
+ rid_v = vec3i_v;
+ CHECK(rid_v.get_type() == Variant::VECTOR3I);
+
+ Object obj_one = Object();
+ Variant object_v = &obj_one;
+ vec3i_v = Vector3i(2, 3, 5);
+ object_v = vec3i_v;
+ CHECK(object_v == Variant(Vector3i(2, 3, 5)));
+ vec3i_v = Vector3i(-5, -7, -2);
+ object_v = vec3i_v;
+ CHECK(object_v.get_type() == Variant::VECTOR3I);
}
-TEST_CASE("[Variant] Assignment To Color from Bool,Int,Float,String,Vec2,Vec2i,Vec3 and Vec3i") {
+TEST_CASE("[Variant] Assignment To Color from Bool,Int,Float,String,Vec2,Vec2i,Vec3,Vec3i,Vec4,Vec4i,Rect2,Rect2i,Trans2d,Trans3d,Color,Call,Plane,Basis,AABB,Quant,Proj,RID,and Object") {
Variant bool_v = false;
Variant col_v = Color(0.25f, 0.4f, 0.78f);
bool_v = col_v; // Now bool_v is Color
@@ -718,6 +1622,119 @@ TEST_CASE("[Variant] Assignment To Color from Bool,Int,Float,String,Vec2,Vec2i,V
col_v = Color(0.33f, 0.75f, 0.21f);
vec3i_v = col_v;
CHECK(vec3i_v.get_type() == Variant::COLOR);
+
+ Variant vec4_v = Vector4(0, 0, 0, 0);
+ col_v = Color(0.25f, 0.4f, 0.78f);
+ vec4_v = col_v;
+ CHECK(vec4_v == Variant(Color(0.25f, 0.4f, 0.78f)));
+ col_v = Color(0.33f, 0.75f, 0.21f);
+ vec4_v = col_v;
+ CHECK(vec4_v.get_type() == Variant::COLOR);
+
+ Variant vec4i_v = Vector4i(0, 0, 0, 0);
+ col_v = Color(0.25f, 0.4f, 0.78f);
+ vec4i_v = col_v;
+ CHECK(vec4i_v == Variant(Color(0.25f, 0.4f, 0.78f)));
+ col_v = Color(0.33f, 0.75f, 0.21f);
+ vec4i_v = col_v;
+ CHECK(vec4i_v.get_type() == Variant::COLOR);
+
+ Variant rect2_v = Rect2();
+ col_v = Color(0.25f, 0.4f, 0.78f);
+ rect2_v = col_v;
+ CHECK(rect2_v == Variant(Color(0.25f, 0.4f, 0.78f)));
+ col_v = Color(0.33f, 0.75f, 0.21f);
+ rect2_v = col_v;
+ CHECK(rect2_v.get_type() == Variant::COLOR);
+
+ Variant rect2i_v = Rect2i();
+ col_v = Color(0.25f, 0.4f, 0.78f);
+ rect2i_v = col_v;
+ CHECK(rect2i_v == Variant(Color(0.25f, 0.4f, 0.78f)));
+ col_v = Color(0.33f, 0.75f, 0.21f);
+ rect2i_v = col_v;
+ CHECK(rect2i_v.get_type() == Variant::COLOR);
+
+ Variant transform2d_v = Transform2D();
+ col_v = Color(0.25f, 0.4f, 0.78f);
+ transform2d_v = col_v;
+ CHECK(transform2d_v == Variant(Color(0.25f, 0.4f, 0.78f)));
+ col_v = Color(0.33f, 0.75f, 0.21f);
+ transform2d_v = col_v;
+ CHECK(transform2d_v.get_type() == Variant::COLOR);
+
+ Variant transform3d_v = Transform3D();
+ col_v = Color(0.25f, 0.4f, 0.78f);
+ transform3d_v = col_v;
+ CHECK(transform3d_v == Variant(Color(0.25f, 0.4f, 0.78f)));
+ col_v = Color(0.33f, 0.75f, 0.21f);
+ transform3d_v = col_v;
+ CHECK(transform3d_v.get_type() == Variant::COLOR);
+
+ Variant call_v = Callable();
+ col_v = Color(0.25f, 0.4f, 0.78f);
+ call_v = col_v;
+ CHECK(call_v == Variant(Color(0.25f, 0.4f, 0.78f)));
+ col_v = Color(0.33f, 0.75f, 0.21f);
+ call_v = col_v;
+ CHECK(call_v.get_type() == Variant::COLOR);
+
+ Variant plane_v = Plane();
+ col_v = Color(0.25f, 0.4f, 0.78f);
+ plane_v = col_v;
+ CHECK(plane_v == Variant(Color(0.25f, 0.4f, 0.78f)));
+ col_v = Color(0.33f, 0.75f, 0.21f);
+ plane_v = col_v;
+ CHECK(plane_v.get_type() == Variant::COLOR);
+
+ Variant basis_v = Basis();
+ col_v = Color(0.25f, 0.4f, 0.78f);
+ basis_v = col_v;
+ CHECK(basis_v == Variant(Color(0.25f, 0.4f, 0.78f)));
+ col_v = Color(0.33f, 0.75f, 0.21f);
+ basis_v = col_v;
+ CHECK(basis_v.get_type() == Variant::COLOR);
+
+ Variant aabb_v = AABB();
+ col_v = Color(0.25f, 0.4f, 0.78f);
+ aabb_v = col_v;
+ CHECK(aabb_v == Variant(Color(0.25f, 0.4f, 0.78f)));
+ col_v = Color(0.33f, 0.75f, 0.21f);
+ aabb_v = col_v;
+ CHECK(aabb_v.get_type() == Variant::COLOR);
+
+ Variant quaternion_v = Quaternion();
+ col_v = Color(0.25f, 0.4f, 0.78f);
+ quaternion_v = col_v;
+ CHECK(quaternion_v == Variant(Color(0.25f, 0.4f, 0.78f)));
+ col_v = Color(0.33f, 0.75f, 0.21f);
+ quaternion_v = col_v;
+ CHECK(quaternion_v.get_type() == Variant::COLOR);
+
+ Variant projection_v = Projection();
+ col_v = Color(0.25f, 0.4f, 0.78f);
+ projection_v = col_v;
+ CHECK(projection_v == Variant(Color(0.25f, 0.4f, 0.78f)));
+ col_v = Color(0.33f, 0.75f, 0.21f);
+ projection_v = col_v;
+ CHECK(projection_v.get_type() == Variant::COLOR);
+
+ Variant rid_v = RID();
+ col_v = Color(0.25f, 0.4f, 0.78f);
+ rid_v = col_v;
+ CHECK(rid_v == Variant(Color(0.25f, 0.4f, 0.78f)));
+ col_v = Color(0.33f, 0.75f, 0.21f);
+ rid_v = col_v;
+ CHECK(rid_v.get_type() == Variant::COLOR);
+
+ Object obj_one = Object();
+ Variant object_v = &obj_one;
+ col_v = Color(0.25f, 0.4f, 0.78f);
+ object_v = col_v;
+ CHECK(object_v == Variant(Color(0.25f, 0.4f, 0.78f)));
+ col_v = Color(0.33f, 0.75f, 0.21f);
+ object_v = col_v;
+ CHECK(object_v.get_type() == Variant::COLOR);
}
TEST_CASE("[Variant] Writer and parser array") {
diff --git a/thirdparty/README.md b/thirdparty/README.md
index 8a0f36a384..4a369a045c 100644
--- a/thirdparty/README.md
+++ b/thirdparty/README.md
@@ -804,7 +804,7 @@ instead of `miniz.h` as an external dependency.
## thorvg
- Upstream: https://github.com/thorvg/thorvg
-- Version: 0.11.0 (12260198d12719ea20939b68492accfc155d9ff5, 2023)
+- Version: 0.11.1 (ca00e52125446a1a5cca20f9d8621b382cff5cb9, 2023)
- License: MIT
Files extracted from upstream source:
@@ -812,6 +812,7 @@ Files extracted from upstream source:
See `thorvg/update-thorvg.sh` for extraction instructions. Set the version
number and run the script.
+Apply patches from the `patches` folder.
## vhacd
diff --git a/thirdparty/thorvg/inc/config.h b/thirdparty/thorvg/inc/config.h
index 735b4df9da..3152dfabd8 100644
--- a/thirdparty/thorvg/inc/config.h
+++ b/thirdparty/thorvg/inc/config.h
@@ -3,9 +3,11 @@
#define THORVG_SW_RASTER_SUPPORT
#define THORVG_SVG_LOADER_SUPPORT
+#define THORVG_PNG_LOADER_SUPPORT
+#define THORVG_JPG_LOADER_SUPPORT
// For internal debugging:
//#define THORVG_LOG_ENABLED
-#define THORVG_VERSION_STRING "0.11.0"
+#define THORVG_VERSION_STRING "0.11.1"
#endif
diff --git a/thirdparty/thorvg/patches/loader_jpg-fix_regression_bug.patch b/thirdparty/thorvg/patches/loader_jpg-fix_regression_bug.patch
new file mode 100644
index 0000000000..a71883cb35
--- /dev/null
+++ b/thirdparty/thorvg/patches/loader_jpg-fix_regression_bug.patch
@@ -0,0 +1,13 @@
+diff --git a/thirdparty/thorvg/src/loaders/jpg/tvgJpgd.cpp b/thirdparty/thorvg/src/loaders/jpg/tvgJpgd.cpp
+index 88d359aaa3..61a5dc1c0f 100644
+--- a/thirdparty/thorvg/src/loaders/jpg/tvgJpgd.cpp
++++ b/thirdparty/thorvg/src/loaders/jpg/tvgJpgd.cpp
+@@ -431,7 +431,7 @@ struct Row<1>
+ {
+ static void idct(int* pTemp, const jpgd_block_t* pSrc)
+ {
+- const int dcval = pSrc[0] * (pSrc[0] * (PASS1_BITS * 2));
++ const int dcval = pSrc[0] * PASS1_BITS * 2;
+
+ pTemp[0] = dcval;
+ pTemp[1] = dcval;
diff --git a/thirdparty/thorvg/src/loaders/external_png/tvgPngLoader.cpp b/thirdparty/thorvg/src/loaders/external_png/tvgPngLoader.cpp
new file mode 100644
index 0000000000..b0a9fdd579
--- /dev/null
+++ b/thirdparty/thorvg/src/loaders/external_png/tvgPngLoader.cpp
@@ -0,0 +1,120 @@
+/*
+ * Copyright (c) 2020 - 2023 the ThorVG project. All rights reserved.
+
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "tvgLoader.h"
+#include "tvgPngLoader.h"
+
+/************************************************************************/
+/* Internal Class Implementation */
+/************************************************************************/
+
+
+/************************************************************************/
+/* External Class Implementation */
+/************************************************************************/
+
+PngLoader::PngLoader()
+{
+ image = static_cast<png_imagep>(calloc(1, sizeof(png_image)));
+ image->version = PNG_IMAGE_VERSION;
+ image->opaque = NULL;
+}
+
+PngLoader::~PngLoader()
+{
+ if (content) {
+ free((void*)content);
+ content = nullptr;
+ }
+ free(image);
+}
+
+bool PngLoader::open(const string& path)
+{
+ image->opaque = NULL;
+
+ if (!png_image_begin_read_from_file(image, path.c_str())) return false;
+
+ w = (float)image->width;
+ h = (float)image->height;
+ cs = ColorSpace::ARGB8888;
+
+ return true;
+}
+
+bool PngLoader::open(const char* data, uint32_t size, bool copy)
+{
+ image->opaque = NULL;
+
+ if (!png_image_begin_read_from_memory(image, data, size)) return false;
+
+ w = (float)image->width;
+ h = (float)image->height;
+ cs = ColorSpace::ARGB8888;
+
+ return true;
+}
+
+
+bool PngLoader::read()
+{
+ png_bytep buffer;
+ image->format = PNG_FORMAT_BGRA;
+ buffer = static_cast<png_bytep>(malloc(PNG_IMAGE_SIZE((*image))));
+ if (!buffer) {
+ //out of memory, only time when libpng doesnt free its data
+ png_image_free(image);
+ return false;
+ }
+ if (!png_image_finish_read(image, NULL, buffer, 0, NULL)) {
+ free(buffer);
+ return false;
+ }
+ content = reinterpret_cast<uint32_t*>(buffer);
+
+ return true;
+}
+
+bool PngLoader::close()
+{
+ png_image_free(image);
+ return true;
+}
+
+unique_ptr<Surface> PngLoader::bitmap()
+{
+ if (!content) return nullptr;
+
+ //TODO: It's better to keep this surface instance in the loader side
+ auto surface = new Surface;
+ surface->buf32 = content;
+ surface->stride = w;
+ surface->w = w;
+ surface->h = h;
+ surface->cs = cs;
+ surface->channelSize = sizeof(uint32_t);
+ surface->owner = true;
+ surface->premultiplied = false;
+
+ return unique_ptr<Surface>(surface);
+}
+
diff --git a/thirdparty/thorvg/src/loaders/external_png/tvgPngLoader.h b/thirdparty/thorvg/src/loaders/external_png/tvgPngLoader.h
new file mode 100644
index 0000000000..5354e1bdd6
--- /dev/null
+++ b/thirdparty/thorvg/src/loaders/external_png/tvgPngLoader.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2020 - 2023 the ThorVG project. All rights reserved.
+
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef _TVG_PNG_LOADER_H_
+#define _TVG_PNG_LOADER_H_
+
+#include <png.h>
+
+class PngLoader : public LoadModule
+{
+public:
+ PngLoader();
+ ~PngLoader();
+
+ using LoadModule::open;
+ bool open(const string& path) override;
+ bool open(const char* data, uint32_t size, bool copy) override;
+ bool read() override;
+ bool close() override;
+
+ unique_ptr<Surface> bitmap() override;
+
+private:
+ png_imagep image = nullptr;
+ uint32_t* content = nullptr;
+};
+
+#endif //_TVG_PNG_LOADER_H_
diff --git a/thirdparty/thorvg/src/loaders/jpg/tvgJpgLoader.cpp b/thirdparty/thorvg/src/loaders/jpg/tvgJpgLoader.cpp
new file mode 100644
index 0000000000..6edda86cc1
--- /dev/null
+++ b/thirdparty/thorvg/src/loaders/jpg/tvgJpgLoader.cpp
@@ -0,0 +1,143 @@
+/*
+ * Copyright (c) 2021 - 2023 the ThorVG project. All rights reserved.
+
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <memory.h>
+#include "tvgLoader.h"
+#include "tvgJpgLoader.h"
+
+/************************************************************************/
+/* Internal Class Implementation */
+/************************************************************************/
+
+void JpgLoader::clear()
+{
+ jpgdDelete(decoder);
+ if (freeData) free(data);
+ decoder = nullptr;
+ data = nullptr;
+ freeData = false;
+}
+
+
+/************************************************************************/
+/* External Class Implementation */
+/************************************************************************/
+
+
+JpgLoader::~JpgLoader()
+{
+ jpgdDelete(decoder);
+ if (freeData) free(data);
+ free(image);
+}
+
+
+bool JpgLoader::open(const string& path)
+{
+ clear();
+
+ int width, height;
+ decoder = jpgdHeader(path.c_str(), &width, &height);
+ if (!decoder) return false;
+
+ w = static_cast<float>(width);
+ h = static_cast<float>(height);
+ cs = ColorSpace::ARGB8888;
+
+ return true;
+}
+
+
+bool JpgLoader::open(const char* data, uint32_t size, bool copy)
+{
+ clear();
+
+ if (copy) {
+ this->data = (char *) malloc(size);
+ if (!this->data) return false;
+ memcpy((char *)this->data, data, size);
+ freeData = true;
+ } else {
+ this->data = (char *) data;
+ freeData = false;
+ }
+
+ int width, height;
+ decoder = jpgdHeader(this->data, size, &width, &height);
+ if (!decoder) return false;
+
+ w = static_cast<float>(width);
+ h = static_cast<float>(height);
+ cs = ColorSpace::ARGB8888;
+
+ return true;
+}
+
+
+
+bool JpgLoader::read()
+{
+ if (!decoder || w <= 0 || h <= 0) return false;
+
+ TaskScheduler::request(this);
+
+ return true;
+}
+
+
+bool JpgLoader::close()
+{
+ this->done();
+ clear();
+ return true;
+}
+
+
+unique_ptr<Surface> JpgLoader::bitmap()
+{
+ this->done();
+
+ if (!image) return nullptr;
+
+ //TODO: It's better to keep this surface instance in the loader side
+ auto surface = new Surface;
+ surface->buf8 = image;
+ surface->stride = static_cast<uint32_t>(w);
+ surface->w = static_cast<uint32_t>(w);
+ surface->h = static_cast<uint32_t>(h);
+ surface->cs = cs;
+ surface->channelSize = sizeof(uint32_t);
+ surface->premultiplied = true;
+ surface->owner = true;
+
+ return unique_ptr<Surface>(surface);
+}
+
+
+void JpgLoader::run(unsigned tid)
+{
+ if (image) {
+ free(image);
+ image = nullptr;
+ }
+ image = jpgdDecompress(decoder);
+}
diff --git a/thirdparty/thorvg/src/loaders/jpg/tvgJpgLoader.h b/thirdparty/thorvg/src/loaders/jpg/tvgJpgLoader.h
new file mode 100644
index 0000000000..6d2febe94f
--- /dev/null
+++ b/thirdparty/thorvg/src/loaders/jpg/tvgJpgLoader.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2021 - 2023 the ThorVG project. All rights reserved.
+
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef _TVG_JPG_LOADER_H_
+#define _TVG_JPG_LOADER_H_
+
+#include "tvgTaskScheduler.h"
+#include "tvgJpgd.h"
+
+class JpgLoader : public LoadModule, public Task
+{
+private:
+ jpeg_decoder* decoder = nullptr;
+ char* data = nullptr;
+ unsigned char *image = nullptr;
+ bool freeData = false;
+
+ void clear();
+
+public:
+ ~JpgLoader();
+
+ using LoadModule::open;
+ bool open(const string& path) override;
+ bool open(const char* data, uint32_t size, bool copy) override;
+ bool read() override;
+ bool close() override;
+
+ unique_ptr<Surface> bitmap() override;
+ void run(unsigned tid) override;
+};
+
+#endif //_TVG_JPG_LOADER_H_
diff --git a/thirdparty/thorvg/src/loaders/jpg/tvgJpgd.cpp b/thirdparty/thorvg/src/loaders/jpg/tvgJpgd.cpp
new file mode 100644
index 0000000000..61a5dc1c0f
--- /dev/null
+++ b/thirdparty/thorvg/src/loaders/jpg/tvgJpgd.cpp
@@ -0,0 +1,3026 @@
+/*
+ * Copyright (c) 2021 - 2023 the ThorVG project. All rights reserved.
+
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+// jpgd.cpp - C++ class for JPEG decompression.
+// Public domain, Rich Geldreich <richgel99@gmail.com>
+// Alex Evans: Linear memory allocator (taken from jpge.h).
+// v1.04, May. 19, 2012: Code tweaks to fix VS2008 static code analysis warnings (all looked harmless)
+//
+// Supports progressive and baseline sequential JPEG image files, and the most common chroma subsampling factors: Y, H1V1, H2V1, H1V2, and H2V2.
+//
+// Chroma upsampling quality: H2V2 is upsampled in the frequency domain, H2V1 and H1V2 are upsampled using point sampling.
+// Chroma upsampling reference: "Fast Scheme for Image Size Change in the Compressed Domain"
+// http://vision.ai.uiuc.edu/~dugad/research/dct/index.html
+
+#include <memory.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <setjmp.h>
+#include <stdint.h>
+#include "tvgJpgd.h"
+
+#ifdef _MSC_VER
+ #pragma warning (disable : 4611) // warning C4611: interaction between '_setjmp' and C++ object destruction is non-portable
+ #define JPGD_NORETURN __declspec(noreturn)
+#elif defined(__GNUC__)
+ #define JPGD_NORETURN __attribute__ ((noreturn))
+#else
+ #define JPGD_NORETURN
+#endif
+
+/************************************************************************/
+/* Internal Class Implementation */
+/************************************************************************/
+
+
+// Set to 1 to enable freq. domain chroma upsampling on images using H2V2 subsampling (0=faster nearest neighbor sampling).
+// This is slower, but results in higher quality on images with highly saturated colors.
+#define JPGD_SUPPORT_FREQ_DOMAIN_UPSAMPLING 1
+
+#define JPGD_ASSERT(x)
+#define JPGD_MAX(a,b) (((a)>(b)) ? (a) : (b))
+#define JPGD_MIN(a,b) (((a)<(b)) ? (a) : (b))
+
+typedef int16_t jpgd_quant_t;
+typedef int16_t jpgd_block_t;
+
+// Success/failure error codes.
+enum jpgd_status
+{
+ JPGD_SUCCESS = 0, JPGD_FAILED = -1, JPGD_DONE = 1,
+ JPGD_BAD_DHT_COUNTS = -256, JPGD_BAD_DHT_INDEX, JPGD_BAD_DHT_MARKER, JPGD_BAD_DQT_MARKER, JPGD_BAD_DQT_TABLE,
+ JPGD_BAD_PRECISION, JPGD_BAD_HEIGHT, JPGD_BAD_WIDTH, JPGD_TOO_MANY_COMPONENTS,
+ JPGD_BAD_SOF_LENGTH, JPGD_BAD_VARIABLE_MARKER, JPGD_BAD_DRI_LENGTH, JPGD_BAD_SOS_LENGTH,
+ JPGD_BAD_SOS_COMP_ID, JPGD_W_EXTRA_BYTES_BEFORE_MARKER, JPGD_NO_ARITHMITIC_SUPPORT, JPGD_UNEXPECTED_MARKER,
+ JPGD_NOT_JPEG, JPGD_UNSUPPORTED_MARKER, JPGD_BAD_DQT_LENGTH, JPGD_TOO_MANY_BLOCKS,
+ JPGD_UNDEFINED_QUANT_TABLE, JPGD_UNDEFINED_HUFF_TABLE, JPGD_NOT_SINGLE_SCAN, JPGD_UNSUPPORTED_COLORSPACE,
+ JPGD_UNSUPPORTED_SAMP_FACTORS, JPGD_DECODE_ERROR, JPGD_BAD_RESTART_MARKER, JPGD_ASSERTION_ERROR,
+ JPGD_BAD_SOS_SPECTRAL, JPGD_BAD_SOS_SUCCESSIVE, JPGD_STREAM_READ, JPGD_NOTENOUGHMEM
+};
+
+enum
+{
+ JPGD_IN_BUF_SIZE = 8192, JPGD_MAX_BLOCKS_PER_MCU = 10, JPGD_MAX_HUFF_TABLES = 8, JPGD_MAX_QUANT_TABLES = 4,
+ JPGD_MAX_COMPONENTS = 4, JPGD_MAX_COMPS_IN_SCAN = 4, JPGD_MAX_BLOCKS_PER_ROW = 8192, JPGD_MAX_HEIGHT = 16384, JPGD_MAX_WIDTH = 16384
+};
+
+// Input stream interface.
+// Derive from this class to read input data from sources other than files or memory. Set m_eof_flag to true when no more data is available.
+// The decoder is rather greedy: it will keep on calling this method until its internal input buffer is full, or until the EOF flag is set.
+// It the input stream contains data after the JPEG stream's EOI (end of image) marker it will probably be pulled into the internal buffer.
+// Call the get_total_bytes_read() method to determine the actual size of the JPEG stream after successful decoding.
+struct jpeg_decoder_stream
+{
+ jpeg_decoder_stream() { }
+ virtual ~jpeg_decoder_stream() { }
+
+ // The read() method is called when the internal input buffer is empty.
+ // Parameters:
+ // pBuf - input buffer
+ // max_bytes_to_read - maximum bytes that can be written to pBuf
+ // pEOF_flag - set this to true if at end of stream (no more bytes remaining)
+ // Returns -1 on error, otherwise return the number of bytes actually written to the buffer (which may be 0).
+ // Notes: This method will be called in a loop until you set *pEOF_flag to true or the internal buffer is full.
+ virtual int read(uint8_t *pBuf, int max_bytes_to_read, bool *pEOF_flag) = 0;
+};
+
+
+// stdio FILE stream class.
+class jpeg_decoder_file_stream : public jpeg_decoder_stream
+{
+ jpeg_decoder_file_stream(const jpeg_decoder_file_stream &);
+ jpeg_decoder_file_stream &operator =(const jpeg_decoder_file_stream &);
+
+ FILE *m_pFile = nullptr;
+ bool m_eof_flag = false;
+ bool m_error_flag = false;
+
+public:
+ jpeg_decoder_file_stream() {}
+ virtual ~jpeg_decoder_file_stream();
+ bool open(const char *Pfilename);
+ void close();
+ virtual int read(uint8_t *pBuf, int max_bytes_to_read, bool *pEOF_flag);
+ };
+
+
+// Memory stream class.
+class jpeg_decoder_mem_stream : public jpeg_decoder_stream
+{
+ const uint8_t *m_pSrc_data;
+ uint32_t m_ofs, m_size;
+
+public:
+ jpeg_decoder_mem_stream() : m_pSrc_data(nullptr), m_ofs(0), m_size(0) {}
+ jpeg_decoder_mem_stream(const uint8_t *pSrc_data, uint32_t size) : m_pSrc_data(pSrc_data), m_ofs(0), m_size(size) {}
+ virtual ~jpeg_decoder_mem_stream() {}
+ bool open(const uint8_t *pSrc_data, uint32_t size);
+ void close() { m_pSrc_data = nullptr; m_ofs = 0; m_size = 0; }
+ virtual int read(uint8_t *pBuf, int max_bytes_to_read, bool *pEOF_flag);
+};
+
+
+class jpeg_decoder
+{
+public:
+ // Call get_error_code() after constructing to determine if the stream is valid or not. You may call the get_width(), get_height(), etc.
+ // methods after the constructor is called. You may then either destruct the object, or begin decoding the image by calling begin_decoding(), then decode() on each scanline.
+ jpeg_decoder(jpeg_decoder_stream *pStream);
+ ~jpeg_decoder();
+
+ // Call this method after constructing the object to begin decompression.
+ // If JPGD_SUCCESS is returned you may then call decode() on each scanline.
+ int begin_decoding();
+ // Returns the next scan line.
+ // For grayscale images, pScan_line will point to a buffer containing 8-bit pixels (get_bytes_per_pixel() will return 1).
+ // Otherwise, it will always point to a buffer containing 32-bit RGBA pixels (A will always be 255, and get_bytes_per_pixel() will return 4).
+ // Returns JPGD_SUCCESS if a scan line has been returned.
+ // Returns JPGD_DONE if all scan lines have been returned.
+ // Returns JPGD_FAILED if an error occurred. Call get_error_code() for a more info.
+ int decode(const void** pScan_line, uint32_t* pScan_line_len);
+ inline jpgd_status get_error_code() const { return m_error_code; }
+ inline int get_width() const { return m_image_x_size; }
+ inline int get_height() const { return m_image_y_size; }
+ inline int get_num_components() const { return m_comps_in_frame; }
+ inline int get_bytes_per_pixel() const { return m_dest_bytes_per_pixel; }
+ inline int get_bytes_per_scan_line() const { return m_image_x_size * get_bytes_per_pixel(); }
+ // Returns the total number of bytes actually consumed by the decoder (which should equal the actual size of the JPEG file).
+ inline int get_total_bytes_read() const { return m_total_bytes_read; }
+
+private:
+ jpeg_decoder(const jpeg_decoder &);
+ jpeg_decoder &operator =(const jpeg_decoder &);
+
+ typedef void (*pDecode_block_func)(jpeg_decoder *, int, int, int);
+
+ struct huff_tables
+ {
+ bool ac_table;
+ uint32_t look_up[256];
+ uint32_t look_up2[256];
+ uint8_t code_size[256];
+ uint32_t tree[512];
+ };
+
+ struct coeff_buf
+ {
+ uint8_t *pData;
+ int block_num_x, block_num_y;
+ int block_len_x, block_len_y;
+ int block_size;
+ };
+
+ struct mem_block
+ {
+ mem_block *m_pNext;
+ size_t m_used_count;
+ size_t m_size;
+ char m_data[1];
+ };
+
+ jmp_buf m_jmp_state;
+ mem_block *m_pMem_blocks;
+ int m_image_x_size;
+ int m_image_y_size;
+ jpeg_decoder_stream *m_pStream;
+ int m_progressive_flag;
+ uint8_t m_huff_ac[JPGD_MAX_HUFF_TABLES];
+ uint8_t* m_huff_num[JPGD_MAX_HUFF_TABLES]; // pointer to number of Huffman codes per bit size
+ uint8_t* m_huff_val[JPGD_MAX_HUFF_TABLES]; // pointer to Huffman codes per bit size
+ jpgd_quant_t* m_quant[JPGD_MAX_QUANT_TABLES]; // pointer to quantization tables
+ int m_scan_type; // Gray, Yh1v1, Yh1v2, Yh2v1, Yh2v2 (CMYK111, CMYK4114 no longer supported)
+ int m_comps_in_frame; // # of components in frame
+ int m_comp_h_samp[JPGD_MAX_COMPONENTS]; // component's horizontal sampling factor
+ int m_comp_v_samp[JPGD_MAX_COMPONENTS]; // component's vertical sampling factor
+ int m_comp_quant[JPGD_MAX_COMPONENTS]; // component's quantization table selector
+ int m_comp_ident[JPGD_MAX_COMPONENTS]; // component's ID
+ int m_comp_h_blocks[JPGD_MAX_COMPONENTS];
+ int m_comp_v_blocks[JPGD_MAX_COMPONENTS];
+ int m_comps_in_scan; // # of components in scan
+ int m_comp_list[JPGD_MAX_COMPS_IN_SCAN]; // components in this scan
+ int m_comp_dc_tab[JPGD_MAX_COMPONENTS]; // component's DC Huffman coding table selector
+ int m_comp_ac_tab[JPGD_MAX_COMPONENTS]; // component's AC Huffman coding table selector
+ int m_spectral_start; // spectral selection start
+ int m_spectral_end; // spectral selection end
+ int m_successive_low; // successive approximation low
+ int m_successive_high; // successive approximation high
+ int m_max_mcu_x_size; // MCU's max. X size in pixels
+ int m_max_mcu_y_size; // MCU's max. Y size in pixels
+ int m_blocks_per_mcu;
+ int m_max_blocks_per_row;
+ int m_mcus_per_row, m_mcus_per_col;
+ int m_mcu_org[JPGD_MAX_BLOCKS_PER_MCU];
+ int m_total_lines_left; // total # lines left in image
+ int m_mcu_lines_left; // total # lines left in this MCU
+ int m_real_dest_bytes_per_scan_line;
+ int m_dest_bytes_per_scan_line; // rounded up
+ int m_dest_bytes_per_pixel; // 4 (RGB) or 1 (Y)
+ huff_tables* m_pHuff_tabs[JPGD_MAX_HUFF_TABLES];
+ coeff_buf* m_dc_coeffs[JPGD_MAX_COMPONENTS];
+ coeff_buf* m_ac_coeffs[JPGD_MAX_COMPONENTS];
+ int m_eob_run;
+ int m_block_y_mcu[JPGD_MAX_COMPONENTS];
+ uint8_t* m_pIn_buf_ofs;
+ int m_in_buf_left;
+ int m_tem_flag;
+ bool m_eof_flag;
+ uint8_t m_in_buf_pad_start[128];
+ uint8_t m_in_buf[JPGD_IN_BUF_SIZE + 128];
+ uint8_t m_in_buf_pad_end[128];
+ int m_bits_left;
+ uint32_t m_bit_buf;
+ int m_restart_interval;
+ int m_restarts_left;
+ int m_next_restart_num;
+ int m_max_mcus_per_row;
+ int m_max_blocks_per_mcu;
+ int m_expanded_blocks_per_mcu;
+ int m_expanded_blocks_per_row;
+ int m_expanded_blocks_per_component;
+ bool m_freq_domain_chroma_upsample;
+ int m_max_mcus_per_col;
+ uint32_t m_last_dc_val[JPGD_MAX_COMPONENTS];
+ jpgd_block_t* m_pMCU_coefficients;
+ int m_mcu_block_max_zag[JPGD_MAX_BLOCKS_PER_MCU];
+ uint8_t* m_pSample_buf;
+ int m_crr[256];
+ int m_cbb[256];
+ int m_crg[256];
+ int m_cbg[256];
+ uint8_t* m_pScan_line_0;
+ uint8_t* m_pScan_line_1;
+ jpgd_status m_error_code;
+ bool m_ready_flag;
+ int m_total_bytes_read;
+
+ void free_all_blocks();
+ JPGD_NORETURN void stop_decoding(jpgd_status status);
+ void *alloc(size_t n, bool zero = false);
+ void word_clear(void *p, uint16_t c, uint32_t n);
+ void prep_in_buffer();
+ void read_dht_marker();
+ void read_dqt_marker();
+ void read_sof_marker();
+ void skip_variable_marker();
+ void read_dri_marker();
+ void read_sos_marker();
+ int next_marker();
+ int process_markers();
+ void locate_soi_marker();
+ void locate_sof_marker();
+ int locate_sos_marker();
+ void init(jpeg_decoder_stream * pStream);
+ void create_look_ups();
+ void fix_in_buffer();
+ void transform_mcu(int mcu_row);
+ void transform_mcu_expand(int mcu_row);
+ coeff_buf* coeff_buf_open(int block_num_x, int block_num_y, int block_len_x, int block_len_y);
+ inline jpgd_block_t *coeff_buf_getp(coeff_buf *cb, int block_x, int block_y);
+ void load_next_row();
+ void decode_next_row();
+ void make_huff_table(int index, huff_tables *pH);
+ void check_quant_tables();
+ void check_huff_tables();
+ void calc_mcu_block_order();
+ int init_scan();
+ void init_frame();
+ void process_restart();
+ void decode_scan(pDecode_block_func decode_block_func);
+ void init_progressive();
+ void init_sequential();
+ void decode_start();
+ void decode_init(jpeg_decoder_stream * pStream);
+ void H2V2Convert();
+ void H2V1Convert();
+ void H1V2Convert();
+ void H1V1Convert();
+ void gray_convert();
+ void expanded_convert();
+ void find_eoi();
+ inline uint32_t get_char();
+ inline uint32_t get_char(bool *pPadding_flag);
+ inline void stuff_char(uint8_t q);
+ inline uint8_t get_octet();
+ inline uint32_t get_bits(int num_bits);
+ inline uint32_t get_bits_no_markers(int numbits);
+ inline int huff_decode(huff_tables *pH);
+ inline int huff_decode(huff_tables *pH, int& extrabits);
+ static inline uint8_t clamp(int i);
+ static void decode_block_dc_first(jpeg_decoder *pD, int component_id, int block_x, int block_y);
+ static void decode_block_dc_refine(jpeg_decoder *pD, int component_id, int block_x, int block_y);
+ static void decode_block_ac_first(jpeg_decoder *pD, int component_id, int block_x, int block_y);
+ static void decode_block_ac_refine(jpeg_decoder *pD, int component_id, int block_x, int block_y);
+};
+
+
+// DCT coefficients are stored in this sequence.
+static int g_ZAG[64] = { 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63 };
+
+enum JPEG_MARKER
+{
+ M_SOF0 = 0xC0, M_SOF1 = 0xC1, M_SOF2 = 0xC2, M_SOF3 = 0xC3, M_SOF5 = 0xC5, M_SOF6 = 0xC6, M_SOF7 = 0xC7, M_JPG = 0xC8,
+ M_SOF9 = 0xC9, M_SOF10 = 0xCA, M_SOF11 = 0xCB, M_SOF13 = 0xCD, M_SOF14 = 0xCE, M_SOF15 = 0xCF, M_DHT = 0xC4, M_DAC = 0xCC,
+ M_RST0 = 0xD0, M_RST1 = 0xD1, M_RST2 = 0xD2, M_RST3 = 0xD3, M_RST4 = 0xD4, M_RST5 = 0xD5, M_RST6 = 0xD6, M_RST7 = 0xD7,
+ M_SOI = 0xD8, M_EOI = 0xD9, M_SOS = 0xDA, M_DQT = 0xDB, M_DNL = 0xDC, M_DRI = 0xDD, M_DHP = 0xDE, M_EXP = 0xDF,
+ M_APP0 = 0xE0, M_APP15 = 0xEF, M_JPG0 = 0xF0, M_JPG13 = 0xFD, M_COM = 0xFE, M_TEM = 0x01, M_ERROR = 0x100, RST0 = 0xD0
+};
+
+enum JPEG_SUBSAMPLING { JPGD_GRAYSCALE = 0, JPGD_YH1V1, JPGD_YH2V1, JPGD_YH1V2, JPGD_YH2V2 };
+
+#define CONST_BITS 13
+#define PASS1_BITS 2
+#define SCALEDONE ((int32_t)1)
+#define DESCALE(x,n) (((x) + (SCALEDONE << ((n)-1))) >> (n))
+#define DESCALE_ZEROSHIFT(x,n) (((x) + (128 << (n)) + (SCALEDONE << ((n)-1))) >> (n))
+#define MULTIPLY(var, cnst) ((var) * (cnst))
+#define CLAMP(i) ((static_cast<uint32_t>(i) > 255) ? (((~i) >> 31) & 0xFF) : (i))
+
+#define FIX_0_298631336 ((int32_t)2446) /* FIX(0.298631336) */
+#define FIX_0_390180644 ((int32_t)3196) /* FIX(0.390180644) */
+#define FIX_0_541196100 ((int32_t)4433) /* FIX(0.541196100) */
+#define FIX_0_765366865 ((int32_t)6270) /* FIX(0.765366865) */
+#define FIX_0_899976223 ((int32_t)7373) /* FIX(0.899976223) */
+#define FIX_1_175875602 ((int32_t)9633) /* FIX(1.175875602) */
+#define FIX_1_501321110 ((int32_t)12299) /* FIX(1.501321110) */
+#define FIX_1_847759065 ((int32_t)15137) /* FIX(1.847759065) */
+#define FIX_1_961570560 ((int32_t)16069) /* FIX(1.961570560) */
+#define FIX_2_053119869 ((int32_t)16819) /* FIX(2.053119869) */
+#define FIX_2_562915447 ((int32_t)20995) /* FIX(2.562915447) */
+#define FIX_3_072711026 ((int32_t)25172) /* FIX(3.072711026) */
+
+
+// Compiler creates a fast path 1D IDCT for X non-zero columns
+template <int NONZERO_COLS>
+struct Row
+{
+ static void idct(int* pTemp, const jpgd_block_t* pSrc)
+ {
+ // ACCESS_COL() will be optimized at compile time to either an array access, or 0.
+ #define ACCESS_COL(x) (((x) < NONZERO_COLS) ? (int)pSrc[x] : 0)
+
+ const int z2 = ACCESS_COL(2), z3 = ACCESS_COL(6);
+ const int z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
+ const int tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
+ const int tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
+
+ const int tmp0 = static_cast<unsigned int>(ACCESS_COL(0) + ACCESS_COL(4)) << CONST_BITS;
+ const int tmp1 = static_cast<unsigned int>(ACCESS_COL(0) - ACCESS_COL(4)) << CONST_BITS;
+
+ const int tmp10 = tmp0 + tmp3, tmp13 = tmp0 - tmp3, tmp11 = tmp1 + tmp2, tmp12 = tmp1 - tmp2;
+
+ const int atmp0 = ACCESS_COL(7), atmp1 = ACCESS_COL(5), atmp2 = ACCESS_COL(3), atmp3 = ACCESS_COL(1);
+
+ const int bz1 = atmp0 + atmp3, bz2 = atmp1 + atmp2, bz3 = atmp0 + atmp2, bz4 = atmp1 + atmp3;
+ const int bz5 = MULTIPLY(bz3 + bz4, FIX_1_175875602);
+
+ const int az1 = MULTIPLY(bz1, - FIX_0_899976223);
+ const int az2 = MULTIPLY(bz2, - FIX_2_562915447);
+ const int az3 = MULTIPLY(bz3, - FIX_1_961570560) + bz5;
+ const int az4 = MULTIPLY(bz4, - FIX_0_390180644) + bz5;
+
+ const int btmp0 = MULTIPLY(atmp0, FIX_0_298631336) + az1 + az3;
+ const int btmp1 = MULTIPLY(atmp1, FIX_2_053119869) + az2 + az4;
+ const int btmp2 = MULTIPLY(atmp2, FIX_3_072711026) + az2 + az3;
+ const int btmp3 = MULTIPLY(atmp3, FIX_1_501321110) + az1 + az4;
+
+ pTemp[0] = DESCALE(tmp10 + btmp3, CONST_BITS-PASS1_BITS);
+ pTemp[7] = DESCALE(tmp10 - btmp3, CONST_BITS-PASS1_BITS);
+ pTemp[1] = DESCALE(tmp11 + btmp2, CONST_BITS-PASS1_BITS);
+ pTemp[6] = DESCALE(tmp11 - btmp2, CONST_BITS-PASS1_BITS);
+ pTemp[2] = DESCALE(tmp12 + btmp1, CONST_BITS-PASS1_BITS);
+ pTemp[5] = DESCALE(tmp12 - btmp1, CONST_BITS-PASS1_BITS);
+ pTemp[3] = DESCALE(tmp13 + btmp0, CONST_BITS-PASS1_BITS);
+ pTemp[4] = DESCALE(tmp13 - btmp0, CONST_BITS-PASS1_BITS);
+ }
+};
+
+
+template <>
+struct Row<0>
+{
+ static void idct(int* pTemp, const jpgd_block_t* pSrc)
+ {
+#ifdef _MSC_VER
+ pTemp; pSrc;
+#endif
+ }
+};
+
+
+template <>
+struct Row<1>
+{
+ static void idct(int* pTemp, const jpgd_block_t* pSrc)
+ {
+ const int dcval = pSrc[0] * PASS1_BITS * 2;
+
+ pTemp[0] = dcval;
+ pTemp[1] = dcval;
+ pTemp[2] = dcval;
+ pTemp[3] = dcval;
+ pTemp[4] = dcval;
+ pTemp[5] = dcval;
+ pTemp[6] = dcval;
+ pTemp[7] = dcval;
+ }
+};
+
+
+// Compiler creates a fast path 1D IDCT for X non-zero rows
+template <int NONZERO_ROWS>
+struct Col
+{
+ static void idct(uint8_t* pDst_ptr, const int* pTemp)
+ {
+ // ACCESS_ROW() will be optimized at compile time to either an array access, or 0.
+ #define ACCESS_ROW(x) (((x) < NONZERO_ROWS) ? pTemp[x * 8] : 0)
+
+ const int z2 = ACCESS_ROW(2);
+ const int z3 = ACCESS_ROW(6);
+
+ const int z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
+ const int tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
+ const int tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
+
+ const int tmp0 = static_cast<unsigned int>(ACCESS_ROW(0) + ACCESS_ROW(4)) << CONST_BITS;
+ const int tmp1 = static_cast<unsigned int>(ACCESS_ROW(0) - ACCESS_ROW(4)) << CONST_BITS;
+
+ const int tmp10 = tmp0 + tmp3, tmp13 = tmp0 - tmp3, tmp11 = tmp1 + tmp2, tmp12 = tmp1 - tmp2;
+
+ const int atmp0 = ACCESS_ROW(7), atmp1 = ACCESS_ROW(5), atmp2 = ACCESS_ROW(3), atmp3 = ACCESS_ROW(1);
+
+ const int bz1 = atmp0 + atmp3, bz2 = atmp1 + atmp2, bz3 = atmp0 + atmp2, bz4 = atmp1 + atmp3;
+ const int bz5 = MULTIPLY(bz3 + bz4, FIX_1_175875602);
+
+ const int az1 = MULTIPLY(bz1, - FIX_0_899976223);
+ const int az2 = MULTIPLY(bz2, - FIX_2_562915447);
+ const int az3 = MULTIPLY(bz3, - FIX_1_961570560) + bz5;
+ const int az4 = MULTIPLY(bz4, - FIX_0_390180644) + bz5;
+
+ const int btmp0 = MULTIPLY(atmp0, FIX_0_298631336) + az1 + az3;
+ const int btmp1 = MULTIPLY(atmp1, FIX_2_053119869) + az2 + az4;
+ const int btmp2 = MULTIPLY(atmp2, FIX_3_072711026) + az2 + az3;
+ const int btmp3 = MULTIPLY(atmp3, FIX_1_501321110) + az1 + az4;
+
+ int i = DESCALE_ZEROSHIFT(tmp10 + btmp3, CONST_BITS+PASS1_BITS+3);
+ pDst_ptr[8*0] = (uint8_t)CLAMP(i);
+
+ i = DESCALE_ZEROSHIFT(tmp10 - btmp3, CONST_BITS+PASS1_BITS+3);
+ pDst_ptr[8*7] = (uint8_t)CLAMP(i);
+
+ i = DESCALE_ZEROSHIFT(tmp11 + btmp2, CONST_BITS+PASS1_BITS+3);
+ pDst_ptr[8*1] = (uint8_t)CLAMP(i);
+
+ i = DESCALE_ZEROSHIFT(tmp11 - btmp2, CONST_BITS+PASS1_BITS+3);
+ pDst_ptr[8*6] = (uint8_t)CLAMP(i);
+
+ i = DESCALE_ZEROSHIFT(tmp12 + btmp1, CONST_BITS+PASS1_BITS+3);
+ pDst_ptr[8*2] = (uint8_t)CLAMP(i);
+
+ i = DESCALE_ZEROSHIFT(tmp12 - btmp1, CONST_BITS+PASS1_BITS+3);
+ pDst_ptr[8*5] = (uint8_t)CLAMP(i);
+
+ i = DESCALE_ZEROSHIFT(tmp13 + btmp0, CONST_BITS+PASS1_BITS+3);
+ pDst_ptr[8*3] = (uint8_t)CLAMP(i);
+
+ i = DESCALE_ZEROSHIFT(tmp13 - btmp0, CONST_BITS+PASS1_BITS+3);
+ pDst_ptr[8*4] = (uint8_t)CLAMP(i);
+ }
+};
+
+
+template <>
+struct Col<1>
+{
+ static void idct(uint8_t* pDst_ptr, const int* pTemp)
+ {
+ int dcval = DESCALE_ZEROSHIFT(pTemp[0], PASS1_BITS+3);
+ const uint8_t dcval_clamped = (uint8_t)CLAMP(dcval);
+ pDst_ptr[0*8] = dcval_clamped;
+ pDst_ptr[1*8] = dcval_clamped;
+ pDst_ptr[2*8] = dcval_clamped;
+ pDst_ptr[3*8] = dcval_clamped;
+ pDst_ptr[4*8] = dcval_clamped;
+ pDst_ptr[5*8] = dcval_clamped;
+ pDst_ptr[6*8] = dcval_clamped;
+ pDst_ptr[7*8] = dcval_clamped;
+ }
+};
+
+
+static const uint8_t s_idct_row_table[] = {
+ 1,0,0,0,0,0,0,0, 2,0,0,0,0,0,0,0, 2,1,0,0,0,0,0,0, 2,1,1,0,0,0,0,0, 2,2,1,0,0,0,0,0, 3,2,1,0,0,0,0,0, 4,2,1,0,0,0,0,0, 4,3,1,0,0,0,0,0,
+ 4,3,2,0,0,0,0,0, 4,3,2,1,0,0,0,0, 4,3,2,1,1,0,0,0, 4,3,2,2,1,0,0,0, 4,3,3,2,1,0,0,0, 4,4,3,2,1,0,0,0, 5,4,3,2,1,0,0,0, 6,4,3,2,1,0,0,0,
+ 6,5,3,2,1,0,0,0, 6,5,4,2,1,0,0,0, 6,5,4,3,1,0,0,0, 6,5,4,3,2,0,0,0, 6,5,4,3,2,1,0,0, 6,5,4,3,2,1,1,0, 6,5,4,3,2,2,1,0, 6,5,4,3,3,2,1,0,
+ 6,5,4,4,3,2,1,0, 6,5,5,4,3,2,1,0, 6,6,5,4,3,2,1,0, 7,6,5,4,3,2,1,0, 8,6,5,4,3,2,1,0, 8,7,5,4,3,2,1,0, 8,7,6,4,3,2,1,0, 8,7,6,5,3,2,1,0,
+ 8,7,6,5,4,2,1,0, 8,7,6,5,4,3,1,0, 8,7,6,5,4,3,2,0, 8,7,6,5,4,3,2,1, 8,7,6,5,4,3,2,2, 8,7,6,5,4,3,3,2, 8,7,6,5,4,4,3,2, 8,7,6,5,5,4,3,2,
+ 8,7,6,6,5,4,3,2, 8,7,7,6,5,4,3,2, 8,8,7,6,5,4,3,2, 8,8,8,6,5,4,3,2, 8,8,8,7,5,4,3,2, 8,8,8,7,6,4,3,2, 8,8,8,7,6,5,3,2, 8,8,8,7,6,5,4,2,
+ 8,8,8,7,6,5,4,3, 8,8,8,7,6,5,4,4, 8,8,8,7,6,5,5,4, 8,8,8,7,6,6,5,4, 8,8,8,7,7,6,5,4, 8,8,8,8,7,6,5,4, 8,8,8,8,8,6,5,4, 8,8,8,8,8,7,5,4,
+ 8,8,8,8,8,7,6,4, 8,8,8,8,8,7,6,5, 8,8,8,8,8,7,6,6, 8,8,8,8,8,7,7,6, 8,8,8,8,8,8,7,6, 8,8,8,8,8,8,8,6, 8,8,8,8,8,8,8,7, 8,8,8,8,8,8,8,8,
+};
+
+
+static const uint8_t s_idct_col_table[] = { 1, 1, 2, 3, 3, 3, 3, 3, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 };
+
+
+void idct(const jpgd_block_t* pSrc_ptr, uint8_t* pDst_ptr, int block_max_zag)
+{
+ JPGD_ASSERT(block_max_zag >= 1);
+ JPGD_ASSERT(block_max_zag <= 64);
+
+ if (block_max_zag <= 1) {
+ int k = ((pSrc_ptr[0] + 4) >> 3) + 128;
+ k = CLAMP(k);
+ k = k | (k<<8);
+ k = k | (k<<16);
+ for (int i = 8; i > 0; i--) {
+ *(int*)&pDst_ptr[0] = k;
+ *(int*)&pDst_ptr[4] = k;
+ pDst_ptr += 8;
+ }
+ return;
+ }
+
+ int temp[64];
+ const jpgd_block_t* pSrc = pSrc_ptr;
+ int* pTemp = temp;
+ const uint8_t* pRow_tab = &s_idct_row_table[(block_max_zag - 1) * 8];
+ int i;
+ for (i = 8; i > 0; i--, pRow_tab++) {
+ switch (*pRow_tab) {
+ case 0: Row<0>::idct(pTemp, pSrc); break;
+ case 1: Row<1>::idct(pTemp, pSrc); break;
+ case 2: Row<2>::idct(pTemp, pSrc); break;
+ case 3: Row<3>::idct(pTemp, pSrc); break;
+ case 4: Row<4>::idct(pTemp, pSrc); break;
+ case 5: Row<5>::idct(pTemp, pSrc); break;
+ case 6: Row<6>::idct(pTemp, pSrc); break;
+ case 7: Row<7>::idct(pTemp, pSrc); break;
+ case 8: Row<8>::idct(pTemp, pSrc); break;
+ }
+ pSrc += 8;
+ pTemp += 8;
+ }
+
+ pTemp = temp;
+
+ const int nonzero_rows = s_idct_col_table[block_max_zag - 1];
+ for (i = 8; i > 0; i--) {
+ switch (nonzero_rows) {
+ case 1: Col<1>::idct(pDst_ptr, pTemp); break;
+ case 2: Col<2>::idct(pDst_ptr, pTemp); break;
+ case 3: Col<3>::idct(pDst_ptr, pTemp); break;
+ case 4: Col<4>::idct(pDst_ptr, pTemp); break;
+ case 5: Col<5>::idct(pDst_ptr, pTemp); break;
+ case 6: Col<6>::idct(pDst_ptr, pTemp); break;
+ case 7: Col<7>::idct(pDst_ptr, pTemp); break;
+ case 8: Col<8>::idct(pDst_ptr, pTemp); break;
+ }
+ pTemp++;
+ pDst_ptr++;
+ }
+}
+
+
+void idct_4x4(const jpgd_block_t* pSrc_ptr, uint8_t* pDst_ptr)
+{
+ int temp[64];
+ int* pTemp = temp;
+ const jpgd_block_t* pSrc = pSrc_ptr;
+
+ for (int i = 4; i > 0; i--) {
+ Row<4>::idct(pTemp, pSrc);
+ pSrc += 8;
+ pTemp += 8;
+ }
+
+ pTemp = temp;
+
+ for (int i = 8; i > 0; i--) {
+ Col<4>::idct(pDst_ptr, pTemp);
+ pTemp++;
+ pDst_ptr++;
+ }
+}
+
+
+// Retrieve one character from the input stream.
+inline uint32_t jpeg_decoder::get_char()
+{
+ // Any bytes remaining in buffer?
+ if (!m_in_buf_left) {
+ // Try to get more bytes.
+ prep_in_buffer();
+ // Still nothing to get?
+ if (!m_in_buf_left) {
+ // Pad the end of the stream with 0xFF 0xD9 (EOI marker)
+ int t = m_tem_flag;
+ m_tem_flag ^= 1;
+ if (t) return 0xD9;
+ else return 0xFF;
+ }
+ }
+ uint32_t c = *m_pIn_buf_ofs++;
+ m_in_buf_left--;
+ return c;
+}
+
+
+// Same as previous method, except can indicate if the character is a pad character or not.
+inline uint32_t jpeg_decoder::get_char(bool *pPadding_flag)
+{
+ if (!m_in_buf_left) {
+ prep_in_buffer();
+ if (!m_in_buf_left) {
+ *pPadding_flag = true;
+ int t = m_tem_flag;
+ m_tem_flag ^= 1;
+ if (t) return 0xD9;
+ else return 0xFF;
+ }
+ }
+ *pPadding_flag = false;
+ uint32_t c = *m_pIn_buf_ofs++;
+ m_in_buf_left--;
+
+ return c;
+}
+
+
+// Inserts a previously retrieved character back into the input buffer.
+inline void jpeg_decoder::stuff_char(uint8_t q)
+{
+ *(--m_pIn_buf_ofs) = q;
+ m_in_buf_left++;
+}
+
+
+// Retrieves one character from the input stream, but does not read past markers. Will continue to return 0xFF when a marker is encountered.
+inline uint8_t jpeg_decoder::get_octet()
+{
+ bool padding_flag;
+ int c = get_char(&padding_flag);
+
+ if (c == 0xFF) {
+ if (padding_flag) return 0xFF;
+
+ c = get_char(&padding_flag);
+ if (padding_flag) {
+ stuff_char(0xFF);
+ return 0xFF;
+ }
+ if (c == 0x00) return 0xFF;
+ else {
+ stuff_char(static_cast<uint8_t>(c));
+ stuff_char(0xFF);
+ return 0xFF;
+ }
+ }
+ return static_cast<uint8_t>(c);
+}
+
+
+// Retrieves a variable number of bits from the input stream. Does not recognize markers.
+inline uint32_t jpeg_decoder::get_bits(int num_bits)
+{
+ if (!num_bits) return 0;
+
+ uint32_t i = m_bit_buf >> (32 - num_bits);
+
+ if ((m_bits_left -= num_bits) <= 0) {
+ m_bit_buf <<= (num_bits += m_bits_left);
+ uint32_t c1 = get_char();
+ uint32_t c2 = get_char();
+ m_bit_buf = (m_bit_buf & 0xFFFF0000) | (c1 << 8) | c2;
+ m_bit_buf <<= -m_bits_left;
+ m_bits_left += 16;
+ JPGD_ASSERT(m_bits_left >= 0);
+ }
+ else m_bit_buf <<= num_bits;
+
+ return i;
+}
+
+
+// Retrieves a variable number of bits from the input stream. Markers will not be read into the input bit buffer. Instead, an infinite number of all 1's will be returned when a marker is encountered.
+inline uint32_t jpeg_decoder::get_bits_no_markers(int num_bits)
+{
+ if (!num_bits)return 0;
+
+ uint32_t i = m_bit_buf >> (32 - num_bits);
+
+ if ((m_bits_left -= num_bits) <= 0) {
+ m_bit_buf <<= (num_bits += m_bits_left);
+ if ((m_in_buf_left < 2) || (m_pIn_buf_ofs[0] == 0xFF) || (m_pIn_buf_ofs[1] == 0xFF)) {
+ uint32_t c1 = get_octet();
+ uint32_t c2 = get_octet();
+ m_bit_buf |= (c1 << 8) | c2;
+ } else {
+ m_bit_buf |= ((uint32_t)m_pIn_buf_ofs[0] << 8) | m_pIn_buf_ofs[1];
+ m_in_buf_left -= 2;
+ m_pIn_buf_ofs += 2;
+ }
+ m_bit_buf <<= -m_bits_left;
+ m_bits_left += 16;
+ JPGD_ASSERT(m_bits_left >= 0);
+ } else m_bit_buf <<= num_bits;
+
+ return i;
+}
+
+
+// Decodes a Huffman encoded symbol.
+inline int jpeg_decoder::huff_decode(huff_tables *pH)
+{
+ int symbol;
+
+ // Check first 8-bits: do we have a complete symbol?
+ if ((symbol = pH->look_up[m_bit_buf >> 24]) < 0) {
+ // Decode more bits, use a tree traversal to find symbol.
+ int ofs = 23;
+ do {
+ symbol = pH->tree[-(int)(symbol + ((m_bit_buf >> ofs) & 1))];
+ ofs--;
+ } while (symbol < 0);
+ get_bits_no_markers(8 + (23 - ofs));
+ } else get_bits_no_markers(pH->code_size[symbol]);
+
+ return symbol;
+}
+
+
+// Decodes a Huffman encoded symbol.
+inline int jpeg_decoder::huff_decode(huff_tables *pH, int& extra_bits)
+{
+ int symbol;
+
+ // Check first 8-bits: do we have a complete symbol?
+ if ((symbol = pH->look_up2[m_bit_buf >> 24]) < 0) {
+ // Use a tree traversal to find symbol.
+ int ofs = 23;
+ do {
+ symbol = pH->tree[-(int)(symbol + ((m_bit_buf >> ofs) & 1))];
+ ofs--;
+ } while (symbol < 0);
+
+ get_bits_no_markers(8 + (23 - ofs));
+ extra_bits = get_bits_no_markers(symbol & 0xF);
+ } else {
+ JPGD_ASSERT(((symbol >> 8) & 31) == pH->code_size[symbol & 255] + ((symbol & 0x8000) ? (symbol & 15) : 0));
+
+ if (symbol & 0x8000) {
+ get_bits_no_markers((symbol >> 8) & 31);
+ extra_bits = symbol >> 16;
+ } else {
+ int code_size = (symbol >> 8) & 31;
+ int num_extra_bits = symbol & 0xF;
+ int bits = code_size + num_extra_bits;
+ if (bits <= (m_bits_left + 16)) extra_bits = get_bits_no_markers(bits) & ((1 << num_extra_bits) - 1);
+ else {
+ get_bits_no_markers(code_size);
+ extra_bits = get_bits_no_markers(num_extra_bits);
+ }
+ }
+ symbol &= 0xFF;
+ }
+ return symbol;
+}
+
+
+// Tables and macro used to fully decode the DPCM differences.
+static const int s_extend_test[16] = { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
+static const unsigned int s_extend_offset[16] = { 0, ((~0u)<<1) + 1, ((~0u)<<2) + 1, ((~0u)<<3) + 1, ((~0u)<<4) + 1, ((~0u)<<5) + 1, ((~0u)<<6) + 1, ((~0u)<<7) + 1, ((~0u)<<8) + 1, ((~0u)<<9) + 1, ((~0u)<<10) + 1, ((~0u)<<11) + 1, ((~0u)<<12) + 1, ((~0u)<<13) + 1, ((~0u)<<14) + 1, ((~0u)<<15) + 1 };
+
+// The logical AND's in this macro are to shut up static code analysis (aren't really necessary - couldn't find another way to do this)
+#define JPGD_HUFF_EXTEND(x, s) (((x) < s_extend_test[s & 15]) ? ((x) + s_extend_offset[s & 15]) : (x))
+
+
+// Clamps a value between 0-255.
+inline uint8_t jpeg_decoder::clamp(int i)
+{
+ if (static_cast<uint32_t>(i) > 255) i = (((~i) >> 31) & 0xFF);
+ return static_cast<uint8_t>(i);
+}
+
+
+namespace DCT_Upsample
+{
+ struct Matrix44
+ {
+ typedef int Element_Type;
+ enum { NUM_ROWS = 4, NUM_COLS = 4 };
+
+ Element_Type v[NUM_ROWS][NUM_COLS];
+
+ inline int rows() const { return NUM_ROWS; }
+ inline int cols() const { return NUM_COLS; }
+ inline const Element_Type & at(int r, int c) const { return v[r][c]; }
+ inline Element_Type & at(int r, int c) { return v[r][c]; }
+
+ inline Matrix44() {}
+
+ inline Matrix44& operator += (const Matrix44& a)
+ {
+ for (int r = 0; r < NUM_ROWS; r++) {
+ at(r, 0) += a.at(r, 0);
+ at(r, 1) += a.at(r, 1);
+ at(r, 2) += a.at(r, 2);
+ at(r, 3) += a.at(r, 3);
+ }
+ return *this;
+ }
+
+ inline Matrix44& operator -= (const Matrix44& a)
+ {
+ for (int r = 0; r < NUM_ROWS; r++) {
+ at(r, 0) -= a.at(r, 0);
+ at(r, 1) -= a.at(r, 1);
+ at(r, 2) -= a.at(r, 2);
+ at(r, 3) -= a.at(r, 3);
+ }
+ return *this;
+ }
+
+ friend inline Matrix44 operator + (const Matrix44& a, const Matrix44& b)
+ {
+ Matrix44 ret;
+ for (int r = 0; r < NUM_ROWS; r++) {
+ ret.at(r, 0) = a.at(r, 0) + b.at(r, 0);
+ ret.at(r, 1) = a.at(r, 1) + b.at(r, 1);
+ ret.at(r, 2) = a.at(r, 2) + b.at(r, 2);
+ ret.at(r, 3) = a.at(r, 3) + b.at(r, 3);
+ }
+ return ret;
+ }
+
+ friend inline Matrix44 operator - (const Matrix44& a, const Matrix44& b)
+ {
+ Matrix44 ret;
+ for (int r = 0; r < NUM_ROWS; r++) {
+ ret.at(r, 0) = a.at(r, 0) - b.at(r, 0);
+ ret.at(r, 1) = a.at(r, 1) - b.at(r, 1);
+ ret.at(r, 2) = a.at(r, 2) - b.at(r, 2);
+ ret.at(r, 3) = a.at(r, 3) - b.at(r, 3);
+ }
+ return ret;
+ }
+
+ static inline void add_and_store(jpgd_block_t* pDst, const Matrix44& a, const Matrix44& b)
+ {
+ for (int r = 0; r < 4; r++) {
+ pDst[0*8 + r] = static_cast<jpgd_block_t>(a.at(r, 0) + b.at(r, 0));
+ pDst[1*8 + r] = static_cast<jpgd_block_t>(a.at(r, 1) + b.at(r, 1));
+ pDst[2*8 + r] = static_cast<jpgd_block_t>(a.at(r, 2) + b.at(r, 2));
+ pDst[3*8 + r] = static_cast<jpgd_block_t>(a.at(r, 3) + b.at(r, 3));
+ }
+ }
+
+ static inline void sub_and_store(jpgd_block_t* pDst, const Matrix44& a, const Matrix44& b)
+ {
+ for (int r = 0; r < 4; r++) {
+ pDst[0*8 + r] = static_cast<jpgd_block_t>(a.at(r, 0) - b.at(r, 0));
+ pDst[1*8 + r] = static_cast<jpgd_block_t>(a.at(r, 1) - b.at(r, 1));
+ pDst[2*8 + r] = static_cast<jpgd_block_t>(a.at(r, 2) - b.at(r, 2));
+ pDst[3*8 + r] = static_cast<jpgd_block_t>(a.at(r, 3) - b.at(r, 3));
+ }
+ }
+ };
+
+ const int FRACT_BITS = 10;
+ const int SCALE = 1 << FRACT_BITS;
+
+ typedef int Temp_Type;
+ #define D(i) (((i) + (SCALE >> 1)) >> FRACT_BITS)
+ #define F(i) ((int)((i) * SCALE + .5f))
+
+ // Any decent C++ compiler will optimize this at compile time to a 0, or an array access.
+ #define AT(c, r) ((((c)>=NUM_COLS)||((r)>=NUM_ROWS)) ? 0 : pSrc[(c)+(r)*8])
+
+ // NUM_ROWS/NUM_COLS = # of non-zero rows/cols in input matrix
+ template<int NUM_ROWS, int NUM_COLS>
+ struct P_Q
+ {
+ static void calc(Matrix44& P, Matrix44& Q, const jpgd_block_t* pSrc)
+ {
+ // 4x8 = 4x8 times 8x8, matrix 0 is constant
+ const Temp_Type X000 = AT(0, 0);
+ const Temp_Type X001 = AT(0, 1);
+ const Temp_Type X002 = AT(0, 2);
+ const Temp_Type X003 = AT(0, 3);
+ const Temp_Type X004 = AT(0, 4);
+ const Temp_Type X005 = AT(0, 5);
+ const Temp_Type X006 = AT(0, 6);
+ const Temp_Type X007 = AT(0, 7);
+ const Temp_Type X010 = D(F(0.415735f) * AT(1, 0) + F(0.791065f) * AT(3, 0) + F(-0.352443f) * AT(5, 0) + F(0.277785f) * AT(7, 0));
+ const Temp_Type X011 = D(F(0.415735f) * AT(1, 1) + F(0.791065f) * AT(3, 1) + F(-0.352443f) * AT(5, 1) + F(0.277785f) * AT(7, 1));
+ const Temp_Type X012 = D(F(0.415735f) * AT(1, 2) + F(0.791065f) * AT(3, 2) + F(-0.352443f) * AT(5, 2) + F(0.277785f) * AT(7, 2));
+ const Temp_Type X013 = D(F(0.415735f) * AT(1, 3) + F(0.791065f) * AT(3, 3) + F(-0.352443f) * AT(5, 3) + F(0.277785f) * AT(7, 3));
+ const Temp_Type X014 = D(F(0.415735f) * AT(1, 4) + F(0.791065f) * AT(3, 4) + F(-0.352443f) * AT(5, 4) + F(0.277785f) * AT(7, 4));
+ const Temp_Type X015 = D(F(0.415735f) * AT(1, 5) + F(0.791065f) * AT(3, 5) + F(-0.352443f) * AT(5, 5) + F(0.277785f) * AT(7, 5));
+ const Temp_Type X016 = D(F(0.415735f) * AT(1, 6) + F(0.791065f) * AT(3, 6) + F(-0.352443f) * AT(5, 6) + F(0.277785f) * AT(7, 6));
+ const Temp_Type X017 = D(F(0.415735f) * AT(1, 7) + F(0.791065f) * AT(3, 7) + F(-0.352443f) * AT(5, 7) + F(0.277785f) * AT(7, 7));
+ const Temp_Type X020 = AT(4, 0);
+ const Temp_Type X021 = AT(4, 1);
+ const Temp_Type X022 = AT(4, 2);
+ const Temp_Type X023 = AT(4, 3);
+ const Temp_Type X024 = AT(4, 4);
+ const Temp_Type X025 = AT(4, 5);
+ const Temp_Type X026 = AT(4, 6);
+ const Temp_Type X027 = AT(4, 7);
+ const Temp_Type X030 = D(F(0.022887f) * AT(1, 0) + F(-0.097545f) * AT(3, 0) + F(0.490393f) * AT(5, 0) + F(0.865723f) * AT(7, 0));
+ const Temp_Type X031 = D(F(0.022887f) * AT(1, 1) + F(-0.097545f) * AT(3, 1) + F(0.490393f) * AT(5, 1) + F(0.865723f) * AT(7, 1));
+ const Temp_Type X032 = D(F(0.022887f) * AT(1, 2) + F(-0.097545f) * AT(3, 2) + F(0.490393f) * AT(5, 2) + F(0.865723f) * AT(7, 2));
+ const Temp_Type X033 = D(F(0.022887f) * AT(1, 3) + F(-0.097545f) * AT(3, 3) + F(0.490393f) * AT(5, 3) + F(0.865723f) * AT(7, 3));
+ const Temp_Type X034 = D(F(0.022887f) * AT(1, 4) + F(-0.097545f) * AT(3, 4) + F(0.490393f) * AT(5, 4) + F(0.865723f) * AT(7, 4));
+ const Temp_Type X035 = D(F(0.022887f) * AT(1, 5) + F(-0.097545f) * AT(3, 5) + F(0.490393f) * AT(5, 5) + F(0.865723f) * AT(7, 5));
+ const Temp_Type X036 = D(F(0.022887f) * AT(1, 6) + F(-0.097545f) * AT(3, 6) + F(0.490393f) * AT(5, 6) + F(0.865723f) * AT(7, 6));
+ const Temp_Type X037 = D(F(0.022887f) * AT(1, 7) + F(-0.097545f) * AT(3, 7) + F(0.490393f) * AT(5, 7) + F(0.865723f) * AT(7, 7));
+
+ // 4x4 = 4x8 times 8x4, matrix 1 is constant
+ P.at(0, 0) = X000;
+ P.at(0, 1) = D(X001 * F(0.415735f) + X003 * F(0.791065f) + X005 * F(-0.352443f) + X007 * F(0.277785f));
+ P.at(0, 2) = X004;
+ P.at(0, 3) = D(X001 * F(0.022887f) + X003 * F(-0.097545f) + X005 * F(0.490393f) + X007 * F(0.865723f));
+ P.at(1, 0) = X010;
+ P.at(1, 1) = D(X011 * F(0.415735f) + X013 * F(0.791065f) + X015 * F(-0.352443f) + X017 * F(0.277785f));
+ P.at(1, 2) = X014;
+ P.at(1, 3) = D(X011 * F(0.022887f) + X013 * F(-0.097545f) + X015 * F(0.490393f) + X017 * F(0.865723f));
+ P.at(2, 0) = X020;
+ P.at(2, 1) = D(X021 * F(0.415735f) + X023 * F(0.791065f) + X025 * F(-0.352443f) + X027 * F(0.277785f));
+ P.at(2, 2) = X024;
+ P.at(2, 3) = D(X021 * F(0.022887f) + X023 * F(-0.097545f) + X025 * F(0.490393f) + X027 * F(0.865723f));
+ P.at(3, 0) = X030;
+ P.at(3, 1) = D(X031 * F(0.415735f) + X033 * F(0.791065f) + X035 * F(-0.352443f) + X037 * F(0.277785f));
+ P.at(3, 2) = X034;
+ P.at(3, 3) = D(X031 * F(0.022887f) + X033 * F(-0.097545f) + X035 * F(0.490393f) + X037 * F(0.865723f));
+ // 40 muls 24 adds
+
+ // 4x4 = 4x8 times 8x4, matrix 1 is constant
+ Q.at(0, 0) = D(X001 * F(0.906127f) + X003 * F(-0.318190f) + X005 * F(0.212608f) + X007 * F(-0.180240f));
+ Q.at(0, 1) = X002;
+ Q.at(0, 2) = D(X001 * F(-0.074658f) + X003 * F(0.513280f) + X005 * F(0.768178f) + X007 * F(-0.375330f));
+ Q.at(0, 3) = X006;
+ Q.at(1, 0) = D(X011 * F(0.906127f) + X013 * F(-0.318190f) + X015 * F(0.212608f) + X017 * F(-0.180240f));
+ Q.at(1, 1) = X012;
+ Q.at(1, 2) = D(X011 * F(-0.074658f) + X013 * F(0.513280f) + X015 * F(0.768178f) + X017 * F(-0.375330f));
+ Q.at(1, 3) = X016;
+ Q.at(2, 0) = D(X021 * F(0.906127f) + X023 * F(-0.318190f) + X025 * F(0.212608f) + X027 * F(-0.180240f));
+ Q.at(2, 1) = X022;
+ Q.at(2, 2) = D(X021 * F(-0.074658f) + X023 * F(0.513280f) + X025 * F(0.768178f) + X027 * F(-0.375330f));
+ Q.at(2, 3) = X026;
+ Q.at(3, 0) = D(X031 * F(0.906127f) + X033 * F(-0.318190f) + X035 * F(0.212608f) + X037 * F(-0.180240f));
+ Q.at(3, 1) = X032;
+ Q.at(3, 2) = D(X031 * F(-0.074658f) + X033 * F(0.513280f) + X035 * F(0.768178f) + X037 * F(-0.375330f));
+ Q.at(3, 3) = X036;
+ // 40 muls 24 adds
+ }
+ };
+
+
+ template<int NUM_ROWS, int NUM_COLS>
+ struct R_S
+ {
+ static void calc(Matrix44& R, Matrix44& S, const jpgd_block_t* pSrc)
+ {
+ // 4x8 = 4x8 times 8x8, matrix 0 is constant
+ const Temp_Type X100 = D(F(0.906127f) * AT(1, 0) + F(-0.318190f) * AT(3, 0) + F(0.212608f) * AT(5, 0) + F(-0.180240f) * AT(7, 0));
+ const Temp_Type X101 = D(F(0.906127f) * AT(1, 1) + F(-0.318190f) * AT(3, 1) + F(0.212608f) * AT(5, 1) + F(-0.180240f) * AT(7, 1));
+ const Temp_Type X102 = D(F(0.906127f) * AT(1, 2) + F(-0.318190f) * AT(3, 2) + F(0.212608f) * AT(5, 2) + F(-0.180240f) * AT(7, 2));
+ const Temp_Type X103 = D(F(0.906127f) * AT(1, 3) + F(-0.318190f) * AT(3, 3) + F(0.212608f) * AT(5, 3) + F(-0.180240f) * AT(7, 3));
+ const Temp_Type X104 = D(F(0.906127f) * AT(1, 4) + F(-0.318190f) * AT(3, 4) + F(0.212608f) * AT(5, 4) + F(-0.180240f) * AT(7, 4));
+ const Temp_Type X105 = D(F(0.906127f) * AT(1, 5) + F(-0.318190f) * AT(3, 5) + F(0.212608f) * AT(5, 5) + F(-0.180240f) * AT(7, 5));
+ const Temp_Type X106 = D(F(0.906127f) * AT(1, 6) + F(-0.318190f) * AT(3, 6) + F(0.212608f) * AT(5, 6) + F(-0.180240f) * AT(7, 6));
+ const Temp_Type X107 = D(F(0.906127f) * AT(1, 7) + F(-0.318190f) * AT(3, 7) + F(0.212608f) * AT(5, 7) + F(-0.180240f) * AT(7, 7));
+ const Temp_Type X110 = AT(2, 0);
+ const Temp_Type X111 = AT(2, 1);
+ const Temp_Type X112 = AT(2, 2);
+ const Temp_Type X113 = AT(2, 3);
+ const Temp_Type X114 = AT(2, 4);
+ const Temp_Type X115 = AT(2, 5);
+ const Temp_Type X116 = AT(2, 6);
+ const Temp_Type X117 = AT(2, 7);
+ const Temp_Type X120 = D(F(-0.074658f) * AT(1, 0) + F(0.513280f) * AT(3, 0) + F(0.768178f) * AT(5, 0) + F(-0.375330f) * AT(7, 0));
+ const Temp_Type X121 = D(F(-0.074658f) * AT(1, 1) + F(0.513280f) * AT(3, 1) + F(0.768178f) * AT(5, 1) + F(-0.375330f) * AT(7, 1));
+ const Temp_Type X122 = D(F(-0.074658f) * AT(1, 2) + F(0.513280f) * AT(3, 2) + F(0.768178f) * AT(5, 2) + F(-0.375330f) * AT(7, 2));
+ const Temp_Type X123 = D(F(-0.074658f) * AT(1, 3) + F(0.513280f) * AT(3, 3) + F(0.768178f) * AT(5, 3) + F(-0.375330f) * AT(7, 3));
+ const Temp_Type X124 = D(F(-0.074658f) * AT(1, 4) + F(0.513280f) * AT(3, 4) + F(0.768178f) * AT(5, 4) + F(-0.375330f) * AT(7, 4));
+ const Temp_Type X125 = D(F(-0.074658f) * AT(1, 5) + F(0.513280f) * AT(3, 5) + F(0.768178f) * AT(5, 5) + F(-0.375330f) * AT(7, 5));
+ const Temp_Type X126 = D(F(-0.074658f) * AT(1, 6) + F(0.513280f) * AT(3, 6) + F(0.768178f) * AT(5, 6) + F(-0.375330f) * AT(7, 6));
+ const Temp_Type X127 = D(F(-0.074658f) * AT(1, 7) + F(0.513280f) * AT(3, 7) + F(0.768178f) * AT(5, 7) + F(-0.375330f) * AT(7, 7));
+ const Temp_Type X130 = AT(6, 0);
+ const Temp_Type X131 = AT(6, 1);
+ const Temp_Type X132 = AT(6, 2);
+ const Temp_Type X133 = AT(6, 3);
+ const Temp_Type X134 = AT(6, 4);
+ const Temp_Type X135 = AT(6, 5);
+ const Temp_Type X136 = AT(6, 6);
+ const Temp_Type X137 = AT(6, 7);
+ // 80 muls 48 adds
+
+ // 4x4 = 4x8 times 8x4, matrix 1 is constant
+ R.at(0, 0) = X100;
+ R.at(0, 1) = D(X101 * F(0.415735f) + X103 * F(0.791065f) + X105 * F(-0.352443f) + X107 * F(0.277785f));
+ R.at(0, 2) = X104;
+ R.at(0, 3) = D(X101 * F(0.022887f) + X103 * F(-0.097545f) + X105 * F(0.490393f) + X107 * F(0.865723f));
+ R.at(1, 0) = X110;
+ R.at(1, 1) = D(X111 * F(0.415735f) + X113 * F(0.791065f) + X115 * F(-0.352443f) + X117 * F(0.277785f));
+ R.at(1, 2) = X114;
+ R.at(1, 3) = D(X111 * F(0.022887f) + X113 * F(-0.097545f) + X115 * F(0.490393f) + X117 * F(0.865723f));
+ R.at(2, 0) = X120;
+ R.at(2, 1) = D(X121 * F(0.415735f) + X123 * F(0.791065f) + X125 * F(-0.352443f) + X127 * F(0.277785f));
+ R.at(2, 2) = X124;
+ R.at(2, 3) = D(X121 * F(0.022887f) + X123 * F(-0.097545f) + X125 * F(0.490393f) + X127 * F(0.865723f));
+ R.at(3, 0) = X130;
+ R.at(3, 1) = D(X131 * F(0.415735f) + X133 * F(0.791065f) + X135 * F(-0.352443f) + X137 * F(0.277785f));
+ R.at(3, 2) = X134;
+ R.at(3, 3) = D(X131 * F(0.022887f) + X133 * F(-0.097545f) + X135 * F(0.490393f) + X137 * F(0.865723f));
+ // 40 muls 24 adds
+ // 4x4 = 4x8 times 8x4, matrix 1 is constant
+ S.at(0, 0) = D(X101 * F(0.906127f) + X103 * F(-0.318190f) + X105 * F(0.212608f) + X107 * F(-0.180240f));
+ S.at(0, 1) = X102;
+ S.at(0, 2) = D(X101 * F(-0.074658f) + X103 * F(0.513280f) + X105 * F(0.768178f) + X107 * F(-0.375330f));
+ S.at(0, 3) = X106;
+ S.at(1, 0) = D(X111 * F(0.906127f) + X113 * F(-0.318190f) + X115 * F(0.212608f) + X117 * F(-0.180240f));
+ S.at(1, 1) = X112;
+ S.at(1, 2) = D(X111 * F(-0.074658f) + X113 * F(0.513280f) + X115 * F(0.768178f) + X117 * F(-0.375330f));
+ S.at(1, 3) = X116;
+ S.at(2, 0) = D(X121 * F(0.906127f) + X123 * F(-0.318190f) + X125 * F(0.212608f) + X127 * F(-0.180240f));
+ S.at(2, 1) = X122;
+ S.at(2, 2) = D(X121 * F(-0.074658f) + X123 * F(0.513280f) + X125 * F(0.768178f) + X127 * F(-0.375330f));
+ S.at(2, 3) = X126;
+ S.at(3, 0) = D(X131 * F(0.906127f) + X133 * F(-0.318190f) + X135 * F(0.212608f) + X137 * F(-0.180240f));
+ S.at(3, 1) = X132;
+ S.at(3, 2) = D(X131 * F(-0.074658f) + X133 * F(0.513280f) + X135 * F(0.768178f) + X137 * F(-0.375330f));
+ S.at(3, 3) = X136;
+ // 40 muls 24 adds
+ }
+ };
+} // end namespace DCT_Upsample
+
+
+// Unconditionally frees all allocated m_blocks.
+void jpeg_decoder::free_all_blocks()
+{
+ delete(m_pStream);
+ m_pStream = nullptr;
+
+ for (mem_block *b = m_pMem_blocks; b; ) {
+ mem_block *n = b->m_pNext;
+ free(b);
+ b = n;
+ }
+ m_pMem_blocks = nullptr;
+}
+
+
+// This method handles all errors. It will never return.
+// It could easily be changed to use C++ exceptions.
+JPGD_NORETURN void jpeg_decoder::stop_decoding(jpgd_status status)
+{
+ m_error_code = status;
+ free_all_blocks();
+ longjmp(m_jmp_state, status);
+}
+
+
+void *jpeg_decoder::alloc(size_t nSize, bool zero)
+{
+ nSize = (JPGD_MAX(nSize, 1) + 3) & ~3;
+ char *rv = nullptr;
+ for (mem_block *b = m_pMem_blocks; b; b = b->m_pNext) {
+ if ((b->m_used_count + nSize) <= b->m_size) {
+ rv = b->m_data + b->m_used_count;
+ b->m_used_count += nSize;
+ break;
+ }
+ }
+ if (!rv) {
+ int capacity = JPGD_MAX(32768 - 256, (nSize + 2047) & ~2047);
+ mem_block *b = (mem_block*)malloc(sizeof(mem_block) + capacity);
+ if (!b) stop_decoding(JPGD_NOTENOUGHMEM);
+ b->m_pNext = m_pMem_blocks; m_pMem_blocks = b;
+ b->m_used_count = nSize;
+ b->m_size = capacity;
+ rv = b->m_data;
+ }
+ if (zero) memset(rv, 0, nSize);
+ return rv;
+}
+
+
+void jpeg_decoder::word_clear(void *p, uint16_t c, uint32_t n)
+{
+ uint8_t *pD = (uint8_t*)p;
+ const uint8_t l = c & 0xFF, h = (c >> 8) & 0xFF;
+ while (n) {
+ pD[0] = l; pD[1] = h; pD += 2;
+ n--;
+ }
+}
+
+
+// Refill the input buffer.
+// This method will sit in a loop until (A) the buffer is full or (B)
+// the stream's read() method reports and end of file condition.
+void jpeg_decoder::prep_in_buffer()
+{
+ m_in_buf_left = 0;
+ m_pIn_buf_ofs = m_in_buf;
+
+ if (m_eof_flag) return;
+
+ do {
+ int bytes_read = m_pStream->read(m_in_buf + m_in_buf_left, JPGD_IN_BUF_SIZE - m_in_buf_left, &m_eof_flag);
+ if (bytes_read == -1) stop_decoding(JPGD_STREAM_READ);
+ m_in_buf_left += bytes_read;
+ } while ((m_in_buf_left < JPGD_IN_BUF_SIZE) && (!m_eof_flag));
+
+ m_total_bytes_read += m_in_buf_left;
+
+ // Pad the end of the block with M_EOI (prevents the decompressor from going off the rails if the stream is invalid).
+ // (This dates way back to when this decompressor was written in C/asm, and the all-asm Huffman decoder did some fancy things to increase perf.)
+ word_clear(m_pIn_buf_ofs + m_in_buf_left, 0xD9FF, 64);
+}
+
+
+// Read a Huffman code table.
+void jpeg_decoder::read_dht_marker()
+{
+ int i, index, count;
+ uint8_t huff_num[17];
+ uint8_t huff_val[256];
+ uint32_t num_left = get_bits(16);
+
+ if (num_left < 2) stop_decoding(JPGD_BAD_DHT_MARKER);
+ num_left -= 2;
+
+ while (num_left) {
+ index = get_bits(8);
+ huff_num[0] = 0;
+ count = 0;
+
+ for (i = 1; i <= 16; i++) {
+ huff_num[i] = static_cast<uint8_t>(get_bits(8));
+ count += huff_num[i];
+ }
+
+ if (count > 255) stop_decoding(JPGD_BAD_DHT_COUNTS);
+
+ for (i = 0; i < count; i++)
+ huff_val[i] = static_cast<uint8_t>(get_bits(8));
+
+ i = 1 + 16 + count;
+
+ if (num_left < (uint32_t)i) stop_decoding(JPGD_BAD_DHT_MARKER);
+ num_left -= i;
+
+ if ((index & 0x10) > 0x10) stop_decoding(JPGD_BAD_DHT_INDEX);
+ index = (index & 0x0F) + ((index & 0x10) >> 4) * (JPGD_MAX_HUFF_TABLES >> 1);
+ if (index >= JPGD_MAX_HUFF_TABLES) stop_decoding(JPGD_BAD_DHT_INDEX);
+
+ if (!m_huff_num[index]) m_huff_num[index] = (uint8_t *)alloc(17);
+ if (!m_huff_val[index]) m_huff_val[index] = (uint8_t *)alloc(256);
+
+ m_huff_ac[index] = (index & 0x10) != 0;
+ memcpy(m_huff_num[index], huff_num, 17);
+ memcpy(m_huff_val[index], huff_val, 256);
+ }
+}
+
+
+// Read a quantization table.
+void jpeg_decoder::read_dqt_marker()
+{
+ int n, i, prec;
+ uint32_t temp;
+ uint32_t num_left = get_bits(16);
+ if (num_left < 2) stop_decoding(JPGD_BAD_DQT_MARKER);
+ num_left -= 2;
+
+ while (num_left) {
+ n = get_bits(8);
+ prec = n >> 4;
+ n &= 0x0F;
+
+ if (n >= JPGD_MAX_QUANT_TABLES) stop_decoding(JPGD_BAD_DQT_TABLE);
+
+ if (!m_quant[n]) m_quant[n] = (jpgd_quant_t *)alloc(64 * sizeof(jpgd_quant_t));
+
+ // read quantization entries, in zag order
+ for (i = 0; i < 64; i++) {
+ temp = get_bits(8);
+ if (prec) temp = (temp << 8) + get_bits(8);
+ m_quant[n][i] = static_cast<jpgd_quant_t>(temp);
+ }
+ i = 64 + 1;
+ if (prec) i += 64;
+ if (num_left < (uint32_t)i) stop_decoding(JPGD_BAD_DQT_LENGTH);
+ num_left -= i;
+ }
+}
+
+
+// Read the start of frame (SOF) marker.
+void jpeg_decoder::read_sof_marker()
+{
+ int i;
+ uint32_t num_left = get_bits(16);
+
+ if (get_bits(8) != 8) stop_decoding(JPGD_BAD_PRECISION); /* precision: sorry, only 8-bit precision is supported right now */
+
+ m_image_y_size = get_bits(16);
+ if ((m_image_y_size < 1) || (m_image_y_size > JPGD_MAX_HEIGHT)) stop_decoding(JPGD_BAD_HEIGHT);
+
+ m_image_x_size = get_bits(16);
+ if ((m_image_x_size < 1) || (m_image_x_size > JPGD_MAX_WIDTH)) stop_decoding(JPGD_BAD_WIDTH);
+
+ m_comps_in_frame = get_bits(8);
+ if (m_comps_in_frame > JPGD_MAX_COMPONENTS) stop_decoding(JPGD_TOO_MANY_COMPONENTS);
+
+ if (num_left != (uint32_t)(m_comps_in_frame * 3 + 8)) stop_decoding(JPGD_BAD_SOF_LENGTH);
+
+ for (i = 0; i < m_comps_in_frame; i++) {
+ m_comp_ident[i] = get_bits(8);
+ m_comp_h_samp[i] = get_bits(4);
+ m_comp_v_samp[i] = get_bits(4);
+ m_comp_quant[i] = get_bits(8);
+ }
+}
+
+
+// Used to skip unrecognized markers.
+void jpeg_decoder::skip_variable_marker()
+{
+ uint32_t num_left = get_bits(16);
+ if (num_left < 2) stop_decoding(JPGD_BAD_VARIABLE_MARKER);
+ num_left -= 2;
+
+ while (num_left) {
+ get_bits(8);
+ num_left--;
+ }
+}
+
+
+// Read a define restart interval (DRI) marker.
+void jpeg_decoder::read_dri_marker()
+{
+ if (get_bits(16) != 4) stop_decoding(JPGD_BAD_DRI_LENGTH);
+ m_restart_interval = get_bits(16);
+}
+
+
+// Read a start of scan (SOS) marker.
+void jpeg_decoder::read_sos_marker()
+{
+ int i, ci, c, cc;
+ uint32_t num_left = get_bits(16);
+ int n = get_bits(8);
+
+ m_comps_in_scan = n;
+ num_left -= 3;
+
+ if ( (num_left != (uint32_t)(n * 2 + 3)) || (n < 1) || (n > JPGD_MAX_COMPS_IN_SCAN) ) stop_decoding(JPGD_BAD_SOS_LENGTH);
+
+ for (i = 0; i < n; i++) {
+ cc = get_bits(8);
+ c = get_bits(8);
+ num_left -= 2;
+
+ for (ci = 0; ci < m_comps_in_frame; ci++)
+ if (cc == m_comp_ident[ci]) break;
+
+ if (ci >= m_comps_in_frame) stop_decoding(JPGD_BAD_SOS_COMP_ID);
+
+ m_comp_list[i] = ci;
+ m_comp_dc_tab[ci] = (c >> 4) & 15;
+ m_comp_ac_tab[ci] = (c & 15) + (JPGD_MAX_HUFF_TABLES >> 1);
+ }
+ m_spectral_start = get_bits(8);
+ m_spectral_end = get_bits(8);
+ m_successive_high = get_bits(4);
+ m_successive_low = get_bits(4);
+
+ if (!m_progressive_flag) {
+ m_spectral_start = 0;
+ m_spectral_end = 63;
+ }
+ num_left -= 3;
+
+ while (num_left) { /* read past whatever is num_left */
+ get_bits(8);
+ num_left--;
+ }
+}
+
+
+// Finds the next marker.
+int jpeg_decoder::next_marker()
+{
+ uint32_t c;
+
+ do {
+ do {
+ c = get_bits(8);
+ } while (c != 0xFF);
+
+ do {
+ c = get_bits(8);
+ } while (c == 0xFF);
+ } while (c == 0);
+
+ return c;
+}
+
+
+// Process markers. Returns when an SOFx, SOI, EOI, or SOS marker is
+// encountered.
+int jpeg_decoder::process_markers()
+{
+ int c;
+
+ for ( ; ; ) {
+ c = next_marker();
+ switch (c) {
+ case M_SOF0:
+ case M_SOF1:
+ case M_SOF2:
+ case M_SOF3:
+ case M_SOF5:
+ case M_SOF6:
+ case M_SOF7:
+ // case M_JPG:
+ case M_SOF9:
+ case M_SOF10:
+ case M_SOF11:
+ case M_SOF13:
+ case M_SOF14:
+ case M_SOF15:
+ case M_SOI:
+ case M_EOI:
+ case M_SOS: return c;
+ case M_DHT: {
+ read_dht_marker();
+ break;
+ }
+ // No arithmitic support - dumb patents!
+ case M_DAC: {
+ stop_decoding(JPGD_NO_ARITHMITIC_SUPPORT);
+ break;
+ }
+ case M_DQT: {
+ read_dqt_marker();
+ break;
+ }
+ case M_DRI: {
+ read_dri_marker();
+ break;
+ }
+ //case M_APP0: /* no need to read the JFIF marker */
+ case M_JPG:
+ case M_RST0: /* no parameters */
+ case M_RST1:
+ case M_RST2:
+ case M_RST3:
+ case M_RST4:
+ case M_RST5:
+ case M_RST6:
+ case M_RST7:
+ case M_TEM: {
+ stop_decoding(JPGD_UNEXPECTED_MARKER);
+ break;
+ }
+ default: { /* must be DNL, DHP, EXP, APPn, JPGn, COM, or RESn or APP0 */
+ skip_variable_marker();
+ break;
+ }
+ }
+ }
+}
+
+
+// Finds the start of image (SOI) marker.
+// This code is rather defensive: it only checks the first 512 bytes to avoid
+// false positives.
+void jpeg_decoder::locate_soi_marker()
+{
+ uint32_t lastchar = get_bits(8);
+ uint32_t thischar = get_bits(8);
+
+ /* ok if it's a normal JPEG file without a special header */
+ if ((lastchar == 0xFF) && (thischar == M_SOI)) return;
+
+ uint32_t bytesleft = 4096; //512;
+
+ while (true) {
+ if (--bytesleft == 0) stop_decoding(JPGD_NOT_JPEG);
+
+ lastchar = thischar;
+ thischar = get_bits(8);
+
+ if (lastchar == 0xFF) {
+ if (thischar == M_SOI) break;
+ else if (thischar == M_EOI) stop_decoding(JPGD_NOT_JPEG); // get_bits will keep returning M_EOI if we read past the end
+ }
+ }
+
+ // Check the next character after marker: if it's not 0xFF, it can't be the start of the next marker, so the file is bad.
+ thischar = (m_bit_buf >> 24) & 0xFF;
+ if (thischar != 0xFF) stop_decoding(JPGD_NOT_JPEG);
+}
+
+
+// Find a start of frame (SOF) marker.
+void jpeg_decoder::locate_sof_marker()
+{
+ locate_soi_marker();
+ int c = process_markers();
+
+ switch (c) {
+ case M_SOF2: m_progressive_flag = true;
+ case M_SOF0: /* baseline DCT */
+ case M_SOF1: { /* extended sequential DCT */
+ read_sof_marker();
+ break;
+ }
+ case M_SOF9: { /* Arithmitic coding */
+ stop_decoding(JPGD_NO_ARITHMITIC_SUPPORT);
+ break;
+ }
+ default: {
+ stop_decoding(JPGD_UNSUPPORTED_MARKER);
+ break;
+ }
+ }
+}
+
+
+// Find a start of scan (SOS) marker.
+int jpeg_decoder::locate_sos_marker()
+{
+ int c = process_markers();
+ if (c == M_EOI) return false;
+ else if (c != M_SOS) stop_decoding(JPGD_UNEXPECTED_MARKER);
+ read_sos_marker();
+ return true;
+}
+
+
+// Reset everything to default/uninitialized state.
+void jpeg_decoder::init(jpeg_decoder_stream *pStream)
+{
+ m_pMem_blocks = nullptr;
+ m_error_code = JPGD_SUCCESS;
+ m_ready_flag = false;
+ m_image_x_size = m_image_y_size = 0;
+ m_pStream = pStream;
+ m_progressive_flag = false;
+
+ memset(m_huff_ac, 0, sizeof(m_huff_ac));
+ memset(m_huff_num, 0, sizeof(m_huff_num));
+ memset(m_huff_val, 0, sizeof(m_huff_val));
+ memset(m_quant, 0, sizeof(m_quant));
+
+ m_scan_type = 0;
+ m_comps_in_frame = 0;
+
+ memset(m_comp_h_samp, 0, sizeof(m_comp_h_samp));
+ memset(m_comp_v_samp, 0, sizeof(m_comp_v_samp));
+ memset(m_comp_quant, 0, sizeof(m_comp_quant));
+ memset(m_comp_ident, 0, sizeof(m_comp_ident));
+ memset(m_comp_h_blocks, 0, sizeof(m_comp_h_blocks));
+ memset(m_comp_v_blocks, 0, sizeof(m_comp_v_blocks));
+
+ m_comps_in_scan = 0;
+ memset(m_comp_list, 0, sizeof(m_comp_list));
+ memset(m_comp_dc_tab, 0, sizeof(m_comp_dc_tab));
+ memset(m_comp_ac_tab, 0, sizeof(m_comp_ac_tab));
+
+ m_spectral_start = 0;
+ m_spectral_end = 0;
+ m_successive_low = 0;
+ m_successive_high = 0;
+ m_max_mcu_x_size = 0;
+ m_max_mcu_y_size = 0;
+ m_blocks_per_mcu = 0;
+ m_max_blocks_per_row = 0;
+ m_mcus_per_row = 0;
+ m_mcus_per_col = 0;
+ m_expanded_blocks_per_component = 0;
+ m_expanded_blocks_per_mcu = 0;
+ m_expanded_blocks_per_row = 0;
+ m_freq_domain_chroma_upsample = false;
+
+ memset(m_mcu_org, 0, sizeof(m_mcu_org));
+
+ m_total_lines_left = 0;
+ m_mcu_lines_left = 0;
+ m_real_dest_bytes_per_scan_line = 0;
+ m_dest_bytes_per_scan_line = 0;
+ m_dest_bytes_per_pixel = 0;
+
+ memset(m_pHuff_tabs, 0, sizeof(m_pHuff_tabs));
+
+ memset(m_dc_coeffs, 0, sizeof(m_dc_coeffs));
+ memset(m_ac_coeffs, 0, sizeof(m_ac_coeffs));
+ memset(m_block_y_mcu, 0, sizeof(m_block_y_mcu));
+
+ m_eob_run = 0;
+
+ memset(m_block_y_mcu, 0, sizeof(m_block_y_mcu));
+
+ m_pIn_buf_ofs = m_in_buf;
+ m_in_buf_left = 0;
+ m_eof_flag = false;
+ m_tem_flag = 0;
+
+ memset(m_in_buf_pad_start, 0, sizeof(m_in_buf_pad_start));
+ memset(m_in_buf, 0, sizeof(m_in_buf));
+ memset(m_in_buf_pad_end, 0, sizeof(m_in_buf_pad_end));
+
+ m_restart_interval = 0;
+ m_restarts_left = 0;
+ m_next_restart_num = 0;
+
+ m_max_mcus_per_row = 0;
+ m_max_blocks_per_mcu = 0;
+ m_max_mcus_per_col = 0;
+
+ memset(m_last_dc_val, 0, sizeof(m_last_dc_val));
+ m_pMCU_coefficients = nullptr;
+ m_pSample_buf = nullptr;
+
+ m_total_bytes_read = 0;
+
+ m_pScan_line_0 = nullptr;
+ m_pScan_line_1 = nullptr;
+
+ // Ready the input buffer.
+ prep_in_buffer();
+
+ // Prime the bit buffer.
+ m_bits_left = 16;
+ m_bit_buf = 0;
+
+ get_bits(16);
+ get_bits(16);
+
+ for (int i = 0; i < JPGD_MAX_BLOCKS_PER_MCU; i++) {
+ m_mcu_block_max_zag[i] = 64;
+ }
+}
+
+#define SCALEBITS 16
+#define ONE_HALF ((int) 1 << (SCALEBITS-1))
+#define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5f))
+
+
+// Create a few tables that allow us to quickly convert YCbCr to RGB.
+void jpeg_decoder::create_look_ups()
+{
+ for (int i = 0; i <= 255; i++) {
+ int k = i - 128;
+ m_crr[i] = ( FIX(1.40200f) * k + ONE_HALF) >> SCALEBITS;
+ m_cbb[i] = ( FIX(1.77200f) * k + ONE_HALF) >> SCALEBITS;
+ m_crg[i] = (-FIX(0.71414f)) * k;
+ m_cbg[i] = (-FIX(0.34414f)) * k + ONE_HALF;
+ }
+}
+
+
+// This method throws back into the stream any bytes that where read
+// into the bit buffer during initial marker scanning.
+void jpeg_decoder::fix_in_buffer()
+{
+ // In case any 0xFF's where pulled into the buffer during marker scanning.
+ JPGD_ASSERT((m_bits_left & 7) == 0);
+
+ if (m_bits_left == 16) stuff_char( (uint8_t)(m_bit_buf & 0xFF));
+ if (m_bits_left >= 8) stuff_char( (uint8_t)((m_bit_buf >> 8) & 0xFF));
+
+ stuff_char((uint8_t)((m_bit_buf >> 16) & 0xFF));
+ stuff_char((uint8_t)((m_bit_buf >> 24) & 0xFF));
+
+ m_bits_left = 16;
+ get_bits_no_markers(16);
+ get_bits_no_markers(16);
+}
+
+
+void jpeg_decoder::transform_mcu(int mcu_row)
+{
+ jpgd_block_t* pSrc_ptr = m_pMCU_coefficients;
+ uint8_t* pDst_ptr = m_pSample_buf + mcu_row * m_blocks_per_mcu * 64;
+
+ for (int mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++) {
+ idct(pSrc_ptr, pDst_ptr, m_mcu_block_max_zag[mcu_block]);
+ pSrc_ptr += 64;
+ pDst_ptr += 64;
+ }
+}
+
+
+static const uint8_t s_max_rc[64] =
+{
+ 17, 18, 34, 50, 50, 51, 52, 52, 52, 68, 84, 84, 84, 84, 85, 86, 86, 86, 86, 86,
+ 102, 118, 118, 118, 118, 118, 118, 119, 120, 120, 120, 120, 120, 120, 120, 136,
+ 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136,
+ 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136
+};
+
+
+void jpeg_decoder::transform_mcu_expand(int mcu_row)
+{
+ jpgd_block_t* pSrc_ptr = m_pMCU_coefficients;
+ uint8_t* pDst_ptr = m_pSample_buf + mcu_row * m_expanded_blocks_per_mcu * 64;
+
+ // Y IDCT
+ int mcu_block;
+ for (mcu_block = 0; mcu_block < m_expanded_blocks_per_component; mcu_block++) {
+ idct(pSrc_ptr, pDst_ptr, m_mcu_block_max_zag[mcu_block]);
+ pSrc_ptr += 64;
+ pDst_ptr += 64;
+ }
+
+ // Chroma IDCT, with upsampling
+ jpgd_block_t temp_block[64];
+
+ for (int i = 0; i < 2; i++) {
+ DCT_Upsample::Matrix44 P, Q, R, S;
+ JPGD_ASSERT(m_mcu_block_max_zag[mcu_block] >= 1);
+ JPGD_ASSERT(m_mcu_block_max_zag[mcu_block] <= 64);
+
+ int max_zag = m_mcu_block_max_zag[mcu_block++] - 1;
+ if (max_zag <= 0) max_zag = 0; // should never happen, only here to shut up static analysis
+
+ switch (s_max_rc[max_zag]) {
+ case 1*16+1:
+ DCT_Upsample::P_Q<1, 1>::calc(P, Q, pSrc_ptr);
+ DCT_Upsample::R_S<1, 1>::calc(R, S, pSrc_ptr);
+ break;
+ case 1*16+2:
+ DCT_Upsample::P_Q<1, 2>::calc(P, Q, pSrc_ptr);
+ DCT_Upsample::R_S<1, 2>::calc(R, S, pSrc_ptr);
+ break;
+ case 2*16+2:
+ DCT_Upsample::P_Q<2, 2>::calc(P, Q, pSrc_ptr);
+ DCT_Upsample::R_S<2, 2>::calc(R, S, pSrc_ptr);
+ break;
+ case 3*16+2:
+ DCT_Upsample::P_Q<3, 2>::calc(P, Q, pSrc_ptr);
+ DCT_Upsample::R_S<3, 2>::calc(R, S, pSrc_ptr);
+ break;
+ case 3*16+3:
+ DCT_Upsample::P_Q<3, 3>::calc(P, Q, pSrc_ptr);
+ DCT_Upsample::R_S<3, 3>::calc(R, S, pSrc_ptr);
+ break;
+ case 3*16+4:
+ DCT_Upsample::P_Q<3, 4>::calc(P, Q, pSrc_ptr);
+ DCT_Upsample::R_S<3, 4>::calc(R, S, pSrc_ptr);
+ break;
+ case 4*16+4:
+ DCT_Upsample::P_Q<4, 4>::calc(P, Q, pSrc_ptr);
+ DCT_Upsample::R_S<4, 4>::calc(R, S, pSrc_ptr);
+ break;
+ case 5*16+4:
+ DCT_Upsample::P_Q<5, 4>::calc(P, Q, pSrc_ptr);
+ DCT_Upsample::R_S<5, 4>::calc(R, S, pSrc_ptr);
+ break;
+ case 5*16+5:
+ DCT_Upsample::P_Q<5, 5>::calc(P, Q, pSrc_ptr);
+ DCT_Upsample::R_S<5, 5>::calc(R, S, pSrc_ptr);
+ break;
+ case 5*16+6:
+ DCT_Upsample::P_Q<5, 6>::calc(P, Q, pSrc_ptr);
+ DCT_Upsample::R_S<5, 6>::calc(R, S, pSrc_ptr);
+ break;
+ case 6*16+6:
+ DCT_Upsample::P_Q<6, 6>::calc(P, Q, pSrc_ptr);
+ DCT_Upsample::R_S<6, 6>::calc(R, S, pSrc_ptr);
+ break;
+ case 7*16+6:
+ DCT_Upsample::P_Q<7, 6>::calc(P, Q, pSrc_ptr);
+ DCT_Upsample::R_S<7, 6>::calc(R, S, pSrc_ptr);
+ break;
+ case 7*16+7:
+ DCT_Upsample::P_Q<7, 7>::calc(P, Q, pSrc_ptr);
+ DCT_Upsample::R_S<7, 7>::calc(R, S, pSrc_ptr);
+ break;
+ case 7*16+8:
+ DCT_Upsample::P_Q<7, 8>::calc(P, Q, pSrc_ptr);
+ DCT_Upsample::R_S<7, 8>::calc(R, S, pSrc_ptr);
+ break;
+ case 8*16+8:
+ DCT_Upsample::P_Q<8, 8>::calc(P, Q, pSrc_ptr);
+ DCT_Upsample::R_S<8, 8>::calc(R, S, pSrc_ptr);
+ break;
+ default:
+ JPGD_ASSERT(false);
+ }
+ DCT_Upsample::Matrix44 a(P + Q); P -= Q;
+ DCT_Upsample::Matrix44& b = P;
+ DCT_Upsample::Matrix44 c(R + S); R -= S;
+ DCT_Upsample::Matrix44& d = R;
+
+ DCT_Upsample::Matrix44::add_and_store(temp_block, a, c);
+ idct_4x4(temp_block, pDst_ptr);
+ pDst_ptr += 64;
+
+ DCT_Upsample::Matrix44::sub_and_store(temp_block, a, c);
+ idct_4x4(temp_block, pDst_ptr);
+ pDst_ptr += 64;
+
+ DCT_Upsample::Matrix44::add_and_store(temp_block, b, d);
+ idct_4x4(temp_block, pDst_ptr);
+ pDst_ptr += 64;
+
+ DCT_Upsample::Matrix44::sub_and_store(temp_block, b, d);
+ idct_4x4(temp_block, pDst_ptr);
+ pDst_ptr += 64;
+ pSrc_ptr += 64;
+ }
+}
+
+
+// Loads and dequantizes the next row of (already decoded) coefficients.
+// Progressive images only.
+void jpeg_decoder::load_next_row()
+{
+ int i;
+ jpgd_block_t *p;
+ jpgd_quant_t *q;
+ int mcu_row, mcu_block;
+ int component_num, component_id;
+ int block_x_mcu[JPGD_MAX_COMPONENTS];
+
+ memset(block_x_mcu, 0, JPGD_MAX_COMPONENTS * sizeof(int));
+
+ for (mcu_row = 0; mcu_row < m_mcus_per_row; mcu_row++) {
+ int block_x_mcu_ofs = 0, block_y_mcu_ofs = 0;
+
+ for (mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++) {
+ component_id = m_mcu_org[mcu_block];
+ q = m_quant[m_comp_quant[component_id]];
+ p = m_pMCU_coefficients + 64 * mcu_block;
+
+ jpgd_block_t* pAC = coeff_buf_getp(m_ac_coeffs[component_id], block_x_mcu[component_id] + block_x_mcu_ofs, m_block_y_mcu[component_id] + block_y_mcu_ofs);
+ jpgd_block_t* pDC = coeff_buf_getp(m_dc_coeffs[component_id], block_x_mcu[component_id] + block_x_mcu_ofs, m_block_y_mcu[component_id] + block_y_mcu_ofs);
+ p[0] = pDC[0];
+ memcpy(&p[1], &pAC[1], 63 * sizeof(jpgd_block_t));
+
+ for (i = 63; i > 0; i--) {
+ if (p[g_ZAG[i]]) break;
+ }
+
+ m_mcu_block_max_zag[mcu_block] = i + 1;
+
+ for ( ; i >= 0; i--) {
+ if (p[g_ZAG[i]]) {
+ p[g_ZAG[i]] = static_cast<jpgd_block_t>(p[g_ZAG[i]] * q[i]);
+ }
+ }
+
+ if (m_comps_in_scan == 1) block_x_mcu[component_id]++;
+ else {
+ if (++block_x_mcu_ofs == m_comp_h_samp[component_id]) block_x_mcu_ofs = 0;
+ if (++block_y_mcu_ofs == m_comp_v_samp[component_id]) {
+ block_y_mcu_ofs = 0;
+ block_x_mcu[component_id] += m_comp_h_samp[component_id];
+ }
+ }
+ }
+ if (m_freq_domain_chroma_upsample) transform_mcu_expand(mcu_row);
+ else transform_mcu(mcu_row);
+ }
+ if (m_comps_in_scan == 1) m_block_y_mcu[m_comp_list[0]]++;
+ else {
+ for (component_num = 0; component_num < m_comps_in_scan; component_num++) {
+ component_id = m_comp_list[component_num];
+ m_block_y_mcu[component_id] += m_comp_v_samp[component_id];
+ }
+ }
+}
+
+
+// Restart interval processing.
+void jpeg_decoder::process_restart()
+{
+ int i;
+ int c = 0;
+
+ // Align to a byte boundry
+ // FIXME: Is this really necessary? get_bits_no_markers() never reads in markers!
+ //get_bits_no_markers(m_bits_left & 7);
+
+ // Let's scan a little bit to find the marker, but not _too_ far.
+ // 1536 is a "fudge factor" that determines how much to scan.
+ for (i = 1536; i > 0; i--) {
+ if (get_char() == 0xFF) break;
+ }
+ if (i == 0) stop_decoding(JPGD_BAD_RESTART_MARKER);
+
+ for ( ; i > 0; i--) {
+ if ((c = get_char()) != 0xFF) break;
+ }
+ if (i == 0) stop_decoding(JPGD_BAD_RESTART_MARKER);
+
+ // Is it the expected marker? If not, something bad happened.
+ if (c != (m_next_restart_num + M_RST0)) stop_decoding(JPGD_BAD_RESTART_MARKER);
+
+ // Reset each component's DC prediction values.
+ memset(&m_last_dc_val, 0, m_comps_in_frame * sizeof(uint32_t));
+
+ m_eob_run = 0;
+ m_restarts_left = m_restart_interval;
+ m_next_restart_num = (m_next_restart_num + 1) & 7;
+
+ // Get the bit buffer going again...
+ m_bits_left = 16;
+ get_bits_no_markers(16);
+ get_bits_no_markers(16);
+}
+
+
+static inline int dequantize_ac(int c, int q)
+{
+ c *= q;
+ return c;
+}
+
+// Decodes and dequantizes the next row of coefficients.
+void jpeg_decoder::decode_next_row()
+{
+ for (int mcu_row = 0; mcu_row < m_mcus_per_row; mcu_row++) {
+ if ((m_restart_interval) && (m_restarts_left == 0)) process_restart();
+
+ jpgd_block_t* p = m_pMCU_coefficients;
+
+ for (int mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++, p += 64) {
+ int component_id = m_mcu_org[mcu_block];
+ jpgd_quant_t* q = m_quant[m_comp_quant[component_id]];
+
+ int r, s;
+ s = huff_decode(m_pHuff_tabs[m_comp_dc_tab[component_id]], r);
+ s = JPGD_HUFF_EXTEND(r, s);
+
+ m_last_dc_val[component_id] = (s += m_last_dc_val[component_id]);
+
+ p[0] = static_cast<jpgd_block_t>(s * q[0]);
+
+ int prev_num_set = m_mcu_block_max_zag[mcu_block];
+ huff_tables *pH = m_pHuff_tabs[m_comp_ac_tab[component_id]];
+ int k;
+ for (k = 1; k < 64; k++) {
+ int extra_bits;
+ s = huff_decode(pH, extra_bits);
+ r = s >> 4;
+ s &= 15;
+
+ if (s) {
+ if (r) {
+ if ((k + r) > 63) stop_decoding(JPGD_DECODE_ERROR);
+ if (k < prev_num_set) {
+ int n = JPGD_MIN(r, prev_num_set - k);
+ int kt = k;
+ while (n--) p[g_ZAG[kt++]] = 0;
+ }
+ k += r;
+ }
+ s = JPGD_HUFF_EXTEND(extra_bits, s);
+ JPGD_ASSERT(k < 64);
+ p[g_ZAG[k]] = static_cast<jpgd_block_t>(dequantize_ac(s, q[k])); //s * q[k];
+ } else {
+ if (r == 15) {
+ if ((k + 16) > 64) stop_decoding(JPGD_DECODE_ERROR);
+ if (k < prev_num_set) {
+ int n = JPGD_MIN(16, prev_num_set - k);
+ int kt = k;
+ while (n--) {
+ JPGD_ASSERT(kt <= 63);
+ p[g_ZAG[kt++]] = 0;
+ }
+ }
+ k += 16 - 1; // - 1 because the loop counter is k
+ JPGD_ASSERT(p[g_ZAG[k]] == 0);
+ } else break;
+ }
+ }
+
+ if (k < prev_num_set) {
+ int kt = k;
+ while (kt < prev_num_set) p[g_ZAG[kt++]] = 0;
+ }
+
+ m_mcu_block_max_zag[mcu_block] = k;
+ }
+ if (m_freq_domain_chroma_upsample) transform_mcu_expand(mcu_row);
+ else transform_mcu(mcu_row);
+ m_restarts_left--;
+ }
+}
+
+
+// YCbCr H1V1 (1x1:1:1, 3 m_blocks per MCU) to RGB
+void jpeg_decoder::H1V1Convert()
+{
+ int row = m_max_mcu_y_size - m_mcu_lines_left;
+ uint8_t *d = m_pScan_line_0;
+ uint8_t *s = m_pSample_buf + row * 8;
+
+ for (int i = m_max_mcus_per_row; i > 0; i--) {
+ for (int j = 0; j < 8; j++) {
+ int y = s[j];
+ int cb = s[64+j];
+ int cr = s[128+j];
+
+ d[0] = clamp(y + m_crr[cr]);
+ d[1] = clamp(y + ((m_crg[cr] + m_cbg[cb]) >> 16));
+ d[2] = clamp(y + m_cbb[cb]);
+ d[3] = 255;
+ d += 4;
+ }
+ s += 64*3;
+ }
+}
+
+
+// YCbCr H2V1 (2x1:1:1, 4 m_blocks per MCU) to RGB
+void jpeg_decoder::H2V1Convert()
+{
+ int row = m_max_mcu_y_size - m_mcu_lines_left;
+ uint8_t *d0 = m_pScan_line_0;
+ uint8_t *y = m_pSample_buf + row * 8;
+ uint8_t *c = m_pSample_buf + 2*64 + row * 8;
+
+ for (int i = m_max_mcus_per_row; i > 0; i--) {
+ for (int l = 0; l < 2; l++) {
+ for (int j = 0; j < 4; j++) {
+ int cb = c[0];
+ int cr = c[64];
+
+ int rc = m_crr[cr];
+ int gc = ((m_crg[cr] + m_cbg[cb]) >> 16);
+ int bc = m_cbb[cb];
+
+ int yy = y[j<<1];
+ d0[0] = clamp(yy+rc);
+ d0[1] = clamp(yy+gc);
+ d0[2] = clamp(yy+bc);
+ d0[3] = 255;
+
+ yy = y[(j<<1)+1];
+ d0[4] = clamp(yy+rc);
+ d0[5] = clamp(yy+gc);
+ d0[6] = clamp(yy+bc);
+ d0[7] = 255;
+ d0 += 8;
+ c++;
+ }
+ y += 64;
+ }
+ y += 64*4 - 64*2;
+ c += 64*4 - 8;
+ }
+}
+
+
+// YCbCr H2V1 (1x2:1:1, 4 m_blocks per MCU) to RGB
+void jpeg_decoder::H1V2Convert()
+{
+ int row = m_max_mcu_y_size - m_mcu_lines_left;
+ uint8_t *d0 = m_pScan_line_0;
+ uint8_t *d1 = m_pScan_line_1;
+ uint8_t *y;
+ uint8_t *c;
+
+ if (row < 8) y = m_pSample_buf + row * 8;
+ else y = m_pSample_buf + 64*1 + (row & 7) * 8;
+
+ c = m_pSample_buf + 64*2 + (row >> 1) * 8;
+
+ for (int i = m_max_mcus_per_row; i > 0; i--) {
+ for (int j = 0; j < 8; j++) {
+ int cb = c[0+j];
+ int cr = c[64+j];
+
+ int rc = m_crr[cr];
+ int gc = ((m_crg[cr] + m_cbg[cb]) >> 16);
+ int bc = m_cbb[cb];
+
+ int yy = y[j];
+ d0[0] = clamp(yy+rc);
+ d0[1] = clamp(yy+gc);
+ d0[2] = clamp(yy+bc);
+ d0[3] = 255;
+
+ yy = y[8+j];
+ d1[0] = clamp(yy+rc);
+ d1[1] = clamp(yy+gc);
+ d1[2] = clamp(yy+bc);
+ d1[3] = 255;
+
+ d0 += 4;
+ d1 += 4;
+ }
+ y += 64*4;
+ c += 64*4;
+ }
+}
+
+
+// YCbCr H2V2 (2x2:1:1, 6 m_blocks per MCU) to RGB
+void jpeg_decoder::H2V2Convert()
+{
+ int row = m_max_mcu_y_size - m_mcu_lines_left;
+ uint8_t *d0 = m_pScan_line_0;
+ uint8_t *d1 = m_pScan_line_1;
+ uint8_t *y;
+ uint8_t *c;
+
+ if (row < 8) y = m_pSample_buf + row * 8;
+ else y = m_pSample_buf + 64*2 + (row & 7) * 8;
+
+ c = m_pSample_buf + 64*4 + (row >> 1) * 8;
+
+ for (int i = m_max_mcus_per_row; i > 0; i--) {
+ for (int l = 0; l < 2; l++) {
+ for (int j = 0; j < 8; j += 2) {
+ int cb = c[0];
+ int cr = c[64];
+
+ int rc = m_crr[cr];
+ int gc = ((m_crg[cr] + m_cbg[cb]) >> 16);
+ int bc = m_cbb[cb];
+
+ int yy = y[j];
+ d0[0] = clamp(yy+rc);
+ d0[1] = clamp(yy+gc);
+ d0[2] = clamp(yy+bc);
+ d0[3] = 255;
+
+ yy = y[j+1];
+ d0[4] = clamp(yy+rc);
+ d0[5] = clamp(yy+gc);
+ d0[6] = clamp(yy+bc);
+ d0[7] = 255;
+
+ yy = y[j+8];
+ d1[0] = clamp(yy+rc);
+ d1[1] = clamp(yy+gc);
+ d1[2] = clamp(yy+bc);
+ d1[3] = 255;
+
+ yy = y[j+8+1];
+ d1[4] = clamp(yy+rc);
+ d1[5] = clamp(yy+gc);
+ d1[6] = clamp(yy+bc);
+ d1[7] = 255;
+
+ d0 += 8;
+ d1 += 8;
+
+ c++;
+ }
+ y += 64;
+ }
+ y += 64*6 - 64*2;
+ c += 64*6 - 8;
+ }
+}
+
+
+// Y (1 block per MCU) to 8-bit grayscale
+void jpeg_decoder::gray_convert()
+{
+ int row = m_max_mcu_y_size - m_mcu_lines_left;
+ uint8_t *d = m_pScan_line_0;
+ uint8_t *s = m_pSample_buf + row * 8;
+
+ for (int i = m_max_mcus_per_row; i > 0; i--) {
+ *(uint32_t *)d = *(uint32_t *)s;
+ *(uint32_t *)(&d[4]) = *(uint32_t *)(&s[4]);
+ s += 64;
+ d += 8;
+ }
+}
+
+
+void jpeg_decoder::expanded_convert()
+{
+ int row = m_max_mcu_y_size - m_mcu_lines_left;
+ uint8_t* Py = m_pSample_buf + (row / 8) * 64 * m_comp_h_samp[0] + (row & 7) * 8;
+ uint8_t* d = m_pScan_line_0;
+
+ for (int i = m_max_mcus_per_row; i > 0; i--) {
+ for (int k = 0; k < m_max_mcu_x_size; k += 8) {
+ const int Y_ofs = k * 8;
+ const int Cb_ofs = Y_ofs + 64 * m_expanded_blocks_per_component;
+ const int Cr_ofs = Y_ofs + 64 * m_expanded_blocks_per_component * 2;
+ for (int j = 0; j < 8; j++) {
+ int y = Py[Y_ofs + j];
+ int cb = Py[Cb_ofs + j];
+ int cr = Py[Cr_ofs + j];
+
+ d[0] = clamp(y + m_crr[cr]);
+ d[1] = clamp(y + ((m_crg[cr] + m_cbg[cb]) >> 16));
+ d[2] = clamp(y + m_cbb[cb]);
+ d[3] = 255;
+
+ d += 4;
+ }
+ }
+ Py += 64 * m_expanded_blocks_per_mcu;
+ }
+}
+
+
+// Find end of image (EOI) marker, so we can return to the user the exact size of the input stream.
+void jpeg_decoder::find_eoi()
+{
+ if (!m_progressive_flag) {
+ // Attempt to read the EOI marker.
+ //get_bits_no_markers(m_bits_left & 7);
+
+ // Prime the bit buffer
+ m_bits_left = 16;
+ get_bits(16);
+ get_bits(16);
+
+ // The next marker _should_ be EOI
+ process_markers();
+ }
+ m_total_bytes_read -= m_in_buf_left;
+}
+
+
+int jpeg_decoder::decode(const void** pScan_line, uint32_t* pScan_line_len)
+{
+ if ((m_error_code) || (!m_ready_flag)) return JPGD_FAILED;
+ if (m_total_lines_left == 0) return JPGD_DONE;
+ if (m_mcu_lines_left == 0) {
+ if (setjmp(m_jmp_state)) return JPGD_FAILED;
+ if (m_progressive_flag) load_next_row();
+ else decode_next_row();
+ // Find the EOI marker if that was the last row.
+ if (m_total_lines_left <= m_max_mcu_y_size) find_eoi();
+ m_mcu_lines_left = m_max_mcu_y_size;
+ }
+
+ if (m_freq_domain_chroma_upsample) {
+ expanded_convert();
+ *pScan_line = m_pScan_line_0;
+ } else {
+ switch (m_scan_type) {
+ case JPGD_YH2V2: {
+ if ((m_mcu_lines_left & 1) == 0) {
+ H2V2Convert();
+ *pScan_line = m_pScan_line_0;
+ }
+ else *pScan_line = m_pScan_line_1;
+ break;
+ }
+ case JPGD_YH2V1: {
+ H2V1Convert();
+ *pScan_line = m_pScan_line_0;
+ break;
+ }
+ case JPGD_YH1V2: {
+ if ((m_mcu_lines_left & 1) == 0) {
+ H1V2Convert();
+ *pScan_line = m_pScan_line_0;
+ } else *pScan_line = m_pScan_line_1;
+ break;
+ }
+ case JPGD_YH1V1: {
+ H1V1Convert();
+ *pScan_line = m_pScan_line_0;
+ break;
+ }
+ case JPGD_GRAYSCALE: {
+ gray_convert();
+ *pScan_line = m_pScan_line_0;
+ break;
+ }
+ }
+ }
+
+ *pScan_line_len = m_real_dest_bytes_per_scan_line;
+ m_mcu_lines_left--;
+ m_total_lines_left--;
+
+ return JPGD_SUCCESS;
+}
+
+
+// Creates the tables needed for efficient Huffman decoding.
+void jpeg_decoder::make_huff_table(int index, huff_tables *pH)
+{
+ int p, i, l, si;
+ uint8_t huffsize[257];
+ uint32_t huffcode[257];
+ uint32_t code;
+ uint32_t subtree;
+ int code_size;
+ int lastp;
+ int nextfreeentry;
+ int currententry;
+
+ pH->ac_table = m_huff_ac[index] != 0;
+ p = 0;
+
+ for (l = 1; l <= 16; l++) {
+ for (i = 1; i <= m_huff_num[index][l]; i++) {
+ huffsize[p++] = static_cast<uint8_t>(l);
+ }
+ }
+
+ huffsize[p] = 0;
+ lastp = p;
+ code = 0;
+ si = huffsize[0];
+ p = 0;
+
+ while (huffsize[p]) {
+ while (huffsize[p] == si) {
+ huffcode[p++] = code;
+ code++;
+ }
+ code <<= 1;
+ si++;
+ }
+
+ memset(pH->look_up, 0, sizeof(pH->look_up));
+ memset(pH->look_up2, 0, sizeof(pH->look_up2));
+ memset(pH->tree, 0, sizeof(pH->tree));
+ memset(pH->code_size, 0, sizeof(pH->code_size));
+
+ nextfreeentry = -1;
+ p = 0;
+
+ while (p < lastp) {
+ i = m_huff_val[index][p];
+ code = huffcode[p];
+ code_size = huffsize[p];
+ pH->code_size[i] = static_cast<uint8_t>(code_size);
+
+ if (code_size <= 8) {
+ code <<= (8 - code_size);
+ for (l = 1 << (8 - code_size); l > 0; l--) {
+ JPGD_ASSERT(i < 256);
+ pH->look_up[code] = i;
+ bool has_extrabits = false;
+ int extra_bits = 0;
+ int num_extra_bits = i & 15;
+ int bits_to_fetch = code_size;
+
+ if (num_extra_bits) {
+ int total_codesize = code_size + num_extra_bits;
+ if (total_codesize <= 8) {
+ has_extrabits = true;
+ extra_bits = ((1 << num_extra_bits) - 1) & (code >> (8 - total_codesize));
+ JPGD_ASSERT(extra_bits <= 0x7FFF);
+ bits_to_fetch += num_extra_bits;
+ }
+ }
+ if (!has_extrabits) pH->look_up2[code] = i | (bits_to_fetch << 8);
+ else pH->look_up2[code] = i | 0x8000 | (extra_bits << 16) | (bits_to_fetch << 8);
+ code++;
+ }
+ } else {
+ subtree = (code >> (code_size - 8)) & 0xFF;
+ currententry = pH->look_up[subtree];
+
+ if (currententry == 0) {
+ pH->look_up[subtree] = currententry = nextfreeentry;
+ pH->look_up2[subtree] = currententry = nextfreeentry;
+ nextfreeentry -= 2;
+ }
+
+ code <<= (16 - (code_size - 8));
+
+ for (l = code_size; l > 9; l--) {
+ if ((code & 0x8000) == 0) currententry--;
+ if (pH->tree[-currententry - 1] == 0) {
+ pH->tree[-currententry - 1] = nextfreeentry;
+ currententry = nextfreeentry;
+ nextfreeentry -= 2;
+ } else currententry = pH->tree[-currententry - 1];
+ code <<= 1;
+ }
+ if ((code & 0x8000) == 0) currententry--;
+ pH->tree[-currententry - 1] = i;
+ }
+ p++;
+ }
+}
+
+
+// Verifies the quantization tables needed for this scan are available.
+void jpeg_decoder::check_quant_tables()
+{
+ for (int i = 0; i < m_comps_in_scan; i++) {
+ if (m_quant[m_comp_quant[m_comp_list[i]]] == nullptr) stop_decoding(JPGD_UNDEFINED_QUANT_TABLE);
+ }
+}
+
+
+// Verifies that all the Huffman tables needed for this scan are available.
+void jpeg_decoder::check_huff_tables()
+{
+ for (int i = 0; i < m_comps_in_scan; i++) {
+ if ((m_spectral_start == 0) && (m_huff_num[m_comp_dc_tab[m_comp_list[i]]] == nullptr)) stop_decoding(JPGD_UNDEFINED_HUFF_TABLE);
+ if ((m_spectral_end > 0) && (m_huff_num[m_comp_ac_tab[m_comp_list[i]]] == nullptr)) stop_decoding(JPGD_UNDEFINED_HUFF_TABLE);
+ }
+
+ for (int i = 0; i < JPGD_MAX_HUFF_TABLES; i++) {
+ if (m_huff_num[i]) {
+ if (!m_pHuff_tabs[i]) m_pHuff_tabs[i] = (huff_tables *)alloc(sizeof(huff_tables));
+ make_huff_table(i, m_pHuff_tabs[i]);
+ }
+ }
+}
+
+
+// Determines the component order inside each MCU.
+// Also calcs how many MCU's are on each row, etc.
+void jpeg_decoder::calc_mcu_block_order()
+{
+ int component_num, component_id;
+ int max_h_samp = 0, max_v_samp = 0;
+
+ for (component_id = 0; component_id < m_comps_in_frame; component_id++) {
+ if (m_comp_h_samp[component_id] > max_h_samp) {
+ max_h_samp = m_comp_h_samp[component_id];
+ }
+ if (m_comp_v_samp[component_id] > max_v_samp) {
+ max_v_samp = m_comp_v_samp[component_id];
+ }
+ }
+
+ for (component_id = 0; component_id < m_comps_in_frame; component_id++) {
+ m_comp_h_blocks[component_id] = ((((m_image_x_size * m_comp_h_samp[component_id]) + (max_h_samp - 1)) / max_h_samp) + 7) / 8;
+ m_comp_v_blocks[component_id] = ((((m_image_y_size * m_comp_v_samp[component_id]) + (max_v_samp - 1)) / max_v_samp) + 7) / 8;
+ }
+
+ if (m_comps_in_scan == 1) {
+ m_mcus_per_row = m_comp_h_blocks[m_comp_list[0]];
+ m_mcus_per_col = m_comp_v_blocks[m_comp_list[0]];
+ } else {
+ m_mcus_per_row = (((m_image_x_size + 7) / 8) + (max_h_samp - 1)) / max_h_samp;
+ m_mcus_per_col = (((m_image_y_size + 7) / 8) + (max_v_samp - 1)) / max_v_samp;
+ }
+
+ if (m_comps_in_scan == 1) {
+ m_mcu_org[0] = m_comp_list[0];
+ m_blocks_per_mcu = 1;
+ } else {
+ m_blocks_per_mcu = 0;
+
+ for (component_num = 0; component_num < m_comps_in_scan; component_num++) {
+ int num_blocks;
+ component_id = m_comp_list[component_num];
+ num_blocks = m_comp_h_samp[component_id] * m_comp_v_samp[component_id];
+ while (num_blocks--) m_mcu_org[m_blocks_per_mcu++] = component_id;
+ }
+ }
+}
+
+
+// Starts a new scan.
+int jpeg_decoder::init_scan()
+{
+ if (!locate_sos_marker()) return false;
+
+ calc_mcu_block_order();
+ check_huff_tables();
+ check_quant_tables();
+
+ memset(m_last_dc_val, 0, m_comps_in_frame * sizeof(uint32_t));
+
+ m_eob_run = 0;
+
+ if (m_restart_interval) {
+ m_restarts_left = m_restart_interval;
+ m_next_restart_num = 0;
+ }
+ fix_in_buffer();
+ return true;
+}
+
+
+// Starts a frame. Determines if the number of components or sampling factors
+// are supported.
+void jpeg_decoder::init_frame()
+{
+ int i;
+
+ if (m_comps_in_frame == 1) {
+ if ((m_comp_h_samp[0] != 1) || (m_comp_v_samp[0] != 1)) stop_decoding(JPGD_UNSUPPORTED_SAMP_FACTORS);
+ m_scan_type = JPGD_GRAYSCALE;
+ m_max_blocks_per_mcu = 1;
+ m_max_mcu_x_size = 8;
+ m_max_mcu_y_size = 8;
+ } else if (m_comps_in_frame == 3) {
+ if (((m_comp_h_samp[1] != 1) || (m_comp_v_samp[1] != 1)) || ((m_comp_h_samp[2] != 1) || (m_comp_v_samp[2] != 1)))
+ stop_decoding(JPGD_UNSUPPORTED_SAMP_FACTORS);
+
+ if ((m_comp_h_samp[0] == 1) && (m_comp_v_samp[0] == 1)) {
+ m_scan_type = JPGD_YH1V1;
+ m_max_blocks_per_mcu = 3;
+ m_max_mcu_x_size = 8;
+ m_max_mcu_y_size = 8;
+ } else if ((m_comp_h_samp[0] == 2) && (m_comp_v_samp[0] == 1)) {
+ m_scan_type = JPGD_YH2V1;
+ m_max_blocks_per_mcu = 4;
+ m_max_mcu_x_size = 16;
+ m_max_mcu_y_size = 8;
+ } else if ((m_comp_h_samp[0] == 1) && (m_comp_v_samp[0] == 2)) {
+ m_scan_type = JPGD_YH1V2;
+ m_max_blocks_per_mcu = 4;
+ m_max_mcu_x_size = 8;
+ m_max_mcu_y_size = 16;
+ } else if ((m_comp_h_samp[0] == 2) && (m_comp_v_samp[0] == 2)) {
+ m_scan_type = JPGD_YH2V2;
+ m_max_blocks_per_mcu = 6;
+ m_max_mcu_x_size = 16;
+ m_max_mcu_y_size = 16;
+ } else stop_decoding(JPGD_UNSUPPORTED_SAMP_FACTORS);
+ } else stop_decoding(JPGD_UNSUPPORTED_COLORSPACE);
+
+ m_max_mcus_per_row = (m_image_x_size + (m_max_mcu_x_size - 1)) / m_max_mcu_x_size;
+ m_max_mcus_per_col = (m_image_y_size + (m_max_mcu_y_size - 1)) / m_max_mcu_y_size;
+
+ // These values are for the *destination* pixels: after conversion.
+ if (m_scan_type == JPGD_GRAYSCALE) m_dest_bytes_per_pixel = 1;
+ else m_dest_bytes_per_pixel = 4;
+
+ m_dest_bytes_per_scan_line = ((m_image_x_size + 15) & 0xFFF0) * m_dest_bytes_per_pixel;
+ m_real_dest_bytes_per_scan_line = (m_image_x_size * m_dest_bytes_per_pixel);
+
+ // Initialize two scan line buffers.
+ m_pScan_line_0 = (uint8_t *)alloc(m_dest_bytes_per_scan_line, true);
+ if ((m_scan_type == JPGD_YH1V2) || (m_scan_type == JPGD_YH2V2)) {
+ m_pScan_line_1 = (uint8_t *)alloc(m_dest_bytes_per_scan_line, true);
+ }
+
+ m_max_blocks_per_row = m_max_mcus_per_row * m_max_blocks_per_mcu;
+
+ // Should never happen
+ if (m_max_blocks_per_row > JPGD_MAX_BLOCKS_PER_ROW) stop_decoding(JPGD_ASSERTION_ERROR);
+
+ // Allocate the coefficient buffer, enough for one MCU
+ m_pMCU_coefficients = (jpgd_block_t*)alloc(m_max_blocks_per_mcu * 64 * sizeof(jpgd_block_t));
+
+ for (i = 0; i < m_max_blocks_per_mcu; i++) {
+ m_mcu_block_max_zag[i] = 64;
+ }
+
+ m_expanded_blocks_per_component = m_comp_h_samp[0] * m_comp_v_samp[0];
+ m_expanded_blocks_per_mcu = m_expanded_blocks_per_component * m_comps_in_frame;
+ m_expanded_blocks_per_row = m_max_mcus_per_row * m_expanded_blocks_per_mcu;
+ // Freq. domain chroma upsampling is only supported for H2V2 subsampling factor (the most common one I've seen).
+ m_freq_domain_chroma_upsample = false;
+#if JPGD_SUPPORT_FREQ_DOMAIN_UPSAMPLING
+ m_freq_domain_chroma_upsample = (m_expanded_blocks_per_mcu == 4*3);
+#endif
+
+ if (m_freq_domain_chroma_upsample)
+ m_pSample_buf = (uint8_t *)alloc(m_expanded_blocks_per_row * 64);
+ else
+ m_pSample_buf = (uint8_t *)alloc(m_max_blocks_per_row * 64);
+
+ m_total_lines_left = m_image_y_size;
+ m_mcu_lines_left = 0;
+ create_look_ups();
+}
+
+
+// The coeff_buf series of methods originally stored the coefficients
+// into a "virtual" file which was located in EMS, XMS, or a disk file. A cache
+// was used to make this process more efficient. Now, we can store the entire
+// thing in RAM.
+jpeg_decoder::coeff_buf* jpeg_decoder::coeff_buf_open(int block_num_x, int block_num_y, int block_len_x, int block_len_y)
+{
+ coeff_buf* cb = (coeff_buf*)alloc(sizeof(coeff_buf));
+ cb->block_num_x = block_num_x;
+ cb->block_num_y = block_num_y;
+ cb->block_len_x = block_len_x;
+ cb->block_len_y = block_len_y;
+ cb->block_size = (block_len_x * block_len_y) * sizeof(jpgd_block_t);
+ cb->pData = (uint8_t *)alloc(cb->block_size * block_num_x * block_num_y, true);
+ return cb;
+}
+
+
+inline jpgd_block_t *jpeg_decoder::coeff_buf_getp(coeff_buf *cb, int block_x, int block_y)
+{
+ JPGD_ASSERT((block_x < cb->block_num_x) && (block_y < cb->block_num_y));
+ return (jpgd_block_t *)(cb->pData + block_x * cb->block_size + block_y * (cb->block_size * cb->block_num_x));
+}
+
+
+// The following methods decode the various types of m_blocks encountered
+// in progressively encoded images.
+void jpeg_decoder::decode_block_dc_first(jpeg_decoder *pD, int component_id, int block_x, int block_y)
+{
+ int s, r;
+ jpgd_block_t *p = pD->coeff_buf_getp(pD->m_dc_coeffs[component_id], block_x, block_y);
+
+ if ((s = pD->huff_decode(pD->m_pHuff_tabs[pD->m_comp_dc_tab[component_id]])) != 0) {
+ r = pD->get_bits_no_markers(s);
+ s = JPGD_HUFF_EXTEND(r, s);
+ }
+ pD->m_last_dc_val[component_id] = (s += pD->m_last_dc_val[component_id]);
+ p[0] = static_cast<jpgd_block_t>(static_cast<unsigned int>(s) << pD->m_successive_low);
+}
+
+
+void jpeg_decoder::decode_block_dc_refine(jpeg_decoder *pD, int component_id, int block_x, int block_y)
+{
+ if (pD->get_bits_no_markers(1)) {
+ jpgd_block_t *p = pD->coeff_buf_getp(pD->m_dc_coeffs[component_id], block_x, block_y);
+ p[0] |= (1 << pD->m_successive_low);
+ }
+}
+
+
+void jpeg_decoder::decode_block_ac_first(jpeg_decoder *pD, int component_id, int block_x, int block_y)
+{
+ int k, s, r;
+
+ if (pD->m_eob_run) {
+ pD->m_eob_run--;
+ return;
+ }
+ jpgd_block_t *p = pD->coeff_buf_getp(pD->m_ac_coeffs[component_id], block_x, block_y);
+
+ for (k = pD->m_spectral_start; k <= pD->m_spectral_end; k++) {
+ s = pD->huff_decode(pD->m_pHuff_tabs[pD->m_comp_ac_tab[component_id]]);
+ r = s >> 4;
+ s &= 15;
+ if (s) {
+ if ((k += r) > 63) pD->stop_decoding(JPGD_DECODE_ERROR);
+ r = pD->get_bits_no_markers(s);
+ s = JPGD_HUFF_EXTEND(r, s);
+ p[g_ZAG[k]] = static_cast<jpgd_block_t>(static_cast<unsigned int>(s) << pD->m_successive_low);
+ } else {
+ if (r == 15) {
+ if ((k += 15) > 63) pD->stop_decoding(JPGD_DECODE_ERROR);
+ } else {
+ pD->m_eob_run = 1 << r;
+ if (r) pD->m_eob_run += pD->get_bits_no_markers(r);
+ pD->m_eob_run--;
+ break;
+ }
+ }
+ }
+}
+
+
+void jpeg_decoder::decode_block_ac_refine(jpeg_decoder *pD, int component_id, int block_x, int block_y)
+{
+ int s, k, r;
+ int p1 = 1 << pD->m_successive_low;
+ int m1 = static_cast<unsigned int>(-1) << pD->m_successive_low;
+ jpgd_block_t *p = pD->coeff_buf_getp(pD->m_ac_coeffs[component_id], block_x, block_y);
+
+ JPGD_ASSERT(pD->m_spectral_end <= 63);
+
+ k = pD->m_spectral_start;
+
+ if (pD->m_eob_run == 0) {
+ for ( ; k <= pD->m_spectral_end; k++) {
+ s = pD->huff_decode(pD->m_pHuff_tabs[pD->m_comp_ac_tab[component_id]]);
+ r = s >> 4;
+ s &= 15;
+ if (s) {
+ if (s != 1) pD->stop_decoding(JPGD_DECODE_ERROR);
+ if (pD->get_bits_no_markers(1)) s = p1;
+ else s = m1;
+ } else {
+ if (r != 15) {
+ pD->m_eob_run = 1 << r;
+ if (r) pD->m_eob_run += pD->get_bits_no_markers(r);
+ break;
+ }
+ }
+
+ do {
+ jpgd_block_t *this_coef = p + g_ZAG[k & 63];
+
+ if (*this_coef != 0) {
+ if (pD->get_bits_no_markers(1)) {
+ if ((*this_coef & p1) == 0) {
+ if (*this_coef >= 0) *this_coef = static_cast<jpgd_block_t>(*this_coef + p1);
+ else *this_coef = static_cast<jpgd_block_t>(*this_coef + m1);
+ }
+ }
+ } else {
+ if (--r < 0) break;
+ }
+ k++;
+ } while (k <= pD->m_spectral_end);
+
+ if ((s) && (k < 64)) {
+ p[g_ZAG[k]] = static_cast<jpgd_block_t>(s);
+ }
+ }
+ }
+
+ if (pD->m_eob_run > 0) {
+ for ( ; k <= pD->m_spectral_end; k++) {
+ jpgd_block_t *this_coef = p + g_ZAG[k & 63]; // logical AND to shut up static code analysis
+
+ if (*this_coef != 0) {
+ if (pD->get_bits_no_markers(1)) {
+ if ((*this_coef & p1) == 0) {
+ if (*this_coef >= 0) *this_coef = static_cast<jpgd_block_t>(*this_coef + p1);
+ else *this_coef = static_cast<jpgd_block_t>(*this_coef + m1);
+ }
+ }
+ }
+ }
+ pD->m_eob_run--;
+ }
+}
+
+
+// Decode a scan in a progressively encoded image.
+void jpeg_decoder::decode_scan(pDecode_block_func decode_block_func)
+{
+ int mcu_row, mcu_col, mcu_block;
+ int block_x_mcu[JPGD_MAX_COMPONENTS], m_block_y_mcu[JPGD_MAX_COMPONENTS];
+
+ memset(m_block_y_mcu, 0, sizeof(m_block_y_mcu));
+
+ for (mcu_col = 0; mcu_col < m_mcus_per_col; mcu_col++) {
+ int component_num, component_id;
+ memset(block_x_mcu, 0, sizeof(block_x_mcu));
+
+ for (mcu_row = 0; mcu_row < m_mcus_per_row; mcu_row++) {
+ int block_x_mcu_ofs = 0, block_y_mcu_ofs = 0;
+
+ if ((m_restart_interval) && (m_restarts_left == 0)) process_restart();
+
+ for (mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++) {
+ component_id = m_mcu_org[mcu_block];
+ decode_block_func(this, component_id, block_x_mcu[component_id] + block_x_mcu_ofs, m_block_y_mcu[component_id] + block_y_mcu_ofs);
+
+ if (m_comps_in_scan == 1) block_x_mcu[component_id]++;
+ else {
+ if (++block_x_mcu_ofs == m_comp_h_samp[component_id]) {
+ block_x_mcu_ofs = 0;
+
+ if (++block_y_mcu_ofs == m_comp_v_samp[component_id]) {
+ block_y_mcu_ofs = 0;
+ block_x_mcu[component_id] += m_comp_h_samp[component_id];
+ }
+ }
+ }
+ }
+ m_restarts_left--;
+ }
+
+ if (m_comps_in_scan == 1) m_block_y_mcu[m_comp_list[0]]++;
+ else {
+ for (component_num = 0; component_num < m_comps_in_scan; component_num++) {
+ component_id = m_comp_list[component_num];
+ m_block_y_mcu[component_id] += m_comp_v_samp[component_id];
+ }
+ }
+ }
+}
+
+
+// Decode a progressively encoded image.
+void jpeg_decoder::init_progressive()
+{
+ int i;
+
+ if (m_comps_in_frame == 4) stop_decoding(JPGD_UNSUPPORTED_COLORSPACE);
+
+ // Allocate the coefficient buffers.
+ for (i = 0; i < m_comps_in_frame; i++) {
+ m_dc_coeffs[i] = coeff_buf_open(m_max_mcus_per_row * m_comp_h_samp[i], m_max_mcus_per_col * m_comp_v_samp[i], 1, 1);
+ m_ac_coeffs[i] = coeff_buf_open(m_max_mcus_per_row * m_comp_h_samp[i], m_max_mcus_per_col * m_comp_v_samp[i], 8, 8);
+ }
+
+ while (true) {
+ int dc_only_scan, refinement_scan;
+ pDecode_block_func decode_block_func;
+
+ if (!init_scan()) break;
+
+ dc_only_scan = (m_spectral_start == 0);
+ refinement_scan = (m_successive_high != 0);
+
+ if ((m_spectral_start > m_spectral_end) || (m_spectral_end > 63)) stop_decoding(JPGD_BAD_SOS_SPECTRAL);
+
+ if (dc_only_scan) {
+ if (m_spectral_end) stop_decoding(JPGD_BAD_SOS_SPECTRAL);
+ } else if (m_comps_in_scan != 1) { /* AC scans can only contain one component */
+ stop_decoding(JPGD_BAD_SOS_SPECTRAL);
+ }
+
+ if ((refinement_scan) && (m_successive_low != m_successive_high - 1)) stop_decoding(JPGD_BAD_SOS_SUCCESSIVE);
+
+ if (dc_only_scan) {
+ if (refinement_scan) decode_block_func = decode_block_dc_refine;
+ else decode_block_func = decode_block_dc_first;
+ } else {
+ if (refinement_scan) decode_block_func = decode_block_ac_refine;
+ else decode_block_func = decode_block_ac_first;
+ }
+ decode_scan(decode_block_func);
+ m_bits_left = 16;
+ get_bits(16);
+ get_bits(16);
+ }
+
+ m_comps_in_scan = m_comps_in_frame;
+
+ for (i = 0; i < m_comps_in_frame; i++) {
+ m_comp_list[i] = i;
+ }
+
+ calc_mcu_block_order();
+}
+
+
+void jpeg_decoder::init_sequential()
+{
+ if (!init_scan()) stop_decoding(JPGD_UNEXPECTED_MARKER);
+}
+
+
+void jpeg_decoder::decode_start()
+{
+ init_frame();
+ if (m_progressive_flag) init_progressive();
+ else init_sequential();
+}
+
+
+void jpeg_decoder::decode_init(jpeg_decoder_stream *pStream)
+{
+ init(pStream);
+ locate_sof_marker();
+}
+
+
+jpeg_decoder::jpeg_decoder(jpeg_decoder_stream *pStream)
+{
+ if (setjmp(m_jmp_state)) return;
+ decode_init(pStream);
+}
+
+
+int jpeg_decoder::begin_decoding()
+{
+ if (m_ready_flag) return JPGD_SUCCESS;
+ if (m_error_code) return JPGD_FAILED;
+ if (setjmp(m_jmp_state)) return JPGD_FAILED;
+
+ decode_start();
+ m_ready_flag = true;
+
+ return JPGD_SUCCESS;
+}
+
+
+jpeg_decoder::~jpeg_decoder()
+{
+ free_all_blocks();
+}
+
+
+void jpeg_decoder_file_stream::close()
+{
+ if (m_pFile) {
+ fclose(m_pFile);
+ m_pFile = nullptr;
+ }
+ m_eof_flag = false;
+ m_error_flag = false;
+}
+
+
+jpeg_decoder_file_stream::~jpeg_decoder_file_stream()
+{
+ close();
+}
+
+
+bool jpeg_decoder_file_stream::open(const char *Pfilename)
+{
+ close();
+
+ m_eof_flag = false;
+ m_error_flag = false;
+
+#if defined(_MSC_VER)
+ m_pFile = nullptr;
+ fopen_s(&m_pFile, Pfilename, "rb");
+#else
+ m_pFile = fopen(Pfilename, "rb");
+#endif
+ return m_pFile != nullptr;
+}
+
+
+int jpeg_decoder_file_stream::read(uint8_t *pBuf, int max_bytes_to_read, bool *pEOF_flag)
+{
+ if (!m_pFile) return -1;
+
+ if (m_eof_flag) {
+ *pEOF_flag = true;
+ return 0;
+ }
+
+ if (m_error_flag) return -1;
+
+ int bytes_read = static_cast<int>(fread(pBuf, 1, max_bytes_to_read, m_pFile));
+ if (bytes_read < max_bytes_to_read) {
+ if (ferror(m_pFile)) {
+ m_error_flag = true;
+ return -1;
+ }
+ m_eof_flag = true;
+ *pEOF_flag = true;
+ }
+ return bytes_read;
+}
+
+
+bool jpeg_decoder_mem_stream::open(const uint8_t *pSrc_data, uint32_t size)
+{
+ close();
+ m_pSrc_data = pSrc_data;
+ m_ofs = 0;
+ m_size = size;
+ return true;
+}
+
+
+int jpeg_decoder_mem_stream::read(uint8_t *pBuf, int max_bytes_to_read, bool *pEOF_flag)
+{
+ *pEOF_flag = false;
+ if (!m_pSrc_data) return -1;
+
+ uint32_t bytes_remaining = m_size - m_ofs;
+ if ((uint32_t)max_bytes_to_read > bytes_remaining) {
+ max_bytes_to_read = bytes_remaining;
+ *pEOF_flag = true;
+ }
+ memcpy(pBuf, m_pSrc_data + m_ofs, max_bytes_to_read);
+ m_ofs += max_bytes_to_read;
+
+ return max_bytes_to_read;
+}
+
+
+/************************************************************************/
+/* External Class Implementation */
+/************************************************************************/
+
+
+jpeg_decoder* jpgdHeader(const char* data, int size, int* width, int* height)
+{
+ auto decoder = new jpeg_decoder(new jpeg_decoder_mem_stream((const uint8_t*)data, size));
+ if (decoder->get_error_code() != JPGD_SUCCESS) {
+ delete(decoder);
+ return nullptr;
+ }
+
+ if (width) *width = decoder->get_width();
+ if (height) *height = decoder->get_height();
+
+ return decoder;
+}
+
+
+jpeg_decoder* jpgdHeader(const char* filename, int* width, int* height)
+{
+ auto fileStream = new jpeg_decoder_file_stream();
+ if (!fileStream->open(filename)) {
+ delete(fileStream);
+ return nullptr;
+ }
+
+ auto decoder = new jpeg_decoder(fileStream);
+ if (decoder->get_error_code() != JPGD_SUCCESS) {
+ delete(fileStream);
+ delete(decoder);
+ return nullptr;
+ }
+
+ if (width) *width = decoder->get_width();
+ if (height) *height = decoder->get_height();
+
+ return decoder;
+}
+
+
+void jpgdDelete(jpeg_decoder* decoder)
+{
+ delete(decoder);
+}
+
+
+unsigned char* jpgdDecompress(jpeg_decoder* decoder)
+{
+ if (!decoder) return nullptr;
+
+ int req_comps = 4; //TODO: fixed 4 channel components now?
+ if ((req_comps != 1) && (req_comps != 3) && (req_comps != 4)) return nullptr;
+
+ auto image_width = decoder->get_width();
+ auto image_height = decoder->get_height();
+ //auto actual_comps = decoder->get_num_components();
+
+ if (decoder->begin_decoding() != JPGD_SUCCESS) return nullptr;
+
+ const int dst_bpl = image_width * req_comps;
+ uint8_t *pImage_data = (uint8_t*)malloc(dst_bpl * image_height);
+ if (!pImage_data) return nullptr;
+
+ for (int y = 0; y < image_height; y++) {
+ const uint8_t* pScan_line;
+ uint32_t scan_line_len;
+ if (decoder->decode((const void**)&pScan_line, &scan_line_len) != JPGD_SUCCESS) {
+ free(pImage_data);
+ return nullptr;
+ }
+
+ uint8_t *pDst = pImage_data + y * dst_bpl;
+
+ //Return as BGRA
+ if ((req_comps == 4) && (decoder->get_num_components() == 3)) {
+ for (int x = 0; x < image_width; x++) {
+ pDst[0] = pScan_line[x*4+2];
+ pDst[1] = pScan_line[x*4+1];
+ pDst[2] = pScan_line[x*4+0];
+ pDst[3] = 255;
+ pDst += 4;
+ }
+ } else if (((req_comps == 1) && (decoder->get_num_components() == 1)) || ((req_comps == 4) && (decoder->get_num_components() == 3))) {
+ memcpy(pDst, pScan_line, dst_bpl);
+ } else if (decoder->get_num_components() == 1) {
+ if (req_comps == 3) {
+ for (int x = 0; x < image_width; x++) {
+ uint8_t luma = pScan_line[x];
+ pDst[0] = luma;
+ pDst[1] = luma;
+ pDst[2] = luma;
+ pDst += 3;
+ }
+ } else {
+ for (int x = 0; x < image_width; x++) {
+ uint8_t luma = pScan_line[x];
+ pDst[0] = luma;
+ pDst[1] = luma;
+ pDst[2] = luma;
+ pDst[3] = 255;
+ pDst += 4;
+ }
+ }
+ } else if (decoder->get_num_components() == 3) {
+ if (req_comps == 1) {
+ const int YR = 19595, YG = 38470, YB = 7471;
+ for (int x = 0; x < image_width; x++) {
+ int r = pScan_line[x*4+0];
+ int g = pScan_line[x*4+1];
+ int b = pScan_line[x*4+2];
+ *pDst++ = static_cast<uint8_t>((r * YR + g * YG + b * YB + 32768) >> 16);
+ }
+ } else {
+ for (int x = 0; x < image_width; x++) {
+ pDst[0] = pScan_line[x*4+0];
+ pDst[1] = pScan_line[x*4+1];
+ pDst[2] = pScan_line[x*4+2];
+ pDst += 3;
+ }
+ }
+ }
+ }
+ return pImage_data;
+}
diff --git a/thirdparty/thorvg/src/loaders/jpg/tvgJpgd.h b/thirdparty/thorvg/src/loaders/jpg/tvgJpgd.h
new file mode 100644
index 0000000000..030fdc2946
--- /dev/null
+++ b/thirdparty/thorvg/src/loaders/jpg/tvgJpgd.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2021 - 2023 the ThorVG project. All rights reserved.
+
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+// jpgd.h - C++ class for JPEG decompression.
+// Public domain, Rich Geldreich <richgel99@gmail.com>
+#ifndef _TVG_JPGD_H_
+#define _TVG_JPGD_H_
+
+class jpeg_decoder;
+
+jpeg_decoder* jpgdHeader(const char* data, int size, int* width, int* height);
+jpeg_decoder* jpgdHeader(const char* filename, int* width, int* height);
+unsigned char* jpgdDecompress(jpeg_decoder* decoder);
+void jpgdDelete(jpeg_decoder* decoder);
+
+#endif //_TVG_JPGD_H_
diff --git a/thirdparty/thorvg/src/loaders/png/tvgLodePng.cpp b/thirdparty/thorvg/src/loaders/png/tvgLodePng.cpp
new file mode 100644
index 0000000000..fc4cce4061
--- /dev/null
+++ b/thirdparty/thorvg/src/loaders/png/tvgLodePng.cpp
@@ -0,0 +1,2628 @@
+/*
+ * Copyright (c) 2020 - 2023 the ThorVG project. All rights reserved.
+
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+/*
+ LodePNG version 20200306
+
+ Copyright (c) 2005-2020 Lode Vandevenne
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+
+ 3. This notice may not be removed or altered from any sourcedistribution.
+*/
+
+#include <cstdlib>
+#include "tvgLodePng.h"
+
+
+/************************************************************************/
+/* Internal Class Implementation */
+/************************************************************************/
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1310) /*Visual Studio: A few warning types are not desired here.*/
+ #pragma warning( disable : 4244 ) /*implicit conversions: not warned by gcc -Wall -Wextra and requires too much casts*/
+ #pragma warning( disable : 4996 ) /*VS does not like fopen, but fopen_s is not standard C so unusable here*/
+#endif /*_MSC_VER */
+
+
+/* convince the compiler to inline a function, for use when this measurably improves performance */
+/* inline is not available in C90, but use it when supported by the compiler */
+#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || (defined(__cplusplus) && (__cplusplus >= 199711L))
+ #define LODEPNG_INLINE inline
+#else
+ #define LODEPNG_INLINE /* not available */
+#endif
+
+/* restrict is not available in C90, but use it when supported by the compiler */
+#if (defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))) ||\
+ (defined(_MSC_VER) && (_MSC_VER >= 1400)) || \
+ (defined(__WATCOMC__) && (__WATCOMC__ >= 1250) && !defined(__cplusplus))
+ #define LODEPNG_RESTRICT __restrict
+#else
+ #define LODEPNG_RESTRICT /* not available */
+#endif
+
+#define LODEPNG_MAX(a, b) (((a) > (b)) ? (a) : (b))
+#define LODEPNG_MIN(a, b) (((a) < (b)) ? (a) : (b))
+#define LODEPNG_ABS(x) ((x) < 0 ? -(x) : (x))
+
+
+/* Replacements for C library functions such as memcpy and strlen, to support platforms
+where a full C library is not available. The compiler can recognize them and compile
+to something as fast. */
+
+static void lodepng_memcpy(void* LODEPNG_RESTRICT dst, const void* LODEPNG_RESTRICT src, size_t size)
+{
+ size_t i;
+ for (i = 0; i < size; i++) ((char*)dst)[i] = ((const char*)src)[i];
+}
+
+
+static void lodepng_memset(void* LODEPNG_RESTRICT dst, int value, size_t num)
+{
+ size_t i;
+ for (i = 0; i < num; i++) ((char*)dst)[i] = (char)value;
+}
+
+
+/* does not check memory out of bounds, do not use on untrusted data */
+static size_t lodepng_strlen(const char* a)
+{
+ const char* orig = a;
+ /* avoid warning about unused function in case of disabled COMPILE... macros */
+ (void)(&lodepng_strlen);
+ while (*a) a++;
+ return (size_t)(a - orig);
+}
+
+
+/* Safely check if adding two integers will overflow (no undefined
+behavior, compiler removing the code, etc...) and output result. */
+static int lodepng_addofl(size_t a, size_t b, size_t* result)
+{
+ *result = a + b; /* Unsigned addition is well defined and safe in C90 */
+ return *result < a;
+}
+
+
+/* Safely check if multiplying two integers will overflow (no undefined
+behavior, compiler removing the code, etc...) and output result. */
+static int lodepng_mulofl(size_t a, size_t b, size_t* result)
+{
+ *result = a * b; /* Unsigned multiplication is well defined and safe in C90 */
+ return (a != 0 && *result / a != b);
+}
+
+
+/* Safely check if a + b > c, even if overflow could happen. */
+static int lodepng_gtofl(size_t a, size_t b, size_t c)
+{
+ size_t d;
+ if (lodepng_addofl(a, b, &d)) return 1;
+ return d > c;
+}
+
+
+/*
+ Often in case of an error a value is assigned to a variable and then it breaks
+ out of a loop (to go to the cleanup phase of a function). This macro does that.
+ It makes the error handling code shorter and more readable.
+
+ Example: if(!uivector_resize(&lz77_encoded, datasize)) ERROR_BREAK(83);
+*/
+#define CERROR_BREAK(errorvar, code){\
+ errorvar = code;\
+ break;\
+}
+
+/* version of CERROR_BREAK that assumes the common case where the error variable is named "error" */
+#define ERROR_BREAK(code) CERROR_BREAK(error, code)
+
+/* Set error var to the error code, and return it.*/
+#define CERROR_RETURN_ERROR(errorvar, code){\
+ errorvar = code;\
+ return code;\
+}
+
+/* Try the code, if it returns error, also return the error. */
+#define CERROR_TRY_RETURN(call){\
+ unsigned error = call;\
+ if(error) return error;\
+}
+
+/* Set error var to the error code, and return from the void function. */
+#define CERROR_RETURN(errorvar, code){\
+ errorvar = code;\
+ return;\
+}
+
+
+/* dynamic vector of unsigned chars */
+struct ucvector
+{
+ unsigned char* data;
+ size_t size; /*used size*/
+ size_t allocsize; /*allocated size*/
+};
+
+
+/* returns 1 if success, 0 if failure ==> nothing done */
+static unsigned ucvector_resize(ucvector* p, size_t size)
+{
+ if (size > p->allocsize) {
+ size_t newsize = size + (p->allocsize >> 1u);
+ void* data = realloc(p->data, newsize);
+ if(data) {
+ p->allocsize = newsize;
+ p->data = (unsigned char*)data;
+ }
+ else return 0; /*error: not enough memory*/
+ }
+ p->size = size;
+ return 1; /*success*/
+}
+
+
+static ucvector ucvector_init(unsigned char* buffer, size_t size)
+{
+ ucvector v;
+ v.data = buffer;
+ v.allocsize = v.size = size;
+ return v;
+}
+
+
+static unsigned lodepng_read32bitInt(const unsigned char* buffer)
+{
+ return (((unsigned)buffer[0] << 24u) | ((unsigned)buffer[1] << 16u) | ((unsigned)buffer[2] << 8u) | (unsigned)buffer[3]);
+}
+
+
+/* ////////////////////////////////////////////////////////////////////////// */
+/* ////////////////////////////////////////////////////////////////////////// */
+/* // End of common code and tools. Begin of Zlib related code. // */
+/* ////////////////////////////////////////////////////////////////////////// */
+/* ////////////////////////////////////////////////////////////////////////// */
+
+struct LodePNGBitReader
+{
+ const unsigned char* data;
+ size_t size; /*size of data in bytes*/
+ size_t bitsize; /*size of data in bits, end of valid bp values, should be 8*size*/
+ size_t bp;
+ unsigned buffer; /*buffer for reading bits. NOTE: 'unsigned' must support at least 32 bits*/
+};
+
+
+/* data size argument is in bytes. Returns error if size too large causing overflow */
+static unsigned LodePNGBitReader_init(LodePNGBitReader* reader, const unsigned char* data, size_t size)
+{
+ size_t temp;
+ reader->data = data;
+ reader->size = size;
+ /* size in bits, return error if overflow (if size_t is 32 bit this supports up to 500MB) */
+ if (lodepng_mulofl(size, 8u, &reader->bitsize)) return 105;
+ /*ensure incremented bp can be compared to bitsize without overflow even when it would be incremented 32 too much and
+ trying to ensure 32 more bits*/
+ if (lodepng_addofl(reader->bitsize, 64u, &temp)) return 105;
+ reader->bp = 0;
+ reader->buffer = 0;
+ return 0; /*ok*/
+ }
+
+/*
+ ensureBits functions:
+ Ensures the reader can at least read nbits bits in one or more readBits calls,
+ safely even if not enough bits are available.
+ Returns 1 if there are enough bits available, 0 if not.
+*/
+
+/*See ensureBits documentation above. This one ensures exactly 1 bit */
+/*static unsigned ensureBits1(LodePNGBitReader* reader) {
+ if(reader->bp >= reader->bitsize) return 0;
+ reader->buffer = (unsigned)reader->data[reader->bp >> 3u] >> (reader->bp & 7u);
+ return 1;
+}*/
+
+/*See ensureBits documentation above. This one ensures up to 9 bits */
+static unsigned ensureBits9(LodePNGBitReader* reader, size_t nbits)
+{
+ size_t start = reader->bp >> 3u;
+ size_t size = reader->size;
+ if (start + 1u < size) {
+ reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u);
+ reader->buffer >>= (reader->bp & 7u);
+ return 1;
+ } else {
+ reader->buffer = 0;
+ if (start + 0u < size) reader->buffer |= reader->data[start + 0];
+ reader->buffer >>= (reader->bp & 7u);
+ return reader->bp + nbits <= reader->bitsize;
+ }
+}
+
+
+/*See ensureBits documentation above. This one ensures up to 17 bits */
+static unsigned ensureBits17(LodePNGBitReader* reader, size_t nbits)
+{
+ size_t start = reader->bp >> 3u;
+ size_t size = reader->size;
+ if (start + 2u < size) {
+ reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u) | ((unsigned)reader->data[start + 2] << 16u);
+ reader->buffer >>= (reader->bp & 7u);
+ return 1;
+ } else {
+ reader->buffer = 0;
+ if (start + 0u < size) reader->buffer |= reader->data[start + 0];
+ if (start + 1u < size) reader->buffer |= ((unsigned)reader->data[start + 1] << 8u);
+ reader->buffer >>= (reader->bp & 7u);
+ return reader->bp + nbits <= reader->bitsize;
+ }
+}
+
+
+/*See ensureBits documentation above. This one ensures up to 25 bits */
+static LODEPNG_INLINE unsigned ensureBits25(LodePNGBitReader* reader, size_t nbits)
+{
+ size_t start = reader->bp >> 3u;
+ size_t size = reader->size;
+ if (start + 3u < size) {
+ reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u) | ((unsigned)reader->data[start + 2] << 16u) | ((unsigned)reader->data[start + 3] << 24u);
+ reader->buffer >>= (reader->bp & 7u);
+ return 1;
+ } else {
+ reader->buffer = 0;
+ if (start + 0u < size) reader->buffer |= reader->data[start + 0];
+ if (start + 1u < size) reader->buffer |= ((unsigned)reader->data[start + 1] << 8u);
+ if (start + 2u < size) reader->buffer |= ((unsigned)reader->data[start + 2] << 16u);
+ reader->buffer >>= (reader->bp & 7u);
+ return reader->bp + nbits <= reader->bitsize;
+ }
+}
+
+
+/*See ensureBits documentation above. This one ensures up to 32 bits */
+static LODEPNG_INLINE unsigned ensureBits32(LodePNGBitReader* reader, size_t nbits)
+{
+ size_t start = reader->bp >> 3u;
+ size_t size = reader->size;
+ if(start + 4u < size) {
+ reader->buffer = (unsigned)reader->data[start + 0] | ((unsigned)reader->data[start + 1] << 8u) | ((unsigned)reader->data[start + 2] << 16u) | ((unsigned)reader->data[start + 3] << 24u);
+ reader->buffer >>= (reader->bp & 7u);
+ reader->buffer |= (((unsigned)reader->data[start + 4] << 24u) << (8u - (reader->bp & 7u)));
+ return 1;
+ } else {
+ reader->buffer = 0;
+ if (start + 0u < size) reader->buffer |= reader->data[start + 0];
+ if (start + 1u < size) reader->buffer |= ((unsigned)reader->data[start + 1] << 8u);
+ if (start + 2u < size) reader->buffer |= ((unsigned)reader->data[start + 2] << 16u);
+ if (start + 3u < size) reader->buffer |= ((unsigned)reader->data[start + 3] << 24u);
+ reader->buffer >>= (reader->bp & 7u);
+ return reader->bp + nbits <= reader->bitsize;
+ }
+}
+
+
+/* Get bits without advancing the bit pointer. Must have enough bits available with ensureBits. Max nbits is 31. */
+static unsigned peekBits(LodePNGBitReader* reader, size_t nbits)
+{
+ /* The shift allows nbits to be only up to 31. */
+ return reader->buffer & ((1u << nbits) - 1u);
+}
+
+
+/* Must have enough bits available with ensureBits */
+static void advanceBits(LodePNGBitReader* reader, size_t nbits)
+{
+ reader->buffer >>= nbits;
+ reader->bp += nbits;
+}
+
+
+/* Must have enough bits available with ensureBits */
+static unsigned readBits(LodePNGBitReader* reader, size_t nbits)
+{
+ unsigned result = peekBits(reader, nbits);
+ advanceBits(reader, nbits);
+ return result;
+}
+
+
+/* Public for testing only. steps and result must have numsteps values. */
+unsigned lode_png_test_bitreader(const unsigned char* data, size_t size, size_t numsteps, const size_t* steps, unsigned* result)
+{
+ size_t i;
+ LodePNGBitReader reader;
+ unsigned error = LodePNGBitReader_init(&reader, data, size);
+ if (error) return 0;
+ for (i = 0; i < numsteps; i++) {
+ size_t step = steps[i];
+ unsigned ok;
+ if (step > 25) ok = ensureBits32(&reader, step);
+ else if (step > 17) ok = ensureBits25(&reader, step);
+ else if (step > 9) ok = ensureBits17(&reader, step);
+ else ok = ensureBits9(&reader, step);
+ if (!ok) return 0;
+ result[i] = readBits(&reader, step);
+ }
+ return 1;
+}
+
+
+static unsigned reverseBits(unsigned bits, unsigned num)
+{
+ /*TODO: implement faster lookup table based version when needed*/
+ unsigned i, result = 0;
+ for (i = 0; i < num; i++) result |= ((bits >> (num - i - 1u)) & 1u) << i;
+ return result;
+}
+
+/* ////////////////////////////////////////////////////////////////////////// */
+/* / Deflate - Huffman / */
+/* ////////////////////////////////////////////////////////////////////////// */
+
+#define FIRST_LENGTH_CODE_INDEX 257
+#define LAST_LENGTH_CODE_INDEX 285
+/*256 literals, the end code, some length codes, and 2 unused codes*/
+#define NUM_DEFLATE_CODE_SYMBOLS 288
+/*the distance codes have their own symbols, 30 used, 2 unused*/
+#define NUM_DISTANCE_SYMBOLS 32
+/*the code length codes. 0-15: code lengths, 16: copy previous 3-6 times, 17: 3-10 zeros, 18: 11-138 zeros*/
+#define NUM_CODE_LENGTH_CODES 19
+
+/*the base lengths represented by codes 257-285*/
+static const unsigned LENGTHBASE[29]
+ = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
+ 67, 83, 99, 115, 131, 163, 195, 227, 258};
+
+/*the extra bits used by codes 257-285 (added to base length)*/
+static const unsigned LENGTHEXTRA[29]
+ = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
+ 4, 4, 4, 4, 5, 5, 5, 5, 0};
+
+/*the base backwards distances (the bits of distance codes appear after length codes and use their own huffman tree)*/
+static const unsigned DISTANCEBASE[30]
+ = {1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
+ 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
+
+/*the extra bits of backwards distances (added to base)*/
+static const unsigned DISTANCEEXTRA[30]
+ = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8,
+ 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
+
+/*the order in which "code length alphabet code lengths" are stored as specified by deflate, out of this the huffman
+tree of the dynamic huffman tree lengths is generated*/
+static const unsigned CLCL_ORDER[NUM_CODE_LENGTH_CODES]
+ = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
+
+/* ////////////////////////////////////////////////////////////////////////// */
+
+/*
+Huffman tree struct, containing multiple representations of the tree
+*/
+struct HuffmanTree
+{
+ unsigned* codes; /*the huffman codes (bit patterns representing the symbols)*/
+ unsigned* lengths; /*the lengths of the huffman codes*/
+ unsigned maxbitlen; /*maximum number of bits a single code can get*/
+ unsigned numcodes; /*number of symbols in the alphabet = number of codes*/
+ /* for reading only */
+ unsigned char* table_len; /*length of symbol from lookup table, or max length if secondary lookup needed*/
+ unsigned short* table_value; /*value of symbol from lookup table, or pointer to secondary table if needed*/
+};
+
+
+static void HuffmanTree_init(HuffmanTree* tree)
+{
+ tree->codes = 0;
+ tree->lengths = 0;
+ tree->table_len = 0;
+ tree->table_value = 0;
+}
+
+
+static void HuffmanTree_cleanup(HuffmanTree* tree)
+{
+ free(tree->codes);
+ free(tree->lengths);
+ free(tree->table_len);
+ free(tree->table_value);
+}
+
+
+/* amount of bits for first huffman table lookup (aka root bits), see HuffmanTree_makeTable and huffmanDecodeSymbol.*/
+/* values 8u and 9u work the fastest */
+#define FIRSTBITS 9u
+
+/* a symbol value too big to represent any valid symbol, to indicate reading disallowed huffman bits combination,
+which is possible in case of only 0 or 1 present symbols. */
+#define INVALIDSYMBOL 65535u
+
+/* make table for huffman decoding */
+static unsigned HuffmanTree_makeTable(HuffmanTree* tree)
+{
+ static const unsigned headsize = 1u << FIRSTBITS; /*size of the first table*/
+ static const unsigned mask = (1u << FIRSTBITS) /*headsize*/ - 1u;
+ size_t i, numpresent, pointer, size; /*total table size*/
+ unsigned* maxlens = (unsigned*)malloc(headsize * sizeof(unsigned));
+ if (!maxlens) return 83; /*alloc fail*/
+
+ /* compute maxlens: max total bit length of symbols sharing prefix in the first table*/
+ lodepng_memset(maxlens, 0, headsize * sizeof(*maxlens));
+ for (i = 0; i < tree->numcodes; i++) {
+ unsigned symbol = tree->codes[i];
+ unsigned l = tree->lengths[i];
+ unsigned index;
+ if(l <= FIRSTBITS) continue; /*symbols that fit in first table don't increase secondary table size*/
+ /*get the FIRSTBITS MSBs, the MSBs of the symbol are encoded first. See later comment about the reversing*/
+ index = reverseBits(symbol >> (l - FIRSTBITS), FIRSTBITS);
+ maxlens[index] = LODEPNG_MAX(maxlens[index], l);
+ }
+ /* compute total table size: size of first table plus all secondary tables for symbols longer than FIRSTBITS */
+ size = headsize;
+ for (i = 0; i < headsize; ++i) {
+ unsigned l = maxlens[i];
+ if (l > FIRSTBITS) size += (1u << (l - FIRSTBITS));
+ }
+ tree->table_len = (unsigned char*)malloc(size * sizeof(*tree->table_len));
+ tree->table_value = (unsigned short*)malloc(size * sizeof(*tree->table_value));
+ if (!tree->table_len || !tree->table_value) {
+ free(maxlens);
+ /* freeing tree->table values is done at a higher scope */
+ return 83; /*alloc fail*/
+ }
+ /*initialize with an invalid length to indicate unused entries*/
+ for (i = 0; i < size; ++i) tree->table_len[i] = 16;
+
+ /*fill in the first table for long symbols: max prefix size and pointer to secondary tables*/
+ pointer = headsize;
+ for (i = 0; i < headsize; ++i) {
+ unsigned l = maxlens[i];
+ if(l <= FIRSTBITS) continue;
+ tree->table_len[i] = l;
+ tree->table_value[i] = pointer;
+ pointer += (1u << (l - FIRSTBITS));
+ }
+ free(maxlens);
+
+ /*fill in the first table for short symbols, or secondary table for long symbols*/
+ numpresent = 0;
+ for (i = 0; i < tree->numcodes; ++i) {
+ unsigned l = tree->lengths[i];
+ unsigned symbol = tree->codes[i]; /*the huffman bit pattern. i itself is the value.*/
+ /*reverse bits, because the huffman bits are given in MSB first order but the bit reader reads LSB first*/
+ unsigned reverse = reverseBits(symbol, l);
+ if (l == 0) continue;
+ numpresent++;
+
+ if (l <= FIRSTBITS) {
+ /*short symbol, fully in first table, replicated num times if l < FIRSTBITS*/
+ unsigned num = 1u << (FIRSTBITS - l);
+ unsigned j;
+ for (j = 0; j < num; ++j) {
+ /*bit reader will read the l bits of symbol first, the remaining FIRSTBITS - l bits go to the MSB's*/
+ unsigned index = reverse | (j << l);
+ if(tree->table_len[index] != 16) return 55; /*invalid tree: long symbol shares prefix with short symbol*/
+ tree->table_len[index] = l;
+ tree->table_value[index] = i;
+ }
+ } else {
+ /*long symbol, shares prefix with other long symbols in first lookup table, needs second lookup*/
+ /*the FIRSTBITS MSBs of the symbol are the first table index*/
+ unsigned index = reverse & mask;
+ unsigned maxlen = tree->table_len[index];
+ /*log2 of secondary table length, should be >= l - FIRSTBITS*/
+ unsigned tablelen = maxlen - FIRSTBITS;
+ unsigned start = tree->table_value[index]; /*starting index in secondary table*/
+ unsigned num = 1u << (tablelen - (l - FIRSTBITS)); /*amount of entries of this symbol in secondary table*/
+ unsigned j;
+ if (maxlen < l) return 55; /*invalid tree: long symbol shares prefix with short symbol*/
+ for (j = 0; j < num; ++j) {
+ unsigned reverse2 = reverse >> FIRSTBITS; /* l - FIRSTBITS bits */
+ unsigned index2 = start + (reverse2 | (j << (l - FIRSTBITS)));
+ tree->table_len[index2] = l;
+ tree->table_value[index2] = i;
+ }
+ }
+ }
+
+ if (numpresent < 2) {
+ /* In case of exactly 1 symbol, in theory the huffman symbol needs 0 bits,
+ but deflate uses 1 bit instead. In case of 0 symbols, no symbols can
+ appear at all, but such huffman tree could still exist (e.g. if distance
+ codes are never used). In both cases, not all symbols of the table will be
+ filled in. Fill them in with an invalid symbol value so returning them from
+ huffmanDecodeSymbol will cause error. */
+ for (i = 0; i < size; ++i) {
+ if (tree->table_len[i] == 16) {
+ /* As length, use a value smaller than FIRSTBITS for the head table,
+ and a value larger than FIRSTBITS for the secondary table, to ensure
+ valid behavior for advanceBits when reading this symbol. */
+ tree->table_len[i] = (i < headsize) ? 1 : (FIRSTBITS + 1);
+ tree->table_value[i] = INVALIDSYMBOL;
+ }
+ }
+ } else {
+ /* A good huffman tree has N * 2 - 1 nodes, of which N - 1 are internal nodes.
+ If that is not the case (due to too long length codes), the table will not
+ have been fully used, and this is an error (not all bit combinations can be
+ decoded): an oversubscribed huffman tree, indicated by error 55. */
+ for (i = 0; i < size; ++i) {
+ if (tree->table_len[i] == 16) return 55;
+ }
+ }
+ return 0;
+}
+
+
+/*
+ Second step for the ...makeFromLengths and ...makeFromFrequencies functions.
+ numcodes, lengths and maxbitlen must already be filled in correctly. return
+ value is error.
+*/
+static unsigned HuffmanTree_makeFromLengths2(HuffmanTree* tree)
+{
+ unsigned* blcount;
+ unsigned* nextcode;
+ unsigned error = 0;
+ unsigned bits, n;
+
+ tree->codes = (unsigned*)malloc(tree->numcodes * sizeof(unsigned));
+ blcount = (unsigned*)malloc((tree->maxbitlen + 1) * sizeof(unsigned));
+ nextcode = (unsigned*)malloc((tree->maxbitlen + 1) * sizeof(unsigned));
+ if (!tree->codes || !blcount || !nextcode) error = 83; /*alloc fail*/
+
+ if (!error) {
+ for (n = 0; n != tree->maxbitlen + 1; n++) blcount[n] = nextcode[n] = 0;
+ /*step 1: count number of instances of each code length*/
+ for (bits = 0; bits != tree->numcodes; ++bits) ++blcount[tree->lengths[bits]];
+ /*step 2: generate the nextcode values*/
+ for(bits = 1; bits <= tree->maxbitlen; ++bits) {
+ nextcode[bits] = (nextcode[bits - 1] + blcount[bits - 1]) << 1u;
+ }
+ /*step 3: generate all the codes*/
+ for (n = 0; n != tree->numcodes; ++n) {
+ if (tree->lengths[n] != 0) {
+ tree->codes[n] = nextcode[tree->lengths[n]]++;
+ /*remove superfluous bits from the code*/
+ tree->codes[n] &= ((1u << tree->lengths[n]) - 1u);
+ }
+ }
+ }
+
+ free(blcount);
+ free(nextcode);
+
+ if (!error) error = HuffmanTree_makeTable(tree);
+ return error;
+}
+
+
+/*
+ given the code lengths (as stored in the PNG file), generate the tree as defined
+ by Deflate. maxbitlen is the maximum bits that a code in the tree can have.
+ return value is error.
+*/
+static unsigned HuffmanTree_makeFromLengths(HuffmanTree* tree, const unsigned* bitlen, size_t numcodes, unsigned maxbitlen)
+{
+ unsigned i;
+ tree->lengths = (unsigned*)malloc(numcodes * sizeof(unsigned));
+ if (!tree->lengths) return 83; /*alloc fail*/
+ for (i = 0; i != numcodes; ++i) tree->lengths[i] = bitlen[i];
+ tree->numcodes = (unsigned)numcodes; /*number of symbols*/
+ tree->maxbitlen = maxbitlen;
+ return HuffmanTree_makeFromLengths2(tree);
+}
+
+
+/*get the literal and length code tree of a deflated block with fixed tree, as per the deflate specification*/
+static unsigned generateFixedLitLenTree(HuffmanTree* tree)
+{
+ unsigned i, error = 0;
+ unsigned* bitlen = (unsigned*)malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned));
+ if (!bitlen) return 83; /*alloc fail*/
+
+ /*288 possible codes: 0-255=literals, 256=endcode, 257-285=lengthcodes, 286-287=unused*/
+ for (i = 0; i <= 143; ++i) bitlen[i] = 8;
+ for (i = 144; i <= 255; ++i) bitlen[i] = 9;
+ for (i = 256; i <= 279; ++i) bitlen[i] = 7;
+ for (i = 280; i <= 287; ++i) bitlen[i] = 8;
+
+ error = HuffmanTree_makeFromLengths(tree, bitlen, NUM_DEFLATE_CODE_SYMBOLS, 15);
+
+ free(bitlen);
+ return error;
+}
+
+
+/*get the distance code tree of a deflated block with fixed tree, as specified in the deflate specification*/
+static unsigned generateFixedDistanceTree(HuffmanTree* tree)
+{
+ unsigned i, error = 0;
+ unsigned* bitlen = (unsigned*)malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned));
+ if (!bitlen) return 83; /*alloc fail*/
+
+ /*there are 32 distance codes, but 30-31 are unused*/
+ for (i = 0; i != NUM_DISTANCE_SYMBOLS; ++i) bitlen[i] = 5;
+ error = HuffmanTree_makeFromLengths(tree, bitlen, NUM_DISTANCE_SYMBOLS, 15);
+
+ free(bitlen);
+ return error;
+}
+
+
+/*
+ returns the code. The bit reader must already have been ensured at least 15 bits
+*/
+static unsigned huffmanDecodeSymbol(LodePNGBitReader* reader, const HuffmanTree* codetree)
+{
+ unsigned short code = peekBits(reader, FIRSTBITS);
+ unsigned short l = codetree->table_len[code];
+ unsigned short value = codetree->table_value[code];
+ if (l <= FIRSTBITS) {
+ advanceBits(reader, l);
+ return value;
+ } else {
+ unsigned index2;
+ advanceBits(reader, FIRSTBITS);
+ index2 = value + peekBits(reader, l - FIRSTBITS);
+ advanceBits(reader, codetree->table_len[index2] - FIRSTBITS);
+ return codetree->table_value[index2];
+ }
+}
+
+
+/* ////////////////////////////////////////////////////////////////////////// */
+/* / Inflator (Decompressor) / */
+/* ////////////////////////////////////////////////////////////////////////// */
+
+/*get the tree of a deflated block with fixed tree, as specified in the deflate specification
+Returns error code.*/
+static unsigned getTreeInflateFixed(HuffmanTree* tree_ll, HuffmanTree* tree_d)
+{
+ unsigned error = generateFixedLitLenTree(tree_ll);
+ if (error) return error;
+ return generateFixedDistanceTree(tree_d);
+}
+
+
+/*get the tree of a deflated block with dynamic tree, the tree itself is also Huffman compressed with a known tree*/
+static unsigned getTreeInflateDynamic(HuffmanTree* tree_ll, HuffmanTree* tree_d, LodePNGBitReader* reader)
+{
+ /*make sure that length values that aren't filled in will be 0, or a wrong tree will be generated*/
+ unsigned error = 0;
+ unsigned n, HLIT, HDIST, HCLEN, i;
+
+ /*see comments in deflateDynamic for explanation of the context and these variables, it is analogous*/
+ unsigned* bitlen_ll = 0; /*lit,len code lengths*/
+ unsigned* bitlen_d = 0; /*dist code lengths*/
+ /*code length code lengths ("clcl"), the bit lengths of the huffman tree used to compress bitlen_ll and bitlen_d*/
+ unsigned* bitlen_cl = 0;
+ HuffmanTree tree_cl; /*the code tree for code length codes (the huffman tree for compressed huffman trees)*/
+
+ if (!ensureBits17(reader, 14)) return 49; /*error: the bit pointer is or will go past the memory*/
+
+ /*number of literal/length codes + 257. Unlike the spec, the value 257 is added to it here already*/
+ HLIT = readBits(reader, 5) + 257;
+ /*number of distance codes. Unlike the spec, the value 1 is added to it here already*/
+ HDIST = readBits(reader, 5) + 1;
+ /*number of code length codes. Unlike the spec, the value 4 is added to it here already*/
+ HCLEN = readBits(reader, 4) + 4;
+
+ bitlen_cl = (unsigned*)malloc(NUM_CODE_LENGTH_CODES * sizeof(unsigned));
+ if(!bitlen_cl) return 83 /*alloc fail*/;
+
+ HuffmanTree_init(&tree_cl);
+
+ while (!error) {
+ /*read the code length codes out of 3 * (amount of code length codes) bits*/
+ if (lodepng_gtofl(reader->bp, HCLEN * 3, reader->bitsize)) {
+ ERROR_BREAK(50); /*error: the bit pointer is or will go past the memory*/
+ }
+ for (i = 0; i != HCLEN; ++i) {
+ ensureBits9(reader, 3); /*out of bounds already checked above */
+ bitlen_cl[CLCL_ORDER[i]] = readBits(reader, 3);
+ }
+ for (i = HCLEN; i != NUM_CODE_LENGTH_CODES; ++i) {
+ bitlen_cl[CLCL_ORDER[i]] = 0;
+ }
+
+ error = HuffmanTree_makeFromLengths(&tree_cl, bitlen_cl, NUM_CODE_LENGTH_CODES, 7);
+ if(error) break;
+
+ /*now we can use this tree to read the lengths for the tree that this function will return*/
+ bitlen_ll = (unsigned*)malloc(NUM_DEFLATE_CODE_SYMBOLS * sizeof(unsigned));
+ bitlen_d = (unsigned*)malloc(NUM_DISTANCE_SYMBOLS * sizeof(unsigned));
+ if (!bitlen_ll || !bitlen_d) ERROR_BREAK(83 /*alloc fail*/);
+ lodepng_memset(bitlen_ll, 0, NUM_DEFLATE_CODE_SYMBOLS * sizeof(*bitlen_ll));
+ lodepng_memset(bitlen_d, 0, NUM_DISTANCE_SYMBOLS * sizeof(*bitlen_d));
+
+ /*i is the current symbol we're reading in the part that contains the code lengths of lit/len and dist codes*/
+ i = 0;
+ while (i < HLIT + HDIST) {
+ unsigned code;
+ ensureBits25(reader, 22); /* up to 15 bits for huffman code, up to 7 extra bits below*/
+ code = huffmanDecodeSymbol(reader, &tree_cl);
+ if (code <= 15) /*a length code*/ {
+ if (i < HLIT) bitlen_ll[i] = code;
+ else bitlen_d[i - HLIT] = code;
+ ++i;
+ } else if (code == 16) /*repeat previous*/ {
+ unsigned replength = 3; /*read in the 2 bits that indicate repeat length (3-6)*/
+ unsigned value; /*set value to the previous code*/
+
+ if (i == 0) ERROR_BREAK(54); /*can't repeat previous if i is 0*/
+
+ replength += readBits(reader, 2);
+
+ if (i < HLIT + 1) value = bitlen_ll[i - 1];
+ else value = bitlen_d[i - HLIT - 1];
+ /*repeat this value in the next lengths*/
+ for (n = 0; n < replength; ++n) {
+ if (i >= HLIT + HDIST) ERROR_BREAK(13); /*error: i is larger than the amount of codes*/
+ if (i < HLIT) bitlen_ll[i] = value;
+ else bitlen_d[i - HLIT] = value;
+ ++i;
+ }
+ } else if(code == 17) /*repeat "0" 3-10 times*/ {
+ unsigned replength = 3; /*read in the bits that indicate repeat length*/
+ replength += readBits(reader, 3);
+
+ /*repeat this value in the next lengths*/
+ for (n = 0; n < replength; ++n) {
+ if (i >= HLIT + HDIST) ERROR_BREAK(14); /*error: i is larger than the amount of codes*/
+
+ if (i < HLIT) bitlen_ll[i] = 0;
+ else bitlen_d[i - HLIT] = 0;
+ ++i;
+ }
+ } else if(code == 18) /*repeat "0" 11-138 times*/ {
+ unsigned replength = 11; /*read in the bits that indicate repeat length*/
+ replength += readBits(reader, 7);
+
+ /*repeat this value in the next lengths*/
+ for (n = 0; n < replength; ++n) {
+ if(i >= HLIT + HDIST) ERROR_BREAK(15); /*error: i is larger than the amount of codes*/
+
+ if(i < HLIT) bitlen_ll[i] = 0;
+ else bitlen_d[i - HLIT] = 0;
+ ++i;
+ }
+ } else /*if(code == INVALIDSYMBOL)*/ {
+ ERROR_BREAK(16); /*error: tried to read disallowed huffman symbol*/
+ }
+ /*check if any of the ensureBits above went out of bounds*/
+ if (reader->bp > reader->bitsize) {
+ /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol
+ (10=no endcode, 11=wrong jump outside of tree)*/
+ /* TODO: revise error codes 10,11,50: the above comment is no longer valid */
+ ERROR_BREAK(50); /*error, bit pointer jumps past memory*/
+ }
+ }
+ if (error) break;
+
+ if (bitlen_ll[256] == 0) ERROR_BREAK(64); /*the length of the end code 256 must be larger than 0*/
+
+ /*now we've finally got HLIT and HDIST, so generate the code trees, and the function is done*/
+ error = HuffmanTree_makeFromLengths(tree_ll, bitlen_ll, NUM_DEFLATE_CODE_SYMBOLS, 15);
+ if (error) break;
+ error = HuffmanTree_makeFromLengths(tree_d, bitlen_d, NUM_DISTANCE_SYMBOLS, 15);
+
+ break; /*end of error-while*/
+ }
+
+ free(bitlen_cl);
+ free(bitlen_ll);
+ free(bitlen_d);
+ HuffmanTree_cleanup(&tree_cl);
+
+ return error;
+}
+
+
+/*inflate a block with dynamic of fixed Huffman tree. btype must be 1 or 2.*/
+static unsigned inflateHuffmanBlock(ucvector* out, LodePNGBitReader* reader, unsigned btype)
+{
+ unsigned error = 0;
+ HuffmanTree tree_ll; /*the huffman tree for literal and length codes*/
+ HuffmanTree tree_d; /*the huffman tree for distance codes*/
+
+ HuffmanTree_init(&tree_ll);
+ HuffmanTree_init(&tree_d);
+
+ if (btype == 1) error = getTreeInflateFixed(&tree_ll, &tree_d);
+ else /*if(btype == 2)*/ error = getTreeInflateDynamic(&tree_ll, &tree_d, reader);
+
+ while (!error) /*decode all symbols until end reached, breaks at end code*/ {
+ /*code_ll is literal, length or end code*/
+ unsigned code_ll;
+ ensureBits25(reader, 20); /* up to 15 for the huffman symbol, up to 5 for the length extra bits */
+ code_ll = huffmanDecodeSymbol(reader, &tree_ll);
+ if (code_ll <= 255) /*literal symbol*/ {
+ if (!ucvector_resize(out, out->size + 1)) ERROR_BREAK(83 /*alloc fail*/);
+ out->data[out->size - 1] = (unsigned char)code_ll;
+ } else if (code_ll >= FIRST_LENGTH_CODE_INDEX && code_ll <= LAST_LENGTH_CODE_INDEX) /*length code*/ {
+ unsigned code_d, distance;
+ unsigned numextrabits_l, numextrabits_d; /*extra bits for length and distance*/
+ size_t start, backward, length;
+
+ /*part 1: get length base*/
+ length = LENGTHBASE[code_ll - FIRST_LENGTH_CODE_INDEX];
+
+ /*part 2: get extra bits and add the value of that to length*/
+ numextrabits_l = LENGTHEXTRA[code_ll - FIRST_LENGTH_CODE_INDEX];
+ if (numextrabits_l != 0) {
+ /* bits already ensured above */
+ length += readBits(reader, numextrabits_l);
+ }
+
+ /*part 3: get distance code*/
+ ensureBits32(reader, 28); /* up to 15 for the huffman symbol, up to 13 for the extra bits */
+ code_d = huffmanDecodeSymbol(reader, &tree_d);
+ if (code_d > 29) {
+ if (code_d <= 31) {
+ ERROR_BREAK(18); /*error: invalid distance code (30-31 are never used)*/
+ } else /* if(code_d == INVALIDSYMBOL) */{
+ ERROR_BREAK(16); /*error: tried to read disallowed huffman symbol*/
+ }
+ }
+ distance = DISTANCEBASE[code_d];
+
+ /*part 4: get extra bits from distance*/
+ numextrabits_d = DISTANCEEXTRA[code_d];
+ if (numextrabits_d != 0) {
+ /* bits already ensured above */
+ distance += readBits(reader, numextrabits_d);
+ }
+
+ /*part 5: fill in all the out[n] values based on the length and dist*/
+ start = out->size;
+ if (distance > start) ERROR_BREAK(52); /*too long backward distance*/
+ backward = start - distance;
+
+ if (!ucvector_resize(out, out->size + length)) ERROR_BREAK(83 /*alloc fail*/);
+ if (distance < length) {
+ size_t forward;
+ lodepng_memcpy(out->data + start, out->data + backward, distance);
+ start += distance;
+ for (forward = distance; forward < length; ++forward) {
+ out->data[start++] = out->data[backward++];
+ }
+ } else {
+ lodepng_memcpy(out->data + start, out->data + backward, length);
+ }
+ } else if (code_ll == 256) {
+ break; /*end code, break the loop*/
+ } else /*if(code_ll == INVALIDSYMBOL)*/ {
+ ERROR_BREAK(16); /*error: tried to read disallowed huffman symbol*/
+ }
+ /*check if any of the ensureBits above went out of bounds*/
+ if (reader->bp > reader->bitsize) {
+ /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol
+ (10=no endcode, 11=wrong jump outside of tree)*/
+ /* TODO: revise error codes 10,11,50: the above comment is no longer valid */
+ ERROR_BREAK(51); /*error, bit pointer jumps past memory*/
+ }
+ }
+
+ HuffmanTree_cleanup(&tree_ll);
+ HuffmanTree_cleanup(&tree_d);
+
+ return error;
+}
+
+
+static unsigned inflateNoCompression(ucvector* out, LodePNGBitReader* reader, const LodePNGDecompressSettings* settings)
+{
+ size_t bytepos;
+ size_t size = reader->size;
+ unsigned LEN, NLEN, error = 0;
+
+ /*go to first boundary of byte*/
+ bytepos = (reader->bp + 7u) >> 3u;
+
+ /*read LEN (2 bytes) and NLEN (2 bytes)*/
+ if (bytepos + 4 >= size) return 52; /*error, bit pointer will jump past memory*/
+ LEN = (unsigned)reader->data[bytepos] + ((unsigned)reader->data[bytepos + 1] << 8u); bytepos += 2;
+ NLEN = (unsigned)reader->data[bytepos] + ((unsigned)reader->data[bytepos + 1] << 8u); bytepos += 2;
+
+ /*check if 16-bit NLEN is really the one's complement of LEN*/
+ if (!settings->ignore_nlen && LEN + NLEN != 65535) {
+ return 21; /*error: NLEN is not one's complement of LEN*/
+ }
+
+ if (!ucvector_resize(out, out->size + LEN)) return 83; /*alloc fail*/
+
+ /*read the literal data: LEN bytes are now stored in the out buffer*/
+ if (bytepos + LEN > size) return 23; /*error: reading outside of in buffer*/
+
+ lodepng_memcpy(out->data + out->size - LEN, reader->data + bytepos, LEN);
+ bytepos += LEN;
+
+ reader->bp = bytepos << 3u;
+
+ return error;
+}
+
+
+static unsigned lodepng_inflatev(ucvector* out, const unsigned char* in, size_t insize, const LodePNGDecompressSettings* settings)
+{
+ unsigned BFINAL = 0;
+ LodePNGBitReader reader;
+ unsigned error = LodePNGBitReader_init(&reader, in, insize);
+
+ if (error) return error;
+
+ while (!BFINAL) {
+ unsigned BTYPE;
+ if (!ensureBits9(&reader, 3)) return 52; /*error, bit pointer will jump past memory*/
+ BFINAL = readBits(&reader, 1);
+ BTYPE = readBits(&reader, 2);
+
+ if (BTYPE == 3) return 20; /*error: invalid BTYPE*/
+ else if (BTYPE == 0) error = inflateNoCompression(out, &reader, settings); /*no compression*/
+ else error = inflateHuffmanBlock(out, &reader, BTYPE); /*compression, BTYPE 01 or 10*/
+
+ if (error) return error;
+ }
+
+ return error;
+}
+
+
+static unsigned inflatev(ucvector* out, const unsigned char* in, size_t insize, const LodePNGDecompressSettings* settings)
+{
+ if (settings->custom_inflate) {
+ unsigned error = settings->custom_inflate(&out->data, &out->size, in, insize, settings);
+ out->allocsize = out->size;
+ return error;
+ } else {
+ return lodepng_inflatev(out, in, insize, settings);
+ }
+}
+
+
+/* ////////////////////////////////////////////////////////////////////////// */
+/* / Adler32 / */
+/* ////////////////////////////////////////////////////////////////////////// */
+
+static unsigned update_adler32(unsigned adler, const unsigned char* data, unsigned len)
+{
+ unsigned s1 = adler & 0xffffu;
+ unsigned s2 = (adler >> 16u) & 0xffffu;
+
+ while (len != 0u) {
+ unsigned i;
+ /*at least 5552 sums can be done before the sums overflow, saving a lot of module divisions*/
+ unsigned amount = len > 5552u ? 5552u : len;
+ len -= amount;
+ for (i = 0; i != amount; ++i) {
+ s1 += (*data++);
+ s2 += s1;
+ }
+ s1 %= 65521u;
+ s2 %= 65521u;
+ }
+
+ return (s2 << 16u) | s1;
+}
+
+/*Return the adler32 of the bytes data[0..len-1]*/
+static unsigned adler32(const unsigned char* data, unsigned len)
+{
+ return update_adler32(1u, data, len);
+}
+
+/* ////////////////////////////////////////////////////////////////////////// */
+/* / Zlib / */
+/* ////////////////////////////////////////////////////////////////////////// */
+
+static unsigned lodepng_zlib_decompressv(ucvector* out, const unsigned char* in, size_t insize, const LodePNGDecompressSettings* settings)
+{
+ unsigned error = 0;
+ unsigned CM, CINFO, FDICT;
+
+ if (insize < 2) return 53; /*error, size of zlib data too small*/
+ /*read information from zlib header*/
+ if ((in[0] * 256 + in[1]) % 31 != 0) {
+ /*error: 256 * in[0] + in[1] must be a multiple of 31, the FCHECK value is supposed to be made that way*/
+ return 24;
+ }
+
+ CM = in[0] & 15;
+ CINFO = (in[0] >> 4) & 15;
+ /*FCHECK = in[1] & 31;*/ /*FCHECK is already tested above*/
+ FDICT = (in[1] >> 5) & 1;
+ /*FLEVEL = (in[1] >> 6) & 3;*/ /*FLEVEL is not used here*/
+
+ if (CM != 8 || CINFO > 7) {
+ /*error: only compression method 8: inflate with sliding window of 32k is supported by the PNG spec*/
+ return 25;
+ }
+ if (FDICT != 0) {
+ /*error: the specification of PNG says about the zlib stream:
+ "The additional flags shall not specify a preset dictionary."*/
+ return 26;
+ }
+
+ error = inflatev(out, in + 2, insize - 2, settings);
+ if (error) return error;
+
+ if (!settings->ignore_adler32) {
+ unsigned ADLER32 = lodepng_read32bitInt(&in[insize - 4]);
+ unsigned checksum = adler32(out->data, (unsigned)(out->size));
+ if(checksum != ADLER32) return 58; /*error, adler checksum not correct, data must be corrupted*/
+ }
+
+ return 0; /*no error*/
+}
+
+
+/*expected_size is expected output size, to avoid intermediate allocations. Set to 0 if not known. */
+static unsigned zlib_decompress(unsigned char** out, size_t* outsize, size_t expected_size, const unsigned char* in, size_t insize, const LodePNGDecompressSettings* settings)
+{
+ if(settings->custom_zlib) {
+ return settings->custom_zlib(out, outsize, in, insize, settings);
+ } else {
+ unsigned error;
+ ucvector v = ucvector_init(*out, *outsize);
+ if (expected_size) {
+ /*reserve the memory to avoid intermediate reallocations*/
+ ucvector_resize(&v, *outsize + expected_size);
+ v.size = *outsize;
+ }
+ error = lodepng_zlib_decompressv(&v, in, insize, settings);
+ *out = v.data;
+ *outsize = v.size;
+ return error;
+ }
+}
+
+
+static void lodepng_decompress_settings_init(LodePNGDecompressSettings* settings)
+{
+ settings->ignore_adler32 = 0;
+ settings->ignore_nlen = 0;
+ settings->custom_zlib = 0;
+ settings->custom_inflate = 0;
+ settings->custom_context = 0;
+}
+
+
+/* ////////////////////////////////////////////////////////////////////////// */
+/* ////////////////////////////////////////////////////////////////////////// */
+/* // End of Zlib related code. Begin of PNG related code. // */
+/* ////////////////////////////////////////////////////////////////////////// */
+/* ////////////////////////////////////////////////////////////////////////// */
+
+
+#if 0 //thorvg don't use crc
+/* CRC polynomial: 0xedb88320 */
+static unsigned lodepng_crc32_table[256] = {
+ 0u, 1996959894u, 3993919788u, 2567524794u, 124634137u, 1886057615u, 3915621685u, 2657392035u,
+ 249268274u, 2044508324u, 3772115230u, 2547177864u, 162941995u, 2125561021u, 3887607047u, 2428444049u,
+ 498536548u, 1789927666u, 4089016648u, 2227061214u, 450548861u, 1843258603u, 4107580753u, 2211677639u,
+ 325883990u, 1684777152u, 4251122042u, 2321926636u, 335633487u, 1661365465u, 4195302755u, 2366115317u,
+ 997073096u, 1281953886u, 3579855332u, 2724688242u, 1006888145u, 1258607687u, 3524101629u, 2768942443u,
+ 901097722u, 1119000684u, 3686517206u, 2898065728u, 853044451u, 1172266101u, 3705015759u, 2882616665u,
+ 651767980u, 1373503546u, 3369554304u, 3218104598u, 565507253u, 1454621731u, 3485111705u, 3099436303u,
+ 671266974u, 1594198024u, 3322730930u, 2970347812u, 795835527u, 1483230225u, 3244367275u, 3060149565u,
+ 1994146192u, 31158534u, 2563907772u, 4023717930u, 1907459465u, 112637215u, 2680153253u, 3904427059u,
+ 2013776290u, 251722036u, 2517215374u, 3775830040u, 2137656763u, 141376813u, 2439277719u, 3865271297u,
+ 1802195444u, 476864866u, 2238001368u, 4066508878u, 1812370925u, 453092731u, 2181625025u, 4111451223u,
+ 1706088902u, 314042704u, 2344532202u, 4240017532u, 1658658271u, 366619977u, 2362670323u, 4224994405u,
+ 1303535960u, 984961486u, 2747007092u, 3569037538u, 1256170817u, 1037604311u, 2765210733u, 3554079995u,
+ 1131014506u, 879679996u, 2909243462u, 3663771856u, 1141124467u, 855842277u, 2852801631u, 3708648649u,
+ 1342533948u, 654459306u, 3188396048u, 3373015174u, 1466479909u, 544179635u, 3110523913u, 3462522015u,
+ 1591671054u, 702138776u, 2966460450u, 3352799412u, 1504918807u, 783551873u, 3082640443u, 3233442989u,
+ 3988292384u, 2596254646u, 62317068u, 1957810842u, 3939845945u, 2647816111u, 81470997u, 1943803523u,
+ 3814918930u, 2489596804u, 225274430u, 2053790376u, 3826175755u, 2466906013u, 167816743u, 2097651377u,
+ 4027552580u, 2265490386u, 503444072u, 1762050814u, 4150417245u, 2154129355u, 426522225u, 1852507879u,
+ 4275313526u, 2312317920u, 282753626u, 1742555852u, 4189708143u, 2394877945u, 397917763u, 1622183637u,
+ 3604390888u, 2714866558u, 953729732u, 1340076626u, 3518719985u, 2797360999u, 1068828381u, 1219638859u,
+ 3624741850u, 2936675148u, 906185462u, 1090812512u, 3747672003u, 2825379669u, 829329135u, 1181335161u,
+ 3412177804u, 3160834842u, 628085408u, 1382605366u, 3423369109u, 3138078467u, 570562233u, 1426400815u,
+ 3317316542u, 2998733608u, 733239954u, 1555261956u, 3268935591u, 3050360625u, 752459403u, 1541320221u,
+ 2607071920u, 3965973030u, 1969922972u, 40735498u, 2617837225u, 3943577151u, 1913087877u, 83908371u,
+ 2512341634u, 3803740692u, 2075208622u, 213261112u, 2463272603u, 3855990285u, 2094854071u, 198958881u,
+ 2262029012u, 4057260610u, 1759359992u, 534414190u, 2176718541u, 4139329115u, 1873836001u, 414664567u,
+ 2282248934u, 4279200368u, 1711684554u, 285281116u, 2405801727u, 4167216745u, 1634467795u, 376229701u,
+ 2685067896u, 3608007406u, 1308918612u, 956543938u, 2808555105u, 3495958263u, 1231636301u, 1047427035u,
+ 2932959818u, 3654703836u, 1088359270u, 936918000u, 2847714899u, 3736837829u, 1202900863u, 817233897u,
+ 3183342108u, 3401237130u, 1404277552u, 615818150u, 3134207493u, 3453421203u, 1423857449u, 601450431u,
+ 3009837614u, 3294710456u, 1567103746u, 711928724u, 3020668471u, 3272380065u, 1510334235u, 755167117u
+};
+
+
+/* Calculate CRC32 of buffer
+ Return the CRC of the bytes buf[0..len-1]. */
+static unsigned lodepng_crc32(const unsigned char* data, size_t length)
+{
+ unsigned r = 0xffffffffu;
+ size_t i;
+ for (i = 0; i < length; ++i) {
+ r = lodepng_crc32_table[(r ^ data[i]) & 0xffu] ^ (r >> 8u);
+ }
+ return r ^ 0xffffffffu;
+}
+#endif
+
+/* ////////////////////////////////////////////////////////////////////////// */
+/* / Reading and writing PNG color channel bits / */
+/* ////////////////////////////////////////////////////////////////////////// */
+
+/* The color channel bits of less-than-8-bit pixels are read with the MSB of bytes first,
+so LodePNGBitWriter and LodePNGBitReader can't be used for those. */
+
+static unsigned char readBitFromReversedStream(size_t* bitpointer, const unsigned char* bitstream)
+{
+ unsigned char result = (unsigned char)((bitstream[(*bitpointer) >> 3] >> (7 - ((*bitpointer) & 0x7))) & 1);
+ ++(*bitpointer);
+ return result;
+}
+
+
+/* TODO: make this faster */
+static unsigned readBitsFromReversedStream(size_t* bitpointer, const unsigned char* bitstream, size_t nbits)
+{
+ unsigned result = 0;
+ size_t i;
+ for (i = 0 ; i < nbits; ++i) {
+ result <<= 1u;
+ result |= (unsigned)readBitFromReversedStream(bitpointer, bitstream);
+ }
+ return result;
+}
+
+
+static void setBitOfReversedStream(size_t* bitpointer, unsigned char* bitstream, unsigned char bit)
+{
+ /*the current bit in bitstream may be 0 or 1 for this to work*/
+ if (bit == 0) bitstream[(*bitpointer) >> 3u] &= (unsigned char)(~(1u << (7u - ((*bitpointer) & 7u))));
+ else bitstream[(*bitpointer) >> 3u] |= (1u << (7u - ((*bitpointer) & 7u)));
+ ++(*bitpointer);
+}
+
+/* ////////////////////////////////////////////////////////////////////////// */
+/* / PNG chunks / */
+/* ////////////////////////////////////////////////////////////////////////// */
+
+/*
+ The lodepng_chunk functions are normally not needed, except to traverse the
+ unknown chunks stored in the LodePNGInfo struct, or add new ones to it.
+ It also allows traversing the chunks of an encoded PNG file yourself.
+
+ The chunk pointer always points to the beginning of the chunk itself, that is
+ the first byte of the 4 length bytes.
+
+ In the PNG file format, chunks have the following format:
+ -4 bytes length: length of the data of the chunk in bytes (chunk itself is 12 bytes longer)
+ -4 bytes chunk type (ASCII a-z,A-Z only, see below)
+ -length bytes of data (may be 0 bytes if length was 0)
+ -4 bytes of CRC, computed on chunk name + data
+
+ The first chunk starts at the 8th byte of the PNG file, the entire rest of the file
+ exists out of concatenated chunks with the above format.
+
+ PNG standard chunk ASCII naming conventions:
+ -First byte: uppercase = critical, lowercase = ancillary
+ -Second byte: uppercase = public, lowercase = private
+ -Third byte: must be uppercase
+ -Fourth byte: uppercase = unsafe to copy, lowercase = safe to copy
+*/
+
+
+/*
+ Gets the length of the data of the chunk. Total chunk length has 12 bytes more.
+ There must be at least 4 bytes to read from. If the result value is too large,
+ it may be corrupt data.
+*/
+static unsigned lodepng_chunk_length(const unsigned char* chunk)
+{
+ return lodepng_read32bitInt(&chunk[0]);
+}
+
+
+/* check if the type is the given type */
+static unsigned char lodepng_chunk_type_equals(const unsigned char* chunk, const char* type)
+{
+ if (lodepng_strlen(type) != 4) return 0;
+ return (chunk[4] == type[0] && chunk[5] == type[1] && chunk[6] == type[2] && chunk[7] == type[3]);
+}
+
+
+/* 0: it's one of the critical chunk types, 1: it's an ancillary chunk (see PNG standard) */
+static unsigned char lodepng_chunk_ancillary(const unsigned char* chunk)
+{
+ return ((chunk[4] & 32) != 0);
+}
+
+
+static const unsigned char* lodepng_chunk_data_const(const unsigned char* chunk)
+{
+ return &chunk[8];
+}
+
+#if 0 //thorvg don't use crc
+/* returns 0 if the crc is correct, 1 if it's incorrect (0 for OK as usual!) */
+static unsigned lodepng_chunk_check_crc(const unsigned char* chunk)
+{
+ unsigned length = lodepng_chunk_length(chunk);
+ unsigned CRC = lodepng_read32bitInt(&chunk[length + 8]);
+ /*the CRC is taken of the data and the 4 chunk type letters, not the length*/
+ unsigned checksum = lodepng_crc32(&chunk[4], length + 4);
+ if (CRC != checksum) return 1;
+ else return 0;
+}
+#endif
+
+static const unsigned char* lodepng_chunk_next_const(const unsigned char* chunk, const unsigned char* end)
+{
+ if (chunk >= end || end - chunk < 12) return end; /*too small to contain a chunk*/
+ if (chunk[0] == 0x89 && chunk[1] == 0x50 && chunk[2] == 0x4e && chunk[3] == 0x47
+ && chunk[4] == 0x0d && chunk[5] == 0x0a && chunk[6] == 0x1a && chunk[7] == 0x0a) {
+ /* Is PNG magic header at start of PNG file. Jump to first actual chunk. */
+ return chunk + 8;
+ } else {
+ size_t total_chunk_length;
+ const unsigned char* result;
+ if (lodepng_addofl(lodepng_chunk_length(chunk), 12, &total_chunk_length)) return end;
+ result = chunk + total_chunk_length;
+ if (result < chunk) return end; /*pointer overflow*/
+ return result;
+ }
+}
+
+
+/* ////////////////////////////////////////////////////////////////////////// */
+/* / Color types, channels, bits / */
+/* ////////////////////////////////////////////////////////////////////////// */
+
+/*checks if the colortype is valid and the bitdepth bd is allowed for this colortype.
+Return value is a LodePNG error code.*/
+static unsigned checkColorValidity(LodePNGColorType colortype, unsigned bd)
+{
+ switch(colortype) {
+ case LCT_GREY: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 || bd == 16)) return 37; break;
+ case LCT_RGB: if(!( bd == 8 || bd == 16)) return 37; break;
+ case LCT_PALETTE: if(!(bd == 1 || bd == 2 || bd == 4 || bd == 8 )) return 37; break;
+ case LCT_GREY_ALPHA: if(!( bd == 8 || bd == 16)) return 37; break;
+ case LCT_RGBA: if(!( bd == 8 || bd == 16)) return 37; break;
+ case LCT_MAX_OCTET_VALUE: return 31; /* invalid color type */
+ default: return 31; /* invalid color type */
+ }
+ return 0; /*allowed color type / bits combination*/
+}
+
+
+static unsigned getNumColorChannels(LodePNGColorType colortype)
+{
+ switch(colortype) {
+ case LCT_GREY: return 1;
+ case LCT_RGB: return 3;
+ case LCT_PALETTE: return 1;
+ case LCT_GREY_ALPHA: return 2;
+ case LCT_RGBA: return 4;
+ case LCT_MAX_OCTET_VALUE: return 0; /* invalid color type */
+ default: return 0; /*invalid color type*/
+ }
+}
+
+
+static unsigned lodepng_get_bpp_lct(LodePNGColorType colortype, unsigned bitdepth)
+{
+ /*bits per pixel is amount of channels * bits per channel*/
+ return getNumColorChannels(colortype) * bitdepth;
+}
+
+
+static void lodepng_color_mode_init(LodePNGColorMode* info)
+{
+ info->key_defined = 0;
+ info->key_r = info->key_g = info->key_b = 0;
+ info->colortype = LCT_RGBA;
+ info->bitdepth = 8;
+ info->palette = 0;
+ info->palettesize = 0;
+}
+
+
+/*allocates palette memory if needed, and initializes all colors to black*/
+static void lodepng_color_mode_alloc_palette(LodePNGColorMode* info)
+{
+ size_t i;
+ /*if the palette is already allocated, it will have size 1024 so no reallocation needed in that case*/
+ /*the palette must have room for up to 256 colors with 4 bytes each.*/
+ if (!info->palette) info->palette = (unsigned char*)malloc(1024);
+ if (!info->palette) return; /*alloc fail*/
+ for (i = 0; i != 256; ++i) {
+ /*Initialize all unused colors with black, the value used for invalid palette indices.
+ This is an error according to the PNG spec, but common PNG decoders make it black instead.
+ That makes color conversion slightly faster due to no error handling needed.*/
+ info->palette[i * 4 + 0] = 0;
+ info->palette[i * 4 + 1] = 0;
+ info->palette[i * 4 + 2] = 0;
+ info->palette[i * 4 + 3] = 255;
+ }
+}
+
+static void lodepng_palette_clear(LodePNGColorMode* info)
+{
+ if (info->palette) free(info->palette);
+ info->palette = 0;
+ info->palettesize = 0;
+}
+
+
+static void lodepng_color_mode_cleanup(LodePNGColorMode* info)
+{
+ lodepng_palette_clear(info);
+}
+
+
+/*return value is error code (0 means no error)*/
+static unsigned lodepng_color_mode_copy(LodePNGColorMode* dest, const LodePNGColorMode* source)
+{
+ lodepng_color_mode_cleanup(dest);
+ lodepng_memcpy(dest, source, sizeof(LodePNGColorMode));
+ if (source->palette) {
+ dest->palette = (unsigned char*)malloc(1024);
+ if (!dest->palette && source->palettesize) return 83; /*alloc fail*/
+ lodepng_memcpy(dest->palette, source->palette, source->palettesize * 4);
+ }
+ return 0;
+}
+
+
+static int lodepng_color_mode_equal(const LodePNGColorMode* a, const LodePNGColorMode* b)
+{
+ size_t i;
+ if (a->colortype != b->colortype) return 0;
+ if (a->bitdepth != b->bitdepth) return 0;
+ if (a->key_defined != b->key_defined) return 0;
+ if (a->key_defined) {
+ if(a->key_r != b->key_r) return 0;
+ if(a->key_g != b->key_g) return 0;
+ if(a->key_b != b->key_b) return 0;
+ }
+ if (a->palettesize != b->palettesize) return 0;
+ for (i = 0; i != a->palettesize * 4; ++i) {
+ if (a->palette[i] != b->palette[i]) return 0;
+ }
+ return 1;
+}
+
+
+static size_t lodepng_get_raw_size_lct(unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth)
+{
+ size_t bpp = lodepng_get_bpp_lct(colortype, bitdepth);
+ size_t n = (size_t)w * (size_t)h;
+ return ((n / 8u) * bpp) + ((n & 7u) * bpp + 7u) / 8u;
+}
+
+
+/* Returns the byte size of a raw image buffer with given width, height and color mode */
+static size_t lodepng_get_raw_size(unsigned w, unsigned h, const LodePNGColorMode* color)
+{
+ return lodepng_get_raw_size_lct(w, h, color->colortype, color->bitdepth);
+}
+
+
+/*in an idat chunk, each scanline is a multiple of 8 bits, unlike the lodepng output buffer,
+and in addition has one extra byte per line: the filter byte. So this gives a larger
+result than lodepng_get_raw_size. Set h to 1 to get the size of 1 row including filter byte. */
+static size_t lodepng_get_raw_size_idat(unsigned w, unsigned h, unsigned bpp)
+{
+ /* + 1 for the filter byte, and possibly plus padding bits per line. */
+ /* Ignoring casts, the expression is equal to (w * bpp + 7) / 8 + 1, but avoids overflow of w * bpp */
+ size_t line = ((size_t)(w / 8u) * bpp) + 1u + ((w & 7u) * bpp + 7u) / 8u;
+ return (size_t)h * line;
+}
+
+
+/* Safely checks whether size_t overflow can be caused due to amount of pixels.
+ This check is overcautious rather than precise. If this check indicates no overflow,
+ you can safely compute in a size_t (but not an unsigned):
+ -(size_t)w * (size_t)h * 8
+ -amount of bytes in IDAT (including filter, padding and Adam7 bytes)
+ -amount of bytes in raw color model
+ Returns 1 if overflow possible, 0 if not. */
+static int lodepng_pixel_overflow(unsigned w, unsigned h, const LodePNGColorMode* pngcolor, const LodePNGColorMode* rawcolor)
+{
+ size_t bpp = LODEPNG_MAX(lodepng_get_bpp_lct(pngcolor->colortype, pngcolor->bitdepth), lodepng_get_bpp_lct(rawcolor->colortype, rawcolor->bitdepth));
+ size_t numpixels, total;
+ size_t line; /* bytes per line in worst case */
+
+ if (lodepng_mulofl((size_t)w, (size_t)h, &numpixels)) return 1;
+ if (lodepng_mulofl(numpixels, 8, &total)) return 1; /* bit pointer with 8-bit color, or 8 bytes per channel color */
+
+ /* Bytes per scanline with the expression "(w / 8u) * bpp) + ((w & 7u) * bpp + 7u) / 8u" */
+ if (lodepng_mulofl((size_t)(w / 8u), bpp, &line)) return 1;
+ if (lodepng_addofl(line, ((w & 7u) * bpp + 7u) / 8u, &line)) return 1;
+
+ if (lodepng_addofl(line, 5, &line)) return 1; /* 5 bytes overhead per line: 1 filterbyte, 4 for Adam7 worst case */
+ if (lodepng_mulofl(line, h, &total)) return 1; /* Total bytes in worst case */
+
+ return 0; /* no overflow */
+}
+
+
+static void lodepng_info_init(LodePNGInfo* info)
+{
+ lodepng_color_mode_init(&info->color);
+ info->interlace_method = 0;
+ info->compression_method = 0;
+ info->filter_method = 0;
+}
+
+
+static void lodepng_info_cleanup(LodePNGInfo* info)
+{
+ lodepng_color_mode_cleanup(&info->color);
+}
+
+
+/* index: bitgroup index, bits: bitgroup size(1, 2 or 4), in: bitgroup value, out: octet array to add bits to */
+static void addColorBits(unsigned char* out, size_t index, unsigned bits, unsigned in)
+{
+ unsigned m = bits == 1 ? 7 : bits == 2 ? 3 : 1; /*8 / bits - 1*/
+ /*p = the partial index in the byte, e.g. with 4 palettebits it is 0 for first half or 1 for second half*/
+ unsigned p = index & m;
+ in &= (1u << bits) - 1u; /*filter out any other bits of the input value*/
+ in = in << (bits * (m - p));
+ if(p == 0) out[index * bits / 8u] = in;
+ else out[index * bits / 8u] |= in;
+}
+
+/*
+ One node of a color tree
+ This is the data structure used to count the number of unique colors and to get a palette
+ index for a color. It's like an octree, but because the alpha channel is used too, each
+ node has 16 instead of 8 children.
+*/
+struct ColorTree
+{
+ ColorTree* children[16]; /* up to 16 pointers to ColorTree of next level */
+ int index; /* the payload. Only has a meaningful value if this is in the last level */
+};
+
+static void color_tree_init(ColorTree* tree)
+{
+ lodepng_memset(tree->children, 0, 16 * sizeof(*tree->children));
+ tree->index = -1;
+}
+
+static void color_tree_cleanup(ColorTree* tree)
+{
+ int i;
+ for (i = 0; i != 16; ++i) {
+ if(tree->children[i]) {
+ color_tree_cleanup(tree->children[i]);
+ free(tree->children[i]);
+ }
+ }
+}
+
+
+/* returns -1 if color not present, its index otherwise */
+static int color_tree_get(ColorTree* tree, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
+{
+ int bit = 0;
+ for (bit = 0; bit < 8; ++bit) {
+ int i = 8 * ((r >> bit) & 1) + 4 * ((g >> bit) & 1) + 2 * ((b >> bit) & 1) + 1 * ((a >> bit) & 1);
+ if (!tree->children[i]) return -1;
+ else tree = tree->children[i];
+ }
+ return tree ? tree->index : -1;
+}
+
+
+/* color is not allowed to already exist.
+ Index should be >= 0 (it's signed to be compatible with using -1 for "doesn't exist")
+ Returns error code, or 0 if ok */
+static unsigned color_tree_add(ColorTree* tree, unsigned char r, unsigned char g, unsigned char b, unsigned char a, unsigned index)
+{
+ int bit;
+ for (bit = 0; bit < 8; ++bit) {
+ int i = 8 * ((r >> bit) & 1) + 4 * ((g >> bit) & 1) + 2 * ((b >> bit) & 1) + 1 * ((a >> bit) & 1);
+ if (!tree->children[i]) {
+ tree->children[i] = (ColorTree*)malloc(sizeof(ColorTree));
+ if (!tree->children[i]) return 83; /*alloc fail*/
+ color_tree_init(tree->children[i]);
+ }
+ tree = tree->children[i];
+ }
+ tree->index = (int)index;
+ return 0;
+}
+
+/* put a pixel, given its RGBA color, into image of any color type */
+static unsigned rgba8ToPixel(unsigned char* out, size_t i, const LodePNGColorMode* mode, ColorTree* tree /*for palette*/, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
+{
+ if (mode->colortype == LCT_GREY) {
+ unsigned char gray = r; /*((unsigned short)r + g + b) / 3u;*/
+ if (mode->bitdepth == 8) out[i] = gray;
+ else if (mode->bitdepth == 16) out[i * 2 + 0] = out[i * 2 + 1] = gray;
+ else {
+ /*take the most significant bits of gray*/
+ gray = ((unsigned)gray >> (8u - mode->bitdepth)) & ((1u << mode->bitdepth) - 1u);
+ addColorBits(out, i, mode->bitdepth, gray);
+ }
+ } else if (mode->colortype == LCT_RGB) {
+ if (mode->bitdepth == 8) {
+ out[i * 3 + 0] = r;
+ out[i * 3 + 1] = g;
+ out[i * 3 + 2] = b;
+ } else {
+ out[i * 6 + 0] = out[i * 6 + 1] = r;
+ out[i * 6 + 2] = out[i * 6 + 3] = g;
+ out[i * 6 + 4] = out[i * 6 + 5] = b;
+ }
+ } else if(mode->colortype == LCT_PALETTE) {
+ int index = color_tree_get(tree, r, g, b, a);
+ if (index < 0) return 82; /*color not in palette*/
+ if (mode->bitdepth == 8) out[i] = index;
+ else addColorBits(out, i, mode->bitdepth, (unsigned)index);
+ } else if (mode->colortype == LCT_GREY_ALPHA) {
+ unsigned char gray = r; /*((unsigned short)r + g + b) / 3u;*/
+ if (mode->bitdepth == 8) {
+ out[i * 2 + 0] = gray;
+ out[i * 2 + 1] = a;
+ } else if (mode->bitdepth == 16) {
+ out[i * 4 + 0] = out[i * 4 + 1] = gray;
+ out[i * 4 + 2] = out[i * 4 + 3] = a;
+ }
+ } else if (mode->colortype == LCT_RGBA) {
+ if (mode->bitdepth == 8) {
+ out[i * 4 + 0] = r;
+ out[i * 4 + 1] = g;
+ out[i * 4 + 2] = b;
+ out[i * 4 + 3] = a;
+ } else {
+ out[i * 8 + 0] = out[i * 8 + 1] = r;
+ out[i * 8 + 2] = out[i * 8 + 3] = g;
+ out[i * 8 + 4] = out[i * 8 + 5] = b;
+ out[i * 8 + 6] = out[i * 8 + 7] = a;
+ }
+ }
+ return 0; /*no error*/
+}
+
+
+/* put a pixel, given its RGBA16 color, into image of any color 16-bitdepth type */
+static void rgba16ToPixel(unsigned char* out, size_t i, const LodePNGColorMode* mode, unsigned short r, unsigned short g, unsigned short b, unsigned short a)
+{
+ if (mode->colortype == LCT_GREY) {
+ unsigned short gray = r; /*((unsigned)r + g + b) / 3u;*/
+ out[i * 2 + 0] = (gray >> 8) & 255;
+ out[i * 2 + 1] = gray & 255;
+ } else if (mode->colortype == LCT_RGB) {
+ out[i * 6 + 0] = (r >> 8) & 255;
+ out[i * 6 + 1] = r & 255;
+ out[i * 6 + 2] = (g >> 8) & 255;
+ out[i * 6 + 3] = g & 255;
+ out[i * 6 + 4] = (b >> 8) & 255;
+ out[i * 6 + 5] = b & 255;
+ } else if (mode->colortype == LCT_GREY_ALPHA) {
+ unsigned short gray = r; /*((unsigned)r + g + b) / 3u;*/
+ out[i * 4 + 0] = (gray >> 8) & 255;
+ out[i * 4 + 1] = gray & 255;
+ out[i * 4 + 2] = (a >> 8) & 255;
+ out[i * 4 + 3] = a & 255;
+ } else if (mode->colortype == LCT_RGBA) {
+ out[i * 8 + 0] = (r >> 8) & 255;
+ out[i * 8 + 1] = r & 255;
+ out[i * 8 + 2] = (g >> 8) & 255;
+ out[i * 8 + 3] = g & 255;
+ out[i * 8 + 4] = (b >> 8) & 255;
+ out[i * 8 + 5] = b & 255;
+ out[i * 8 + 6] = (a >> 8) & 255;
+ out[i * 8 + 7] = a & 255;
+ }
+}
+
+
+/* Get RGBA8 color of pixel with index i (y * width + x) from the raw image with given color type. */
+static void getPixelColorRGBA8(unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a, const unsigned char* in, size_t i, const LodePNGColorMode* mode)
+{
+ if (mode->colortype == LCT_GREY) {
+ if (mode->bitdepth == 8) {
+ *r = *g = *b = in[i];
+ if (mode->key_defined && *r == mode->key_r) *a = 0;
+ else *a = 255;
+ } else if (mode->bitdepth == 16) {
+ *r = *g = *b = in[i * 2 + 0];
+ if (mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r) *a = 0;
+ else *a = 255;
+ } else {
+ unsigned highest = ((1U << mode->bitdepth) - 1U); /* highest possible value for this bit depth */
+ size_t j = i * mode->bitdepth;
+ unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth);
+ *r = *g = *b = (value * 255) / highest;
+ if (mode->key_defined && value == mode->key_r) *a = 0;
+ else *a = 255;
+ }
+ } else if (mode->colortype == LCT_RGB) {
+ if (mode->bitdepth == 8) {
+ *r = in[i * 3 + 0]; *g = in[i * 3 + 1]; *b = in[i * 3 + 2];
+ if (mode->key_defined && *r == mode->key_r && *g == mode->key_g && *b == mode->key_b) *a = 0;
+ else *a = 255;
+ } else {
+ *r = in[i * 6 + 0];
+ *g = in[i * 6 + 2];
+ *b = in[i * 6 + 4];
+ if (mode->key_defined && 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r
+ && 256U * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g
+ && 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b) *a = 0;
+ else *a = 255;
+ }
+ } else if (mode->colortype == LCT_PALETTE) {
+ unsigned index;
+ if (mode->bitdepth == 8) index = in[i];
+ else {
+ size_t j = i * mode->bitdepth;
+ index = readBitsFromReversedStream(&j, in, mode->bitdepth);
+ }
+ /* out of bounds of palette not checked: see lodepng_color_mode_alloc_palette. */
+ *r = mode->palette[index * 4 + 0];
+ *g = mode->palette[index * 4 + 1];
+ *b = mode->palette[index * 4 + 2];
+ *a = mode->palette[index * 4 + 3];
+ } else if (mode->colortype == LCT_GREY_ALPHA) {
+ if (mode->bitdepth == 8) {
+ *r = *g = *b = in[i * 2 + 0];
+ *a = in[i * 2 + 1];
+ } else {
+ *r = *g = *b = in[i * 4 + 0];
+ *a = in[i * 4 + 2];
+ }
+ } else if (mode->colortype == LCT_RGBA) {
+ if (mode->bitdepth == 8) {
+ *r = in[i * 4 + 0];
+ *g = in[i * 4 + 1];
+ *b = in[i * 4 + 2];
+ *a = in[i * 4 + 3];
+ } else {
+ *r = in[i * 8 + 0];
+ *g = in[i * 8 + 2];
+ *b = in[i * 8 + 4];
+ *a = in[i * 8 + 6];
+ }
+ }
+}
+
+
+/* Similar to getPixelColorRGBA8, but with all the for loops inside of the color
+ mode test cases, optimized to convert the colors much faster, when converting
+ to the common case of RGBA with 8 bit per channel. buffer must be RGBA with
+ enough memory.*/
+static void getPixelColorsRGBA8(unsigned char* LODEPNG_RESTRICT buffer, size_t numpixels, const unsigned char* LODEPNG_RESTRICT in, const LodePNGColorMode* mode)
+{
+ unsigned num_channels = 4;
+ size_t i;
+ if (mode->colortype == LCT_GREY) {
+ if (mode->bitdepth == 8) {
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ buffer[0] = buffer[1] = buffer[2] = in[i];
+ buffer[3] = 255;
+ }
+ if (mode->key_defined) {
+ buffer -= numpixels * num_channels;
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ if(buffer[0] == mode->key_r) buffer[3] = 0;
+ }
+ }
+ } else if (mode->bitdepth == 16) {
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ buffer[0] = buffer[1] = buffer[2] = in[i * 2];
+ buffer[3] = mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r ? 0 : 255;
+ }
+ } else {
+ unsigned highest = ((1U << mode->bitdepth) - 1U); /* highest possible value for this bit depth */
+ size_t j = 0;
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth);
+ buffer[0] = buffer[1] = buffer[2] = (value * 255) / highest;
+ buffer[3] = mode->key_defined && value == mode->key_r ? 0 : 255;
+ }
+ }
+ } else if (mode->colortype == LCT_RGB) {
+ if (mode->bitdepth == 8) {
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ lodepng_memcpy(buffer, &in[i * 3], 3);
+ buffer[3] = 255;
+ }
+ if (mode->key_defined) {
+ buffer -= numpixels * num_channels;
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ if (buffer[0] == mode->key_r && buffer[1]== mode->key_g && buffer[2] == mode->key_b) buffer[3] = 0;
+ }
+ }
+ } else {
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ buffer[0] = in[i * 6 + 0];
+ buffer[1] = in[i * 6 + 2];
+ buffer[2] = in[i * 6 + 4];
+ buffer[3] = mode->key_defined
+ && 256U * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r
+ && 256U * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g
+ && 256U * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b ? 0 : 255;
+ }
+ }
+ } else if (mode->colortype == LCT_PALETTE) {
+ if (mode->bitdepth == 8) {
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ unsigned index = in[i];
+ /* out of bounds of palette not checked: see lodepng_color_mode_alloc_palette. */
+ lodepng_memcpy(buffer, &mode->palette[index * 4], 4);
+ }
+ } else {
+ size_t j = 0;
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ unsigned index = readBitsFromReversedStream(&j, in, mode->bitdepth);
+ /* out of bounds of palette not checked: see lodepng_color_mode_alloc_palette. */
+ lodepng_memcpy(buffer, &mode->palette[index * 4], 4);
+ }
+ }
+ } else if (mode->colortype == LCT_GREY_ALPHA) {
+ if (mode->bitdepth == 8) {
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ buffer[0] = buffer[1] = buffer[2] = in[i * 2 + 0];
+ buffer[3] = in[i * 2 + 1];
+ }
+ } else {
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ buffer[0] = buffer[1] = buffer[2] = in[i * 4 + 0];
+ buffer[3] = in[i * 4 + 2];
+ }
+ }
+ } else if (mode->colortype == LCT_RGBA) {
+ if (mode->bitdepth == 8) {
+ lodepng_memcpy(buffer, in, numpixels * 4);
+ } else {
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ buffer[0] = in[i * 8 + 0];
+ buffer[1] = in[i * 8 + 2];
+ buffer[2] = in[i * 8 + 4];
+ buffer[3] = in[i * 8 + 6];
+ }
+ }
+ }
+}
+
+
+/* Similar to getPixelColorsRGBA8, but with 3-channel RGB output. */
+static void getPixelColorsRGB8(unsigned char* LODEPNG_RESTRICT buffer, size_t numpixels, const unsigned char* LODEPNG_RESTRICT in, const LodePNGColorMode* mode)
+{
+ const unsigned num_channels = 3;
+ size_t i;
+ if (mode->colortype == LCT_GREY) {
+ if (mode->bitdepth == 8) {
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ buffer[0] = buffer[1] = buffer[2] = in[i];
+ }
+ } else if (mode->bitdepth == 16) {
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ buffer[0] = buffer[1] = buffer[2] = in[i * 2];
+ }
+ } else {
+ unsigned highest = ((1U << mode->bitdepth) - 1U); /* highest possible value for this bit depth */
+ size_t j = 0;
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ unsigned value = readBitsFromReversedStream(&j, in, mode->bitdepth);
+ buffer[0] = buffer[1] = buffer[2] = (value * 255) / highest;
+ }
+ }
+ } else if (mode->colortype == LCT_RGB) {
+ if (mode->bitdepth == 8) {
+ lodepng_memcpy(buffer, in, numpixels * 3);
+ } else {
+ for(i = 0; i != numpixels; ++i, buffer += num_channels) {
+ buffer[0] = in[i * 6 + 0];
+ buffer[1] = in[i * 6 + 2];
+ buffer[2] = in[i * 6 + 4];
+ }
+ }
+ } else if (mode->colortype == LCT_PALETTE) {
+ if (mode->bitdepth == 8) {
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ unsigned index = in[i];
+ /* out of bounds of palette not checked: see lodepng_color_mode_alloc_palette. */
+ lodepng_memcpy(buffer, &mode->palette[index * 4], 3);
+ }
+ } else {
+ size_t j = 0;
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ unsigned index = readBitsFromReversedStream(&j, in, mode->bitdepth);
+ /* out of bounds of palette not checked: see lodepng_color_mode_alloc_palette. */
+ lodepng_memcpy(buffer, &mode->palette[index * 4], 3);
+ }
+ }
+ } else if (mode->colortype == LCT_GREY_ALPHA) {
+ if (mode->bitdepth == 8) {
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ buffer[0] = buffer[1] = buffer[2] = in[i * 2 + 0];
+ }
+ } else {
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ buffer[0] = buffer[1] = buffer[2] = in[i * 4 + 0];
+ }
+ }
+ } else if (mode->colortype == LCT_RGBA) {
+ if (mode->bitdepth == 8) {
+ for(i = 0; i != numpixels; ++i, buffer += num_channels) {
+ lodepng_memcpy(buffer, &in[i * 4], 3);
+ }
+ } else {
+ for (i = 0; i != numpixels; ++i, buffer += num_channels) {
+ buffer[0] = in[i * 8 + 0];
+ buffer[1] = in[i * 8 + 2];
+ buffer[2] = in[i * 8 + 4];
+ }
+ }
+ }
+}
+
+
+/* Get RGBA16 color of pixel with index i (y * width + x) from the raw image with
+ given color type, but the given color type must be 16-bit itself. */
+static void getPixelColorRGBA16(unsigned short* r, unsigned short* g, unsigned short* b, unsigned short* a, const unsigned char* in, size_t i, const LodePNGColorMode* mode)
+{
+ if (mode->colortype == LCT_GREY) {
+ *r = *g = *b = 256 * in[i * 2 + 0] + in[i * 2 + 1];
+ if (mode->key_defined && 256U * in[i * 2 + 0] + in[i * 2 + 1] == mode->key_r) *a = 0;
+ else *a = 65535;
+ } else if (mode->colortype == LCT_RGB) {
+ *r = 256u * in[i * 6 + 0] + in[i * 6 + 1];
+ *g = 256u * in[i * 6 + 2] + in[i * 6 + 3];
+ *b = 256u * in[i * 6 + 4] + in[i * 6 + 5];
+ if (mode->key_defined
+ && 256u * in[i * 6 + 0] + in[i * 6 + 1] == mode->key_r
+ && 256u * in[i * 6 + 2] + in[i * 6 + 3] == mode->key_g
+ && 256u * in[i * 6 + 4] + in[i * 6 + 5] == mode->key_b) *a = 0;
+ else *a = 65535;
+ } else if (mode->colortype == LCT_GREY_ALPHA) {
+ *r = *g = *b = 256u * in[i * 4 + 0] + in[i * 4 + 1];
+ *a = 256u * in[i * 4 + 2] + in[i * 4 + 3];
+ } else if (mode->colortype == LCT_RGBA) {
+ *r = 256u * in[i * 8 + 0] + in[i * 8 + 1];
+ *g = 256u * in[i * 8 + 2] + in[i * 8 + 3];
+ *b = 256u * in[i * 8 + 4] + in[i * 8 + 5];
+ *a = 256u * in[i * 8 + 6] + in[i * 8 + 7];
+ }
+}
+
+/*
+ Converts raw buffer from one color type to another color type, based on
+ LodePNGColorMode structs to describe the input and output color type.
+ See the reference manual at the end of this header file to see which color conversions are supported.
+ return value = LodePNG error code (0 if all went ok, an error if the conversion isn't supported)
+ The out buffer must have size (w * h * bpp + 7) / 8, where bpp is the bits per pixel
+ of the output color type (lodepng_get_bpp).
+ For < 8 bpp images, there should not be padding bits at the end of scanlines.
+ For 16-bit per channel colors, uses big endian format like PNG does.
+ Return value is LodePNG error code
+*/
+static unsigned lodepng_convert(unsigned char* out, const unsigned char* in, const LodePNGColorMode* mode_out, const LodePNGColorMode* mode_in, unsigned w, unsigned h)
+{
+ size_t i;
+ ColorTree tree;
+ size_t numpixels = (size_t)w * (size_t)h;
+ unsigned error = 0;
+
+ if (mode_in->colortype == LCT_PALETTE && !mode_in->palette) {
+ return 107; /* error: must provide palette if input mode is palette */
+ }
+
+ if (lodepng_color_mode_equal(mode_out, mode_in)) {
+ size_t numbytes = lodepng_get_raw_size(w, h, mode_in);
+ lodepng_memcpy(out, in, numbytes);
+ return 0;
+ }
+
+ if (mode_out->colortype == LCT_PALETTE) {
+ size_t palettesize = mode_out->palettesize;
+ const unsigned char* palette = mode_out->palette;
+ size_t palsize = (size_t)1u << mode_out->bitdepth;
+ /* if the user specified output palette but did not give the values, assume
+ they want the values of the input color type (assuming that one is palette).
+ Note that we never create a new palette ourselves.*/
+ if (palettesize == 0) {
+ palettesize = mode_in->palettesize;
+ palette = mode_in->palette;
+ /* if the input was also palette with same bitdepth, then the color types are also
+ equal, so copy literally. This to preserve the exact indices that were in the PNG
+ even in case there are duplicate colors in the palette.*/
+ if (mode_in->colortype == LCT_PALETTE && mode_in->bitdepth == mode_out->bitdepth) {
+ size_t numbytes = lodepng_get_raw_size(w, h, mode_in);
+ lodepng_memcpy(out, in, numbytes);
+ return 0;
+ }
+ }
+ if (palettesize < palsize) palsize = palettesize;
+ color_tree_init(&tree);
+ for (i = 0; i != palsize; ++i) {
+ const unsigned char* p = &palette[i * 4];
+ error = color_tree_add(&tree, p[0], p[1], p[2], p[3], (unsigned)i);
+ if (error) break;
+ }
+ }
+
+ if (!error) {
+ if (mode_in->bitdepth == 16 && mode_out->bitdepth == 16) {
+ for (i = 0; i != numpixels; ++i) {
+ unsigned short r = 0, g = 0, b = 0, a = 0;
+ getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in);
+ rgba16ToPixel(out, i, mode_out, r, g, b, a);
+ }
+ } else if (mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGBA) {
+ getPixelColorsRGBA8(out, numpixels, in, mode_in);
+ } else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGB) {
+ getPixelColorsRGB8(out, numpixels, in, mode_in);
+ } else {
+ unsigned char r = 0, g = 0, b = 0, a = 0;
+ for (i = 0; i != numpixels; ++i) {
+ getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in);
+ error = rgba8ToPixel(out, i, mode_out, &tree, r, g, b, a);
+ if (error) break;
+ }
+ }
+ }
+
+ if (mode_out->colortype == LCT_PALETTE) {
+ color_tree_cleanup(&tree);
+ }
+
+ return error;
+}
+
+
+/* Paeth predictor, used by PNG filter type 4
+ The parameters are of type short, but should come from unsigned chars, the shorts
+ are only needed to make the paeth calculation correct.
+*/
+static unsigned char paethPredictor(short a, short b, short c)
+{
+ short pa = LODEPNG_ABS(b - c);
+ short pb = LODEPNG_ABS(a - c);
+ short pc = LODEPNG_ABS(a + b - c - c);
+ /* return input value associated with smallest of pa, pb, pc (with certain priority if equal) */
+ if (pb < pa) { a = b; pa = pb; }
+ return (pc < pa) ? c : a;
+}
+
+
+/*shared values used by multiple Adam7 related functions*/
+static const unsigned ADAM7_IX[7] = { 0, 4, 0, 2, 0, 1, 0 }; /*x start values*/
+static const unsigned ADAM7_IY[7] = { 0, 0, 4, 0, 2, 0, 1 }; /*y start values*/
+static const unsigned ADAM7_DX[7] = { 8, 8, 4, 4, 2, 2, 1 }; /*x delta values*/
+static const unsigned ADAM7_DY[7] = { 8, 8, 8, 4, 4, 2, 2 }; /*y delta values*/
+
+/* Outputs various dimensions and positions in the image related to the Adam7 reduced images.
+ passw: output containing the width of the 7 passes
+ passh: output containing the height of the 7 passes
+ filter_passstart: output containing the index of the start and end of each
+ reduced image with filter bytes
+ padded_passstart output containing the index of the start and end of each
+ reduced image when without filter bytes but with padded scanlines
+ passstart: output containing the index of the start and end of each reduced
+ image without padding between scanlines, but still padding between the images
+ w, h: width and height of non-interlaced image
+ bpp: bits per pixel
+ "padded" is only relevant if bpp is less than 8 and a scanline or image does not
+ end at a full byte */
+static void Adam7_getpassvalues(unsigned passw[7], unsigned passh[7], size_t filter_passstart[8], size_t padded_passstart[8], size_t passstart[8], unsigned w, unsigned h, unsigned bpp)
+{
+ /* the passstart values have 8 values: the 8th one indicates the byte after the end of the 7th (= last) pass */
+ unsigned i;
+
+ /* calculate width and height in pixels of each pass */
+ for (i = 0; i != 7; ++i) {
+ passw[i] = (w + ADAM7_DX[i] - ADAM7_IX[i] - 1) / ADAM7_DX[i];
+ passh[i] = (h + ADAM7_DY[i] - ADAM7_IY[i] - 1) / ADAM7_DY[i];
+ if(passw[i] == 0) passh[i] = 0;
+ if(passh[i] == 0) passw[i] = 0;
+ }
+
+ filter_passstart[0] = padded_passstart[0] = passstart[0] = 0;
+ for (i = 0; i != 7; ++i) {
+ /* if passw[i] is 0, it's 0 bytes, not 1 (no filtertype-byte) */
+ filter_passstart[i + 1] = filter_passstart[i]
+ + ((passw[i] && passh[i]) ? passh[i] * (1u + (passw[i] * bpp + 7u) / 8u) : 0);
+ /* bits padded if needed to fill full byte at end of each scanline */
+ padded_passstart[i + 1] = padded_passstart[i] + passh[i] * ((passw[i] * bpp + 7u) / 8u);
+ /* only padded at end of reduced image */
+ passstart[i + 1] = passstart[i] + (passh[i] * passw[i] * bpp + 7u) / 8u;
+ }
+}
+
+
+/* ////////////////////////////////////////////////////////////////////////// */
+/* / PNG Decoder / */
+/* ////////////////////////////////////////////////////////////////////////// */
+
+static unsigned unfilterScanline(unsigned char* recon, const unsigned char* scanline, const unsigned char* precon, size_t bytewidth, unsigned char filterType, size_t length)
+{
+ /* For PNG filter method 0
+ unfilter a PNG image scanline by scanline. when the pixels are smaller than 1 byte,
+ the filter works byte per byte (bytewidth = 1)
+ precon is the previous unfiltered scanline, recon the result, scanline the current one
+ the incoming scanlines do NOT include the filtertype byte, that one is given in the parameter filterType instead
+ recon and scanline MAY be the same memory address! precon must be disjoint. */
+
+ size_t i;
+ switch (filterType) {
+ case 0:
+ for (i = 0; i != length; ++i) recon[i] = scanline[i];
+ break;
+ case 1:
+ for (i = 0; i != bytewidth; ++i) recon[i] = scanline[i];
+ for (i = bytewidth; i < length; ++i) recon[i] = scanline[i] + recon[i - bytewidth];
+ break;
+ case 2:
+ if (precon) {
+ for(i = 0; i != length; ++i) recon[i] = scanline[i] + precon[i];
+ } else {
+ for(i = 0; i != length; ++i) recon[i] = scanline[i];
+ }
+ break;
+ case 3:
+ if (precon) {
+ for (i = 0; i != bytewidth; ++i) recon[i] = scanline[i] + (precon[i] >> 1u);
+ for (i = bytewidth; i < length; ++i) recon[i] = scanline[i] + ((recon[i - bytewidth] + precon[i]) >> 1u);
+ } else {
+ for (i = 0; i != bytewidth; ++i) recon[i] = scanline[i];
+ for (i = bytewidth; i < length; ++i) recon[i] = scanline[i] + (recon[i - bytewidth] >> 1u);
+ }
+ break;
+ case 4:
+ if (precon) {
+ for (i = 0; i != bytewidth; ++i) {
+ recon[i] = (scanline[i] + precon[i]); /*paethPredictor(0, precon[i], 0) is always precon[i]*/
+ }
+
+ /* Unroll independent paths of the paeth predictor. A 6x and 8x version would also be possible but that
+ adds too much code. Whether this actually speeds anything up at all depends on compiler and settings. */
+ if (bytewidth >= 4) {
+ for (; i + 3 < length; i += 4) {
+ size_t j = i - bytewidth;
+ unsigned char s0 = scanline[i + 0], s1 = scanline[i + 1], s2 = scanline[i + 2], s3 = scanline[i + 3];
+ unsigned char r0 = recon[j + 0], r1 = recon[j + 1], r2 = recon[j + 2], r3 = recon[j + 3];
+ unsigned char p0 = precon[i + 0], p1 = precon[i + 1], p2 = precon[i + 2], p3 = precon[i + 3];
+ unsigned char q0 = precon[j + 0], q1 = precon[j + 1], q2 = precon[j + 2], q3 = precon[j + 3];
+ recon[i + 0] = s0 + paethPredictor(r0, p0, q0);
+ recon[i + 1] = s1 + paethPredictor(r1, p1, q1);
+ recon[i + 2] = s2 + paethPredictor(r2, p2, q2);
+ recon[i + 3] = s3 + paethPredictor(r3, p3, q3);
+ }
+ } else if (bytewidth >= 3) {
+ for (; i + 2 < length; i += 3) {
+ size_t j = i - bytewidth;
+ unsigned char s0 = scanline[i + 0], s1 = scanline[i + 1], s2 = scanline[i + 2];
+ unsigned char r0 = recon[j + 0], r1 = recon[j + 1], r2 = recon[j + 2];
+ unsigned char p0 = precon[i + 0], p1 = precon[i + 1], p2 = precon[i + 2];
+ unsigned char q0 = precon[j + 0], q1 = precon[j + 1], q2 = precon[j + 2];
+ recon[i + 0] = s0 + paethPredictor(r0, p0, q0);
+ recon[i + 1] = s1 + paethPredictor(r1, p1, q1);
+ recon[i + 2] = s2 + paethPredictor(r2, p2, q2);
+ }
+ } else if (bytewidth >= 2) {
+ for (; i + 1 < length; i += 2) {
+ size_t j = i - bytewidth;
+ unsigned char s0 = scanline[i + 0], s1 = scanline[i + 1];
+ unsigned char r0 = recon[j + 0], r1 = recon[j + 1];
+ unsigned char p0 = precon[i + 0], p1 = precon[i + 1];
+ unsigned char q0 = precon[j + 0], q1 = precon[j + 1];
+ recon[i + 0] = s0 + paethPredictor(r0, p0, q0);
+ recon[i + 1] = s1 + paethPredictor(r1, p1, q1);
+ }
+ }
+
+ for (; i != length; ++i) {
+ recon[i] = (scanline[i] + paethPredictor(recon[i - bytewidth], precon[i], precon[i - bytewidth]));
+ }
+ } else {
+ for (i = 0; i != bytewidth; ++i) {
+ recon[i] = scanline[i];
+ }
+ for (i = bytewidth; i < length; ++i) {
+ /* paethPredictor(recon[i - bytewidth], 0, 0) is always recon[i - bytewidth] */
+ recon[i] = (scanline[i] + recon[i - bytewidth]);
+ }
+ }
+ break;
+ default: return 36; /* error: invalid filter type given */
+ }
+ return 0;
+}
+
+
+static unsigned unfilter(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp)
+{
+ /* For PNG filter method 0
+ this function unfilters a single image (e.g. without interlacing this is called once, with Adam7 seven times)
+ out must have enough bytes allocated already, in must have the scanlines + 1 filtertype byte per scanline
+ w and h are image dimensions or dimensions of reduced image, bpp is bits per pixel
+ in and out are allowed to be the same memory address (but aren't the same size since in has the extra filter bytes) */
+
+ unsigned y;
+ unsigned char* prevline = 0;
+
+ /* bytewidth is used for filtering, is 1 when bpp < 8, number of bytes per pixel otherwise */
+ size_t bytewidth = (bpp + 7u) / 8u;
+ /* the width of a scanline in bytes, not including the filter type */
+ size_t linebytes = lodepng_get_raw_size_idat(w, 1, bpp) - 1u;
+
+ for (y = 0; y < h; ++y) {
+ size_t outindex = linebytes * y;
+ size_t inindex = (1 + linebytes) * y; /* the extra filterbyte added to each row */
+ unsigned char filterType = in[inindex];
+ CERROR_TRY_RETURN(unfilterScanline(&out[outindex], &in[inindex + 1], prevline, bytewidth, filterType, linebytes));
+ prevline = &out[outindex];
+ }
+
+ return 0;
+}
+
+/* in: Adam7 interlaced image, with no padding bits between scanlines, but between
+ reduced images so that each reduced image starts at a byte.
+ out: the same pixels, but re-ordered so that they're now a non-interlaced image with size w*h
+ bpp: bits per pixel
+ out has the following size in bits: w * h * bpp.
+ in is possibly bigger due to padding bits between reduced images.
+ out must be big enough AND must be 0 everywhere if bpp < 8 in the current implementation
+ (because that's likely a little bit faster)
+ NOTE: comments about padding bits are only relevant if bpp < 8 */
+static void Adam7_deinterlace(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp)
+{
+ unsigned passw[7], passh[7];
+ size_t filter_passstart[8], padded_passstart[8], passstart[8];
+ unsigned i;
+
+ Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
+
+ if (bpp >= 8) {
+ for(i = 0; i != 7; ++i) {
+ unsigned x, y, b;
+ size_t bytewidth = bpp / 8u;
+ for (y = 0; y < passh[i]; ++y)
+ for (x = 0; x < passw[i]; ++x) {
+ size_t pixelinstart = passstart[i] + (y * passw[i] + x) * bytewidth;
+ size_t pixeloutstart = ((ADAM7_IY[i] + (size_t)y * ADAM7_DY[i]) * (size_t)w + ADAM7_IX[i] + (size_t)x * ADAM7_DX[i]) * bytewidth;
+ for (b = 0; b < bytewidth; ++b) {
+ out[pixeloutstart + b] = in[pixelinstart + b];
+ }
+ }
+ }
+ } else /* bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers */ {
+ for (i = 0; i != 7; ++i) {
+ unsigned x, y, b;
+ unsigned ilinebits = bpp * passw[i];
+ unsigned olinebits = bpp * w;
+ size_t obp, ibp; /* bit pointers (for out and in buffer) */
+ for (y = 0; y < passh[i]; ++y)
+ for (x = 0; x < passw[i]; ++x) {
+ ibp = (8 * passstart[i]) + (y * ilinebits + x * bpp);
+ obp = (ADAM7_IY[i] + (size_t)y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + (size_t)x * ADAM7_DX[i]) * bpp;
+ for (b = 0; b < bpp; ++b) {
+ unsigned char bit = readBitFromReversedStream(&ibp, in);
+ setBitOfReversedStream(&obp, out, bit);
+ }
+ }
+ }
+ }
+}
+
+
+static void removePaddingBits(unsigned char* out, const unsigned char* in, size_t olinebits, size_t ilinebits, unsigned h)
+{
+ /* After filtering there are still padding bits if scanlines have non multiple of 8 bit amounts. They need
+ to be removed (except at last scanline of (Adam7-reduced) image) before working with pure image buffers
+ for the Adam7 code, the color convert code and the output to the user.
+ in and out are allowed to be the same buffer, in may also be higher but still overlapping; in must
+ have >= ilinebits*h bits, out must have >= olinebits*h bits, olinebits must be <= ilinebits
+ also used to move bits after earlier such operations happened, e.g. in a sequence of reduced images from Adam7
+ only useful if (ilinebits - olinebits) is a value in the range 1..7 */
+ unsigned y;
+ size_t diff = ilinebits - olinebits;
+ size_t ibp = 0, obp = 0; /*input and output bit pointers*/
+ for (y = 0; y < h; ++y) {
+ size_t x;
+ for (x = 0; x < olinebits; ++x) {
+ unsigned char bit = readBitFromReversedStream(&ibp, in);
+ setBitOfReversedStream(&obp, out, bit);
+ }
+ ibp += diff;
+ }
+}
+
+
+/* out must be buffer big enough to contain full image, and in must contain the full decompressed data from
+ the IDAT chunks (with filter index bytes and possible padding bits)
+ return value is error */
+static unsigned postProcessScanlines(unsigned char* out, unsigned char* in, unsigned w, unsigned h, const LodePNGInfo* info_png)
+{
+ /* This function converts the filtered-padded-interlaced data into pure 2D image buffer with the PNG's colortype.
+ Steps:
+ *) if no Adam7: 1) unfilter 2) remove padding bits (= possible extra bits per scanline if bpp < 8)
+ *) if adam7: 1) 7x unfilter 2) 7x remove padding bits 3) Adam7_deinterlace
+ NOTE: the in buffer will be overwritten with intermediate data! */
+ unsigned bpp = lodepng_get_bpp_lct(info_png->color.colortype, info_png->color.bitdepth);
+ if (bpp == 0) return 31; /* error: invalid colortype */
+
+ if (info_png->interlace_method == 0) {
+ if (bpp < 8 && w * bpp != ((w * bpp + 7u) / 8u) * 8u) {
+ CERROR_TRY_RETURN(unfilter(in, in, w, h, bpp));
+ removePaddingBits(out, in, w * bpp, ((w * bpp + 7u) / 8u) * 8u, h);
+ }
+ /* we can immediately filter into the out buffer, no other steps needed */
+ else CERROR_TRY_RETURN(unfilter(out, in, w, h, bpp));
+ } else /* interlace_method is 1 (Adam7) */ {
+ unsigned passw[7], passh[7]; size_t filter_passstart[8], padded_passstart[8], passstart[8];
+ unsigned i;
+
+ Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp);
+
+ for (i = 0; i != 7; ++i) {
+ CERROR_TRY_RETURN(unfilter(&in[padded_passstart[i]], &in[filter_passstart[i]], passw[i], passh[i], bpp));
+ /* TODO: possible efficiency improvement: if in this reduced image the bits fit nicely in 1 scanline,
+ move bytes instead of bits or move not at all */
+ if (bpp < 8) {
+ /* remove padding bits in scanlines; after this there still may be padding
+ bits between the different reduced images: each reduced image still starts nicely at a byte */
+ removePaddingBits(&in[passstart[i]], &in[padded_passstart[i]], passw[i] * bpp, ((passw[i] * bpp + 7u) / 8u) * 8u, passh[i]);
+ }
+ }
+ Adam7_deinterlace(out, in, w, h, bpp);
+ }
+ return 0;
+}
+
+
+static unsigned readChunk_PLTE(LodePNGColorMode* color, const unsigned char* data, size_t chunkLength)
+{
+ unsigned pos = 0, i;
+ color->palettesize = chunkLength / 3u;
+ if (color->palettesize == 0 || color->palettesize > 256) return 38; /* error: palette too small or big */
+ lodepng_color_mode_alloc_palette(color);
+ if (!color->palette && color->palettesize) {
+ color->palettesize = 0;
+ return 83; /* alloc fail */
+ }
+
+ for (i = 0; i != color->palettesize; ++i) {
+ color->palette[4 * i + 0] = data[pos++]; /*R*/
+ color->palette[4 * i + 1] = data[pos++]; /*G*/
+ color->palette[4 * i + 2] = data[pos++]; /*B*/
+ color->palette[4 * i + 3] = 255; /*alpha*/
+ }
+
+ return 0; /* OK */
+}
+
+
+static unsigned readChunk_tRNS(LodePNGColorMode* color, const unsigned char* data, size_t chunkLength)
+{
+ unsigned i;
+ if (color->colortype == LCT_PALETTE) {
+ /* error: more alpha values given than there are palette entries */
+ if (chunkLength > color->palettesize) return 39;
+
+ for (i = 0; i != chunkLength; ++i) color->palette[4 * i + 3] = data[i];
+ } else if (color->colortype == LCT_GREY) {
+ /* error: this chunk must be 2 bytes for grayscale image */
+ if (chunkLength != 2) return 30;
+
+ color->key_defined = 1;
+ color->key_r = color->key_g = color->key_b = 256u * data[0] + data[1];
+ } else if (color->colortype == LCT_RGB) {
+ /* error: this chunk must be 6 bytes for RGB image */
+ if (chunkLength != 6) return 41;
+
+ color->key_defined = 1;
+ color->key_r = 256u * data[0] + data[1];
+ color->key_g = 256u * data[2] + data[3];
+ color->key_b = 256u * data[4] + data[5];
+ }
+ else return 42; /* error: tRNS chunk not allowed for other color models */
+
+ return 0; /* OK */
+}
+
+
+/* read a PNG, the result will be in the same color type as the PNG (hence "generic") */
+static void decodeGeneric(unsigned char** out, unsigned* w, unsigned* h, LodePNGState* state, const unsigned char* in, size_t insize)
+{
+ unsigned char IEND = 0;
+ const unsigned char* chunk;
+ unsigned char* idat; /*the data from idat chunks, zlib compressed*/
+ size_t idatsize = 0;
+ unsigned char* scanlines = 0;
+ size_t scanlines_size = 0, expected_size = 0;
+ size_t outsize = 0;
+
+ /* safe output values in case error happens */
+ *out = 0;
+ *w = *h = 0;
+
+ state->error = lodepng_inspect(w, h, state, in, insize); /*reads header and resets other parameters in state->info_png*/
+ if (state->error) return;
+
+ if (lodepng_pixel_overflow(*w, *h, &state->info_png.color, &state->info_raw)) {
+ CERROR_RETURN(state->error, 92); /*overflow possible due to amount of pixels*/
+ }
+
+ /*the input filesize is a safe upper bound for the sum of idat chunks size*/
+ idat = (unsigned char*)malloc(insize);
+ if (!idat) CERROR_RETURN(state->error, 83); /*alloc fail*/
+
+ chunk = &in[33]; /*first byte of the first chunk after the header*/
+
+ /*loop through the chunks, ignoring unknown chunks and stopping at IEND chunk.
+ IDAT data is put at the start of the in buffer*/
+ while (!IEND && !state->error) {
+ unsigned chunkLength;
+ const unsigned char* data; /*the data in the chunk*/
+
+ /*error: size of the in buffer too small to contain next chunk*/
+ if ((size_t)((chunk - in) + 12) > insize || chunk < in) {
+ if (state->decoder.ignore_end) break; /*other errors may still happen though*/
+ CERROR_BREAK(state->error, 30);
+ }
+
+ /*length of the data of the chunk, excluding the length bytes, chunk type and CRC bytes*/
+ chunkLength = lodepng_chunk_length(chunk);
+ /*error: chunk length larger than the max PNG chunk size*/
+ if (chunkLength > 2147483647) {
+ if (state->decoder.ignore_end) break; /*other errors may still happen though*/
+ CERROR_BREAK(state->error, 63);
+ }
+
+ if ((size_t)((chunk - in) + chunkLength + 12) > insize || (chunk + chunkLength + 12) < in) {
+ CERROR_BREAK(state->error, 64); /*error: size of the in buffer too small to contain next chunk*/
+ }
+
+ data = lodepng_chunk_data_const(chunk);
+
+ /*for unknown chunk order*/
+ //unsigned unknown = 0;
+
+ /*IDAT chunk, containing compressed image data*/
+ if (lodepng_chunk_type_equals(chunk, "IDAT")) {
+ size_t newsize;
+ if (lodepng_addofl(idatsize, chunkLength, &newsize)) CERROR_BREAK(state->error, 95);
+ if (newsize > insize) CERROR_BREAK(state->error, 95);
+ lodepng_memcpy(idat + idatsize, data, chunkLength);
+ idatsize += chunkLength;
+ } else if (lodepng_chunk_type_equals(chunk, "IEND")) {
+ /*IEND chunk*/
+ IEND = 1;
+ } else if (lodepng_chunk_type_equals(chunk, "PLTE")) {
+ /*palette chunk (PLTE)*/
+ state->error = readChunk_PLTE(&state->info_png.color, data, chunkLength);
+ if (state->error) break;
+ } else if (lodepng_chunk_type_equals(chunk, "tRNS")) {
+ /*palette transparency chunk (tRNS). Even though this one is an ancillary chunk , it is still compiled
+ in without 'LODEPNG_COMPILE_ANCILLARY_CHUNKS' because it contains essential color information that
+ affects the alpha channel of pixels. */
+ state->error = readChunk_tRNS(&state->info_png.color, data, chunkLength);
+ if (state->error) break;
+ } else /*it's not an implemented chunk type, so ignore it: skip over the data*/ {
+ /*error: unknown critical chunk (5th bit of first byte of chunk type is 0)*/
+ if (!state->decoder.ignore_critical && !lodepng_chunk_ancillary(chunk)) {
+ CERROR_BREAK(state->error, 69);
+ }
+ //unknown = 1;
+ }
+
+#if 0 //We don't use CRC
+ if (!state->decoder.ignore_crc && !unknown) /*check CRC if wanted, only on known chunk types*/ {
+ if (lodepng_chunk_check_crc(chunk)) CERROR_BREAK(state->error, 57); /*invalid CRC*/
+ }
+#endif
+ if (!IEND) chunk = lodepng_chunk_next_const(chunk, in + insize);
+ }
+
+ if (state->info_png.color.colortype == LCT_PALETTE && !state->info_png.color.palette) {
+ state->error = 106; /* error: PNG file must have PLTE chunk if color type is palette */
+ }
+
+ if (!state->error) {
+ /*predict output size, to allocate exact size for output buffer to avoid more dynamic allocation.
+ If the decompressed size does not match the prediction, the image must be corrupt.*/
+ if (state->info_png.interlace_method == 0) {
+ size_t bpp = lodepng_get_bpp_lct(state->info_png.color.colortype, state->info_png.color.bitdepth);
+ expected_size = lodepng_get_raw_size_idat(*w, *h, bpp);
+ } else {
+ size_t bpp = lodepng_get_bpp_lct(state->info_png.color.colortype, state->info_png.color.bitdepth);
+ /*Adam-7 interlaced: expected size is the sum of the 7 sub-images sizes*/
+ expected_size = 0;
+ expected_size += lodepng_get_raw_size_idat((*w + 7) >> 3, (*h + 7) >> 3, bpp);
+ if (*w > 4) expected_size += lodepng_get_raw_size_idat((*w + 3) >> 3, (*h + 7) >> 3, bpp);
+ expected_size += lodepng_get_raw_size_idat((*w + 3) >> 2, (*h + 3) >> 3, bpp);
+ if (*w > 2) expected_size += lodepng_get_raw_size_idat((*w + 1) >> 2, (*h + 3) >> 2, bpp);
+ expected_size += lodepng_get_raw_size_idat((*w + 1) >> 1, (*h + 1) >> 2, bpp);
+ if (*w > 1) expected_size += lodepng_get_raw_size_idat((*w + 0) >> 1, (*h + 1) >> 1, bpp);
+ expected_size += lodepng_get_raw_size_idat((*w + 0), (*h + 0) >> 1, bpp);
+ }
+ state->error = zlib_decompress(&scanlines, &scanlines_size, expected_size, idat, idatsize, &state->decoder.zlibsettings);
+ }
+
+ if (!state->error && scanlines_size != expected_size) state->error = 91; /*decompressed size doesn't match prediction*/
+ free(idat);
+
+ if (!state->error) {
+ outsize = lodepng_get_raw_size(*w, *h, &state->info_png.color);
+ *out = (unsigned char*)malloc(outsize);
+ if (!*out) state->error = 83; /*alloc fail*/
+ }
+ if (!state->error) {
+ lodepng_memset(*out, 0, outsize);
+ state->error = postProcessScanlines(*out, scanlines, *w, *h, &state->info_png);
+ }
+ free(scanlines);
+}
+
+
+static void lodepng_decoder_settings_init(LodePNGDecoderSettings* settings)
+{
+ settings->color_convert = 1;
+ settings->ignore_crc = 0;
+ settings->ignore_critical = 0;
+ settings->ignore_end = 0;
+ lodepng_decompress_settings_init(&settings->zlibsettings);
+}
+
+
+/************************************************************************/
+/* External Class Implementation */
+/************************************************************************/
+
+/*read the information from the header and store it in the LodePNGInfo. return value is error*/
+unsigned lodepng_inspect(unsigned* w, unsigned* h, LodePNGState* state, const unsigned char* in, size_t insize)
+{
+ unsigned width, height;
+ LodePNGInfo* info = &state->info_png;
+ if (insize == 0 || in == 0) {
+ CERROR_RETURN_ERROR(state->error, 48); /*error: the given data is empty*/
+ }
+ if (insize < 33) {
+ CERROR_RETURN_ERROR(state->error, 27); /*error: the data length is smaller than the length of a PNG header*/
+ }
+
+ /* when decoding a new PNG image, make sure all parameters created after previous decoding are reset */
+ /* TODO: remove this. One should use a new LodePNGState for new sessions */
+ lodepng_info_cleanup(info);
+ lodepng_info_init(info);
+
+ if (in[0] != 137 || in[1] != 80 || in[2] != 78 || in[3] != 71 || in[4] != 13 || in[5] != 10 || in[6] != 26 || in[7] != 10) {
+ CERROR_RETURN_ERROR(state->error, 28); /*error: the first 8 bytes are not the correct PNG signature*/
+ }
+ if (lodepng_chunk_length(in + 8) != 13) {
+ CERROR_RETURN_ERROR(state->error, 94); /*error: header size must be 13 bytes*/
+ }
+ if (!lodepng_chunk_type_equals(in + 8, "IHDR")) {
+ CERROR_RETURN_ERROR(state->error, 29); /*error: it doesn't start with a IHDR chunk!*/
+ }
+
+ /*read the values given in the header*/
+ width = lodepng_read32bitInt(&in[16]);
+ height = lodepng_read32bitInt(&in[20]);
+ /*TODO: remove the undocumented feature that allows to give null pointers to width or height*/
+ if (w) *w = width;
+ if (h) *h = height;
+ info->color.bitdepth = in[24];
+ info->color.colortype = (LodePNGColorType)in[25];
+ info->compression_method = in[26];
+ info->filter_method = in[27];
+ info->interlace_method = in[28];
+
+ /*errors returned only after the parsing so other values are still output*/
+
+ /*error: invalid image size*/
+ if (width == 0 || height == 0) CERROR_RETURN_ERROR(state->error, 93);
+ /*error: invalid colortype or bitdepth combination*/
+ state->error = checkColorValidity(info->color.colortype, info->color.bitdepth);
+ if (state->error) return state->error;
+ /*error: only compression method 0 is allowed in the specification*/
+ if (info->compression_method != 0) CERROR_RETURN_ERROR(state->error, 32);
+ /*error: only filter method 0 is allowed in the specification*/
+ if (info->filter_method != 0) CERROR_RETURN_ERROR(state->error, 33);
+ /*error: only interlace methods 0 and 1 exist in the specification*/
+ if (info->interlace_method > 1) CERROR_RETURN_ERROR(state->error, 34);
+
+#if 0 //thorvg don't use crc
+ if (!state->decoder.ignore_crc) {
+ unsigned CRC = lodepng_read32bitInt(&in[29]);
+ unsigned checksum = lodepng_crc32(&in[12], 17);
+ if (CRC != checksum) {
+ CERROR_RETURN_ERROR(state->error, 57); /*invalid CRC*/
+ }
+ }
+#endif
+ return state->error;
+}
+
+
+unsigned lodepng_decode(unsigned char** out, unsigned* w, unsigned* h, LodePNGState* state, const unsigned char* in, size_t insize)
+{
+ *out = 0;
+ decodeGeneric(out, w, h, state, in, insize);
+ if (state->error) return state->error;
+ if (!state->decoder.color_convert || lodepng_color_mode_equal(&state->info_raw, &state->info_png.color)) {
+ /*same color type, no copying or converting of data needed*/
+ /*store the info_png color settings on the info_raw so that the info_raw still reflects what colortype
+ the raw image has to the end user*/
+ if (!state->decoder.color_convert) {
+ state->error = lodepng_color_mode_copy(&state->info_raw, &state->info_png.color);
+ if (state->error) return state->error;
+ }
+ } else { /*color conversion needed*/
+ unsigned char* data = *out;
+ size_t outsize;
+
+ /*TODO: check if this works according to the statement in the documentation: "The converter can convert
+ from grayscale input color type, to 8-bit grayscale or grayscale with alpha"*/
+ if (!(state->info_raw.colortype == LCT_RGB || state->info_raw.colortype == LCT_RGBA) && !(state->info_raw.bitdepth == 8)) {
+ return 56; /*unsupported color mode conversion*/
+ }
+
+ outsize = lodepng_get_raw_size(*w, *h, &state->info_raw);
+ *out = (unsigned char*)malloc(outsize);
+ if (!(*out)) {
+ state->error = 83; /*alloc fail*/
+ }
+ else state->error = lodepng_convert(*out, data, &state->info_raw, &state->info_png.color, *w, *h);
+ free(data);
+ }
+ return state->error;
+}
+
+
+void lodepng_state_init(LodePNGState* state)
+{
+ lodepng_decoder_settings_init(&state->decoder);
+ lodepng_color_mode_init(&state->info_raw);
+ lodepng_info_init(&state->info_png);
+ state->error = 1;
+}
+
+
+void lodepng_state_cleanup(LodePNGState* state)
+{
+ lodepng_color_mode_cleanup(&state->info_raw);
+ lodepng_info_cleanup(&state->info_png);
+}
diff --git a/thirdparty/thorvg/src/loaders/png/tvgLodePng.h b/thirdparty/thorvg/src/loaders/png/tvgLodePng.h
new file mode 100644
index 0000000000..7254a55477
--- /dev/null
+++ b/thirdparty/thorvg/src/loaders/png/tvgLodePng.h
@@ -0,0 +1,174 @@
+/*
+ * Copyright (c) 2020 - 2023 the ThorVG project. All rights reserved.
+
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+/*
+ LodePNG version 20200306
+
+ Copyright (c) 2005-2020 Lode Vandevenne
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+
+ 3. This notice may not be removed or altered from any source
+ distribution.
+*/
+
+#ifndef _TVG_LODEPNG_H_
+#define _TVG_LODEPNG_H_
+
+#include <stddef.h>
+
+/*The PNG color types (also used for raw image).*/
+enum LodePNGColorType
+{
+ LCT_GREY = 0, /*grayscale: 1,2,4,8,16 bit*/
+ LCT_RGB = 2, /*RGB: 8,16 bit*/
+ LCT_PALETTE = 3, /*palette: 1,2,4,8 bit*/
+ LCT_GREY_ALPHA = 4, /*grayscale with alpha: 8,16 bit*/
+ LCT_RGBA = 6, /*RGB with alpha: 8,16 bit*/
+ /*LCT_MAX_OCTET_VALUE lets the compiler allow this enum to represent any invalid
+ byte value from 0 to 255 that could be present in an invalid PNG file header. Do
+ not use, compare with or set the name LCT_MAX_OCTET_VALUE, instead either use
+ the valid color type names above, or numeric values like 1 or 7 when checking for
+ particular disallowed color type byte values, or cast to integer to print it.*/
+ LCT_MAX_OCTET_VALUE = 255
+};
+
+/*Settings for zlib decompression*/
+struct LodePNGDecompressSettings
+{
+ /* Check LodePNGDecoderSettings for more ignorable errors such as ignore_crc */
+ unsigned ignore_adler32; /*if 1, continue and don't give an error message if the Adler32 checksum is corrupted*/
+ unsigned ignore_nlen; /*ignore complement of len checksum in uncompressed blocks*/
+
+ /*use custom zlib decoder instead of built in one (default: null)*/
+ unsigned (*custom_zlib)(unsigned char**, size_t*, const unsigned char*, size_t, const LodePNGDecompressSettings*);
+ /*use custom deflate decoder instead of built in one (default: null) if custom_zlib is not null, custom_inflate is ignored (the zlib format uses deflate)*/
+ unsigned (*custom_inflate)(unsigned char**, size_t*, const unsigned char*, size_t, const LodePNGDecompressSettings*);
+
+ const void* custom_context; /*optional custom settings for custom functions*/
+};
+
+/*
+ Color mode of an image. Contains all information required to decode the pixel
+ bits to RGBA colors. This information is the same as used in the PNG file
+ format, and is used both for PNG and raw image data in LodePNG.
+*/
+struct LodePNGColorMode
+{
+ /*header (IHDR)*/
+ LodePNGColorType colortype; /*color type, see PNG standard or documentation further in this header file*/
+ unsigned bitdepth; /*bits per sample, see PNG standard or documentation further in this header file*/
+
+ /*
+ palette (PLTE and tRNS)
+
+ Dynamically allocated with the colors of the palette, including alpha.
+ This field may not be allocated directly, use lodepng_color_mode_init first,
+ then lodepng_palette_add per color to correctly initialize it (to ensure size
+ of exactly 1024 bytes).
+
+ The alpha channels must be set as well, set them to 255 for opaque images.
+
+ When decoding, by default you can ignore this palette, since LodePNG already
+ fills the palette colors in the pixels of the raw RGBA output.
+
+ The palette is only supported for color type 3.
+ */
+ unsigned char* palette; /*palette in RGBARGBA... order. Must be either 0, or when allocated must have 1024 bytes*/
+ size_t palettesize; /*palette size in number of colors (amount of used bytes is 4 * palettesize)*/
+
+ /*
+ transparent color key (tRNS)
+
+ This color uses the same bit depth as the bitdepth value in this struct, which can be 1-bit to 16-bit.
+ For grayscale PNGs, r, g and b will all 3 be set to the same.
+
+ When decoding, by default you can ignore this information, since LodePNG sets
+ pixels with this key to transparent already in the raw RGBA output.
+
+ The color key is only supported for color types 0 and 2.
+ */
+ unsigned key_defined; /*is a transparent color key given? 0 = false, 1 = true*/
+ unsigned key_r; /*red/grayscale component of color key*/
+ unsigned key_g; /*green component of color key*/
+ unsigned key_b; /*blue component of color key*/
+};
+
+/*Information about the PNG image, except pixels, width and height.*/
+struct LodePNGInfo
+{
+ /*header (IHDR), palette (PLTE) and transparency (tRNS) chunks*/
+ unsigned compression_method;/*compression method of the original file. Always 0.*/
+ unsigned filter_method; /*filter method of the original file*/
+ unsigned interlace_method; /*interlace method of the original file: 0=none, 1=Adam7*/
+ LodePNGColorMode color; /*color type and bits, palette and transparency of the PNG file*/
+};
+
+/*
+ Settings for the decoder. This contains settings for the PNG and the Zlib
+ decoder, but not the Info settings from the Info structs.
+*/
+struct LodePNGDecoderSettings
+{
+ LodePNGDecompressSettings zlibsettings; /*in here is the setting to ignore Adler32 checksums*/
+
+ /* Check LodePNGDecompressSettings for more ignorable errors such as ignore_adler32 */
+ unsigned ignore_crc; /*ignore CRC checksums*/
+ unsigned ignore_critical; /*ignore unknown critical chunks*/
+ unsigned ignore_end; /*ignore issues at end of file if possible (missing IEND chunk, too large chunk, ...)*/
+ /* TODO: make a system involving warnings with levels and a strict mode instead. Other potentially recoverable
+ errors: srgb rendering intent value, size of content of ancillary chunks, more than 79 characters for some
+ strings, placement/combination rules for ancillary chunks, crc of unknown chunks, allowed characters
+ in string keys, etc... */
+
+ unsigned color_convert; /*whether to convert the PNG to the color type you want. Default: yes*/
+};
+
+/*The settings, state and information for extended encoding and decoding.*/
+struct LodePNGState
+{
+ LodePNGDecoderSettings decoder; /*the decoding settings*/
+ LodePNGColorMode info_raw; /*specifies the format in which you would like to get the raw pixel buffer*/
+ LodePNGInfo info_png; /*info of the PNG image obtained after decoding*/
+ unsigned error;
+};
+
+void lodepng_state_init(LodePNGState* state);
+void lodepng_state_cleanup(LodePNGState* state);
+unsigned lodepng_decode(unsigned char** out, unsigned* w, unsigned* h, LodePNGState* state, const unsigned char* in, size_t insize);
+unsigned lodepng_inspect(unsigned* w, unsigned* h, LodePNGState* state, const unsigned char* in, size_t insize);
+
+#endif //_TVG_LODEPNG_H_
diff --git a/thirdparty/thorvg/src/loaders/png/tvgPngLoader.cpp b/thirdparty/thorvg/src/loaders/png/tvgPngLoader.cpp
new file mode 100644
index 0000000000..32ff57c5c3
--- /dev/null
+++ b/thirdparty/thorvg/src/loaders/png/tvgPngLoader.cpp
@@ -0,0 +1,185 @@
+/*
+ * Copyright (c) 2021 - 2023 the ThorVG project. All rights reserved.
+
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <memory.h>
+#include "tvgLoader.h"
+#include "tvgPngLoader.h"
+
+
+/************************************************************************/
+/* Internal Class Implementation */
+/************************************************************************/
+
+void PngLoader::clear()
+{
+ lodepng_state_cleanup(&state);
+
+ if (freeData) free(data);
+ data = nullptr;
+ size = 0;
+ freeData = false;
+}
+
+
+/************************************************************************/
+/* External Class Implementation */
+/************************************************************************/
+
+PngLoader::PngLoader()
+{
+ lodepng_state_init(&state);
+}
+
+
+PngLoader::~PngLoader()
+{
+ if (freeData) free(data);
+ free(image);
+}
+
+
+bool PngLoader::open(const string& path)
+{
+ clear();
+
+ auto pngFile = fopen(path.c_str(), "rb");
+ if (!pngFile) return false;
+
+ auto ret = false;
+
+ //determine size
+ if (fseek(pngFile, 0, SEEK_END) < 0) goto finalize;
+ if (((size = ftell(pngFile)) < 1)) goto finalize;
+ if (fseek(pngFile, 0, SEEK_SET)) goto finalize;
+
+ data = (unsigned char *) malloc(size);
+ if (!data) goto finalize;
+
+ freeData = true;
+
+ if (fread(data, size, 1, pngFile) < 1) goto failure;
+
+ lodepng_state_init(&state);
+
+ unsigned int width, height;
+ if (lodepng_inspect(&width, &height, &state, data, size) > 0) goto failure;
+
+ w = static_cast<float>(width);
+ h = static_cast<float>(height);
+
+ if (state.info_png.color.colortype == LCT_RGBA) cs = ColorSpace::ABGR8888;
+ else cs = ColorSpace::ARGB8888;
+
+ ret = true;
+
+ goto finalize;
+
+failure:
+ clear();
+
+finalize:
+ fclose(pngFile);
+ return ret;
+}
+
+
+bool PngLoader::open(const char* data, uint32_t size, bool copy)
+{
+ clear();
+
+ lodepng_state_init(&state);
+
+ unsigned int width, height;
+ if (lodepng_inspect(&width, &height, &state, (unsigned char*)(data), size) > 0) return false;
+
+ if (copy) {
+ this->data = (unsigned char *) malloc(size);
+ if (!this->data) return false;
+ memcpy((unsigned char *)this->data, data, size);
+ freeData = true;
+ } else {
+ this->data = (unsigned char *) data;
+ freeData = false;
+ }
+
+ w = static_cast<float>(width);
+ h = static_cast<float>(height);
+ this->size = size;
+
+ cs = ColorSpace::ABGR8888;
+
+ return true;
+}
+
+
+bool PngLoader::read()
+{
+ if (!data || w <= 0 || h <= 0) return false;
+
+ TaskScheduler::request(this);
+
+ return true;
+}
+
+
+bool PngLoader::close()
+{
+ this->done();
+ clear();
+ return true;
+}
+
+
+unique_ptr<Surface> PngLoader::bitmap()
+{
+ this->done();
+
+ if (!image) return nullptr;
+
+ //TODO: It's better to keep this surface instance in the loader side
+ auto surface = new Surface;
+ surface->buf8 = image;
+ surface->stride = static_cast<uint32_t>(w);
+ surface->w = static_cast<uint32_t>(w);
+ surface->h = static_cast<uint32_t>(h);
+ surface->cs = cs;
+ surface->channelSize = sizeof(uint32_t);
+ surface->premultiplied = false;
+ surface->owner = true;
+
+ return unique_ptr<Surface>(surface);
+}
+
+
+void PngLoader::run(unsigned tid)
+{
+ if (image) {
+ free(image);
+ image = nullptr;
+ }
+ auto width = static_cast<unsigned>(w);
+ auto height = static_cast<unsigned>(h);
+
+ if (lodepng_decode(&image, &width, &height, &state, data, size)) {
+ TVGERR("PNG", "Failed to decode image");
+ }
+}
diff --git a/thirdparty/thorvg/src/loaders/png/tvgPngLoader.h b/thirdparty/thorvg/src/loaders/png/tvgPngLoader.h
new file mode 100644
index 0000000000..579d197ad6
--- /dev/null
+++ b/thirdparty/thorvg/src/loaders/png/tvgPngLoader.h
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2021 - 2023 the ThorVG project. All rights reserved.
+
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef _TVG_PNG_LOADER_H_
+#define _TVG_PNG_LOADER_H_
+
+#include "tvgLodePng.h"
+#include "tvgTaskScheduler.h"
+
+
+class PngLoader : public LoadModule, public Task
+{
+private:
+ LodePNGState state;
+ unsigned char* data = nullptr;
+ unsigned char *image = nullptr;
+ unsigned long size = 0;
+ bool freeData = false;
+
+ void clear();
+
+public:
+ PngLoader();
+ ~PngLoader();
+
+ using LoadModule::open;
+ bool open(const string& path) override;
+ bool open(const char* data, uint32_t size, bool copy) override;
+ bool read() override;
+ bool close() override;
+
+ unique_ptr<Surface> bitmap() override;
+ void run(unsigned tid) override;
+};
+
+#endif //_TVG_PNG_LOADER_H_
diff --git a/thirdparty/thorvg/src/loaders/svg/tvgSvgSceneBuilder.cpp b/thirdparty/thorvg/src/loaders/svg/tvgSvgSceneBuilder.cpp
index 2e018e2799..1791df57f0 100644
--- a/thirdparty/thorvg/src/loaders/svg/tvgSvgSceneBuilder.cpp
+++ b/thirdparty/thorvg/src/loaders/svg/tvgSvgSceneBuilder.cpp
@@ -284,8 +284,12 @@ static void _applyComposition(SvgLoaderData& loaderData, Paint* paint, const Svg
bool isMaskWhite = true;
if (auto comp = _sceneBuildHelper(loaderData, compNode, vBox, svgPath, true, 0, &isMaskWhite)) {
- Matrix finalTransform = _compositionTransform(paint, node, compNode, SvgNodeType::Mask);
- comp->transform(finalTransform);
+ if (!compNode->node.mask.userSpace) {
+ Matrix finalTransform = _compositionTransform(paint, node, compNode, SvgNodeType::Mask);
+ comp->transform(finalTransform);
+ } else {
+ if (node->transform) comp->transform(*node->transform);
+ }
if (compNode->node.mask.type == SvgMaskType::Luminance && !isMaskWhite) {
paint->composite(std::move(comp), CompositeMethod::LumaMask);
diff --git a/thirdparty/thorvg/src/renderer/sw_engine/tvgSwCommon.h b/thirdparty/thorvg/src/renderer/sw_engine/tvgSwCommon.h
index c0cd8863a2..c6dcf79a48 100644
--- a/thirdparty/thorvg/src/renderer/sw_engine/tvgSwCommon.h
+++ b/thirdparty/thorvg/src/renderer/sw_engine/tvgSwCommon.h
@@ -205,6 +205,7 @@ struct SwDashStroke
float* pattern = nullptr;
uint32_t cnt = 0;
bool curOpGap = false;
+ bool move = true;
};
struct SwShape
diff --git a/thirdparty/thorvg/src/renderer/sw_engine/tvgSwShape.cpp b/thirdparty/thorvg/src/renderer/sw_engine/tvgSwShape.cpp
index 159898c750..4f66cdacc0 100644
--- a/thirdparty/thorvg/src/renderer/sw_engine/tvgSwShape.cpp
+++ b/thirdparty/thorvg/src/renderer/sw_engine/tvgSwShape.cpp
@@ -121,7 +121,10 @@ static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* trans
if (len < dash.curLen) {
dash.curLen -= len;
if (!dash.curOpGap) {
- _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
+ if (dash.move) {
+ _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
+ dash.move = false;
+ }
_outlineLineTo(*dash.outline, to, transform);
}
} else {
@@ -131,7 +134,10 @@ static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* trans
len -= dash.curLen;
_lineSplitAt(cur, dash.curLen, left, right);
if (!dash.curOpGap) {
- _outlineMoveTo(*dash.outline, &left.pt1, transform);
+ if (dash.move || dash.pattern[dash.curIdx] - dash.curLen < FLT_EPSILON) {
+ _outlineMoveTo(*dash.outline, &left.pt1, transform);
+ dash.move = false;
+ }
_outlineLineTo(*dash.outline, &left.pt2, transform);
}
} else {
@@ -142,11 +148,15 @@ static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* trans
dash.curOpGap = !dash.curOpGap;
cur = right;
dash.ptCur = cur.pt1;
+ dash.move = true;
}
//leftovers
dash.curLen -= len;
if (!dash.curOpGap) {
- _outlineMoveTo(*dash.outline, &cur.pt1, transform);
+ if (dash.move) {
+ _outlineMoveTo(*dash.outline, &cur.pt1, transform);
+ dash.move = false;
+ }
_outlineLineTo(*dash.outline, &cur.pt2, transform);
}
if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
@@ -168,21 +178,22 @@ static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ct
if (len < dash.curLen) {
dash.curLen -= len;
if (!dash.curOpGap) {
- _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
+ if (dash.move) {
+ _outlineMoveTo(*dash.outline, &dash.ptCur, transform);
+ dash.move = false;
+ }
_outlineCubicTo(*dash.outline, ctrl1, ctrl2, to, transform);
}
} else {
- bool begin = true; //starting with move_to
while (len > dash.curLen) {
Bezier left, right;
if (dash.curLen > 0) {
len -= dash.curLen;
bezSplitAt(cur, dash.curLen, left, right);
if (!dash.curOpGap) {
- // leftovers from a previous command don't require moveTo
- if (begin || dash.pattern[dash.curIdx] - dash.curLen < FLT_EPSILON) {
+ if (dash.move || dash.pattern[dash.curIdx] - dash.curLen < FLT_EPSILON) {
_outlineMoveTo(*dash.outline, &left.start, transform);
- begin = false;
+ dash.move = false;
}
_outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end, transform);
}
@@ -194,11 +205,15 @@ static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ct
dash.curOpGap = !dash.curOpGap;
cur = right;
dash.ptCur = right.start;
+ dash.move = true;
}
//leftovers
dash.curLen -= len;
if (!dash.curOpGap) {
- _outlineMoveTo(*dash.outline, &cur.start, transform);
+ if (dash.move) {
+ _outlineMoveTo(*dash.outline, &cur.start, transform);
+ dash.move = false;
+ }
_outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end, transform);
}
if (dash.curLen < 1 && TO_SWCOORD(len) > 1) {
@@ -212,6 +227,22 @@ static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ct
}
+static void _dashClose(SwDashStroke& dash, const Matrix* transform)
+{
+ _dashLineTo(dash, &dash.ptStart, transform);
+}
+
+
+static void _dashMoveTo(SwDashStroke& dash, uint32_t offIdx, float offset, const Point* pts, const Matrix* transform)
+{
+ dash.curIdx = offIdx % dash.cnt;
+ dash.curLen = dash.pattern[dash.curIdx] - offset;
+ dash.curOpGap = offIdx % 2;
+ dash.ptStart = dash.ptCur = *pts;
+ dash.move = true;
+}
+
+
static SwOutline* _genDashOutline(const RenderShape* rshape, const Matrix* transform, float length, SwMpool* mpool, unsigned tid)
{
const PathCommand* cmds = rshape->path.cmds.data;
@@ -261,7 +292,6 @@ static SwOutline* _genDashOutline(const RenderShape* rshape, const Matrix* trans
trimmed = true;
//just a dasy style.
} else {
-
if (dash.cnt == 0) return nullptr;
}
@@ -303,15 +333,11 @@ static SwOutline* _genDashOutline(const RenderShape* rshape, const Matrix* trans
while (cmdCnt-- > 0) {
switch (*cmds) {
case PathCommand::Close: {
- _dashLineTo(dash, &dash.ptStart, transform);
+ _dashClose(dash, transform);
break;
}
case PathCommand::MoveTo: {
- //reset the dash
- dash.curIdx = offIdx % dash.cnt;
- dash.curLen = dash.pattern[dash.curIdx] - offset;
- dash.curOpGap = offIdx % 2;
- dash.ptStart = dash.ptCur = *pts;
+ _dashMoveTo(dash, offIdx, offset, pts, transform);
++pts;
break;
}
diff --git a/thirdparty/thorvg/src/renderer/sw_engine/tvgSwStroke.cpp b/thirdparty/thorvg/src/renderer/sw_engine/tvgSwStroke.cpp
index b1bdccbbba..75c5fa5667 100644
--- a/thirdparty/thorvg/src/renderer/sw_engine/tvgSwStroke.cpp
+++ b/thirdparty/thorvg/src/renderer/sw_engine/tvgSwStroke.cpp
@@ -689,7 +689,6 @@ static void _endSubPath(SwStroke& stroke)
//No specific corner processing is required if the turn is 0
if (turn != 0) {
-
//when we turn to the right, the inside is 0
int32_t inside = 0;
diff --git a/thirdparty/thorvg/update-thorvg.sh b/thirdparty/thorvg/update-thorvg.sh
index c36c6e9eaa..0139f522b8 100755
--- a/thirdparty/thorvg/update-thorvg.sh
+++ b/thirdparty/thorvg/update-thorvg.sh
@@ -1,6 +1,6 @@
#!/bin/bash -e
-VERSION=0.11.0
+VERSION=0.11.1
cd thirdparty/thorvg/ || true
rm -rf AUTHORS LICENSE inc/ src/ *.zip *.tar.gz tmp/
@@ -36,6 +36,8 @@ cat << EOF > ../inc/config.h
#define THORVG_SW_RASTER_SUPPORT
#define THORVG_SVG_LOADER_SUPPORT
+#define THORVG_PNG_LOADER_SUPPORT
+#define THORVG_JPG_LOADER_SUPPORT
// For internal debugging:
//#define THORVG_LOG_ENABLED
@@ -51,9 +53,12 @@ cp -rv src/renderer ../src/
# Only sw_engine is enabled.
rm -rfv ../src/renderer/gl_engine
-# Only svg (+raw) loader is enabled.
+# Enabled embedded loaders: raw, JPEG, PNG.
mkdir ../src/loaders
cp -rv src/loaders/svg src/loaders/raw ../src/loaders/
+cp -rv src/loaders/svg src/loaders/jpg ../src/loaders/
+cp -rv src/loaders/svg src/loaders/png ../src/loaders/
+cp -rv src/loaders/svg src/loaders/external_png ../src/loaders/
popd
rm -rf tmp