Skip to content

Commit

Permalink
No need to use fmemopen
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Nov 23, 2023
1 parent 79f3692 commit c2b4df0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
30 changes: 18 additions & 12 deletions kitty/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,22 @@ print_png_read_error(png_read_data *d, const char *code, const char* msg) {
d->error.used += snprintf(d->error.buf + d->error.used, d->error.capacity - d->error.used, "%s: %s ", code, msg);
}

bool
png_from_data(void *png_data, size_t png_data_sz, const char *path_for_error_messages, uint8_t** data, unsigned int* width, unsigned int* height, size_t* sz) {
png_read_data d = {.err_handler=print_png_read_error};
inflate_png_inner(&d, png_data, png_data_sz);
if (!d.ok) {
log_error("Failed to decode PNG image at: %s with error: %s", path_for_error_messages, d.error.used > 0 ? d.error.buf : "");
free(d.decompressed); free(d.row_pointers); free(d.error.buf);
return false;
}
*data = d.decompressed;
free(d.row_pointers); free(d.error.buf);
*sz = d.sz;
*height = d.height; *width = d.width;
return true;
}

bool
png_from_file_pointer(FILE *fp, const char *path_for_error_messages, uint8_t** data, unsigned int* width, unsigned int* height, size_t* sz) {
size_t capacity = 16*1024, pos = 0;
Expand All @@ -367,19 +383,9 @@ png_from_file_pointer(FILE *fp, const char *path_for_error_messages, uint8_t** d
return false;
}
}
png_read_data d = {.err_handler=print_png_read_error};
inflate_png_inner(&d, buf, pos);
bool ret = png_from_data(buf, pos, path_for_error_messages, data, width, height, sz);
free(buf);
if (!d.ok) {
log_error("Failed to decode PNG image at: %s with error: %s", path_for_error_messages, d.error.used > 0 ? d.error.buf : "");
free(d.decompressed); free(d.row_pointers); free(d.error.buf);
return false;
}
*data = d.decompressed;
free(d.row_pointers); free(d.error.buf);
*sz = d.sz;
*height = d.height; *width = d.width;
return true;
return ret;
}

bool
Expand Down
1 change: 1 addition & 0 deletions kitty/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,6 @@ void grman_remove_all_cell_images(GraphicsManager *self);
void gpu_data_for_image(ImageRenderData *ans, float left, float top, float right, float bottom);
bool png_from_file_pointer(FILE* fp, const char *path, uint8_t** data, unsigned int* width, unsigned int* height, size_t* sz);
bool png_path_to_bitmap(const char *path, uint8_t** data, unsigned int* width, unsigned int* height, size_t* sz);
bool png_from_data(void *png_data, size_t png_data_sz, const char *path_for_error_messages, uint8_t** data, unsigned int* width, unsigned int* height, size_t* sz);
bool scan_active_animations(GraphicsManager *self, const monotonic_t now, monotonic_t *minimum_gap, bool os_window_context_set);
void scale_rendered_graphic(ImageRenderData*, float xstart, float ystart, float x_scale, float y_scale);
9 changes: 1 addition & 8 deletions kitty/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -1154,14 +1154,7 @@ pyset_background_image(PyObject *self UNUSED, PyObject *args) {
if (!bgimage) return PyErr_NoMemory();
bool ok;
if (png_data) {
FILE *fp = fmemopen(png_data, png_data_size, "r");
if (fp == NULL) {
PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
free(bgimage);
return NULL;
}
ok = png_from_file_pointer(fp, path, &bgimage->bitmap, &bgimage->width, &bgimage->height, &size);
fclose(fp);
ok = png_from_data(png_data, png_data_size, path, &bgimage->bitmap, &bgimage->width, &bgimage->height, &size);
} else {
ok = png_path_to_bitmap(path, &bgimage->bitmap, &bgimage->width, &bgimage->height, &size);
}
Expand Down
6 changes: 1 addition & 5 deletions kitty/window_logo.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ find_or_create_window_logo(WindowLogoTable *head, const char *path, void *png_da
if (png_data == NULL) {
ok = png_path_to_bitmap(path, &s->wl.bitmap, &s->wl.width, &s->wl.height, &size);
} else {
FILE *fp = fmemopen(png_data, png_data_size, "r");
if (fp != NULL) {
ok = png_from_file_pointer(fp, path, &s->wl.bitmap, &s->wl.width, &s->wl.height, &size);
fclose(fp);
}
ok = png_from_data(png_data, png_data_size, path, &s->wl.bitmap, &s->wl.width, &s->wl.height, &size);
}
if (ok) s->wl.load_from_disk_ok = true;
s->refcnt++;
Expand Down

0 comments on commit c2b4df0

Please sign in to comment.