Blender V5.3
thumbnail_provider.mm
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#import <AppKit/NSImage.h>
10
11#include "BLI_fileops.hh"
12#include "BLI_filereader.hh"
13#include "BLI_utility_mixins.hh"
14#include "blendthumb.hh"
15
16#include "thumbnail_provider.h"
74
76 private:
77 int src_fd = -1;
78
79 public:
80 explicit FileDescriptorRAII(const char *file_path)
81 {
82 src_fd = blender::BLI_open(file_path, O_BINARY | O_RDONLY, 0);
83 }
84
86 {
87 if (good()) {
88 int ok = close(src_fd);
89 if (!ok) {
90 NSLog(@"Blender Thumbnailer Error: Failed to close the blend file.");
91 }
92 }
93 }
94
95 bool good()
96 {
97 return src_fd > 0;
98 }
99
100 int get()
101 {
102 return src_fd;
103 }
104};
105
106static NSError *create_nserror_from_string(NSString *errorStr)
107{
108 NSLog(@"Blender Thumbnailer Error: %@", errorStr);
109 return [NSError errorWithDomain:@"org.blenderfoundation.blender.thumbnailer"
110 code:-1
111 userInfo:@{NSLocalizedDescriptionKey : errorStr}];
112}
113
114static NSImage *generate_nsimage_for_file(const char *src_blend_path, NSError *error)
115{
116 /* Open source file `src_blend`. */
117 FileDescriptorRAII src_file_fd = FileDescriptorRAII(src_blend_path);
118 if (!src_file_fd.good()) {
119 error = create_nserror_from_string(@"Failed to open blend");
120 return nil;
121 }
122
123 blender::FileReader *file_content = blender::BLI_filereader_new_file(src_file_fd.get());
124 if (file_content == nullptr) {
125 error = create_nserror_from_string(@"Failed to read from blend");
126 return nil;
127 }
128
129 /* Extract thumbnail from file. */
130 blender::Thumbnail thumb;
131 blender::eThumbStatus err = blendthumb_create_thumb_from_file(file_content, &thumb);
132 if (err != blender::BT_OK) {
133 error = create_nserror_from_string(@"Failed to create thumbnail from file");
134 return nil;
135 }
136
137 std::optional<blender::Vector<uint8_t>> png_buf_opt = blendthumb_create_png_data_from_thumb(
138 &thumb);
139 if (!png_buf_opt) {
140 error = create_nserror_from_string(@"Failed to create png data from thumbnail");
141 return nil;
142 }
143
144 NSData *ns_data = [NSData dataWithBytes:png_buf_opt->data() length:png_buf_opt->size()];
145 NSImage *ns_image = [[NSImage alloc] initWithData:ns_data];
146 return ns_image;
147}
148
149@implementation ThumbnailProvider
150
151- (void)provideThumbnailForFileRequest:(QLFileThumbnailRequest *)request
152 completionHandler:(void (^)(QLThumbnailReply *_Nullable reply,
153 NSError *_Nullable error))handler
154{
155
156 NSLog(@"Generating thumbnail for %@", request.fileURL.path);
157 @autoreleasepool {
158 NSError *error = nil;
159 NSImage *image = generate_nsimage_for_file(request.fileURL.path.fileSystemRepresentation,
160 error);
161 if (image == nil || image.size.width <= 0 || image.size.height <= 0) {
162 handler(nil, error);
163 return;
164 }
165
166 const CGFloat width_ratio = request.maximumSize.width / image.size.width;
167 const CGFloat height_ratio = request.maximumSize.height / image.size.height;
168 const CGFloat scale_factor = MIN(width_ratio, height_ratio);
169
170 const NSSize context_size = NSMakeSize(image.size.width * scale_factor,
171 image.size.height * scale_factor);
172
173 const NSRect context_rect = NSMakeRect(0, 0, context_size.width, context_size.height);
174
175 QLThumbnailReply *thumbnailReply = [QLThumbnailReply replyWithContextSize:context_size
176 currentContextDrawingBlock:^BOOL {
177 [image drawInRect:context_rect];
178 /* Release the image that was strongly
179 * captured by this block. */
180 [image release];
181 return YES;
182 }];
183
184 /* Return the thumbnail reply. */
185 handler(thumbnailReply, nil);
186 }
187 NSLog(@"Thumbnail generation successfully completed");
188}
189
190@end
File and directory operations.
#define O_BINARY
Wrapper for reading from various sources (e.g. raw files, compressed files, memory....
#define MIN(_a, _b)
FileDescriptorRAII(const char *file_path)
#define image(slot, qualifiers, format)
float length(VecOp< float, D >) RET
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
FileReader * BLI_filereader_new_file(int filedes) ATTR_WARN_UNUSED_RESULT
static NSError * create_nserror_from_string(NSString *errorStr)
static NSImage * generate_nsimage_for_file(const char *src_blend_path, NSError *error)