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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
/*
* Dpx image file format library routines.
*
* Copyright 1999 - 2002 David Hodson <hodsond@acm.org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributor(s): Julien Enche.
*
*/
/** \file blender/imbuf/intern/cineon/dpxlib.c
* \ingroup imbcineon
*/
#include "dpxlib.h"
#include "logmemfile.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <string.h>
#include "BLI_fileops.h"
#include "MEM_guardedalloc.h"
/*
* For debug purpose
*/
static int verbose = 0;
void dpxSetVerbose(int verbosity) {
verbose = verbosity;
}
/*
* Headers
*/
static void fillDpxMainHeader(LogImageFile *dpx, DpxMainHeader *header, const char *filename, const char *creator)
{
time_t fileClock;
struct tm *fileTime;
memset(header, 0, sizeof(DpxMainHeader));
/* --- File header --- */
header->fileHeader.magic_num = swap_uint(DPX_FILE_MAGIC, dpx->isMSB);
header->fileHeader.offset = swap_uint(dpx->element[0].dataOffset, dpx->isMSB);
strcpy(header->fileHeader.version, "v2.0");
header->fileHeader.file_size = swap_uint(dpx->element[0].dataOffset + dpx->height * getRowLength(dpx->width, dpx->element[0]), dpx->isMSB);
header->fileHeader.ditto_key = 0;
header->fileHeader.gen_hdr_size = swap_uint(sizeof(DpxFileHeader) + sizeof(DpxImageHeader) + sizeof(DpxOrientationHeader), dpx->isMSB);
header->fileHeader.ind_hdr_size = swap_uint(sizeof(DpxFilmHeader) + sizeof(DpxTelevisionHeader), dpx->isMSB);
header->fileHeader.user_data_size = DPX_UNDEFINED_U32;
strncpy(header->fileHeader.file_name, filename, 99);
header->fileHeader.file_name[99] = 0;
fileClock = time(0);
fileTime = localtime(&fileClock);
strftime(header->fileHeader.creation_date, 24, "%Y:%m:%d:%H:%M:%S%Z", fileTime);
header->fileHeader.creation_date[23] = 0;
strncpy(header->fileHeader.creator, creator, 99);
header->fileHeader.creator[99] = 0;
header->fileHeader.project[0] = 0;
header->fileHeader.copyright[0] = 0;
header->fileHeader.key = 0xFFFFFFFF;
/* --- Image header --- */
header->imageHeader.orientation = 0;
header->imageHeader.elements_per_image = swap_ushort(1, dpx->isMSB);
header->imageHeader.pixels_per_line = swap_uint(dpx->width, dpx->isMSB);
header->imageHeader.lines_per_element = swap_uint(dpx->height, dpx->isMSB);
/* Fills element */
header->imageHeader.element[0].data_sign = 0;
header->imageHeader.element[0].ref_low_data = swap_uint(dpx->element[0].refLowData, dpx->isMSB);
header->imageHeader.element[0].ref_low_quantity = swap_float(dpx->element[0].refLowQuantity, dpx->isMSB);
header->imageHeader.element[0].ref_high_data = swap_uint(dpx->element[0].refHighData, dpx->isMSB);
header->imageHeader.element[0].ref_high_quantity = swap_float(dpx->element[0].refHighQuantity, dpx->isMSB);
header->imageHeader.element[0].descriptor = dpx->element[0].descriptor;
header->imageHeader.element[0].transfer = dpx->element[0].transfer;
header->imageHeader.element[0].colorimetric = 0;
header->imageHeader.element[0].bits_per_sample = dpx->element[0].bitsPerSample;
header->imageHeader.element[0].packing = swap_ushort(dpx->element[0].packing, dpx->isMSB);
header->imageHeader.element[0].encoding = 0;
header->imageHeader.element[0].data_offset = swap_uint(dpx->element[0].dataOffset, dpx->isMSB);
header->imageHeader.element[0].line_padding = 0;
header->imageHeader.element[0].element_padding = 0;
header->imageHeader.element[0].description[0] = 0;
/* --- Orientation header --- */
/* we leave it blank */
/* --- Television header --- */
header->televisionHeader.time_code = DPX_UNDEFINED_U32;
header->televisionHeader.user_bits = DPX_UNDEFINED_U32;
header->televisionHeader.interlace = DPX_UNDEFINED_U8;
header->televisionHeader.field_number = DPX_UNDEFINED_U8;
header->televisionHeader.video_signal = DPX_UNDEFINED_U8;
header->televisionHeader.padding = DPX_UNDEFINED_U8;
header->televisionHeader.horizontal_sample_rate = DPX_UNDEFINED_R32;
header->televisionHeader.vertical_sample_rate = DPX_UNDEFINED_R32;
header->televisionHeader.frame_rate = DPX_UNDEFINED_R32;
header->televisionHeader.time_offset = DPX_UNDEFINED_R32;
header->televisionHeader.gamma = swap_float(dpx->gamma, dpx->isMSB);
header->televisionHeader.black_level = swap_float(dpx->referenceBlack, dpx->isMSB);
header->televisionHeader.black_gain = DPX_UNDEFINED_R32;
header->televisionHeader.breakpoint = DPX_UNDEFINED_R32;
header->televisionHeader.white_level = swap_float(dpx->referenceWhite, dpx->isMSB);
header->televisionHeader.integration_times = DPX_UNDEFINED_R32;
}
LogImageFile *dpxOpen(const unsigned char *byteStuff, int fromMemory, size_t bufferSize)
{
DpxMainHeader header;
LogImageFile *dpx = (LogImageFile*)MEM_mallocN(sizeof(LogImageFile), __func__);
char *filename = (char*)byteStuff;
int i;
if (dpx == 0) {
if (verbose) printf("DPX: Failed to malloc dpx file structure.\n");
return 0;
}
/* zero the header */
memset(&header, 0, sizeof(DpxMainHeader));
/* for close routine */
dpx->file = 0;
if (fromMemory == 0) {
/* byteStuff is then the filename */
dpx->file = BLI_fopen(filename, "rb");
if (dpx->file == 0) {
if (verbose) printf("DPX: Failed to open file \"%s\".\n", filename);
logImageClose(dpx);
return 0;
}
/* not used in this case */
dpx->memBuffer = 0;
dpx->memCursor = 0;
dpx->memBufferSize = 0;
} else {
dpx->memBuffer = (unsigned char*)byteStuff;
dpx->memCursor = (unsigned char*)byteStuff;
dpx->memBufferSize = bufferSize;
}
if (logimage_fread(&header, sizeof(header), 1, dpx) == 0) {
if (verbose) printf("DPX: Not enough data for header in \"%s\".\n", byteStuff);
logImageClose(dpx);
return 0;
}
/* endianness determination */
if (header.fileHeader.magic_num == swap_uint(DPX_FILE_MAGIC, 1)) {
dpx->isMSB = 1;
if (verbose) printf("DPX: File is MSB.\n");
} else if (header.fileHeader.magic_num == DPX_FILE_MAGIC) {
dpx->isMSB = 0;
if (verbose) printf("DPX: File is LSB.\n");
} else {
if (verbose) printf("DPX: Bad magic number %lu in \"%s\".\n",
(uintptr_t)header.fileHeader.magic_num, byteStuff);
logImageClose(dpx);
return 0;
}
dpx->srcFormat = format_DPX;
dpx->numElements = swap_ushort(header.imageHeader.elements_per_image, dpx->isMSB);
dpx->width = swap_uint(header.imageHeader.pixels_per_line, dpx->isMSB);
dpx->height = swap_uint(header.imageHeader.lines_per_element, dpx->isMSB);
dpx->depth = 0;
for (i = 0; i < dpx->numElements; i++) {
dpx->element[i].descriptor = header.imageHeader.element[i].descriptor;
switch (dpx->element[i].descriptor) {
case descriptor_Red:
case descriptor_Green:
case descriptor_Blue:
case descriptor_Alpha:
case descriptor_Luminance:
case descriptor_Chrominance:
dpx->depth++;
dpx->element[i].depth = 1;
break;
case descriptor_CbYCrY:
dpx->depth += 2;
dpx->element[i].depth = 2;
break;
case descriptor_RGB:
case descriptor_CbYCr:
case descriptor_CbYACrYA:
dpx->depth += 3;
dpx->element[i].depth = 3;
break;
case descriptor_RGBA:
case descriptor_ABGR:
case descriptor_CbYCrA:
dpx->depth += 4;
dpx->element[i].depth = 4;
break;
case descriptor_Depth:
case descriptor_Composite:
/* unsupported */
break;
}
if (dpx->depth == 0 || dpx->depth > 4) {
if (verbose) printf("DPX: Unsupported image depth: %d\n", dpx->depth);
logImageClose(dpx);
return 0;
}
dpx->element[i].bitsPerSample = header.imageHeader.element[i].bits_per_sample;
dpx->element[i].maxValue = powf(2, dpx->element[i].bitsPerSample) - 1.0f;
dpx->element[i].packing = swap_ushort(header.imageHeader.element[i].packing, dpx->isMSB);
/* Sometimes, the offset is not set correctly in the header */
dpx->element[i].dataOffset = swap_uint(header.imageHeader.element[i].data_offset, dpx->isMSB);
if (dpx->element[i].dataOffset == 0 && dpx->numElements == 1)
dpx->element[i].dataOffset = swap_uint(header.fileHeader.offset, dpx->isMSB);
if (dpx->element[i].dataOffset == 0) {
if (verbose) printf("DPX: Image header is corrupted.\n");
logImageClose(dpx);
return 0;
}
dpx->element[i].transfer = header.imageHeader.element[i].transfer;
/* if undefined, assign default */
dpx->element[i].refLowData = swap_uint(header.imageHeader.element[i].ref_low_data, dpx->isMSB);
dpx->element[i].refLowQuantity = swap_float(header.imageHeader.element[i].ref_low_quantity, dpx->isMSB);
dpx->element[i].refHighData = swap_uint(header.imageHeader.element[i].ref_high_data, dpx->isMSB);
dpx->element[i].refHighQuantity = swap_float(header.imageHeader.element[i].ref_high_quantity, dpx->isMSB);
switch (dpx->element[i].descriptor) {
case descriptor_Red:
case descriptor_Green:
case descriptor_Blue:
case descriptor_Alpha:
case descriptor_RGB:
case descriptor_RGBA:
case descriptor_ABGR:
if (dpx->element[i].refLowData == DPX_UNDEFINED_U32 || isnan(dpx->element[i].refLowData))
dpx->element[i].refLowData = 0;
if (dpx->element[i].refHighData == DPX_UNDEFINED_U32 || isnan(dpx->element[i].refHighData))
dpx->element[i].refHighData = (unsigned int)dpx->element[i].maxValue;
if(dpx->element[i].refLowQuantity == DPX_UNDEFINED_R32 || isnan(dpx->element[i].refLowQuantity))
dpx->element[i].refLowQuantity = 0.0f;
if(dpx->element[i].refHighQuantity == DPX_UNDEFINED_R32 || isnan(dpx->element[i].refHighQuantity)) {
if (dpx->element[i].transfer == transfer_PrintingDensity || dpx->element[i].transfer == transfer_Logarithmic)
dpx->element[i].refHighQuantity = 2.048f;
else
dpx->element[i].refHighQuantity = dpx->element[i].maxValue;
}
break;
case descriptor_Luminance:
case descriptor_Chrominance:
case descriptor_CbYCrY:
case descriptor_CbYCr:
case descriptor_CbYACrYA:
case descriptor_CbYCrA:
if (dpx->element[i].refLowData == DPX_UNDEFINED_U32 || isnan(dpx->element[i].refLowData))
dpx->element[i].refLowData = 16.0f / 255.0f * dpx->element[i].maxValue;
if (dpx->element[i].refHighData == DPX_UNDEFINED_U32 || isnan(dpx->element[i].refHighData))
dpx->element[i].refHighData = 235.0f / 255.0f * dpx->element[i].maxValue;
if(dpx->element[i].refLowQuantity == DPX_UNDEFINED_R32 || isnan(dpx->element[i].refLowQuantity))
dpx->element[i].refLowQuantity = 0.0f;
if(dpx->element[i].refHighQuantity == DPX_UNDEFINED_R32 || isnan(dpx->element[i].refHighQuantity))
dpx->element[i].refHighQuantity = 0.7f;
break;
default:
break;
}
}
dpx->referenceBlack = swap_float(header.televisionHeader.black_level, dpx->isMSB);
dpx->referenceWhite = swap_float(header.televisionHeader.white_level, dpx->isMSB);
dpx->gamma = swap_float(header.televisionHeader.gamma, dpx->isMSB);
if ((dpx->referenceBlack == DPX_UNDEFINED_R32 || isnan(dpx->referenceBlack)) ||
(dpx->referenceWhite == DPX_UNDEFINED_R32 || dpx->referenceWhite <= dpx->referenceBlack || isnan(dpx->referenceWhite)) ||
(dpx->gamma == DPX_UNDEFINED_R32 || dpx->gamma <= 0 || isnan(dpx->gamma)))
{
dpx->referenceBlack = 95.0f / 1023.0f * dpx->element[0].maxValue;
dpx->referenceWhite = 685.0f / 1023.0f * dpx->element[0].maxValue;
dpx->gamma = 1.7f;
}
if (verbose) {
printf("size %d x %d x %d elements\n", dpx->width, dpx->height, dpx->numElements);
for (i = 0; i < dpx->numElements; i++) {
printf(" Element %d:\n", i);
printf(" Bits per sample: %d\n", dpx->element[i].bitsPerSample);
printf(" Depth: %d\n", dpx->element[i].depth);
printf(" Transfer characteristics: %d\n", dpx->element[i].transfer);
printf(" Packing: %d\n", dpx->element[i].packing);
printf(" Descriptor: %d\n", dpx->element[i].descriptor);
printf(" Data offset: %u\n", dpx->element[i].dataOffset);
printf(" Reference low data: %u\n", dpx->element[i].refLowData);
printf(" Reference low quantity: %f\n", dpx->element[i].refLowQuantity);
printf(" Reference high data: %u\n", dpx->element[i].refHighData);
printf(" Reference high quantity: %f\n", dpx->element[i].refHighQuantity);
printf("\n");
}
printf("Gamma: %f\n", dpx->gamma);
printf("Reference black: %f\n", dpx->referenceBlack);
printf("Reference white: %f\n", dpx->referenceWhite);
printf("----------------------------\n");
}
return dpx;
}
LogImageFile *dpxCreate(const char *filename, int width, int height, int bitsPerSample, int hasAlpha,
int isLogarithmic, int referenceWhite, int referenceBlack, float gamma,
const char *creator)
{
DpxMainHeader header;
const char *shortFilename = 0;
unsigned char pad[6044];
LogImageFile *dpx = (LogImageFile*)MEM_mallocN(sizeof(LogImageFile), __func__);
if (dpx == 0) {
if (verbose) printf("DPX: Failed to malloc dpx file structure.\n");
return 0;
}
dpx->width = width;
dpx->height = height;
dpx->element[0].bitsPerSample = bitsPerSample;
dpx->element[0].dataOffset = 8092;
dpx->element[0].maxValue = powf(2, dpx->element[0].bitsPerSample) - 1.0f;
dpx->isMSB = 1;
dpx->numElements = 1;
switch (bitsPerSample) {
case 8:
case 16:
dpx->element[0].packing = 0;
break;
case 10:
case 12:
/* Packed Type A padding is the most common 10/12 bits format */
dpx->element[0].packing = 1;
break;
default:
if (verbose) printf("DPX: bitsPerSample not supported: %d\n", bitsPerSample);
logImageClose(dpx);
return 0;
}
if (hasAlpha == 0) {
dpx->depth = 3;
dpx->element[0].depth = 3;
dpx->element[0].descriptor = descriptor_RGB;
}
else {
dpx->depth = 4;
dpx->element[0].depth = 4;
dpx->element[0].descriptor = descriptor_RGBA;
}
if (isLogarithmic == 0) {
dpx->element[0].transfer = transfer_Linear;
dpx->element[0].refHighQuantity = dpx->element[0].maxValue;
}
else {
dpx->element[0].transfer = transfer_PrintingDensity;
dpx->element[0].refHighQuantity = 2.048f;
}
dpx->element[0].refLowQuantity = 0;
dpx->element[0].refLowData = 0;
dpx->element[0].refHighData = dpx->element[0].maxValue;
if (referenceWhite > 0)
dpx->referenceWhite = referenceWhite;
else
dpx->referenceWhite = 685.0f / 1023.0f * dpx->element[0].maxValue;
if (referenceBlack > 0)
dpx->referenceBlack = referenceBlack;
else
dpx->referenceBlack = 95.0f / 1023.0f * dpx->element[0].maxValue;
if (gamma > 0.0f)
dpx->gamma = gamma;
else
dpx->gamma = 1.7f;
shortFilename = strrchr(filename, '/');
if (shortFilename == 0)
shortFilename = filename;
else
shortFilename++;
dpx->file = BLI_fopen(filename, "wb");
if (dpx->file == 0) {
if (verbose) printf("DPX: Couldn't open file %s\n", filename);
logImageClose(dpx);
return 0;
}
fillDpxMainHeader(dpx, &header, shortFilename, creator);
if (fwrite(&header, sizeof(header), 1, dpx->file) == 0) {
if (verbose) printf("DPX: Couldn't write image header\n");
logImageClose(dpx);
return 0;
}
/* Header should be rounded to next 8k block
6044 = 8092 - sizeof(DpxMainHeader) */
memset(&pad, 0, 6044);
if (fwrite(&pad, 6044, 1, dpx->file) == 0) {
if (verbose) printf("DPX: Couldn't write image header\n");
logImageClose(dpx);
return 0;
}
return dpx;
}
|