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