summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/cowdata.h6
-rw-r--r--core/image.cpp23
-rw-r--r--core/image.h6
-rw-r--r--core/io/image_loader.cpp2
-rw-r--r--core/vector.h49
5 files changed, 56 insertions, 30 deletions
diff --git a/core/cowdata.h b/core/cowdata.h
index 66e7d1c343..6a8f644d53 100644
--- a/core/cowdata.h
+++ b/core/cowdata.h
@@ -100,6 +100,7 @@ private:
}
void _unref(void *p_data);
+ void _ref(const CowData *p_from);
void _ref(const CowData &p_from);
void _copy_on_write();
@@ -301,6 +302,11 @@ Error CowData<T>::resize(int p_size) {
}
template <class T>
+void CowData<T>::_ref(const CowData *p_from) {
+ _ref(*p_from);
+}
+
+template <class T>
void CowData<T>::_ref(const CowData &p_from) {
if (_ptr == p_from._ptr)
diff --git a/core/image.cpp b/core/image.cpp
index 19440d1718..65905c83e8 100644
--- a/core/image.cpp
+++ b/core/image.cpp
@@ -987,8 +987,10 @@ int Image::_get_dst_image_size(int p_width, int p_height, Format p_format, int &
int pixsize = get_format_pixel_size(p_format);
int pixshift = get_format_pixel_rshift(p_format);
int block = get_format_block_size(p_format);
- int minw, minh;
- get_format_min_pixel_size(p_format, minw, minh);
+ //technically, you can still compress up to 1 px no matter the format, so commenting this
+ //int minw, minh;
+ //get_format_min_pixel_size(p_format, minw, minh);
+ int minw = 1, minh = 1;
while (true) {
@@ -1304,7 +1306,7 @@ void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_forma
int size = _get_dst_image_size(p_width, p_height, p_format, mm, p_use_mipmaps ? -1 : 0);
if (size != p_data.size()) {
- ERR_EXPLAIN("Expected data size of " + itos(size) + " in Image::create()");
+ ERR_EXPLAIN("Expected data size of " + itos(size) + " bytes in Image::create(), got instead " + itos(p_data.size()) + " bytes.");
ERR_FAIL_COND(p_data.size() != size);
}
@@ -1592,10 +1594,10 @@ Error Image::save_png(const String &p_path) const {
return save_png_func(p_path, Ref<Image>((Image *)this));
}
-int Image::get_image_data_size(int p_width, int p_height, Format p_format, int p_mipmaps) {
+int Image::get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps) {
int mm;
- return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmaps);
+ return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmaps ? -1 : 0);
}
int Image::get_image_required_mipmaps(int p_width, int p_height, Format p_format) {
@@ -2376,6 +2378,17 @@ Image::DetectChannels Image::get_detected_channels() {
return DETECTED_RGBA;
}
+void Image::optimize_channels() {
+ switch (get_detected_channels()) {
+ case DETECTED_L: convert(FORMAT_L8); break;
+ case DETECTED_LA: convert(FORMAT_LA8); break;
+ case DETECTED_R: convert(FORMAT_R8); break;
+ case DETECTED_RG: convert(FORMAT_RG8); break;
+ case DETECTED_RGB: convert(FORMAT_RGB8); break;
+ case DETECTED_RGBA: convert(FORMAT_RGBA8); break;
+ }
+}
+
void Image::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_width"), &Image::get_width);
diff --git a/core/image.h b/core/image.h
index 8c4854e053..c8dd647c31 100644
--- a/core/image.h
+++ b/core/image.h
@@ -116,7 +116,8 @@ public:
enum CompressSource {
COMPRESS_SOURCE_GENERIC,
COMPRESS_SOURCE_SRGB,
- COMPRESS_SOURCE_NORMAL
+ COMPRESS_SOURCE_NORMAL,
+ COMPRESS_SOURCE_LAYERED,
};
//some functions provided by something else
@@ -272,7 +273,7 @@ public:
static int get_format_block_size(Format p_format);
static void get_format_min_pixel_size(Format p_format, int &r_w, int &r_h);
- static int get_image_data_size(int p_width, int p_height, Format p_format, int p_mipmaps = 0);
+ static int get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps = false);
static int get_image_required_mipmaps(int p_width, int p_height, Format p_format);
enum CompressMode {
@@ -329,6 +330,7 @@ public:
};
DetectChannels get_detected_channels();
+ void optimize_channels();
Color get_pixelv(const Point2 &p_src) const;
Color get_pixel(int p_x, int p_y) const;
diff --git a/core/io/image_loader.cpp b/core/io/image_loader.cpp
index 614fbb771f..b8fd13d67c 100644
--- a/core/io/image_loader.cpp
+++ b/core/io/image_loader.cpp
@@ -185,5 +185,5 @@ bool ResourceFormatLoaderImage::handles_type(const String &p_type) const {
String ResourceFormatLoaderImage::get_resource_type(const String &p_path) const {
- return "Image";
+ return p_path.get_extension().to_lower() == "image" ? "Image" : String();
}
diff --git a/core/vector.h b/core/vector.h
index 7e3da34be0..52e8758f9b 100644
--- a/core/vector.h
+++ b/core/vector.h
@@ -44,17 +44,16 @@
template <class T>
class VectorWriteProxy {
friend class Vector<T>;
- Vector<T> &_parent;
+ CowData<T> *_parent;
- _FORCE_INLINE_ VectorWriteProxy(Vector<T> &parent) :
+ _FORCE_INLINE_ VectorWriteProxy(CowData<T> *parent) :
_parent(parent){};
- VectorWriteProxy(const VectorWriteProxy<T> &p_other);
public:
_FORCE_INLINE_ T &operator[](int p_index) {
- CRASH_BAD_INDEX(p_index, _parent.size());
+ CRASH_BAD_INDEX(p_index, _parent->size());
- return _parent.ptrw()[p_index];
+ return _parent->ptrw()[p_index];
}
};
@@ -62,39 +61,39 @@ template <class T>
class Vector {
friend class VectorWriteProxy<T>;
- CowData<T> _cowdata;
+ CowData<T> *_cowdata;
public:
VectorWriteProxy<T> write;
bool push_back(const T &p_elem);
- void remove(int p_index) { _cowdata.remove(p_index); }
+ void remove(int p_index) { _cowdata->remove(p_index); }
void erase(const T &p_val) {
int idx = find(p_val);
if (idx >= 0) remove(idx);
};
void invert();
- _FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); }
- _FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); }
+ _FORCE_INLINE_ T *ptrw() { return _cowdata->ptrw(); }
+ _FORCE_INLINE_ const T *ptr() const { return _cowdata->ptr(); }
_FORCE_INLINE_ void clear() { resize(0); }
- _FORCE_INLINE_ bool empty() const { return _cowdata.empty(); }
+ _FORCE_INLINE_ bool empty() const { return _cowdata->empty(); }
- _FORCE_INLINE_ T get(int p_index) { return _cowdata.get(p_index); }
- _FORCE_INLINE_ const T get(int p_index) const { return _cowdata.get(p_index); }
- _FORCE_INLINE_ void set(int p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); }
- _FORCE_INLINE_ int size() const { return _cowdata.size(); }
- Error resize(int p_size) { return _cowdata.resize(p_size); }
- _FORCE_INLINE_ const T &operator[](int p_index) const { return _cowdata.get(p_index); }
- Error insert(int p_pos, const T &p_val) { return _cowdata.insert(p_pos, p_val); }
+ _FORCE_INLINE_ T get(int p_index) { return _cowdata->get(p_index); }
+ _FORCE_INLINE_ const T get(int p_index) const { return _cowdata->get(p_index); }
+ _FORCE_INLINE_ void set(int p_index, const T &p_elem) { _cowdata->set(p_index, p_elem); }
+ _FORCE_INLINE_ int size() const { return _cowdata->size(); }
+ Error resize(int p_size) { return _cowdata->resize(p_size); }
+ _FORCE_INLINE_ const T &operator[](int p_index) const { return _cowdata->get(p_index); }
+ Error insert(int p_pos, const T &p_val) { return _cowdata->insert(p_pos, p_val); }
void append_array(const Vector<T> &p_other);
template <class C>
void sort_custom() {
- int len = _cowdata.size();
+ int len = _cowdata->size();
if (len == 0)
return;
@@ -110,7 +109,7 @@ public:
void ordered_insert(const T &p_val) {
int i;
- for (i = 0; i < _cowdata.size(); i++) {
+ for (i = 0; i < _cowdata->size(); i++) {
if (p_val < operator[](i)) {
break;
@@ -136,13 +135,19 @@ public:
}
_FORCE_INLINE_ Vector() :
- write(VectorWriteProxy<T>(*this)) {}
+ _cowdata(new CowData<T>()),
+ write(VectorWriteProxy<T>(_cowdata)) {}
_FORCE_INLINE_ Vector(const Vector &p_from) :
- write(VectorWriteProxy<T>(*this)) { _cowdata._ref(p_from._cowdata); }
+ _cowdata(new CowData<T>()),
+ write(VectorWriteProxy<T>(_cowdata)) { _cowdata->_ref(p_from._cowdata); }
inline Vector &operator=(const Vector &p_from) {
- _cowdata._ref(p_from._cowdata);
+ _cowdata->_ref(p_from._cowdata);
return *this;
}
+
+ _FORCE_INLINE_ ~Vector() {
+ delete _cowdata;
+ }
};
template <class T>