diff options
author | Campbell Barton <ideasman42@gmail.com> | 2021-10-20 02:16:36 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2021-10-20 02:31:30 +0300 |
commit | ef9269bd62f31e39d39cc59cd34354b53eae6661 (patch) | |
tree | 5ff2fb2f3b03678b7e58a23397d4d53bb83f35f8 /source/blender/blendthumb/src/blender_thumbnailer.cc | |
parent | bca2701236ab60dce5e32a8731f3bba43783b5b2 (diff) |
Thumbnails: refactor extraction to use one code-path for all platforms
Thumbnail extraction now shares code between Linux/Windows,
allowing thumbnails from Zstd compressed blend files to be extracted.
The main logic is placed in blendthumb_extract.cc and is built as static
library. For windows there is DLL which is registered during blender
install and which then reads and generates thumbnails.
For other platforms there is blender-thumbnailer executable file which
takes blend file as an input and generates PNG file. As a result
Python script blender-thumbnailer.py is no longer needed.
The thumbnail extractor shares the same code-path as Blenders file
reading, so there is no need to duplicate any file reading logic.
This means reading compressed blend files is supported (broken since
the recent move Zstd compression - D5799).
This resolves T63736.
Contributors:
- @alausic original patch.
- @LazyDodo windows fixes/support.
- @campbellbarton general fixes/update.
- @lukasstockner97 Zstd support.
Reviewed By: sybren, mont29, LazyDodo, campbellbarton
Ref D6408
Diffstat (limited to 'source/blender/blendthumb/src/blender_thumbnailer.cc')
-rw-r--r-- | source/blender/blendthumb/src/blender_thumbnailer.cc | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/source/blender/blendthumb/src/blender_thumbnailer.cc b/source/blender/blendthumb/src/blender_thumbnailer.cc new file mode 100644 index 00000000000..8dd9d5c0c0a --- /dev/null +++ b/source/blender/blendthumb/src/blender_thumbnailer.cc @@ -0,0 +1,113 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +/** \file + * \ingroup blendthumb + * + * This file defines the thumbnail generation command (typically used on UNIX). + * + * To run automatically with a file manager such as Nautilus, save this file + * in a directory that is listed in PATH environment variable, and create + * `blender.thumbnailer` file in `${HOME}/.local/share/thumbnailers/` directory + * with the following contents: + * + * \code{.txt} + * [Thumbnailer Entry] + * TryExec=blender-thumbnailer + * Exec=blender-thumbnailer %u %o + * MimeType=application/x-blender; + * \endcode + */ + +#include <fstream> +#include <optional> + +#include <fcntl.h> +#ifndef WIN32 +# include <unistd.h> /* For read close. */ +#else +# include "BLI_winstuff.h" +# include "winsock2.h" +# include <io.h> /* For open close read. */ +#endif + +#include "BLI_fileops.h" +#include "BLI_filereader.h" +#include "BLI_vector.hh" + +#include "blendthumb.hh" + +/** + * This function opens .blend file from src_blend, extracts thumbnail from file if there is one, + * and writes `.png` image into `dst_png`. + * Returns exit code (0 if successful). + */ +static eThumbStatus extract_png_from_blend_file(const char *src_blend, const char *dst_png) +{ + eThumbStatus err; + + /* Open source file `src_blend`. */ + const int src_file = BLI_open(src_blend, O_BINARY | O_RDONLY, 0); + if (src_file == -1) { + return BT_FILE_ERR; + } + + /* Thumbnail reading is responsible for freeing `file` and closing `src_file`. */ + FileReader *file = BLI_filereader_new_file(src_file); + if (file == nullptr) { + close(src_file); + return BT_FILE_ERR; + } + + /* Extract thumbnail from file. */ + Thumbnail thumb; + err = blendthumb_create_thumb_from_file(file, &thumb); + if (err != BT_OK) { + return err; + } + + /* Write thumbnail to `dst_png`. */ + const int dst_file = BLI_open(dst_png, O_BINARY | O_WRONLY | O_CREAT | O_TRUNC, 0666); + if (dst_file == -1) { + return BT_FILE_ERR; + } + + std::optional<blender::Vector<uint8_t>> png_buf_opt = blendthumb_create_png_data_from_thumb( + &thumb); + if (png_buf_opt == std::nullopt) { + err = BT_ERROR; + } + else { + blender::Vector<uint8_t> png_buf = *png_buf_opt; + err = (write(dst_file, png_buf.data(), png_buf.size()) == png_buf.size()) ? BT_OK : + BT_FILE_ERR; + } + close(dst_file); + + return err; +} + +int main(int argc, char *argv[]) +{ + if (argc < 3) { + std::cerr << "Usage: blender-thumbnailer <input.blend> <output.png>" << std::endl; + return -1; + } + + eThumbStatus ret = extract_png_from_blend_file(argv[1], argv[2]); + + return (int)ret; +} |