Blender V4.3
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
9#include "logImageCore.h"
10#include <cmath>
11#include <cstdio>
12#include <cstring>
13
15#include "IMB_filetype.hh"
16#include "IMB_imbuf.hh"
17#include "IMB_imbuf_types.hh"
18
19#include "BKE_global.hh"
20
21#include "MEM_guardedalloc.h"
22
24 const uchar *mem, size_t size, int use_cineon, int flags, char colorspace[IM_MAX_SPACE])
25{
26 ImBuf *ibuf;
28 int width, height, depth;
29
31
32 logImageSetVerbose((G.debug & G_DEBUG) ? 1 : 0);
33
34 image = logImageOpenFromMemory(mem, size);
35
36 if (image == nullptr) {
37 printf("DPX/Cineon: error opening image.\n");
38 return nullptr;
39 }
40
41 logImageGetSize(image, &width, &height, &depth);
42
43 ibuf = IMB_allocImBuf(width, height, 32, IB_rectfloat | flags);
44 if (ibuf == nullptr) {
45 logImageClose(image);
46 return nullptr;
47 }
48
49 if (!(flags & IB_test)) {
50 if (logImageGetDataRGBA(image, ibuf->float_buffer.data, 1) != 0) {
51 logImageClose(image);
52 IMB_freeImBuf(ibuf);
53 return nullptr;
54 }
55 IMB_flipy(ibuf);
56 }
57
58 logImageClose(image);
59 ibuf->ftype = use_cineon ? IMB_FTYPE_CINEON : IMB_FTYPE_DPX;
60
61 if (flags & IB_alphamode_detect) {
63 }
64
65 return ibuf;
66}
67
68static int imb_save_dpx_cineon(ImBuf *ibuf, const char *filepath, int use_cineon, int flags)
69{
70 LogImageFile *logImage;
71 float *fbuf;
72 float *fbuf_ptr;
73 const uchar *rect_ptr;
74 int x, y, depth, bitspersample, rvalue;
75
76 if (flags & IB_mem) {
77 printf("DPX/Cineon: saving in memory is not supported.\n");
78 return 0;
79 }
80
81 logImageSetVerbose((G.debug & G_DEBUG) ? 1 : 0);
82
83 depth = (ibuf->planes + 7) >> 3;
84 if (depth > 4 || depth < 3) {
85 printf("DPX/Cineon: unsupported depth: %d for file: '%s'\n", depth, filepath);
86 return 0;
87 }
88
89 if (ibuf->foptions.flag & CINEON_10BIT) {
90 bitspersample = 10;
91 }
92 else if (ibuf->foptions.flag & CINEON_12BIT) {
93 bitspersample = 12;
94 }
95 else if (ibuf->foptions.flag & CINEON_16BIT) {
96 bitspersample = 16;
97 }
98 else {
99 bitspersample = 8;
100 }
101
102 logImage = logImageCreate(filepath,
103 use_cineon,
104 ibuf->x,
105 ibuf->y,
106 bitspersample,
107 (depth == 4),
108 (ibuf->foptions.flag & CINEON_LOG),
109 -1,
110 -1,
111 -1,
112 "Blender");
113
114 if (logImage == nullptr) {
115 printf("DPX/Cineon: error creating file.\n");
116 return 0;
117 }
118
119 if (ibuf->float_buffer.data != nullptr && bitspersample != 8) {
120 /* Don't use the float buffer to save 8 BPP picture to prevent color banding
121 * (there's no dithering algorithm behind the #logImageSetDataRGBA function). */
122
123 fbuf = (float *)MEM_mallocN(sizeof(float[4]) * ibuf->x * ibuf->y,
124 "fbuf in imb_save_dpx_cineon");
125
126 for (y = 0; y < ibuf->y; y++) {
127 float *dst_ptr = fbuf + 4 * ((ibuf->y - y - 1) * ibuf->x);
128 const float *src_ptr = ibuf->float_buffer.data + 4 * (y * ibuf->x);
129
130 memcpy(dst_ptr, src_ptr, 4 * ibuf->x * sizeof(float));
131 }
132
133 rvalue = (logImageSetDataRGBA(logImage, fbuf, 1) == 0);
134
135 MEM_freeN(fbuf);
136 }
137 else {
138 if (ibuf->byte_buffer.data == nullptr) {
140 }
141
142 fbuf = (float *)MEM_mallocN(sizeof(float[4]) * ibuf->x * ibuf->y,
143 "fbuf in imb_save_dpx_cineon");
144 if (fbuf == nullptr) {
145 printf("DPX/Cineon: error allocating memory.\n");
146 logImageClose(logImage);
147 return 0;
148 }
149 for (y = 0; y < ibuf->y; y++) {
150 for (x = 0; x < ibuf->x; x++) {
151 fbuf_ptr = fbuf + 4 * ((ibuf->y - y - 1) * ibuf->x + x);
152 rect_ptr = ibuf->byte_buffer.data + 4 * (y * ibuf->x + x);
153 fbuf_ptr[0] = float(rect_ptr[0]) / 255.0f;
154 fbuf_ptr[1] = float(rect_ptr[1]) / 255.0f;
155 fbuf_ptr[2] = float(rect_ptr[2]) / 255.0f;
156 fbuf_ptr[3] = (depth == 4) ? (float(rect_ptr[3]) / 255.0f) : 1.0f;
157 }
158 }
159 rvalue = (logImageSetDataRGBA(logImage, fbuf, 0) == 0);
160 MEM_freeN(fbuf);
161 }
162
163 logImageClose(logImage);
164 return rvalue;
165}
166
167bool imb_save_cineon(ImBuf *buf, const char *filepath, int flags)
168{
169 return imb_save_dpx_cineon(buf, filepath, 1, flags);
170}
171
172bool imb_is_a_cineon(const uchar *buf, size_t size)
173{
174 return logImageIsCineon(buf, size);
175}
176
177ImBuf *imb_load_cineon(const uchar *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
178{
179 if (!imb_is_a_cineon(mem, size)) {
180 return nullptr;
181 }
182 return imb_load_dpx_cineon(mem, size, 1, flags, colorspace);
183}
@ G_DEBUG
unsigned char uchar
@ COLOR_ROLE_DEFAULT_FLOAT
void IMB_flipy(ImBuf *ibuf)
void IMB_rect_from_float(ImBuf *ibuf)
Definition divers.cc:694
#define IM_MAX_SPACE
Definition IMB_imbuf.hh:49
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 int imb_save_dpx_cineon(ImBuf *ibuf, const char *filepath, int use_cineon, int flags)
Definition cineon_dpx.cc:68
bool imb_save_cineon(ImBuf *buf, const char *filepath, int flags)
bool imb_is_a_cineon(const uchar *buf, size_t size)
ImBuf * imb_load_cineon(const uchar *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
static ImBuf * imb_load_dpx_cineon(const uchar *mem, size_t size, int use_cineon, int flags, char colorspace[IM_MAX_SPACE])
Definition cineon_dpx.cc:23
void colorspace_set_default_role(char *colorspace, int size, int role)
input_tx image(0, GPU_RGBA16F, Qualifier::WRITE, ImageType::FLOAT_2D, "preview_img") .compute_source("compositor_compute_preview.glsl") .do_static_compilation(true)
#define printf
draw_view in_light_buf[] float
struct ImBuf * IMB_allocImBuf(unsigned int, unsigned int, unsigned char, unsigned int)
void IMB_freeImBuf(ImBuf *)
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_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
#define G(x, y, z)
ImBufFloatBuffer float_buffer
ImbFormatOptions foptions
ImBufByteBuffer byte_buffer
unsigned char planes
enum eImbFileType ftype