2021-08-27 05:33:20 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* libragephoto RAGE Photo Parser
|
|
|
|
* Copyright (C) 2021 Syping
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* This software is provided as-is, no warranties are given to you, we are not
|
|
|
|
* responsible for anything with use of the software, you are self responsible.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "PhotoViewer.h"
|
|
|
|
#include <cairomm/context.h>
|
|
|
|
#include <gdkmm/general.h>
|
2021-08-27 23:17:57 +02:00
|
|
|
#include <gtkmm/messagedialog.h>
|
2021-08-27 05:33:20 +02:00
|
|
|
#include <RagePhoto.h>
|
2021-09-17 08:17:25 +02:00
|
|
|
#include <fstream>
|
2021-08-27 05:33:20 +02:00
|
|
|
#include <iostream>
|
2021-09-17 08:17:25 +02:00
|
|
|
#include <iterator>
|
2021-08-27 05:33:20 +02:00
|
|
|
|
2021-08-27 20:07:14 +02:00
|
|
|
PhotoViewer::PhotoViewer(Gtk::Window *win) : p_win(win)
|
2021-08-27 05:33:20 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-14 07:09:58 +02:00
|
|
|
void PhotoViewer::open_file(const std::string &filename)
|
2021-08-27 05:33:20 +02:00
|
|
|
{
|
2021-08-29 02:35:25 +02:00
|
|
|
if (p_image)
|
|
|
|
p_image.clear();
|
|
|
|
|
2021-08-27 05:33:20 +02:00
|
|
|
RagePhoto ragePhoto;
|
2021-09-17 08:17:25 +02:00
|
|
|
|
2021-08-27 05:33:20 +02:00
|
|
|
// Read file
|
2021-09-17 08:17:25 +02:00
|
|
|
std::ifstream ifs(filename, std::ios::in | std::ios::binary);
|
|
|
|
if (!ifs.is_open()) {
|
2021-10-14 07:09:58 +02:00
|
|
|
Gtk::MessageDialog msg(*p_win, "Failed to open file: " + filename, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
|
2021-08-27 23:17:57 +02:00
|
|
|
msg.set_title("Open Photo");
|
|
|
|
msg.run();
|
2021-08-27 05:33:20 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-09-17 08:17:25 +02:00
|
|
|
std::string data(std::istreambuf_iterator<char>{ifs}, {});
|
|
|
|
ifs.close();
|
|
|
|
|
|
|
|
// Load Photo
|
|
|
|
const bool loaded = ragePhoto.load(data);
|
2021-08-27 05:33:20 +02:00
|
|
|
if (!loaded) {
|
|
|
|
const RagePhoto::Error error = ragePhoto.error();
|
2021-08-27 23:17:57 +02:00
|
|
|
if (error <= RagePhoto::Error::PhotoReadError) {
|
2021-10-14 07:09:58 +02:00
|
|
|
Gtk::MessageDialog msg(*p_win, "Failed to read photo: " + filename, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
|
2021-08-27 23:17:57 +02:00
|
|
|
msg.set_title("Open Photo");
|
|
|
|
msg.run();
|
2021-08-27 05:33:20 +02:00
|
|
|
return;
|
2021-08-27 23:17:57 +02:00
|
|
|
}
|
2021-08-27 05:33:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GdkPixbufLoader *loader = gdk_pixbuf_loader_new();
|
2021-09-17 08:17:25 +02:00
|
|
|
gdk_pixbuf_loader_write(loader, reinterpret_cast<const guchar*>(ragePhoto.photoData()), ragePhoto.photoSize(), NULL);
|
2021-08-27 05:33:20 +02:00
|
|
|
GdkPixbuf *c_pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
|
2021-08-29 02:35:25 +02:00
|
|
|
gdk_pixbuf_loader_close(loader, NULL);
|
2021-08-27 05:33:20 +02:00
|
|
|
p_image = Glib::wrap(c_pixbuf);
|
|
|
|
|
2021-08-27 20:07:14 +02:00
|
|
|
p_win->set_title("RagePhoto GTK Photo Viewer - " + ragePhoto.title());
|
2021-08-27 05:33:20 +02:00
|
|
|
|
|
|
|
if (p_image)
|
|
|
|
set_size_request(p_image->get_width(), p_image->get_height());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhotoViewer::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
|
|
|
|
{
|
|
|
|
if (!p_image)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Gtk::Allocation allocation = get_allocation();
|
|
|
|
const int width = allocation.get_width();
|
|
|
|
const int height = allocation.get_height();
|
|
|
|
|
|
|
|
Gdk::Cairo::set_source_pixbuf(cr, p_image, (width - p_image->get_width()) / 2, (height - p_image->get_height()) / 2);
|
|
|
|
cr->paint();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|