Blender V4.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
9#include <cstdio>
10#include <cstdlib>
11#include <cstring>
12
13#include "BLI_listbase.h" /* Needed due to import of BLO_readfile.hh */
14#include "BLI_utildefines.h"
15
16#include "BLO_readfile.hh"
17
18#include "BKE_idtype.hh"
19#include "BKE_main.hh"
20#include "BKE_preview_image.hh"
21
22#include "IMB_imbuf_types.hh"
23#include "IMB_thumbs.hh"
24
25#include "MEM_guardedalloc.h"
26
27/* NOTE: we should handle all previews for a same group at once, would avoid reopening
28 * `.blend` file for each and every ID. However, this adds some complexity,
29 * so keep it for later. */
30static ImBuf *imb_thumb_load_from_blend_id(const char *blen_path,
31 const char *blen_group,
32 const char *blen_id)
33{
34 ImBuf *ima = nullptr;
35 BlendFileReadReport bf_reports = {};
36 bf_reports.reports = nullptr;
37
38 BlendHandle *libfiledata = BLO_blendhandle_from_file(blen_path, &bf_reports);
39 if (libfiledata == nullptr) {
40 return nullptr;
41 }
42
43 int idcode = BKE_idtype_idcode_from_name(blen_group);
44 PreviewImage *preview = BLO_blendhandle_get_preview_for_id(libfiledata, idcode, blen_id);
45 BLO_blendhandle_close(libfiledata);
46
47 if (preview) {
50 }
51 return ima;
52}
53
54static ImBuf *imb_thumb_load_from_blendfile(const char *blen_path)
55{
56 BlendThumbnail *data = BLO_thumbnail_from_file(blen_path);
57 ImBuf *ima = BKE_main_thumbnail_to_imbuf(nullptr, data);
58
59 if (data) {
60 MEM_freeN(data);
61 }
62 return ima;
63}
64
65ImBuf *IMB_thumb_load_blend(const char *blen_path, const char *blen_group, const char *blen_id)
66{
67 if (blen_group && blen_id) {
68 return imb_thumb_load_from_blend_id(blen_path, blen_group, blen_id);
69 }
70 return imb_thumb_load_from_blendfile(blen_path);
71}
short BKE_idtype_idcode_from_name(const char *idtype_name)
Definition idtype.cc:189
ImBuf * BKE_main_thumbnail_to_imbuf(Main *bmain, BlendThumbnail *data)
Definition main.cc:806
void BKE_previewimg_freefunc(void *link)
ImBuf * BKE_previewimg_to_imbuf(PreviewImage *prv, int size)
external readfile function prototypes.
BlendThumbnail * BLO_thumbnail_from_file(const char *filepath)
Definition readfile.cc:1422
PreviewImage * BLO_blendhandle_get_preview_for_id(BlendHandle *bh, int ofblocktype, const char *name)
BlendHandle * BLO_blendhandle_from_file(const char *filepath, BlendFileReadReport *reports)
void BLO_blendhandle_close(BlendHandle *bh) ATTR_NONNULL(1)
@ ICON_SIZE_PREVIEW
Contains defines and structs used throughout the imbuf module.
Read Guarded memory(de)allocation.
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
static ImBuf * imb_thumb_load_from_blendfile(const char *blen_path)
static ImBuf * imb_thumb_load_from_blend_id(const char *blen_path, const char *blen_group, const char *blen_id)
ImBuf * IMB_thumb_load_blend(const char *blen_path, const char *blen_group, const char *blen_id)