1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 1999,2000,2001 David Hodson <hodsond@acm.org>
17  */
18 
19 /** \file
20  * \ingroup imbcineon
21  *
22  * Cineon image file format library definitions.
23  * Cineon and DPX common structures.
24  *
25  * This header file contains private details.
26  * User code should generally use cineonlib.h and dpxlib.h only.
27  * Hmm. I thought the two formats would have more in common!
28  */
29 
30 #pragma once
31 
32 #include <stdio.h>
33 
34 #include "BLI_sys_types.h"
35 #include "BLI_utildefines.h"
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /*
42  * Image structure
43  */
44 
45 /* There are some differences between DPX and Cineon
46  * so we need to know from what type of file the data came from. */
47 enum format {
48   format_DPX,
49   format_Cineon,
50 };
51 
52 typedef struct LogImageElement {
53   int depth;
54   int bitsPerSample;
55   int dataOffset;
56   int packing;
57   int transfer;
58   int descriptor;
59   unsigned int refLowData;
60   unsigned int refHighData;
61   float refLowQuantity;
62   float refHighQuantity;
63   float maxValue; /* = 2^bitsPerSample - 1 (used internally, doesn't come from the file header) */
64 } LogImageElement;
65 
66 typedef struct LogImageFile {
67   /* specified in header */
68   int width;
69   int height;
70   int numElements;
71   int depth;
72   LogImageElement element[8];
73 
74   /* used for log <-> lin conversion */
75   float referenceBlack;
76   float referenceWhite;
77   float gamma;
78 
79   /* io stuff */
80   FILE *file;
81   unsigned char *memBuffer;
82   uintptr_t memBufferSize;
83   unsigned char *memCursor;
84 
85   /* is the file LSB or MSB ? */
86   int isMSB;
87 
88   /* DPX or Cineon ? */
89   int srcFormat;
90 } LogImageFile;
91 
92 /* The SMPTE defines this code:
93  *  0 - User-defined
94  *  1 - Printing density
95  *  2 - Linear
96  *  3 - Logarithmic
97  *  4 - Unspecified video
98  *  5 - SMPTE 240M
99  *  6 - CCIR 709-1
100  *  7 - CCIR 601-2 system B or G
101  *  8 - CCIR 601-2 system M
102  *  9 - NTSC composite video
103  *  10 - PAL composite video
104  *  11 - Z linear
105  *  12 - homogeneous
106  *
107  * Note that transfer_characteristics is U8, don't need
108  * check the byte order.
109  */
110 
111 enum transfer {
112   transfer_UserDefined,
113   transfer_PrintingDensity,
114   transfer_Linear,
115   transfer_Logarithmic,
116   transfer_Unspecified,
117   transfer_Smpte240M,
118   transfer_Ccir7091,
119   transfer_Ccir6012BG,
120   transfer_Ccir6012M,
121   transfer_NTSC,
122   transfer_PAL,
123   transfer_ZLinear,
124   transfer_Homogeneous,
125 };
126 
127 /* The SMPTE defines this code:
128  * 0 - User-defined
129  * 1 - Red
130  * 2 - Green
131  * 3 - Blue
132  * 4 - Alpha
133  * 6 - Luminance
134  * 7 - Chrominance
135  * 8 - Depth
136  * 9 - Composite video
137  * 50 - RGB
138  * 51 - RGBA
139  * 52 - ABGR
140  * 100 - CbYCrY
141  * 101 - CbYACrYA
142  * 102 - CbYCr
143  * 103 - CbYCrA
144  * 150 - User-defined 2-component element
145  * 151 - User-defined 3-component element
146  * 152 - User-defined 4-component element
147  * 153 - User-defined 5-component element
148  * 154 - User-defined 6-component element
149  * 155 - User-defined 7-component element
150  * 156 - User-defined 8-component element
151  */
152 
153 enum descriptor {
154   descriptor_UserDefined,
155   descriptor_Red,
156   descriptor_Green,
157   descriptor_Blue,
158   descriptor_Alpha,
159   descriptor_Luminance = 6, /* don't ask me why there's no 5 */
160   descriptor_Chrominance,
161   descriptor_Depth,
162   descriptor_Composite,
163   descriptor_RGB = 50,
164   descriptor_RGBA,
165   descriptor_ABGR,
166   descriptor_CbYCrY = 100,
167   descriptor_CbYACrYA,
168   descriptor_CbYCr,
169   descriptor_CbYCrA,
170   descriptor_UserDefined2Elt = 150,
171   descriptor_UserDefined3Elt,
172   descriptor_UserDefined4Elt,
173   descriptor_UserDefined5Elt,
174   descriptor_UserDefined6Elt,
175   descriptor_UserDefined7Elt,
176   descriptor_UserDefined8Elt,
177   /* following descriptors are for internal use only */
178   descriptor_YA,
179 };
180 
181 /* int functions return 0 for OK */
182 
183 void logImageSetVerbose(int verbosity);
184 int logImageIsDpx(const void *buffer);
185 int logImageIsCineon(const void *buffer);
186 LogImageFile *logImageOpenFromMemory(const unsigned char *buffer, unsigned int size);
187 LogImageFile *logImageOpenFromFile(const char *filename, int cineon);
188 void logImageGetSize(LogImageFile *logImage, int *width, int *height, int *depth);
189 LogImageFile *logImageCreate(const char *filename,
190                              int cineon,
191                              int width,
192                              int height,
193                              int bitsPerSample,
194                              int isLogarithmic,
195                              int hasAlpha,
196                              int referenceWhite,
197                              int referenceBlack,
198                              float gamma,
199                              const char *creator);
200 void logImageClose(LogImageFile *logImage);
201 
202 /* Data handling */
203 size_t getRowLength(size_t width, LogImageElement logElement);
204 int logImageSetDataRGBA(LogImageFile *logImage, float *data, int dataIsLinearRGB);
205 int logImageGetDataRGBA(LogImageFile *logImage, float *data, int dataIsLinearRGB);
206 
207 /*
208  * Inline routines
209  */
210 
211 /* Endianness swapping */
212 
swap_ushort(unsigned short x,int swap)213 BLI_INLINE unsigned short swap_ushort(unsigned short x, int swap)
214 {
215   if (swap != 0) {
216     return (x >> 8) | (x << 8);
217   }
218   else {
219     return x;
220   }
221 }
222 
swap_uint(unsigned int x,int swap)223 BLI_INLINE unsigned int swap_uint(unsigned int x, int swap)
224 {
225   if (swap != 0) {
226     return (x >> 24) | ((x << 8) & 0x00FF0000) | ((x >> 8) & 0x0000FF00) | (x << 24);
227   }
228   else {
229     return x;
230   }
231 }
232 
swap_float(float x,int swap)233 BLI_INLINE float swap_float(float x, int swap)
234 {
235   if (swap != 0) {
236     union {
237       float f;
238       unsigned char b[4];
239     } dat1, dat2;
240 
241     dat1.f = x;
242     dat2.b[0] = dat1.b[3];
243     dat2.b[1] = dat1.b[2];
244     dat2.b[2] = dat1.b[1];
245     dat2.b[3] = dat1.b[0];
246     return dat2.f;
247   }
248   else {
249     return x;
250   }
251 }
252 
253 /* Other */
254 
clamp_uint(unsigned int x,unsigned int low,unsigned int high)255 BLI_INLINE unsigned int clamp_uint(unsigned int x, unsigned int low, unsigned int high)
256 {
257   if (x > high) {
258     return high;
259   }
260   else if (x < low) {
261     return low;
262   }
263   else {
264     return x;
265   }
266 }
267 
clamp_float(float x,float low,float high)268 BLI_INLINE float clamp_float(float x, float low, float high)
269 {
270   if (x > high) {
271     return high;
272   }
273   else if (x < low) {
274     return low;
275   }
276   else {
277     return x;
278   }
279 }
280 
float_uint(float value,unsigned int max)281 BLI_INLINE unsigned int float_uint(float value, unsigned int max)
282 {
283   if (value < 0.0f) {
284     return 0;
285   }
286   else if (value > (1.0f - 0.5f / (float)max)) {
287     return max;
288   }
289   else {
290     return (unsigned int)(((float)max * value) + 0.5f);
291   }
292 }
293 
294 #ifdef __cplusplus
295 }
296 #endif
297