/src/libvips/libvips/resample/thumbnail.c
Line | Count | Source |
1 | | /* make a thumbnail ... wraps up the process of thumbnailing, including |
2 | | * premultiply, colour management etc etc |
3 | | * |
4 | | * 2/11/16 |
5 | | * - from vipsthumbnail.c |
6 | | * 6/1/17 |
7 | | * - add @size parameter |
8 | | * 4/5/17 |
9 | | * - add FORCE |
10 | | * 29/5/17 |
11 | | * - don't cache (thanks tomasc) |
12 | | * 30/8/17 |
13 | | * - add intent option, thanks kleisauke |
14 | | * 31/10/18 |
15 | | * - deprecate auto_rotate, add no_rotate |
16 | | * - implement shrink-on-load for openslide images |
17 | | * 16/11/18 |
18 | | * - implement shrink-on-load for tiff pyramid |
19 | | * 3/2/19 kleisauke |
20 | | * - add option_string param to thumbnail_buffer |
21 | | * 23/4/19 |
22 | | * - don't force import CMYK, since colourspace knows about it now |
23 | | * 24/4/19 |
24 | | * - support multi-page (animated) images |
25 | | * 27/8/19 kleisauke |
26 | | * - prevent over-pre-shrink in thumbnail |
27 | | * 30/9/19 |
28 | | * - smarter heif thumbnail selection |
29 | | * 12/10/19 |
30 | | * - add thumbnail_source |
31 | | * 2/6/20 |
32 | | * - add subifd pyr support |
33 | | * 27/2/21 |
34 | | * - simplify rules re. processing space, colour management and linear |
35 | | * mode |
36 | | * 14/4/22 |
37 | | * - add a seq to thumbnail_image to stop cache thrashing |
38 | | * 28/4/22 |
39 | | * - add fail_on |
40 | | * 1/3/23 kleisauke |
41 | | * - skip colourspace conversion when needed |
42 | | * 27/1/24 |
43 | | * - make icc profile transforms always write 8 bits |
44 | | * 22/8/25 kleisauke |
45 | | * - remove seq line cache from thumbnail_image, use hint instead |
46 | | * 6/10/26 |
47 | | * - add dcrawload preview and thumbnail handling |
48 | | */ |
49 | | |
50 | | /* |
51 | | |
52 | | Copyright (C) 1991-2005 The National Gallery |
53 | | |
54 | | This library is free software; you can redistribute it and/or |
55 | | modify it under the terms of the GNU Lesser General Public |
56 | | License as published by the Free Software Foundation; either |
57 | | version 2.1 of the License, or (at your option) any later version. |
58 | | |
59 | | This library is distributed in the hope that it will be useful, |
60 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
61 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
62 | | Lesser General Public License for more details. |
63 | | |
64 | | You should have received a copy of the GNU Lesser General Public |
65 | | License along with this library; if not, write to the Free Software |
66 | | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
67 | | 02110-1301 USA |
68 | | |
69 | | */ |
70 | | |
71 | | /* |
72 | | |
73 | | These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk |
74 | | |
75 | | */ |
76 | | |
77 | | /* |
78 | | #define DEBUG |
79 | | */ |
80 | | |
81 | | #ifdef HAVE_CONFIG_H |
82 | | #include <config.h> |
83 | | #endif /*HAVE_CONFIG_H*/ |
84 | | #include <glib/gi18n-lib.h> |
85 | | |
86 | | #include <stdio.h> |
87 | | #include <stdlib.h> |
88 | | #include <math.h> |
89 | | |
90 | | #include <vips/vips.h> |
91 | | #include <vips/internal.h> |
92 | | |
93 | | #define VIPS_TYPE_THUMBNAIL (vips_thumbnail_get_type()) |
94 | | #define VIPS_THUMBNAIL(obj) \ |
95 | 0 | (G_TYPE_CHECK_INSTANCE_CAST((obj), VIPS_TYPE_THUMBNAIL, VipsThumbnail)) |
96 | | #define VIPS_THUMBNAIL_CLASS(klass) \ |
97 | 4 | (G_TYPE_CHECK_CLASS_CAST((klass), \ |
98 | 4 | VIPS_TYPE_THUMBNAIL, VipsThumbnailClass)) |
99 | | #define VIPS_IS_THUMBNAIL(obj) \ |
100 | | (G_TYPE_CHECK_INSTANCE_TYPE((obj), VIPS_TYPE_THUMBNAIL)) |
101 | | #define VIPS_IS_THUMBNAIL_CLASS(klass) \ |
102 | | (G_TYPE_CHECK_CLASS_TYPE((klass), VIPS_TYPE_THUMBNAIL)) |
103 | | #define VIPS_THUMBNAIL_GET_CLASS(obj) \ |
104 | 0 | (G_TYPE_INSTANCE_GET_CLASS((obj), \ |
105 | 0 | VIPS_TYPE_THUMBNAIL, VipsThumbnailClass)) |
106 | | |
107 | | /* Should be plenty. |
108 | | */ |
109 | | #define MAX_LEVELS (256) |
110 | | |
111 | | typedef struct _VipsThumbnail { |
112 | | VipsOperation parent_instance; |
113 | | |
114 | | VipsImage *out; |
115 | | int width; |
116 | | int height; |
117 | | VipsSize size; |
118 | | |
119 | | gboolean auto_rotate; |
120 | | gboolean no_rotate; |
121 | | VipsInteresting crop; |
122 | | int interesting_x; |
123 | | int interesting_y; |
124 | | gboolean linear; |
125 | | char *output_profile; |
126 | | char *input_profile; |
127 | | VipsIntent intent; |
128 | | VipsFailOn fail_on; |
129 | | |
130 | | /* Bits of info we read from the input image when we get the header of |
131 | | * the original. |
132 | | */ |
133 | | const char *loader; /* Eg. "VipsForeignLoadJpeg*" */ |
134 | | int input_width; |
135 | | int input_height; |
136 | | int page_height; |
137 | | int orientation; /* From vips_image_get_orientation() */ |
138 | | gboolean swap; /* If we must swap width / height */ |
139 | | int n_pages; /* Pages in file */ |
140 | | int n_loaded_pages; /* Pages we've loaded from file */ |
141 | | int n_subifds; /* Number of subifds */ |
142 | | |
143 | | /* For pyramidal formats, we need to read out the size of each level. |
144 | | */ |
145 | | int level_count; |
146 | | int level_width[MAX_LEVELS]; |
147 | | int level_height[MAX_LEVELS]; |
148 | | |
149 | | /* For HEIF, try to fetch the size of the stored thumbnail. |
150 | | */ |
151 | | int heif_thumbnail_width; |
152 | | int heif_thumbnail_height; |
153 | | |
154 | | /* Pyramids are stored in subifds. |
155 | | */ |
156 | | gboolean subifd_pyramid; |
157 | | |
158 | | /* Pyramids are stored in pages. |
159 | | */ |
160 | | gboolean page_pyramid; |
161 | | |
162 | | } VipsThumbnail; |
163 | | |
164 | | typedef struct _VipsThumbnailClass { |
165 | | VipsOperationClass parent_class; |
166 | | |
167 | | /* Fill out the info section of VipsThumbnail from the input object. |
168 | | */ |
169 | | int (*get_info)(VipsThumbnail *thumbnail); |
170 | | |
171 | | /* Open with some kind of shrink or scale factor. Exactly what we pass |
172 | | * and to what param depends on the loader. It'll be an integer shrink |
173 | | * factor for vips_jpegload(), a double scale factor for vips_svgload(). |
174 | | * |
175 | | * See VipsThumbnail::loader |
176 | | */ |
177 | | VipsImage *(*open)(VipsThumbnail *thumbnail, double factor); |
178 | | |
179 | | } VipsThumbnailClass; |
180 | | |
181 | 8 | G_DEFINE_ABSTRACT_TYPE(VipsThumbnail, vips_thumbnail, VIPS_TYPE_OPERATION); |
182 | 8 | |
183 | 8 | static void |
184 | 8 | vips_thumbnail_dispose(GObject *gobject) |
185 | 8 | { |
186 | | #ifdef DEBUG |
187 | | printf("vips_thumbnail_dispose: "); |
188 | | vips_object_print_name(VIPS_OBJECT(gobject)); |
189 | | printf("\n"); |
190 | | #endif /*DEBUG*/ |
191 | |
|
192 | 0 | G_OBJECT_CLASS(vips_thumbnail_parent_class)->dispose(gobject); |
193 | 0 | } |
194 | | |
195 | | static void |
196 | | vips_thumbnail_finalize(GObject *gobject) |
197 | 0 | { |
198 | | #ifdef DEBUG |
199 | | printf("vips_thumbnail_finalize: "); |
200 | | vips_object_print_name(VIPS_OBJECT(gobject)); |
201 | | printf("\n"); |
202 | | #endif /*DEBUG*/ |
203 | |
|
204 | 0 | G_OBJECT_CLASS(vips_thumbnail_parent_class)->finalize(gobject); |
205 | 0 | } |
206 | | |
207 | | /* Fetch an int openslide field from metadata. These are all represented as |
208 | | * strings. Return the default value if there's any problem. |
209 | | */ |
210 | | static int |
211 | | get_int(VipsImage *image, const char *field, int default_value) |
212 | 0 | { |
213 | 0 | const char *str; |
214 | |
|
215 | 0 | if (vips_image_get_typeof(image, field) && |
216 | 0 | !vips_image_get_string(image, field, &str)) |
217 | 0 | return atoi(str); |
218 | | |
219 | 0 | return default_value; |
220 | 0 | } |
221 | | |
222 | | /* Open the level of a dcrawload image, where 0 is original (slow), 1 is |
223 | | * preview (large blob) and 2 is thumbnail (small blob). |
224 | | * |
225 | | * NULL return means level is missing and is not an error. |
226 | | */ |
227 | | static VipsImage * |
228 | | vips_thumbnail_get_blob(VipsThumbnail *thumbnail, VipsImage *image, int level) |
229 | 0 | { |
230 | 0 | const char *field; |
231 | 0 | switch (level) { |
232 | 0 | case 0: |
233 | 0 | g_object_ref(image); |
234 | 0 | return image; |
235 | | |
236 | 0 | case 1: |
237 | 0 | field = "jpeg-preview-data"; |
238 | 0 | break; |
239 | | |
240 | 0 | case 2: |
241 | 0 | field = "jpeg-thumbnail-data"; |
242 | 0 | break; |
243 | | |
244 | 0 | default: |
245 | 0 | g_assert_not_reached(); |
246 | 0 | } |
247 | | |
248 | 0 | VipsBlob *blob; |
249 | 0 | if (!vips_image_get_typeof(image, field) || |
250 | 0 | vips_image_get_blob_value(image, field, &blob)) |
251 | 0 | return NULL; |
252 | | |
253 | 0 | VipsSource *source; |
254 | 0 | if (!(source = vips_source_new_from_blob(blob))) |
255 | 0 | return NULL; |
256 | | |
257 | 0 | VipsImage *out; |
258 | 0 | if (!(out = vips_image_new_from_source(source, "", |
259 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
260 | 0 | "fail_on", thumbnail->fail_on, |
261 | 0 | NULL))) { |
262 | 0 | VIPS_UNREF(source); |
263 | 0 | return NULL; |
264 | 0 | } |
265 | | |
266 | 0 | VIPS_UNREF(source); |
267 | |
|
268 | 0 | return out; |
269 | 0 | } |
270 | | |
271 | | static void |
272 | | vips_thumbnail_read_header(VipsThumbnail *thumbnail, VipsImage *image) |
273 | 0 | { |
274 | 0 | thumbnail->input_width = image->Xsize; |
275 | 0 | thumbnail->input_height = image->Ysize; |
276 | 0 | thumbnail->orientation = vips_image_get_orientation(image); |
277 | 0 | thumbnail->swap = vips_image_get_orientation_swap(image); |
278 | 0 | thumbnail->page_height = vips_image_get_page_height(image); |
279 | 0 | thumbnail->n_pages = vips_image_get_n_pages(image); |
280 | 0 | thumbnail->n_subifds = vips_image_get_n_subifds(image); |
281 | | |
282 | | /* VIPS_META_N_PAGES is the number of pages in the document, |
283 | | * not the number we've read out into this image. We calculate |
284 | | * ourselves from page_height. |
285 | | * |
286 | | * vips_image_get_page_height() has verified that Ysize is a simple |
287 | | * multiple of page_height. |
288 | | */ |
289 | 0 | thumbnail->n_loaded_pages = |
290 | 0 | thumbnail->input_height / thumbnail->page_height; |
291 | | |
292 | | /* For openslide, read out the level structure too. |
293 | | */ |
294 | 0 | if (vips_isprefix("VipsForeignLoadOpenslide", thumbnail->loader)) { |
295 | 0 | int level_count = VIPS_CLIP(1, |
296 | 0 | get_int(image, "openslide.level-count", 1), MAX_LEVELS); |
297 | |
|
298 | 0 | thumbnail->level_count = level_count; |
299 | |
|
300 | 0 | for (int level = 0; level < level_count; level++) { |
301 | 0 | char name[256]; |
302 | |
|
303 | 0 | g_snprintf(name, 256, "openslide.level[%d].width", level); |
304 | 0 | thumbnail->level_width[level] = get_int(image, name, 0); |
305 | 0 | g_snprintf(name, 256, "openslide.level[%d].height", level); |
306 | 0 | thumbnail->level_height[level] = get_int(image, name, 0); |
307 | 0 | } |
308 | 0 | } |
309 | |
|
310 | 0 | if (vips_isprefix("VipsForeignLoadDcRaw", thumbnail->loader)) { |
311 | 0 | for (int i = 0; i < 3; i++) { |
312 | 0 | VipsImage *blob; |
313 | |
|
314 | 0 | if ((blob = vips_thumbnail_get_blob(thumbnail, image, i))) { |
315 | 0 | thumbnail->level_width[i] = blob->Xsize; |
316 | 0 | thumbnail->level_height[i] = blob->Ysize; |
317 | |
|
318 | 0 | VIPS_UNREF(blob); |
319 | 0 | } |
320 | 0 | } |
321 | |
|
322 | 0 | thumbnail->level_count = 3; |
323 | 0 | } |
324 | 0 | } |
325 | | |
326 | | /* Detect a pyramid made of pages following a roughly /2 shrink. |
327 | | * |
328 | | * This may not be a pyr tiff, so no error if we can't find the layers. |
329 | | */ |
330 | | static void |
331 | | vips_thumbnail_get_pyramid_page(VipsThumbnail *thumbnail) |
332 | 0 | { |
333 | 0 | VipsThumbnailClass *class = VIPS_THUMBNAIL_GET_CLASS(thumbnail); |
334 | |
|
335 | | #ifdef DEBUG |
336 | | printf("vips_thumbnail_get_pyramid_page:\n"); |
337 | | #endif /*DEBUG*/ |
338 | | |
339 | | /* Single-page docs can't be pyramids, more than 30 levels will int |
340 | | * overflow. |
341 | | */ |
342 | 0 | if (thumbnail->n_pages < 2 || |
343 | 0 | thumbnail->n_pages > 29) |
344 | 0 | return; |
345 | | |
346 | 0 | for (int i = 0; i < thumbnail->n_pages; i++) { |
347 | 0 | VipsImage *page; |
348 | 0 | int level_width; |
349 | 0 | int level_height; |
350 | 0 | int expected_level_width; |
351 | 0 | int expected_level_height; |
352 | |
|
353 | 0 | if (!(page = class->open(thumbnail, i))) |
354 | 0 | return; |
355 | 0 | level_width = page->Xsize; |
356 | 0 | level_height = page->Ysize; |
357 | 0 | VIPS_UNREF(page); |
358 | |
|
359 | 0 | expected_level_width = thumbnail->input_width / (1 << i); |
360 | 0 | expected_level_height = thumbnail->input_height / (1 << i); |
361 | | |
362 | | /* This won't be exact due to rounding etc. |
363 | | */ |
364 | 0 | if (abs(level_width - expected_level_width) > 5 || |
365 | 0 | level_width < 2) |
366 | 0 | return; |
367 | 0 | if (abs(level_height - expected_level_height) > 5 || |
368 | 0 | level_height < 2) |
369 | 0 | return; |
370 | | |
371 | 0 | thumbnail->level_width[i] = level_width; |
372 | 0 | thumbnail->level_height[i] = level_height; |
373 | 0 | } |
374 | | |
375 | | /* Now set level_count. This signals that we've found a pyramid. |
376 | | */ |
377 | | #ifdef DEBUG |
378 | | printf("vips_thumbnail_get_pyramid_page: " |
379 | | "%d layer pyramid detected\n", thumbnail->n_pages); |
380 | | for (int i = 0; i < thumbnail->n_pages; i++) |
381 | | printf(" %d - %d x %d\n", |
382 | | i, thumbnail->level_width[i], thumbnail->level_height[i]); |
383 | | #endif /*DEBUG*/ |
384 | 0 | thumbnail->level_count = thumbnail->n_pages; |
385 | 0 | } |
386 | | |
387 | | /* Detect a TIFF pyramid made of subifds following a roughly /2 shrink. |
388 | | * |
389 | | * This may not be a pyr tiff, so no error if we can't find the layers. |
390 | | */ |
391 | | static void |
392 | | vips_thumbnail_get_tiff_pyramid_subifd(VipsThumbnail *thumbnail) |
393 | 0 | { |
394 | 0 | VipsThumbnailClass *class = VIPS_THUMBNAIL_GET_CLASS(thumbnail); |
395 | |
|
396 | | #ifdef DEBUG |
397 | | printf("vips_thumbnail_get_tiff_pyramid_subifd:\n"); |
398 | | #endif /*DEBUG*/ |
399 | | |
400 | | /* Very small pyramids are useless, large ones int overflow. |
401 | | */ |
402 | 0 | if (thumbnail->n_subifds < 1 || |
403 | 0 | thumbnail->n_subifds > 28) |
404 | 0 | return; |
405 | | |
406 | 0 | for (int i = 0; i < thumbnail->n_subifds; i++) { |
407 | 0 | VipsImage *page; |
408 | 0 | int level_width; |
409 | 0 | int level_height; |
410 | 0 | int expected_level_width; |
411 | 0 | int expected_level_height; |
412 | |
|
413 | 0 | if (!(page = class->open(thumbnail, i))) |
414 | 0 | return; |
415 | 0 | level_width = page->Xsize; |
416 | 0 | level_height = page->Ysize; |
417 | 0 | VIPS_UNREF(page); |
418 | | |
419 | | /* The main image is size 1, subifd 0 is half that. |
420 | | */ |
421 | 0 | expected_level_width = thumbnail->input_width / (2 << i); |
422 | 0 | expected_level_height = thumbnail->input_height / (2 << i); |
423 | | |
424 | | /* This won't be exact due to rounding etc. |
425 | | */ |
426 | 0 | if (abs(level_width - expected_level_width) > 5 || |
427 | 0 | level_width < 2) |
428 | 0 | return; |
429 | 0 | if (abs(level_height - expected_level_height) > 5 || |
430 | 0 | level_height < 2) |
431 | 0 | return; |
432 | | |
433 | 0 | thumbnail->level_width[i] = level_width; |
434 | 0 | thumbnail->level_height[i] = level_height; |
435 | 0 | } |
436 | | |
437 | | /* Now set level_count. This signals that we've found a pyramid. |
438 | | */ |
439 | | #ifdef DEBUG |
440 | | printf("vips_thumbnail_get_tiff_pyramid_subifd: " |
441 | | "%d layer pyramid detected\n", thumbnail->n_subifds); |
442 | | #endif /*DEBUG*/ |
443 | 0 | thumbnail->level_count = thumbnail->n_subifds; |
444 | 0 | } |
445 | | |
446 | | static int |
447 | | vips_thumbnail_get_heif_thumb_info(VipsThumbnail *thumbnail) |
448 | 0 | { |
449 | 0 | VipsThumbnailClass *class = VIPS_THUMBNAIL_GET_CLASS(thumbnail); |
450 | |
|
451 | 0 | VipsImage *thumb; |
452 | |
|
453 | 0 | if (!(thumb = class->open(thumbnail, 1))) |
454 | 0 | return -1; |
455 | | |
456 | 0 | if (thumb->Xsize < thumbnail->input_width) { |
457 | 0 | thumbnail->heif_thumbnail_width = thumb->Xsize; |
458 | 0 | thumbnail->heif_thumbnail_height = thumb->Ysize; |
459 | 0 | } |
460 | |
|
461 | 0 | VIPS_UNREF(thumb); |
462 | |
|
463 | 0 | return 0; |
464 | 0 | } |
465 | | |
466 | | /* Calculate the shrink factor, taking into account auto-rotate, the fit mode, |
467 | | * and so on. |
468 | | * |
469 | | * The hshrink/vshrink are the amount to shrink the input image axes by in |
470 | | * order for the output axes (ie. after rotation) to match the required |
471 | | * thumbnail->width, thumbnail->height and fit mode. |
472 | | */ |
473 | | static void |
474 | | vips_thumbnail_calculate_shrink(VipsThumbnail *thumbnail, |
475 | | int input_width, int input_height, double *hshrink, double *vshrink) |
476 | 0 | { |
477 | | /* If we will be rotating, swap the target width and height. |
478 | | */ |
479 | 0 | gboolean rotate = thumbnail->swap && thumbnail->auto_rotate; |
480 | 0 | int target_width = rotate ? thumbnail->height : thumbnail->width; |
481 | 0 | int target_height = rotate ? thumbnail->width : thumbnail->height; |
482 | |
|
483 | 0 | VipsDirection direction; |
484 | | |
485 | | /* Calculate the horizontal and vertical shrink we'd need to fit the |
486 | | * image to the bounding box, and pick the biggest. |
487 | | * |
488 | | * In crop mode, we aim to fill the bounding box, so we must use the |
489 | | * smaller axis. |
490 | | */ |
491 | 0 | *hshrink = (double) input_width / target_width; |
492 | 0 | *vshrink = (double) input_height / target_height; |
493 | |
|
494 | 0 | if (thumbnail->crop != VIPS_INTERESTING_NONE) { |
495 | 0 | if (*hshrink < *vshrink) |
496 | 0 | direction = VIPS_DIRECTION_HORIZONTAL; |
497 | 0 | else |
498 | 0 | direction = VIPS_DIRECTION_VERTICAL; |
499 | 0 | } |
500 | 0 | else { |
501 | 0 | if (*hshrink < *vshrink) |
502 | 0 | direction = VIPS_DIRECTION_VERTICAL; |
503 | 0 | else |
504 | 0 | direction = VIPS_DIRECTION_HORIZONTAL; |
505 | 0 | } |
506 | |
|
507 | 0 | if (thumbnail->size != VIPS_SIZE_FORCE) { |
508 | 0 | if (direction == VIPS_DIRECTION_HORIZONTAL) |
509 | 0 | *vshrink = *hshrink; |
510 | 0 | else |
511 | 0 | *hshrink = *vshrink; |
512 | 0 | } |
513 | |
|
514 | 0 | if (thumbnail->size == VIPS_SIZE_UP) { |
515 | 0 | *hshrink = VIPS_MIN(1, *hshrink); |
516 | 0 | *vshrink = VIPS_MIN(1, *vshrink); |
517 | 0 | } |
518 | 0 | else if (thumbnail->size == VIPS_SIZE_DOWN) { |
519 | 0 | *hshrink = VIPS_MAX(1, *hshrink); |
520 | 0 | *vshrink = VIPS_MAX(1, *vshrink); |
521 | 0 | } |
522 | | |
523 | | /* We don't want to shrink so much that we send an axis to 0. |
524 | | */ |
525 | 0 | *hshrink = VIPS_MIN(*hshrink, input_width); |
526 | 0 | *vshrink = VIPS_MIN(*vshrink, input_height); |
527 | 0 | } |
528 | | |
529 | | /* Just the common part of the shrink: the bit by which both axes must be |
530 | | * shrunk. |
531 | | */ |
532 | | static double |
533 | | vips_thumbnail_calculate_common_shrink(VipsThumbnail *thumbnail, |
534 | | int width, int height) |
535 | 0 | { |
536 | 0 | double hshrink; |
537 | 0 | double vshrink; |
538 | 0 | vips_thumbnail_calculate_shrink(thumbnail, width, height, |
539 | 0 | &hshrink, &vshrink); |
540 | |
|
541 | 0 | return VIPS_MIN(hshrink, vshrink); |
542 | 0 | } |
543 | | |
544 | | /* Find the best jpeg preload shrink. |
545 | | */ |
546 | | static int |
547 | | vips_thumbnail_find_jpegshrink(VipsThumbnail *thumbnail, int width, int height) |
548 | 0 | { |
549 | 0 | double shrink = |
550 | 0 | vips_thumbnail_calculate_common_shrink(thumbnail, width, height); |
551 | | |
552 | | /* We can't use pre-shrunk images in linear mode. libjpeg shrinks in Y |
553 | | * (of YCbCR), not linear space. |
554 | | */ |
555 | 0 | if (thumbnail->linear) |
556 | 0 | return 1; |
557 | | |
558 | | /* Shrink-on-load is a simple block shrink and will add quite a bit of |
559 | | * extra sharpness to the image. We want to block shrink to a |
560 | | * bit above our target, then vips_shrink / vips_reduce() to the |
561 | | * final size. |
562 | | * |
563 | | * Leave at least a factor of two for the final resize step. |
564 | | */ |
565 | 0 | if (shrink >= 16) |
566 | 0 | return 8; |
567 | 0 | else if (shrink >= 8) |
568 | 0 | return 4; |
569 | 0 | else if (shrink >= 4) |
570 | 0 | return 2; |
571 | 0 | else |
572 | 0 | return 1; |
573 | 0 | } |
574 | | |
575 | | /* Find the best pyramid (openslide, tiff, etc.) level. |
576 | | * |
577 | | * We assume that levels are high-quality shrinks and we do not need to take a |
578 | | * level below, as we must for JPEG. |
579 | | * |
580 | | * Levels can be empty (width | height zero). |
581 | | */ |
582 | | static int |
583 | | vips_thumbnail_find_pyrlevel(VipsThumbnail *thumbnail, int width, int height) |
584 | 0 | { |
585 | 0 | g_assert(thumbnail->level_count > 0); |
586 | 0 | g_assert(thumbnail->level_count <= MAX_LEVELS); |
587 | | |
588 | 0 | for (int level = thumbnail->level_count - 1; level >= 0; level--) { |
589 | 0 | if (!thumbnail->level_width[level] || |
590 | 0 | !thumbnail->level_height[level]) |
591 | 0 | continue; |
592 | | |
593 | 0 | double shrink = vips_thumbnail_calculate_common_shrink(thumbnail, |
594 | 0 | thumbnail->level_width[level], |
595 | 0 | thumbnail->level_height[level]); |
596 | | |
597 | | // not >=, shrink can clip to 1.0 |
598 | 0 | if (shrink > 1.0) |
599 | 0 | return level; |
600 | 0 | } |
601 | | |
602 | 0 | return 0; |
603 | 0 | } |
604 | | |
605 | | /* Open the image, returning the best version for this thumbnail size. |
606 | | * |
607 | | * For example, libjpeg supports fast shrink-on-read, so if we have a JPEG, |
608 | | * we can ask VIPS to load a lower resolution version. |
609 | | */ |
610 | | static VipsImage * |
611 | | vips_thumbnail_get_level(VipsThumbnail *thumbnail) |
612 | 0 | { |
613 | 0 | VipsThumbnailClass *class = VIPS_THUMBNAIL_GET_CLASS(thumbnail); |
614 | |
|
615 | 0 | VipsImage *im; |
616 | 0 | double factor; |
617 | |
|
618 | 0 | if (class->get_info(thumbnail)) |
619 | 0 | return NULL; |
620 | 0 | g_info("selected loader is %s", thumbnail->loader); |
621 | 0 | g_info("input size is %d x %d", |
622 | 0 | thumbnail->input_width, thumbnail->input_height); |
623 | | |
624 | | /* For tiff, scan the image and try to spot page-based and ifd-based |
625 | | * pyramids. |
626 | | */ |
627 | 0 | if (vips_isprefix("VipsForeignLoadTiff", thumbnail->loader)) { |
628 | | /* Test for a subifd pyr first, since we can do that from just |
629 | | * one page. |
630 | | */ |
631 | 0 | thumbnail->subifd_pyramid = TRUE; |
632 | 0 | vips_thumbnail_get_tiff_pyramid_subifd(thumbnail); |
633 | |
|
634 | 0 | if (thumbnail->level_count == 0) { |
635 | 0 | thumbnail->subifd_pyramid = FALSE; |
636 | 0 | thumbnail->page_pyramid = TRUE; |
637 | |
|
638 | 0 | vips_thumbnail_get_pyramid_page(thumbnail); |
639 | |
|
640 | 0 | if (thumbnail->level_count == 0) |
641 | 0 | thumbnail->page_pyramid = FALSE; |
642 | 0 | } |
643 | 0 | } |
644 | | |
645 | | /* jp2k uses page-based pyramids. |
646 | | */ |
647 | 0 | if (vips_isprefix("VipsForeignLoadJp2k", thumbnail->loader)) { |
648 | 0 | if (thumbnail->level_count == 0) { |
649 | 0 | thumbnail->subifd_pyramid = FALSE; |
650 | 0 | thumbnail->page_pyramid = TRUE; |
651 | |
|
652 | 0 | vips_thumbnail_get_pyramid_page(thumbnail); |
653 | |
|
654 | 0 | if (thumbnail->level_count == 0) |
655 | 0 | thumbnail->page_pyramid = FALSE; |
656 | 0 | } |
657 | 0 | } |
658 | | |
659 | | /* For heif, we need to fetch the thumbnail size, in case we can use |
660 | | * that as the source. |
661 | | */ |
662 | 0 | if (vips_isprefix("VipsForeignLoadHeif", thumbnail->loader)) |
663 | 0 | vips_thumbnail_get_heif_thumb_info(thumbnail); |
664 | | |
665 | | /* We read the openslide level structure and the dcrawload metadata in |
666 | | * vips_thumbnail_read_header(). |
667 | | */ |
668 | |
|
669 | 0 | factor = 1.0; |
670 | |
|
671 | 0 | if (vips_isprefix("VipsForeignLoadJpeg", thumbnail->loader) || |
672 | 0 | vips_isprefix("VipsForeignLoadUhdr", thumbnail->loader)) { |
673 | 0 | factor = vips_thumbnail_find_jpegshrink(thumbnail, |
674 | 0 | thumbnail->input_width, thumbnail->input_height); |
675 | 0 | g_info("loading with factor %g pre-shrink", factor); |
676 | 0 | } |
677 | 0 | else if (vips_isprefix("VipsForeignLoadTiff", thumbnail->loader) || |
678 | 0 | vips_isprefix("VipsForeignLoadJp2k", thumbnail->loader) || |
679 | 0 | vips_isprefix("VipsForeignLoadOpenslide", thumbnail->loader) || |
680 | 0 | vips_isprefix("VipsForeignLoadDcRaw", thumbnail->loader)) { |
681 | 0 | if (thumbnail->level_count > 0) { |
682 | 0 | factor = vips_thumbnail_find_pyrlevel(thumbnail, |
683 | 0 | thumbnail->input_width, |
684 | 0 | thumbnail->input_height); |
685 | 0 | g_info("loading pyramid page %g", factor); |
686 | 0 | } |
687 | 0 | else |
688 | 0 | g_info("loading with factor %g pre-shrink", factor); |
689 | 0 | } |
690 | 0 | else if (vips_isprefix("VipsForeignLoadWebp", thumbnail->loader)) { |
691 | 0 | factor = vips_thumbnail_calculate_common_shrink(thumbnail, |
692 | 0 | thumbnail->input_width, |
693 | 0 | thumbnail->page_height); |
694 | | |
695 | | /* Avoid upsizing via libwebp. |
696 | | */ |
697 | 0 | factor = VIPS_MAX(1.0, factor); |
698 | |
|
699 | 0 | g_info("loading with factor %g pre-shrink", factor); |
700 | 0 | } |
701 | 0 | else if (vips_isprefix("VipsForeignLoadPdf", thumbnail->loader) || |
702 | 0 | vips_isprefix("VipsForeignLoadSvg", thumbnail->loader)) { |
703 | 0 | factor = vips_thumbnail_calculate_common_shrink(thumbnail, |
704 | 0 | thumbnail->input_width, |
705 | 0 | thumbnail->page_height); |
706 | |
|
707 | 0 | g_info("loading with factor %g pre-shrink", factor); |
708 | 0 | } |
709 | 0 | else if (vips_isprefix("VipsForeignLoadHeif", thumbnail->loader)) { |
710 | | /* 'factor' is a gboolean which enables thumbnail load instead |
711 | | * of image load. |
712 | | * |
713 | | * Use the thumbnail if, by using it, we could get a factor > |
714 | | * 1.0, ie. we would not need to expand the thumbnail. |
715 | | * |
716 | | * Don't use >= since factor can be clipped to 1.0 under some |
717 | | * resizing modes. |
718 | | */ |
719 | 0 | double shrink_factor = vips_thumbnail_calculate_common_shrink( |
720 | 0 | thumbnail, |
721 | 0 | thumbnail->heif_thumbnail_width, |
722 | 0 | thumbnail->heif_thumbnail_height); |
723 | |
|
724 | 0 | factor = shrink_factor > 1.0 ? 1 : 0; |
725 | |
|
726 | 0 | if (factor == 1) |
727 | 0 | g_info("selected HEIF thumbnail for shrinking"); |
728 | 0 | else |
729 | 0 | g_info("selected main HEIF image for shrinking"); |
730 | 0 | } |
731 | |
|
732 | 0 | if (!(im = class->open(thumbnail, factor))) |
733 | 0 | return NULL; |
734 | | |
735 | 0 | g_info("pre-shrunk size is %d x %d", im->Xsize, im->Ysize); |
736 | |
|
737 | 0 | return im; |
738 | 0 | } |
739 | | |
740 | | /* Crop the same window from every page of an image and rejoin. |
741 | | * |
742 | | * left/top are relative to a page, not to the whole stack. |
743 | | */ |
744 | | static VipsImage * |
745 | | vips_thumbnail_crop_pages(VipsObject *object, VipsImage *in, |
746 | | int left, int top, int width, int height, int page_height, int n_pages) |
747 | 0 | { |
748 | 0 | VipsImage **pages = (VipsImage **) vips_object_local_array(object, n_pages); |
749 | |
|
750 | 0 | VipsImage *out; |
751 | |
|
752 | 0 | for (int i = 0; i < n_pages; i++) |
753 | 0 | if (vips_crop(in, &pages[i], |
754 | 0 | left, i * page_height + top, width, height, NULL)) |
755 | 0 | return NULL; |
756 | 0 | if (vips_arrayjoin(pages, &out, n_pages, "across", 1, NULL)) |
757 | 0 | return NULL; |
758 | | |
759 | 0 | if (n_pages > 1) { |
760 | 0 | VipsImage *t; |
761 | |
|
762 | 0 | if (vips_copy(out, &t, NULL)) { |
763 | 0 | g_object_unref(out); |
764 | 0 | return NULL; |
765 | 0 | } |
766 | 0 | g_object_unref(out); |
767 | 0 | out = t; |
768 | |
|
769 | 0 | vips_image_set_int(out, VIPS_META_PAGE_HEIGHT, height); |
770 | 0 | } |
771 | | |
772 | 0 | return out; |
773 | 0 | } |
774 | | |
775 | | static int |
776 | | vips_thumbnail_build(VipsObject *object) |
777 | 0 | { |
778 | 0 | VipsThumbnail *thumbnail = VIPS_THUMBNAIL(object); |
779 | 0 | VipsImage **t = (VipsImage **) vips_object_local_array(object, 25); |
780 | |
|
781 | 0 | VipsImage *in; |
782 | 0 | double hshrink; |
783 | 0 | double vshrink; |
784 | 0 | VipsImage *gainmap; |
785 | |
|
786 | | #ifdef DEBUG |
787 | | printf("vips_thumbnail_build: "); |
788 | | vips_object_print_name(object); |
789 | | printf("\n"); |
790 | | #endif /*DEBUG*/ |
791 | |
|
792 | 0 | if (VIPS_OBJECT_CLASS(vips_thumbnail_parent_class)->build(object)) |
793 | 0 | return -1; |
794 | | |
795 | | /* We have to support both no_rotate and auto_rotate optional args, |
796 | | * with no_rotate being the new and not-deprecated one. |
797 | | * |
798 | | * If the new no_rotate flag has been set, that value overrides |
799 | | * auto_rotate. |
800 | | */ |
801 | 0 | if (vips_object_argument_isset(object, "no_rotate")) |
802 | 0 | thumbnail->auto_rotate = !thumbnail->no_rotate; // FIXME: Invalidates operation cache |
803 | |
|
804 | 0 | if (!vips_object_argument_isset(object, "height")) |
805 | 0 | thumbnail->height = thumbnail->width; // FIXME: Invalidates operation cache |
806 | | |
807 | | /* Open and do any pre-shrinking. |
808 | | */ |
809 | 0 | if (!(t[0] = vips_thumbnail_get_level(thumbnail))) |
810 | 0 | return -1; |
811 | 0 | in = t[0]; |
812 | | |
813 | | /* After pre-shrink, but before the main shrink stage. |
814 | | */ |
815 | 0 | int preshrunk_page_height = vips_image_get_page_height(in); |
816 | |
|
817 | 0 | int source_depth = in->BandFmt == VIPS_FORMAT_USHORT ? 16 : 8; |
818 | | |
819 | | /* RAD needs special unpacking. |
820 | | */ |
821 | 0 | if (in->Coding == VIPS_CODING_RAD) { |
822 | 0 | g_info("unpacking Rad to float"); |
823 | | |
824 | | /* rad is scrgb. |
825 | | */ |
826 | 0 | if (vips_rad2float(in, &t[1], NULL)) |
827 | 0 | return -1; |
828 | 0 | in = t[1]; |
829 | 0 | } |
830 | | |
831 | | /* We can transform with CICP. |
832 | | */ |
833 | 0 | gboolean can_cicp = vips__image_is_cicp(in); |
834 | | |
835 | | /* We have an input profile. |
836 | | */ |
837 | 0 | gboolean can_icc_import = vips_image_get_typeof(in, VIPS_META_ICC_NAME) || |
838 | 0 | thumbnail->input_profile; |
839 | | |
840 | | /* TRUE if we imported to scRGB from CICP and need to reexport to device |
841 | | * space before write. |
842 | | */ |
843 | 0 | gboolean have_cicped = FALSE; |
844 | | |
845 | | /* TRUE if we've done the import of an ICC transform and still need to |
846 | | * export. |
847 | | */ |
848 | 0 | gboolean have_imported = FALSE; |
849 | | |
850 | | /* In linear mode, we need to transform to a linear space before |
851 | | * vips_resize(). |
852 | | */ |
853 | 0 | if (thumbnail->linear) { |
854 | | /* We can linearise in three ways: cicp, uhdr, icc, and manually. |
855 | | * We don't linearise uhdr, it's too slow and annoying. |
856 | | */ |
857 | 0 | if (can_cicp) { |
858 | 0 | g_info("linearising CICP to scRGB"); |
859 | 0 | if (vips_CICP2scRGB(in, &t[2], NULL)) |
860 | 0 | return -1; |
861 | 0 | in = t[2]; |
862 | |
|
863 | 0 | have_cicped = TRUE; |
864 | 0 | } |
865 | 0 | else if (can_icc_import) { |
866 | 0 | g_info("importing to XYZ PCS"); |
867 | 0 | if (thumbnail->input_profile) |
868 | 0 | g_info("fallback input profile %s", thumbnail->input_profile); |
869 | |
|
870 | 0 | if (vips_icc_import(in, &t[2], |
871 | 0 | "input_profile", thumbnail->input_profile, |
872 | 0 | "embedded", TRUE, |
873 | 0 | "intent", thumbnail->intent, |
874 | 0 | "pcs", VIPS_PCS_XYZ, |
875 | 0 | NULL)) |
876 | 0 | return -1; |
877 | 0 | in = t[2]; |
878 | |
|
879 | 0 | have_imported = TRUE; |
880 | 0 | } |
881 | 0 | else { |
882 | | /* Otherwise, use scRGB or GREY16 for linear shrink. |
883 | | */ |
884 | 0 | VipsInterpretation interpretation; |
885 | |
|
886 | 0 | if (in->Bands < 3) |
887 | 0 | interpretation = VIPS_INTERPRETATION_GREY16; |
888 | 0 | else |
889 | 0 | interpretation = VIPS_INTERPRETATION_scRGB; |
890 | |
|
891 | 0 | g_info("converting to processing space %s", |
892 | 0 | vips_enum_nick(VIPS_TYPE_INTERPRETATION, interpretation)); |
893 | 0 | if (vips_colourspace(in, &t[2], interpretation, NULL)) |
894 | 0 | return -1; |
895 | 0 | in = t[2]; |
896 | 0 | } |
897 | 0 | } |
898 | 0 | else if (!thumbnail->output_profile && |
899 | 0 | !can_cicp) { |
900 | | /* Non linear mode ... interpretations like eg. LAB need to go |
901 | | * to sRGB or B_W for shrinking. |
902 | | * |
903 | | * We mustn't convert images which are due colour management later on |
904 | | * by either exporting to a specified output profile, or which have |
905 | | * CICP metadata (they will be transformed by the saver). |
906 | | */ |
907 | 0 | VipsInterpretation interpretation; |
908 | |
|
909 | 0 | if (in->Bands < 3) |
910 | 0 | interpretation = VIPS_INTERPRETATION_B_W; |
911 | 0 | else |
912 | 0 | interpretation = VIPS_INTERPRETATION_sRGB; |
913 | |
|
914 | 0 | g_info("converting to processing space %s", |
915 | 0 | vips_enum_nick(VIPS_TYPE_INTERPRETATION, interpretation)); |
916 | 0 | if (vips_colourspace(in, &t[2], interpretation, NULL)) |
917 | 0 | return -1; |
918 | 0 | in = t[2]; |
919 | 0 | } |
920 | | |
921 | | /* Shrink to preshrunk_page_height, so we work for multi-page images. |
922 | | */ |
923 | 0 | vips_thumbnail_calculate_shrink(thumbnail, |
924 | 0 | in->Xsize, preshrunk_page_height, &hshrink, &vshrink); |
925 | | |
926 | | /* In toilet-roll mode, we must adjust vshrink so that we exactly hit |
927 | | * page_height or we'll have pixels straddling page boundaries. |
928 | | */ |
929 | 0 | if (in->Ysize > preshrunk_page_height) { |
930 | 0 | int target_page_height = rint(preshrunk_page_height / vshrink); |
931 | 0 | int target_image_height = |
932 | 0 | target_page_height * thumbnail->n_loaded_pages; |
933 | |
|
934 | 0 | vshrink = (double) in->Ysize / target_image_height; |
935 | 0 | } |
936 | | |
937 | | /* The format we need to revert to after unpremultiply. |
938 | | * |
939 | | * Use NOTSET to mean no pre/unmultiply. |
940 | | */ |
941 | 0 | VipsBandFormat unpremultiplied_format = VIPS_FORMAT_NOTSET; |
942 | | |
943 | | /* If there's an alpha, we have to premultiply before shrinking. See |
944 | | * https://github.com/libvips/libvips/issues/291 |
945 | | */ |
946 | 0 | if (vips_image_hasalpha(in) && |
947 | 0 | hshrink != 1.0 && |
948 | 0 | vshrink != 1.0) { |
949 | 0 | g_info("premultiplying alpha"); |
950 | 0 | unpremultiplied_format = in->BandFmt; |
951 | |
|
952 | 0 | if (vips_premultiply(in, &t[3], |
953 | | /* Fast path: stay in uchar. |
954 | | */ |
955 | 0 | "uchar", in->BandFmt == VIPS_FORMAT_UCHAR, |
956 | 0 | NULL)) |
957 | 0 | return -1; |
958 | 0 | in = t[3]; |
959 | 0 | } |
960 | | |
961 | 0 | if (vips_resize(in, &t[4], 1.0 / hshrink, "vscale", 1.0 / vshrink, NULL)) |
962 | 0 | return -1; |
963 | 0 | in = t[4]; |
964 | | |
965 | | /* Also resize the gainmap, if any. |
966 | | */ |
967 | 0 | if ((gainmap = vips_image_get_gainmap(in))) { |
968 | 0 | if (vips_resize(gainmap, &t[5], 1.0 / hshrink, |
969 | 0 | "vscale", 1.0 / vshrink, |
970 | 0 | "kernel", VIPS_KERNEL_LINEAR, |
971 | 0 | NULL)) |
972 | 0 | return -1; |
973 | 0 | g_object_unref(gainmap); |
974 | | |
975 | | /* Make sure we don't have a shared image. |
976 | | */ |
977 | 0 | if (vips_copy(in, &t[6], NULL)) |
978 | 0 | return -1; |
979 | 0 | in = t[6]; |
980 | |
|
981 | 0 | vips_image_set_image(in, "gainmap", t[5]); |
982 | 0 | } |
983 | | |
984 | 0 | if (unpremultiplied_format != VIPS_FORMAT_NOTSET) { |
985 | 0 | g_info("unpremultiplying alpha"); |
986 | |
|
987 | 0 | if (unpremultiplied_format == VIPS_FORMAT_UCHAR) { |
988 | | /* Fast path: unpremultiply to UCHAR directly. |
989 | | */ |
990 | 0 | if (vips_unpremultiply(in, &t[7], "uchar", TRUE, NULL)) |
991 | 0 | return -1; |
992 | 0 | in = t[7]; |
993 | 0 | } |
994 | 0 | else { |
995 | 0 | if (vips_unpremultiply(in, &t[8], NULL) || |
996 | 0 | vips_cast(t[8], &t[9], unpremultiplied_format, NULL)) |
997 | 0 | return -1; |
998 | 0 | in = t[9]; |
999 | 0 | } |
1000 | 0 | } |
1001 | | |
1002 | | /* Only set page-height if we have more than one page, or this could |
1003 | | * accidentally turn into an animated image later. |
1004 | | */ |
1005 | 0 | if (thumbnail->n_loaded_pages > 1) { |
1006 | 0 | int output_page_height = rint(preshrunk_page_height / vshrink); |
1007 | | |
1008 | | /* Make sure we don't have a shared image. |
1009 | | */ |
1010 | 0 | if (vips_copy(in, &t[10], NULL)) |
1011 | 0 | return -1; |
1012 | 0 | in = t[10]; |
1013 | |
|
1014 | 0 | vips_image_set_int(in, VIPS_META_PAGE_HEIGHT, output_page_height); |
1015 | 0 | } |
1016 | | |
1017 | 0 | if (thumbnail->output_profile) { |
1018 | | /* Colour management to an output ICC profile. Cases: |
1019 | | * |
1020 | | * - use icc_export if: |
1021 | | * - we have previously imported with icc_import OR |
1022 | | * - we have a CICP image in scRGB OR |
1023 | | * - we have a CICP image |
1024 | | * - otherwise, we must be in device space, use icc_transform |
1025 | | */ |
1026 | 0 | if (have_imported || |
1027 | 0 | have_cicped || |
1028 | 0 | can_cicp) { |
1029 | | /* If we're in CICP device space, transform to ICC PCS. |
1030 | | */ |
1031 | 0 | if (!have_cicped && |
1032 | 0 | can_cicp) { |
1033 | 0 | g_info("transforming CICP to ICC PCS"); |
1034 | 0 | if (vips_CICP2scRGB(in, &t[11], NULL) || |
1035 | 0 | vips_scRGB2XYZ(t[11], &t[12], NULL)) |
1036 | 0 | return -1; |
1037 | 0 | in = t[12]; |
1038 | 0 | } |
1039 | | |
1040 | | /* Should be in ICC PCS now. |
1041 | | */ |
1042 | 0 | g_assert(in->Type == VIPS_INTERPRETATION_XYZ); |
1043 | | |
1044 | | /* We are in ICC PCS. Export with the output profile, if any (this |
1045 | | * will export with the embedded input profile if there's no |
1046 | | * output profile). |
1047 | | * |
1048 | | * We always export as depth 8, to match the no profile case which |
1049 | | * uses vips_colourspace(sRGB|B_W). |
1050 | | */ |
1051 | 0 | g_info("exporting to device space with a profile"); |
1052 | 0 | if (vips_icc_export(in, &t[13], |
1053 | 0 | "output_profile", thumbnail->output_profile, |
1054 | 0 | "intent", thumbnail->intent, |
1055 | 0 | "depth", 8, |
1056 | 0 | NULL)) |
1057 | 0 | return -1; |
1058 | 0 | in = t[13]; |
1059 | 0 | } |
1060 | 0 | else { |
1061 | | /* We are in processing space, so b-w, srgb, scrgb. We can |
1062 | | * transform. thumbnail->input_profile can be null. |
1063 | | */ |
1064 | 0 | g_info("transforming with supplied profiles"); |
1065 | 0 | if (vips_icc_transform(in, &t[11], thumbnail->output_profile, |
1066 | 0 | "input_profile", thumbnail->input_profile, |
1067 | 0 | "intent", thumbnail->intent, |
1068 | 0 | "embedded", TRUE, |
1069 | 0 | "depth", 8, |
1070 | 0 | NULL)) |
1071 | 0 | return -1; |
1072 | 0 | in = t[11]; |
1073 | 0 | } |
1074 | | |
1075 | | /* Any CICP/CLLI metadata is stale after ICC export. |
1076 | | */ |
1077 | 0 | if (vips_copy(in, &t[14], NULL)) |
1078 | 0 | return -1; |
1079 | 0 | in = t[14]; |
1080 | |
|
1081 | 0 | vips__image_remove_cicp(in); |
1082 | 0 | vips__image_remove_clli(in); |
1083 | 0 | } |
1084 | 0 | else { |
1085 | | /* No colour management. |
1086 | | * |
1087 | | * Just undo any linearisation we applied. |
1088 | | */ |
1089 | 0 | if (have_cicped) { |
1090 | 0 | g_info("re-encoding to CICP"); |
1091 | 0 | if (vips_scRGB2CICP(in, &t[11], |
1092 | 0 | "depth", source_depth, |
1093 | 0 | NULL)) |
1094 | 0 | return -1; |
1095 | 0 | in = t[11]; |
1096 | 0 | } |
1097 | 0 | else if (have_imported) { |
1098 | 0 | g_info("unlinearising with a profile"); |
1099 | 0 | if (vips_icc_export(in, &t[11], |
1100 | 0 | "intent", thumbnail->intent, |
1101 | 0 | "depth", 8, |
1102 | 0 | NULL)) |
1103 | 0 | return -1; |
1104 | 0 | in = t[11]; |
1105 | 0 | } |
1106 | 0 | else if (thumbnail->linear) { |
1107 | | /* We did a generic conversion to linear space. Just reverse that. |
1108 | | */ |
1109 | 0 | VipsInterpretation interpretation; |
1110 | |
|
1111 | 0 | if (in->Bands < 3) |
1112 | 0 | interpretation = VIPS_INTERPRETATION_B_W; |
1113 | 0 | else |
1114 | 0 | interpretation = VIPS_INTERPRETATION_sRGB; |
1115 | |
|
1116 | 0 | g_info("converting to output space %s", |
1117 | 0 | vips_enum_nick(VIPS_TYPE_INTERPRETATION, interpretation)); |
1118 | 0 | if (vips_colourspace(in, &t[11], interpretation, NULL)) |
1119 | 0 | return -1; |
1120 | 0 | in = t[11]; |
1121 | 0 | } |
1122 | 0 | } |
1123 | | |
1124 | 0 | if (thumbnail->auto_rotate && |
1125 | 0 | thumbnail->orientation != 1) { |
1126 | 0 | g_info("rotating by EXIF orientation %d", thumbnail->orientation); |
1127 | | /* Need to copy to memory, we have to stay seq. |
1128 | | */ |
1129 | 0 | if (!(t[15] = vips_image_copy_memory(in)) || |
1130 | 0 | vips_autorot(t[15], &t[16], NULL)) |
1131 | 0 | return -1; |
1132 | 0 | in = t[16]; |
1133 | | |
1134 | | /* Also rotate the gainmap, if any. |
1135 | | */ |
1136 | 0 | if ((gainmap = vips_image_get_gainmap(in))) { |
1137 | 0 | vips_image_set_int(gainmap, |
1138 | 0 | VIPS_META_ORIENTATION, thumbnail->orientation); |
1139 | 0 | if (vips_autorot(gainmap, &t[17], NULL)) |
1140 | 0 | return -1; |
1141 | 0 | g_object_unref(gainmap); |
1142 | |
|
1143 | 0 | vips_image_set_image(in, "gainmap", t[17]); |
1144 | 0 | } |
1145 | 0 | } |
1146 | | |
1147 | | /* Crop after rotate so we don't need to rotate the crop box. |
1148 | | */ |
1149 | 0 | if (thumbnail->crop != VIPS_INTERESTING_NONE) { |
1150 | 0 | int page_height = vips_image_get_page_height(in); |
1151 | 0 | int n_pages = thumbnail->n_loaded_pages; |
1152 | | |
1153 | | /* crop_height is relative to a single frame. |
1154 | | */ |
1155 | 0 | int crop_width = VIPS_MIN(thumbnail->width, in->Xsize); |
1156 | 0 | int crop_height = VIPS_MIN(thumbnail->height, |
1157 | 0 | n_pages > 1 ? page_height : in->Ysize); |
1158 | 0 | int original_width = in->Xsize; |
1159 | |
|
1160 | 0 | g_info("cropping to %dx%d", crop_width, crop_height); |
1161 | |
|
1162 | 0 | gboolean rotated = thumbnail->swap && thumbnail->auto_rotate; |
1163 | 0 | int rotated_input_width = rotated ? |
1164 | 0 | thumbnail->input_height : thumbnail->input_width; |
1165 | 0 | int rotated_input_height = rotated ? |
1166 | 0 | thumbnail->input_width : thumbnail->input_height; |
1167 | 0 | double overall_hshrink = (double) rotated_input_width / in->Xsize; |
1168 | 0 | double overall_vshrink = (double) rotated_input_height / in->Ysize; |
1169 | |
|
1170 | 0 | int bbox_x, bbox_y; |
1171 | | |
1172 | | /* Need to copy to memory, we have to stay seq. |
1173 | | * |
1174 | | * FIXME ... could skip the copy if we've rotated. |
1175 | | */ |
1176 | 0 | if (!(t[18] = vips_image_copy_memory(in))) |
1177 | 0 | return -1; |
1178 | | |
1179 | | /* Smartcrop the first page to find a shared crop window, then |
1180 | | * apply that same window to every page (and to the gainmap, if |
1181 | | * any) so the bbox does not drift frame-to-frame. |
1182 | | */ |
1183 | 0 | if (vips_crop(t[18], &t[19], |
1184 | 0 | 0, 0, in->Xsize, page_height, NULL)) |
1185 | 0 | return -1; |
1186 | | |
1187 | 0 | if (vips_smartcrop(t[19], &t[20], |
1188 | 0 | crop_width, crop_height, |
1189 | 0 | "interesting", thumbnail->crop, |
1190 | 0 | "interesting_x", VIPS_ROUND_UINT( |
1191 | 0 | (double) thumbnail->interesting_x / overall_vshrink), |
1192 | 0 | "interesting_y", VIPS_ROUND_UINT( |
1193 | 0 | (double) thumbnail->interesting_y / overall_hshrink), |
1194 | 0 | NULL)) |
1195 | 0 | return -1; |
1196 | | |
1197 | 0 | bbox_x = -vips_image_get_xoffset(t[20]); |
1198 | 0 | bbox_y = -vips_image_get_yoffset(t[20]); |
1199 | |
|
1200 | 0 | if (!(t[21] = vips_thumbnail_crop_pages(object, t[18], |
1201 | 0 | bbox_x, bbox_y, crop_width, crop_height, |
1202 | 0 | page_height, n_pages))) |
1203 | 0 | return -1; |
1204 | 0 | in = t[21]; |
1205 | | |
1206 | | /* Also crop the gainmap, if any. |
1207 | | */ |
1208 | 0 | if ((gainmap = vips_image_get_gainmap(t[18]))) { |
1209 | 0 | int gm_page_height = vips_image_get_page_height(gainmap); |
1210 | 0 | int gm_n_pages = gainmap->Ysize / gm_page_height; |
1211 | 0 | double xscale = (double) gainmap->Xsize / original_width; |
1212 | 0 | double yscale = (double) gm_page_height / page_height; |
1213 | |
|
1214 | 0 | if (gm_n_pages != n_pages) { |
1215 | 0 | g_object_unref(gainmap); |
1216 | 0 | vips_error("thumbnail", "%s", |
1217 | 0 | "gainmap page count does not match image page count"); |
1218 | 0 | return -1; |
1219 | 0 | } |
1220 | | |
1221 | 0 | if (!(t[22] = vips_thumbnail_crop_pages(object, gainmap, |
1222 | 0 | bbox_x * xscale, bbox_y * yscale, |
1223 | 0 | crop_width * xscale, crop_height * yscale, |
1224 | 0 | gm_page_height, gm_n_pages))) { |
1225 | 0 | g_object_unref(gainmap); |
1226 | 0 | return -1; |
1227 | 0 | } |
1228 | 0 | g_object_unref(gainmap); |
1229 | |
|
1230 | 0 | if (vips_copy(in, &t[23], NULL)) |
1231 | 0 | return -1; |
1232 | 0 | in = t[23]; |
1233 | |
|
1234 | 0 | vips_image_set_image(in, "gainmap", t[22]); |
1235 | 0 | } |
1236 | 0 | } |
1237 | | |
1238 | 0 | g_object_set(thumbnail, "out", vips_image_new(), NULL); |
1239 | |
|
1240 | 0 | if (vips_image_write(in, thumbnail->out)) |
1241 | 0 | return -1; |
1242 | | |
1243 | 0 | return 0; |
1244 | 0 | } |
1245 | | |
1246 | | static void |
1247 | | vips_thumbnail_class_init(VipsThumbnailClass *class) |
1248 | 1 | { |
1249 | 1 | GObjectClass *gobject_class = G_OBJECT_CLASS(class); |
1250 | 1 | VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS(class); |
1251 | 1 | VipsOperationClass *operation_class = VIPS_OPERATION_CLASS(class); |
1252 | | |
1253 | 1 | gobject_class->dispose = vips_thumbnail_dispose; |
1254 | 1 | gobject_class->finalize = vips_thumbnail_finalize; |
1255 | 1 | gobject_class->set_property = vips_object_set_property; |
1256 | 1 | gobject_class->get_property = vips_object_get_property; |
1257 | | |
1258 | 1 | vobject_class->nickname = "thumbnail_base"; |
1259 | 1 | vobject_class->description = _("thumbnail generation"); |
1260 | 1 | vobject_class->build = vips_thumbnail_build; |
1261 | | |
1262 | | /* We mustn't cache these calls, since we open the file or buffer in |
1263 | | * sequential mode. |
1264 | | */ |
1265 | 1 | operation_class->flags |= VIPS_OPERATION_NOCACHE; |
1266 | | |
1267 | 1 | VIPS_ARG_IMAGE(class, "out", 2, |
1268 | 1 | _("Output"), |
1269 | 1 | _("Output image"), |
1270 | 1 | VIPS_ARGUMENT_REQUIRED_OUTPUT, |
1271 | 1 | G_STRUCT_OFFSET(VipsThumbnail, out)); |
1272 | | |
1273 | 1 | VIPS_ARG_INT(class, "width", 3, |
1274 | 1 | _("Target width"), |
1275 | 1 | _("Size to this width"), |
1276 | 1 | VIPS_ARGUMENT_REQUIRED_INPUT, |
1277 | 1 | G_STRUCT_OFFSET(VipsThumbnail, width), |
1278 | 1 | 1, VIPS_MAX_COORD, 1); |
1279 | | |
1280 | 1 | VIPS_ARG_INT(class, "height", 113, |
1281 | 1 | _("Target height"), |
1282 | 1 | _("Size to this height"), |
1283 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
1284 | 1 | G_STRUCT_OFFSET(VipsThumbnail, height), |
1285 | 1 | 1, VIPS_MAX_COORD, 1); |
1286 | | |
1287 | 1 | VIPS_ARG_ENUM(class, "size", 114, |
1288 | 1 | _("Size"), |
1289 | 1 | _("Only upsize, only downsize, or both"), |
1290 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
1291 | 1 | G_STRUCT_OFFSET(VipsThumbnail, size), |
1292 | 1 | VIPS_TYPE_SIZE, VIPS_SIZE_BOTH); |
1293 | | |
1294 | 1 | VIPS_ARG_BOOL(class, "no_rotate", 115, |
1295 | 1 | _("No rotate"), |
1296 | 1 | _("Don't use orientation tags to rotate image upright"), |
1297 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
1298 | 1 | G_STRUCT_OFFSET(VipsThumbnail, no_rotate), |
1299 | 1 | FALSE); |
1300 | | |
1301 | 1 | VIPS_ARG_ENUM(class, "crop", 116, |
1302 | 1 | _("Crop"), |
1303 | 1 | _("Reduce to fill target rectangle, then crop"), |
1304 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
1305 | 1 | G_STRUCT_OFFSET(VipsThumbnail, crop), |
1306 | 1 | VIPS_TYPE_INTERESTING, VIPS_INTERESTING_NONE); |
1307 | | |
1308 | 1 | VIPS_ARG_INT(class, "interesting_x", 117, |
1309 | 1 | _("Interesting x"), |
1310 | 1 | _("Horizontal position of the specific point of interest for cropping"), |
1311 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
1312 | 1 | G_STRUCT_OFFSET(VipsThumbnail, interesting_x), |
1313 | 1 | 0, VIPS_MAX_COORD, 0); |
1314 | | |
1315 | 1 | VIPS_ARG_INT(class, "interesting_y", 118, |
1316 | 1 | _("Interesting y"), |
1317 | 1 | _("Vertical position of the specific point of interest for cropping"), |
1318 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
1319 | 1 | G_STRUCT_OFFSET(VipsThumbnail, interesting_y), |
1320 | 1 | 0, VIPS_MAX_COORD, 0); |
1321 | | |
1322 | 1 | VIPS_ARG_BOOL(class, "linear", 119, |
1323 | 1 | _("Linear"), |
1324 | 1 | _("Reduce in linear light"), |
1325 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
1326 | 1 | G_STRUCT_OFFSET(VipsThumbnail, linear), |
1327 | 1 | FALSE); |
1328 | | |
1329 | 1 | VIPS_ARG_STRING(class, "input_profile", 120, |
1330 | 1 | _("Input profile"), |
1331 | 1 | _("Fallback input profile"), |
1332 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
1333 | 1 | G_STRUCT_OFFSET(VipsThumbnail, input_profile), |
1334 | 1 | NULL); |
1335 | | |
1336 | 1 | VIPS_ARG_STRING(class, "output_profile", 121, |
1337 | 1 | _("Output profile"), |
1338 | 1 | _("Fallback output profile"), |
1339 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
1340 | 1 | G_STRUCT_OFFSET(VipsThumbnail, output_profile), |
1341 | 1 | NULL); |
1342 | | |
1343 | 1 | VIPS_ARG_ENUM(class, "intent", 122, |
1344 | 1 | _("Intent"), |
1345 | 1 | _("Rendering intent"), |
1346 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
1347 | 1 | G_STRUCT_OFFSET(VipsThumbnail, intent), |
1348 | 1 | VIPS_TYPE_INTENT, VIPS_INTENT_RELATIVE); |
1349 | | |
1350 | 1 | VIPS_ARG_ENUM(class, "fail_on", 123, |
1351 | 1 | _("Fail on"), |
1352 | 1 | _("Error level to fail on"), |
1353 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
1354 | 1 | G_STRUCT_OFFSET(VipsThumbnail, fail_on), |
1355 | 1 | VIPS_TYPE_FAIL_ON, VIPS_FAIL_ON_NONE); |
1356 | | |
1357 | | /* BOOL args which default TRUE arguments don't work with the |
1358 | | * command-line -- GOption does not allow --auto-rotate=false. |
1359 | | * |
1360 | | * This is now replaced (though still functional) with "no-rotate", |
1361 | | * see above. |
1362 | | */ |
1363 | 1 | VIPS_ARG_BOOL(class, "auto_rotate", 130, |
1364 | 1 | _("Auto rotate"), |
1365 | 1 | _("Use orientation tags to rotate image upright"), |
1366 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT | VIPS_ARGUMENT_DEPRECATED, |
1367 | 1 | G_STRUCT_OFFSET(VipsThumbnail, auto_rotate), |
1368 | 1 | TRUE); |
1369 | | |
1370 | | /* Renamed as input-profile and output-profile for consistency with the |
1371 | | * rest of the API. |
1372 | | */ |
1373 | | |
1374 | 1 | VIPS_ARG_STRING(class, "import_profile", 131, |
1375 | 1 | _("Import profile"), |
1376 | 1 | _("Fallback import profile"), |
1377 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT | VIPS_ARGUMENT_DEPRECATED, |
1378 | 1 | G_STRUCT_OFFSET(VipsThumbnail, input_profile), |
1379 | 1 | NULL); |
1380 | | |
1381 | 1 | VIPS_ARG_STRING(class, "export_profile", 132, |
1382 | 1 | _("Export profile"), |
1383 | 1 | _("Fallback export profile"), |
1384 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT | VIPS_ARGUMENT_DEPRECATED, |
1385 | 1 | G_STRUCT_OFFSET(VipsThumbnail, output_profile), |
1386 | 1 | NULL); |
1387 | 1 | } |
1388 | | |
1389 | | static void |
1390 | | vips_thumbnail_init(VipsThumbnail *thumbnail) |
1391 | 0 | { |
1392 | 0 | thumbnail->width = 1; |
1393 | 0 | thumbnail->height = 1; |
1394 | 0 | thumbnail->auto_rotate = TRUE; |
1395 | 0 | thumbnail->intent = VIPS_INTENT_RELATIVE; |
1396 | 0 | thumbnail->fail_on = VIPS_FAIL_ON_NONE; |
1397 | 0 | } |
1398 | | |
1399 | | typedef struct _VipsThumbnailFile { |
1400 | | VipsThumbnail parent_object; |
1401 | | |
1402 | | char *filename; |
1403 | | } VipsThumbnailFile; |
1404 | | |
1405 | | typedef VipsThumbnailClass VipsThumbnailFileClass; |
1406 | | |
1407 | 2 | G_DEFINE_TYPE(VipsThumbnailFile, vips_thumbnail_file, |
1408 | 2 | vips_thumbnail_get_type()); |
1409 | 2 | |
1410 | 2 | /* Get the info from a file. |
1411 | 2 | */ |
1412 | 2 | static int |
1413 | 2 | vips_thumbnail_file_get_info(VipsThumbnail *thumbnail) |
1414 | 2 | { |
1415 | 0 | VipsThumbnailFile *file = (VipsThumbnailFile *) thumbnail; |
1416 | |
|
1417 | 0 | VipsImage *image; |
1418 | |
|
1419 | 0 | g_info("thumbnailing %s", file->filename); |
1420 | |
|
1421 | 0 | if (!(thumbnail->loader = vips_foreign_find_load(file->filename)) || |
1422 | 0 | !(image = vips_image_new_from_file(file->filename, NULL))) |
1423 | 0 | return -1; |
1424 | | |
1425 | 0 | vips_thumbnail_read_header(thumbnail, image); |
1426 | |
|
1427 | 0 | g_object_unref(image); |
1428 | |
|
1429 | 0 | return 0; |
1430 | 0 | } |
1431 | | |
1432 | | /* Open an image, pre-shrinking as appropriate. |
1433 | | */ |
1434 | | static VipsImage * |
1435 | | vips_thumbnail_file_open(VipsThumbnail *thumbnail, double factor) |
1436 | 0 | { |
1437 | 0 | VipsThumbnailFile *file = (VipsThumbnailFile *) thumbnail; |
1438 | |
|
1439 | 0 | if (vips_isprefix("VipsForeignLoadJpeg", thumbnail->loader) || |
1440 | 0 | vips_isprefix("VipsForeignLoadUhdr", thumbnail->loader)) { |
1441 | 0 | return vips_image_new_from_file(file->filename, |
1442 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1443 | 0 | "fail_on", thumbnail->fail_on, |
1444 | 0 | "shrink", (int) factor, |
1445 | 0 | NULL); |
1446 | 0 | } |
1447 | 0 | else if (vips_isprefix("VipsForeignLoadOpenslide", thumbnail->loader)) { |
1448 | 0 | return vips_image_new_from_file(file->filename, |
1449 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1450 | 0 | "fail_on", thumbnail->fail_on, |
1451 | 0 | "level", (int) factor, |
1452 | 0 | NULL); |
1453 | 0 | } |
1454 | 0 | else if (vips_isprefix("VipsForeignLoadPdf", thumbnail->loader) || |
1455 | 0 | vips_isprefix("VipsForeignLoadSvg", thumbnail->loader) || |
1456 | 0 | vips_isprefix("VipsForeignLoadWebp", thumbnail->loader)) { |
1457 | 0 | return vips_image_new_from_file(file->filename, |
1458 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1459 | 0 | "fail_on", thumbnail->fail_on, |
1460 | 0 | "scale", 1.0 / factor, |
1461 | 0 | NULL); |
1462 | 0 | } |
1463 | 0 | else if (vips_isprefix("VipsForeignLoadJp2k", thumbnail->loader)) { |
1464 | | /* jp2k optionally uses page-based pyramids. |
1465 | | */ |
1466 | 0 | if (thumbnail->page_pyramid) |
1467 | 0 | return vips_image_new_from_file(file->filename, |
1468 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1469 | 0 | "fail_on", thumbnail->fail_on, |
1470 | 0 | "page", (int) factor, |
1471 | 0 | NULL); |
1472 | 0 | else |
1473 | 0 | return vips_image_new_from_file(file->filename, |
1474 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1475 | 0 | "fail_on", thumbnail->fail_on, |
1476 | 0 | NULL); |
1477 | 0 | } |
1478 | 0 | else if (vips_isprefix("VipsForeignLoadTiff", thumbnail->loader)) { |
1479 | | /* We support three modes: subifd pyramids, page-based |
1480 | | * pyramids, and simple multi-page TIFFs (no pyramid). |
1481 | | */ |
1482 | 0 | if (thumbnail->subifd_pyramid) |
1483 | 0 | return vips_image_new_from_file(file->filename, |
1484 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1485 | 0 | "fail_on", thumbnail->fail_on, |
1486 | 0 | "subifd", (int) factor, |
1487 | 0 | NULL); |
1488 | 0 | else if (thumbnail->page_pyramid) |
1489 | 0 | return vips_image_new_from_file(file->filename, |
1490 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1491 | 0 | "fail_on", thumbnail->fail_on, |
1492 | 0 | "page", (int) factor, |
1493 | 0 | NULL); |
1494 | 0 | else |
1495 | 0 | return vips_image_new_from_file(file->filename, |
1496 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1497 | 0 | "fail_on", thumbnail->fail_on, |
1498 | 0 | NULL); |
1499 | 0 | } |
1500 | 0 | else if (vips_isprefix("VipsForeignLoadHeif", thumbnail->loader)) { |
1501 | 0 | return vips_image_new_from_file(file->filename, |
1502 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1503 | 0 | "fail_on", thumbnail->fail_on, |
1504 | 0 | "thumbnail", (int) factor, |
1505 | 0 | NULL); |
1506 | 0 | } |
1507 | 0 | else if (vips_isprefix("VipsForeignLoadDcRaw", thumbnail->loader)) { |
1508 | 0 | VipsImage *image; |
1509 | |
|
1510 | 0 | if (!(image = vips_image_new_from_file(file->filename, |
1511 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1512 | 0 | "fail_on", thumbnail->fail_on, |
1513 | 0 | NULL))) |
1514 | 0 | return NULL; |
1515 | | |
1516 | 0 | VipsImage *blob; |
1517 | 0 | if (!(blob = vips_thumbnail_get_blob(thumbnail, image, factor))) { |
1518 | 0 | VIPS_UNREF(image); |
1519 | 0 | return NULL; |
1520 | 0 | } |
1521 | 0 | VIPS_UNREF(image); |
1522 | |
|
1523 | 0 | return blob; |
1524 | 0 | } |
1525 | 0 | else |
1526 | 0 | return vips_image_new_from_file(file->filename, |
1527 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1528 | 0 | "fail_on", thumbnail->fail_on, |
1529 | 0 | NULL); |
1530 | 0 | } |
1531 | | |
1532 | | static void |
1533 | | vips_thumbnail_file_class_init(VipsThumbnailClass *class) |
1534 | 1 | { |
1535 | 1 | GObjectClass *gobject_class = G_OBJECT_CLASS(class); |
1536 | 1 | VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS(class); |
1537 | 1 | VipsThumbnailClass *thumbnail_class = VIPS_THUMBNAIL_CLASS(class); |
1538 | | |
1539 | 1 | gobject_class->set_property = vips_object_set_property; |
1540 | 1 | gobject_class->get_property = vips_object_get_property; |
1541 | | |
1542 | 1 | vobject_class->nickname = "thumbnail"; |
1543 | 1 | vobject_class->description = _("generate thumbnail from file"); |
1544 | | |
1545 | 1 | thumbnail_class->get_info = vips_thumbnail_file_get_info; |
1546 | 1 | thumbnail_class->open = vips_thumbnail_file_open; |
1547 | | |
1548 | 1 | VIPS_ARG_STRING(class, "filename", 1, |
1549 | 1 | _("Filename"), |
1550 | 1 | _("Filename to read from"), |
1551 | 1 | VIPS_ARGUMENT_REQUIRED_INPUT, |
1552 | 1 | G_STRUCT_OFFSET(VipsThumbnailFile, filename), |
1553 | 1 | NULL); |
1554 | 1 | } |
1555 | | |
1556 | | static void |
1557 | | vips_thumbnail_file_init(VipsThumbnailFile *file) |
1558 | 0 | { |
1559 | 0 | } |
1560 | | |
1561 | | /** |
1562 | | * vips_thumbnail: |
1563 | | * @filename: file to read from |
1564 | | * @out: (out): output image |
1565 | | * @width: target width in pixels |
1566 | | * @...: `NULL`-terminated list of optional named arguments |
1567 | | * |
1568 | | * Make a thumbnail from a file. |
1569 | | * |
1570 | | * Shrinking is done in three stages: using any |
1571 | | * shrink-on-load features available in the image load library, using a block |
1572 | | * shrink, and using a lanczos3 shrink. At least the final 200% is done with |
1573 | | * lanczos3. The output should be high quality, and the operation should be |
1574 | | * quick. |
1575 | | * |
1576 | | * See [ctor@Image.thumbnail_buffer] to thumbnail from a memory buffer, or |
1577 | | * [ctor@Image.thumbnail_source] to thumbnail from an arbitrary byte source. |
1578 | | * |
1579 | | * By default, libvips will only use the first frame of animated or multipage |
1580 | | * images. To thumbnail all pages or frames, pass `n=-1` to the loader in |
1581 | | * @filename, for example `"x.gif[n=-1]"`. |
1582 | | * |
1583 | | * The output image will fit within a square of size @width x @width. You can |
1584 | | * specify a separate height with the @height option. Set either @width or |
1585 | | * @height to a very large number to ignore that dimension. |
1586 | | * |
1587 | | * If you set @crop, then the output image will fill the whole of the @width x |
1588 | | * @height rectangle, with any excess cropped away. See [method@Image.smartcrop] for |
1589 | | * details on the cropping strategy. To use a specific point of interest for |
1590 | | * cropping use [enum@Vips.Interesting.SPECIFIC] and set @interesting_x and |
1591 | | * @interesting_y. |
1592 | | * |
1593 | | * Normally the operation will upsize or downsize as required to fit the image |
1594 | | * inside or outside the target size. If @size is set to [enum@Vips.Size.UP], |
1595 | | * the operation will only upsize and will just copy if asked to downsize. |
1596 | | * If @size is set to [enum@Vips.Size.DOWN], the operation will only downsize |
1597 | | * and will just copy if asked to upsize. |
1598 | | * If @size is [enum@Vips.Size.FORCE], the image aspect ratio will be broken |
1599 | | * and the image will be forced to fit the target. |
1600 | | * |
1601 | | * Normally any orientation tags on the input image (such as EXIF tags) are |
1602 | | * interpreted to rotate the image upright. If you set @no_rotate to `TRUE`, |
1603 | | * these tags will not be interpreted. |
1604 | | * |
1605 | | * Shrinking is normally done in sRGB colourspace. Set @linear to shrink in |
1606 | | * linear light colourspace instead. This can give better results, but can |
1607 | | * also be far slower, since tricks like JPEG shrink-on-load cannot be used in |
1608 | | * linear space. |
1609 | | * |
1610 | | * If you set @output_profile to the filename of an ICC profile, the image |
1611 | | * will be transformed to the target colourspace before writing to the |
1612 | | * output. You can also give an @input_profile which will be used if the |
1613 | | * input image has no ICC profile, or if the profile embedded in the |
1614 | | * input image is broken, or if the input has a valid CICP record. |
1615 | | * |
1616 | | * A CICP record describes the encoding, so it is what @linear linearises |
1617 | | * through. Such images are returned in the encoding and bit depth they arrived |
1618 | | * in, unless @output_profile asks for something else. |
1619 | | * |
1620 | | * Use @intent to set the rendering intent for any ICC transform. The default |
1621 | | * is [enum@Vips.Intent.RELATIVE]. |
1622 | | * |
1623 | | * Use @fail_on to control the types of error that will cause loading to fail. |
1624 | | * The default is [enum@Vips.FailOn.NONE], ie. thumbnail is permissive. |
1625 | | * |
1626 | | * ::: tip "Optional arguments" |
1627 | | * * @height: `gint`, target height in pixels |
1628 | | * * @size: [enum@Size], upsize, downsize, both or force |
1629 | | * * @no_rotate: `gboolean`, don't rotate upright using orientation tag |
1630 | | * * @crop: [enum@Interesting], shrink and crop to fill target |
1631 | | * * @interesting_x: `gint`, horizontal position of the specific point of |
1632 | | * interest when cropping with [enum@Vips.Interesting.SPECIFIC]) |
1633 | | * * @interesting_y: `gint`, vertical position of the specific point of |
1634 | | * interest when cropping with [enum@Vips.Interesting.SPECIFIC]) |
1635 | | * * @linear: `gboolean`, perform shrink in linear light |
1636 | | * * @input_profile: `gchararray`, fallback input ICC profile |
1637 | | * * @output_profile: `gchararray`, output ICC profile |
1638 | | * * @intent: [enum@Intent], rendering intent |
1639 | | * * @fail_on: [enum@FailOn], load error types to fail on |
1640 | | * |
1641 | | * ::: seealso |
1642 | | * [ctor@Image.thumbnail_buffer]. |
1643 | | * |
1644 | | * Returns: 0 on success, -1 on error. |
1645 | | */ |
1646 | | int |
1647 | | vips_thumbnail(const char *filename, VipsImage **out, int width, ...) |
1648 | 0 | { |
1649 | 0 | va_list ap; |
1650 | 0 | int result; |
1651 | |
|
1652 | 0 | va_start(ap, width); |
1653 | 0 | result = vips_call_split("thumbnail", ap, filename, out, width); |
1654 | 0 | va_end(ap); |
1655 | |
|
1656 | 0 | return result; |
1657 | 0 | } |
1658 | | |
1659 | | typedef struct _VipsThumbnailBuffer { |
1660 | | VipsThumbnail parent_object; |
1661 | | |
1662 | | VipsBlob *blob; // buffer as a blob |
1663 | | VipsSource *source; // ... and blob as a source |
1664 | | char *option_string; |
1665 | | } VipsThumbnailBuffer; |
1666 | | |
1667 | | typedef VipsThumbnailClass VipsThumbnailBufferClass; |
1668 | | |
1669 | 2 | G_DEFINE_TYPE(VipsThumbnailBuffer, vips_thumbnail_buffer, |
1670 | 2 | vips_thumbnail_get_type()); |
1671 | 2 | |
1672 | 2 | static void |
1673 | 2 | vips_thumbnail_buffer_dispose(GObject *gobject) |
1674 | 2 | { |
1675 | 0 | VipsThumbnailBuffer *buffer = (VipsThumbnailBuffer *) gobject; |
1676 | |
|
1677 | 0 | VIPS_UNREF(buffer->source); |
1678 | |
|
1679 | 0 | G_OBJECT_CLASS(vips_thumbnail_buffer_parent_class)->dispose(gobject); |
1680 | 0 | } |
1681 | | |
1682 | | static int |
1683 | | vips_thumbnail_buffer_build(VipsObject *object) |
1684 | 0 | { |
1685 | 0 | VipsThumbnailBuffer *buffer = (VipsThumbnailBuffer *) object; |
1686 | |
|
1687 | 0 | if (buffer->blob && |
1688 | 0 | !(buffer->source = vips_source_new_from_blob(buffer->blob))) |
1689 | 0 | return -1; |
1690 | | |
1691 | 0 | return VIPS_OBJECT_CLASS(vips_thumbnail_buffer_parent_class)-> |
1692 | 0 | build(object); |
1693 | 0 | } |
1694 | | |
1695 | | /* Get the info from a buffer. |
1696 | | */ |
1697 | | static int |
1698 | | vips_thumbnail_buffer_get_info(VipsThumbnail *thumbnail) |
1699 | 0 | { |
1700 | 0 | VipsThumbnailBuffer *buffer = (VipsThumbnailBuffer *) thumbnail; |
1701 | 0 | size_t length; |
1702 | 0 | const void *data = vips_blob_get(buffer->blob, &length); |
1703 | |
|
1704 | 0 | VipsImage *image; |
1705 | |
|
1706 | 0 | g_info("thumbnailing %zd bytes of data", length); |
1707 | |
|
1708 | 0 | if (!(thumbnail->loader = vips_foreign_find_load_buffer(data, length)) || |
1709 | 0 | !(image = vips_image_new_from_source(buffer->source, |
1710 | 0 | buffer->option_string, NULL))) |
1711 | 0 | return -1; |
1712 | | |
1713 | 0 | vips_thumbnail_read_header(thumbnail, image); |
1714 | |
|
1715 | 0 | g_object_unref(image); |
1716 | |
|
1717 | 0 | return 0; |
1718 | 0 | } |
1719 | | |
1720 | | /* Open an image, scaling as appropriate. |
1721 | | */ |
1722 | | static VipsImage * |
1723 | | vips_thumbnail_buffer_open(VipsThumbnail *thumbnail, double factor) |
1724 | 0 | { |
1725 | 0 | VipsThumbnailBuffer *buffer = (VipsThumbnailBuffer *) thumbnail; |
1726 | |
|
1727 | 0 | if (vips_isprefix("VipsForeignLoadJpeg", thumbnail->loader) || |
1728 | 0 | vips_isprefix("VipsForeignLoadUhdr", thumbnail->loader)) { |
1729 | 0 | return vips_image_new_from_source(buffer->source, buffer->option_string, |
1730 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1731 | 0 | "fail_on", thumbnail->fail_on, |
1732 | 0 | "shrink", (int) factor, |
1733 | 0 | NULL); |
1734 | 0 | } |
1735 | 0 | else if (vips_isprefix("VipsForeignLoadPdf", thumbnail->loader) || |
1736 | 0 | vips_isprefix("VipsForeignLoadSvg", thumbnail->loader) || |
1737 | 0 | vips_isprefix("VipsForeignLoadWebp", thumbnail->loader)) { |
1738 | 0 | return vips_image_new_from_source(buffer->source, buffer->option_string, |
1739 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1740 | 0 | "fail_on", thumbnail->fail_on, |
1741 | 0 | "scale", 1.0 / factor, |
1742 | 0 | NULL); |
1743 | 0 | } |
1744 | 0 | else if (vips_isprefix("VipsForeignLoadJp2k", thumbnail->loader)) { |
1745 | | /* Optional page-based pyramids. |
1746 | | */ |
1747 | 0 | if (thumbnail->page_pyramid) |
1748 | 0 | return vips_image_new_from_source(buffer->source, |
1749 | 0 | buffer->option_string, |
1750 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1751 | 0 | "fail_on", thumbnail->fail_on, |
1752 | 0 | "page", (int) factor, |
1753 | 0 | NULL); |
1754 | 0 | else |
1755 | 0 | return vips_image_new_from_source(buffer->source, |
1756 | 0 | buffer->option_string, |
1757 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1758 | 0 | "fail_on", thumbnail->fail_on, |
1759 | 0 | NULL); |
1760 | 0 | } |
1761 | 0 | else if (vips_isprefix("VipsForeignLoadTiff", thumbnail->loader)) { |
1762 | | /* We support three modes: subifd pyramids, page-based |
1763 | | * pyramids, and simple multi-page TIFFs (no pyramid). |
1764 | | */ |
1765 | 0 | if (thumbnail->subifd_pyramid) |
1766 | 0 | return vips_image_new_from_source(buffer->source, |
1767 | 0 | buffer->option_string, |
1768 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1769 | 0 | "fail_on", thumbnail->fail_on, |
1770 | 0 | "subifd", (int) factor, |
1771 | 0 | NULL); |
1772 | 0 | else if (thumbnail->page_pyramid) |
1773 | 0 | return vips_image_new_from_source(buffer->source, |
1774 | 0 | buffer->option_string, |
1775 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1776 | 0 | "fail_on", thumbnail->fail_on, |
1777 | 0 | "page", (int) factor, |
1778 | 0 | NULL); |
1779 | 0 | else |
1780 | 0 | return vips_image_new_from_source(buffer->source, |
1781 | 0 | buffer->option_string, |
1782 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1783 | 0 | "fail_on", thumbnail->fail_on, |
1784 | 0 | NULL); |
1785 | 0 | } |
1786 | 0 | else if (vips_isprefix("VipsForeignLoadHeif", thumbnail->loader)) { |
1787 | 0 | return vips_image_new_from_source(buffer->source, buffer->option_string, |
1788 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1789 | 0 | "fail_on", thumbnail->fail_on, |
1790 | 0 | "thumbnail", (int) factor, |
1791 | 0 | NULL); |
1792 | 0 | } |
1793 | 0 | else |
1794 | 0 | return vips_image_new_from_source(buffer->source, buffer->option_string, |
1795 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1796 | 0 | "fail_on", thumbnail->fail_on, |
1797 | 0 | NULL); |
1798 | 0 | } |
1799 | | |
1800 | | static void |
1801 | | vips_thumbnail_buffer_class_init(VipsThumbnailClass *class) |
1802 | 1 | { |
1803 | 1 | GObjectClass *gobject_class = G_OBJECT_CLASS(class); |
1804 | 1 | VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS(class); |
1805 | 1 | VipsThumbnailClass *thumbnail_class = VIPS_THUMBNAIL_CLASS(class); |
1806 | | |
1807 | 1 | gobject_class->dispose = vips_thumbnail_buffer_dispose; |
1808 | 1 | gobject_class->set_property = vips_object_set_property; |
1809 | 1 | gobject_class->get_property = vips_object_get_property; |
1810 | | |
1811 | 1 | vobject_class->nickname = "thumbnail_buffer"; |
1812 | 1 | vobject_class->description = _("generate thumbnail from buffer"); |
1813 | 1 | vobject_class->build = vips_thumbnail_buffer_build; |
1814 | | |
1815 | 1 | thumbnail_class->get_info = vips_thumbnail_buffer_get_info; |
1816 | 1 | thumbnail_class->open = vips_thumbnail_buffer_open; |
1817 | | |
1818 | 1 | VIPS_ARG_BOXED(class, "buffer", 1, |
1819 | 1 | _("Buffer"), |
1820 | 1 | _("Buffer to load from"), |
1821 | 1 | VIPS_ARGUMENT_REQUIRED_INPUT, |
1822 | 1 | G_STRUCT_OFFSET(VipsThumbnailBuffer, blob), |
1823 | 1 | VIPS_TYPE_BLOB); |
1824 | | |
1825 | 1 | VIPS_ARG_STRING(class, "option_string", 20, |
1826 | 1 | _("Extra options"), |
1827 | 1 | _("Options that are passed on to the underlying loader"), |
1828 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
1829 | 1 | G_STRUCT_OFFSET(VipsThumbnailBuffer, option_string), |
1830 | 1 | ""); |
1831 | 1 | } |
1832 | | |
1833 | | static void |
1834 | | vips_thumbnail_buffer_init(VipsThumbnailBuffer *buffer) |
1835 | 0 | { |
1836 | 0 | } |
1837 | | |
1838 | | /** |
1839 | | * vips_thumbnail_buffer: |
1840 | | * @buf: (array length=len) (element-type guint8): memory area to load |
1841 | | * @len: (type gsize): size of memory area |
1842 | | * @out: (out): output image |
1843 | | * @width: target width in pixels |
1844 | | * @...: `NULL`-terminated list of optional named arguments |
1845 | | * |
1846 | | * Exactly as [ctor@Image.thumbnail], but read from a memory buffer. |
1847 | | * |
1848 | | * One extra optional argument, @option_string, lets you pass options to the |
1849 | | * underlying loader. |
1850 | | * |
1851 | | * ::: tip "Optional arguments" |
1852 | | * * @height: `gint`, target height in pixels |
1853 | | * * @size: [enum@Size], upsize, downsize, both or force |
1854 | | * * @no_rotate: `gboolean`, don't rotate upright using orientation tag |
1855 | | * * @crop: [enum@Interesting], shrink and crop to fill target |
1856 | | * * @interesting_x: `gint`, horizontal position of the specific point of |
1857 | | * interest when cropping with [enum@Vips.Interesting.SPECIFIC]) |
1858 | | * * @interesting_y: `gint`, vertical position of the specific point of |
1859 | | * interest when cropping with [enum@Vips.Interesting.SPECIFIC]) |
1860 | | * * @linear: `gboolean`, perform shrink in linear light |
1861 | | * * @input_profile: `gchararray`, fallback input ICC profile |
1862 | | * * @output_profile: `gchararray`, output ICC profile |
1863 | | * * @intent: [enum@Intent], rendering intent |
1864 | | * * @fail_on: [enum@FailOn], load error types to fail on |
1865 | | * * @option_string: `gchararray`, extra loader options |
1866 | | * |
1867 | | * ::: seealso |
1868 | | * [ctor@Image.thumbnail]. |
1869 | | * |
1870 | | * Returns: 0 on success, -1 on error. |
1871 | | */ |
1872 | | int |
1873 | | vips_thumbnail_buffer(void *buf, size_t len, VipsImage **out, int width, ...) |
1874 | 0 | { |
1875 | 0 | va_list ap; |
1876 | 0 | VipsBlob *blob; |
1877 | 0 | int result; |
1878 | | |
1879 | | /* We don't take a copy of the data or free it. |
1880 | | */ |
1881 | 0 | blob = vips_blob_new(NULL, buf, len); |
1882 | |
|
1883 | 0 | va_start(ap, width); |
1884 | 0 | result = vips_call_split("thumbnail_buffer", ap, blob, out, width); |
1885 | 0 | va_end(ap); |
1886 | |
|
1887 | 0 | vips_area_unref(VIPS_AREA(blob)); |
1888 | |
|
1889 | 0 | return result; |
1890 | 0 | } |
1891 | | |
1892 | | typedef struct _VipsThumbnailSource { |
1893 | | VipsThumbnail parent_object; |
1894 | | |
1895 | | VipsSource *source; |
1896 | | char *option_string; |
1897 | | } VipsThumbnailSource; |
1898 | | |
1899 | | typedef VipsThumbnailClass VipsThumbnailSourceClass; |
1900 | | |
1901 | 2 | G_DEFINE_TYPE(VipsThumbnailSource, vips_thumbnail_source, |
1902 | 2 | vips_thumbnail_get_type()); |
1903 | 2 | |
1904 | 2 | /* Get the info from a source. |
1905 | 2 | */ |
1906 | 2 | static int |
1907 | 2 | vips_thumbnail_source_get_info(VipsThumbnail *thumbnail) |
1908 | 2 | { |
1909 | 0 | VipsThumbnailSource *source = (VipsThumbnailSource *) thumbnail; |
1910 | |
|
1911 | 0 | VipsImage *image; |
1912 | |
|
1913 | 0 | g_info("thumbnailing source"); |
1914 | |
|
1915 | 0 | if (!(thumbnail->loader = vips_foreign_find_load_source(source->source)) || |
1916 | 0 | !(image = vips_image_new_from_source(source->source, |
1917 | 0 | source->option_string, NULL))) |
1918 | 0 | return -1; |
1919 | | |
1920 | 0 | vips_thumbnail_read_header(thumbnail, image); |
1921 | |
|
1922 | 0 | g_object_unref(image); |
1923 | |
|
1924 | 0 | return 0; |
1925 | 0 | } |
1926 | | |
1927 | | /* Open an image, scaling as appropriate. |
1928 | | */ |
1929 | | static VipsImage * |
1930 | | vips_thumbnail_source_open(VipsThumbnail *thumbnail, double factor) |
1931 | 0 | { |
1932 | 0 | VipsThumbnailSource *source = (VipsThumbnailSource *) thumbnail; |
1933 | |
|
1934 | 0 | if (vips_isprefix("VipsForeignLoadJpeg", thumbnail->loader) || |
1935 | 0 | vips_isprefix("VipsForeignLoadUhdr", thumbnail->loader)) { |
1936 | 0 | return vips_image_new_from_source(source->source, source->option_string, |
1937 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1938 | 0 | "fail_on", thumbnail->fail_on, |
1939 | 0 | "shrink", (int) factor, |
1940 | 0 | NULL); |
1941 | 0 | } |
1942 | 0 | else if (vips_isprefix("VipsForeignLoadOpenslide", thumbnail->loader)) { |
1943 | 0 | return vips_image_new_from_source(source->source, source->option_string, |
1944 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1945 | 0 | "fail_on", thumbnail->fail_on, |
1946 | 0 | "level", (int) factor, |
1947 | 0 | NULL); |
1948 | 0 | } |
1949 | 0 | else if (vips_isprefix("VipsForeignLoadPdf", thumbnail->loader) || |
1950 | 0 | vips_isprefix("VipsForeignLoadSvg", thumbnail->loader) || |
1951 | 0 | vips_isprefix("VipsForeignLoadWebp", thumbnail->loader)) { |
1952 | 0 | return vips_image_new_from_source(source->source, source->option_string, |
1953 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1954 | 0 | "fail_on", thumbnail->fail_on, |
1955 | 0 | "scale", 1.0 / factor, |
1956 | 0 | NULL); |
1957 | 0 | } |
1958 | 0 | else if (vips_isprefix("VipsForeignLoadJp2k", thumbnail->loader)) { |
1959 | | /* Optional page-based pyramids. |
1960 | | */ |
1961 | 0 | if (thumbnail->page_pyramid) |
1962 | 0 | return vips_image_new_from_source(source->source, |
1963 | 0 | source->option_string, |
1964 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1965 | 0 | "fail_on", thumbnail->fail_on, |
1966 | 0 | "page", (int) factor, |
1967 | 0 | NULL); |
1968 | 0 | else |
1969 | 0 | return vips_image_new_from_source(source->source, |
1970 | 0 | source->option_string, |
1971 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1972 | 0 | "fail_on", thumbnail->fail_on, |
1973 | 0 | NULL); |
1974 | 0 | } |
1975 | 0 | else if (vips_isprefix("VipsForeignLoadTiff", thumbnail->loader)) { |
1976 | | /* We support three modes: subifd pyramids, page-based |
1977 | | * pyramids, and simple multi-page TIFFs (no pyramid). |
1978 | | */ |
1979 | 0 | if (thumbnail->subifd_pyramid) |
1980 | 0 | return vips_image_new_from_source(source->source, |
1981 | 0 | source->option_string, |
1982 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1983 | 0 | "fail_on", thumbnail->fail_on, |
1984 | 0 | "subifd", (int) factor, |
1985 | 0 | NULL); |
1986 | 0 | else if (thumbnail->page_pyramid) |
1987 | 0 | return vips_image_new_from_source(source->source, |
1988 | 0 | source->option_string, |
1989 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1990 | 0 | "fail_on", thumbnail->fail_on, |
1991 | 0 | "page", (int) factor, |
1992 | 0 | NULL); |
1993 | 0 | else |
1994 | 0 | return vips_image_new_from_source(source->source, |
1995 | 0 | source->option_string, |
1996 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
1997 | 0 | "fail_on", thumbnail->fail_on, |
1998 | 0 | NULL); |
1999 | 0 | } |
2000 | 0 | else if (vips_isprefix("VipsForeignLoadHeif", thumbnail->loader)) { |
2001 | 0 | return vips_image_new_from_source(source->source, source->option_string, |
2002 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
2003 | 0 | "fail_on", thumbnail->fail_on, |
2004 | 0 | "thumbnail", (int) factor, |
2005 | 0 | NULL); |
2006 | 0 | } |
2007 | 0 | else |
2008 | 0 | return vips_image_new_from_source(source->source, source->option_string, |
2009 | 0 | "access", VIPS_ACCESS_SEQUENTIAL, |
2010 | 0 | "fail_on", thumbnail->fail_on, |
2011 | 0 | NULL); |
2012 | 0 | } |
2013 | | |
2014 | | static void |
2015 | | vips_thumbnail_source_class_init(VipsThumbnailClass *class) |
2016 | 1 | { |
2017 | 1 | GObjectClass *gobject_class = G_OBJECT_CLASS(class); |
2018 | 1 | VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS(class); |
2019 | 1 | VipsThumbnailClass *thumbnail_class = VIPS_THUMBNAIL_CLASS(class); |
2020 | | |
2021 | 1 | gobject_class->set_property = vips_object_set_property; |
2022 | 1 | gobject_class->get_property = vips_object_get_property; |
2023 | | |
2024 | 1 | vobject_class->nickname = "thumbnail_source"; |
2025 | 1 | vobject_class->description = _("generate thumbnail from source"); |
2026 | | |
2027 | 1 | thumbnail_class->get_info = vips_thumbnail_source_get_info; |
2028 | 1 | thumbnail_class->open = vips_thumbnail_source_open; |
2029 | | |
2030 | 1 | VIPS_ARG_OBJECT(class, "source", 1, |
2031 | 1 | _("Source"), |
2032 | 1 | _("Source to load from"), |
2033 | 1 | VIPS_ARGUMENT_REQUIRED_INPUT, |
2034 | 1 | G_STRUCT_OFFSET(VipsThumbnailSource, source), |
2035 | 1 | VIPS_TYPE_SOURCE); |
2036 | | |
2037 | 1 | VIPS_ARG_STRING(class, "option_string", 20, |
2038 | 1 | _("Extra options"), |
2039 | 1 | _("Options that are passed on to the underlying loader"), |
2040 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
2041 | 1 | G_STRUCT_OFFSET(VipsThumbnailSource, option_string), |
2042 | 1 | ""); |
2043 | 1 | } |
2044 | | |
2045 | | static void |
2046 | | vips_thumbnail_source_init(VipsThumbnailSource *source) |
2047 | 0 | { |
2048 | 0 | } |
2049 | | |
2050 | | /** |
2051 | | * vips_thumbnail_source: |
2052 | | * @source: source to thumbnail |
2053 | | * @out: (out): output image |
2054 | | * @width: target width in pixels |
2055 | | * @...: `NULL`-terminated list of optional named arguments |
2056 | | * |
2057 | | * Exactly as [ctor@Image.thumbnail], but read from a source. |
2058 | | * |
2059 | | * One extra |
2060 | | * optional argument, @option_string, lets you pass options to the underlying |
2061 | | * loader. |
2062 | | * |
2063 | | * ::: tip "Optional arguments" |
2064 | | * * @height: `gint`, target height in pixels |
2065 | | * * @size: [enum@Size], upsize, downsize, both or force |
2066 | | * * @no_rotate: `gboolean`, don't rotate upright using orientation tag |
2067 | | * * @crop: [enum@Interesting], shrink and crop to fill target |
2068 | | * * @interesting_x: `gint`, horizontal position of the specific point of |
2069 | | * interest when cropping with [enum@Vips.Interesting.SPECIFIC]) |
2070 | | * * @interesting_y: `gint`, vertical position of the specific point of |
2071 | | * interest when cropping with [enum@Vips.Interesting.SPECIFIC]) |
2072 | | * * @linear: `gboolean`, perform shrink in linear light |
2073 | | * * @input_profile: `gchararray`, fallback input ICC profile |
2074 | | * * @output_profile: `gchararray`, output ICC profile |
2075 | | * * @intent: [enum@Intent], rendering intent |
2076 | | * * @fail_on: [enum@FailOn], load error types to fail on |
2077 | | * * @option_string: `gchararray`, extra loader options |
2078 | | * |
2079 | | * ::: seealso |
2080 | | * [ctor@Image.thumbnail]. |
2081 | | * |
2082 | | * Returns: 0 on success, -1 on error. |
2083 | | */ |
2084 | | int |
2085 | | vips_thumbnail_source(VipsSource *source, VipsImage **out, int width, ...) |
2086 | 0 | { |
2087 | 0 | va_list ap; |
2088 | 0 | int result; |
2089 | |
|
2090 | 0 | va_start(ap, width); |
2091 | 0 | result = vips_call_split("thumbnail_source", ap, source, out, width); |
2092 | 0 | va_end(ap); |
2093 | |
|
2094 | 0 | return result; |
2095 | 0 | } |
2096 | | |
2097 | | typedef struct _VipsThumbnailImage { |
2098 | | VipsThumbnail parent_object; |
2099 | | |
2100 | | VipsImage *in; |
2101 | | } VipsThumbnailImage; |
2102 | | |
2103 | | typedef VipsThumbnailClass VipsThumbnailImageClass; |
2104 | | |
2105 | 2 | G_DEFINE_TYPE(VipsThumbnailImage, vips_thumbnail_image, |
2106 | 2 | vips_thumbnail_get_type()); |
2107 | 2 | |
2108 | 2 | /* Get the info from a image. |
2109 | 2 | */ |
2110 | 2 | static int |
2111 | 2 | vips_thumbnail_image_get_info(VipsThumbnail *thumbnail) |
2112 | 2 | { |
2113 | 0 | VipsThumbnailImage *image = (VipsThumbnailImage *) thumbnail; |
2114 | | |
2115 | | /* Doesn't really matter what we put here. |
2116 | | */ |
2117 | 0 | thumbnail->loader = "image source"; |
2118 | |
|
2119 | 0 | vips_thumbnail_read_header(thumbnail, image->in); |
2120 | |
|
2121 | 0 | return 0; |
2122 | 0 | } |
2123 | | |
2124 | | /* Open an image. We can't pre-shrink with an image source, sadly. |
2125 | | */ |
2126 | | static VipsImage * |
2127 | | vips_thumbnail_image_open(VipsThumbnail *thumbnail, double factor) |
2128 | 0 | { |
2129 | 0 | VipsThumbnailImage *image = (VipsThumbnailImage *) thumbnail; |
2130 | |
|
2131 | 0 | g_object_ref(image->in); |
2132 | |
|
2133 | 0 | return image->in; |
2134 | 0 | } |
2135 | | |
2136 | | static void |
2137 | | vips_thumbnail_image_class_init(VipsThumbnailClass *class) |
2138 | 1 | { |
2139 | 1 | GObjectClass *gobject_class = G_OBJECT_CLASS(class); |
2140 | 1 | VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS(class); |
2141 | 1 | VipsOperationClass *operation_class = VIPS_OPERATION_CLASS(class); |
2142 | 1 | VipsThumbnailClass *thumbnail_class = VIPS_THUMBNAIL_CLASS(class); |
2143 | | |
2144 | 1 | gobject_class->set_property = vips_object_set_property; |
2145 | 1 | gobject_class->get_property = vips_object_get_property; |
2146 | | |
2147 | 1 | vobject_class->nickname = "thumbnail_image"; |
2148 | 1 | vobject_class->description = _("generate thumbnail from image"); |
2149 | | |
2150 | 1 | operation_class->flags = VIPS_OPERATION_SEQUENTIAL; |
2151 | | |
2152 | 1 | thumbnail_class->get_info = vips_thumbnail_image_get_info; |
2153 | 1 | thumbnail_class->open = vips_thumbnail_image_open; |
2154 | | |
2155 | 1 | VIPS_ARG_IMAGE(class, "in", 1, |
2156 | 1 | _("Input"), |
2157 | 1 | _("Input image argument"), |
2158 | 1 | VIPS_ARGUMENT_REQUIRED_INPUT, |
2159 | 1 | G_STRUCT_OFFSET(VipsThumbnailImage, in)); |
2160 | 1 | } |
2161 | | |
2162 | | static void |
2163 | | vips_thumbnail_image_init(VipsThumbnailImage *image) |
2164 | 0 | { |
2165 | 0 | } |
2166 | | |
2167 | | /** |
2168 | | * vips_thumbnail_image: (method) |
2169 | | * @in: input image |
2170 | | * @out: (out): output image |
2171 | | * @width: target width in pixels |
2172 | | * @...: `NULL`-terminated list of optional named arguments |
2173 | | * |
2174 | | * Exactly as [ctor@Image.thumbnail], but read from an existing image. |
2175 | | * |
2176 | | * This operation |
2177 | | * is not able to exploit shrink-on-load features of image load libraries, so |
2178 | | * it can be much slower than [ctor@Image.thumbnail] and produce poorer quality |
2179 | | * output. Only use this operation if you really have to. |
2180 | | * |
2181 | | * ::: tip "Optional arguments" |
2182 | | * * @height: `gint`, target height in pixels |
2183 | | * * @size: [enum@Size], upsize, downsize, both or force |
2184 | | * * @no_rotate: `gboolean`, don't rotate upright using orientation tag |
2185 | | * * @crop: [enum@Interesting], shrink and crop to fill target |
2186 | | * * @interesting_x: `gint`, horizontal position of the specific point of |
2187 | | * interest when cropping with [enum@Vips.Interesting.SPECIFIC]) |
2188 | | * * @interesting_y: `gint`, vertical position of the specific point of |
2189 | | * interest when cropping with [enum@Vips.Interesting.SPECIFIC]) |
2190 | | * * @linear: `gboolean`, perform shrink in linear light |
2191 | | * * @input_profile: `gchararray`, fallback input ICC profile |
2192 | | * * @output_profile: `gchararray`, output ICC profile |
2193 | | * * @intent: [enum@Intent], rendering intent |
2194 | | * * @fail_on: [enum@FailOn], load error types to fail on |
2195 | | * |
2196 | | * ::: seealso |
2197 | | * [ctor@Image.thumbnail]. |
2198 | | * |
2199 | | * Returns: 0 on success, -1 on error. |
2200 | | */ |
2201 | | int |
2202 | | vips_thumbnail_image(VipsImage *in, VipsImage **out, int width, ...) |
2203 | 0 | { |
2204 | 0 | va_list ap; |
2205 | 0 | int result; |
2206 | |
|
2207 | 0 | va_start(ap, width); |
2208 | 0 | result = vips_call_split("thumbnail_image", ap, in, out, width); |
2209 | 0 | va_end(ap); |
2210 | |
|
2211 | 0 | return result; |
2212 | 0 | } |