| 1 |
/* Thumbnailer.vala |
| 2 |
* |
| 3 |
* Copyright 2023 Diego Iván <[email protected]> |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
*/ |
| 7 |
|
| 8 |
public class PaperClip.Services.Thumbnailer { |
| 9 |
private float area_threshold = 2.5f; |
| 10 |
|
| 11 |
private int _max_size; |
| 12 |
public int max_size { |
| 13 |
get { |
| 14 |
return _max_size; |
| 15 |
} set { |
| 16 |
_max_size = value; |
| 17 |
} |
| 18 |
} |
| 19 |
private Gdk.MemoryFormat? _default_format = null; |
| 20 |
private Gdk.MemoryFormat default_format { |
| 21 |
get { |
| 22 |
if (_default_format == null) { |
| 23 |
if (BYTE_ORDER == LITTLE_ENDIAN) { |
| 24 |
_default_format = B8G8R8A8_PREMULTIPLIED; |
| 25 |
} else { |
| 26 |
_default_format = A8R8G8B8_PREMULTIPLIED; |
| 27 |
} |
| 28 |
} |
| 29 |
return _default_format; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
protected static Thumbnailer? instance = null; |
| 34 |
public static Thumbnailer get_default () { |
| 35 |
if (instance == null) { |
| 36 |
instance = new Thumbnailer (); |
| 37 |
} |
| 38 |
return instance; |
| 39 |
} |
| 40 |
|
| 41 |
[CCode (has_construct_function = false)] |
| 42 |
protected Thumbnailer () { |
| 43 |
} |
| 44 |
|
| 45 |
public async Gdk.Texture create_thumbnail (Poppler.Page page, string basename) throws Error { |
| 46 |
float max_area = max_size * max_size * area_threshold; |
| 47 |
double height, width, image_area; |
| 48 |
page.get_size (out width, out height); |
| 49 |
image_area = width * height; |
| 50 |
|
| 51 |
var surface = new Cairo.ImageSurface (Cairo.Format.ARGB32, (int) width, (int) height); |
| 52 |
var context = new Cairo.Context (surface); |
| 53 |
// Lets render in a separate thread, it is expensive. |
| 54 |
yield ThreadManager.run_in_thread<void> (() => page.render (context)); |
| 55 |
|
| 56 |
Gdk.Texture retval; |
| 57 |
if (image_area >= max_area) { |
| 58 |
debug ("Image is too big! Loading it in a reduced size"); |
| 59 |
retval = yield ThreadManager.run_in_thread<Gdk.Texture> ( |
| 60 |
() => load_scaled (surface, basename)); |
| 61 |
} else { |
| 62 |
debug ("Loading image in its default size"); |
| 63 |
retval = yield ThreadManager.run_in_thread<Gdk.Texture> (() => load_memory (surface)); |
| 64 |
} |
| 65 |
|
| 66 |
return retval; |
| 67 |
} |
| 68 |
|
| 69 |
private Gdk.Texture load_scaled (Cairo.ImageSurface surface, string basename) throws Error { |
| 70 |
float max_area = max_size * max_size * area_threshold; |
| 71 |
int width = surface.get_width (), height = surface.get_height (); |
| 72 |
string save_path = create_cache_file (basename); |
| 73 |
Cairo.Status status = surface.write_to_png (save_path); |
| 74 |
|
| 75 |
if (status != SUCCESS) { |
| 76 |
throw new ThumbnailError.FAILED_EXPORT (@"Failed to export $save_path: $status"); |
| 77 |
} |
| 78 |
|
| 79 |
float scale_factor = 0.1f; |
| 80 |
for (; scale_factor < 0.8f; scale_factor+=0.1f) { |
| 81 |
float scaled_area = height * width * scale_factor; |
| 82 |
if (scaled_area >= max_area) { |
| 83 |
break; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
debug (@"Loading image at scale $scale_factor"); |
| 88 |
float scaled_height = height * scale_factor, scaled_width = width * scale_factor; |
| 89 |
debug (@"Height: $height -> $scaled_height. Width: $width -> $scaled_width"); |
| 90 |
|
| 91 |
var pixbuf = new Gdk.Pixbuf.from_file_at_size (save_path, (int) scaled_width, (int) scaled_height); |
| 92 |
var texture = Gdk.Texture.for_pixbuf (pixbuf); |
| 93 |
return texture; |
| 94 |
} |
| 95 |
|
| 96 |
private string? create_cache_file (string basename) throws Error { |
| 97 |
string destination_path = Path.build_path (Path.DIR_SEPARATOR_S, |
| 98 |
Environment.get_tmp_dir (), |
| 99 |
"thumbnails"); |
| 100 |
int result = DirUtils.create_with_parents (destination_path, 0777); |
| 101 |
if (result < 0) { |
| 102 |
throw new ThumbnailError.FAILED_TO_CACHE (@"Unable to create tmp path $destination_path"); |
| 103 |
} |
| 104 |
|
| 105 |
string destination_file = Path.build_filename (destination_path, |
| 106 |
"%s.png".printf (basename)); |
| 107 |
|
| 108 |
var file = File.new_for_path (destination_file); |
| 109 |
if (!file.query_exists ()) { |
| 110 |
file.create (NONE); |
| 111 |
} |
| 112 |
return destination_file; |
| 113 |
} |
| 114 |
|
| 115 |
private Gdk.Texture load_memory (Cairo.ImageSurface surface) throws Error { |
| 116 |
int height = surface.get_height (), width = surface.get_width (); |
| 117 |
size_t size = height * width * 4; |
| 118 |
var bytes = new Bytes.take (create_sized_copy (surface.get_data (), size)); |
| 119 |
var memory_texture = new Gdk.MemoryTexture (width, height, default_format, bytes, |
| 120 |
surface.get_stride ()); |
| 121 |
return memory_texture; |
| 122 |
} |
| 123 |
|
| 124 |
private uint8[] create_sized_copy ([CCode (array_length = false)]uint8[] data, size_t size) { |
| 125 |
uint8[] copy = new uint8[size]; |
| 126 |
for (size_t i = 0; i < size; i++) { |
| 127 |
copy[i] = data[i]; |
| 128 |
} |
| 129 |
return copy; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
public errordomain PaperClip.ThumbnailError { |
| 134 |
FAILED_EXPORT, |
| 135 |
FAILED_TO_CACHE |
| 136 |
} |