diff --git a/examples/ragephoto-gtkviewer/src/PhotoViewer.cpp b/examples/ragephoto-gtkviewer/src/PhotoViewer.cpp index fe31710..385f926 100644 --- a/examples/ragephoto-gtkviewer/src/PhotoViewer.cpp +++ b/examples/ragephoto-gtkviewer/src/PhotoViewer.cpp @@ -29,6 +29,9 @@ PhotoViewer::PhotoViewer(Gtk::Window *win) : p_win(win) void PhotoViewer::open_file(const char *filename) { + if (p_image) + p_image.clear(); + RagePhoto ragePhoto; // Read file FILE *file = fopen(filename, "rb"); @@ -81,6 +84,7 @@ void PhotoViewer::open_file(const char *filename) GdkPixbufLoader *loader = gdk_pixbuf_loader_new(); gdk_pixbuf_loader_write(loader, photoData, static_cast(ragePhoto.photoSize()), NULL); GdkPixbuf *c_pixbuf = gdk_pixbuf_loader_get_pixbuf(loader); + gdk_pixbuf_loader_close(loader, NULL); p_image = Glib::wrap(c_pixbuf); p_win->set_title("RagePhoto GTK Photo Viewer - " + ragePhoto.title()); diff --git a/src/RagePhoto.cpp b/src/RagePhoto.cpp index 0f471c0..2b81ddd 100644 --- a/src/RagePhoto.cpp +++ b/src/RagePhoto.cpp @@ -75,7 +75,12 @@ bool RagePhoto::load(const char *data, size_t length) return false; } +#if __BYTE_ORDER == __LITTLE_ENDIAN + uint32_t format; + memcpy(&format, uInt32Buffer, 4); +#else uint32_t format = charToUInt32LE(uInt32Buffer); +#endif if (format == static_cast(PhotoFormat::GTA5)) { char photoHeader[256]; 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 return false; } +#if __BYTE_ORDER == __LITTLE_ENDIAN + memcpy(&p_headerSum, uInt32Buffer, 4); +#else p_headerSum = charToUInt32LE(uInt32Buffer); +#endif size = readBuffer(data, uInt32Buffer, &pos, 4, length); if (size != 4) { p_error = Error::IncompleteEOF; // 7 return false; } +#if __BYTE_ORDER == __LITTLE_ENDIAN + memcpy(&p_endOfFile, uInt32Buffer, 4); +#else p_endOfFile = charToUInt32LE(uInt32Buffer); +#endif size = readBuffer(data, uInt32Buffer, &pos, 4, length); if (size != 4) { p_error = Error::IncompleteJsonOffset; // 8 return false; } +#if __BYTE_ORDER == __LITTLE_ENDIAN + memcpy(&p_jsonOffset, uInt32Buffer, 4); +#else p_jsonOffset = charToUInt32LE(uInt32Buffer); - +#endif size = readBuffer(data, uInt32Buffer, &pos, 4, length); if (size != 4) { p_error = Error::IncompleteTitleOffset; // 9 return false; } +#if __BYTE_ORDER == __LITTLE_ENDIAN + memcpy(&p_titlOffset, uInt32Buffer, 4); +#else p_titlOffset = charToUInt32LE(uInt32Buffer); +#endif size = readBuffer(data, uInt32Buffer, &pos, 4, length); if (size != 4) { p_error = Error::IncompleteDescOffset; // 10 return false; } +#if __BYTE_ORDER == __LITTLE_ENDIAN + memcpy(&p_descOffset, uInt32Buffer, 4); +#else p_descOffset = charToUInt32LE(uInt32Buffer); +#endif char markerBuffer[4]; 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 return false; } +#if __BYTE_ORDER == __LITTLE_ENDIAN + memcpy(&p_photoBuffer, uInt32Buffer, 4); +#else p_photoBuffer = charToUInt32LE(uInt32Buffer); +#endif size = readBuffer(data, uInt32Buffer, &pos, 4, length); if (size != 4) { p_error = Error::IncompletePhotoSize; // 14 return false; } +#if __BYTE_ORDER == __LITTLE_ENDIAN + memcpy(&p_photoSize, uInt32Buffer, 4); +#else p_photoSize = charToUInt32LE(uInt32Buffer); +#endif p_photoData = static_cast(malloc(p_photoSize)); if (!p_photoData) { @@ -197,7 +229,11 @@ bool RagePhoto::load(const char *data, size_t length) p_error = Error::IncompleteJsonBuffer; // 19 return false; } +#if __BYTE_ORDER == __LITTLE_ENDIAN + memcpy(&p_jsonBuffer, uInt32Buffer, 4); +#else p_jsonBuffer = charToUInt32LE(uInt32Buffer); +#endif char *t_jsonData = static_cast(malloc(p_jsonBuffer)); if (!t_jsonData) { @@ -229,7 +265,11 @@ bool RagePhoto::load(const char *data, size_t length) p_error = Error::IncompleteTitleBuffer; // 24 return false; } +#if __BYTE_ORDER == __LITTLE_ENDIAN + memcpy(&p_titlBuffer, uInt32Buffer, 4); +#else p_titlBuffer = charToUInt32LE(uInt32Buffer); +#endif char *t_titlData = static_cast(malloc(p_titlBuffer)); if (!t_titlData) { @@ -261,7 +301,11 @@ bool RagePhoto::load(const char *data, size_t length) p_error = Error::IncompleteDescBuffer; // 29 return false; } +#if __BYTE_ORDER == __LITTLE_ENDIAN + memcpy(&p_descBuffer, uInt32Buffer, 4); +#else p_descBuffer = charToUInt32LE(uInt32Buffer); +#endif char *t_descData = static_cast(malloc(p_descBuffer)); if (!t_descData) { diff --git a/src/RagePhoto.h b/src/RagePhoto.h index 92c75d9..18e38d9 100644 --- a/src/RagePhoto.h +++ b/src/RagePhoto.h @@ -80,7 +80,7 @@ public: const std::string header(); 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 uint32_t charToUInt32BE(char *x); inline uint32_t charToUInt32LE(char *x);