Blender V5.0
cineon_dpx.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2006 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "logImageCore.h"
10#include <cstdio>
11#include <cstring>
12
14#include "IMB_filetype.hh"
15#include "IMB_imbuf.hh"
16#include "IMB_imbuf_types.hh"
17
18#include "BKE_global.hh"
19
20#include "MEM_guardedalloc.h"
21
23 const uchar *mem, size_t size, int use_cineon, int flags, ImFileColorSpace &r_colorspace)
24{
25 ImBuf *ibuf;
26 LogImageFile *image;
27 int width, height, depth;
28
29 logImageSetVerbose((G.debug & G_DEBUG) ? 1 : 0);
30
31 image = logImageOpenFromMemory(mem, size);
32
33 if (image == nullptr) {
34 printf("DPX/Cineon: error opening image.\n");
35 return nullptr;
36 }
37
38 logImageGetSize(image, &width, &height, &depth);
39
40 ibuf = IMB_allocImBuf(width, height, 32, IB_float_data | flags);
41 if (ibuf == nullptr) {
42 logImageClose(image);
43 return nullptr;
44 }
45
46 if (!(flags & IB_test)) {
47 if (logImageGetDataRGBA(image, ibuf->float_buffer.data, 1) != 0) {
48 logImageClose(image);
49 IMB_freeImBuf(ibuf);
50 return nullptr;
51 }
52 IMB_flipy(ibuf);
53 }
54
55 logImageClose(image);
56 ibuf->ftype = use_cineon ? IMB_FTYPE_CINEON : IMB_FTYPE_DPX;
57
58 if (flags & IB_alphamode_detect) {
60 }
61
62 r_colorspace.is_hdr_float = true;
63
64 return ibuf;
65}
66
67static int imb_save_dpx_cineon(ImBuf *ibuf, const char *filepath, int use_cineon, int flags)
68{
69 LogImageFile *logImage;
70 float *fbuf;
71 float *fbuf_ptr;
72 const uchar *rect_ptr;
73 int x, y, depth, bitspersample, rvalue;
74
75 if (flags & IB_mem) {
76 printf("DPX/Cineon: saving in memory is not supported.\n");
77 return 0;
78 }
79
80 logImageSetVerbose((G.debug & G_DEBUG) ? 1 : 0);
81
82 depth = (ibuf->planes + 7) >> 3;
83 if (depth > 4 || depth < 3) {
84 printf("DPX/Cineon: unsupported depth: %d for file: '%s'\n", depth, filepath);
85 return 0;
86 }
87
88 if (use_cineon) {
89 /* Only 10bit is supported. */
90 bitspersample = 10;
91 }
92 else {
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
107 logImage = logImageCreate(filepath,
108 use_cineon,
109 ibuf->x,
110 ibuf->y,
111 bitspersample,
112 (depth == 4),
113 (ibuf->foptions.flag & CINEON_LOG),
114 -1,
115 -1,
116 -1,
117 "Blender");
118
119 if (logImage == nullptr) {
120 printf("DPX/Cineon: error creating file.\n");
121 return 0;
122 }
123
124 if (ibuf->float_buffer.data != nullptr && bitspersample != 8) {
125 /* Don't use the float buffer to save 8 BPP picture to prevent color banding
126 * (there's no dithering algorithm behind the #logImageSetDataRGBA function). */
127
128 fbuf = MEM_malloc_arrayN<float>(4 * size_t(ibuf->x) * size_t(ibuf->y),
129 "fbuf in imb_save_dpx_cineon");
130
131 for (y = 0; y < ibuf->y; y++) {
132 float *dst_ptr = fbuf + (4 * (size_t(ibuf->y - y - 1) * size_t(ibuf->x)));
133 const float *src_ptr = ibuf->float_buffer.data + (4 * (size_t(y) * size_t(ibuf->x)));
134
135 memcpy(dst_ptr, src_ptr, 4 * ibuf->x * sizeof(float));
136 }
137
138 rvalue = (logImageSetDataRGBA(logImage, fbuf, 1) == 0);
139
140 MEM_freeN(fbuf);
141 }
142 else {
143 if (ibuf->byte_buffer.data == nullptr) {
145 }
146
147 fbuf = MEM_malloc_arrayN<float>(4 * size_t(ibuf->x) * size_t(ibuf->y),
148 "fbuf in imb_save_dpx_cineon");
149 if (fbuf == nullptr) {
150 printf("DPX/Cineon: error allocating memory.\n");
151 logImageClose(logImage);
152 return 0;
153 }
154 for (y = 0; y < ibuf->y; y++) {
155 fbuf_ptr = fbuf + (4 * (size_t(ibuf->y - y - 1) * size_t(ibuf->x)));
156 rect_ptr = ibuf->byte_buffer.data + (4 * (size_t(y) * size_t(ibuf->x)));
157 for (x = 0; x < ibuf->x; x++) {
158 fbuf_ptr[0] = float(rect_ptr[0]) / 255.0f;
159 fbuf_ptr[1] = float(rect_ptr[1]) / 255.0f;
160 fbuf_ptr[2] = float(rect_ptr[2]) / 255.0f;
161 fbuf_ptr[3] = (depth == 4) ? (float(rect_ptr[3]) / 255.0f) : 1.0f;
162 fbuf_ptr += 4;
163 rect_ptr += 4;
164 }
165 }
166 rvalue = (logImageSetDataRGBA(logImage, fbuf, 0) == 0);
167 MEM_freeN(fbuf);
168 }
169
170 logImageClose(logImage);
171 return rvalue;
172}
173
174bool imb_save_cineon(ImBuf *buf, const char *filepath, int flags)
175{
176 return imb_save_dpx_cineon(buf, filepath, 1, flags);
177}
178
179bool imb_is_a_cineon(const uchar *mem, size_t size)
180{
181 return logImageIsCineon(mem, size);
182}
183
184ImBuf *imb_load_cineon(const uchar *mem, size_t size, int flags, ImFileColorSpace &r_colorspace)
185{
186 if (!imb_is_a_cineon(mem, size)) {
187 return nullptr;
188 }
189 return imb_load_dpx_cineon(mem, size, 1, flags, r_colorspace);
190}
@ G_DEBUG
unsigned char uchar
void IMB_flipy(ImBuf *ibuf)
void IMB_byte_from_float(ImBuf *ibuf)
void IMB_freeImBuf(ImBuf *ibuf)
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, unsigned char planes, unsigned int flags)
@ IB_float_data
@ IB_alphamode_premul
@ 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.cc:67
bool imb_save_cineon(ImBuf *buf, const char *filepath, int flags)
ImBuf * imb_load_cineon(const uchar *mem, size_t size, int flags, ImFileColorSpace &r_colorspace)
bool imb_is_a_cineon(const uchar *mem, size_t size)
static ImBuf * imb_load_dpx_cineon(const uchar *mem, size_t size, int use_cineon, int flags, ImFileColorSpace &r_colorspace)
Definition cineon_dpx.cc:22
nullptr float
#define printf(...)
int logImageIsCineon(const void *buffer, const uint size)
void logImageSetVerbose(int verbosity)
int logImageSetDataRGBA(LogImageFile *logImage, const float *data, int dataIsLinearRGB)
void logImageClose(LogImageFile *logImage)
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)
int logImageGetDataRGBA(LogImageFile *logImage, float *data, int dataIsLinearRGB)
LogImageFile * logImageOpenFromMemory(const uchar *buffer, uint size)
void logImageGetSize(const LogImageFile *logImage, int *width, int *height, int *depth)
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:133
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
#define G(x, y, z)
ImBufFloatBuffer float_buffer
ImbFormatOptions foptions
ImBufByteBuffer byte_buffer
unsigned char planes
enum eImbFileType ftype