Blender V5.3
thumbs_blend.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 <cstdlib>
10#include <cstring>
11
12#include "BLO_readfile.hh"
13
14#include "BKE_idtype.hh"
15#include "BKE_main.hh"
16#include "BKE_preview_image.hh"
17
18#include "IMB_imbuf_types.hh"
19#include "IMB_thumbs.hh"
20
21#include "MEM_guardedalloc.h"
22
23namespace blender {
24
25/* NOTE: we should handle all previews for a same group at once, would avoid reopening
26 * `.blend` file for each and every ID. However, this adds some complexity,
27 * so keep it for later. */
28static ImBuf *imb_thumb_load_from_blend_id(const char *blen_path,
29 const char *blen_group,
30 const char *blen_id)
31{
32 ImBuf *ima = nullptr;
33 BlendFileReadReport bf_reports = {};
34 bf_reports.reports = nullptr;
35
36 BlendHandle *libfiledata = BLO_blendhandle_from_file(blen_path, &bf_reports);
37 if (libfiledata == nullptr) {
38 return nullptr;
39 }
40
41 int idcode = BKE_idtype_idcode_from_name(blen_group);
42 PreviewImage *preview = BLO_blendhandle_get_preview_for_id(libfiledata, idcode, blen_id);
43 BLO_blendhandle_close(libfiledata);
44
45 if (preview) {
47 BKE_previewimg_free(&preview);
48 }
49 return ima;
50}
51
52static ImBuf *imb_thumb_load_from_blendfile(const char *blen_path)
53{
55 ImBuf *ima = BKE_main_thumbnail_to_imbuf(nullptr, data);
56
57 if (data) {
58 MEM_delete(data);
59 }
60 return ima;
61}
62
63ImBuf *IMB_thumb_load_blend(const char *blen_path, const char *blen_group, const char *blen_id)
64{
65 if (blen_group && blen_id) {
66 return imb_thumb_load_from_blend_id(blen_path, blen_group, blen_id);
67 }
68 return imb_thumb_load_from_blendfile(blen_path);
69}
70
71} // namespace blender
external readfile function prototypes.
Read Blender memory allocation and freeing.
BMesh Iterators.
BlendHandle * BLO_blendhandle_from_file(const char *filepath, BlendFileReadReport *reports)
@ ICON_SIZE_PREVIEW
ImBuf * BKE_main_thumbnail_to_imbuf(Main *bmain, BlendThumbnail *data)
Definition main.cc:1029
void BLO_blendhandle_close(BlendHandle *bh) ATTR_NONNULL(1)
BlendThumbnail * BLO_thumbnail_from_file(const char *filepath)
Definition readfile.cc:1458
ImBuf * IMB_thumb_load_blend(const char *blen_path, const char *blen_group, const char *blen_id)
static ImBuf * imb_thumb_load_from_blend_id(const char *blen_path, const char *blen_group, const char *blen_id)
static ImBuf * imb_thumb_load_from_blendfile(const char *blen_path)
BMesh const char void * data
void BKE_previewimg_free(PreviewImage **prv)
ImBuf * BKE_previewimg_to_imbuf(const PreviewImage *prv, int size)
short BKE_idtype_idcode_from_name(const char *idtype_name)
Definition idtype.cc:282
PreviewImage * BLO_blendhandle_get_preview_for_id(BlendHandle *bh, int ofblocktype, const char *name)