Blender  V3.3
cineon_dpx.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2006 Blender Foundation. */
3 
8 #include "logImageCore.h"
9 #include <math.h>
10 #include <stdio.h>
11 #include <string.h>
12 
13 #include "IMB_filetype.h"
14 #include "IMB_imbuf.h"
15 #include "IMB_imbuf_types.h"
16 
17 #include "IMB_colormanagement.h"
19 
20 #include "BKE_global.h"
21 
22 #include "MEM_guardedalloc.h"
23 
24 static struct ImBuf *imb_load_dpx_cineon(const unsigned char *mem,
25  size_t size,
26  int use_cineon,
27  int flags,
28  char colorspace[IM_MAX_SPACE])
29 {
30  ImBuf *ibuf;
32  int width, height, depth;
33 
35 
36  logImageSetVerbose((G.debug & G_DEBUG) ? 1 : 0);
37 
39 
40  if (image == NULL) {
41  printf("DPX/Cineon: error opening image.\n");
42  return NULL;
43  }
44 
45  logImageGetSize(image, &width, &height, &depth);
46 
48  if (ibuf == NULL) {
50  return NULL;
51  }
52 
53  if (!(flags & IB_test)) {
54  if (logImageGetDataRGBA(image, ibuf->rect_float, 1) != 0) {
56  IMB_freeImBuf(ibuf);
57  return NULL;
58  }
59  IMB_flipy(ibuf);
60  }
61 
63  ibuf->ftype = use_cineon ? IMB_FTYPE_CINEON : IMB_FTYPE_DPX;
64 
65  if (flags & IB_alphamode_detect) {
66  ibuf->flags |= IB_alphamode_premul;
67  }
68 
69  return ibuf;
70 }
71 
72 static int imb_save_dpx_cineon(ImBuf *ibuf, const char *filepath, int use_cineon, int flags)
73 {
74  LogImageFile *logImage;
75  float *fbuf;
76  float *fbuf_ptr;
77  unsigned char *rect_ptr;
78  int x, y, depth, bitspersample, rvalue;
79 
80  if (flags & IB_mem) {
81  printf("DPX/Cineon: saving in memory is not supported.\n");
82  return 0;
83  }
84 
85  logImageSetVerbose((G.debug & G_DEBUG) ? 1 : 0);
86 
87  depth = (ibuf->planes + 7) >> 3;
88  if (depth > 4 || depth < 3) {
89  printf("DPX/Cineon: unsupported depth: %d for file: '%s'\n", depth, filepath);
90  return 0;
91  }
92 
93  if (ibuf->foptions.flag & CINEON_10BIT) {
94  bitspersample = 10;
95  }
96  else if (ibuf->foptions.flag & CINEON_12BIT) {
97  bitspersample = 12;
98  }
99  else if (ibuf->foptions.flag & CINEON_16BIT) {
100  bitspersample = 16;
101  }
102  else {
103  bitspersample = 8;
104  }
105 
106  logImage = logImageCreate(filepath,
107  use_cineon,
108  ibuf->x,
109  ibuf->y,
110  bitspersample,
111  (depth == 4),
112  (ibuf->foptions.flag & CINEON_LOG),
113  -1,
114  -1,
115  -1,
116  "Blender");
117 
118  if (logImage == NULL) {
119  printf("DPX/Cineon: error creating file.\n");
120  return 0;
121  }
122 
123  if (ibuf->rect_float != NULL && bitspersample != 8) {
124  /* Don't use the float buffer to save 8 BPP picture to prevent color banding
125  * (there's no dithering algorithm behind the #logImageSetDataRGBA function). */
126 
127  fbuf = (float *)MEM_mallocN(sizeof(float[4]) * ibuf->x * ibuf->y,
128  "fbuf in imb_save_dpx_cineon");
129 
130  for (y = 0; y < ibuf->y; y++) {
131  float *dst_ptr = fbuf + 4 * ((ibuf->y - y - 1) * ibuf->x);
132  float *src_ptr = ibuf->rect_float + 4 * (y * ibuf->x);
133 
134  memcpy(dst_ptr, src_ptr, 4 * ibuf->x * sizeof(float));
135  }
136 
137  rvalue = (logImageSetDataRGBA(logImage, fbuf, 1) == 0);
138 
139  MEM_freeN(fbuf);
140  }
141  else {
142  if (ibuf->rect == NULL) {
143  IMB_rect_from_float(ibuf);
144  }
145 
146  fbuf = (float *)MEM_mallocN(sizeof(float[4]) * ibuf->x * ibuf->y,
147  "fbuf in imb_save_dpx_cineon");
148  if (fbuf == NULL) {
149  printf("DPX/Cineon: error allocating memory.\n");
150  logImageClose(logImage);
151  return 0;
152  }
153  for (y = 0; y < ibuf->y; y++) {
154  for (x = 0; x < ibuf->x; x++) {
155  fbuf_ptr = fbuf + 4 * ((ibuf->y - y - 1) * ibuf->x + x);
156  rect_ptr = (unsigned char *)ibuf->rect + 4 * (y * ibuf->x + x);
157  fbuf_ptr[0] = (float)rect_ptr[0] / 255.0f;
158  fbuf_ptr[1] = (float)rect_ptr[1] / 255.0f;
159  fbuf_ptr[2] = (float)rect_ptr[2] / 255.0f;
160  fbuf_ptr[3] = (depth == 4) ? ((float)rect_ptr[3] / 255.0f) : 1.0f;
161  }
162  }
163  rvalue = (logImageSetDataRGBA(logImage, fbuf, 0) == 0);
164  MEM_freeN(fbuf);
165  }
166 
167  logImageClose(logImage);
168  return rvalue;
169 }
170 
171 bool imb_save_cineon(struct ImBuf *buf, const char *filepath, int flags)
172 {
173  return imb_save_dpx_cineon(buf, filepath, 1, flags);
174 }
175 
176 bool imb_is_a_cineon(const unsigned char *buf, size_t size)
177 {
178  return logImageIsCineon(buf, size);
179 }
180 
181 ImBuf *imb_load_cineon(const unsigned char *mem,
182  size_t size,
183  int flags,
184  char colorspace[IM_MAX_SPACE])
185 {
186  if (!imb_is_a_cineon(mem, size)) {
187  return NULL;
188  }
189  return imb_load_dpx_cineon(mem, size, 1, flags, colorspace);
190 }
191 
192 bool imb_save_dpx(struct ImBuf *buf, const char *filepath, int flags)
193 {
194  return imb_save_dpx_cineon(buf, filepath, 0, flags);
195 }
196 
197 bool imb_is_a_dpx(const unsigned char *buf, size_t size)
198 {
199  return logImageIsDpx(buf, size);
200 }
201 
202 ImBuf *imb_load_dpx(const unsigned char *mem,
203  size_t size,
204  int flags,
205  char colorspace[IM_MAX_SPACE])
206 {
207  if (!imb_is_a_dpx(mem, size)) {
208  return NULL;
209  }
210  return imb_load_dpx_cineon(mem, size, 0, flags, colorspace);
211 }
typedef float(TangentPoint)[2]
@ G_DEBUG
Definition: BKE_global.h:174
_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
_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
@ COLOR_ROLE_DEFAULT_FLOAT
struct ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, unsigned char planes, unsigned int flags)
Definition: allocimbuf.c:500
void IMB_rect_from_float(struct ImBuf *ibuf)
Definition: divers.c:696
#define IM_MAX_SPACE
Definition: IMB_imbuf.h:49
void IMB_flipy(struct ImBuf *ibuf)
Definition: rotate.c:16
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
static int imb_save_dpx_cineon(ImBuf *ibuf, const char *filepath, int use_cineon, int flags)
Definition: cineon_dpx.c:72
bool imb_save_dpx(struct ImBuf *buf, const char *filepath, int flags)
Definition: cineon_dpx.c:192
ImBuf * imb_load_dpx(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
Definition: cineon_dpx.c:202
bool imb_is_a_cineon(const unsigned char *buf, size_t size)
Definition: cineon_dpx.c:176
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:24
bool imb_is_a_dpx(const unsigned char *buf, size_t size)
Definition: cineon_dpx.c:197
bool imb_save_cineon(struct ImBuf *buf, const char *filepath, int flags)
Definition: cineon_dpx.c:171
ImBuf * imb_load_cineon(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
Definition: cineon_dpx.c:181
void colorspace_set_default_role(char *colorspace, int size, int role)
depth_tx normal_tx diffuse_light_tx specular_light_tx volume_light_tx environment_tx ambient_occlusion_tx aov_value_tx in_weight_img image(1, GPU_R32F, Qualifier::WRITE, ImageType::FLOAT_2D_ARRAY, "out_weight_img") .image(3
void IMB_freeImBuf(ImBuf *UNUSED(ibuf))
LogImageFile * logImageCreate(const char *filepath, int cineon, int width, int height, int bitsPerSample, int isLogarithmic, int hasAlpha, int referenceWhite, int referenceBlack, float gamma, const char *creator)
Definition: logImageCore.c:144
void logImageSetVerbose(int verbosity)
Definition: logImageCore.c:73
int logImageIsCineon(const void *buffer, const unsigned int size)
Definition: logImageCore.c:94
LogImageFile * logImageOpenFromMemory(const unsigned char *buffer, unsigned int size)
Definition: logImageCore.c:132
void logImageClose(LogImageFile *logImage)
Definition: logImageCore.c:175
int logImageIsDpx(const void *buffer, const unsigned int size)
Definition: logImageCore.c:84
void logImageGetSize(LogImageFile *logImage, int *width, int *height, int *depth)
Definition: logImageCore.c:186
int logImageSetDataRGBA(LogImageFile *logImage, float *data, int dataIsLinearRGB)
Definition: logImageCore.c:233
int logImageGetDataRGBA(LogImageFile *logImage, float *data, int dataIsLinearRGB)
Definition: logImageCore.c:421
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
#define G(x, y, z)
ImbFormatOptions foptions
unsigned char planes
enum eImbFileType ftype
unsigned int * rect
float * rect_float