Merge of trunk into blender 2.5:
[blender.git] / source / blender / imbuf / intern / cineon / logImageLib.c
1 /*
2  *       Cineon and 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */
21
22 #include "cineonlib.h"
23 #include "dpxlib.h"
24
25 #include <stdio.h>
26 #include <math.h>
27 #include <stdlib.h>
28 #include <time.h>                                /* strftime() */
29 #include <sys/types.h>
30 #ifdef WIN32
31 #include <winsock.h>
32 #else
33 #include <netinet/in.h>  /* htonl() */
34 #endif
35 #include <string.h>                      /* memset */
36
37 #define MIN_GAMMA 0.01
38 #define MAX_GAMMA 99.9
39 #define DEFAULT_GAMMA 1.0
40 #define DEFAULT_BLACK_POINT 95
41 #define DEFAULT_WHITE_POINT 685
42
43 void
44 logImageSetVerbose(int verbosity) {
45         cineonSetVerbose(verbosity);
46         dpxSetVerbose(verbosity);
47 }
48
49 LogImageFile*
50 logImageOpen(const char* filename, int cineon) {
51         if (cineon) {
52                 return cineonOpen(filename);
53         } else {
54                 return dpxOpen(filename);
55         }
56         return 0;
57 }
58
59 LogImageFile*
60 logImageOpenFromMem(unsigned char *buffer, unsigned int size, int cineon) {
61         if (cineon) {
62                 return cineonOpenFromMem(buffer, size);
63         } else {
64                 return dpxOpenFromMem(buffer, size);
65         }
66         return 0;
67 }
68
69 LogImageFile*
70 logImageCreate(const char* filename, int cineon, int width, int height, int depth) {
71         if (cineon) {
72                 return cineonCreate(filename, width, height, depth);
73         } else {
74                 return dpxCreate(filename, width, height, depth);
75         }
76         return 0;
77 }
78
79 int
80 logImageGetSize(const LogImageFile* logImage, int* width, int* height, int* depth) {
81         *width = logImage->width;
82         *height = logImage->height;
83         *depth = logImage->depth;
84         return 0;
85 }
86
87 int
88 logImageGetByteConversionDefaults(LogImageByteConversionParameters* params) {
89         params->gamma = DEFAULT_GAMMA;
90         params->blackPoint = DEFAULT_BLACK_POINT;
91         params->whitePoint = DEFAULT_WHITE_POINT;
92         params->doLogarithm = 0;
93         return 0;
94 }
95
96 int
97 logImageGetByteConversion(const LogImageFile* logImage, LogImageByteConversionParameters* params) {
98         params->gamma = logImage->params.gamma;
99         params->blackPoint = logImage->params.blackPoint;
100         params->whitePoint = logImage->params.whitePoint;
101         params->doLogarithm = 0;
102         return 0;
103 }
104
105 int
106 logImageSetByteConversion(LogImageFile* logImage, const LogImageByteConversionParameters* params) {
107         if ((params->gamma >= MIN_GAMMA) &&
108                         (params->gamma <= MAX_GAMMA) &&
109                         (params->blackPoint >= 0) &&
110                         (params->blackPoint < params->whitePoint) &&
111                         (params->whitePoint <= 1023)) {
112                 logImage->params.gamma = params->gamma;
113                 logImage->params.blackPoint = params->blackPoint;
114                 logImage->params.whitePoint = params->whitePoint;
115                 logImage->params.doLogarithm = params->doLogarithm;
116                 setupLut16(logImage);
117                 return 0;
118         }
119         return 1;
120 }
121
122 int
123 logImageGetRowBytes(LogImageFile* logImage, unsigned short* row, int y) {
124         return logImage->getRow(logImage, row, y);
125 }
126
127 int
128 logImageSetRowBytes(LogImageFile* logImage, const unsigned short* row, int y) {
129         return logImage->setRow(logImage, row, y);
130 }
131
132 void
133 logImageClose(LogImageFile* logImage) {
134         logImage->close(logImage);
135 }
136
137 void
138 logImageDump(const char* filename) {
139
140         U32 magic;
141
142         FILE* foo = fopen(filename, "rb");
143         if (foo == 0) {
144                 return;
145         }
146
147         if (fread(&magic, sizeof(magic), 1, foo) == 0) {
148                 fclose(foo);
149                 return;
150         }
151
152         fclose(foo);
153
154         if (magic == ntohl(CINEON_FILE_MAGIC)) {
155 #if 0
156                 cineonDump(filename);
157 #endif
158         } else if (magic == ntohl(DPX_FILE_MAGIC)) {
159                 dpxDump(filename);
160         }
161 }