fix for crash when converting sequencer profiles with non-float buffers.
[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 }
192
193 static void imb_float_from_rect_nonlinear(struct ImBuf *ibuf, float *fbuf)
194 {
195         float *tof = fbuf;
196         int i;
197         unsigned char *to = (unsigned char *) ibuf->rect;
198
199         for (i = ibuf->x * ibuf->y; i > 0; i--) 
200         {
201                 tof[0] = ((float)to[0])*(1.0f/255.0f);
202                 tof[1] = ((float)to[1])*(1.0f/255.0f);
203                 tof[2] = ((float)to[2])*(1.0f/255.0f);
204                 tof[3] = ((float)to[3])*(1.0f/255.0f);
205                 to += 4; 
206                 tof += 4;
207         }
208 }
209
210
211 static void imb_float_from_rect_linear(struct ImBuf *ibuf, float *fbuf)
212 {
213         float *tof = fbuf;
214         int i;
215         unsigned char *to = (unsigned char *) ibuf->rect;
216
217         for (i = ibuf->x * ibuf->y; i > 0; i--) 
218         {
219                 tof[0] = srgb_to_linearrgb(((float)to[0])*(1.0f/255.0f));
220                 tof[1] = srgb_to_linearrgb(((float)to[1])*(1.0f/255.0f));
221                 tof[2] = srgb_to_linearrgb(((float)to[2])*(1.0f/255.0f));
222                 tof[3] = ((float)to[3])*(1.0f/255.0f);
223                 to += 4; 
224                 tof += 4;
225         }
226 }
227
228 void IMB_float_from_rect(struct ImBuf *ibuf)
229 {
230         /* quick method to convert byte to floatbuf */
231         float *tof = ibuf->rect_float;
232
233         unsigned char *to = (unsigned char *) ibuf->rect;
234         if(to==NULL) return;
235         if(tof==NULL) {
236                 if (imb_addrectfloatImBuf(ibuf) == 0) return;
237                 tof = ibuf->rect_float;
238         }
239         
240         /* Float bufs should be stored linear */
241
242         if (ibuf->profile != IB_PROFILE_NONE) {
243                 /* if the image has been given a profile then we're working 
244                  * with color management in mind, so convert it to linear space */
245                 imb_float_from_rect_linear(ibuf, ibuf->rect_float);
246         } else {
247                 imb_float_from_rect_nonlinear(ibuf, ibuf->rect_float);
248         }
249 }
250
251 /* no profile conversion */
252 void IMB_float_from_rect_simple(struct ImBuf *ibuf)
253 {
254         if(ibuf->rect_float==NULL)
255                 imb_addrectfloatImBuf(ibuf);
256         imb_float_from_rect_nonlinear(ibuf, ibuf->rect_float);
257 }
258
259 void IMB_convert_profile(struct ImBuf *ibuf, int profile)
260 {
261         int ok= FALSE;
262         int i;
263
264         unsigned char *rct= (unsigned char *)ibuf->rect;
265         float *rctf= ibuf->rect_float;
266
267         if(ibuf->profile == profile)
268                 return;
269
270         if(ELEM(ibuf->profile, IB_PROFILE_NONE, IB_PROFILE_SRGB)) { /* from */
271                 if(profile == IB_PROFILE_LINEAR_RGB) { /* to */
272                         if(ibuf->rect_float) {
273                                 for (i = ibuf->x * ibuf->y; i > 0; i--, rctf+=4) {
274                                         rctf[0]= srgb_to_linearrgb(rctf[0]);
275                                         rctf[1]= srgb_to_linearrgb(rctf[1]);
276                                         rctf[2]= srgb_to_linearrgb(rctf[2]);
277                                 }
278                         }
279                         if(ibuf->rect) {
280                                 for (i = ibuf->x * ibuf->y; i > 0; i--, rct+=4) {
281                                         rct[0]= (unsigned char)((srgb_to_linearrgb((float)rct[0]/255.0f) * 255.0f) + 0.5f);
282                                         rct[1]= (unsigned char)((srgb_to_linearrgb((float)rct[1]/255.0f) * 255.0f) + 0.5f);
283                                         rct[2]= (unsigned char)((srgb_to_linearrgb((float)rct[2]/255.0f) * 255.0f) + 0.5f);
284                                 }
285                         }
286                         ok= TRUE;
287                 }
288         }
289         else if (ibuf->profile == IB_PROFILE_LINEAR_RGB) { /* from */
290                 if(ELEM(profile, IB_PROFILE_NONE, IB_PROFILE_SRGB)) { /* to */
291                         if(ibuf->rect_float) {
292                                 for (i = ibuf->x * ibuf->y; i > 0; i--, rctf+=4) {
293                                         rctf[0]= linearrgb_to_srgb(rctf[0]);
294                                         rctf[1]= linearrgb_to_srgb(rctf[1]);
295                                         rctf[2]= linearrgb_to_srgb(rctf[2]);
296                                 }
297                         }
298                         if(ibuf->rect) {
299                                 for (i = ibuf->x * ibuf->y; i > 0; i--, rct+=4) {
300                                         rct[0]= (unsigned char)((linearrgb_to_srgb((float)rct[0]/255.0f) * 255.0f) + 0.5f);
301                                         rct[1]= (unsigned char)((linearrgb_to_srgb((float)rct[1]/255.0f) * 255.0f) + 0.5f);
302                                         rct[2]= (unsigned char)((linearrgb_to_srgb((float)rct[2]/255.0f) * 255.0f) + 0.5f);
303                                 }
304                         }
305                         ok= TRUE;
306                 }
307         }
308
309         if(ok==FALSE){
310                 printf("IMB_convert_profile: failed profile conversion %d -> %d\n", ibuf->profile, profile);
311                 return;
312         }
313
314         ibuf->profile= profile;
315 }
316
317 /* use when you need to get a buffer with a certain profile
318  * if the return  */
319 float *IMB_float_profile_ensure(struct ImBuf *ibuf, int profile, int *alloc)
320 {
321         /* stupid but it works like this everywhere now */
322         const short is_lin_from= (ibuf->profile != IB_PROFILE_NONE);
323         const short is_lin_to= (profile != IB_PROFILE_NONE);
324
325         
326         if(is_lin_from == is_lin_to) {
327                 *alloc= 0;
328
329                 /* simple case, just allocate the buffer and return */
330                 if(ibuf->rect_float == NULL) {
331                         IMB_float_from_rect(ibuf);
332                 }
333
334                 return ibuf->rect_float;
335         }
336         else {
337                 /* conversion is needed, first check */
338                 float *fbuf= MEM_mallocN(ibuf->x * ibuf->y * sizeof(float) * 4, "IMB_float_profile_ensure");
339                 *alloc= 1;
340
341                 if(ibuf->rect_float == NULL) {
342                         if(is_lin_to) {
343                                 imb_float_from_rect_linear(ibuf, fbuf);
344                         }
345                         else {
346                                 imb_float_from_rect_nonlinear(ibuf, fbuf);
347                         }
348                 }
349                 else {
350                         if(is_lin_to) { /* lin -> nonlin */
351                                 linearrgb_to_srgb_rgba_rgba_buf(fbuf, ibuf->rect_float, ibuf->x * ibuf->y);
352                         }
353                         else { /* nonlin -> lin */
354                                 srgb_to_linearrgb_rgba_rgba_buf(fbuf, ibuf->rect_float, ibuf->x * ibuf->y);
355                         }
356                 }
357
358                 return fbuf;
359         }
360 }