Skip to content

Commit

Permalink
ReadWrite proxy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Meptl committed May 9, 2023
1 parent 1218180 commit 64fc9b5
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 104 deletions.
6 changes: 2 additions & 4 deletions core/image/drivers/png/image_loader_indexed_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ Error ImageLoaderIndexedPNG::_load_image(void *rf_up, png_rw_ptr p_func, Ref<Ima

dstbuff.resize(rowsize * height);

Vector<uint8_t>::Write dstbuff_write = dstbuff.write();

uint8_t *data = dstbuff_write.ptr();
uint8_t *data = dstbuff.ptrw();

uint8_t **row_p = memnew_arr(uint8_t *, height);

Expand All @@ -155,7 +153,7 @@ Error ImageLoaderIndexedPNG::_load_image(void *rf_up, png_rw_ptr p_func, Ref<Ima

Vector<uint8_t> palette_data;
palette_data.resize(palette_size * 4);
Vector<uint8_t>::Write w = palette_data.write();
uint8_t *w = palette_data.ptrw();

if (png_palette_alpha && palette_alpha_size == palette_size) {
for (int i = 0; i < palette_size; i++) {
Expand Down
8 changes: 4 additions & 4 deletions core/image/drivers/png/resource_saver_indexed_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Error ResourceSaverIndexedPNG::save_image(const String &p_path, const Ref<ImageI
png_bytep png_palette_alpha = nullptr;

if (has_palette) {
Vector<uint8_t>::Read r = p_img->get_palette_data().read();
const uint8_t *r = p_img->get_palette_data().ptr();

int ps = 4;

Expand Down Expand Up @@ -164,11 +164,11 @@ Error ResourceSaverIndexedPNG::save_image(const String &p_path, const Ref<ImageI
ERR_FAIL_V(ERR_CANT_OPEN);
}

Vector<uint8_t>::Read r;
const uint8_t *r;
if (has_palette) {
r = p_img->get_index_data().read();
r = p_img->get_index_data().ptr();
} else {
r = img->get_data().read();
r = img->get_data().ptr();
}
png_bytep *row_pointers = (png_bytep *)memalloc(sizeof(png_bytep) * h);
for (int i = 0; i < h; ++i) {
Expand Down
22 changes: 3 additions & 19 deletions core/image/goost_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ void GoostImage::replace_color(Ref<Image> p_image, const Color &p_color, const C
return;
}

p_image->lock();

for (int y = 0; y < p_image->get_height(); ++y) {
for (int x = 0; x < p_image->get_width(); ++x) {
if (p_image->get_pixel(x, y) == p_color) {
p_image->set_pixel(x, y, p_with_color);
}
}
}
p_image->unlock();
}

// Based on flood-fill algorithm.
Expand All @@ -40,7 +37,6 @@ Ref<Image> GoostImage::bucket_fill(Ref<Image> p_image, const Point2 &p_at, const
if (!has_pixelv(p_image, p_at)) {
return Ref<Image>();
}
p_image->lock();

Color filling_color = p_image->get_pixelv(p_at);

Expand All @@ -51,7 +47,6 @@ Ref<Image> GoostImage::bucket_fill(Ref<Image> p_image, const Point2 &p_at, const

Ref<Image> fill_image = memnew(Image);
fill_image->create(width, height, mipmaps, format);
fill_image->lock();

Vector2 at;
Vector2 pos = p_at;
Expand Down Expand Up @@ -114,9 +109,6 @@ Ref<Image> GoostImage::bucket_fill(Ref<Image> p_image, const Point2 &p_at, const
p_image->blend_rect(fill_image, fill_rect, Point2());
}

fill_image->unlock();
p_image->unlock();

return fill_image;
}

Expand All @@ -139,11 +131,6 @@ void GoostImage::resize_hqx(Ref<Image> p_image, int p_scale) {
const int new_height = p_image->get_height() * p_scale;
dest.resize(new_width * new_height * 4);
{
Vector<uint8_t>::Read r = src.read();
Vector<uint8_t>::Write w = dest.write();

ERR_FAIL_COND(!r.ptr());

HQx *hqx;
if (p_scale == 2) {
hqx = memnew(HQ2x);
Expand All @@ -152,7 +139,7 @@ void GoostImage::resize_hqx(Ref<Image> p_image, int p_scale) {
} else {
hqx = memnew(HQ2x); // Fallback to HQ2x in all cases.
}
hqx->resize((const uint32_t *)r.ptr(), p_image->get_width(), p_image->get_height(), (uint32_t *)w.ptr());
hqx->resize((const uint32_t *)src.ptr(), p_image->get_width(), p_image->get_height(), (uint32_t *)dest.ptrw());
memdelete(hqx);
}
p_image->create(new_width, new_height, false, Image::FORMAT_RGBA8, dest);
Expand Down Expand Up @@ -554,9 +541,7 @@ PIX *pix_create_from_image(Ref<Image> p_image) {
const Image::Format format = p_image->get_format();

Vector<uint8_t> src = p_image->get_data();
Vector<uint8_t>::Read read = src.read();
ERR_FAIL_COND_V(!read.ptr(), nullptr);
const uint8_t *r = read.ptr();
const uint8_t *r = src.ptr();

const int w = p_image->get_width();
const int h = p_image->get_height();
Expand Down Expand Up @@ -618,8 +603,7 @@ void _image_from_pix(Ref<Image> p_image, PIX *p_pix, bool p_include_alpha) {
{
const int data_size = Image::get_image_data_size(width, height, format);
image_data.resize(data_size);
Vector<uint8_t>::Write write = image_data.write();
uint8_t *w = write.ptr();
uint8_t *w = image_data.ptrw();

const int pixel_count = width * height;
const int pixel_size = Image::get_format_pixel_size(format);
Expand Down
71 changes: 18 additions & 53 deletions core/image/image_indexed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ Error ImageIndexed::create_indexed(int p_num_palette_entries) {
index_data.resize(0);
index_data.resize(num_pixels);
{
Vector<uint8_t>::Write w = index_data.write();
memset(w.ptr(), 0, num_pixels);
memset(index_data.ptrw(), 0, num_pixels);
}

const int ps = get_format_pixel_size(get_format());
Expand All @@ -29,8 +28,7 @@ Error ImageIndexed::create_indexed(int p_num_palette_entries) {
palette_data.resize(0);
palette_data.resize(num_colors * ps);
{
Vector<uint8_t>::Write w = palette_data.write();
memset(w.ptr(), 0, num_colors * ps);
memset(palette_data.ptrw(), 0, num_colors * ps);
}
return OK;
}
Expand Down Expand Up @@ -63,10 +61,8 @@ Error ImageIndexed::create_indexed_from_data(const Vector<uint8_t> &p_palette_da
#ifdef DEBUG_ENABLED
// Ensure all indices point to valid palette entries
{
Vector<uint8_t>::Read ind = p_index_data.read();

for (int i = 0; i < index_size; ++i) {
ERR_FAIL_COND_V_MSG(ind[i] > palette_size - 1, ERR_INVALID_DATA, "Indices exceed (maximum) palette size.");
ERR_FAIL_COND_V_MSG(p_index_data.ptr()[i] > palette_size - 1, ERR_INVALID_DATA, "Indices exceed (maximum) palette size.");
}
}
#endif
Expand All @@ -77,7 +73,7 @@ Error ImageIndexed::create_indexed_from_data(const Vector<uint8_t> &p_palette_da
}

void ImageIndexed::set_pixel_indexed(int p_x, int p_y, int p_index) {
uint8_t *ptr = write_lock_indexed.ptr();
uint8_t *ptr = index_data.ptrw();

#ifdef DEBUG_ENABLED
if (!ptr) {
Expand All @@ -94,7 +90,7 @@ void ImageIndexed::set_pixel_indexed(int p_x, int p_y, int p_index) {
}

int ImageIndexed::get_pixel_indexed(int p_x, int p_y) const {
uint8_t *ptr = write_lock_indexed.ptr();
uint8_t *ptr = index_data.ptrw();

#ifdef DEBUG_ENABLED
if (!ptr) {
Expand All @@ -112,15 +108,6 @@ int ImageIndexed::get_pixel_indexed(int p_x, int p_y) const {
return index;
}

void ImageIndexed::lock_indexed() {
ERR_FAIL_COND(index_data.size() == 0);
write_lock_indexed = index_data.write();
}

void ImageIndexed::unlock_indexed() {
write_lock_indexed = Vector<uint8_t>::Write();
}

real_t ImageIndexed::generate_palette(int p_num_colors, DitherMode p_dither, bool p_with_alpha, bool p_high_quality) {
ERR_FAIL_COND_V_MSG(empty(), -1.0, "Cannot generate a palette from an empty image.");
ERR_FAIL_COND_V_MSG(get_format() != FORMAT_RGBA8, -1.0, "Cannot generate a palette, convert to FORMAT_RBGA8 first.");
Expand All @@ -133,8 +120,7 @@ real_t ImageIndexed::generate_palette(int p_num_colors, DitherMode p_dither, boo

// Init
Vector<uint8_t> data = get_data();
Vector<uint8_t>::Write w_src = data.write();
uint8_t *src = w_src.ptr();
uint8_t *src = data.ptrw();

exq_data *pExq = exq_init();
if (!p_with_alpha) {
Expand All @@ -149,17 +135,15 @@ real_t ImageIndexed::generate_palette(int p_num_colors, DitherMode p_dither, boo
palette_data.resize(0);
palette_data.resize(num_colors * 4);

Vector<uint8_t>::Write w_pal_raw = palette_data.write();
uint8_t *pal_raw = w_pal_raw.ptr();
uint8_t *pal_raw = palette_data.ptrw();

exq_get_palette(pExq, pal_raw, num_colors);

// Map indices to palette (doesn't overwrite original image)
index_data.resize(0);
index_data.resize(num_pixels);

Vector<uint8_t>::Write w_dest = index_data.write();
uint8_t *dest = w_dest.ptr();
uint8_t *dest = index_data.ptrw();

switch (p_dither) {
case DITHER_NONE: {
Expand Down Expand Up @@ -193,11 +177,10 @@ Error ImageIndexed::apply_palette() {

Vector<uint8_t> dest_data;
dest_data.resize(get_data().size());
Vector<uint8_t>::Write w_dest = dest_data.write();
uint8_t *dest = w_dest.ptr();
uint8_t *dest = dest_data.ptrw();

Vector<uint8_t>::Read pal = palette_data.read();
Vector<uint8_t>::Read ind = index_data.read();
const uint8_t *pal = palette_data.ptr();
const uint8_t *ind = index_data.ptr();

int ps = get_format_pixel_size(get_format());
int num_pixels = get_width() * get_height();
Expand Down Expand Up @@ -255,13 +238,12 @@ void ImageIndexed::set_palette(const PackedColorArray &p_palette) {
int ps = get_format_pixel_size(get_format());
int num_colors = p_palette.size();

PackedColorArray *r = p_palette.ptr();
Color *r = p_palette.ptr();

palette_data.resize(0);
palette_data.resize(num_colors * ps);

Vector<uint8_t>::Write w_pal = palette_data.write();
uint8_t *w = w_pal.ptr();
uint8_t *w = palette_data.ptrw();

switch (ps) {
case 3: {
Expand Down Expand Up @@ -299,10 +281,8 @@ PackedColorArray ImageIndexed::get_palette() const {
PackedColorArray palette;
palette.resize(num_colors);

PackedColorArray *w = palette.ptrw();

Vector<uint8_t>::Read r_pal = palette_data.read();
const uint8_t *r = r_pal.ptr();
Color *w = palette.ptrw();
const Color *r = palette.ptr();

switch (ps) {
case 3: {
Expand Down Expand Up @@ -339,8 +319,7 @@ void ImageIndexed::set_palette_color(int p_idx, const Color p_color) {

ERR_FAIL_INDEX(ofs, palette_data.size());

Vector<uint8_t>::Write write = palette_data.write();
uint8_t *ptr = write.ptr();
uint8_t *ptr = palette_data.ptrw();

switch (pixel_size) {
case 3: {
Expand Down Expand Up @@ -368,8 +347,7 @@ Color ImageIndexed::get_palette_color(int p_idx) const {

ERR_FAIL_INDEX_V(ofs, palette_data.size(), Color());

Vector<uint8_t>::Read read = palette_data.read();
const uint8_t *ptr = read.ptr();
const uint8_t *ptr = palette_data.ptr();

switch (ps) {
case 3: {
Expand Down Expand Up @@ -414,8 +392,7 @@ Error ImageIndexed::load_indexed_png(const String &p_path) {

int len = f->get_len();
buffer.resize(len);
Vector<uint8_t>::Write w = buffer.write();
uint8_t *png = w.ptr();
uint8_t *png = buffer.ptrw();
f->get_buffer(png, len);

if (_indexed_png_mem_loader_func) {
Expand All @@ -436,25 +413,13 @@ Error ImageIndexed::save_indexed_png(const String &p_path) const {
return save_indexed_png_func(p_path, Ref<ImageIndexed>((ImageIndexed *)this));
}

ImageIndexed::ImageIndexed() {
}

ImageIndexed::~ImageIndexed() {
if (write_lock_indexed.ptr()) {
unlock_indexed();
}
}

void ImageIndexed::_bind_methods() {
ClassDB::bind_method(D_METHOD("create_indexed", "num_palette_entries"), &ImageIndexed::create_indexed, DEFVAL(MAX_PALETTE_SIZE));
ClassDB::bind_method(D_METHOD("create_indexed_from_data", "palette_data", "index_data"), &ImageIndexed::create_indexed_from_data);

ClassDB::bind_method(D_METHOD("set_pixel_indexed", "x", "y", "index"), &ImageIndexed::set_pixel_indexed);
ClassDB::bind_method(D_METHOD("get_pixel_indexed", "x", "y"), &ImageIndexed::get_pixel_indexed);

ClassDB::bind_method(D_METHOD("lock_indexed"), &ImageIndexed::lock_indexed);
ClassDB::bind_method(D_METHOD("unlock_indexed"), &ImageIndexed::unlock_indexed);

ClassDB::bind_method(D_METHOD("generate_palette", "num_colors", "dithering", "with_alpha", "high_quality"), &ImageIndexed::generate_palette, DEFVAL(MAX_PALETTE_SIZE), DEFVAL(DITHER_NONE), DEFVAL(true), DEFVAL(false));
ClassDB::bind_method(D_METHOD("clear_palette"), &ImageIndexed::clear_palette);
ClassDB::bind_method(D_METHOD("apply_palette"), &ImageIndexed::apply_palette);
Expand Down
5 changes: 0 additions & 5 deletions core/image/image_indexed.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ class ImageIndexed : public Image {
Vector<uint8_t> index_data;
Vector<uint8_t> palette_data;

Vector<uint8_t>::Write write_lock_indexed;

protected:
static void _bind_methods();

Expand Down Expand Up @@ -45,9 +43,6 @@ class ImageIndexed : public Image {
void set_pixel_indexed(int p_x, int p_y, int p_index);
int get_pixel_indexed(int p_x, int p_y) const;

void lock_indexed();
void unlock_indexed();

real_t generate_palette(int p_num_colors = MAX_PALETTE_SIZE, DitherMode p_dither = DITHER_NONE, bool p_with_alpha = true, bool p_high_quality = false);
void clear_palette();
Error apply_palette();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ Error ResourceImporterAnimatedTexture::import(const String &p_source_file, const
for (int i = 0; i < frame_count; ++i) {
// Frame image data.
Vector<uint8_t> data = image_frames->get_frame_image(i)->get_data();
Vector<uint8_t>::Read r = data.read();
f->store_32(data.size());
f->store_buffer(r.ptr(), data.size());
f->store_buffer(data.ptr(), data.size());
// Frame delay data.
const real_t delay = image_frames->get_frame_delay(i);
f->store_real(delay);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ Error ResourceImporterSpriteFrames::import(const String &p_source_file, const St
for (int i = 0; i < frame_count; ++i) {
// Frame image data.
Vector<uint8_t> data = image_frames->get_frame_image(i)->get_data();
Vector<uint8_t>::Read r = data.read();
f->store_32(data.size());
f->store_buffer(r.ptr(), data.size());
f->store_buffer(data.ptr(), data.size());
}
// Animation speed.
real_t speed = 1.0 / average_time;
Expand Down
5 changes: 2 additions & 3 deletions modules/gif/image_frames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Error ImageFrames::save_gif(const String &p_filepath, int p_color_count) {
Vector<uint8_t> color_map = indexed->get_palette_data();
ColorMapObject *gif_color_map = nullptr;
{
Vector<uint8_t>::Read r = color_map.read();
uint8_t *r = color_map.ptr();
GifColorType *gif_colors = (GifColorType *)malloc(sizeof(GifColorType) * indexed->get_palette_size());
const int pixel_size = Image::get_format_pixel_size(indexed->get_format());

Expand Down Expand Up @@ -153,8 +153,7 @@ Error ImageFrames::save_gif(const String &p_filepath, int p_color_count) {
GifFreeMapObject(gif_color_map);

Vector<uint8_t> index_data = indexed->get_index_data();
Vector<uint8_t>::Write w = index_data.write();
GifPixelType *raster = static_cast<GifPixelType *>(w.ptr());
GifPixelType *raster = static_cast<GifPixelType *>(index_data.ptrw());

for (int j = 0; j < frame->get_height(); j++) {
if (EGifPutLine(gif, raster + j * frame->get_width(), frame->get_width()) == GIF_ERROR) {
Expand Down
Loading

0 comments on commit 64fc9b5

Please sign in to comment.