This repository has been archived by the owner on Jun 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
292 lines (234 loc) · 9.5 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "BlackmagicRawAPI.h"
#include "BlackmagicRawAPIDispatch.cpp"
#include <iostream>
#include <sys/stat.h>
#include <sys/dir.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
#include <CoreGraphics/CoreGraphics.h>
#include <ImageIO/ImageIO.h>
#ifdef DEBUG
#include <cassert>
#define VERIFY(condition) assert(SUCCEEDED(condition))
#else
#define VERIFY(condition) condition
#endif
BlackmagicRawResourceFormat s_resourceFormat = blackmagicRawResourceFormatRGBAU8;
static const CFStringRef s_brawSdkPath = CFSTR("/Applications/Blackmagic RAW/Blackmagic RAW SDK/Mac/Libraries");
CFStringRef s_outputFileName = CFSTR("/tmp/outputRGBAU8.png");
static void OutputImage(CFStringRef outputFileName, uint32_t width, uint32_t height, uint32_t sizeBytes, void* imageData)
{
bool success = false;
const char* outputFileNameAsCString = CFStringGetCStringPtr(outputFileName, kCFStringEncodingMacRoman);
CFURLRef file = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, outputFileName, kCFURLPOSIXPathStyle, false);
if (file != nullptr)
{
const uint32_t bitsPerComponent = 8;
const uint32_t bitsPerPixel = 32;
const uint32_t bytesPerRow = (bitsPerPixel * width) / 8U;
CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
CGBitmapInfo bitmapInfo = kCGImageAlphaNoneSkipLast | kCGImageByteOrderDefault;
CGDataProviderRef provider = CGDataProviderCreateWithData(nullptr, imageData, sizeBytes, nullptr);
const CGFloat* decode = nullptr;
bool shouldInterpolate = false;
CGColorRenderingIntent intent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, space, bitmapInfo, provider, decode, shouldInterpolate, intent);
if (imageRef != nullptr)
{
CGImageDestinationRef destination = CGImageDestinationCreateWithURL(file, kUTTypePNG, 1, nullptr);
if (destination)
{
CGImageDestinationAddImage(destination, imageRef, nil);
CGImageDestinationFinalize(destination);
CFRelease(destination);
std::cout << "Created " << outputFileNameAsCString << std::endl;
success = true;
}
CGImageRelease(imageRef);
}
CGDataProviderRelease(provider);
CGColorSpaceRelease(space);
CFRelease(file);
}
if (! success)
std::cerr << "Failed to create " << outputFileNameAsCString << "!" << std::endl;
}
class CameraCodecCallback : public IBlackmagicRawCallback {
public:
explicit CameraCodecCallback() = default;
~CameraCodecCallback() override = default;
void ReadComplete(IBlackmagicRawJob* job, HRESULT result, IBlackmagicRawFrame* frame) override {
IBlackmagicRawJob* procJob = nullptr;
if (result == S_OK)
VERIFY(frame->SetResourceFormat(s_resourceFormat));
if (result == S_OK)
result = frame->CreateJobDecodeAndProcessFrame(nullptr, nullptr, &procJob);
if (result == S_OK)
result = procJob->Submit();
if (result != S_OK)
{
if (procJob)
procJob->Release();
}
job->Release();
}
void ProcessComplete(IBlackmagicRawJob* job, HRESULT result, IBlackmagicRawProcessedImage* processedImage) override {
unsigned int width = 0;
unsigned int height = 0;
unsigned int sizeBytes = 0;
void* imageData = nullptr;
if (result == S_OK)
result = processedImage->GetWidth(&width);
if (result == S_OK)
result = processedImage->GetHeight(&height);
if (result == S_OK)
result = processedImage->GetResourceSizeBytes(&sizeBytes);
if (result == S_OK)
result = processedImage->GetResource(&imageData);
if (result == S_OK)
OutputImage(s_outputFileName, width, height, sizeBytes, imageData);
job->Release();
}
void DecodeComplete(IBlackmagicRawJob*, HRESULT) override {}
void TrimProgress(IBlackmagicRawJob*, float) override {}
void TrimComplete(IBlackmagicRawJob*, HRESULT) override {}
void SidecarMetadataParseWarning(IBlackmagicRawClip*, CFStringRef, uint32_t, CFStringRef) override {}
void SidecarMetadataParseError(IBlackmagicRawClip*, CFStringRef, uint32_t, CFStringRef) override {}
void PreparePipelineComplete(void*, HRESULT) override {}
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID, LPVOID*) override
{
return E_NOTIMPL;
}
ULONG STDMETHODCALLTYPE AddRef() override
{
return 0;
}
ULONG STDMETHODCALLTYPE Release() override
{
return 0;
}
};
int main(int argc, char** argv) {
std::string outDir;
std::string inDir;
std::string outputFormat;
for (int i = 1; i < argc; ++i) {
if (std::string(argv[i]) == "-f" || std::string(argv[i]) == "--outputFormat") {
if (i + 1 < argc) {
outputFormat = argv[++i];
} else {
std::cerr << "--outputFormat option requires one argument." << std::endl;
return 1;
}
} else if (std::string(argv[i]) == "-i" || std::string(argv[i]) == "--inDir") {
if (i + 1 < argc) {
inDir = argv[++i];
} else {
std::cerr << "--inDir option requires one argument." << std::endl;
return 1;
}
} else if (std::string(argv[i]) == "-o" || std::string(argv[i]) == "--outDir") {
if (i + 1 < argc) {
outDir = argv[++i];
} else {
std::cerr << "--outDir option requires one argument." << std::endl;
return 1;
}
}
}
if (outputFormat.empty() || inDir.empty()) {
std::cerr << "Usage: " << argv[0] << " -o/--outputFormat <outputFormat> -i/--inDir <inDir>" << std::endl;
return 1;
}
if (outputFormat == "png") {
s_resourceFormat = blackmagicRawResourceFormatRGBAU8;
} else if (outputFormat == "dng") {
s_resourceFormat = blackmagicRawResourceFormatRGBF16;
} else {
std::cerr << "Unknown output format: " << outputFormat << std::endl;
return 1;
}
struct stat inInfo;
if (stat(inDir.c_str(), &inInfo) != 0) {
std::cerr << "Failed to stat input directory: " << inDir << std::endl;
return 1;
}
struct stat outInfo;
if (stat(outDir.c_str(), &outInfo) != 0) {
std::cerr << "Failed to stat output directory: " << outDir << std::endl;
return 1;
}
std::cout << "outDir: " << outDir << std::endl;
std::cout << "inDir: " << inDir << std::endl;
std::cout << "outputFormat: " << outputFormat << std::endl;
std::cout << "----------" << std::endl;
DIR* dir = opendir(inDir.c_str());
if (dir == nullptr) {
std::cerr << "Failed to open input directory: " << inDir << std::endl;
return 1;
}
struct dirent* file;
while ((file = readdir(dir)) != nullptr) {
if (file->d_type != DT_REG) {
continue;
}
std::string fileName = file->d_name;
std::string extension = fileName.substr(fileName.find_last_of(".") + 1);
if (extension != "braw") {
continue;
}
std::string inPath = inDir + "/" + fileName;
std::string outPath = outDir + "/" + file->d_name + "." + outputFormat;
std::cout << "processing file " << inPath << std::endl;
CFStringRef clipName = CFStringCreateWithCString( nullptr, inPath.c_str(), kCFStringEncodingUTF8);
if (clipName == nullptr) {
std::cerr << "Failed to create CFString from input path: " << inPath << std::endl;
return 1;
}
IBlackmagicRawFactory* factory = CreateBlackmagicRawFactoryInstanceFromPath(s_brawSdkPath);
if (factory == nullptr) {
std::cerr << "Failed to create IBlackmagicRawFactory!" << std::endl;
return 1;
}
IBlackmagicRaw* codec = nullptr;
HRESULT result = factory->CreateCodec(&codec);
if (result != S_OK) {
std::cerr << "Failed to create IBlackmagicRaw!" << std::endl;
return 1;
}
IBlackmagicRawClip* clip = nullptr;
result = codec->OpenClip(clipName, &clip);
if (result != S_OK) {
std::cerr << "Failed to open IBlackmagicRawClip!" << std::endl;
return 1;
}
s_outputFileName = CFStringCreateWithCString( nullptr, outPath.c_str(), kCFStringEncodingUTF8);
CameraCodecCallback callback;
result = codec->SetCallback(&callback);
if (result != S_OK) {
std::cerr << "Failed to set IBlackmagicRawCallback!" << std::endl;
return 1;
}
IBlackmagicRawJob* readJob = nullptr;
long readTime = 0;
result = clip->CreateJobReadFrame(readTime, &readJob);
if (result != S_OK) {
std::cerr << "Failed to create IBlackmagicRawJob!" << std::endl;
return 1;
}
result = readJob->Submit();
if (result != S_OK) {
std::cerr << "Failed to submit IBlackmagicRawJob!" << std::endl;
return 1;
}
codec->FlushJobs();
if (clip != nullptr)
clip->Release();
if (codec != nullptr)
codec->Release();
if (factory != nullptr)
factory->Release();
CFRelease(clipName);
}
return 0;
}