RagePhoto improvements, GTK example fix

This commit is contained in:
Syping 2021-08-29 02:35:25 +02:00
parent 61a9770951
commit 8c860855ae
3 changed files with 50 additions and 2 deletions

View File

@ -29,6 +29,9 @@ PhotoViewer::PhotoViewer(Gtk::Window *win) : p_win(win)
void PhotoViewer::open_file(const char *filename) void PhotoViewer::open_file(const char *filename)
{ {
if (p_image)
p_image.clear();
RagePhoto ragePhoto; RagePhoto ragePhoto;
// Read file // Read file
FILE *file = fopen(filename, "rb"); FILE *file = fopen(filename, "rb");
@ -81,6 +84,7 @@ void PhotoViewer::open_file(const char *filename)
GdkPixbufLoader *loader = gdk_pixbuf_loader_new(); GdkPixbufLoader *loader = gdk_pixbuf_loader_new();
gdk_pixbuf_loader_write(loader, photoData, static_cast<gsize>(ragePhoto.photoSize()), NULL); gdk_pixbuf_loader_write(loader, photoData, static_cast<gsize>(ragePhoto.photoSize()), NULL);
GdkPixbuf *c_pixbuf = gdk_pixbuf_loader_get_pixbuf(loader); GdkPixbuf *c_pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
gdk_pixbuf_loader_close(loader, NULL);
p_image = Glib::wrap(c_pixbuf); p_image = Glib::wrap(c_pixbuf);
p_win->set_title("RagePhoto GTK Photo Viewer - " + ragePhoto.title()); p_win->set_title("RagePhoto GTK Photo Viewer - " + ragePhoto.title());

View File

@ -75,7 +75,12 @@ bool RagePhoto::load(const char *data, size_t length)
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN
uint32_t format;
memcpy(&format, uInt32Buffer, 4);
#else
uint32_t format = charToUInt32LE(uInt32Buffer); uint32_t format = charToUInt32LE(uInt32Buffer);
#endif
if (format == static_cast<uint32_t>(PhotoFormat::GTA5)) { if (format == static_cast<uint32_t>(PhotoFormat::GTA5)) {
char photoHeader[256]; char photoHeader[256];
size = readBuffer(data, photoHeader, &pos, 256, length); size = readBuffer(data, photoHeader, &pos, 256, length);
@ -112,35 +117,54 @@ bool RagePhoto::load(const char *data, size_t length)
p_error = Error::IncompleteChecksum; // 6 p_error = Error::IncompleteChecksum; // 6
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(&p_headerSum, uInt32Buffer, 4);
#else
p_headerSum = charToUInt32LE(uInt32Buffer); p_headerSum = charToUInt32LE(uInt32Buffer);
#endif
size = readBuffer(data, uInt32Buffer, &pos, 4, length); size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
p_error = Error::IncompleteEOF; // 7 p_error = Error::IncompleteEOF; // 7
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(&p_endOfFile, uInt32Buffer, 4);
#else
p_endOfFile = charToUInt32LE(uInt32Buffer); p_endOfFile = charToUInt32LE(uInt32Buffer);
#endif
size = readBuffer(data, uInt32Buffer, &pos, 4, length); size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
p_error = Error::IncompleteJsonOffset; // 8 p_error = Error::IncompleteJsonOffset; // 8
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(&p_jsonOffset, uInt32Buffer, 4);
#else
p_jsonOffset = charToUInt32LE(uInt32Buffer); p_jsonOffset = charToUInt32LE(uInt32Buffer);
#endif
size = readBuffer(data, uInt32Buffer, &pos, 4, length); size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
p_error = Error::IncompleteTitleOffset; // 9 p_error = Error::IncompleteTitleOffset; // 9
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(&p_titlOffset, uInt32Buffer, 4);
#else
p_titlOffset = charToUInt32LE(uInt32Buffer); p_titlOffset = charToUInt32LE(uInt32Buffer);
#endif
size = readBuffer(data, uInt32Buffer, &pos, 4, length); size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
p_error = Error::IncompleteDescOffset; // 10 p_error = Error::IncompleteDescOffset; // 10
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(&p_descOffset, uInt32Buffer, 4);
#else
p_descOffset = charToUInt32LE(uInt32Buffer); p_descOffset = charToUInt32LE(uInt32Buffer);
#endif
char markerBuffer[4]; char markerBuffer[4];
size = readBuffer(data, markerBuffer, &pos, 4, length); size = readBuffer(data, markerBuffer, &pos, 4, length);
@ -158,14 +182,22 @@ bool RagePhoto::load(const char *data, size_t length)
p_error = Error::IncompletePhotoBuffer; // 13 p_error = Error::IncompletePhotoBuffer; // 13
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(&p_photoBuffer, uInt32Buffer, 4);
#else
p_photoBuffer = charToUInt32LE(uInt32Buffer); p_photoBuffer = charToUInt32LE(uInt32Buffer);
#endif
size = readBuffer(data, uInt32Buffer, &pos, 4, length); size = readBuffer(data, uInt32Buffer, &pos, 4, length);
if (size != 4) { if (size != 4) {
p_error = Error::IncompletePhotoSize; // 14 p_error = Error::IncompletePhotoSize; // 14
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(&p_photoSize, uInt32Buffer, 4);
#else
p_photoSize = charToUInt32LE(uInt32Buffer); p_photoSize = charToUInt32LE(uInt32Buffer);
#endif
p_photoData = static_cast<char*>(malloc(p_photoSize)); p_photoData = static_cast<char*>(malloc(p_photoSize));
if (!p_photoData) { if (!p_photoData) {
@ -197,7 +229,11 @@ bool RagePhoto::load(const char *data, size_t length)
p_error = Error::IncompleteJsonBuffer; // 19 p_error = Error::IncompleteJsonBuffer; // 19
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(&p_jsonBuffer, uInt32Buffer, 4);
#else
p_jsonBuffer = charToUInt32LE(uInt32Buffer); p_jsonBuffer = charToUInt32LE(uInt32Buffer);
#endif
char *t_jsonData = static_cast<char*>(malloc(p_jsonBuffer)); char *t_jsonData = static_cast<char*>(malloc(p_jsonBuffer));
if (!t_jsonData) { if (!t_jsonData) {
@ -229,7 +265,11 @@ bool RagePhoto::load(const char *data, size_t length)
p_error = Error::IncompleteTitleBuffer; // 24 p_error = Error::IncompleteTitleBuffer; // 24
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(&p_titlBuffer, uInt32Buffer, 4);
#else
p_titlBuffer = charToUInt32LE(uInt32Buffer); p_titlBuffer = charToUInt32LE(uInt32Buffer);
#endif
char *t_titlData = static_cast<char*>(malloc(p_titlBuffer)); char *t_titlData = static_cast<char*>(malloc(p_titlBuffer));
if (!t_titlData) { if (!t_titlData) {
@ -261,7 +301,11 @@ bool RagePhoto::load(const char *data, size_t length)
p_error = Error::IncompleteDescBuffer; // 29 p_error = Error::IncompleteDescBuffer; // 29
return false; return false;
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(&p_descBuffer, uInt32Buffer, 4);
#else
p_descBuffer = charToUInt32LE(uInt32Buffer); p_descBuffer = charToUInt32LE(uInt32Buffer);
#endif
char *t_descData = static_cast<char*>(malloc(p_descBuffer)); char *t_descData = static_cast<char*>(malloc(p_descBuffer));
if (!t_descData) { if (!t_descData) {

View File

@ -80,7 +80,7 @@ public:
const std::string header(); const std::string header();
const std::string title(); const std::string title();
private: protected:
inline size_t readBuffer(const char *input, char *output, size_t *pos, size_t len, size_t inputLen); inline size_t readBuffer(const char *input, char *output, size_t *pos, size_t len, size_t inputLen);
inline uint32_t charToUInt32BE(char *x); inline uint32_t charToUInt32BE(char *x);
inline uint32_t charToUInt32LE(char *x); inline uint32_t charToUInt32LE(char *x);