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