From f21cfab7b4deba88da7ecd2530538a91ad77f46a Mon Sep 17 00:00:00 2001 From: Anurag Aggarwal Date: Wed, 1 Jul 2020 12:02:19 +0530 Subject: [PATCH] Use memory for saving images --- src/include/misc-f.h | 4 ++-- src/include/ui_helper.h | 2 +- src/ui-hlp/render.cpp | 2 +- src/ui-hlp/ui_helper.cpp | 26 ++++++++-------------- src/ui/image_qt.cpp | 47 +++++++++------------------------------- 5 files changed, 23 insertions(+), 58 deletions(-) diff --git a/src/include/misc-f.h b/src/include/misc-f.h index 2496a2da..34aebbc5 100644 --- a/src/include/misc-f.h +++ b/src/include/misc-f.h @@ -4,8 +4,8 @@ #include "config.h" struct image; -const char *writepng(xio_constpath filename, const struct image *image); -const char *readpng(xio_constpath filename); +const char *writepng(xio_constpath filename, const struct image *image, xio_file xpf_data); +const char* readpng(xio_constpath filename); void XaoS_srandom(unsigned int x); long int XaoS_random(void); char *mystrdup(const char *); diff --git a/src/include/ui_helper.h b/src/include/ui_helper.h index 539cb2e7..d03014c6 100644 --- a/src/include/ui_helper.h +++ b/src/include/ui_helper.h @@ -387,7 +387,7 @@ void uih_autopilot_off(uih_context *c); /*misc functions */ int uih_update(uih_context *c, int mousex, int mousey, int mousebuttons); -const char *uih_save(struct uih_context *c, xio_constpath filename); +const char *uih_save(struct uih_context *c, xio_constpath filename, xio_file xpf_data); void uih_tbreak(uih_context *c); double uih_displayed(uih_context *c); void uih_do_fractal(uih_context *c); diff --git a/src/ui-hlp/render.cpp b/src/ui-hlp/render.cpp index c91b8f02..97ab2e66 100644 --- a/src/ui-hlp/render.cpp +++ b/src/ui-hlp/render.cpp @@ -301,7 +301,7 @@ int uih_renderanimation(struct uih_context *gc1, const char *basename, curframe.angle = uih->fcontext->angle; curframe.name = s; curframe.newimage = newimage; - writepng(s, uih->image); + writepng(s, uih->image, NULL); uih_displayed(uih); lastframenum = framenum; } else { diff --git a/src/ui-hlp/ui_helper.cpp b/src/ui-hlp/ui_helper.cpp index 0f880b33..70683e33 100644 --- a/src/ui-hlp/ui_helper.cpp +++ b/src/ui-hlp/ui_helper.cpp @@ -628,17 +628,13 @@ void uih_loadpngfile(struct uih_context *c, xio_constpath d) uih_error(c, strerror(errno)); return; } - const char *s = readpng(d); - if(s != NULL) { + const char* xpf_chunk = readpng(d); + if(xpf_chunk == NULL) { uih_error(c, TR("Error", "Could not open image")); return; } - int pathlength = strlen(d) + 16; - static char* filepath; - filepath = (char* )malloc(pathlength * sizeof (char)); - strcpy(filepath, xio_getdirectory(d)); - strcat(filepath, ".xaos_temp.xpf"); - uih_loadfile(c, filepath); + xio_file xpf_data = xio_strropen(xpf_chunk); + uih_load(c, xpf_data, d); if(c->errstring == NULL) { char s[256]; sprintf(s, TR("Message", "File %s loaded."), d); @@ -663,13 +659,9 @@ void uih_savepngfile(struct uih_context *c, xio_constpath d) return; } c->errstring = NULL; - int pathlength = strlen(d) + 16; - static char* filepath; - filepath = (char* )malloc(pathlength * sizeof (char)); - strcpy(filepath, xio_getdirectory(d)); - strcat(filepath, ".xaos_temp.xpf"); - uih_saveposfile(c, filepath); - s = uih_save(c, d); + xio_file xpf_data = xio_strwopen(); + uih_save_position(c, xpf_data, UIH_SAVEPOS); + s = uih_save(c, d, xpf_data); if (s != NULL) uih_error(c, s); if (c->errstring == NULL) { @@ -786,13 +778,13 @@ void uih_saveanimfile(struct uih_context *c, xio_constpath d) uih_updatemenus(c, "record"); } -const char *uih_save(struct uih_context *c, xio_constpath filename) +const char *uih_save(struct uih_context *c, xio_constpath filename, xio_file xpf_data) { const char *r; uih_cycling_stop(c); uih_stoptimers(c); uih_clearwindows(c); - r = writepng(filename, c->queue->saveimage); + r = writepng(filename, c->queue->saveimage, xpf_data); uih_cycling_continue(c); uih_resumetimers(c); return (r); diff --git a/src/ui/image_qt.cpp b/src/ui/image_qt.cpp index b6be1fb5..347e7935 100644 --- a/src/ui/image_qt.cpp +++ b/src/ui/image_qt.cpp @@ -4,6 +4,7 @@ #include "filter.h" #include "grlib.h" #include "xio.h" +#include "misc-f.h" static QFont getFont(void *font) { if (font) @@ -62,55 +63,27 @@ int xtextcharw(struct image */*image*/, void *font, const char c) } // Saves image as png with xpf chunk data -const char *writepng(xio_constpath filename, const struct image *image) +const char *writepng(xio_constpath filename, const struct image *image, xio_file xpf_data) { QImage *qimage = reinterpret_cast(image->data)[image->currimage]; - int pathlength = strlen(filename) + 16; - static char* filepath; - filepath = (char* )malloc(pathlength * sizeof (char)); - strcpy(filepath, xio_getdirectory(filename)); - strcat(filepath, ".xaos_temp.xpf"); - QFile f(filepath); - if(!f.open(QFile::ReadOnly | - QFile::Text)) - { - qDebug()<<"Could not open the file for reading"; - qDebug()<<"Image will be created without xpf data"; + if(xpf_data != NULL){ + QString xpf_chunk = xio_getstring(xpf_data); + qimage->setText("Metadata", xpf_chunk); } - QTextStream in(&f); - QString xpf_chunk = in.readAll(); - f.close(); - f.remove(); - qimage->setText("Metadata", xpf_chunk); qimage->save(filename); return NULL; } // Reads png image and xpf associated data -const char *readpng(xio_constpath filename) +const char* readpng(xio_constpath filename) { QImageReader reader(filename); const QImage xaos_image = reader.read(); QString xpf_chunk = xaos_image.text("Metadata"); - if(xpf_chunk.isNull() || xpf_chunk.isEmpty()) { - return "Not valid image"; - } - int pathlength = strlen(filename) + 16; - static char* filepath; - filepath = (char* )malloc(pathlength * sizeof (char)); - strcpy(filepath, xio_getdirectory(filename)); - strcat(filepath, ".xaos_temp.xpf"); - QFile f(filepath); - if(!f.open(QFile::WriteOnly | - QFile::Text)) - { - qDebug() << " Could not open the file for writing"; - return "No file or permission"; - } - QTextStream in(&f); - in<