Blender  V3.3
blender_thumbnailer.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
21 #include <fstream>
22 #include <optional>
23 
24 #include <fcntl.h>
25 #ifndef WIN32
26 # include <unistd.h> /* For read close. */
27 #else
28 # include "BLI_winstuff.h"
29 # include "winsock2.h"
30 # include <io.h> /* For open close read. */
31 #endif
32 
33 #include "BLI_fileops.h"
34 #include "BLI_filereader.h"
35 #include "BLI_vector.hh"
36 
37 #include "blendthumb.hh"
38 
44 static eThumbStatus extract_png_from_blend_file(const char *src_blend, const char *dst_png)
45 {
47 
48  /* Open source file `src_blend`. */
49  const int src_file = BLI_open(src_blend, O_BINARY | O_RDONLY, 0);
50  if (src_file == -1) {
51  return BT_FILE_ERR;
52  }
53 
54  /* Thumbnail reading is responsible for freeing `file` and closing `src_file`. */
56  if (file == nullptr) {
57  close(src_file);
58  return BT_FILE_ERR;
59  }
60 
61  /* Extract thumbnail from file. */
62  Thumbnail thumb;
64  if (err != BT_OK) {
65  return err;
66  }
67 
68  /* Write thumbnail to `dst_png`. */
69  const int dst_file = BLI_open(dst_png, O_BINARY | O_WRONLY | O_CREAT | O_TRUNC, 0666);
70  if (dst_file == -1) {
71  return BT_FILE_ERR;
72  }
73 
74  std::optional<blender::Vector<uint8_t>> png_buf_opt = blendthumb_create_png_data_from_thumb(
75  &thumb);
76  if (png_buf_opt == std::nullopt) {
77  err = BT_ERROR;
78  }
79  else {
80  blender::Vector<uint8_t> png_buf = *png_buf_opt;
81  err = (write(dst_file, png_buf.data(), png_buf.size()) == png_buf.size()) ? BT_OK :
83  }
84  close(dst_file);
85 
86  return err;
87 }
88 
89 int main(int argc, char *argv[])
90 {
91  if (argc < 3) {
92  std::cerr << "Usage: blender-thumbnailer <input.blend> <output.png>" << std::endl;
93  return -1;
94  }
95 
96  eThumbStatus ret = extract_png_from_blend_file(argv[1], argv[2]);
97 
98  return (int)ret;
99 }
File and directory operations.
#define O_BINARY
Definition: BLI_fileops.h:319
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: fileops.c:920
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.
int main(int argc, char *argv[])
static eThumbStatus extract_png_from_blend_file(const char *src_blend, const char *dst_png)
eThumbStatus
Definition: blendthumb.hh:27
@ BT_ERROR
Definition: blendthumb.hh:35
@ BT_FILE_ERR
Definition: blendthumb.hh:29
@ BT_OK
Definition: blendthumb.hh:28
std::optional< blender::Vector< uint8_t > > blendthumb_create_png_data_from_thumb(const Thumbnail *thumb)
eThumbStatus blendthumb_create_thumb_from_file(struct FileReader *rawfile, Thumbnail *thumb)
int64_t size() const
Definition: BLI_vector.hh:694
FILE * file
return ret
static FT_Error err