Skip to content

Commit

Permalink
Make sure all files are properly closed
Browse files Browse the repository at this point in the history
  • Loading branch information
Xrayez committed Jul 22, 2021
1 parent 5a48e63 commit 3b2799e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
29 changes: 14 additions & 15 deletions core/image/drivers/png/resource_saver_indexed_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Error ResourceSaverIndexedPNG::save_image(const String &p_path, const Ref<ImageI
png_infop info_ptr;
png_bytep *row_pointers;

/* initialize stuff */
// Initialize.
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

ERR_FAIL_COND_V(!png_ptr, ERR_CANT_CREATE);
Expand All @@ -56,17 +56,18 @@ Error ResourceSaverIndexedPNG::save_image(const String &p_path, const Ref<ImageI
if (setjmp(png_jmpbuf(png_ptr))) {
ERR_FAIL_V(ERR_CANT_OPEN);
}
//change this

Error err;
FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE, &err);
if (err) {
ERR_FAIL_V(err);
if (err != OK) {
ERR_FAIL_V_MSG(err, "Cannot open file.");
}

png_set_write_fn(png_ptr, f, _write_png_data, NULL);

/* write header */
// Write header.
if (setjmp(png_jmpbuf(png_ptr))) {
f->close();
memdelete(f);
ERR_FAIL_V(ERR_CANT_OPEN);
}

Expand Down Expand Up @@ -135,7 +136,7 @@ Error ResourceSaverIndexedPNG::save_image(const String &p_path, const Ref<ImageI
ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "Cannot save indexed PNG image, unsupported format");
}
}
// RGB
// RGB channels.
int palette_size = p_img->get_palette_size();
png_palette = (png_color *)png_malloc(png_ptr, palette_size * sizeof(png_color));

Expand All @@ -148,7 +149,7 @@ Error ResourceSaverIndexedPNG::save_image(const String &p_path, const Ref<ImageI
}
png_set_PLTE(png_ptr, info_ptr, png_palette, palette_size);

// Alpha
// Alpha channel.
if (has_alpha) {
ERR_FAIL_COND_V(format != Image::FORMAT_RGBA8, ERR_BUG);

Expand All @@ -162,38 +163,36 @@ Error ResourceSaverIndexedPNG::save_image(const String &p_path, const Ref<ImageI
}
png_write_info(png_ptr, info_ptr);

/* write bytes */
// Write data.
if (setjmp(png_jmpbuf(png_ptr))) {
f->close();
memdelete(f);
ERR_FAIL_V(ERR_CANT_OPEN);
}

PoolVector<uint8_t>::Read r;

if (has_palette) {
r = p_img->get_index_data().read();
} else {
r = img->get_data().read();
}

row_pointers = (png_bytep *)memalloc(sizeof(png_bytep) * h);
for (int i = 0; i < h; i++) {
row_pointers[i] = (png_bytep)&r[i * w * cs];
}
png_write_image(png_ptr, row_pointers);

memfree(row_pointers);

/* end write */
if (setjmp(png_jmpbuf(png_ptr))) {
f->close();
memdelete(f);
ERR_FAIL_V(ERR_CANT_OPEN);
}

png_write_end(png_ptr, NULL);
f->close();
memdelete(f);

/* cleanup heap allocation */
// Cleanup.
png_destroy_write_struct(&png_ptr, &info_ptr);
png_free(png_ptr, png_palette);
png_free(png_ptr, png_palette_alpha);
Expand Down
11 changes: 6 additions & 5 deletions core/image/image_indexed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,24 +406,25 @@ Error ImageIndexed::load_indexed_png(const String &p_path) {
Error err;
PoolVector<uint8_t> buffer;

FileAccess *fr = FileAccess::open(p_path, FileAccess::READ, &err);
if (!fr) {
FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
if (!f) {
ERR_PRINT("Error opening file: " + p_path);
return err;
}

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

if (_indexed_png_mem_loader_func) {
Ref<ImageIndexed> img = _indexed_png_mem_loader_func(png, len);
copy_internals_from(img);
create_indexed_from_data(img->get_palette_data(), img->get_index_data());
}
memdelete(fr);
f->close();
memdelete(f);

return err;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ Error ResourceImporterAnimatedTexture::import(const String &p_source_file, const
const int frame_count = MIN(image_frames->get_frame_count(), max_frames);

FileAccess *f = FileAccess::open(p_save_path + ".atex", FileAccess::WRITE);
ERR_FAIL_COND_V(!f, ERR_CANT_OPEN);
ERR_FAIL_COND_V_MSG(!f, ERR_CANT_OPEN, "Error opening file for writing.");

const uint8_t header[4] = { 'G', 'D', 'A', 'T' };
f->store_buffer(header, 4); // Godot Animated Texture.
f->store_32(tex_flags);
f->store_32(frame_count);

// We already assume image frames already contains at least one frame,
// We already assume `ImageFrames` already contains at least one frame,
// and that all frames have the same size.
f->store_32(image_frames->get_frame_image(0)->get_width());
f->store_32(image_frames->get_frame_image(0)->get_height());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Error ResourceImporterSpriteFrames::import(const String &p_source_file, const St
const int frame_count = MIN(image_frames->get_frame_count(), max_frames);

FileAccess *f = FileAccess::open(p_save_path + ".sframes", FileAccess::WRITE);
ERR_FAIL_COND_V(!f, ERR_CANT_OPEN);
ERR_FAIL_COND_V_MSG(!f, ERR_CANT_OPEN, "Error opening file for writing.");

const uint8_t header[4] = { 'G', 'D', 'S', 'F' };
f->store_buffer(header, 4); // GoDot Sprite Frames.
Expand Down
12 changes: 6 additions & 6 deletions modules/gif/image_frames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ Error ImageFrames::save_gif(const String &p_filepath, int p_color_count) {
int error;

FileAccess *f = FileAccess::open(p_filepath, FileAccess::WRITE);
ERR_FAIL_COND_V(!f, ERR_CANT_OPEN);
ERR_FAIL_COND_V_MSG(!f, ERR_CANT_OPEN, "Error opening file.");

GifFileType *gif = EGifOpen(f, save_gif_func, &error);
if (!gif) {
ERR_PRINT(vformat("EGifOpen() failed - %s", error));
memdelete(f);
ERR_PRINT(vformat("EGifOpen() failed - %s.", error));
return ERR_CANT_CREATE;
}

Expand Down Expand Up @@ -158,11 +158,11 @@ Error ImageFrames::save_gif(const String &p_filepath, int p_color_count) {
EGifPutExtensionBlock(gif, 3, sub);
EGifPutExtensionTrailer(gif);
}

// Write out the GIF file.
EGifCloseFile(gif, &error);
if (f) {
memdelete(f);
}
f->close();
memdelete(f);

return OK;
}
#endif // GOOST_ImageIndexed
Expand Down

0 comments on commit 3b2799e

Please sign in to comment.