code cleanup: use bool for imbuf allocation functions.
[blender.git] / source / blender / imbuf / intern / allocimbuf.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/imbuf/intern/allocimbuf.c
29  *  \ingroup imbuf
30  */
31
32
33 /* It's become a bit messy... Basically, only the IMB_ prefixed files
34  * should remain. */
35
36 #include <stddef.h>
37
38 #include "IMB_imbuf.h"
39 #include "IMB_imbuf_types.h"
40
41 #include "IMB_allocimbuf.h"
42 #include "IMB_filetype.h"
43 #include "IMB_metadata.h"
44 #include "IMB_colormanagement_intern.h"
45
46 #include "imbuf.h"
47
48 #include "MEM_guardedalloc.h"
49 #include "MEM_CacheLimiterC-Api.h"
50
51 void imb_freemipmapImBuf(ImBuf *ibuf)
52 {
53         int a;
54         
55         for (a = 1; a < ibuf->miptot; a++) {
56                 if (ibuf->mipmap[a - 1])
57                         IMB_freeImBuf(ibuf->mipmap[a - 1]);
58                 ibuf->mipmap[a - 1] = NULL;
59         }
60
61         ibuf->miptot = 0;
62 }
63
64 /* any free rect frees mipmaps to be sure, creation is in render on first request */
65 void imb_freerectfloatImBuf(ImBuf *ibuf)
66 {
67         if (ibuf == NULL) return;
68         
69         if (ibuf->rect_float && (ibuf->mall & IB_rectfloat)) {
70                 MEM_freeN(ibuf->rect_float);
71                 ibuf->rect_float = NULL;
72         }
73
74         imb_freemipmapImBuf(ibuf);
75         
76         ibuf->rect_float = NULL;
77         ibuf->mall &= ~IB_rectfloat;
78 }
79
80 /* any free rect frees mipmaps to be sure, creation is in render on first request */
81 void imb_freerectImBuf(ImBuf *ibuf)
82 {
83         if (ibuf == NULL) return;
84
85         if (ibuf->rect && (ibuf->mall & IB_rect))
86                 MEM_freeN(ibuf->rect);
87         ibuf->rect = NULL;
88         
89         imb_freemipmapImBuf(ibuf);
90
91         ibuf->mall &= ~IB_rect;
92 }
93
94 void imb_freetilesImBuf(ImBuf *ibuf)
95 {
96         int tx, ty;
97
98         if (ibuf == NULL) return;
99
100         if (ibuf->tiles && (ibuf->mall & IB_tiles)) {
101                 for (ty = 0; ty < ibuf->ytiles; ty++) {
102                         for (tx = 0; tx < ibuf->xtiles; tx++) {
103                                 if (ibuf->tiles[ibuf->xtiles * ty + tx]) {
104                                         imb_tile_cache_tile_free(ibuf, tx, ty);
105                                         MEM_freeN(ibuf->tiles[ibuf->xtiles * ty + tx]);
106                                 }
107                         }
108                 }
109
110                 MEM_freeN(ibuf->tiles);
111         }
112
113         ibuf->tiles = NULL;
114         ibuf->mall &= ~IB_tiles;
115 }
116
117 static void freeencodedbufferImBuf(ImBuf *ibuf)
118 {
119         if (ibuf == NULL) return;
120
121         if (ibuf->encodedbuffer && (ibuf->mall & IB_mem))
122                 MEM_freeN(ibuf->encodedbuffer);
123
124         ibuf->encodedbuffer = NULL;
125         ibuf->encodedbuffersize = 0;
126         ibuf->encodedsize = 0;
127         ibuf->mall &= ~IB_mem;
128 }
129
130 void IMB_freezbufImBuf(ImBuf *ibuf)
131 {
132         if (ibuf == NULL) return;
133
134         if (ibuf->zbuf && (ibuf->mall & IB_zbuf))
135                 MEM_freeN(ibuf->zbuf);
136
137         ibuf->zbuf = NULL;
138         ibuf->mall &= ~IB_zbuf;
139 }
140
141 void IMB_freezbuffloatImBuf(ImBuf *ibuf)
142 {
143         if (ibuf == NULL) return;
144
145         if (ibuf->zbuf_float && (ibuf->mall & IB_zbuffloat))
146                 MEM_freeN(ibuf->zbuf_float);
147
148         ibuf->zbuf_float = NULL;
149         ibuf->mall &= ~IB_zbuffloat;
150 }
151
152 void IMB_freeImBuf(ImBuf *ibuf)
153 {
154         if (ibuf) {
155                 if (ibuf->refcounter > 0) {
156                         ibuf->refcounter--;
157                 }
158                 else {
159                         imb_freerectImBuf(ibuf);
160                         imb_freerectfloatImBuf(ibuf);
161                         imb_freetilesImBuf(ibuf);
162                         IMB_freezbufImBuf(ibuf);
163                         IMB_freezbuffloatImBuf(ibuf);
164                         freeencodedbufferImBuf(ibuf);
165                         IMB_metadata_free(ibuf);
166                         colormanage_cache_free(ibuf);
167
168                         if (ibuf->dds_data.data != NULL) {
169                                 free(ibuf->dds_data.data); /* dds_data.data is allocated by DirectDrawSurface::readData(), so don't use MEM_freeN! */
170                         }
171                         MEM_freeN(ibuf);
172                 }
173         }
174 }
175
176 void IMB_refImBuf(ImBuf *ibuf)
177 {
178         ibuf->refcounter++;
179 }
180
181 ImBuf *IMB_makeSingleUser(ImBuf *ibuf)
182 {
183         ImBuf *rval;
184
185         if (!ibuf || ibuf->refcounter == 0) { return ibuf; }
186
187         rval = IMB_dupImBuf(ibuf);
188
189         IMB_freeImBuf(ibuf);
190
191         return rval;
192 }
193
194 bool addzbufImBuf(ImBuf *ibuf)
195 {
196         size_t size;
197         
198         if (ibuf == NULL) return false;
199         
200         IMB_freezbufImBuf(ibuf);
201         
202         size = (size_t)(ibuf->x * ibuf->y) * sizeof(unsigned int);
203
204         if ((ibuf->zbuf = MEM_mapallocN(size, __func__))) {
205                 ibuf->mall |= IB_zbuf;
206                 ibuf->flags |= IB_zbuf;
207                 return true;
208         }
209         
210         return false;
211 }
212
213 bool addzbuffloatImBuf(ImBuf *ibuf)
214 {
215         size_t size;
216         
217         if (ibuf == NULL) return false;
218         
219         IMB_freezbuffloatImBuf(ibuf);
220         
221         size = (size_t)(ibuf->x * ibuf->y) * sizeof(float);
222
223         if ((ibuf->zbuf_float = MEM_mapallocN(size, __func__))) {
224                 ibuf->mall |= IB_zbuffloat;
225                 ibuf->flags |= IB_zbuffloat;
226                 return true;
227         }
228         
229         return false;
230 }
231
232
233 bool imb_addencodedbufferImBuf(ImBuf *ibuf)
234 {
235         if (ibuf == NULL) return false;
236
237         freeencodedbufferImBuf(ibuf);
238
239         if (ibuf->encodedbuffersize == 0)
240                 ibuf->encodedbuffersize = 10000;
241
242         ibuf->encodedsize = 0;
243
244         if ((ibuf->encodedbuffer = MEM_mallocN(ibuf->encodedbuffersize, __func__))) {
245                 ibuf->mall |= IB_mem;
246                 ibuf->flags |= IB_mem;
247                 return true;
248         }
249
250         return false;
251 }
252
253
254 bool imb_enlargeencodedbufferImBuf(ImBuf *ibuf)
255 {
256         unsigned int newsize, encodedsize;
257         void *newbuffer;
258
259         if (ibuf == NULL) return false;
260
261         if (ibuf->encodedbuffersize < ibuf->encodedsize) {
262                 printf("%s: error in parameters\n", __func__);
263                 return false;
264         }
265
266         newsize = 2 * ibuf->encodedbuffersize;
267         if (newsize < 10000) newsize = 10000;
268
269         newbuffer = MEM_mallocN(newsize, __func__);
270         if (newbuffer == NULL) return false;
271
272         if (ibuf->encodedbuffer) {
273                 memcpy(newbuffer, ibuf->encodedbuffer, ibuf->encodedsize);
274         }
275         else {
276                 ibuf->encodedsize = 0;
277         }
278
279         encodedsize = ibuf->encodedsize;
280
281         freeencodedbufferImBuf(ibuf);
282
283         ibuf->encodedbuffersize = newsize;
284         ibuf->encodedsize = encodedsize;
285         ibuf->encodedbuffer = newbuffer;
286         ibuf->mall |= IB_mem;
287         ibuf->flags |= IB_mem;
288
289         return true;
290 }
291
292 bool imb_addrectfloatImBuf(ImBuf *ibuf)
293 {
294         size_t size;
295         
296         if (ibuf == NULL) return false;
297         
298         if (ibuf->rect_float)
299                 imb_freerectfloatImBuf(ibuf);  /* frees mipmap too, hrm */
300         
301         size = (size_t)(ibuf->x * ibuf->y) * sizeof(float[4]);
302
303         ibuf->channels = 4;
304         if ((ibuf->rect_float = MEM_mapallocN(size, __func__))) {
305                 ibuf->mall |= IB_rectfloat;
306                 ibuf->flags |= IB_rectfloat;
307                 return true;
308         }
309         
310         return false;
311 }
312
313 /* question; why also add zbuf? */
314 bool imb_addrectImBuf(ImBuf *ibuf)
315 {
316         size_t size;
317
318         if (ibuf == NULL) return false;
319         
320         /* don't call imb_freerectImBuf, it frees mipmaps, this call is used only too give float buffers display */
321         if (ibuf->rect && (ibuf->mall & IB_rect))
322                 MEM_freeN(ibuf->rect);
323         ibuf->rect = NULL;
324         
325         size = (size_t)(ibuf->x * ibuf->y) * sizeof(unsigned int);
326
327         if ((ibuf->rect = MEM_mapallocN(size, __func__))) {
328                 ibuf->mall |= IB_rect;
329                 ibuf->flags |= IB_rect;
330                 if (ibuf->planes > 32) {
331                         return (addzbufImBuf(ibuf));
332                 }
333                 else {
334                         return true;
335                 }
336         }
337
338         return false;
339 }
340
341 bool imb_addtilesImBuf(ImBuf *ibuf)
342 {
343         if (ibuf == NULL) return false;
344
345         if (!ibuf->tiles)
346                 if ((ibuf->tiles = MEM_callocN(sizeof(unsigned int *) * ibuf->xtiles * ibuf->ytiles, "imb_tiles")))
347                         ibuf->mall |= IB_tiles;
348
349         return (ibuf->tiles != NULL);
350 }
351
352 ImBuf *IMB_allocImBuf(unsigned int x, unsigned int y, uchar planes, unsigned int flags)
353 {
354         ImBuf *ibuf;
355
356         ibuf = MEM_callocN(sizeof(ImBuf), "ImBuf_struct");
357
358         if (ibuf) {
359                 ibuf->x = x;
360                 ibuf->y = y;
361                 ibuf->planes = planes;
362                 ibuf->ftype = PNG | 90; /* the 90 means, set compression to nearly the maximum */
363                 ibuf->channels = 4;  /* float option, is set to other values when buffers get assigned */
364                 ibuf->ppm[0] = ibuf->ppm[1] = IMB_DPI_DEFAULT / 0.0254f; /* IMB_DPI_DEFAULT -> pixels-per-meter */
365
366                 if (flags & IB_rect) {
367                         if (imb_addrectImBuf(ibuf) == false) {
368                                 IMB_freeImBuf(ibuf);
369                                 return NULL;
370                         }
371                 }
372                 
373                 if (flags & IB_rectfloat) {
374                         if (imb_addrectfloatImBuf(ibuf) == false) {
375                                 IMB_freeImBuf(ibuf);
376                                 return NULL;
377                         }
378                 }
379                 
380                 if (flags & IB_zbuf) {
381                         if (addzbufImBuf(ibuf) == false) {
382                                 IMB_freeImBuf(ibuf);
383                                 return NULL;
384                         }
385                 }
386                 
387                 if (flags & IB_zbuffloat) {
388                         if (addzbuffloatImBuf(ibuf) == false) {
389                                 IMB_freeImBuf(ibuf);
390                                 return NULL;
391                         }
392                 }
393
394                 /* assign default spaces */
395                 colormanage_imbuf_set_default_spaces(ibuf);
396         }
397         return (ibuf);
398 }
399
400 /* does no zbuffers? */
401 ImBuf *IMB_dupImBuf(ImBuf *ibuf1)
402 {
403         ImBuf *ibuf2, tbuf;
404         int flags = 0;
405         int a, x, y;
406         
407         if (ibuf1 == NULL) return NULL;
408
409         if (ibuf1->rect) flags |= IB_rect;
410         if (ibuf1->rect_float) flags |= IB_rectfloat;
411
412         x = ibuf1->x;
413         y = ibuf1->y;
414         if (ibuf1->flags & IB_fields) y *= 2;
415         
416         ibuf2 = IMB_allocImBuf(x, y, ibuf1->planes, flags);
417         if (ibuf2 == NULL) return NULL;
418
419         if (flags & IB_rect)
420                 memcpy(ibuf2->rect, ibuf1->rect, x * y * sizeof(int));
421         
422         if (flags & IB_rectfloat)
423                 memcpy(ibuf2->rect_float, ibuf1->rect_float, ibuf1->channels * x * y * sizeof(float));
424
425         if (ibuf1->encodedbuffer) {
426                 ibuf2->encodedbuffersize = ibuf1->encodedbuffersize;
427                 if (imb_addencodedbufferImBuf(ibuf2) == false) {
428                         IMB_freeImBuf(ibuf2);
429                         return NULL;
430                 }
431
432                 memcpy(ibuf2->encodedbuffer, ibuf1->encodedbuffer, ibuf1->encodedsize);
433         }
434
435         /* silly trick to copy the entire contents of ibuf1 struct over to ibuf */
436         tbuf = *ibuf1;
437         
438         /* fix pointers */
439         tbuf.rect          = ibuf2->rect;
440         tbuf.rect_float    = ibuf2->rect_float;
441         tbuf.encodedbuffer = ibuf2->encodedbuffer;
442         tbuf.zbuf          = NULL;
443         tbuf.zbuf_float    = NULL;
444         for (a = 0; a < IB_MIPMAP_LEVELS; a++)
445                 tbuf.mipmap[a] = NULL;
446         tbuf.dds_data.data = NULL;
447         
448         /* set malloc flag */
449         tbuf.mall               = ibuf2->mall;
450         tbuf.c_handle           = NULL;
451         tbuf.refcounter         = 0;
452
453         /* for now don't duplicate metadata */
454         tbuf.metadata = NULL;
455
456         tbuf.display_buffer_flags = NULL;
457         tbuf.colormanage_cache = NULL;
458
459         *ibuf2 = tbuf;
460
461         return(ibuf2);
462 }
463
464 #if 0 /* remove? - campbell */
465 /* support for cache limiting */
466
467 static void imbuf_cache_destructor(void *data)
468 {
469         ImBuf *ibuf = (ImBuf *) data;
470
471         imb_freerectImBuf(ibuf);
472         imb_freerectfloatImBuf(ibuf);
473         IMB_freezbufImBuf(ibuf);
474         IMB_freezbuffloatImBuf(ibuf);
475         freeencodedbufferImBuf(ibuf);
476
477         ibuf->c_handle = NULL;
478 }
479
480
481 static MEM_CacheLimiterC **get_imbuf_cache_limiter(void)
482 {
483         static MEM_CacheLimiterC *c = NULL;
484
485         if (!c)
486                 c = new_MEM_CacheLimiter(imbuf_cache_destructor, NULL);
487
488         return &c;
489 }
490 #endif