Blender  V2.93
thumbs_blend.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "BLI_linklist.h"
26 #include "BLI_listbase.h" /* Needed due to import of BLO_readfile.h */
27 #include "BLI_utildefines.h"
28 
29 #include "BLO_blend_defs.h"
30 #include "BLO_readfile.h"
31 
32 #include "BKE_icons.h"
33 #include "BKE_idtype.h"
34 #include "BKE_main.h"
35 
36 #include "DNA_ID.h" /* For preview images... */
37 
38 #include "IMB_imbuf.h"
39 #include "IMB_imbuf_types.h"
40 #include "IMB_thumbs.h"
41 
42 #include "MEM_guardedalloc.h"
43 
44 ImBuf *IMB_thumb_load_blend(const char *blen_path, const char *blen_group, const char *blen_id)
45 {
46  ImBuf *ima = NULL;
47 
48  if (blen_group && blen_id) {
49  LinkNode *ln, *names, *lp, *previews = NULL;
50  struct BlendHandle *libfiledata = BLO_blendhandle_from_file(blen_path, NULL);
51  int idcode = BKE_idtype_idcode_from_name(blen_group);
52  int i, nprevs, nnames;
53 
54  if (libfiledata == NULL) {
55  return ima;
56  }
57 
58  /* Note: we should handle all previews for a same group at once, would avoid reopening
59  * `.blend` file for each and every ID. However, this adds some complexity,
60  * so keep it for later. */
61  names = BLO_blendhandle_get_datablock_names(libfiledata, idcode, false, &nnames);
62  previews = BLO_blendhandle_get_previews(libfiledata, idcode, &nprevs);
63 
64  BLO_blendhandle_close(libfiledata);
65 
66  if (!previews || (nnames != nprevs)) {
67  if (previews != 0) {
68  /* No previews at all is not a bug! */
69  printf("%s: error, found %d items, %d previews\n", __func__, nnames, nprevs);
70  }
73  return ima;
74  }
75 
76  for (i = 0, ln = names, lp = previews; i < nnames; i++, ln = ln->next, lp = lp->next) {
77  const char *blockname = ln->link;
78  PreviewImage *img = lp->link;
79 
80  if (STREQ(blockname, blen_id)) {
81  if (img) {
83  }
84  break;
85  }
86  }
87 
90  }
91  else {
93 
94  data = BLO_thumbnail_from_file(blen_path);
96 
97  if (data) {
98  MEM_freeN(data);
99  }
100  }
101 
102  return ima;
103 }
void BKE_previewimg_freefunc(void *link)
Definition: icons.cc:278
struct ImBuf * BKE_previewimg_to_imbuf(struct PreviewImage *prv, const int size)
Definition: icons.cc:597
short BKE_idtype_idcode_from_name(const char *idtype_name)
Definition: idtype.c:208
struct ImBuf * BKE_main_thumbnail_to_imbuf(struct Main *bmain, struct BlendThumbnail *data)
Definition: main.c:394
#define STREQ(a, b)
defines for blend-file codes.
external readfile function prototypes.
struct LinkNode * BLO_blendhandle_get_previews(BlendHandle *bh, int ofblocktype, int *r_tot_prev)
struct BlendHandle BlendHandle
Definition: BLO_readfile.h:49
struct LinkNode * BLO_blendhandle_get_datablock_names(BlendHandle *bh, int ofblocktype, const bool use_assets_only, int *r_tot_names)
BlendHandle * BLO_blendhandle_from_file(const char *filepath, struct ReportList *reports)
Definition: readblenentry.c:71
void BLO_blendhandle_close(BlendHandle *bh)
struct BlendThumbnail * BLO_thumbnail_from_file(const char *filepath)
Definition: readfile.c:1769
ID and Library types, which are fundamental for sdna.
@ ICON_SIZE_PREVIEW
Definition: DNA_ID_enums.h:30
Contains defines and structs used throughout the imbuf module.
Read Guarded memory(de)allocation.
static char ** names
Definition: makesdna.c:162
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void * link
Definition: BLI_linklist.h:40
struct LinkNode * next
Definition: BLI_linklist.h:39
ImBuf * IMB_thumb_load_blend(const char *blen_path, const char *blen_group, const char *blen_id)
Definition: thumbs_blend.c:44