Commit: d141442bd125fa68c6e029e6383e5cacc96087fa Author: Bastien Montagne Date: Mon Aug 17 17:33:24 2015 +0200 Branches: blend-thumbnail https://developer.blender.org/rBd141442bd125fa68c6e029e6383e5cacc96087fa
Make .blend file thumbnail reading simpler and more coherent, read/store them when reading in background mode, and allow user to define custom thumbnails. Primary goal of this commit is to fix an annoying issue - when processing and saving .blend files in background mode you lose their thumbnails, since it can only be generated with an OpenGL context. Solution to that is to read .blend thumbnail while reading .blend file (only done in background mode currently), and store it in Main struct. Also, this lead to removing .blend file reading code from thumb_blend (no need to have doublons, and always hated that piece of unreadable geeky code there anyway). We now have a small interface in regular reading code area, which keeps it reasonbaly light by only reading/parsing header info, and first few BHead blocks. This makes code reading .blend thumbnail about 3 to 4 times slower than previous highly specialized one in blend_thumb.c, but overall thumbnail generation of a big .blend files folder only grows of about 1%, think we can bare with it. Finally, since thumbnail is now optionally stored in Main struct, it makes it easy to allow user to define their own custom one (instead of auto-generated one), either by generating it from a specific scene, or giving it a raw array of pixels. Reviewers: sergey, campbellbarton Subscribers: Severin, psy-fi Differential Revision: https://developer.blender.org/D1469 =================================================================== M source/blender/blenkernel/BKE_library.h M source/blender/blenkernel/BKE_main.h M source/blender/blenkernel/intern/library.c M source/blender/blenloader/BLO_readfile.h M source/blender/blenloader/BLO_writefile.h M source/blender/blenloader/intern/readfile.c M source/blender/imbuf/intern/thumbs_blend.c M source/blender/makesrna/intern/rna_main.c M source/blender/makesrna/intern/rna_main_api.c M source/blender/windowmanager/intern/wm_files.c M source/blenderplayer/bad_level_call_stubs/stubs.c =================================================================== diff --git a/source/blender/blenkernel/BKE_library.h b/source/blender/blenkernel/BKE_library.h index 6ecc955..c54989f 100644 --- a/source/blender/blenkernel/BKE_library.h +++ b/source/blender/blenkernel/BKE_library.h @@ -40,6 +40,7 @@ extern "C" { struct ListBase; struct ID; +struct ImBuf; struct Main; struct Library; struct wmWindowManager; @@ -87,6 +88,10 @@ void BKE_main_free(struct Main *mainvar); void BKE_main_lock(struct Main *bmain); void BKE_main_unlock(struct Main *bmain); +int *BKE_main_thumbnail_from_imbuf(struct Main *bmain, struct ImBuf *img); +struct ImBuf *BKE_main_thumbnail_to_imbuf(struct Main *bmain, int *data); +void BKE_main_thumbnail_create(struct Main *bmain); + void BKE_main_id_tag_idcode(struct Main *mainvar, const short type, const bool tag); void BKE_main_id_tag_listbase(struct ListBase *lb, const bool tag); void BKE_main_id_tag_all(struct Main *mainvar, const bool tag); diff --git a/source/blender/blenkernel/BKE_main.h b/source/blender/blenkernel/BKE_main.h index ec654ea..0365281 100644 --- a/source/blender/blenkernel/BKE_main.h +++ b/source/blender/blenkernel/BKE_main.h @@ -58,6 +58,8 @@ typedef struct Main { uint64_t build_commit_timestamp; /* commit's timestamp from buildinfo */ char build_hash[16]; /* hash from buildinfo */ short recovered; /* indicate the main->name (file) is the recovered one */ + + int *blen_thumb; /* Blender thumbnail, as written on file (width, height, and data as char RGBA). */ struct Library *curlib; ListBase scene; @@ -109,7 +111,10 @@ typedef struct Main { #define MAIN_VERSION_OLDER(main, ver, subver) \ ((main)->versionfile < (ver) || (main->versionfile == (ver) && (main)->subversionfile < (subver))) - +#define BLEN_THUMB_SIZE 128 + +#define BLEN_THUMB_MEMSIZE(_x, _y) ((size_t)(2 + (_x) * (_y)) * sizeof(int)) + #ifdef __cplusplus } #endif diff --git a/source/blender/blenkernel/intern/library.c b/source/blender/blenkernel/intern/library.c index 8bb2864..7a84141 100644 --- a/source/blender/blenkernel/intern/library.c +++ b/source/blender/blenkernel/intern/library.c @@ -119,6 +119,9 @@ #include "RNA_access.h" +#include "IMB_imbuf.h" +#include "IMB_imbuf_types.h" + #ifdef WITH_PYTHON #include "BPY_extern.h" #endif @@ -1097,6 +1100,8 @@ void BKE_main_free(Main *mainvar) ListBase *lbarray[MAX_LIBARRAY]; int a; + MEM_SAFE_FREE(mainvar->blen_thumb); + a = set_listbasepointers(mainvar, lbarray); while (a--) { ListBase *lb = lbarray[a]; @@ -1166,6 +1171,74 @@ void BKE_main_unlock(struct Main *bmain) BLI_spin_unlock((SpinLock *) bmain->lock); } +/** + * Generates a raw .blend file thumbnail data from given image. + * + * @param bmain If not NULL, also store generated data in this Main. + * @param img ImBuf image to generate thumbnail data from. + * @return The generated .blend file raw thumbnail data. + */ +int *BKE_main_thumbnail_from_imbuf(Main *bmain, ImBuf *img) +{ + int *data = NULL; + + if (bmain) { + MEM_SAFE_FREE(bmain->blen_thumb); + } + + if (img) { + const size_t sz = BLEN_THUMB_MEMSIZE(img->x, img->y); + data = MEM_mallocN(sz, __func__); + + IMB_rect_from_float(img); /* Just in case... */ + data[0] = img->x; + data[1] = img->y; + memcpy(&data[2], img->rect, sz - (sizeof(*data) * 2)); + } + + if (bmain) { + bmain->blen_thumb = data; + } + return data; +} + +/** + * Generates an image from raw .blend file thumbnail @a data. + * + * @param bmain Use this bmain->blen_thumb data if given @data is NULL. + * @param data Raw .blend file thumbnail data. + * @return An ImBuf from given data, or NULL if invalid. + */ +ImBuf *BKE_main_thumbnail_to_imbuf(Main *bmain, int *data) +{ + ImBuf *img = NULL; + + if (!data && bmain) { + data = bmain->blen_thumb; + } + + if (data) { + /* Note: we cannot use IMB_allocFromBuffer(), since it tries to dupalloc passed buffer, which will fail + * here (we do not want to pass the first two ints!). */ + img = IMB_allocImBuf((unsigned int)data[0], (unsigned int)data[1], 32, IB_rect | IB_metadata); + memcpy(img->rect, &data[2], sizeof(*data) * (size_t)data[0] * (size_t)data[1]); + } + + return img; +} + +/** + * Generates an empty (black) thumbnail for given Main. + */ +void BKE_main_thumbnail_create(struct Main *bmain) +{ + MEM_SAFE_FREE(bmain->blen_thumb); + + bmain->blen_thumb = MEM_callocN(BLEN_THUMB_MEMSIZE(BLEN_THUMB_SIZE, BLEN_THUMB_SIZE), __func__); + bmain->blen_thumb[0] = BLEN_THUMB_SIZE; + bmain->blen_thumb[1] = BLEN_THUMB_SIZE; +} + /* ***************** ID ************************ */ ID *BKE_libblock_find_name_ex(struct Main *bmain, const short type, const char *name) { diff --git a/source/blender/blenloader/BLO_readfile.h b/source/blender/blenloader/BLO_readfile.h index 5f881c0..71e06a8 100644 --- a/source/blender/blenloader/BLO_readfile.h +++ b/source/blender/blenloader/BLO_readfile.h @@ -278,6 +278,15 @@ void BLO_expand_main(void *fdhandle, struct Main *mainvar); void BLO_update_defaults_userpref_blend(void); void BLO_update_defaults_startup_blend(struct Main *mainvar); +/** + * Does a very light reading of given .blend file to extract its stored thumbnail. + * + * @param filepath The path of the file to extract thumbnail from. + * @return The raw thumbnail + * (MEM-allocated, as stored in file, use BKE_main_thumbnail_to_imbuf() to convert it to ImBuf image). + */ +int *BLO_thumbnail_from_file(const char *filepath); + #ifdef __cplusplus } #endif diff --git a/source/blender/blenloader/BLO_writefile.h b/source/blender/blenloader/BLO_writefile.h index 7a8429a..9dde2f0 100644 --- a/source/blender/blenloader/BLO_writefile.h +++ b/source/blender/blenloader/BLO_writefile.h @@ -40,7 +40,5 @@ struct ReportList; extern int BLO_write_file(struct Main *mainvar, const char *filepath, int write_flags, struct ReportList *reports, const int *thumb); extern int BLO_write_file_mem(struct Main *mainvar, struct MemFile *compare, struct MemFile *current, int write_flags); -#define BLEN_THUMB_SIZE 128 - #endif diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c index 6581fb3..9d3b24e 100644 --- a/source/blender/blenloader/intern/readfile.c +++ b/source/blender/blenloader/intern/readfile.c @@ -917,6 +917,41 @@ static int read_file_dna(FileData *fd) return 0; } +static int *read_file_thumbnail(FileData *fd) +{ + BHead *bhead; + int *blend_thumb = NULL; + + for (bhead = blo_firstbhead(fd); bhead; bhead = blo_nextbhead(fd, bhead)) { + if (bhead->code == TEST) { + const bool do_endian_swap = (fd->flags & FD_FLAGS_SWITCH_ENDIAN) != 0; + int *data = (int *)(bhead + 1); + + if (bhead->len < (2 * sizeof(int))) { + break; + } + + if (do_endian_swap) { + BLI_endian_switch_int32(&data[0]); + BLI_endian_switch_int32(&data[1]); + } + + if (bhead->len < BLEN_THUMB_MEMSIZE(data[0], data[1])) { + break; + } + + blend_thumb = data; + break; + } + else if (bhead->code != REND) { + /* Thumbnail is stored in TEST immediately after first REND... */ + break; + } + } + + return blend_thumb; +} + static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size) { int readsize = read(filedata->filedes, buffer, size); @@ -1080,6 +1115,33 @@ FileData *blo_openblenderfile(const char *filepath, ReportList *reports) } } +/** + * Same as blo_openblenderfile(), but does not reads DNA data, only header. Use it for light access + * (e.g. thumbnail reading). + */ +static FileData *blo_openblenderfile_minimal(const char *filepath) +{ + gzFile gzfile; + errno = 0; + gzfile = BLI_gzopen(filepath, "rb"); + + if (gzfile != (gzFile)Z_NULL) { + FileData *fd = filedata_new(); + fd->gzfiledes = gzfile; + fd->read = fd_read_gzip_from_file; + + decode_blender_header(fd); + + if (fd->flags & FD_FLAGS_FILE_OK) { + return fd; + } + + blo_freefiledata(fd); + } + + return NULL; +} + static int fd_read_gzip_from_memory(FileData *filedata, void *buffer, unsigned int size) { int err; @@ -1290,6 +1352,28 @@ bool BLO_library_path_explode(const char *path, char *r_dir, char **r_group, cha return true; } +int *BLO_thumbnail_from_file(const char *filepath) +{ + FileData *fd; + int *fd_data, *data; + + fd = blo_openblenderfile_minimal(filepath); + fd_data = fd ? read_file_thumbnail(fd) : NULL; + + if (fd_data) { + const size_t data_size = BLEN_THUMB_MEMSIZE(fd_data[0], fd_data[1]); + data = MEM_mallocN(data_size, __func__); + memcpy(data, fd_data, data_size); + } + else { + data = NULL; + } + + blo_freefiledata(fd); + + return data; +} + /* ************** OLD POINTERS ******************* */ static void *newdataadr(FileData *fd, void *adr) /* only direct databocks */ @@ -8174,6 +8258,20 @@ BlendFileData *blo_read_file_internal(FileData *fd, const char *filepath) bfd->type = BLENFILETYPE_BLEND; BLI_strncpy(bfd->main->name, filepath, sizeof(bfd->main->name)); + if (G.background) { + /* We only read & store .blend thumbnail in background mode + * (because we cannot re-generate it, no OpenGL available). + */ + const int *data = read_file_thumbnail(fd); + + if (data) { + const size_t data_size = BLEN_THUMB_MEMSIZE(data[0], data[1]); + + bfd->main->blen_thumb = MEM_mallocN(data_size, __func__); + memcpy(bfd->main->blen_thumb, data, data_size); + } + } + while (bhead) { switch (bhead->code) { case DATA: diff --git a/source/blender/imbuf/intern/thumbs_blend.c b/source/blender/imbuf/intern/thumbs_blend.c index 17d9f3d..f3a21bf 100644 --- a/source/blender/imbuf/intern/thumbs_blend.c +++ b/source/blender/imbuf/intern/thumbs_blend.c @@ -28,8 +28,6 @@ #include <stdlib.h> #include <string.h> -#include "zlib.h" - #include "BLI_utildefines.h" #include "BLI_endian_switch.h" #include "BLI_fileops.h" @@ -41,6 +39,7 @@ #include "BKE_global.h" #include "BKE_idcode.h" #include "BKE_icons.h" +#include "BKE_library.h" #include "DNA_ID.h" /* For preview images... */ @@ -48,97 +47,20 @@ #include "IMB_imbuf.h" #include "IMB_thumbs.h" -/* extracts the thumbnail from between the 'REND' and the 'GLOB' - * chunks of the header, don't use typical blend loader because its too slow */ - -static ImBuf *loadblend_thumb(gzFile gzfile) -{ - char buf[12]; - int bhead[24 / s @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list [email protected] http://lists.blender.org/mailman/listinfo/bf-blender-cvs
