diff --git a/examples/ragephoto-gtkviewer/src/PhotoViewer.cpp b/examples/ragephoto-gtkviewer/src/PhotoViewer.cpp index 385f926..a32fd4e 100644 --- a/examples/ragephoto-gtkviewer/src/PhotoViewer.cpp +++ b/examples/ragephoto-gtkviewer/src/PhotoViewer.cpp @@ -21,7 +21,9 @@ #include #include #include +#include #include +#include PhotoViewer::PhotoViewer(Gtk::Window *win) : p_win(win) { @@ -33,38 +35,20 @@ void PhotoViewer::open_file(const char *filename) p_image.clear(); RagePhoto ragePhoto; + // Read file - FILE *file = fopen(filename, "rb"); - if (!file) { - Gtk::MessageDialog msg(*p_win, "Failed to read file: " + Glib::ustring(filename), false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true); + std::ifstream ifs(filename, std::ios::in | std::ios::binary); + if (!ifs.is_open()) { + Gtk::MessageDialog msg(*p_win, "Failed to open file: " + Glib::ustring(filename), false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true); msg.set_title("Open Photo"); msg.run(); return; } - const int fseek_end_value = fseek(file, 0, SEEK_END); - if (fseek_end_value == -1) { - fclose(file); - return; - } - const size_t file_size = ftell(file); - if (file_size == -1) { - fclose(file); - return; - } - const int fseek_set_value = fseek(file, 0, SEEK_SET); - if (fseek_set_value == -1) { - fclose(file); - return; - } - char *data = static_cast(malloc(file_size)); - const size_t file_rsize = fread(data, 1, file_size, file); - if (file_size != file_rsize) { - fclose(file); - return; - } - fclose(file); - const bool loaded = ragePhoto.load(data, file_size); - free(data); + std::string data(std::istreambuf_iterator{ifs}, {}); + ifs.close(); + + // Load Photo + const bool loaded = ragePhoto.load(data); if (!loaded) { const RagePhoto::Error error = ragePhoto.error(); if (error <= RagePhoto::Error::PhotoReadError) { @@ -75,22 +59,14 @@ void PhotoViewer::open_file(const char *filename) } } - guchar *photoData = static_cast(malloc(ragePhoto.photoSize())); - if (!photoData) - return; - - memcpy(photoData, ragePhoto.photoData(), ragePhoto.photoSize()); - GdkPixbufLoader *loader = gdk_pixbuf_loader_new(); - gdk_pixbuf_loader_write(loader, photoData, static_cast(ragePhoto.photoSize()), NULL); + gdk_pixbuf_loader_write(loader, reinterpret_cast(ragePhoto.photoData()), 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()); - free(photoData); - if (p_image) set_size_request(p_image->get_width(), p_image->get_height()); } diff --git a/src/RagePhoto-Extract.cpp b/src/RagePhoto-Extract.cpp index 2130d1a..37ea6d3 100644 --- a/src/RagePhoto-Extract.cpp +++ b/src/RagePhoto-Extract.cpp @@ -17,7 +17,9 @@ *****************************************************************************/ #include "RagePhoto.h" +#include #include +#include int main(int argc, char *argv[]) { @@ -32,65 +34,32 @@ int main(int argc, char *argv[]) RagePhoto ragePhoto; // Read file - FILE *file = fopen(argv[1], "rb"); - if (!file) { - std::cout << "Failed to open import file" << std::endl; - return -1; + std::ifstream ifs(argv[1], std::ios::in | std::ios::binary); + if (!ifs.is_open()) { + std::cout << "Failed to open file: " << argv[1] << std::endl; + return 1; } - const int fseek_end_value = fseek(file, 0, SEEK_END); - if (fseek_end_value == -1) { - fclose(file); - std::cout << "Failed to read file" << std::endl; - return -1; - } - const size_t file_size = ftell(file); - if (file_size == -1) { - fclose(file); - std::cout << "Failed to read file" << std::endl; - return -1; - } - const int fseek_set_value = fseek(file, 0, SEEK_SET); - if (fseek_set_value == -1) { - fclose(file); - std::cout << "Failed to read file" << std::endl; - return -1; - } - char *data = static_cast(malloc(file_size)); - const size_t file_rsize = fread(data, 1, file_size, file); - if (file_size != file_rsize) { - fclose(file); - std::cout << "Failed to read file" << std::endl; - return -1; - } - fclose(file); + std::string data(std::istreambuf_iterator{ifs}, {}); + ifs.close(); // Load Photo - const bool loaded = ragePhoto.load(data, file_size); - - // Deinitialise data after Photo loaded - free(data); + const bool loaded = ragePhoto.load(data); if (!loaded) { - const RagePhoto::Error error = ragePhoto.error(); - if (error <= RagePhoto::Error::PhotoReadError) { + if (ragePhoto.error() <= RagePhoto::Error::PhotoReadError) { std::cout << "Failed to load photo" << std::endl; return 1; } } // Write jpeg - file = fopen(argv[2], "wb"); - if (!file) { - std::cout << "Failed to open export file" << std::endl; - return -1; - } - const size_t written = fwrite(ragePhoto.photoData(), sizeof(char), ragePhoto.photoSize(), file); - fclose(file); - - if (written != ragePhoto.photoSize()) { - std::cout << "Failed to write file" << std::endl; - return -1; + std::ofstream ofs(argv[2], std::ios::out | std::ios::binary | std::ios::trunc); + if (!ofs.is_open()) { + std::cout << "Failed to write file: " << argv[2] << std::endl; + return 1; } + ofs << ragePhoto.photo(); + ofs.close(); if (ragePhoto.format() == RagePhoto::PhotoFormat::GTA5) std::cout << "GTA V Photo successfully exported" << std::endl;