Blender V5.3
thumbs.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cstdlib>
11
12#include "MEM_guardedalloc.h"
13
14#include "BKE_blendfile.hh"
15
16#include "BLI_fileops.hh"
17#include "BLI_ghash.hh"
18#include "BLI_hash_md5.hh"
19#include "BLI_path_utils.hh"
20#include "BLI_string.hh"
21#include "BLI_string_utf8.hh"
22#include "BLI_string_utils.hh"
23#include "BLI_system.hh"
24#include "BLI_tempfile.hh"
25#include "BLI_threads.hh"
26#include "BLI_utildefines.hh"
27#include BLI_SYSTEM_PID_H
28
29#include "DNA_space_types.h" /* For FILE_MAX_LIBEXTRA */
30
31#include "IMB_imbuf.hh"
32#include "IMB_imbuf_types.hh"
33#include "IMB_metadata.hh"
34#include "IMB_thumbs.hh"
35
36#include "MOV_read.hh"
37
38#include <cctype>
39#include <cstring>
40#include <ctime>
41#include <sys/stat.h>
42#include <sys/types.h>
43
44#ifdef WIN32
45/* Need to include windows.h so _WIN32_IE is defined. */
46# include <windows.h>
47# ifndef _WIN32_IE
48/* Minimal requirements for SHGetSpecialFolderPath on MINGW MSVC has this defined already. */
49# define _WIN32_IE 0x0400
50# endif
51/* For SHGetSpecialFolderPath, has to be done before BLI_winstuff
52 * because 'near' is disabled through BLI_windstuff */
53# include "BLI_winstuff.hh"
54# include "utfconv.hh"
55# include <direct.h> /* #chdir */
56# include <shlobj.h>
57#endif
58
59#if defined(WIN32) || defined(__APPLE__)
60/* pass */
61#else
62# define USE_FREEDESKTOP
63#endif
64
65/* '$HOME/.cache/thumbnails' or '$HOME/.thumbnails' */
66#ifdef USE_FREEDESKTOP
67# define THUMBNAILS "thumbnails"
68#else
69# define THUMBNAILS ".thumbnails"
70#endif
71
72#define URI_MAX (FILE_MAX * 3 + 8)
73
74namespace blender {
75
76static bool get_thumb_dir(char *dir, ThumbSize size)
77{
78 char *s = dir;
79 const char *subdir;
80#ifdef WIN32
81 wchar_t dir_16[MAX_PATH];
82 /* Yes, applications shouldn't store data there, but so does GIMP :). */
83 SHGetSpecialFolderPathW(0, dir_16, CSIDL_PROFILE, 0);
84 conv_utf_16_to_8(dir_16, dir, FILE_MAX);
85 s += strlen(dir);
86#else
87# if defined(USE_FREEDESKTOP)
88 const char *home_cache = BLI_getenv("XDG_CACHE_HOME");
89 const char *home = home_cache ? home_cache : BLI_dir_home();
90# else
91 const char *home = BLI_dir_home();
92# endif
93 if (!home) {
94 return false;
95 }
96 s += BLI_strncpy_rlen(s, home, FILE_MAX);
97
98# ifdef USE_FREEDESKTOP
99 if (!home_cache) {
100 s += BLI_strncpy_rlen(s, "/.cache", FILE_MAX - (s - dir));
101 }
102# endif
103#endif
104 switch (size) {
105 case THB_NORMAL:
106 subdir = SEP_STR THUMBNAILS SEP_STR "normal" SEP_STR;
107 break;
108 case THB_LARGE:
109 subdir = SEP_STR THUMBNAILS SEP_STR "large" SEP_STR;
110 break;
111 case THB_FAIL:
112 subdir = SEP_STR THUMBNAILS SEP_STR "fail" SEP_STR "blender" SEP_STR;
113 break;
114 default:
115 return false; /* unknown size */
116 }
117
118 s += BLI_strncpy_rlen(s, subdir, FILE_MAX - (s - dir));
119 (void)s;
120
121 return true;
122}
123
124#undef THUMBNAILS
125
126/* --- Begin of adapted code from glib. --- */
127
128/* -------------------------------------------------------------------- */
136
138 UNSAFE_ALL = 0x1, /* Escape all unsafe characters. */
139 UNSAFE_ALLOW_PLUS = 0x2, /* Allows '+' */
140 UNSAFE_PATH = 0x8, /* Allows '/', '&', '=', ':', '@', '+', '$' and ',' */
141 UNSAFE_HOST = 0x10, /* Allows '/' and ':' and '@' */
142 UNSAFE_SLASHES = 0x20, /* Allows all characters except for '/' and '%' */
143};
144
145/* Don't lose comment alignment. */
146/* clang-format off */
147static const uchar acceptable[96] = {
148 /* A table of the ASCII chars from space (32) to DEL (127) */
149 /* ! " # $ % & ' ( ) * + , - . / */
150 0x00,0x3F,0x20,0x20,0x28,0x00,0x2C,0x3F,0x3F,0x3F,0x3F,0x2A,0x28,0x3F,0x3F,0x1C,
151 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
152 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x38,0x20,0x20,0x2C,0x20,0x20,
153 /* @ A B C D E F G H I J K L M N O */
154 0x38,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
155 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
156 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x20,0x3F,
157 /* ` a b c d e f g h i j k l m n o */
158 0x20,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
159 /* p q r s t u v w x y z { | } ~ DEL */
160 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x3F,0x20,
161};
162/* clang-format on */
163
164static const char hex[17] = "0123456789abcdef";
165
166/* NOTE: This escape function works on file: URIs, but if you want to
167 * escape something else, please read RFC-2396 */
168static void escape_uri_string(const char *string,
169 char *escaped_string,
170 const int escaped_string_size,
172{
173#define ACCEPTABLE(a) ((a) >= 32 && (a) < 128 && (acceptable[(a) - 32] & mask))
174
175 BLI_assert(escaped_string_size > 0);
176 /* Remove space for \0. */
177 int escaped_string_len = escaped_string_size - 1;
178
179 const char *p;
180 char *q;
181 int c;
182
183 for (q = escaped_string, p = string; (*p != '\0') && escaped_string_len; p++) {
184 c = uchar(*p);
185
186 if (!ACCEPTABLE(c)) {
187 if (escaped_string_len < 3) {
188 break;
189 }
190
191 *q++ = '%'; /* means hex coming */
192 *q++ = hex[c >> 4];
193 *q++ = hex[c & 15];
194 escaped_string_len -= 3;
195 }
196 else {
197 *q++ = *p;
198 escaped_string_len -= 1;
199 }
200 }
201
202 *q = '\0';
203}
204
206
207/* --- End of adapted code from glib. --- */
208
209static bool thumbhash_from_path(const char * /*path*/, ThumbSource source, char *r_hash)
210{
211 switch (source) {
212 case THB_SOURCE_FONT:
213 return IMB_thumb_load_font_get_hash(r_hash);
214 default:
215 r_hash[0] = '\0';
216 return false;
217 }
218}
219
220static bool uri_from_filepath(const char *path, char *uri)
221{
222 char orig_uri[URI_MAX];
223
224#ifdef WIN32
225 bool path_is_unc = BLI_path_is_unc(path);
226 char path_unc_normalized[FILE_MAX];
227 if (path_is_unc) {
228 STRNCPY(path_unc_normalized, path);
229 BLI_path_normalize_unc(path_unc_normalized, sizeof(path_unc_normalized));
230 path = path_unc_normalized;
231 /* Assign again because a normalized UNC path may resolve to a drive letter. */
232 path_is_unc = BLI_path_is_unc(path);
233 }
234
235 if (path_is_unc) {
236 /* Skip over the `\\` prefix, it's not needed for a URI. */
237 SNPRINTF(orig_uri, "file://%s", BLI_path_slash_skip(path));
238 }
239 else if (BLI_path_is_win32_drive(path)) {
240 SNPRINTF(orig_uri, "file:///%s", path);
241 /* Always use an uppercase drive/volume letter in the URI. */
242 orig_uri[8] = char(toupper(orig_uri[8]));
243 }
244 else {
245 /* Not a correct absolute path with a drive letter or UNC prefix. */
246 return false;
247 }
248 BLI_string_replace_char(orig_uri, '\\', '/');
249#else
250 SNPRINTF(orig_uri, "file://%s", path);
251#endif
252
253 escape_uri_string(orig_uri, uri, URI_MAX, UNSAFE_PATH);
254
255 return true;
256}
257
258static bool thumbpathname_from_uri(const char *uri,
259 char *r_path,
260 const int path_maxncpy,
261 char *r_name,
262 int name_maxncpy,
263 ThumbSize size)
264{
265 char name_buff[40];
266
267 if (r_path && !r_name) {
268 r_name = name_buff;
269 name_maxncpy = sizeof(name_buff);
270 }
271
272 if (r_name) {
273 char hexdigest[33];
274 uchar digest[16];
275 BLI_hash_md5_buffer(uri, strlen(uri), digest);
276 hexdigest[0] = '\0';
277 BLI_snprintf(r_name, name_maxncpy, "%s.png", BLI_hash_md5_to_hexdigest(digest, hexdigest));
278 // printf("%s: '%s' --> '%s'\n", __func__, uri, r_name);
279 }
280
281 if (r_path) {
282 char tmppath[FILE_MAX];
283
284 if (get_thumb_dir(tmppath, size)) {
285 BLI_snprintf(r_path, path_maxncpy, "%s%s", tmppath, r_name);
286 // printf("%s: '%s' --> '%s'\n", __func__, uri, r_path);
287 return true;
288 }
289 }
290 return false;
291}
292
293static void thumbname_from_uri(const char *uri, char *thumb, const int thumb_maxncpy)
294{
295 thumbpathname_from_uri(uri, nullptr, 0, thumb, thumb_maxncpy, THB_FAIL);
296}
297
298static bool thumbpath_from_uri(const char *uri, char *path, const int path_maxncpy, ThumbSize size)
299{
300 return thumbpathname_from_uri(uri, path, path_maxncpy, nullptr, 0, size);
301}
302
304{
305 char tpath[FILE_MAX];
306#if 0 /* UNUSED */
307 if (get_thumb_dir(tpath, THB_NORMAL)) {
309 }
310#endif
311 if (get_thumb_dir(tpath, THB_LARGE)) {
313 }
314 if (get_thumb_dir(tpath, THB_FAIL)) {
316 }
317}
318
319static bool thumb_cancel_requested(const ThumbCancellationToken *cancel_token)
320{
321 return cancel_token && cancel_token->is_cancelled();
322}
323
324/* create thumbnail for file and returns new imbuf for thumbnail */
325static ImBuf *thumb_create_ex(const char *file_path,
326 const char *uri,
327 const char *thumb,
328 const bool use_hash,
329 const char *hash,
330 const char *blen_group,
331 const char *blen_id,
332 ThumbSize size,
333 ThumbSource source,
334 ImBuf *img,
335 const ThumbCancellationToken *cancel_token = nullptr)
336{
337 if (thumb_cancel_requested(cancel_token)) {
338 return nullptr;
339 }
340
341 /* Just in case these folders got deleted somehow. */
343
344 char desc[URI_MAX + 22];
345 char tpath[FILE_MAX];
346 char tdir[FILE_MAX];
347 char temp[FILE_MAX];
348 char mtime[40] = "0"; /* in case we can't stat the file */
349 short tsize = 128;
350 BLI_stat_t info;
351
352 switch (size) {
353 case THB_NORMAL:
355 break;
356 case THB_LARGE:
358 break;
359 case THB_FAIL:
360 tsize = 1;
361 break;
362 default:
363 return nullptr; /* unknown size */
364 }
365
366 if (get_thumb_dir(tdir, size)) {
367 SNPRINTF(tpath, "%s%s", tdir, thumb);
368 // thumb[8] = '\0'; /* shorten for `temp` name, not needed anymore */
369 SNPRINTF(temp, "%sblender_%d_%s.png", tdir, abs(getpid()), thumb);
370 if (BLI_path_ncmp(file_path, tdir, sizeof(tdir)) == 0) {
371 return nullptr;
372 }
373 if (size == THB_FAIL) {
375 if (!img) {
376 return nullptr;
377 }
378 }
379 else {
381 {
382 /* only load if we didn't give an image */
383 if (img == nullptr) {
384 switch (source) {
385 case THB_SOURCE_IMAGE:
386 img = IMB_thumb_load_image(file_path, tsize, nullptr);
387 break;
388 case THB_SOURCE_BLEND:
389 img = IMB_thumb_load_blend(file_path, blen_group, blen_id);
390 break;
391 case THB_SOURCE_FONT:
392 img = IMB_thumb_load_font(file_path, tsize, tsize);
393 break;
395 if (BLI_path_extension_check(file_path, ".svg")) {
396 img = IMB_thumb_load_image(file_path, tsize, nullptr);
397 }
398 break;
399 }
400 default:
401 BLI_assert_unreachable(); /* This should never happen */
402 }
403 }
404
405 if (img != nullptr) {
406 if (BLI_stat(file_path, &info) != -1) {
407 SNPRINTF_UTF8(mtime, "%ld", (long int)info.st_mtime);
408 }
409 }
410 }
411 else if (THB_SOURCE_MOVIE == source) {
412 MovieReader *anim = nullptr;
413 /* Image buffer is converted from float to byte and only the latter one is used, and the
414 * conversion process is aware of the float color-space. So it is possible to save some
415 * compute time by keeping the original color-space for movies. */
416 anim = MOV_open_file(file_path, ImBufFlags::Zero, 0, true, nullptr);
417 if (anim != nullptr) {
418 img = MOV_decode_frame(anim, 0, IMB_PROXY_NONE);
419 if (img == nullptr) {
420 // printf("not an anim; %s\n", file_path);
421 }
422 else {
423 IMB_freeImBuf(img);
424 img = MOV_decode_preview_frame(anim);
425 }
426 MOV_close(anim);
427 }
428 if (BLI_stat(file_path, &info) != -1) {
429 SNPRINTF_UTF8(mtime, "%ld", (long int)info.st_mtime);
430 }
431 }
432 if (!img) {
433 return nullptr;
434 }
435
436 if (thumb_cancel_requested(cancel_token)) {
437 IMB_freeImBuf(img);
438 return nullptr;
439 }
440
441 if (img->x > tsize || img->y > tsize) {
442 float scale = std::min(float(tsize) / float(img->x), float(tsize) / float(img->y));
443 /* Scaling down must never assign zero width/height, see: #89868. */
444 short ex = std::max(short(1), short(img->x * scale));
445 short ey = std::max(short(1), short(img->y * scale));
446 /* Save some time by only scaling byte buffer. */
447 if (img->float_data()) {
448 if (img->byte_data() == nullptr) {
450 }
452 }
453 IMB_scale(img, ex, ey, IMBScaleFilter::Box, false);
454 }
455 }
456
457 if (thumb_cancel_requested(cancel_token)) {
458 IMB_freeImBuf(img);
459 return nullptr;
460 }
461
462 SNPRINTF_UTF8(desc, "Thumbnail for %s", uri);
464 IMB_metadata_set_field(metadata, "Software", "Blender");
465 IMB_metadata_set_field(metadata, "Thumb::URI", uri);
466 IMB_metadata_set_field(metadata, "Description", desc);
467 IMB_metadata_set_field(metadata, "Thumb::MTime", mtime);
468 if (use_hash) {
469 IMB_metadata_set_field(metadata, "X-Blender::Hash", hash);
470 }
471 img->ftype = IMB_FTYPE_PNG;
473
474 /* If we generated from a 16bit PNG e.g., we have a float rect, not a byte one - fix this. */
477
479#ifndef WIN32
480 chmod(temp, S_IRUSR | S_IWUSR);
481#endif
482 // printf("%s saving thumb: '%s'\n", __func__, tpath);
483
484 BLI_rename_overwrite(temp, tpath);
485 }
486 }
487 return img;
488}
489
490static ImBuf *thumb_create_or_fail(const char *file_path,
491 const char *uri,
492 const char *thumb,
493 const bool use_hash,
494 const char *hash,
495 const char *blen_group,
496 const char *blen_id,
497 ThumbSize size,
498 ThumbSource source,
499 const ThumbCancellationToken *cancel_token)
500{
501 ImBuf *img = thumb_create_ex(file_path,
502 uri,
503 thumb,
504 use_hash,
505 hash,
506 blen_group,
507 blen_id,
508 size,
509 source,
510 nullptr,
511 cancel_token);
512
513 if (!img && !thumb_cancel_requested(cancel_token)) {
514 /* thumb creation failed, write fail thumb */
515 img = thumb_create_ex(
516 file_path, uri, thumb, use_hash, hash, blen_group, blen_id, THB_FAIL, source, nullptr);
517 if (img) {
518 /* we don't need failed thumb anymore */
519 IMB_freeImBuf(img);
520 img = nullptr;
521 }
522 }
523
524 return img;
525}
526
531static bool skip_thumbnails_for_filepath(const char *filepath)
532{
533 if (!BLI_path_extension_check(filepath, ".blend")) {
534 return false;
535 }
536 char temp_dir[FILE_MAX];
537 BLI_temp_directory_path_get(temp_dir, sizeof(temp_dir));
538 return BLI_path_contains(temp_dir, filepath);
539}
540
541ImBuf *IMB_thumb_create(const char *filepath, ThumbSize size, ThumbSource source, ImBuf *img)
542{
543 if (source == THB_SOURCE_DIRECT) {
544 /* Not yet implemented (not needed currently). Could just directly write the image to the given
545 * `filepath`. */
547 "Writing thumbnails with direct source isn't implemented");
548 return nullptr;
549 }
550
551 if (skip_thumbnails_for_filepath(filepath)) {
552 return nullptr;
553 }
554
555 char uri[URI_MAX] = "";
556 char thumb_name[40];
557
558 if (!uri_from_filepath(filepath, uri)) {
559 return nullptr;
560 }
561 thumbname_from_uri(uri, thumb_name, sizeof(thumb_name));
562
563 return thumb_create_ex(
564 filepath, uri, thumb_name, false, THUMB_DEFAULT_HASH, nullptr, nullptr, size, source, img);
565}
566
567ImBuf *IMB_thumb_read(const char *file_or_lib_path, ThumbSize size)
568{
569 char thumb[FILE_MAX];
570 char uri[URI_MAX];
571 ImBuf *img = nullptr;
572
573 if (!uri_from_filepath(file_or_lib_path, uri)) {
574 return nullptr;
575 }
576 if (thumbpath_from_uri(uri, thumb, sizeof(thumb), size)) {
578 }
579
580 return img;
581}
582
583void IMB_thumb_delete(const char *file_or_lib_path, ThumbSize size)
584{
585 char thumb[FILE_MAX];
586 char uri[URI_MAX];
587
588 if (!uri_from_filepath(file_or_lib_path, uri)) {
589 return;
590 }
591 if (thumbpath_from_uri(uri, thumb, sizeof(thumb), size)) {
592 if (BLI_path_ncmp(file_or_lib_path, thumb, sizeof(thumb)) == 0) {
593 return;
594 }
595 if (BLI_exists(thumb)) {
596 BLI_delete(thumb, false, false);
597 }
598 }
599}
600
601ImBuf *IMB_thumb_manage(const char *file_or_lib_path,
602 ThumbSize size,
603 ThumbSource source,
604 const ThumbCancellationToken *cancel_token)
605{
606 if (thumb_cancel_requested(cancel_token)) {
607 return nullptr;
608 }
609
610 if (source == THB_SOURCE_DIRECT) {
611 const eFileAttributes file_attributes = BLI_file_attributes(file_or_lib_path);
612 /* Don't trigger download files from online drives. Maybe less of a problem for
613 * #THE_SOURCE_DIRECT, since what we request is the actual image itself. For other sources this
614 * may download a bunch of large files like videos or blends, just to extract a thumbnail. But
615 * for now, keep the API consistent and do not trigger download of such files. */
616 if (file_attributes & FILE_ATTR_OFFLINE) {
617 return nullptr;
618 }
619
620 ImBuf *thumb = IMB_load_image_from_filepath(file_or_lib_path,
622 if (!thumb) {
623 return nullptr;
624 }
625
626 IMB_byte_from_float(thumb);
628 return thumb;
629 }
630
631 if (skip_thumbnails_for_filepath(file_or_lib_path)) {
632 return nullptr;
633 }
634
635 char path_buff[FILE_MAX_LIBEXTRA];
636 char *blen_group = nullptr, *blen_id = nullptr;
637
638 /* Will be the actual path to the file, i.e. the same as #file_or_lib_path, or if that points
639 * into a .blend, the path of the .blend. */
640 const char *file_path = file_or_lib_path;
641 if (source == THB_SOURCE_BLEND) {
642 if (BKE_blendfile_library_path_explode(file_or_lib_path, path_buff, &blen_group, &blen_id)) {
643 if (blen_group) {
644 if (!blen_id) {
645 /* No preview for blen groups */
646 return nullptr;
647 }
648 file_path = path_buff; /* path needs to be a valid file! */
649 }
650 }
651 }
652
653 BLI_stat_t st;
654 if (BLI_stat(file_path, &st) == -1) {
655 return nullptr;
656 }
657 char uri[URI_MAX];
658 if (!uri_from_filepath(file_or_lib_path, uri)) {
659 return nullptr;
660 }
661
662 /* Don't access offline files, only use already existing thumbnails (don't recreate). */
663 const eFileAttributes file_attributes = BLI_file_attributes(file_path);
664 if (file_attributes & FILE_ATTR_OFFLINE) {
665 char thumb_path[FILE_MAX];
666 if (thumbpath_from_uri(uri, thumb_path, sizeof(thumb_path), size)) {
668 }
669 return nullptr;
670 }
671
672 char thumb_path[FILE_MAX];
673 if (thumbpath_from_uri(uri, thumb_path, sizeof(thumb_path), THB_FAIL)) {
674 /* failure thumb exists, don't try recreating */
675 if (BLI_exists(thumb_path)) {
676 /* clear out of date fail case (note for blen IDs we use blender file itself here) */
677 if (BLI_file_older(thumb_path, file_path)) {
678 BLI_delete(thumb_path, false, false);
679 }
680 else {
681 return nullptr;
682 }
683 }
684 }
685
686 ImBuf *img = nullptr;
687 char thumb_name[40];
689 uri, thumb_path, sizeof(thumb_path), thumb_name, sizeof(thumb_name), size))
690 {
691 /* The requested path points to a generated thumbnail already (path into the thumbnail cache
692 * directory). Attempt to load that, there's nothing we can recreate. */
693 if (BLI_path_ncmp(file_or_lib_path, thumb_path, sizeof(thumb_path)) == 0) {
694 img = IMB_load_image_from_filepath(file_or_lib_path, ImBufFlags::ByteData);
695 }
696 else {
698 if (img) {
699 bool regenerate = false;
700
701 char mtime[40];
702 char thumb_hash[33];
703 char thumb_hash_curr[33];
704
705 const bool use_hash = thumbhash_from_path(file_path, source, thumb_hash);
706
707 if (IMB_metadata_get_field(img->metadata(), "Thumb::MTime", mtime, sizeof(mtime))) {
708 regenerate = (st.st_mtime != atol(mtime));
709 }
710 else {
711 /* illegal thumb, regenerate it! */
712 regenerate = true;
713 }
714
715 if (use_hash && !regenerate) {
717 img->metadata(), "X-Blender::Hash", thumb_hash_curr, sizeof(thumb_hash_curr)))
718 {
719 regenerate = !STREQ(thumb_hash, thumb_hash_curr);
720 }
721 else {
722 regenerate = true;
723 }
724 }
725
726 if (regenerate) {
727 /* recreate all thumbs */
728 IMB_freeImBuf(img);
729 img = nullptr;
730 IMB_thumb_delete(file_or_lib_path, THB_NORMAL);
731 IMB_thumb_delete(file_or_lib_path, THB_LARGE);
732 IMB_thumb_delete(file_or_lib_path, THB_FAIL);
733 img = thumb_create_or_fail(file_path,
734 uri,
735 thumb_name,
736 use_hash,
737 thumb_hash,
738 blen_group,
739 blen_id,
740 size,
741 source,
742 cancel_token);
743 }
744 }
745 else {
746 char thumb_hash[33];
747 const bool use_hash = thumbhash_from_path(file_path, source, thumb_hash);
748
749 img = thumb_create_or_fail(file_path,
750 uri,
751 thumb_name,
752 use_hash,
753 thumb_hash,
754 blen_group,
755 blen_id,
756 size,
757 source,
758 cancel_token);
759 }
760 }
761 }
762
763 /* Our imbuf **must** have a valid rect (i.e. 8-bits/channels)
764 * data, we rely on this in draw code.
765 * However, in some cases we may end loading 16bits PNGs, which generated float buffers.
766 * This should be taken care of in generation step, but add also a safeguard here! */
767 if (img) {
770 }
771
772 return img;
773}
774
775/* ***** Threading ***** */
776/* Thumbnail handling is not really threadsafe in itself.
777 * However, as long as we do not operate on the same file, we shall have no collision.
778 * So idea is to 'lock' a given source file path.
779 */
780
786
788{
789 static IMBThumbLocks thumb_locks{};
790 return thumb_locks;
791}
792
794{
796
797 IMBThumbLocks &thumb_locks = get_thumb_locks();
798 if (thumb_locks.lock_counter == 0) {
799 BLI_condition_init(&thumb_locks.cond);
800 }
801 thumb_locks.lock_counter++;
802
803 BLI_assert(thumb_locks.lock_counter > 0);
805}
806
808{
809 IMBThumbLocks &thumb_locks = get_thumb_locks();
811 BLI_assert(thumb_locks.lock_counter > 0);
812
813 thumb_locks.lock_counter--;
814 if (thumb_locks.lock_counter == 0) {
815 thumb_locks.locked_paths.clear();
816 BLI_condition_end(&thumb_locks.cond);
817 }
818
820}
821
822void IMB_thumb_path_lock(const char *path)
823{
824 IMBThumbLocks &thumb_locks = get_thumb_locks();
825
827 BLI_assert(thumb_locks.lock_counter > 0);
828
829 while (!thumb_locks.locked_paths.add(path)) {
831 }
832
834}
835
836void IMB_thumb_path_unlock(const char *path)
837{
838 IMBThumbLocks &thumb_locks = get_thumb_locks();
839
841 BLI_assert(thumb_locks.lock_counter > 0);
842
843 if (!thumb_locks.locked_paths.remove(path)) {
845 }
846 BLI_condition_notify_all(&thumb_locks.cond);
847
849}
850
851} // namespace blender
File and directory operations.
#define BLI_path_ncmp
#define FILE_MAX
#define SNPRINTF(dst, format,...)
#define SNPRINTF_UTF8(dst, format,...)
Compatibility-like things for windows.
#define FILE_MAX_LIBEXTRA
#define PREVIEW_RENDER_DEFAULT_HEIGHT
Definition IMB_thumbs.hh:76
#define PREVIEW_RENDER_LARGE_HEIGHT
Definition IMB_thumbs.hh:77
#define THUMB_DEFAULT_HASH
Definition IMB_thumbs.hh:84
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
bool add(const Key &key)
Definition BLI_set.hh:248
void clear()
Definition BLI_set.hh:570
bool remove(const Key &key)
Definition BLI_set.hh:404
#define abs
float3x3 scale(float3x3 mat, float2 scale)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool BLI_path_is_win32_drive(const char *path)
size_t BLI_snprintf(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
char * STRNCPY(char(&dst)[N], const char *src)
void IMB_thumb_locks_release()
Definition thumbs.cc:807
void * BLI_hash_md5_buffer(const char *buffer, size_t len, void *resblock)
Definition hash_md5.cc:345
static bool thumbpath_from_uri(const char *uri, char *path, const int path_maxncpy, ThumbSize size)
Definition thumbs.cc:298
void BLI_condition_init(ThreadCondition *cond)
Definition threads.cc:578
void IMB_thumb_path_unlock(const char *path)
Definition thumbs.cc:836
static ImBuf * thumb_create_ex(const char *file_path, const char *uri, const char *thumb, const bool use_hash, const char *hash, const char *blen_group, const char *blen_id, ThumbSize size, ThumbSource source, ImBuf *img, const ThumbCancellationToken *cancel_token=nullptr)
Definition thumbs.cc:325
bool BKE_blendfile_library_path_explode(const char *path, char *r_dir, char **r_group, char **r_name)
Definition blendfile.cc:94
ImBuf * IMB_thumb_create(const char *filepath, ThumbSize size, ThumbSource source, ImBuf *img)
Definition thumbs.cc:541
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
Definition fileops_c.cc:416
void MOV_close(MovieReader *anim)
Definition movie_read.cc:70
eUnsafeCharacterSet
Definition thumbs.cc:137
@ UNSAFE_ALL
Definition thumbs.cc:138
@ UNSAFE_HOST
Definition thumbs.cc:141
@ UNSAFE_ALLOW_PLUS
Definition thumbs.cc:139
@ UNSAFE_PATH
Definition thumbs.cc:140
@ UNSAFE_SLASHES
Definition thumbs.cc:142
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
void BLI_string_replace_char(char *str, char src, char dst) ATTR_NONNULL(1)
void IMB_thumb_locks_acquire()
Definition thumbs.cc:793
ImBuf * IMB_thumb_load_blend(const char *blen_path, const char *blen_group, const char *blen_id)
ImBuf * IMB_thumb_read(const char *file_or_lib_path, ThumbSize size)
Definition thumbs.cc:567
ImBuf * MOV_decode_frame(MovieReader *anim, int frame_index, IMB_Proxy_Size preview_size)
static bool thumbpathname_from_uri(const char *uri, char *r_path, const int path_maxncpy, char *r_name, int name_maxncpy, ThumbSize size)
Definition thumbs.cc:258
void IMB_thumb_delete(const char *file_or_lib_path, ThumbSize size)
Definition thumbs.cc:583
eFileAttributes BLI_file_attributes(const char *path)
Definition storage.cc:239
void IMB_thumb_path_lock(const char *path)
Definition thumbs.cc:822
void BLI_condition_end(ThreadCondition *cond)
Definition threads.cc:603
const char * BLI_dir_home()
Definition storage.cc:99
@ FILE_ATTR_OFFLINE
bool IMB_save_image(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
Definition writeimage.cc:40
ImBuf * IMB_load_image_from_filepath(const char *filepath, ImBufFlags flags, ColorManagedColorspaceSettings *colorspace_settings=nullptr)
Definition readimage.cc:201
bool IMB_thumb_load_font_get_hash(char *r_hash)
MovieReader * MOV_open_file(const char *filepath, const ImBufFlags ib_flags, const int streamindex, const bool keep_original_colorspace, ColorManagedColorspaceSettings *colorspace_settings)
void IMB_freeImBuf(ImBuf *ibuf)
bool BLI_path_is_unc(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
unsigned char uchar
Definition BKE_text.h:58
int BLI_stat(const char *path, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool void BLI_temp_directory_path_get(char *tempdir, const size_t tempdir_maxncpy) ATTR_NONNULL(1)
Definition tempfile.cc:59
static const char hex[17]
Definition thumbs.cc:164
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
char char size_t BLI_strncpy_rlen(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
void BLI_condition_wait_global_mutex(ThreadCondition *cond, int type)
Definition threads.cc:588
static bool get_thumb_dir(char *dir, ThumbSize size)
Definition thumbs.cc:76
void IMB_thumb_makedirs()
Definition thumbs.cc:303
int BLI_rename_overwrite(const char *from, const char *to) ATTR_NONNULL()
Definition fileops_c.cc:541
static bool skip_thumbnails_for_filepath(const char *filepath)
Definition thumbs.cc:531
static bool thumb_cancel_requested(const ThumbCancellationToken *cancel_token)
Definition thumbs.cc:319
static const uchar acceptable[96]
Definition thumbs.cc:147
static IMBThumbLocks & get_thumb_locks()
Definition thumbs.cc:787
void BLI_condition_notify_all(ThreadCondition *cond)
Definition threads.cc:598
ImBuf * IMB_thumb_load_font(const char *filepath, unsigned int x, unsigned int y)
@ THB_SOURCE_IMAGE
Definition IMB_thumbs.hh:33
@ THB_SOURCE_DIRECT
Definition IMB_thumbs.hh:47
@ THB_SOURCE_MOVIE
Definition IMB_thumbs.hh:34
@ THB_SOURCE_FONT
Definition IMB_thumbs.hh:36
@ THB_SOURCE_BLEND
Definition IMB_thumbs.hh:35
@ THB_SOURCE_OBJECT_IO
Definition IMB_thumbs.hh:37
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
static ImBuf * thumb_create_or_fail(const char *file_path, const char *uri, const char *thumb, const bool use_hash, const char *hash, const char *blen_group, const char *blen_id, ThumbSize size, ThumbSource source, const ThumbCancellationToken *cancel_token)
Definition thumbs.cc:490
ImBuf * IMB_thumb_manage(const char *file_or_lib_path, ThumbSize size, ThumbSource source, const ThumbCancellationToken *cancel_token=nullptr)
Definition thumbs.cc:601
char * BLI_hash_md5_to_hexdigest(const void *resblock, char r_hex_digest[33])
Definition hash_md5.cc:381
void IMB_byte_from_float(ImBuf *ibuf)
bool BLI_path_extension_check(const char *path, const char *ext) ATTR_NONNULL(1
bool BLI_path_contains(const char *container_path, const char *containee_path) ATTR_NONNULL(1
pthread_cond_t ThreadCondition
bool BLI_file_older(const char *file1, const char *file2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:642
bool IMB_metadata_get_field(const IDProperty *metadata, const char *key, char *value, size_t value_maxncpy)
ImBuf * IMB_thumb_load_image(const char *filepath, size_t max_thumb_size, ColorManagedColorspaceSettings *colorspace_settings, IMBThumbLoadFlags load_flags=IMBThumbLoadFlags::Zero)
Definition readimage.cc:226
static void escape_uri_string(const char *string, char *escaped_string, const int escaped_string_size, const eUnsafeCharacterSet mask)
Definition thumbs.cc:168
struct stat BLI_stat_t
ImBuf * MOV_decode_preview_frame(MovieReader *anim)
bool IMB_scale(ImBuf *ibuf, int2 new_size, IMBScaleFilter filter, bool threaded=true)
Definition scaling.cc:467
static void thumbname_from_uri(const char *uri, char *thumb, const int thumb_maxncpy)
Definition thumbs.cc:293
void BLI_thread_unlock(int type)
Definition threads.cc:336
static bool thumbhash_from_path(const char *, ThumbSource source, char *r_hash)
Definition thumbs.cc:209
void IMB_free_float_pixels(ImBuf *ibuf)
static bool uri_from_filepath(const char *path, char *uri)
Definition thumbs.cc:220
void IMB_metadata_set_field(IDProperty *metadata, const char *key, const char *value)
const char * BLI_getenv(const char *env) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void BLI_thread_lock(int type)
Definition threads.cc:331
const char * BLI_path_slash_skip(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
#define hash
Definition noise_c.cc:156
Set< std::string > locked_paths
Definition thumbs.cc:782
ThreadCondition cond
Definition thumbs.cc:784
const uint8_t * byte_data() const
IDProperty * metadata_for_write()
const IDProperty * metadata() const
const float * float_data() const
ImColorMode color_mode
eImbFileType ftype
#define URI_MAX
Definition thumbs.cc:72
#define THUMBNAILS
Definition thumbs.cc:67
#define ACCEPTABLE(a)
#define SEP_STR
Definition unit.cc:41
int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8)
Definition utfconv.cc:116