Added GPL header to sconscripts!
[blender.git] / source / blender / imbuf / intern / cineon / dpxlib.c
1 /*
2  * Dpx image file format library routines.
3  *
4  * Copyright 1999 - 2002 David Hodson <hodsond@acm.org>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 2 of the License, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): Julien Enche.
21  *
22  */
23
24 /** \file blender/imbuf/intern/cineon/dpxlib.c
25  *  \ingroup imbcineon
26  */
27
28
29 #include "dpxlib.h"
30 #include "logmemfile.h"
31
32 #include <stdio.h>
33 #include <math.h>
34 #include <stdlib.h>
35 #include <time.h>
36 #include <sys/types.h>
37 #include <string.h>
38
39 #include "BLI_fileops.h"
40 #include "BLI_math_base.h"
41 #include "BLI_utildefines.h"
42
43 #include "MEM_guardedalloc.h"
44
45 /*
46  * For debug purpose
47  */
48
49 static int verbose = 0;
50
51 void dpxSetVerbose(int verbosity) {
52         verbose = verbosity;
53 }
54
55
56 /*
57  * Headers
58  */
59
60 static void fillDpxMainHeader(LogImageFile *dpx, DpxMainHeader *header, const char *filename, const char *creator)
61 {
62         time_t fileClock;
63         struct tm *fileTime;
64
65         memset(header, 0, sizeof(DpxMainHeader));
66
67         /* --- File header --- */
68         header->fileHeader.magic_num = swap_uint(DPX_FILE_MAGIC, dpx->isMSB);
69         header->fileHeader.offset = swap_uint(dpx->element[0].dataOffset, dpx->isMSB);
70         strcpy(header->fileHeader.version, "v2.0");
71         header->fileHeader.file_size = swap_uint(dpx->element[0].dataOffset + dpx->height * getRowLength(dpx->width, dpx->element[0]), dpx->isMSB);
72         header->fileHeader.ditto_key = 0;
73         header->fileHeader.gen_hdr_size = swap_uint(sizeof(DpxFileHeader) + sizeof(DpxImageHeader) + sizeof(DpxOrientationHeader), dpx->isMSB);
74         header->fileHeader.ind_hdr_size = swap_uint(sizeof(DpxFilmHeader) + sizeof(DpxTelevisionHeader), dpx->isMSB);
75         header->fileHeader.user_data_size = DPX_UNDEFINED_U32;
76         strncpy(header->fileHeader.file_name, filename, 99);
77         header->fileHeader.file_name[99] = 0;
78         fileClock = time(0);
79         fileTime = localtime(&fileClock);
80         strftime(header->fileHeader.creation_date, 24, "%Y:%m:%d:%H:%M:%S%Z", fileTime);
81         header->fileHeader.creation_date[23] = 0;
82         strncpy(header->fileHeader.creator, creator, 99);
83         header->fileHeader.creator[99] = 0;
84         header->fileHeader.project[0] = 0;
85         header->fileHeader.copyright[0] = 0;
86         header->fileHeader.key = 0xFFFFFFFF;
87
88         /* --- Image header --- */
89         header->imageHeader.orientation = 0;
90         header->imageHeader.elements_per_image = swap_ushort(1, dpx->isMSB);
91         header->imageHeader.pixels_per_line = swap_uint(dpx->width, dpx->isMSB);
92         header->imageHeader.lines_per_element = swap_uint(dpx->height, dpx->isMSB);
93
94         /* Fills element */
95         header->imageHeader.element[0].data_sign = 0;
96         header->imageHeader.element[0].ref_low_data = swap_uint(dpx->element[0].refLowData, dpx->isMSB);
97         header->imageHeader.element[0].ref_low_quantity = swap_float(dpx->element[0].refLowQuantity, dpx->isMSB);
98         header->imageHeader.element[0].ref_high_data = swap_uint(dpx->element[0].refHighData, dpx->isMSB);
99         header->imageHeader.element[0].ref_high_quantity = swap_float(dpx->element[0].refHighQuantity, dpx->isMSB);
100         header->imageHeader.element[0].descriptor = dpx->element[0].descriptor;
101         header->imageHeader.element[0].transfer = dpx->element[0].transfer;
102         header->imageHeader.element[0].colorimetric = 0;
103         header->imageHeader.element[0].bits_per_sample = dpx->element[0].bitsPerSample;
104         header->imageHeader.element[0].packing = swap_ushort(dpx->element[0].packing, dpx->isMSB);
105         header->imageHeader.element[0].encoding = 0;
106         header->imageHeader.element[0].data_offset = swap_uint(dpx->element[0].dataOffset, dpx->isMSB);
107         header->imageHeader.element[0].line_padding = 0;
108         header->imageHeader.element[0].element_padding = 0;
109         header->imageHeader.element[0].description[0] = 0;
110
111         /* --- Orientation header --- */
112         /* we leave it blank */
113
114         /* --- Television header --- */
115         header->televisionHeader.time_code = DPX_UNDEFINED_U32;
116         header->televisionHeader.user_bits = DPX_UNDEFINED_U32;
117         header->televisionHeader.interlace = DPX_UNDEFINED_U8;
118         header->televisionHeader.field_number = DPX_UNDEFINED_U8;
119         header->televisionHeader.video_signal = DPX_UNDEFINED_U8;
120         header->televisionHeader.padding = DPX_UNDEFINED_U8;
121         header->televisionHeader.horizontal_sample_rate = DPX_UNDEFINED_R32;
122         header->televisionHeader.vertical_sample_rate = DPX_UNDEFINED_R32;
123         header->televisionHeader.frame_rate = DPX_UNDEFINED_R32;
124         header->televisionHeader.time_offset = DPX_UNDEFINED_R32;
125         header->televisionHeader.gamma = swap_float(dpx->gamma, dpx->isMSB);
126         header->televisionHeader.black_level = swap_float(dpx->referenceBlack, dpx->isMSB);
127         header->televisionHeader.black_gain = DPX_UNDEFINED_R32;
128         header->televisionHeader.breakpoint = DPX_UNDEFINED_R32;
129         header->televisionHeader.white_level = swap_float(dpx->referenceWhite, dpx->isMSB);
130         header->televisionHeader.integration_times = DPX_UNDEFINED_R32;
131 }
132
133 LogImageFile *dpxOpen(const unsigned char *byteStuff, int fromMemory, size_t bufferSize)
134 {
135         DpxMainHeader header;
136         LogImageFile *dpx = (LogImageFile *)MEM_mallocN(sizeof(LogImageFile), __func__);
137         char *filename = (char *)byteStuff;
138         int i;
139
140         if (dpx == 0) {
141                 if (verbose) printf("DPX: Failed to malloc dpx file structure.\n");
142                 return 0;
143         }
144
145         /* zero the header */
146         memset(&header, 0, sizeof(DpxMainHeader));
147
148         /* for close routine */
149         dpx->file = 0;
150
151         if (fromMemory == 0) {
152                 /* byteStuff is then the filename */
153                 dpx->file = BLI_fopen(filename, "rb");
154                 if (dpx->file == 0) {
155                         if (verbose) printf("DPX: Failed to open file \"%s\".\n", filename);
156                         logImageClose(dpx);
157                         return 0;
158                 }
159                 /* not used in this case */
160                 dpx->memBuffer = 0;
161                 dpx->memCursor = 0;
162                 dpx->memBufferSize = 0;
163         }
164         else {
165                 dpx->memBuffer = (unsigned char *)byteStuff;
166                 dpx->memCursor = (unsigned char *)byteStuff;
167                 dpx->memBufferSize = bufferSize;
168         }
169
170         if (logimage_fread(&header, sizeof(header), 1, dpx) == 0) {
171                 if (verbose) printf("DPX: Not enough data for header in \"%s\".\n", byteStuff);
172                 logImageClose(dpx);
173                 return 0;
174         }
175
176         /* endianness determination */
177         if (header.fileHeader.magic_num == swap_uint(DPX_FILE_MAGIC, 1)) {
178                 dpx->isMSB = 1;
179                 if (verbose) printf("DPX: File is MSB.\n");
180         }
181         else if (header.fileHeader.magic_num == DPX_FILE_MAGIC) {
182                 dpx->isMSB = 0;
183                 if (verbose) printf("DPX: File is LSB.\n");
184         }
185         else {
186                 if (verbose) printf("DPX: Bad magic number %lu in \"%s\".\n",
187                                     (uintptr_t)header.fileHeader.magic_num, byteStuff);
188                 logImageClose(dpx);
189                 return 0;
190         }
191
192         dpx->srcFormat = format_DPX;
193         dpx->numElements = swap_ushort(header.imageHeader.elements_per_image, dpx->isMSB);
194         dpx->width = swap_uint(header.imageHeader.pixels_per_line, dpx->isMSB);
195         dpx->height = swap_uint(header.imageHeader.lines_per_element, dpx->isMSB);
196         dpx->depth = 0;
197
198         for (i = 0; i < dpx->numElements; i++) {
199                 dpx->element[i].descriptor = header.imageHeader.element[i].descriptor;
200
201                 switch (dpx->element[i].descriptor) {
202                         case descriptor_Red:
203                         case descriptor_Green:
204                         case descriptor_Blue:
205                         case descriptor_Alpha:
206                         case descriptor_Luminance:
207                         case descriptor_Chrominance:
208                                 dpx->depth++;
209                                 dpx->element[i].depth = 1;
210                                 break;
211
212                         case descriptor_CbYCrY:
213                                 dpx->depth += 2;
214                                 dpx->element[i].depth = 2;
215                                 break;
216
217                         case descriptor_RGB:
218                         case descriptor_CbYCr:
219                         case descriptor_CbYACrYA:
220                                 dpx->depth += 3;
221                                 dpx->element[i].depth = 3;
222                                 break;
223
224                         case descriptor_RGBA:
225                         case descriptor_ABGR:
226                         case descriptor_CbYCrA:
227                                 dpx->depth += 4;
228                                 dpx->element[i].depth = 4;
229                                 break;
230
231                         case descriptor_Depth:
232                         case descriptor_Composite:
233                                 /* unsupported */
234                                 break;
235                 }
236
237                 if (dpx->depth == 0 || dpx->depth > 4) {
238                         if (verbose) printf("DPX: Unsupported image depth: %d\n", dpx->depth);
239                         logImageClose(dpx);
240                         return 0;
241                 }
242
243                 dpx->element[i].bitsPerSample = header.imageHeader.element[i].bits_per_sample;
244                 dpx->element[i].maxValue = powf(2, dpx->element[i].bitsPerSample) - 1.0f;
245                 dpx->element[i].packing = swap_ushort(header.imageHeader.element[i].packing, dpx->isMSB);
246
247                 /* Sometimes, the offset is not set correctly in the header */
248                 dpx->element[i].dataOffset = swap_uint(header.imageHeader.element[i].data_offset, dpx->isMSB);
249                 if (dpx->element[i].dataOffset == 0 && dpx->numElements == 1)
250                         dpx->element[i].dataOffset = swap_uint(header.fileHeader.offset, dpx->isMSB);
251
252                 if (dpx->element[i].dataOffset == 0) {
253                         if (verbose) printf("DPX: Image header is corrupted.\n");
254                         logImageClose(dpx);
255                         return 0;
256                 }
257
258                 dpx->element[i].transfer = header.imageHeader.element[i].transfer;
259
260                 /* if undefined, assign default */
261                 dpx->element[i].refLowData = swap_uint(header.imageHeader.element[i].ref_low_data, dpx->isMSB);
262                 dpx->element[i].refLowQuantity = swap_float(header.imageHeader.element[i].ref_low_quantity, dpx->isMSB);
263                 dpx->element[i].refHighData = swap_uint(header.imageHeader.element[i].ref_high_data, dpx->isMSB);
264                 dpx->element[i].refHighQuantity = swap_float(header.imageHeader.element[i].ref_high_quantity, dpx->isMSB);
265
266                 switch (dpx->element[i].descriptor) {
267                         case descriptor_Red:
268                         case descriptor_Green:
269                         case descriptor_Blue:
270                         case descriptor_Alpha:
271                         case descriptor_RGB:
272                         case descriptor_RGBA:
273                         case descriptor_ABGR:
274                                 if (dpx->element[i].refLowData == DPX_UNDEFINED_U32 || isnan(dpx->element[i].refLowData))
275                                         dpx->element[i].refLowData = 0;
276
277                                 if (dpx->element[i].refHighData == DPX_UNDEFINED_U32 || isnan(dpx->element[i].refHighData))
278                                         dpx->element[i].refHighData = (unsigned int)dpx->element[i].maxValue;
279
280                                 if (dpx->element[i].refLowQuantity == DPX_UNDEFINED_R32 || isnan(dpx->element[i].refLowQuantity))
281                                         dpx->element[i].refLowQuantity = 0.0f;
282
283                                 if (dpx->element[i].refHighQuantity == DPX_UNDEFINED_R32 || isnan(dpx->element[i].refHighQuantity)) {
284                                         if (dpx->element[i].transfer == transfer_PrintingDensity || dpx->element[i].transfer == transfer_Logarithmic)
285                                                 dpx->element[i].refHighQuantity = 2.048f;
286                                         else
287                                                 dpx->element[i].refHighQuantity = dpx->element[i].maxValue;
288                                 }
289
290                                 break;
291
292                         case descriptor_Luminance:
293                         case descriptor_Chrominance:
294                         case descriptor_CbYCrY:
295                         case descriptor_CbYCr:
296                         case descriptor_CbYACrYA:
297                         case descriptor_CbYCrA:
298                                 if (dpx->element[i].refLowData == DPX_UNDEFINED_U32 || isnan(dpx->element[i].refLowData))
299                                         dpx->element[i].refLowData = 16.0f / 255.0f * dpx->element[i].maxValue;
300
301                                 if (dpx->element[i].refHighData == DPX_UNDEFINED_U32 || isnan(dpx->element[i].refHighData))
302                                         dpx->element[i].refHighData = 235.0f / 255.0f * dpx->element[i].maxValue;
303
304                                 if (dpx->element[i].refLowQuantity == DPX_UNDEFINED_R32 || isnan(dpx->element[i].refLowQuantity))
305                                         dpx->element[i].refLowQuantity = 0.0f;
306
307                                 if (dpx->element[i].refHighQuantity == DPX_UNDEFINED_R32 || isnan(dpx->element[i].refHighQuantity))
308                                         dpx->element[i].refHighQuantity = 0.7f;
309
310                                 break;
311
312                         default:
313                                 break;
314                 }
315         }
316
317         dpx->referenceBlack = swap_float(header.televisionHeader.black_level, dpx->isMSB);
318         dpx->referenceWhite = swap_float(header.televisionHeader.white_level, dpx->isMSB);
319         dpx->gamma = swap_float(header.televisionHeader.gamma, dpx->isMSB);
320
321         if ((dpx->referenceBlack == DPX_UNDEFINED_R32 || isnan(dpx->referenceBlack)) ||
322             (dpx->referenceWhite == DPX_UNDEFINED_R32 || dpx->referenceWhite <= dpx->referenceBlack || isnan(dpx->referenceWhite)) ||
323             (dpx->gamma == DPX_UNDEFINED_R32 || dpx->gamma <= 0 || isnan(dpx->gamma)))
324         {
325
326                 dpx->referenceBlack = 95.0f / 1023.0f * dpx->element[0].maxValue;
327                 dpx->referenceWhite = 685.0f / 1023.0f * dpx->element[0].maxValue;
328                 dpx->gamma = 1.7f;
329         }
330
331         if (verbose) {
332                 printf("size %d x %d x %d elements\n", dpx->width, dpx->height, dpx->numElements);
333                 for (i = 0; i < dpx->numElements; i++) {
334                         printf(" Element %d:\n", i);
335                         printf("  Bits per sample: %d\n", dpx->element[i].bitsPerSample);
336                         printf("  Depth: %d\n", dpx->element[i].depth);
337                         printf("  Transfer characteristics: %d\n", dpx->element[i].transfer);
338                         printf("  Packing: %d\n", dpx->element[i].packing);
339                         printf("  Descriptor: %d\n", dpx->element[i].descriptor);
340                         printf("  Data offset: %u\n", dpx->element[i].dataOffset);
341                         printf("  Reference low data: %u\n", dpx->element[i].refLowData);
342                         printf("  Reference low quantity: %f\n", dpx->element[i].refLowQuantity);
343                         printf("  Reference high data: %u\n", dpx->element[i].refHighData);
344                         printf("  Reference high quantity: %f\n", dpx->element[i].refHighQuantity);
345                         printf("\n");
346                 }
347
348                 printf("Gamma: %f\n", dpx->gamma);
349                 printf("Reference black: %f\n", dpx->referenceBlack);
350                 printf("Reference white: %f\n", dpx->referenceWhite);
351                 printf("----------------------------\n");
352         }
353         return dpx;
354 }
355
356 LogImageFile *dpxCreate(const char *filename, int width, int height, int bitsPerSample, int hasAlpha,
357                         int isLogarithmic, int referenceWhite, int referenceBlack, float gamma,
358                         const char *creator)
359 {
360         DpxMainHeader header;
361         const char *shortFilename = 0;
362         unsigned char pad[6044];
363
364         LogImageFile *dpx = (LogImageFile *)MEM_mallocN(sizeof(LogImageFile), __func__);
365         if (dpx == 0) {
366                 if (verbose) printf("DPX: Failed to malloc dpx file structure.\n");
367                 return 0;
368         }
369
370         dpx->width = width;
371         dpx->height = height;
372         dpx->element[0].bitsPerSample = bitsPerSample;
373         dpx->element[0].dataOffset = 8092;
374         dpx->element[0].maxValue = powf(2, dpx->element[0].bitsPerSample) - 1.0f;
375         dpx->isMSB = 1;
376         dpx->numElements = 1;
377
378         switch (bitsPerSample) {
379                 case 8:
380                 case 16:
381                         dpx->element[0].packing = 0;
382                         break;
383
384                 case 10:
385                 case 12:
386                         /* Packed Type A padding is the most common 10/12 bits format */
387                         dpx->element[0].packing = 1;
388                         break;
389
390                 default:
391                         if (verbose) printf("DPX: bitsPerSample not supported: %d\n", bitsPerSample);
392                         logImageClose(dpx);
393                         return 0;
394         }
395
396         if (hasAlpha == 0) {
397                 dpx->depth = 3;
398                 dpx->element[0].depth = 3;
399                 dpx->element[0].descriptor = descriptor_RGB;
400         }
401         else {
402                 dpx->depth = 4;
403                 dpx->element[0].depth = 4;
404                 dpx->element[0].descriptor = descriptor_RGBA;
405         }
406
407         if (isLogarithmic == 0) {
408                 dpx->element[0].transfer = transfer_Linear;
409                 dpx->element[0].refHighQuantity = dpx->element[0].maxValue;
410         }
411         else {
412                 dpx->element[0].transfer = transfer_PrintingDensity;
413                 dpx->element[0].refHighQuantity = 2.048f;
414
415         }
416
417         dpx->element[0].refLowQuantity = 0;
418         dpx->element[0].refLowData = 0;
419         dpx->element[0].refHighData = dpx->element[0].maxValue;
420
421         if (referenceWhite > 0)
422                 dpx->referenceWhite = referenceWhite;
423         else
424                 dpx->referenceWhite = 685.0f / 1023.0f * dpx->element[0].maxValue;
425
426         if (referenceBlack > 0)
427                 dpx->referenceBlack = referenceBlack;
428         else
429                 dpx->referenceBlack = 95.0f / 1023.0f * dpx->element[0].maxValue;
430
431         if (gamma > 0.0f)
432                 dpx->gamma = gamma;
433         else
434                 dpx->gamma = 1.7f;
435
436
437         shortFilename = strrchr(filename, '/');
438         if (shortFilename == 0)
439                 shortFilename = filename;
440         else
441                 shortFilename++;
442
443         dpx->file = BLI_fopen(filename, "wb");
444
445         if (dpx->file == 0) {
446                 if (verbose) printf("DPX: Couldn't open file %s\n", filename);
447                 logImageClose(dpx);
448                 return 0;
449         }
450
451         fillDpxMainHeader(dpx, &header, shortFilename, creator);
452
453         if (fwrite(&header, sizeof(header), 1, dpx->file) == 0) {
454                 if (verbose) printf("DPX: Couldn't write image header\n");
455                 logImageClose(dpx);
456                 return 0;
457         }
458
459         /* Header should be rounded to next 8k block
460          * 6044 = 8092 - sizeof(DpxMainHeader) */
461         memset(&pad, 0, 6044);
462         if (fwrite(&pad, 6044, 1, dpx->file) == 0) {
463                 if (verbose) printf("DPX: Couldn't write image header\n");
464                 logImageClose(dpx);
465                 return 0;
466         }
467
468         return dpx;
469 }