Bugfix #25657
[blender.git] / source / blender / imbuf / intern / divers.c
1 /**
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
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.
9  *
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.
14  *
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.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  * allocimbuf.c
28  *
29  * $Id$
30  */
31
32 #include "BLI_blenlib.h"
33 #include "BLI_rand.h"
34 #include "BLI_math.h"
35 #include "BLI_utildefines.h"
36
37 #include "imbuf.h"
38 #include "IMB_imbuf_types.h"
39 #include "IMB_imbuf.h"
40 #include "IMB_allocimbuf.h"
41
42 #include "BKE_colortools.h"
43
44 #include "MEM_guardedalloc.h"
45
46 void IMB_de_interlace(struct ImBuf *ibuf)
47 {
48         struct ImBuf * tbuf1, * tbuf2;
49         
50         if (ibuf == 0) return;
51         if (ibuf->flags & IB_fields) return;
52         ibuf->flags |= IB_fields;
53         
54         if (ibuf->rect) {
55                 /* make copies */
56                 tbuf1 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect);
57                 tbuf2 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect);
58                 
59                 ibuf->x *= 2;   
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);
62         
63                 ibuf->x /= 2;
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);
66                 
67                 IMB_freeImBuf(tbuf1);
68                 IMB_freeImBuf(tbuf2);
69         }
70         ibuf->y /= 2;
71 }
72
73 void IMB_interlace(struct ImBuf *ibuf)
74 {
75         struct ImBuf * tbuf1, * tbuf2;
76
77         if (ibuf == 0) return;
78         ibuf->flags &= ~IB_fields;
79
80         ibuf->y *= 2;
81
82         if (ibuf->rect) {
83                 /* make copies */
84                 tbuf1 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect);
85                 tbuf2 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect);
86
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);
89
90                 ibuf->x *= 2;
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);
93                 ibuf->x /= 2;
94
95                 IMB_freeImBuf(tbuf1);
96                 IMB_freeImBuf(tbuf2);
97         }
98 }
99
100
101 /* assume converting from linear float to sRGB byte */
102 void IMB_rect_from_float(struct ImBuf *ibuf)
103 {
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;
108         float srgb[4];
109         int i, channels= ibuf->channels;
110         short profile= ibuf->profile;
111         unsigned char *to = (unsigned char *) ibuf->rect;
112         
113         if(tof==NULL) return;
114         if(to==NULL) {
115                 imb_addrectImBuf(ibuf);
116                 to = (unsigned char *) ibuf->rect;
117         }
118         
119         if(channels==1) {
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]);
122         }
123         else if (profile == IB_PROFILE_LINEAR_RGB) {
124                 if(channels == 3) {
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]);
129
130                                 to[0] = FTOCHAR(srgb[0]);
131                                 to[1] = FTOCHAR(srgb[1]);
132                                 to[2] = FTOCHAR(srgb[2]);
133                                 to[3] = 255;
134                         }
135                 }
136                 else if (channels == 4) {
137                         if (dither != 0.f) {
138                                 for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof+=4) {
139                                         const float d = (BLI_frand()-0.5)*dither;
140                                         
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]);
144                                         srgb[3]= d + tof[3]; 
145                                         
146                                         to[0] = FTOCHAR(srgb[0]);
147                                         to[1] = FTOCHAR(srgb[1]);
148                                         to[2] = FTOCHAR(srgb[2]);
149                                         to[3] = FTOCHAR(srgb[3]);
150                                 }
151                         } else {
152                                 floatbuf_to_srgb_byte(tof, to, 0, ibuf->x, 0, ibuf->y, ibuf->x);
153                         }
154                 }
155         }
156         else if(ELEM(profile, IB_PROFILE_NONE, IB_PROFILE_SRGB)) {
157                 if(channels==3) {
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]);
162                                 to[3] = 255;
163                         }
164                 }
165                 else {
166                         if (dither != 0.f) {
167                                 for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof+=4) {
168                                         const float d = (BLI_frand()-0.5)*dither;
169                                         float col[4];
170
171                                         col[0]= d + tof[0];
172                                         col[1]= d + tof[1];
173                                         col[2]= d + tof[2];
174                                         col[3]= d + tof[3];
175
176                                         to[0] = FTOCHAR(col[0]);
177                                         to[1] = FTOCHAR(col[1]);
178                                         to[2] = FTOCHAR(col[2]);
179                                         to[3] = FTOCHAR(col[3]);
180                                 }
181                         } else {
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]);
187                                 }
188                         }
189                 }
190         }
191         /* ensure user flag is reset */
192         ibuf->userflags &= ~IB_RECT_INVALID;
193 }
194
195 static void imb_float_from_rect_nonlinear(struct ImBuf *ibuf, float *fbuf)
196 {
197         float *tof = fbuf;
198         int i;
199         unsigned char *to = (unsigned char *) ibuf->rect;
200
201         for (i = ibuf->x * ibuf->y; i > 0; i--) 
202         {
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);
207                 to += 4; 
208                 tof += 4;
209         }
210 }
211
212
213 static void imb_float_from_rect_linear(struct ImBuf *ibuf, float *fbuf)
214 {
215         float *tof = fbuf;
216         int i;
217         unsigned char *to = (unsigned char *) ibuf->rect;
218
219         for (i = ibuf->x * ibuf->y; i > 0; i--) 
220         {
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);
225                 to += 4; 
226                 tof += 4;
227         }
228 }
229
230 void IMB_float_from_rect(struct ImBuf *ibuf)
231 {
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;
236         }
237         
238         /* Float bufs should be stored linear */
239
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);
244         } else {
245                 imb_float_from_rect_nonlinear(ibuf, ibuf->rect_float);
246         }
247 }
248
249 /* no profile conversion */
250 void IMB_float_from_rect_simple(struct ImBuf *ibuf)
251 {
252         if(ibuf->rect_float==NULL)
253                 imb_addrectfloatImBuf(ibuf);
254         imb_float_from_rect_nonlinear(ibuf, ibuf->rect_float);
255 }
256
257 void IMB_convert_profile(struct ImBuf *ibuf, int profile)
258 {
259         int ok= FALSE;
260         int i;
261
262         unsigned char *rct= (unsigned char *)ibuf->rect;
263         float *rctf= ibuf->rect_float;
264
265         if(ibuf->profile == profile)
266                 return;
267
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]);
275                                 }
276                         }
277                         if(ibuf->rect) {
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);
282                                 }
283                         }
284                         ok= TRUE;
285                 }
286         }
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]);
294                                 }
295                         }
296                         if(ibuf->rect) {
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);
301                                 }
302                         }
303                         ok= TRUE;
304                 }
305         }
306
307         if(ok==FALSE){
308                 printf("IMB_convert_profile: failed profile conversion %d -> %d\n", ibuf->profile, profile);
309                 return;
310         }
311
312         ibuf->profile= profile;
313 }
314
315 /* use when you need to get a buffer with a certain profile
316  * if the return  */
317 float *IMB_float_profile_ensure(struct ImBuf *ibuf, int profile, int *alloc)
318 {
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);
322
323         
324         if(is_lin_from == is_lin_to) {
325                 *alloc= 0;
326
327                 /* simple case, just allocate the buffer and return */
328                 if(ibuf->rect_float == NULL) {
329                         IMB_float_from_rect(ibuf);
330                 }
331
332                 return ibuf->rect_float;
333         }
334         else {
335                 /* conversion is needed, first check */
336                 float *fbuf= MEM_mallocN(ibuf->x * ibuf->y * sizeof(float) * 4, "IMB_float_profile_ensure");
337                 *alloc= 1;
338
339                 if(ibuf->rect_float == NULL) {
340                         if(is_lin_to) {
341                                 imb_float_from_rect_linear(ibuf, fbuf);
342                         }
343                         else {
344                                 imb_float_from_rect_nonlinear(ibuf, fbuf);
345                         }
346                 }
347                 else {
348                         if(is_lin_to) { /* lin -> nonlin */
349                                 linearrgb_to_srgb_rgba_rgba_buf(fbuf, ibuf->rect_float, ibuf->x * ibuf->y);
350                         }
351                         else { /* nonlin -> lin */
352                                 srgb_to_linearrgb_rgba_rgba_buf(fbuf, ibuf->rect_float, ibuf->x * ibuf->y);
353                         }
354                 }
355
356                 return fbuf;
357         }
358 }