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