Oolite
Loading...
Searching...
No Matches
OOSaveEXRSnapshot.cpp
Go to the documentation of this file.
1#include <cstdio>
2#include <cstdlib>
3#include <ctime>
4#include <vector>
5
6extern "C" {
7int SaveEXRSnapshot(const char* outfilename, int width, int height, const float* rgb);
8}
9
10#ifdef WIN32
11#define TINYEXR_IMPLEMENTATION
12#include "tinyexr.h"
13
14int SaveEXRSnapshot(const char* outfilename, int width, int height, const float* rgb)
15{
16 EXRHeader header;
17 InitEXRHeader(&header);
18
19 EXRImage image;
20 InitEXRImage(&image);
21
22 image.num_channels = 3;
23
24 std::vector<float> images[3];
25 images[0].resize(width * height);
26 images[1].resize(width * height);
27 images[2].resize(width * height);
28
29 for (int i = 0; i < width * height; i++) {
30 images[0][i] = rgb[3 * i + 0];
31 images[1][i] = rgb[3 * i + 1];
32 images[2][i] = rgb[3 * i + 2];
33 }
34
35 float* image_ptr[3];
36 image_ptr[0] = &(images[2].at(0)); // B
37 image_ptr[1] = &(images[1].at(0)); // G
38 image_ptr[2] = &(images[0].at(0)); // R
39
40 image.images = (unsigned char**)image_ptr;
41 image.width = width;
42 image.height = height;
43
44 header.num_channels = 3;
46 header.channels = (EXRChannelInfo*)malloc(sizeof(EXRChannelInfo) * header.num_channels);
47
48 // sign our snapshot
49 header.num_custom_attributes = 1;
50 header.custom_attributes = static_cast<EXRAttribute*>(malloc(sizeof(EXRAttribute)));
51 strncpy(header.custom_attributes[0].name, "signature", 255);
52 strncpy(header.custom_attributes[0].type, "string", 255);
53 std::time_t timestamp;
54 std::time(&timestamp);
55 char signatureStringIntro[255] = "Generated by Oolite ";
56 char* signatureString = strcat(signatureStringIntro, std::ctime(&timestamp));
57 // ctime adds a newline at the end of the string, strip it
58 signatureString[strlen(signatureString) - 1] = '\0';
59 header.custom_attributes[0].value = reinterpret_cast<uint8_t*>(signatureString);
60 header.custom_attributes[0].size = strlen(signatureString);
61
62 // Must be BGR(A) order, since most of EXR viewers expect this channel order.
63 strncpy(header.channels[0].name, "B", 255);
64 header.channels[0].name[strlen("B")] = '\0';
65 strncpy(header.channels[1].name, "G", 255);
66 header.channels[1].name[strlen("G")] = '\0';
67 strncpy(header.channels[2].name, "R", 255);
68 header.channels[2].name[strlen("R")] = '\0';
69
70 header.pixel_types = (int*)malloc(sizeof(int) * header.num_channels);
71 header.requested_pixel_types = (int*)malloc(sizeof(int) * header.num_channels);
72 for (int i = 0; i < header.num_channels; i++) {
73 header.pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT; // pixel type of input image
74 header.requested_pixel_types[i] = TINYEXR_PIXELTYPE_HALF; // pixel type of output image to be stored in .EXR
75 }
76
77 const char* err;
78 int ret = SaveEXRImageToFile(&image, &header, outfilename, &err);
79 if (ret != TINYEXR_SUCCESS) {
80 fprintf(stderr, "Save EXR err: %s\n", err);
81 }
82
83 free(header.channels);
84 free(header.pixel_types);
85 free(header.requested_pixel_types);
86
87 return ret;
88}
89#else // Linux EXR snaphsots not supported yet
90int SaveEXRSnapshot(const char* outfilename, int width, int height, const float* rgb)
91{
92 fprintf(stderr, "EXR Snapshot not supported yet under Linux!\n");
93 return 0;
94}
95#endif
int SaveEXRSnapshot(const char *outfilename, int width, int height, const float *rgb)
char type[256]
Definition tinyexr.h:200
char name[256]
Definition tinyexr.h:199
unsigned char * value
Definition tinyexr.h:201
char name[256]
Definition tinyexr.h:207
EXRChannelInfo * channels
Definition tinyexr.h:264
int num_custom_attributes
Definition tinyexr.h:260
int compression_type
Definition tinyexr.h:271
EXRAttribute * custom_attributes
Definition tinyexr.h:261
int num_channels
Definition tinyexr.h:269
int * pixel_types
Definition tinyexr.h:266
int * requested_pixel_types
Definition tinyexr.h:272
int num_channels
Definition tinyexr.h:300
unsigned char ** images
Definition tinyexr.h:296
int width
Definition tinyexr.h:298
int height
Definition tinyexr.h:299
#define TINYEXR_SUCCESS
Definition tinyexr.h:142
struct TEXRImage EXRImage
#define TINYEXR_PIXELTYPE_HALF
Definition tinyexr.h:162
#define TINYEXR_COMPRESSIONTYPE_ZIP
Definition tinyexr.h:171
struct TEXRAttribute EXRAttribute
struct TEXRHeader EXRHeader
void InitEXRHeader(EXRHeader *exr_header)
#define TINYEXR_PIXELTYPE_FLOAT
Definition tinyexr.h:163
struct TEXRChannelInfo EXRChannelInfo
void InitEXRImage(EXRImage *exr_image)
int SaveEXRImageToFile(const EXRImage *image, const EXRHeader *exr_header, const char *filename, const char **err)