refactor image/crop encoding

This commit is contained in:
GenevensiS
2026-01-18 18:56:21 +01:00
parent 28d5993fe6
commit d421d0d92b
7 changed files with 242 additions and 174 deletions
+8 -65
View File
@@ -13,7 +13,7 @@
#include <util/error.hpp>
#include <util/file_utils.hpp>
#include <util/vcs.hpp>
#include <data/format/file_to_text.h>
#include <data/format/image_encoding.hpp>
class Package;
class wxFileInputStream;
@@ -54,71 +54,14 @@ public:
inline String const& toStringForKey() const { return fn; }
/// Retreive a rect from a filename
inline static void getExternalRect(const String& filename, wxRect& rect_out, int& degrees_out) {
size_t first = filename.find(_("<mse-crop-data>"));
if (first == String::npos) return;
size_t last = filename.find(_("</mse-crop-data>"), first + 15);
if (last == String::npos) return;
String string = filename.substr(first + 15, last - (first + 15));
if (string.empty()) return;
size_t divider = string.find(_("-"));
if (divider == String::npos) return;
if (divider == 0) return;
int x;
if(!string.substr(0, divider).ToInt(&x)) return;
string = string.substr(divider + 1);
divider = string.find(_("-"));
if (divider == String::npos) return;
if (divider == 0) return;
int y;
if(!string.substr(0, divider).ToInt(&y)) return;
string = string.substr(divider + 1);
divider = string.find(_("-"));
if (divider == String::npos) return;
if (divider == 0) return;
int width;
if(!string.substr(0, divider).ToInt(&width)) return;
string = string.substr(divider + 1);
divider = string.find(_("-"));
if (divider == String::npos) return;
if (divider == 0) return;
int height;
if(!string.substr(0, divider).ToInt(&height)) return;
string = string.substr(divider + 1);
if(!string.ToInt(&degrees_out)) return;
rect_out = wxRect(x, y, width, height);
}
/// Retreive a rect from a filename
inline void getExternalRect(wxRect& rect_out, int& degrees_out) {
getExternalRect(fn, rect_out, degrees_out);
}
/// Retreive an image from a filename
inline static Image getExternalImage(const String& filename) {
Image img;
size_t first = filename.find(_("<mse-image-data>"));
if (first == String::npos) return img;
size_t last = filename.find(_("</mse-image-data>"), first + 16);
if (last == String::npos) return img;
std::string s = filename.substr(first + 16, last - (first + 16)).ToStdString();
if (s.empty()) return img;
const std::string& temppath = (wxFileName::CreateTempFileName(_("mse")) + _(".png")).ToStdString();
UTF8ToFile(temppath, s);
img.LoadFile(temppath, wxBITMAP_TYPE_PNG);
wxRemoveFile(temppath);
wxRemoveFile(temppath.substr(0, temppath.size() - 4));
return img;
}
inline Image getExternalImage() {
return getExternalImage(fn);
}
decodeRectFromString(fn, rect_out, degrees_out);
}
/// Retreive an image from a filename
inline Image getExternalImage() {
return decodeImageFromString(fn);
}
private:
LocalFileName(const wxString& fn) : fn(fn) {}