Fix #20619: subsurf + explode modifier crash.
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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
36 #include "imbuf.h"
37 #include "imbuf_patch.h"
38 #include "IMB_imbuf_types.h"
39 #include "IMB_imbuf.h"
40 #include "IMB_allocimbuf.h"
41 #include "IMB_divers.h"
42 #include "BKE_utildefines.h"
43 #include "BKE_colortools.h"
44
45 void imb_checkncols(struct ImBuf *ibuf)
46 {
47         unsigned int i;
48
49         if (ibuf==0) return;
50         
51         if (IS_amiga(ibuf)){
52                 if (IS_ham(ibuf)){
53                         if (ibuf->depth == 0) ibuf->depth = 6;
54                         ibuf->mincol = 0;
55                         ibuf->maxcol = 1 << (ibuf->depth - 2);
56                         /*printf("%d %d\n", ibuf->maxcol, ibuf->depth);*/
57                         return;
58                 } else if (IS_hbrite(ibuf)){
59                         ibuf->mincol = 0;
60                         ibuf->maxcol = 64;
61                         ibuf->depth = 6;
62                         return;
63                 }
64         }
65
66         if (ibuf->maxcol == 0){
67                 if (ibuf->depth <= 8){
68                         ibuf->mincol = 0;
69                         ibuf->maxcol = (1 << ibuf->depth);
70                         return;
71                 } else if (ibuf->depth == 0){
72                         ibuf->depth = 5;
73                         ibuf->mincol = 0;
74                         ibuf->maxcol = 32;
75                 }
76                 return;
77         } else {
78                 /* ibuf->maxcol defines the depth */
79                 for (i=1 ; ibuf->maxcol > (1 << i); i++);
80                 ibuf->depth = i;
81                 return;
82         }
83 }
84
85
86 void IMB_de_interlace(struct ImBuf *ibuf)
87 {
88         struct ImBuf * tbuf1, * tbuf2;
89         
90         if (ibuf == 0) return;
91         if (ibuf->flags & IB_fields) return;
92         ibuf->flags |= IB_fields;
93         
94         if (ibuf->rect) {
95                 /* make copies */
96                 tbuf1 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect, 0);
97                 tbuf2 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect, 0);
98                 
99                 ibuf->x *= 2;   
100                 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
101                 IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
102         
103                 ibuf->x /= 2;
104                 IMB_rectcpy(ibuf, tbuf1, 0, 0, 0, 0, tbuf1->x, tbuf1->y);
105                 IMB_rectcpy(ibuf, tbuf2, 0, tbuf2->y, 0, 0, tbuf2->x, tbuf2->y);
106                 
107                 IMB_freeImBuf(tbuf1);
108                 IMB_freeImBuf(tbuf2);
109         }
110         ibuf->y /= 2;
111 }
112
113 void IMB_interlace(struct ImBuf *ibuf)
114 {
115         struct ImBuf * tbuf1, * tbuf2;
116
117         if (ibuf == 0) return;
118         ibuf->flags &= ~IB_fields;
119
120         ibuf->y *= 2;
121
122         if (ibuf->rect) {
123                 /* make copies */
124                 tbuf1 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect, 0);
125                 tbuf2 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect, 0);
126
127                 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
128                 IMB_rectcpy(tbuf2, ibuf, 0, 0, 0, tbuf2->y, ibuf->x, ibuf->y);
129
130                 ibuf->x *= 2;
131                 IMB_rectcpy(ibuf, tbuf1, 0, 0, 0, 0, tbuf1->x, tbuf1->y);
132                 IMB_rectcpy(ibuf, tbuf2, tbuf2->x, 0, 0, 0, tbuf2->x, tbuf2->y);
133                 ibuf->x /= 2;
134
135                 IMB_freeImBuf(tbuf1);
136                 IMB_freeImBuf(tbuf2);
137         }
138 }
139
140
141 void IMB_gamwarp(struct ImBuf *ibuf, double gamma)
142 {
143         uchar gam[256];
144         int i;
145         uchar *rect;
146         float *rectf;
147
148         if (ibuf == 0) return;
149         if (gamma == 1.0) return;
150
151         rect = (uchar *) ibuf->rect;
152         rectf = ibuf->rect_float;
153
154         gamma = 1.0 / gamma;
155
156         if (rect) {
157                 for (i = 255 ; i >= 0 ; i--) 
158                         gam[i] = (255.0 * pow(i / 255.0 ,
159                                               gamma))  + 0.5;
160
161                 for (i = ibuf->x * ibuf->y ; i>0 ; i--, rect+=4){
162                         rect[0] = gam[rect[0]];
163                         rect[1] = gam[rect[1]];
164                         rect[2] = gam[rect[2]];
165                 }
166         }
167
168         if (rectf) {
169                 for (i = ibuf->x * ibuf->y ; i>0 ; i--, rectf+=4){
170                         rectf[0] = pow(rectf[0] / 255.0, gamma);
171                         rectf[1] = pow(rectf[1] / 255.0, gamma);
172                         rectf[2] = pow(rectf[2] / 255.0, gamma);
173                 }
174         }
175 }
176
177
178 /* assume converting from linear float to sRGB byte */
179 void IMB_rect_from_float(struct ImBuf *ibuf)
180 {
181         /* quick method to convert floatbuf to byte */
182         float *tof = (float *)ibuf->rect_float;
183         int do_dither = ibuf->dither != 0.f;
184         float dither= ibuf->dither / 255.0;
185         float srgb[4];
186         int i, channels= ibuf->channels;
187         short profile= ibuf->profile;
188         unsigned char *to = (unsigned char *) ibuf->rect;
189         
190         if(tof==NULL) return;
191         if(to==NULL) {
192                 imb_addrectImBuf(ibuf);
193                 to = (unsigned char *) ibuf->rect;
194         }
195         
196         if(channels==1) {
197                 for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof++)
198                         to[1]= to[2]= to[3]= to[0] = FTOCHAR(tof[0]);
199         }
200         else if (profile == IB_PROFILE_LINEAR_RGB) {
201                 if(channels == 3) {
202                         for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof+=3) {
203                                 srgb[0]= linearrgb_to_srgb(tof[0]);
204                                 srgb[1]= linearrgb_to_srgb(tof[1]);
205                                 srgb[2]= linearrgb_to_srgb(tof[2]);
206
207                                 to[0] = FTOCHAR(srgb[0]);
208                                 to[1] = FTOCHAR(srgb[1]);
209                                 to[2] = FTOCHAR(srgb[2]);
210                                 to[3] = 255;
211                         }
212                 }
213                 else if (channels == 4) {
214                         if (dither != 0.f) {
215                                 for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof+=4) {
216                                         const float d = (BLI_frand()-0.5)*dither;
217                                         
218                                         srgb[0]= d + linearrgb_to_srgb(tof[0]);
219                                         srgb[1]= d + linearrgb_to_srgb(tof[1]);
220                                         srgb[2]= d + linearrgb_to_srgb(tof[2]);
221                                         srgb[3]= d + tof[3]; 
222                                         
223                                         to[0] = FTOCHAR(srgb[0]);
224                                         to[1] = FTOCHAR(srgb[1]);
225                                         to[2] = FTOCHAR(srgb[2]);
226                                         to[3] = FTOCHAR(srgb[3]);
227                                 }
228                         } else {
229                                 floatbuf_to_srgb_byte(tof, to, 0, ibuf->x, 0, ibuf->y, ibuf->x);
230                         }
231                 }
232         }
233         else if(ELEM(profile, IB_PROFILE_NONE, IB_PROFILE_SRGB)) {
234                 if(channels==3) {
235                         for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof+=3) {
236                                 to[0] = FTOCHAR(tof[0]);
237                                 to[1] = FTOCHAR(tof[1]);
238                                 to[2] = FTOCHAR(tof[2]);
239                                 to[3] = 255;
240                         }
241                 }
242                 else {
243                         if (dither != 0.f) {
244                                 float col[3];           
245                                 for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof+=4) {
246                                         const float d = (BLI_frand()-0.5)*dither;
247                                         const float col[4] = {d+tof[0], d+tof[1], d+tof[2], d+tof[3]};
248                                         to[0] = FTOCHAR(col[0]);
249                                         to[1] = FTOCHAR(col[1]);
250                                         to[2] = FTOCHAR(col[2]);
251                                         to[3] = FTOCHAR(col[3]);
252                                 }
253                         } else {
254                                 for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof+=4) {
255                                         to[0] = FTOCHAR(tof[0]);
256                                         to[1] = FTOCHAR(tof[1]);
257                                         to[2] = FTOCHAR(tof[2]);
258                                         to[3] = FTOCHAR(tof[3]);
259                                 }
260                         }
261                 }
262         }
263 }
264
265 void IMB_float_from_rect(struct ImBuf *ibuf)
266 {
267         /* quick method to convert byte to floatbuf */
268         float *tof = ibuf->rect_float;
269         int i;
270         unsigned char *to = (unsigned char *) ibuf->rect;
271         
272         if(to==NULL) return;
273         if(tof==NULL) {
274                 imb_addrectfloatImBuf(ibuf);
275                 tof = ibuf->rect_float;
276         }
277         
278         /* Float bufs should be stored linear */
279
280         if (ibuf->profile != IB_PROFILE_NONE) {
281                 /* if the image has been given a profile then we're working 
282                  * with color management in mind, so convert it to linear space */
283                 
284                 for (i = ibuf->x * ibuf->y; i > 0; i--) 
285                 {
286                         tof[0] = srgb_to_linearrgb(((float)to[0])*(1.0f/255.0f));
287                         tof[1] = srgb_to_linearrgb(((float)to[1])*(1.0f/255.0f));
288                         tof[2] = srgb_to_linearrgb(((float)to[2])*(1.0f/255.0f));
289                         tof[3] = ((float)to[3])*(1.0f/255.0f);
290                         to += 4; 
291                         tof += 4;
292                 }
293         } else {
294                 for (i = ibuf->x * ibuf->y; i > 0; i--) 
295                 {
296                         tof[0] = ((float)to[0])*(1.0f/255.0f);
297                         tof[1] = ((float)to[1])*(1.0f/255.0f);
298                         tof[2] = ((float)to[2])*(1.0f/255.0f);
299                         tof[3] = ((float)to[3])*(1.0f/255.0f);
300                         to += 4; 
301                         tof += 4;
302                 }
303         }
304 }
305