← Back to team overview

simple-scan-team team mailing list archive

[Merge] lp:~victor-mireyev/simple-scan/484616 into lp:simple-scan

 

Victor Mireyev has proposed merging lp:~victor-mireyev/simple-scan/484616 into lp:simple-scan.

Requested reviews:
  Robert Ancell (robert-ancell)

For more details, see:
https://code.launchpad.net/~victor-mireyev/simple-scan/484616/+merge/166356

Save DPI with JPEG
-- 
https://code.launchpad.net/~victor-mireyev/simple-scan/484616/+merge/166356
Your team Simple Scan Development Team is subscribed to branch lp:simple-scan.
=== modified file 'src/book.vala'
--- src/book.vala	2012-10-14 20:16:55 +0000
+++ src/book.vala	2013-05-29 18:45:34 +0000
@@ -177,49 +177,6 @@
         return out_data;
     }
 
-    private static void jpeg_init_cb (JPEG.Compress info) {}
-    private static bool jpeg_empty_cb (JPEG.Compress info) { return true; }
-    private static void jpeg_term_cb (JPEG.Compress info) {}
-
-    private uint8[] compress_jpeg (Gdk.Pixbuf image, out size_t n_written)
-    {
-        var info = JPEG.Compress ();
-        var jerr = JPEG.ErrorManager ();
-        var dest_mgr = JPEG.DestinationManager ();
-
-        info.err = jerr.std_error ();
-        info.create_compress ();
-
-        info.image_width = image.get_width ();
-        info.image_height = image.get_height ();
-        info.input_components = 3;
-        info.in_color_space = JPEG.ColorSpace.RGB; /* TODO: JCS_GRAYSCALE? */
-        info.set_defaults ();
-
-        var max_length = info.image_width * info.image_height * info.input_components;
-        var data = new uint8[max_length];
-        dest_mgr.next_output_byte = data;
-        dest_mgr.free_in_buffer = max_length;
-        dest_mgr.init_destination = jpeg_init_cb;
-        dest_mgr.empty_output_buffer = jpeg_empty_cb;
-        dest_mgr.term_destination = jpeg_term_cb;
-        info.dest = &dest_mgr;
-
-        info.start_compress (true);
-        unowned uint8[] pixels = image.get_pixels ();
-        for (var r = 0; r < info.image_height; r++)
-        {
-            uint8* row[1];
-            row[0] = (uint8*) pixels + r * image.get_rowstride ();
-            info.write_scanlines (row, 1);
-        }
-        info.finish_compress ();
-        n_written = max_length - dest_mgr.free_in_buffer;
-        data.resize ((int) n_written);
-
-        return data;
-    }
-
     private void save_pdf (File file) throws Error
     {
         var stream = file.replace (null, false, FileCreateFlags.NONE, null);
@@ -412,9 +369,8 @@
                 /* Try if JPEG compression is better */
                 if (depth > 1)
                 {
-                    size_t jpeg_length;
-                    var jpeg_data = compress_jpeg (image, out jpeg_length);
-                    if (jpeg_length < compressed_data.length)
+                    var jpeg_data = JpegWriter.compress_jpeg(image);
+                    if (jpeg_data.length < compressed_data.length)
                     {
                         filter = "DCTDecode";
                         data = jpeg_data;
@@ -602,3 +558,58 @@
         return Cairo.Status.SUCCESS;
     }
 }
+
+public class JpegWriter
+{
+    private static void jpeg_init_cb (JPEG.Compress info) {}
+    private static bool jpeg_empty_cb (JPEG.Compress info) { return true; }
+    private static void jpeg_term_cb (JPEG.Compress info) {}
+
+    public static uint8[] compress_jpeg (Gdk.Pixbuf image, int? density = null)
+    {
+        var info = JPEG.Compress ();
+        var jerr = JPEG.ErrorManager ();
+        var dest_mgr = JPEG.DestinationManager ();
+
+        info.err = jerr.std_error ();
+        info.create_compress ();
+
+        info.image_width = image.get_width ();
+        info.image_height = image.get_height ();
+        info.input_components = 3;
+        info.in_color_space = JPEG.ColorSpace.RGB; /* TODO: JCS_GRAYSCALE? */
+        info.set_defaults ();
+
+        info.set_quality (90);        
+        if (density != null)
+        {
+            info.density_unit = 1;
+            info.X_density = (uint16) density;
+            info.Y_density = (uint16) density;
+        }
+
+        var max_length = info.image_width * info.image_height * info.input_components;
+        var data = new uint8[max_length];
+        dest_mgr.next_output_byte = data;
+        dest_mgr.free_in_buffer = max_length;
+        dest_mgr.init_destination = jpeg_init_cb;
+        dest_mgr.empty_output_buffer = jpeg_empty_cb;
+        dest_mgr.term_destination = jpeg_term_cb;
+        info.dest = &dest_mgr;
+
+        info.start_compress (true);
+        unowned uint8[] pixels = image.get_pixels ();
+        for (var r = 0; r < info.image_height; r++)
+        {
+            uint8* row[1];
+            row[0] = (uint8*) pixels + r * image.get_rowstride ();
+            info.write_scanlines (row, 1);
+        }
+        info.finish_compress ();
+        int n_written = max_length - dest_mgr.free_in_buffer;
+        data.resize (n_written);
+
+        return data;
+    }
+}
+

=== modified file 'src/jpeglib.vapi'
--- src/jpeglib.vapi	2011-06-12 04:55:11 +0000
+++ src/jpeglib.vapi	2013-05-29 18:45:34 +0000
@@ -22,6 +22,11 @@
         public int input_components;
         public ColorSpace in_color_space;
         public ErrorManager* err;
+        
+        public uint8 density_unit;
+        public uint16 X_density;
+        public uint16 Y_density;
+        public void set_quality(int quality, bool force_baseline = true);
 
         public void create_compress ();
         public void set_defaults ();

=== modified file 'src/page.vala'
--- src/page.vala	2013-05-19 21:55:27 +0000
+++ src/page.vala	2013-05-29 18:45:34 +0000
@@ -704,10 +704,8 @@
 
         if (strcmp (type, "jpeg") == 0)
         {
-            /* ICC profile is awaiting review in gtk2+ bugzilla */
-            string[] keys = { "quality", /* "icc-profile", */ null };
-            string[] values = { "90", /* icc_profile_data, */ null };
-            writer.save (image, "jpeg", keys, values);
+            var data = JpegWriter.compress_jpeg (image, this.get_dpi ());
+            stream.write_all (data, null, null);
         }
         else if (strcmp (type, "png") == 0)
         {


Follow ups