Patch #8461, by Rob Hausauer
[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
43 void imb_checkncols(struct ImBuf *ibuf)
44 {
45         unsigned int i;
46
47         if (ibuf==0) return;
48         
49         if (IS_amiga(ibuf)){
50                 if (IS_ham(ibuf)){
51                         if (ibuf->depth == 0) ibuf->depth = 6;
52                         ibuf->mincol = 0;
53                         ibuf->maxcol = 1 << (ibuf->depth - 2);
54                         /*printf("%d %d\n", ibuf->maxcol, ibuf->depth);*/
55                         return;
56                 } else if (IS_hbrite(ibuf)){
57                         ibuf->mincol = 0;
58                         ibuf->maxcol = 64;
59                         ibuf->depth = 6;
60                         return;
61                 }
62         }
63
64         if (ibuf->maxcol == 0){
65                 if (ibuf->depth <= 8){
66                         ibuf->mincol = 0;
67                         ibuf->maxcol = (1 << ibuf->depth);
68                         return;
69                 } else if (ibuf->depth == 0){
70                         ibuf->depth = 5;
71                         ibuf->mincol = 0;
72                         ibuf->maxcol = 32;
73                 }
74                 return;
75         } else {
76                 /* ibuf->maxcol defines the depth */
77                 for (i=1 ; ibuf->maxcol > (1 << i); i++);
78                 ibuf->depth = i;
79                 return;
80         }
81 }
82
83
84 void IMB_de_interlace(struct ImBuf *ibuf)
85 {
86         struct ImBuf * tbuf1, * tbuf2;
87         
88         if (ibuf == 0) return;
89         if (ibuf->flags & IB_fields) return;
90         ibuf->flags |= IB_fields;
91         
92         if (ibuf->rect) {
93                 /* make copies */
94                 tbuf1 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect, 0);
95                 tbuf2 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect, 0);
96                 
97                 ibuf->x *= 2;   
98                 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
99                 IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
100         
101                 ibuf->x /= 2;
102                 IMB_rectcpy(ibuf, tbuf1, 0, 0, 0, 0, tbuf1->x, tbuf1->y);
103                 IMB_rectcpy(ibuf, tbuf2, 0, tbuf2->y, 0, 0, tbuf2->x, tbuf2->y);
104                 
105                 IMB_freeImBuf(tbuf1);
106                 IMB_freeImBuf(tbuf2);
107         }
108         ibuf->y /= 2;
109 }
110
111 void IMB_interlace(struct ImBuf *ibuf)
112 {
113         struct ImBuf * tbuf1, * tbuf2;
114
115         if (ibuf == 0) return;
116         ibuf->flags &= ~IB_fields;
117
118         ibuf->y *= 2;
119
120         if (ibuf->rect) {
121                 /* make copies */
122                 tbuf1 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect, 0);
123                 tbuf2 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 32, IB_rect, 0);
124
125                 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
126                 IMB_rectcpy(tbuf2, ibuf, 0, 0, 0, tbuf2->y, ibuf->x, ibuf->y);
127
128                 ibuf->x *= 2;
129                 IMB_rectcpy(ibuf, tbuf1, 0, 0, 0, 0, tbuf1->x, tbuf1->y);
130                 IMB_rectcpy(ibuf, tbuf2, tbuf2->x, 0, 0, 0, tbuf2->x, tbuf2->y);
131                 ibuf->x /= 2;
132
133                 IMB_freeImBuf(tbuf1);
134                 IMB_freeImBuf(tbuf2);
135         }
136 }
137
138
139 void IMB_gamwarp(struct ImBuf *ibuf, double gamma)
140 {
141         uchar gam[256];
142         int i;
143         uchar *rect;
144         float *rectf;
145
146         if (ibuf == 0) return;
147         if (gamma == 1.0) return;
148
149         rect = (uchar *) ibuf->rect;
150         rectf = ibuf->rect_float;
151
152         gamma = 1.0 / gamma;
153
154         if (rect) {
155                 for (i = 255 ; i >= 0 ; i--) 
156                         gam[i] = (255.0 * pow(i / 255.0 ,
157                                               gamma))  + 0.5;
158
159                 for (i = ibuf->x * ibuf->y ; i>0 ; i--, rect+=4){
160                         rect[0] = gam[rect[0]];
161                         rect[1] = gam[rect[1]];
162                         rect[2] = gam[rect[2]];
163                 }
164         }
165
166         if (rectf) {
167                 for (i = ibuf->x * ibuf->y ; i>0 ; i--, rectf+=4){
168                         rectf[0] = pow(rectf[0] / 255.0, gamma);
169                         rectf[1] = pow(rectf[1] / 255.0, gamma);
170                         rectf[2] = pow(rectf[2] / 255.0, gamma);
171                 }
172         }
173 }
174
175
176 void IMB_rect_from_float(struct ImBuf *ibuf)
177 {
178         /* quick method to convert floatbuf to byte */
179         float *tof = ibuf->rect_float;
180         float dither= ibuf->dither;
181         int i, channels= ibuf->channels;
182         unsigned char *to = (unsigned char *) ibuf->rect;
183         
184         if(tof==NULL) return;
185         if(to==NULL) {
186                 imb_addrectImBuf(ibuf);
187                 to = (unsigned char *) ibuf->rect;
188         }
189         
190         if(dither==0.0f || channels!=4) {
191                 if(channels==1) {
192                         for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof++)
193                                 to[1]= to[2]= to[3]= to[0] = FTOCHAR(tof[0]);
194                 }
195                 else if(channels==3) {
196                         for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof+=3) {
197                                 to[0] = FTOCHAR(tof[0]);
198                                 to[1] = FTOCHAR(tof[1]);
199                                 to[2] = FTOCHAR(tof[2]);
200                                 to[3] = 255;
201                         }
202                 }
203                 else {
204                         for (i = ibuf->x * ibuf->y; i > 0; i--, to+=4, tof+=4) {
205                                 to[0] = FTOCHAR(tof[0]);
206                                 to[1] = FTOCHAR(tof[1]);
207                                 to[2] = FTOCHAR(tof[2]);
208                                 to[3] = FTOCHAR(tof[3]);
209                         }
210                 }
211         }
212         else {
213                 float dither_value, col;
214                 dither= dither/255.0f;
215                 for (i = ibuf->x * ibuf->y; i > 0; i--) {
216                         dither_value = (BLI_frand()-0.5)*dither; 
217                         col= tof[0] + dither_value;
218                         to[0] = FTOCHAR(col);
219                         col= tof[1] + dither_value;
220                         to[1] = FTOCHAR(col);
221                         col= tof[2] + dither_value;
222                         to[2] = FTOCHAR(col);
223                         col= tof[3] + dither_value;
224                         to[3] = FTOCHAR(col);
225
226                         to += 4; 
227                         tof += 4;
228                 }
229         }
230 }
231
232 void IMB_float_from_rect(struct ImBuf *ibuf)
233 {
234         /* quick method to convert byte to floatbuf */
235         float *tof = ibuf->rect_float;
236         int i;
237         unsigned char *to = (unsigned char *) ibuf->rect;
238         
239         if(to==NULL) return;
240         if(tof==NULL) {
241                 imb_addrectfloatImBuf(ibuf);
242                 tof = ibuf->rect_float;
243         }
244         
245         for (i = ibuf->x * ibuf->y; i > 0; i--) 
246         {
247                 tof[0] = ((float)to[0])*(1.0f/255.0f);
248                 tof[1] = ((float)to[1])*(1.0f/255.0f);
249                 tof[2] = ((float)to[2])*(1.0f/255.0f);
250                 tof[3] = ((float)to[3])*(1.0f/255.0f);
251                 to += 4; 
252                 tof += 4;
253         }
254 }
255