Blender V5.3
cineonlib.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 1999-2001 David Hodson <hodsond@acm.org>.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "cineonlib.h"
12#include "logmemfile.h"
13
14#include <cmath>
15#include <cstdio>
16#include <cstdlib>
17#include <cstring>
18#include <ctime>
19#include <sys/types.h>
20
21#include "BLI_fileops.hh"
22#include "BLI_string.hh"
23
24#include "MEM_guardedalloc.h"
25
26namespace blender {
27
28/*
29 * For debug purpose
30 */
31
32static int verbose = 0;
33
34void cineonSetVerbose(int verbosity)
35{
36 verbose = verbosity;
37}
38
40 CineonMainHeader *header,
41 const char *filepath,
42 const char *creator)
43{
44 time_t fileClock;
45 const tm *fileTime;
46 int i;
47
48 memset(header, 0, sizeof(CineonMainHeader));
49
50 /* --- File header --- */
52 header->fileHeader.offset = swap_uint(cineon->element[0].dataOffset, cineon->isMSB);
55 cineon->isMSB);
56 header->fileHeader.ind_hdr_size = 0;
57 header->fileHeader.user_data_size = 0;
59 cineon->element[0].dataOffset +
60 cineon->height * getRowLength(cineon->width, &cineon->element[0]),
61 cineon->isMSB);
62 STRNCPY(header->fileHeader.version, "v4.5");
63 STRNCPY(header->fileHeader.file_name, filepath);
64 fileClock = time(nullptr);
65 fileTime = localtime(&fileClock);
66 strftime(header->fileHeader.creation_date, 12, "%Y:%m:%d", fileTime);
67 strftime(header->fileHeader.creation_time, 12, "%H:%M:%S%Z", fileTime);
68 header->fileHeader.creation_time[11] = 0;
69
70 /* --- Image header --- */
71 header->imageHeader.orientation = 0;
72 header->imageHeader.elements_per_image = cineon->depth;
73
74 for (i = 0; i < 3; i++) {
75 header->imageHeader.element[i].descriptor1 = 0;
76 header->imageHeader.element[i].descriptor2 = i;
78 header->imageHeader.element[i].pixels_per_line = swap_uint(cineon->width, cineon->isMSB);
79 header->imageHeader.element[i].lines_per_image = swap_uint(cineon->height, cineon->isMSB);
81 cineon->isMSB);
83 cineon->isMSB);
85 cineon->isMSB);
87 cineon->element[0].refHighQuantity, cineon->isMSB);
88 }
89
90 header->imageHeader.white_point_x = swap_float(0.0f, cineon->isMSB);
91 header->imageHeader.white_point_y = swap_float(0.0f, cineon->isMSB);
92 header->imageHeader.red_primary_x = swap_float(0.0f, cineon->isMSB);
93 header->imageHeader.red_primary_y = swap_float(0.0f, cineon->isMSB);
94 header->imageHeader.green_primary_x = swap_float(0.0f, cineon->isMSB);
95 header->imageHeader.green_primary_y = swap_float(0.0f, cineon->isMSB);
96 header->imageHeader.blue_primary_x = swap_float(0.0f, cineon->isMSB);
97 header->imageHeader.blue_primary_y = swap_float(0.0f, cineon->isMSB);
98 STRNCPY(header->imageHeader.label, creator);
99 header->imageHeader.interleave = 0;
100 header->imageHeader.data_sign = 0;
101 header->imageHeader.sense = 0;
102 header->imageHeader.line_padding = swap_uint(0, cineon->isMSB);
103 header->imageHeader.element_padding = swap_uint(0, cineon->isMSB);
104
105 switch (cineon->element[0].packing) {
106 case 0:
107 header->imageHeader.packing = 0;
108 break;
109
110 case 1:
111 header->imageHeader.packing = 5;
112 break;
113
114 case 2:
115 header->imageHeader.packing = 6;
116 break;
117 }
118
119 /* --- Origination header --- */
120 /* we leave it blank */
121
122 /* --- Film header --- */
123 /* we leave it blank */
124}
125
126LogImageFile *cineonOpen(const uchar *buffer, size_t bufferSize)
127{
128 CineonMainHeader header;
130 int i;
131 uint dataOffset;
132
133 if (cineon == nullptr) {
134 if (verbose) {
135 printf("Cineon: Failed to malloc cineon file structure.\n");
136 }
137 return nullptr;
138 }
139
140 /* zero the header */
141 memset(&header, 0, sizeof(CineonMainHeader));
142
143 /* for close routine */
144 cineon->file = nullptr;
145
146 cineon->memBuffer = const_cast<uchar *>(buffer);
147 cineon->memCursor = const_cast<uchar *>(buffer);
148 cineon->memBufferSize = bufferSize;
149
150 if (logimage_fread(&header, sizeof(header), 1, cineon) == 0) {
151 if (verbose) {
152 printf("Cineon: Not enough data for header.\n");
153 }
154 logImageClose(cineon);
155 return nullptr;
156 }
157
158 /* endianness determination */
160 cineon->isMSB = 1;
161 if (verbose) {
162 printf("Cineon: File is MSB.\n");
163 }
164 }
165 else if (header.fileHeader.magic_num == CINEON_FILE_MAGIC) {
166 cineon->isMSB = 0;
167 if (verbose) {
168 printf("Cineon: File is LSB.\n");
169 }
170 }
171 else {
172 if (verbose) {
173 printf("Cineon: Bad magic number %lu.\n", ulong(header.fileHeader.magic_num));
174 }
175 logImageClose(cineon);
176 return nullptr;
177 }
178
179 cineon->width = swap_uint(header.imageHeader.element[0].pixels_per_line, cineon->isMSB);
180 cineon->height = swap_uint(header.imageHeader.element[0].lines_per_image, cineon->isMSB);
181
182 if (cineon->width == 0 || cineon->height == 0) {
183 if (verbose) {
184 printf("Cineon: Wrong image dimension: %dx%d\n", cineon->width, cineon->height);
185 }
186 logImageClose(cineon);
187 return nullptr;
188 }
189
190 cineon->depth = header.imageHeader.elements_per_image;
191
192 if (header.imageHeader.interleave == 0) {
193 cineon->numElements = 1;
194 }
195 else if (header.imageHeader.interleave == 2) {
197 }
198 else {
199 if (verbose) {
200 printf("Cineon: Data interleave not supported: %d\n", header.imageHeader.interleave);
201 }
202 logImageClose(cineon);
203 return nullptr;
204 }
205
206 if (cineon->depth == 1) {
207 /* Gray-scale image. */
209 cineon->element[0].transfer = transfer_Linear;
210 cineon->element[0].depth = 1;
211 }
212 else if (cineon->depth == 3) {
213 /* RGB image. */
214 if (cineon->numElements == 1) {
215 cineon->element[0].descriptor = descriptor_RGB;
217 cineon->element[0].depth = 3;
218 }
219 else if (cineon->numElements == 3) {
220 cineon->element[0].descriptor = descriptor_Red;
222 cineon->element[0].depth = 1;
225 cineon->element[1].depth = 1;
228 cineon->element[2].depth = 1;
229 }
230 }
231 else {
232 if (verbose) {
233 printf("Cineon: Cineon image depth unsupported: %d\n", cineon->depth);
234 }
235 logImageClose(cineon);
236 return nullptr;
237 }
238
239 dataOffset = swap_uint(header.fileHeader.offset, cineon->isMSB);
240
241 for (i = 0; i < cineon->numElements; i++) {
243 cineon->element[i].maxValue = powf(2, cineon->element[i].bitsPerSample) - 1.0f;
245 cineon->isMSB);
247 cineon->isMSB);
249 cineon->isMSB);
251 header.imageHeader.element[i].ref_high_quantity, cineon->isMSB);
252
253 switch (header.imageHeader.packing) {
254 case 0:
255 cineon->element[i].packing = 0;
256 break;
257
258 case 5:
259 cineon->element[i].packing = 1;
260 break;
261
262 case 6:
263 cineon->element[i].packing = 2;
264 break;
265
266 default:
267 /* Not supported */
268 if (verbose) {
269 printf("Cineon: packing unsupported: %d\n", header.imageHeader.packing);
270 }
271 logImageClose(cineon);
272 return nullptr;
273 }
274
275 if (cineon->element[i].refLowData == CINEON_UNDEFINED_U32) {
276 cineon->element[i].refLowData = 0;
277 }
278
279 if (cineon->element[i].refHighData == CINEON_UNDEFINED_U32) {
280 cineon->element[i].refHighData = uint(cineon->element[i].maxValue);
281 }
282
283 if (cineon->element[i].refLowQuantity == CINEON_UNDEFINED_R32 ||
284 std::isnan(cineon->element[i].refLowQuantity))
285 {
286 cineon->element[i].refLowQuantity = 0.0f;
287 }
288
289 if (cineon->element[i].refHighQuantity == CINEON_UNDEFINED_R32 ||
290 std::isnan(cineon->element[i].refHighQuantity))
291 {
292 if (cineon->element[i].transfer == transfer_PrintingDensity) {
293 cineon->element[i].refHighQuantity = 2.048f;
294 }
295 else {
296 cineon->element[i].refHighQuantity = cineon->element[i].maxValue;
297 }
298 }
299
300 cineon->element[i].dataOffset = dataOffset;
301 dataOffset += cineon->height * getRowLength(cineon->width, &cineon->element[i]);
302 }
303
304 cineon->referenceBlack = 95.0f / 1023.0f * cineon->element[0].maxValue;
305 cineon->referenceWhite = 685.0f / 1023.0f * cineon->element[0].maxValue;
306 cineon->gamma = 1.7f;
307
308 if (verbose) {
309 printf("size %d x %d x %d elements\n", cineon->width, cineon->height, cineon->numElements);
310 for (i = 0; i < cineon->numElements; i++) {
311 printf(" Element %d:\n", i);
312 printf(" Bits per sample: %d\n", cineon->element[i].bitsPerSample);
313 printf(" Depth: %d\n", cineon->element[i].depth);
314 printf(" Transfer characteristics: %d\n", cineon->element[i].transfer);
315 printf(" Packing: %d\n", cineon->element[i].packing);
316 printf(" Descriptor: %d\n", cineon->element[i].descriptor);
317 printf(" Data offset: %d\n", cineon->element[i].dataOffset);
318 printf(" Reference low data: %u\n", cineon->element[i].refLowData);
319 printf(" Reference low quantity: %f\n", cineon->element[i].refLowQuantity);
320 printf(" Reference high data: %u\n", cineon->element[i].refHighData);
321 printf(" Reference high quantity: %f\n", cineon->element[i].refHighQuantity);
322 printf("\n");
323 }
324
325 printf("Gamma: %f\n", cineon->gamma);
326 printf("Reference black: %f\n", cineon->referenceBlack);
327 printf("Reference white: %f\n", cineon->referenceWhite);
328 printf("Orientation: %d\n", header.imageHeader.orientation);
329 printf("----------------------------\n");
330 }
331 return cineon;
332}
333
334LogImageFile *cineonCreate(const char *filepath, int width, int height, const char *creator)
335{
336 CineonMainHeader header;
337 const char *shortFilename = nullptr;
338 // uchar pad[6044];
339
341 if (cineon == nullptr) {
342 if (verbose) {
343 printf("cineon: Failed to malloc cineon file structure.\n");
344 }
345 return nullptr;
346 }
347
348 /* Only 10 bits Cineon are supported */
349 cineon->width = width;
350 cineon->height = height;
351 cineon->element[0].bitsPerSample = 10;
352 cineon->element[0].dataOffset = sizeof(CineonMainHeader);
353 cineon->element[0].maxValue = 1023;
354 cineon->isMSB = 1;
355 cineon->numElements = 1;
356 cineon->element[0].packing = 1;
357 cineon->depth = 3;
358 cineon->element[0].depth = 3;
359 cineon->element[0].descriptor = descriptor_RGB;
361 cineon->element[0].refHighQuantity = 2.048f;
362 cineon->element[0].refLowQuantity = 0;
363 cineon->element[0].refLowData = 0;
364 cineon->element[0].refHighData = cineon->element[0].maxValue;
365 cineon->referenceWhite = 685.0f;
366 cineon->referenceBlack = 95.0f;
367 cineon->gamma = 1.7f;
368
369 shortFilename = strrchr(filepath, PATHSEP_CHAR);
370 if (shortFilename == nullptr) {
371 shortFilename = filepath;
372 }
373 else {
374 shortFilename++;
375 }
376
377 cineon->file = BLI_fopen(filepath, "wb");
378 if (cineon->file == nullptr) {
379 if (verbose) {
380 printf("cineon: Couldn't open file %s\n", filepath);
381 }
382 logImageClose(cineon);
383 return nullptr;
384 }
385
386 fillCineonMainHeader(cineon, &header, shortFilename, creator);
387
388 if (fwrite(&header, sizeof(header), 1, cineon->file) == 0) {
389 if (verbose) {
390 printf("cineon: Couldn't write image header\n");
391 }
392 logImageClose(cineon);
393 return nullptr;
394 }
395
396 return cineon;
397}
398
399} // namespace blender
File and directory operations.
Read Blender memory allocation and freeing.
#define CINEON_FILE_MAGIC
Definition cineonlib.h:17
#define CINEON_UNDEFINED_U32
Definition cineonlib.h:20
#define CINEON_UNDEFINED_R32
Definition cineonlib.h:21
#define powf(x, y)
#define PATHSEP_CHAR
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
size_t getRowLength(size_t width, const LogImageElement *logElement)
char * STRNCPY(char(&dst)[N], const char *src)
unsigned int uint
Definition BKE_text.h:55
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
unsigned long ulong
Definition BKE_text.h:57
void logImageClose(LogImageFile *logImage)
BLI_INLINE unsigned int swap_uint(unsigned int x, int swap)
void cineonSetVerbose(int verbosity)
Definition cineonlib.cc:34
unsigned char uchar
Definition BKE_text.h:58
LogImageFile * cineonOpen(const uchar *buffer, size_t bufferSize)
Definition cineonlib.cc:126
@ descriptor_Red
@ descriptor_Green
@ descriptor_Blue
@ descriptor_RGB
@ descriptor_Luminance
@ transfer_PrintingDensity
@ transfer_Linear
int logimage_fread(void *buffer, size_t size, uint count, LogImageFile *logFile)
Definition logmemfile.cc:34
static int verbose
Definition cineonlib.cc:32
BLI_INLINE float swap_float(float x, int swap)
static void fillCineonMainHeader(LogImageFile *cineon, CineonMainHeader *header, const char *filepath, const char *creator)
Definition cineonlib.cc:39
LogImageFile * cineonCreate(const char *filepath, int width, int height, const char *creator)
Definition cineonlib.cc:334
unsigned int gen_hdr_size
Definition cineonlib.h:27
unsigned int user_data_size
Definition cineonlib.h:29
unsigned int ind_hdr_size
Definition cineonlib.h:28
unsigned int line_padding
Definition cineonlib.h:70
unsigned int element_padding
Definition cineonlib.h:71
CineonElementHeader element[8]
Definition cineonlib.h:55
CineonImageHeader imageHeader
Definition cineonlib.h:107
CineonFileHeader fileHeader
Definition cineonlib.h:106
unsigned char * memCursor
LogImageElement element[8]
unsigned char * memBuffer