[shotwell] If Preview is JPEG, save as-is



commit 2ef1bfc0f98e2b35212777e83dbac157b133472e
Author: Jens Georg <mail jensge org>
Date:   Sun Jul 9 23:36:53 2017 +0200

    If Preview is JPEG, save as-is
    
    Then there is no need to do a decode/encode

 src/Photo.vala              |   26 +++++++++++++++++++++-----
 src/photos/JfifSupport.vala |   22 ++++++++++++++++------
 2 files changed, 37 insertions(+), 11 deletions(-)
---
diff --git a/src/Photo.vala b/src/Photo.vala
index c768017..cf051ec 100644
--- a/src/Photo.vala
+++ b/src/Photo.vala
@@ -743,17 +743,33 @@ public abstract class Photo : PhotoSource, Dateable {
                         return;
                     }
                     
-                    Gdk.Pixbuf? pix = prev.get_pixbuf();
+                    var pix = prev.flatten();
                     if (pix == null) {
                         debug("Could not get preview pixbuf");
                         return;
                     }
-                    
+
                     // Write out file.
                     bps = d.create_backing_row_for_development(row.master.filepath);
-                    PhotoFileWriter writer = PhotoFileFormat.JFIF.create_writer(bps.filepath);
-                    writer.write(pix, Jpeg.Quality.HIGH);
-                    
+
+                    // Peek at data. If we really have a JPEG image, just use it,
+                    // otherwise do GdkPixbuf roundtrip
+                    if (Jpeg.is_jpeg_bytes(pix)) {
+                        var outfile = File.new_for_path(bps.filepath);
+                        outfile.replace_contents(pix.get_data(), null,
+                                false, FileCreateFlags.NONE, null);
+                    } else {
+                        var pixbuf = prev.get_pixbuf();
+                        if (pixbuf == null) {
+                            debug("Could not get preview pixbuf");
+                            return;
+                        }
+
+                        var writer = PhotoFileFormat.JFIF.create_writer(bps.filepath);
+                        writer.write(pixbuf, Jpeg.Quality.HIGH);
+                    }
+
+
                     // Remember that we wrote it (see above
                     // case for why this is necessary).
                     wrote_img_to_disk = true;
diff --git a/src/photos/JfifSupport.vala b/src/photos/JfifSupport.vala
index 2861353..5ea64a5 100644
--- a/src/photos/JfifSupport.vala
+++ b/src/photos/JfifSupport.vala
@@ -201,16 +201,25 @@ namespace Jpeg {
     }
     
     public bool is_jpeg(File file) throws Error {
-        FileInputStream fins = file.read(null);
-        
+        var fins = file.read(null);
+        return is_jpeg_stream(fins);
+    }
+
+    public bool is_jpeg_stream(InputStream ins) throws Error {
         Marker marker;
-        int segment_length = read_marker(fins, out marker);
+        int segment_length = read_marker(ins, out marker);
         
         // for now, merely checking for SOI
         return (marker == Marker.SOI) && (segment_length == 0);
     }
 
-    private int read_marker(FileInputStream fins, out Jpeg.Marker marker) throws Error {
+    public bool is_jpeg_bytes(Bytes bytes) throws Error {
+        var mins = new MemoryInputStream.from_bytes(bytes);
+
+        return is_jpeg_stream(mins);
+    }
+
+    private int read_marker(InputStream fins, out Jpeg.Marker marker) throws Error {
         marker = Jpeg.Marker.INVALID;
         
         DataInputStream dins = new DataInputStream(fins);
@@ -226,8 +235,9 @@ namespace Jpeg {
         }
         
         uint16 length = dins.read_uint16();
-        if (length < 2) {
-            debug("Invalid length %Xh at ofs %" + int64.FORMAT + "Xh", length, fins.tell() - 2);
+        if (length < 2 && fins is Seekable) {
+            debug("Invalid length %Xh at ofs %" + int64.FORMAT + "Xh", length,
+                    (fins as Seekable).tell() - 2);
             
             return -1;
         }


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