Blender V5.3
blender_thumbnailer.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
22
23#include <iostream>
24#include <optional>
25
26#include <fcntl.h>
27#ifndef WIN32
28# include <unistd.h> /* For read close. */
29#else
30# include "BLI_winstuff.hh"
31# include "winsock2.h"
32# include <io.h> /* For open close read. */
33#endif
34
35#include "BLI_fileops.hh"
36#include "BLI_filereader.hh"
37#include "BLI_vector.hh"
38
39#include "blendthumb.hh"
40
41using namespace blender;
42
48static eThumbStatus extract_png_from_blend_file(const char *src_blend, const char *dst_png)
49{
50 eThumbStatus err;
51
52 /* Open source file `src_blend`. */
53 const int src_file = BLI_open(src_blend, O_BINARY | O_RDONLY, 0);
54 if (src_file == -1) {
55 return BT_FILE_ERR;
56 }
57
58 /* Thumbnail reading is responsible for freeing `file` and closing `src_file`. */
59 FileReader *file = BLI_filereader_new_file(src_file);
60 if (file == nullptr) {
61 close(src_file);
62 return BT_FILE_ERR;
63 }
64
65 /* Extract thumbnail from file. */
66 Thumbnail thumb;
67 err = blendthumb_create_thumb_from_file(file, &thumb);
68 if (err != BT_OK) {
69 return err;
70 }
71
72 /* Write thumbnail to `dst_png`. */
73 const int dst_file = BLI_open(dst_png, O_BINARY | O_WRONLY | O_CREAT | O_TRUNC, 0666);
74 if (dst_file == -1) {
75 return BT_FILE_ERR;
76 }
77
78 std::optional<Vector<uint8_t>> png_buf_opt = blendthumb_create_png_data_from_thumb(&thumb);
79 if (!png_buf_opt) {
80 err = BT_ERROR;
81 }
82 else {
83 Vector<uint8_t> png_buf = *png_buf_opt;
84 err = (write(dst_file, png_buf.data(), png_buf.size()) == png_buf.size()) ? BT_OK :
86 }
87 close(dst_file);
88
89 return err;
90}
91
92int main(int argc, char *argv[])
93{
94 if (argc < 3) {
95 std::cerr << "Usage: blender-thumbnailer <input.blend> <output.png>" << std::endl;
96 return -1;
97 }
98
100
101 return int(ret);
102}
File and directory operations.
#define O_BINARY
Wrapper for reading from various sources (e.g. raw files, compressed files, memory....
Compatibility-like things for windows.
static eThumbStatus extract_png_from_blend_file(const char *src_blend, const char *dst_png)
int64_t size() const
#define main()
BMesh Iterators.
eThumbStatus blendthumb_create_thumb_from_file(FileReader *rawfile, Thumbnail *thumb)
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
std::optional< Vector< uint8_t > > blendthumb_create_png_data_from_thumb(const Thumbnail *thumb)
@ BT_FILE_ERR
Definition blendthumb.hh:32
FileReader * BLI_filereader_new_file(int filedes) ATTR_WARN_UNUSED_RESULT