[glom] ImageGlom: Do not transform to PNG output when saving.



commit e1cbe6f1d695ae741a810f144bf2765efca0697f
Author: Murray Cumming <murrayc murrayc com>
Date:   Tue Jul 12 14:20:41 2011 +0200

    ImageGlom: Do not transform to PNG output when saving.
    
    * glom/utility_widgets/dialog_image_save_progress.[h|cc]: Replace set_pixbuf()
    with set_image_data().
    save(): Save the original data, instead of saving the data from the pixbuf.
    * glom/utility_widgets/imageglom.cc: Adapt.
    This means, for instance, that if the user loads the a JPG in then they will
    get exactly the same JPG out.

 ChangeLog                                          |   11 ++++
 glom/utility_widgets/dialog_image_save_progress.cc |   62 ++++++++++++++++----
 glom/utility_widgets/dialog_image_save_progress.h  |   12 +++-
 glom/utility_widgets/imageglom.cc                  |    6 ++-
 4 files changed, 74 insertions(+), 17 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index cc7e1df..8552388 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
 2011-07-12  Murray Cumming  <murrayc murrayc com>
 
+	ImageGlom: Do not transform to PNG output when saving.
+
+	* glom/utility_widgets/dialog_image_save_progress.[h|cc]: Replace set_pixbuf() 
+	with set_image_data().
+	save(): Save the original data, instead of saving the data from the pixbuf.
+	* glom/utility_widgets/imageglom.cc: Adapt.
+	This means, for instance, that if the user loads the a JPG in then they will 
+	get exactly the same JPG out.
+
+2011-07-12  Murray Cumming  <murrayc murrayc com>
+
 	Some minor signed type corrections.
 
 	* glom/libglom/connectionpool_backends/postgres.cc:
diff --git a/glom/utility_widgets/dialog_image_save_progress.cc b/glom/utility_widgets/dialog_image_save_progress.cc
index 3a5b868..4e7753b 100644
--- a/glom/utility_widgets/dialog_image_save_progress.cc
+++ b/glom/utility_widgets/dialog_image_save_progress.cc
@@ -31,7 +31,8 @@ const char* DialogImageSaveProgress::glade_id("dialog_image_save_progress");
 const bool DialogImageSaveProgress::glade_developer(false);
 
 DialogImageSaveProgress::DialogImageSaveProgress(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder)
-: Gtk::Dialog(cobject)
+: Gtk::Dialog(cobject),
+  m_data(0)
 {
   builder->get_widget("progress_bar", m_progress_bar);
 
@@ -45,22 +46,59 @@ DialogImageSaveProgress::~DialogImageSaveProgress()
 
 void DialogImageSaveProgress::save(const Glib::ustring& uri)
 {
-  //TODO: Support non-local URIs when we do this properly, using Gio::File.
-  const std::string filepath = Glib::filename_from_uri(uri);
-  m_progress_bar->set_text(Glib::ustring::compose("Saving %1...", 
-    Glib::filename_display_basename(filepath)));
+  g_assert(m_data);
 
+  if(m_data->data == 0)
+    return;
+    
+  if(m_data->binary_length == 0)
+    return;
+
+  m_file = Gio::File::create_for_uri(uri);
+  m_progress_bar->set_text(Glib::ustring::compose("Saving %1...", m_file->get_parse_name()));
+
+  m_stream.reset();
+   
   try
   {
-    // Open the file for reading:
-    m_pixbuf->save(filepath, GLOM_IMAGE_FORMAT);
+    if(m_file->query_exists())
+    {
+      m_stream = m_file->replace(); //Instead of append_to().
+    }
+    else
+    {
+      //By default files created are generally readable by everyone, but if we pass FILE_CREATE_PRIVATE in flags the file will be made readable only to the current user, to the level that is supported on the target filesystem.
+      //TODO: Do we want to specify 0660 exactly? (means "this user and his group can read and write this non-executable file".)
+      m_stream = m_file->create_file();
+    }
+  }
+  catch(const Gio::Error& ex)
+  {
+    std::cerr << G_STRFUNC << ": exception: " << ex.what() << std::endl;
+    response(Gtk::RESPONSE_REJECT);
+    return;
   }
-  catch(const Glib::Error& ex)
+  
+  //Write the data to the output uri
+  gssize bytes_written = 0;
+  try
+  {
+    bytes_written = m_stream->write(m_data->data, m_data->binary_length);
+  }
+  catch(const Gio::Error& ex)
+  {
+    std::cerr << G_STRFUNC << ": exception: " << ex.what() << std::endl;
+    response(Gtk::RESPONSE_REJECT);
+    return;
+  }
+
+  if(bytes_written != m_data->binary_length)
   {
-    error(ex.what());
+    std::cerr << G_STRFUNC << ": unexpected number of bytes written: bytes_written=" << bytes_written <<
+       ", binary_length=" << m_data->binary_length << std::endl;
   }
   
-  //response(Gtk::RESPONSE_ACCEPT);
+  response(Gtk::RESPONSE_ACCEPT);
 }
 
 void DialogImageSaveProgress::error(const Glib::ustring& error_message)
@@ -74,9 +112,9 @@ void DialogImageSaveProgress::error(const Glib::ustring& error_message)
 }
 
 
-void DialogImageSaveProgress::set_pixbuf(const Glib::RefPtr<Gdk::Pixbuf>& pixbuf)
+void DialogImageSaveProgress::set_image_data(const GdaBinary& data)
 {
-  m_pixbuf = pixbuf;
+  m_data = &data;
 }
 
 } // namespace Glom
diff --git a/glom/utility_widgets/dialog_image_save_progress.h b/glom/utility_widgets/dialog_image_save_progress.h
index a8f1ca0..7506606 100644
--- a/glom/utility_widgets/dialog_image_save_progress.h
+++ b/glom/utility_widgets/dialog_image_save_progress.h
@@ -24,8 +24,9 @@
 #include <gtkmm/dialog.h>
 #include <gtkmm/builder.h>
 #include <gtkmm/progressbar.h>
-#include <libglom/data_structure/layout/layoutitem_image.h> //For the file formats.
-//#include <memory>
+#include <giomm/file.h>
+#include <giomm/fileoutputstream.h>
+#include <libgda/libgda.h>
 
 namespace Glom
 {
@@ -41,13 +42,16 @@ public:
 
   void save(const Glib::ustring& uri);
 
-  void set_pixbuf(const Glib::RefPtr<Gdk::Pixbuf>& pixbuf);
+  void set_image_data(const GdaBinary& data);
 
 private:
   void error(const Glib::ustring& error_message);
 
-  Glib::RefPtr<Gdk::Pixbuf> m_pixbuf;
   Gtk::ProgressBar* m_progress_bar;
+  const GdaBinary* m_data;
+
+  Glib::RefPtr<Gio::File> m_file;
+  Glib::RefPtr<Gio::FileOutputStream> m_stream;
 };
 
 } //namespace Glom
diff --git a/glom/utility_widgets/imageglom.cc b/glom/utility_widgets/imageglom.cc
index 1102030..552dbc9 100644
--- a/glom/utility_widgets/imageglom.cc
+++ b/glom/utility_widgets/imageglom.cc
@@ -442,7 +442,11 @@ bool ImageGlom::save_file(const Glib::ustring& uri)
   if(pApp)
     dialog_save->set_transient_for(*pApp);
 
-  dialog_save->set_pixbuf(m_pixbuf_original);
+  const GdaBinary* gda_binary = gda_value_get_binary(m_original_data.gobj());
+  if(!gda_binary)
+    return false;
+
+  dialog_save->set_image_data(*gda_binary);
   dialog_save->save(uri);
 
   //TODO: Use this when we do async saving:



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]