Orange fix: Lamp ipos were also not duplicated when indicated... old stuff
[blender.git] / source / blender / imbuf / intern / allocimbuf.c
1 /*
2  * allocimbuf.c
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version. The Blender
12  * Foundation also sells licenses for use in proprietary software under
13  * the Blender License.  See http://www.blender.org/BL/ for information
14  * about this.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26  * All rights reserved.
27  *
28  * The Original Code is: all of this file.
29  *
30  * Contributor(s): none yet.
31  *
32  * ***** END GPL/BL DUAL LICENSE BLOCK *****
33  */
34
35 /* It's become a bit messy... Basically, only the IMB_ prefixed files
36  * should remain. */
37
38 #include "IMB_imbuf_types.h"
39
40 #include "imbuf.h"
41 #include "imbuf_patch.h"
42 #include "IMB_imbuf.h"
43
44 #include "IMB_divers.h"
45 #include "IMB_allocimbuf.h"
46
47 static unsigned int dfltcmap[16] = {
48         0x00000000, 0xffffffff, 0x777777ff, 0xccccccff, 
49         0xcc3344ff, 0xdd8844ff, 0xccdd44ff, 0x888833ff, 
50         0x338844ff, 0x44dd44ff, 0x44ddccff, 0x3388ccff, 
51         0x8888ddff, 0x4433ccff, 0xcc33ccff, 0xcc88ddff
52 };
53
54 void imb_freeplanesImBuf(struct ImBuf * ibuf)
55 {
56         if (ibuf==NULL) return;
57         if (ibuf->planes){
58                 if (ibuf->mall & IB_planes) MEM_freeN(ibuf->planes);
59         }
60         ibuf->planes = 0;
61         ibuf->mall &= ~IB_planes;
62 }
63
64 void imb_freerectfloatImBuf(struct ImBuf * ibuf)
65 {
66         if (ibuf==NULL) return;
67         
68         if (ibuf->rect_float) {
69                 if (ibuf->mall & IB_rectfloat) {
70                         MEM_freeN(ibuf->rect_float);
71                         ibuf->rect_float=NULL;
72                 }
73         }
74
75         ibuf->rect_float= NULL;
76         ibuf->mall &= ~IB_rectfloat;
77 }
78
79 void imb_freerectImBuf(struct ImBuf * ibuf)
80 {
81         if (ibuf==NULL) return;
82         
83         if (ibuf->rect) {
84                 if (ibuf->mall & IB_rect) {
85                         MEM_freeN(ibuf->rect);
86                 }
87         }
88         
89         ibuf->rect= NULL;
90         ibuf->mall &= ~IB_rect;
91 }
92
93 static void freeencodedbufferImBuf(struct ImBuf * ibuf)
94 {
95         if (ibuf==NULL) return;
96         if (ibuf->encodedbuffer){
97                 if (ibuf->mall & IB_mem) MEM_freeN(ibuf->encodedbuffer);
98         }
99         ibuf->encodedbuffer = 0;
100         ibuf->encodedbuffersize = 0;
101         ibuf->encodedsize = 0;
102         ibuf->mall &= ~IB_mem;
103 }
104
105 void IMB_freezbufImBuf(struct ImBuf * ibuf)
106 {
107         if (ibuf==NULL) return;
108         if (ibuf->zbuf){
109                 if (ibuf->mall & IB_zbuf) MEM_freeN(ibuf->zbuf);
110         }
111         ibuf->zbuf= NULL;
112         ibuf->mall &= ~IB_zbuf;
113 }
114
115 void IMB_freecmapImBuf(struct ImBuf * ibuf)
116 {
117         if (ibuf==NULL) return;
118         if (ibuf->cmap){
119                 if (ibuf->mall & IB_cmap) MEM_freeN(ibuf->cmap);
120         }
121         ibuf->cmap = 0;
122         ibuf->mall &= ~IB_cmap;
123 }
124
125 void IMB_freeImBuf(struct ImBuf * ibuf)
126 {
127         if (ibuf){
128                 imb_freeplanesImBuf(ibuf);
129                 imb_freerectImBuf(ibuf);
130                 imb_freerectfloatImBuf(ibuf);
131                 IMB_freezbufImBuf(ibuf);
132                 IMB_freecmapImBuf(ibuf);
133                 freeencodedbufferImBuf(ibuf);
134                 MEM_freeN(ibuf);
135         }
136 }
137
138 short addzbufImBuf(struct ImBuf * ibuf)
139 {
140         int size;
141
142         if (ibuf==NULL) return(FALSE);
143         
144         IMB_freezbufImBuf(ibuf);
145
146         size = ibuf->x * ibuf->y * sizeof(unsigned int);
147         if ( (ibuf->zbuf = MEM_mallocN(size, "addzbufImBuf")) ){
148                 ibuf->mall |= IB_zbuf;
149                 return (TRUE);
150         }
151
152         return (FALSE);
153 }
154
155
156 short imb_addencodedbufferImBuf(struct ImBuf * ibuf)
157 {
158         if (ibuf==NULL) return(FALSE);
159
160         freeencodedbufferImBuf(ibuf);
161
162         if (ibuf->encodedbuffersize == 0) 
163                 ibuf->encodedbuffersize = 10000;
164
165         ibuf->encodedsize = 0;
166
167         if ( (ibuf->encodedbuffer = MEM_mallocN(ibuf->encodedbuffersize, "addencodedbufferImBuf") )){
168                 ibuf->mall |= IB_mem;
169                 return (TRUE);
170         }
171
172         return (FALSE);
173 }
174
175
176 short imb_enlargeencodedbufferImBuf(struct ImBuf * ibuf)
177 {
178         unsigned int newsize, encodedsize;
179         void *newbuffer;
180
181         if (ibuf==NULL) return(FALSE);
182
183         if (ibuf->encodedbuffersize < ibuf->encodedsize) {
184                 printf("imb_enlargeencodedbufferImBuf: error in parameters\n");
185                 return(FALSE);
186         }
187
188         newsize = 2 * ibuf->encodedbuffersize;
189         if (newsize < 10000) newsize = 10000;
190
191         newbuffer = MEM_mallocN(newsize, "enlargeencodedbufferImBuf");
192         if (newbuffer == NULL) return(FALSE);
193
194         if (ibuf->encodedbuffer) {
195                 memcpy(newbuffer, ibuf->encodedbuffer, ibuf->encodedsize);
196         } else {
197                 ibuf->encodedsize = 0;
198         }
199
200         encodedsize = ibuf->encodedsize;
201
202         freeencodedbufferImBuf(ibuf);
203
204         ibuf->encodedbuffersize = newsize;
205         ibuf->encodedsize = encodedsize;
206         ibuf->encodedbuffer = newbuffer;
207         ibuf->mall |= IB_mem;
208
209         return (TRUE);
210 }
211
212 short imb_addrectfloatImBuf(struct ImBuf * ibuf)
213 {
214         int size;
215         
216         if (ibuf==NULL) return(FALSE);
217         
218         imb_freerectfloatImBuf(ibuf);
219         
220         size = ibuf->x * ibuf->y;
221         size = size * 4 * sizeof(float);
222         
223         if ( (ibuf->rect_float = MEM_mallocN(size, "imb_addrectImBuf")) ){
224                 ibuf->mall |= IB_rectfloat;
225                 return (TRUE);
226         }
227         
228         return (FALSE);
229 }
230
231 /* question; why also add zbuf? */
232 short imb_addrectImBuf(struct ImBuf * ibuf)
233 {
234         int size;
235
236         if (ibuf==NULL) return(FALSE);
237         imb_freerectImBuf(ibuf);
238
239         size = ibuf->x * ibuf->y;
240         size = size * sizeof(unsigned int);
241
242         if ( (ibuf->rect = MEM_mallocN(size, "imb_addrectImBuf")) ){
243                 ibuf->mall |= IB_rect;
244                 if (ibuf->depth > 32) return (addzbufImBuf(ibuf));
245                 else return (TRUE);
246         }
247
248         return (FALSE);
249 }
250
251
252 short imb_addcmapImBuf(struct ImBuf *ibuf)
253 {
254         int min;
255         
256         if (ibuf==NULL) return(FALSE);
257         IMB_freecmapImBuf(ibuf);
258
259         imb_checkncols(ibuf);
260         if (ibuf->maxcol == 0) return (TRUE);
261
262         if ( (ibuf->cmap = MEM_callocN(sizeof(unsigned int) * ibuf->maxcol, "imb_addcmapImBuf") ) ){
263                 min = ibuf->maxcol * sizeof(unsigned int);
264                 if (min > sizeof(dfltcmap)) min = sizeof(dfltcmap);
265                 memcpy(ibuf->cmap, dfltcmap, min);
266                 ibuf->mall |= IB_cmap;
267                 return (TRUE);
268         }
269
270         return (FALSE);
271 }
272
273
274 short imb_addplanesImBuf(struct ImBuf *ibuf)
275 {
276         int size;
277         short skipx,d,y;
278         unsigned int **planes;
279         unsigned int *point2;
280
281         if (ibuf==NULL) return(FALSE);
282         imb_freeplanesImBuf(ibuf);
283
284         skipx = ((ibuf->x+31) >> 5);
285         ibuf->skipx=skipx;
286         y=ibuf->y;
287         d=ibuf->depth;
288
289         planes = MEM_mallocN( (d*skipx*y)*sizeof(int) + d*sizeof(int *), "imb_addplanesImBuf");
290         
291         ibuf->planes = planes;
292         if (planes==0) return (FALSE);
293
294         point2 = (unsigned int *)(planes+d);
295         size = skipx*y;
296
297         for (;d>0;d--){
298                 *(planes++) = point2;
299                 point2 += size;
300         }
301         ibuf->mall |= IB_planes;
302
303         return (TRUE);
304 }
305
306
307 struct ImBuf *IMB_allocImBuf(short x, short y, uchar d, unsigned int flags, uchar bitmap)
308 {
309         struct ImBuf *ibuf;
310
311         ibuf = MEM_callocN(sizeof(struct ImBuf), "ImBuf_struct");
312         if (bitmap) flags |= IB_planes;
313
314         if (ibuf){
315                 ibuf->x= x;
316                 ibuf->y= y;
317                 ibuf->depth= d;
318                 ibuf->ftype= TGA;
319
320                 if (flags & IB_rect){
321                         if (imb_addrectImBuf(ibuf)==FALSE){
322                                 IMB_freeImBuf(ibuf);
323                                 return NULL;
324                         }
325                 }
326                 
327                 if (flags & IB_rectfloat){
328                         if (imb_addrectfloatImBuf(ibuf)==FALSE){
329                                 IMB_freeImBuf(ibuf);
330                                 return NULL;
331                         }
332                 }
333                 
334                 if (flags & IB_zbuf){
335                         if (addzbufImBuf(ibuf)==FALSE){
336                                 IMB_freeImBuf(ibuf);
337                                 return NULL;
338                         }
339                 }
340                 
341                 if (flags & IB_planes){
342                         if (imb_addplanesImBuf(ibuf)==FALSE){
343                                 IMB_freeImBuf(ibuf);
344                                 return NULL;
345                         }
346                 }
347         }
348         return (ibuf);
349 }
350
351 struct ImBuf *IMB_dupImBuf(struct ImBuf *ibuf1)
352 {
353         struct ImBuf *ibuf2, tbuf;
354         int flags = 0;
355         int x, y;
356         
357         if (ibuf1 == NULL) return NULL;
358
359         if (ibuf1->rect) flags |= IB_rect;
360         if (ibuf1->rect_float) flags |= IB_rectfloat;
361         if (ibuf1->planes) flags |= IB_planes;
362
363         x = ibuf1->x;
364         y = ibuf1->y;
365         if (ibuf1->flags & IB_fields) y *= 2;
366         
367         ibuf2 = IMB_allocImBuf(x, y, ibuf1->depth, flags, 0);
368         if (ibuf2 == NULL) return NULL;
369
370         if (flags & IB_rect)
371                 memcpy(ibuf2->rect, ibuf1->rect, x * y * sizeof(int));
372         
373         if (flags & IB_rectfloat)
374                 memcpy(ibuf2->rect_float, ibuf1->rect_float, x * y * sizeof(float));
375
376         if (flags & IB_planes) 
377                 memcpy(*(ibuf2->planes),*(ibuf1->planes),ibuf1->depth * ibuf1->skipx * y * sizeof(int));
378
379         if (ibuf1->encodedbuffer) {
380                 ibuf2->encodedbuffersize = ibuf1->encodedbuffersize;
381                 if (imb_addencodedbufferImBuf(ibuf2) == FALSE) {
382                         IMB_freeImBuf(ibuf2);
383                         return NULL;
384                 }
385
386                 memcpy(ibuf2->encodedbuffer, ibuf1->encodedbuffer, ibuf1->encodedsize);
387         }
388
389         /* silly trick to copy the entire contents of ibuf1 struct over to ibuf */
390         tbuf = *ibuf1;
391         
392         // fix pointers 
393         tbuf.rect               = ibuf2->rect;
394         tbuf.rect_float = ibuf2->rect_float;
395         tbuf.planes             = ibuf2->planes;
396         tbuf.cmap               = ibuf2->cmap;
397         tbuf.encodedbuffer = ibuf2->encodedbuffer;
398         
399         // set malloc flag
400         tbuf.mall               = ibuf2->mall;
401         
402         *ibuf2 = tbuf;
403         
404         if (ibuf1->cmap){
405                 imb_addcmapImBuf(ibuf2);
406                 if (ibuf2->cmap) memcpy(ibuf2->cmap,ibuf1->cmap,ibuf2->maxcol * sizeof(int));
407         }
408
409         return(ibuf2);
410 }