RagePhoto: improve save efficiency by using std::string as buffer

This commit is contained in:
Syping 2021-11-19 09:55:52 +01:00
parent 285df4893d
commit c89296c775
2 changed files with 10 additions and 22 deletions

View file

@ -743,24 +743,18 @@ bool RagePhoto::save(char *data)
const std::string RagePhoto::save(uint32_t photoFormat, bool *ok) const std::string RagePhoto::save(uint32_t photoFormat, bool *ok)
{ {
std::string data;
const size_t size = saveSize(photoFormat); const size_t size = saveSize(photoFormat);
if (size == 0) { if (size == 0) {
if (ok) if (ok)
*ok = false; *ok = false;
return std::string(); return data;
} }
char *data = static_cast<char*>(std::malloc(size)); data.resize(size);
if (!data) { const bool saved = save(&data[0], photoFormat);
if (ok)
*ok = false;
return std::string();
}
const bool saved = save(data, photoFormat);
if (ok) if (ok)
*ok = saved; *ok = saved;
const std::string sdata = std::string(data, size); return data;
std::free(data);
return sdata;
} }
const std::string RagePhoto::save(bool *ok) const std::string RagePhoto::save(bool *ok)

View file

@ -88,24 +88,18 @@ public:
return ragephoto_save(instance, data); return ragephoto_save(instance, data);
} }
const std::string save(uint32_t photoFormat, bool *ok = nullptr) { const std::string save(uint32_t photoFormat, bool *ok = nullptr) {
std::string data;
const size_t size = ragephoto_getsavesizef(instance, photoFormat); const size_t size = ragephoto_getsavesizef(instance, photoFormat);
if (size == 0) { if (size == 0) {
if (ok) if (ok)
*ok = false; *ok = false;
return std::string(); return data;
} }
char *data = static_cast<char*>(std::malloc(size)); data.resize(size);
if (!data) { const bool saved = ragephoto_savef(instance, &data[0], photoFormat);
if (ok)
*ok = false;
return std::string();
}
const bool saved = ragephoto_savef(instance, data, photoFormat);
if (ok) if (ok)
*ok = saved; *ok = saved;
const std::string sdata = std::string(data, size); return data;
std::free(data);
return sdata;
} }
const std::string save(bool *ok = nullptr) { const std::string save(bool *ok = nullptr) {
return save(ragephoto_getphotoformat(instance), ok); return save(ragephoto_getphotoformat(instance), ok);