summaryrefslogtreecommitdiff
path: root/src/book.vala
diff options
context:
space:
mode:
authorAlessio Treglia <alessio@debian.org>2013-10-05 15:28:26 +0100
committerAlessio Treglia <alessio@debian.org>2013-10-05 15:28:26 +0100
commit2d71d20a62ca8f6989d86db58ce64c7a862675dd (patch)
tree534c40e7c4b35662d60e261898b6b1ebd5724bed /src/book.vala
parent695f537db79b0921621b54998ae46f331fff9105 (diff)
Imported Upstream version 3.10.0upstream/3.10.0
Diffstat (limited to 'src/book.vala')
-rw-r--r--src/book.vala78
1 files changed, 31 insertions, 47 deletions
diff --git a/src/book.vala b/src/book.vala
index 6740fcb..a4e6cdd 100644
--- a/src/book.vala
+++ b/src/book.vala
@@ -113,16 +113,18 @@ public class Book
suffix = basename.slice (extension_index, basename.length);
prefix = uri.slice (0, uri.length - suffix.length);
}
-
- return File.new_for_uri ("%s-%d%s".printf (prefix, i+1, suffix));
+ var width = get_n_pages ().to_string().length;
+ var number_format = "%%0%dd".printf (width);
+ var filename = prefix + "-" + number_format.printf (i + 1) + suffix;
+ return File.new_for_uri (filename);
}
- private void save_multi_file (string type, File file) throws Error
+ private void save_multi_file (string type, int quality, File file) throws Error
{
for (var i = 0; i < get_n_pages (); i++)
{
var page = get_page (i);
- page.save (type, make_indexed_file (file.get_uri (), i));
+ page.save (type, quality, make_indexed_file (file.get_uri (), i));
saving (i);
}
}
@@ -177,50 +179,33 @@ public class Book
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 ByteArray jpeg_data;
- private uint8[] compress_jpeg (Gdk.Pixbuf image, out size_t n_written)
+ private uint8[] compress_jpeg (Gdk.Pixbuf image, int quality, int dpi)
{
- 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++)
+ jpeg_data = new ByteArray ();
+ string[] keys = { "quality", "density-unit", "x-density", "y-density", null };
+ string[] values = { "%d".printf (quality), "dots-per-inch", "%d".printf (dpi), "%d".printf (dpi), null };
+ try
+ {
+ image.save_to_callbackv (write_pixbuf_data, "jpeg", keys, values);
+ }
+ catch (Error e)
{
- 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);
+ var data = (owned) jpeg_data.data;
+ jpeg_data = null;
return data;
}
- private void save_pdf (File file) throws Error
+ private bool write_pixbuf_data (uint8[] buf) throws Error
+ {
+ jpeg_data.append (buf);
+ return true;
+ }
+
+ private void save_pdf (File file, int quality) throws Error
{
var stream = file.replace (null, false, FileCreateFlags.NONE, null);
var writer = new PDFWriter (stream);
@@ -412,9 +397,8 @@ public class Book
/* 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 = compress_jpeg (image, quality, page.get_dpi ());
+ if (jpeg_data.length < compressed_data.length)
{
filter = "DCTDecode";
data = jpeg_data;
@@ -484,7 +468,7 @@ public class Book
var info_number = writer.start_object ();
writer.write_string ("%u 0 obj\n".printf (info_number));
writer.write_string ("<<\n");
- writer.write_string ("/Creator (Simple Scan %s)\n".printf (Config.VERSION));
+ writer.write_string ("/Creator (Simple Scan %s)\n".printf (VERSION));
writer.write_string (">>\n");
writer.write_string ("endobj\n");
@@ -508,20 +492,20 @@ public class Book
writer.write_string ("%%EOF\n");
}
- public void save (string type, File file) throws Error
+ public void save (string type, int quality, File file) throws Error
{
switch (type)
{
case "jpeg":
case "png":
case "tiff":
- save_multi_file (type, file);
+ save_multi_file (type, quality, file);
break;
case "ps":
save_ps (file);
break;
case "pdf":
- save_pdf (file);
+ save_pdf (file, quality);
break;
}
}