Blender  V2.93
cineon_dpx.c
Go to the documentation of this file.
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  * The Original Code is Copyright (C) 2006 Blender Foundation.
17  */
18 
23 #include "logImageCore.h"
24 #include <math.h>
25 #include <stdio.h>
26 #include <string.h>
27 
28 #include "IMB_filetype.h"
29 #include "IMB_imbuf.h"
30 #include "IMB_imbuf_types.h"
31 
32 #include "IMB_colormanagement.h"
34 
35 #include "BKE_global.h"
36 
37 #include "MEM_guardedalloc.h"
38 
39 static struct ImBuf *imb_load_dpx_cineon(const unsigned char *mem,
40  size_t size,
41  int use_cineon,
42  int flags,
43  char colorspace[IM_MAX_SPACE])
44 {
45  ImBuf *ibuf;
46  LogImageFile *image;
47  int width, height, depth;
48 
50 
51  logImageSetVerbose((G.debug & G_DEBUG) ? 1 : 0);
52 
53  image = logImageOpenFromMemory(mem, size);
54 
55  if (image == NULL) {
56  printf("DPX/Cineon: error opening image.\n");
57  return NULL;
58  }
59 
60  logImageGetSize(image, &width, &height, &depth);
61 
63  if (ibuf == NULL) {
64  logImageClose(image);
65  return NULL;
66  }
67 
68  if (!(flags & IB_test)) {
69  if (logImageGetDataRGBA(image, ibuf->rect_float, 1) != 0) {
70  logImageClose(image);
71  IMB_freeImBuf(ibuf);
72  return NULL;
73  }
74  IMB_flipy(ibuf);
75  }
76 
77  logImageClose(image);
78  ibuf->ftype = use_cineon ? IMB_FTYPE_CINEON : IMB_FTYPE_DPX;
79 
80  if (flags & IB_alphamode_detect) {
81  ibuf->flags |= IB_alphamode_premul;
82  }
83 
84  return ibuf;
85 }
86 
87 static int imb_save_dpx_cineon(ImBuf *ibuf, const char *filename, int use_cineon, int flags)
88 {
89  LogImageFile *logImage;
90  float *fbuf;
91  float *fbuf_ptr;
92  unsigned char *rect_ptr;
93  int x, y, depth, bitspersample, rvalue;
94 
95  if (flags & IB_mem) {
96  printf("DPX/Cineon: saving in memory is not supported.\n");
97  return 0;
98  }
99 
100  logImageSetVerbose((G.debug & G_DEBUG) ? 1 : 0);
101 
102  depth = (ibuf->planes + 7) >> 3;
103  if (depth > 4 || depth < 3) {
104  printf("DPX/Cineon: unsupported depth: %d for file: '%s'\n", depth, filename);
105  return 0;
106  }
107 
108  if (ibuf->foptions.flag & CINEON_10BIT) {
109  bitspersample = 10;
110  }
111  else if (ibuf->foptions.flag & CINEON_12BIT) {
112  bitspersample = 12;
113  }
114  else if (ibuf->foptions.flag & CINEON_16BIT) {
115  bitspersample = 16;
116  }
117  else {
118  bitspersample = 8;
119  }
120 
121  logImage = logImageCreate(filename,
122  use_cineon,
123  ibuf->x,
124  ibuf->y,
125  bitspersample,
126  (depth == 4),
127  (ibuf->foptions.flag & CINEON_LOG),
128  -1,
129  -1,
130  -1,
131  "Blender");
132 
133  if (logImage == NULL) {
134  printf("DPX/Cineon: error creating file.\n");
135  return 0;
136  }
137 
138  if (ibuf->rect_float != NULL && bitspersample != 8) {
139  /* don't use the float buffer to save 8 bpp picture to prevent color banding
140  * (there's no dithering algorithm behind the logImageSetDataRGBA function) */
141 
142  fbuf = (float *)MEM_mallocN(sizeof(float[4]) * ibuf->x * ibuf->y,
143  "fbuf in imb_save_dpx_cineon");
144 
145  for (y = 0; y < ibuf->y; y++) {
146  float *dst_ptr = fbuf + 4 * ((ibuf->y - y - 1) * ibuf->x);
147  float *src_ptr = ibuf->rect_float + 4 * (y * ibuf->x);
148 
149  memcpy(dst_ptr, src_ptr, 4 * ibuf->x * sizeof(float));
150  }
151 
152  rvalue = (logImageSetDataRGBA(logImage, fbuf, 1) == 0);
153 
154  MEM_freeN(fbuf);
155  }
156  else {
157  if (ibuf->rect == NULL) {
158  IMB_rect_from_float(ibuf);
159  }
160 
161  fbuf = (float *)MEM_mallocN(sizeof(float[4]) * ibuf->x * ibuf->y,
162  "fbuf in imb_save_dpx_cineon");
163  if (fbuf == NULL) {
164  printf("DPX/Cineon: error allocating memory.\n");
165  logImageClose(logImage);
166  return 0;
167  }
168  for (y = 0; y < ibuf->y; y++) {
169  for (x = 0; x < ibuf->x; x++) {
170  fbuf_ptr = fbuf + 4 * ((ibuf->y - y - 1) * ibuf->x + x);
171  rect_ptr = (unsigned char *)ibuf->rect + 4 * (y * ibuf->x + x);
172  fbuf_ptr[0] = (float)rect_ptr[0] / 255.0f;
173  fbuf_ptr[1] = (float)rect_ptr[1] / 255.0f;
174  fbuf_ptr[2] = (float)rect_ptr[2] / 255.0f;
175  fbuf_ptr[3] = (depth == 4) ? ((float)rect_ptr[3] / 255.0f) : 1.0f;
176  }
177  }
178  rvalue = (logImageSetDataRGBA(logImage, fbuf, 0) == 0);
179  MEM_freeN(fbuf);
180  }
181 
182  logImageClose(logImage);
183  return rvalue;
184 }
185 
186 bool imb_save_cineon(struct ImBuf *buf, const char *filepath, int flags)
187 {
188  return imb_save_dpx_cineon(buf, filepath, 1, flags);
189 }
190 
191 bool imb_is_a_cineon(const unsigned char *buf, size_t size)
192 {
193  return logImageIsCineon(buf, size);
194 }
195 
196 ImBuf *imb_load_cineon(const unsigned char *mem,
197  size_t size,
198  int flags,
199  char colorspace[IM_MAX_SPACE])
200 {
201  if (!imb_is_a_cineon(mem, size)) {
202  return NULL;
203  }
204  return imb_load_dpx_cineon(mem, size, 1, flags, colorspace);
205 }
206 
207 bool imb_save_dpx(struct ImBuf *buf, const char *filepath, int flags)
208 {
209  return imb_save_dpx_cineon(buf, filepath, 0, flags);
210 }
211 
212 bool imb_is_a_dpx(const unsigned char *buf, size_t size)
213 {
214  return logImageIsDpx(buf, size);
215 }
216 
217 ImBuf *imb_load_dpx(const unsigned char *mem,
218  size_t size,
219  int flags,
220  char colorspace[IM_MAX_SPACE])
221 {
222  if (!imb_is_a_dpx(mem, size)) {
223  return NULL;
224  }
225  return imb_load_dpx_cineon(mem, size, 0, flags, colorspace);
226 }
typedef float(TangentPoint)[2]
@ G_DEBUG
Definition: BKE_global.h:133
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei width
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei height
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
@ COLOR_ROLE_DEFAULT_FLOAT
struct ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, unsigned char planes, unsigned int flags)
Definition: allocimbuf.c:478
void IMB_rect_from_float(struct ImBuf *ibuf)
Definition: divers.c:720
void IMB_freeImBuf(struct ImBuf *ibuf)
Definition: allocimbuf.c:211
#define IM_MAX_SPACE
Definition: IMB_imbuf.h:65
void IMB_flipy(struct ImBuf *ibuf)
Definition: rotate.c:33
Contains defines and structs used throughout the imbuf module.
@ IB_alphamode_premul
@ IB_rectfloat
@ IB_alphamode_detect
@ IB_mem
@ IB_test
Read Guarded memory(de)allocation.
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
bool imb_save_dpx(struct ImBuf *buf, const char *filepath, int flags)
Definition: cineon_dpx.c:207
ImBuf * imb_load_dpx(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
Definition: cineon_dpx.c:217
bool imb_is_a_cineon(const unsigned char *buf, size_t size)
Definition: cineon_dpx.c:191
static struct ImBuf * imb_load_dpx_cineon(const unsigned char *mem, size_t size, int use_cineon, int flags, char colorspace[IM_MAX_SPACE])
Definition: cineon_dpx.c:39
bool imb_is_a_dpx(const unsigned char *buf, size_t size)
Definition: cineon_dpx.c:212
bool imb_save_cineon(struct ImBuf *buf, const char *filepath, int flags)
Definition: cineon_dpx.c:186
ImBuf * imb_load_cineon(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
Definition: cineon_dpx.c:196
static int imb_save_dpx_cineon(ImBuf *ibuf, const char *filename, int use_cineon, int flags)
Definition: cineon_dpx.c:87
void colorspace_set_default_role(char *colorspace, int size, int role)
LogImageFile * logImageCreate(const char *filename, int cineon, int width, int height, int bitsPerSample, int isLogarithmic, int hasAlpha, int referenceWhite, int referenceBlack, float gamma, const char *creator)
Definition: logImageCore.c:159
void logImageSetVerbose(int verbosity)
Definition: logImageCore.c:88
int logImageIsCineon(const void *buffer, const unsigned int size)
Definition: logImageCore.c:109
LogImageFile * logImageOpenFromMemory(const unsigned char *buffer, unsigned int size)
Definition: logImageCore.c:147
void logImageClose(LogImageFile *logImage)
Definition: logImageCore.c:190
int logImageIsDpx(const void *buffer, const unsigned int size)
Definition: logImageCore.c:99
void logImageGetSize(LogImageFile *logImage, int *width, int *height, int *depth)
Definition: logImageCore.c:201
int logImageSetDataRGBA(LogImageFile *logImage, float *data, int dataIsLinearRGB)
Definition: logImageCore.c:248
int logImageGetDataRGBA(LogImageFile *logImage, float *data, int dataIsLinearRGB)
Definition: logImageCore.c:436
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
ImbFormatOptions foptions
unsigned char planes
enum eImbFileType ftype
unsigned int * rect
float * rect_float
#define G(x, y, z)