Orange: more exr & imbuf cleanup
[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                 ibuf->flags |= IB_zbuf;
150                 return (TRUE);
151         }
152
153         return (FALSE);
154 }
155
156
157 short imb_addencodedbufferImBuf(struct ImBuf * ibuf)
158 {
159         if (ibuf==NULL) return(FALSE);
160
161         freeencodedbufferImBuf(ibuf);
162
163         if (ibuf->encodedbuffersize == 0) 
164                 ibuf->encodedbuffersize = 10000;
165
166         ibuf->encodedsize = 0;
167
168         if ( (ibuf->encodedbuffer = MEM_mallocN(ibuf->encodedbuffersize, "addencodedbufferImBuf") )){
169                 ibuf->mall |= IB_mem;
170                 ibuf->flags |= IB_mem;
171                 return (TRUE);
172         }
173
174         return (FALSE);
175 }
176
177
178 short imb_enlargeencodedbufferImBuf(struct ImBuf * ibuf)
179 {
180         unsigned int newsize, encodedsize;
181         void *newbuffer;
182
183         if (ibuf==NULL) return(FALSE);
184
185         if (ibuf->encodedbuffersize < ibuf->encodedsize) {
186                 printf("imb_enlargeencodedbufferImBuf: error in parameters\n");
187                 return(FALSE);
188         }
189
190         newsize = 2 * ibuf->encodedbuffersize;
191         if (newsize < 10000) newsize = 10000;
192
193         newbuffer = MEM_mallocN(newsize, "enlargeencodedbufferImBuf");
194         if (newbuffer == NULL) return(FALSE);
195
196         if (ibuf->encodedbuffer) {
197                 memcpy(newbuffer, ibuf->encodedbuffer, ibuf->encodedsize);
198         } else {
199                 ibuf->encodedsize = 0;
200         }
201
202         encodedsize = ibuf->encodedsize;
203
204         freeencodedbufferImBuf(ibuf);
205
206         ibuf->encodedbuffersize = newsize;
207         ibuf->encodedsize = encodedsize;
208         ibuf->encodedbuffer = newbuffer;
209         ibuf->mall |= IB_mem;
210         ibuf->flags |= IB_mem;
211
212         return (TRUE);
213 }
214
215 short imb_addrectfloatImBuf(struct ImBuf * ibuf)
216 {
217         int size;
218         
219         if (ibuf==NULL) return(FALSE);
220         
221         imb_freerectfloatImBuf(ibuf);
222         
223         size = ibuf->x * ibuf->y;
224         size = size * 4 * sizeof(float);
225         
226         if ( (ibuf->rect_float = MEM_mallocN(size, "imb_addrectfloatImBuf")) ){
227                 ibuf->mall |= IB_rectfloat;
228                 ibuf->flags |= IB_rectfloat;
229                 return (TRUE);
230         }
231         
232         return (FALSE);
233 }
234
235 /* question; why also add zbuf? */
236 short imb_addrectImBuf(struct ImBuf * ibuf)
237 {
238         int size;
239
240         if (ibuf==NULL) return(FALSE);
241         imb_freerectImBuf(ibuf);
242
243         size = ibuf->x * ibuf->y;
244         size = size * sizeof(unsigned int);
245
246         if ( (ibuf->rect = MEM_mallocN(size, "imb_addrectImBuf")) ){
247                 ibuf->mall |= IB_rect;
248                 ibuf->flags |= IB_rect;
249                 if (ibuf->depth > 32) return (addzbufImBuf(ibuf));
250                 else return (TRUE);
251         }
252
253         return (FALSE);
254 }
255
256
257 short imb_addcmapImBuf(struct ImBuf *ibuf)
258 {
259         int min;
260         
261         if (ibuf==NULL) return(FALSE);
262         IMB_freecmapImBuf(ibuf);
263
264         imb_checkncols(ibuf);
265         if (ibuf->maxcol == 0) return (TRUE);
266
267         if ( (ibuf->cmap = MEM_callocN(sizeof(unsigned int) * ibuf->maxcol, "imb_addcmapImBuf") ) ){
268                 min = ibuf->maxcol * sizeof(unsigned int);
269                 if (min > sizeof(dfltcmap)) min = sizeof(dfltcmap);
270                 memcpy(ibuf->cmap, dfltcmap, min);
271                 ibuf->mall |= IB_cmap;
272                 ibuf->flags |= IB_cmap;
273                 return (TRUE);
274         }
275
276         return (FALSE);
277 }
278
279
280 short imb_addplanesImBuf(struct ImBuf *ibuf)
281 {
282         int size;
283         short skipx,d,y;
284         unsigned int **planes;
285         unsigned int *point2;
286
287         if (ibuf==NULL) return(FALSE);
288         imb_freeplanesImBuf(ibuf);
289
290         skipx = ((ibuf->x+31) >> 5);
291         ibuf->skipx=skipx;
292         y=ibuf->y;
293         d=ibuf->depth;
294
295         planes = MEM_mallocN( (d*skipx*y)*sizeof(int) + d*sizeof(int *), "imb_addplanesImBuf");
296         
297         ibuf->planes = planes;
298         if (planes==0) return (FALSE);
299
300         point2 = (unsigned int *)(planes+d);
301         size = skipx*y;
302
303         for (;d>0;d--){
304                 *(planes++) = point2;
305                 point2 += size;
306         }
307         ibuf->mall |= IB_planes;
308         ibuf->flags |= IB_planes;
309
310         return (TRUE);
311 }
312
313
314 struct ImBuf *IMB_allocImBuf(short x, short y, uchar d, unsigned int flags, uchar bitmap)
315 {
316         struct ImBuf *ibuf;
317
318         ibuf = MEM_callocN(sizeof(struct ImBuf), "ImBuf_struct");
319         if (bitmap) flags |= IB_planes;
320
321         if (ibuf){
322                 ibuf->x= x;
323                 ibuf->y= y;
324                 ibuf->depth= d;
325                 ibuf->ftype= TGA;
326
327                 if (flags & IB_rect){
328                         if (imb_addrectImBuf(ibuf)==FALSE){
329                                 IMB_freeImBuf(ibuf);
330                                 return NULL;
331                         }
332                 }
333                 
334                 if (flags & IB_rectfloat){
335                         if (imb_addrectfloatImBuf(ibuf)==FALSE){
336                                 IMB_freeImBuf(ibuf);
337                                 return NULL;
338                         }
339                 }
340                 
341                 if (flags & IB_zbuf){
342                         if (addzbufImBuf(ibuf)==FALSE){
343                                 IMB_freeImBuf(ibuf);
344                                 return NULL;
345                         }
346                 }
347                 
348                 if (flags & IB_planes){
349                         if (imb_addplanesImBuf(ibuf)==FALSE){
350                                 IMB_freeImBuf(ibuf);
351                                 return NULL;
352                         }
353                 }
354         }
355         return (ibuf);
356 }
357
358 struct ImBuf *IMB_dupImBuf(struct ImBuf *ibuf1)
359 {
360         struct ImBuf *ibuf2, tbuf;
361         int flags = 0;
362         int x, y;
363         
364         if (ibuf1 == NULL) return NULL;
365
366         if (ibuf1->rect) flags |= IB_rect;
367         if (ibuf1->rect_float) flags |= IB_rectfloat;
368         if (ibuf1->planes) flags |= IB_planes;
369
370         x = ibuf1->x;
371         y = ibuf1->y;
372         if (ibuf1->flags & IB_fields) y *= 2;
373         
374         ibuf2 = IMB_allocImBuf(x, y, ibuf1->depth, flags, 0);
375         if (ibuf2 == NULL) return NULL;
376
377         if (flags & IB_rect)
378                 memcpy(ibuf2->rect, ibuf1->rect, x * y * sizeof(int));
379         
380         if (flags & IB_rectfloat)
381                 memcpy(ibuf2->rect_float, ibuf1->rect_float, x * y * sizeof(float));
382
383         if (flags & IB_planes) 
384                 memcpy(*(ibuf2->planes),*(ibuf1->planes),ibuf1->depth * ibuf1->skipx * y * sizeof(int));
385
386         if (ibuf1->encodedbuffer) {
387                 ibuf2->encodedbuffersize = ibuf1->encodedbuffersize;
388                 if (imb_addencodedbufferImBuf(ibuf2) == FALSE) {
389                         IMB_freeImBuf(ibuf2);
390                         return NULL;
391                 }
392
393                 memcpy(ibuf2->encodedbuffer, ibuf1->encodedbuffer, ibuf1->encodedsize);
394         }
395
396         /* silly trick to copy the entire contents of ibuf1 struct over to ibuf */
397         tbuf = *ibuf1;
398         
399         // fix pointers 
400         tbuf.rect               = ibuf2->rect;
401         tbuf.rect_float = ibuf2->rect_float;
402         tbuf.planes             = ibuf2->planes;
403         tbuf.cmap               = ibuf2->cmap;
404         tbuf.encodedbuffer = ibuf2->encodedbuffer;
405         
406         // set malloc flag
407         tbuf.mall               = ibuf2->mall;
408         
409         *ibuf2 = tbuf;
410         
411         if (ibuf1->cmap){
412                 imb_addcmapImBuf(ibuf2);
413                 if (ibuf2->cmap) memcpy(ibuf2->cmap,ibuf1->cmap,ibuf2->maxcol * sizeof(int));
414         }
415
416         return(ibuf2);
417 }