3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
26 * ***** END GPL LICENSE BLOCK *****
32 #include "BLI_blenlib.h"
35 #include "BLI_utildefines.h"
38 #include "IMB_imbuf_types.h"
39 #include "IMB_imbuf.h"
40 #include "IMB_allocimbuf.h"
42 #include "BKE_colortools.h"
44 #include "MEM_guardedalloc.h"
46 void IMB_de_interlace(struct ImBuf *ibuf)
48 struct ImBuf * tbuf1, * tbuf2;
50 if (ibuf == 0) return;
51 if (ibuf->flags & IB_fields) return;
52 ibuf->flags |= IB_fields;
56 tbuf1 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect);
57 tbuf2 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect);
60 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
61 IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
64 IMB_rectcpy(ibuf, tbuf1, 0, 0, 0, 0, tbuf1->x, tbuf1->y);
65 IMB_rectcpy(ibuf, tbuf2, 0, tbuf2->y, 0, 0, tbuf2->x, tbuf2->y);
73 void IMB_interlace(struct ImBuf *ibuf)
75 struct ImBuf * tbuf1, * tbuf2;
77 if (ibuf == 0) return;
78 ibuf->flags &= ~IB_fields;
84 tbuf1 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect);
85 tbuf2 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect);
87 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
88 IMB_rectcpy(tbuf2, ibuf, 0, 0, 0, tbuf2->y, ibuf->x, ibuf->y);
91 IMB_rectcpy(ibuf, tbuf1, 0, 0, 0, 0, tbuf1->x, tbuf1->y);
92 IMB_rectcpy(ibuf, tbuf2, tbuf2->x, 0, 0, 0, tbuf2->x, tbuf2->y);
101 /* assume converting from linear float to sRGB byte */
102 void IMB_rect_from_float(struct ImBuf *ibuf)
104 /* quick method to convert floatbuf to byte */
105 float *tof = (float *)ibuf->rect_float;
106 // int do_dither = ibuf->dither != 0.f;
107 float dither= ibuf->dither / 255.0;
109 int i, channels= ibuf->channels;
110 short profile= ibuf->profile;
111 unsigned char *to = (unsigned char *) ibuf->rect;
113 if(tof==NULL) return;
115 imb_addrectImBuf(ibuf);
116 to = (unsigned char *) ibuf->rect;
120 for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof++)
121 to[1]= to[2]= to[3]= to[0] = FTOCHAR(tof[0]);
123 else if (profile == IB_PROFILE_LINEAR_RGB) {
125 for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof+=3) {
126 srgb[0]= linearrgb_to_srgb(tof[0]);
127 srgb[1]= linearrgb_to_srgb(tof[1]);
128 srgb[2]= linearrgb_to_srgb(tof[2]);
130 to[0] = FTOCHAR(srgb[0]);
131 to[1] = FTOCHAR(srgb[1]);
132 to[2] = FTOCHAR(srgb[2]);
136 else if (channels == 4) {
138 for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof+=4) {
139 const float d = (BLI_frand()-0.5)*dither;
141 srgb[0]= d + linearrgb_to_srgb(tof[0]);
142 srgb[1]= d + linearrgb_to_srgb(tof[1]);
143 srgb[2]= d + linearrgb_to_srgb(tof[2]);
146 to[0] = FTOCHAR(srgb[0]);
147 to[1] = FTOCHAR(srgb[1]);
148 to[2] = FTOCHAR(srgb[2]);
149 to[3] = FTOCHAR(srgb[3]);
152 floatbuf_to_srgb_byte(tof, to, 0, ibuf->x, 0, ibuf->y, ibuf->x);
156 else if(ELEM(profile, IB_PROFILE_NONE, IB_PROFILE_SRGB)) {
158 for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof+=3) {
159 to[0] = FTOCHAR(tof[0]);
160 to[1] = FTOCHAR(tof[1]);
161 to[2] = FTOCHAR(tof[2]);
167 for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof+=4) {
168 const float d = (BLI_frand()-0.5)*dither;
176 to[0] = FTOCHAR(col[0]);
177 to[1] = FTOCHAR(col[1]);
178 to[2] = FTOCHAR(col[2]);
179 to[3] = FTOCHAR(col[3]);
182 for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof+=4) {
183 to[0] = FTOCHAR(tof[0]);
184 to[1] = FTOCHAR(tof[1]);
185 to[2] = FTOCHAR(tof[2]);
186 to[3] = FTOCHAR(tof[3]);
191 /* ensure user flag is reset */
192 ibuf->userflags &= ~IB_RECT_INVALID;
195 static void imb_float_from_rect_nonlinear(struct ImBuf *ibuf, float *fbuf)
199 unsigned char *to = (unsigned char *) ibuf->rect;
201 for (i = ibuf->x * ibuf->y; i > 0; i--)
203 tof[0] = ((float)to[0])*(1.0f/255.0f);
204 tof[1] = ((float)to[1])*(1.0f/255.0f);
205 tof[2] = ((float)to[2])*(1.0f/255.0f);
206 tof[3] = ((float)to[3])*(1.0f/255.0f);
213 static void imb_float_from_rect_linear(struct ImBuf *ibuf, float *fbuf)
217 unsigned char *to = (unsigned char *) ibuf->rect;
219 for (i = ibuf->x * ibuf->y; i > 0; i--)
221 tof[0] = srgb_to_linearrgb(((float)to[0])*(1.0f/255.0f));
222 tof[1] = srgb_to_linearrgb(((float)to[1])*(1.0f/255.0f));
223 tof[2] = srgb_to_linearrgb(((float)to[2])*(1.0f/255.0f));
224 tof[3] = ((float)to[3])*(1.0f/255.0f);
230 void IMB_float_from_rect(struct ImBuf *ibuf)
232 /* quick method to convert byte to floatbuf */
233 if(ibuf->rect==NULL) return;
234 if(ibuf->rect_float==NULL) {
235 if (imb_addrectfloatImBuf(ibuf) == 0) return;
238 /* Float bufs should be stored linear */
240 if (ibuf->profile != IB_PROFILE_NONE) {
241 /* if the image has been given a profile then we're working
242 * with color management in mind, so convert it to linear space */
243 imb_float_from_rect_linear(ibuf, ibuf->rect_float);
245 imb_float_from_rect_nonlinear(ibuf, ibuf->rect_float);
249 /* no profile conversion */
250 void IMB_float_from_rect_simple(struct ImBuf *ibuf)
252 if(ibuf->rect_float==NULL)
253 imb_addrectfloatImBuf(ibuf);
254 imb_float_from_rect_nonlinear(ibuf, ibuf->rect_float);
257 void IMB_convert_profile(struct ImBuf *ibuf, int profile)
262 unsigned char *rct= (unsigned char *)ibuf->rect;
263 float *rctf= ibuf->rect_float;
265 if(ibuf->profile == profile)
268 if(ELEM(ibuf->profile, IB_PROFILE_NONE, IB_PROFILE_SRGB)) { /* from */
269 if(profile == IB_PROFILE_LINEAR_RGB) { /* to */
270 if(ibuf->rect_float) {
271 for (i = ibuf->x * ibuf->y; i > 0; i--, rctf+=4) {
272 rctf[0]= srgb_to_linearrgb(rctf[0]);
273 rctf[1]= srgb_to_linearrgb(rctf[1]);
274 rctf[2]= srgb_to_linearrgb(rctf[2]);
278 for (i = ibuf->x * ibuf->y; i > 0; i--, rct+=4) {
279 rct[0]= (unsigned char)((srgb_to_linearrgb((float)rct[0]/255.0f) * 255.0f) + 0.5f);
280 rct[1]= (unsigned char)((srgb_to_linearrgb((float)rct[1]/255.0f) * 255.0f) + 0.5f);
281 rct[2]= (unsigned char)((srgb_to_linearrgb((float)rct[2]/255.0f) * 255.0f) + 0.5f);
287 else if (ibuf->profile == IB_PROFILE_LINEAR_RGB) { /* from */
288 if(ELEM(profile, IB_PROFILE_NONE, IB_PROFILE_SRGB)) { /* to */
289 if(ibuf->rect_float) {
290 for (i = ibuf->x * ibuf->y; i > 0; i--, rctf+=4) {
291 rctf[0]= linearrgb_to_srgb(rctf[0]);
292 rctf[1]= linearrgb_to_srgb(rctf[1]);
293 rctf[2]= linearrgb_to_srgb(rctf[2]);
297 for (i = ibuf->x * ibuf->y; i > 0; i--, rct+=4) {
298 rct[0]= (unsigned char)((linearrgb_to_srgb((float)rct[0]/255.0f) * 255.0f) + 0.5f);
299 rct[1]= (unsigned char)((linearrgb_to_srgb((float)rct[1]/255.0f) * 255.0f) + 0.5f);
300 rct[2]= (unsigned char)((linearrgb_to_srgb((float)rct[2]/255.0f) * 255.0f) + 0.5f);
308 printf("IMB_convert_profile: failed profile conversion %d -> %d\n", ibuf->profile, profile);
312 ibuf->profile= profile;
315 /* use when you need to get a buffer with a certain profile
317 float *IMB_float_profile_ensure(struct ImBuf *ibuf, int profile, int *alloc)
319 /* stupid but it works like this everywhere now */
320 const short is_lin_from= (ibuf->profile != IB_PROFILE_NONE);
321 const short is_lin_to= (profile != IB_PROFILE_NONE);
324 if(is_lin_from == is_lin_to) {
327 /* simple case, just allocate the buffer and return */
328 if(ibuf->rect_float == NULL) {
329 IMB_float_from_rect(ibuf);
332 return ibuf->rect_float;
335 /* conversion is needed, first check */
336 float *fbuf= MEM_mallocN(ibuf->x * ibuf->y * sizeof(float) * 4, "IMB_float_profile_ensure");
339 if(ibuf->rect_float == NULL) {
341 imb_float_from_rect_linear(ibuf, fbuf);
344 imb_float_from_rect_nonlinear(ibuf, fbuf);
348 if(is_lin_to) { /* lin -> nonlin */
349 linearrgb_to_srgb_rgba_rgba_buf(fbuf, ibuf->rect_float, ibuf->x * ibuf->y);
351 else { /* nonlin -> lin */
352 srgb_to_linearrgb_rgba_rgba_buf(fbuf, ibuf->rect_float, ibuf->x * ibuf->y);