Color management refactoiring and some extra options
[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.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                         IMB_colormanage_flags_free(ibuf);
167                         IMB_colormanage_cache_data_free(ibuf);
168                         MEM_freeN(ibuf);
169                 }
170         }
171 }
172
173 void IMB_refImBuf(ImBuf *ibuf)
174 {
175         ibuf->refcounter++;
176 }
177
178 ImBuf *IMB_makeSingleUser(ImBuf *ibuf)
179 {
180         ImBuf *rval;
181
182         if (!ibuf || ibuf->refcounter == 0) { return ibuf; }
183
184         rval = IMB_dupImBuf(ibuf);
185
186         IMB_freeImBuf(ibuf);
187
188         return rval;
189 }
190
191 short addzbufImBuf(ImBuf *ibuf)
192 {
193         int size;
194         
195         if (ibuf == NULL) return FALSE;
196         
197         IMB_freezbufImBuf(ibuf);
198         
199         size = ibuf->x * ibuf->y * sizeof(unsigned int);
200         if ((ibuf->zbuf = MEM_mapallocN(size, "addzbufImBuf"))) {
201                 ibuf->mall |= IB_zbuf;
202                 ibuf->flags |= IB_zbuf;
203                 return TRUE;
204         }
205         
206         return FALSE;
207 }
208
209 short addzbuffloatImBuf(ImBuf *ibuf)
210 {
211         int size;
212         
213         if (ibuf == NULL) return FALSE;
214         
215         IMB_freezbuffloatImBuf(ibuf);
216         
217         size = ibuf->x * ibuf->y * sizeof(float);
218         if ((ibuf->zbuf_float = MEM_mapallocN(size, "addzbuffloatImBuf"))) {
219                 ibuf->mall |= IB_zbuffloat;
220                 ibuf->flags |= IB_zbuffloat;
221                 return TRUE;
222         }
223         
224         return FALSE;
225 }
226
227
228 short imb_addencodedbufferImBuf(ImBuf *ibuf)
229 {
230         if (ibuf == NULL) return FALSE;
231
232         freeencodedbufferImBuf(ibuf);
233
234         if (ibuf->encodedbuffersize == 0)
235                 ibuf->encodedbuffersize = 10000;
236
237         ibuf->encodedsize = 0;
238
239         if ((ibuf->encodedbuffer = MEM_mallocN(ibuf->encodedbuffersize, "addencodedbufferImBuf"))) {
240                 ibuf->mall |= IB_mem;
241                 ibuf->flags |= IB_mem;
242                 return TRUE;
243         }
244
245         return FALSE;
246 }
247
248
249 short imb_enlargeencodedbufferImBuf(ImBuf *ibuf)
250 {
251         unsigned int newsize, encodedsize;
252         void *newbuffer;
253
254         if (ibuf == NULL) return FALSE;
255
256         if (ibuf->encodedbuffersize < ibuf->encodedsize) {
257                 printf("imb_enlargeencodedbufferImBuf: error in parameters\n");
258                 return FALSE;
259         }
260
261         newsize = 2 * ibuf->encodedbuffersize;
262         if (newsize < 10000) newsize = 10000;
263
264         newbuffer = MEM_mallocN(newsize, "enlargeencodedbufferImBuf");
265         if (newbuffer == NULL) return FALSE;
266
267         if (ibuf->encodedbuffer) {
268                 memcpy(newbuffer, ibuf->encodedbuffer, ibuf->encodedsize);
269         }
270         else {
271                 ibuf->encodedsize = 0;
272         }
273
274         encodedsize = ibuf->encodedsize;
275
276         freeencodedbufferImBuf(ibuf);
277
278         ibuf->encodedbuffersize = newsize;
279         ibuf->encodedsize = encodedsize;
280         ibuf->encodedbuffer = newbuffer;
281         ibuf->mall |= IB_mem;
282         ibuf->flags |= IB_mem;
283
284         return TRUE;
285 }
286
287 short imb_addrectfloatImBuf(ImBuf *ibuf)
288 {
289         int size;
290         
291         if (ibuf == NULL) return FALSE;
292         
293         if (ibuf->rect_float)
294                 imb_freerectfloatImBuf(ibuf);  /* frees mipmap too, hrm */
295         
296         size = ibuf->x * ibuf->y;
297         size = size * 4 * sizeof(float);
298         ibuf->channels = 4;
299         
300         if ((ibuf->rect_float = MEM_mapallocN(size, "imb_addrectfloatImBuf"))) {
301                 ibuf->mall |= IB_rectfloat;
302                 ibuf->flags |= IB_rectfloat;
303                 return TRUE;
304         }
305         
306         return FALSE;
307 }
308
309 /* question; why also add zbuf? */
310 short imb_addrectImBuf(ImBuf *ibuf)
311 {
312         int size;
313
314         if (ibuf == NULL) return FALSE;
315         
316         /* don't call imb_freerectImBuf, it frees mipmaps, this call is used only too give float buffers display */
317         if (ibuf->rect && (ibuf->mall & IB_rect))
318                 MEM_freeN(ibuf->rect);
319         ibuf->rect = NULL;
320         
321         size = ibuf->x * ibuf->y;
322         size = size * sizeof(unsigned int);
323
324         if ((ibuf->rect = MEM_mapallocN(size, "imb_addrectImBuf"))) {
325                 ibuf->mall |= IB_rect;
326                 ibuf->flags |= IB_rect;
327                 if (ibuf->planes > 32) return (addzbufImBuf(ibuf));
328                 else return TRUE;
329         }
330
331         return FALSE;
332 }
333
334 short imb_addtilesImBuf(ImBuf *ibuf)
335 {
336         if (ibuf == NULL) return FALSE;
337
338         if (!ibuf->tiles)
339                 if ((ibuf->tiles = MEM_callocN(sizeof(unsigned int *) * ibuf->xtiles * ibuf->ytiles, "imb_tiles")))
340                         ibuf->mall |= IB_tiles;
341
342         return (ibuf->tiles != NULL);
343 }
344
345 ImBuf *IMB_allocImBuf(unsigned int x, unsigned int y, uchar planes, unsigned int flags)
346 {
347         ImBuf *ibuf;
348
349         ibuf = MEM_callocN(sizeof(ImBuf), "ImBuf_struct");
350
351         if (ibuf) {
352                 ibuf->x = x;
353                 ibuf->y = y;
354                 ibuf->planes = planes;
355                 ibuf->ftype = TGA;
356                 ibuf->channels = 4;  /* float option, is set to other values when buffers get assigned */
357                 ibuf->ppm[0] = ibuf->ppm[1] = IMB_DPI_DEFAULT / 0.0254; /* IMB_DPI_DEFAULT -> pixels-per-meter */
358                 
359                 if (flags & IB_rect) {
360                         if (imb_addrectImBuf(ibuf) == FALSE) {
361                                 IMB_freeImBuf(ibuf);
362                                 return NULL;
363                         }
364                 }
365                 
366                 if (flags & IB_rectfloat) {
367                         if (imb_addrectfloatImBuf(ibuf) == FALSE) {
368                                 IMB_freeImBuf(ibuf);
369                                 return NULL;
370                         }
371                 }
372                 
373                 if (flags & IB_zbuf) {
374                         if (addzbufImBuf(ibuf) == FALSE) {
375                                 IMB_freeImBuf(ibuf);
376                                 return NULL;
377                         }
378                 }
379                 
380                 if (flags & IB_zbuffloat) {
381                         if (addzbuffloatImBuf(ibuf) == FALSE) {
382                                 IMB_freeImBuf(ibuf);
383                                 return NULL;
384                         }
385                 }
386         }
387         return (ibuf);
388 }
389
390 /* does no zbuffers? */
391 ImBuf *IMB_dupImBuf(ImBuf *ibuf1)
392 {
393         ImBuf *ibuf2, tbuf;
394         int flags = 0;
395         int a, x, y;
396         
397         if (ibuf1 == NULL) return NULL;
398
399         if (ibuf1->rect) flags |= IB_rect;
400         if (ibuf1->rect_float) flags |= IB_rectfloat;
401
402         x = ibuf1->x;
403         y = ibuf1->y;
404         if (ibuf1->flags & IB_fields) y *= 2;
405         
406         ibuf2 = IMB_allocImBuf(x, y, ibuf1->planes, flags);
407         if (ibuf2 == NULL) return NULL;
408
409         if (flags & IB_rect)
410                 memcpy(ibuf2->rect, ibuf1->rect, x * y * sizeof(int));
411         
412         if (flags & IB_rectfloat)
413                 memcpy(ibuf2->rect_float, ibuf1->rect_float, ibuf1->channels * x * y * sizeof(float));
414
415         if (ibuf1->encodedbuffer) {
416                 ibuf2->encodedbuffersize = ibuf1->encodedbuffersize;
417                 if (imb_addencodedbufferImBuf(ibuf2) == FALSE) {
418                         IMB_freeImBuf(ibuf2);
419                         return NULL;
420                 }
421
422                 memcpy(ibuf2->encodedbuffer, ibuf1->encodedbuffer, ibuf1->encodedsize);
423         }
424
425         /* silly trick to copy the entire contents of ibuf1 struct over to ibuf */
426         tbuf = *ibuf1;
427         
428         // fix pointers 
429         tbuf.rect          = ibuf2->rect;
430         tbuf.rect_float    = ibuf2->rect_float;
431         tbuf.encodedbuffer = ibuf2->encodedbuffer;
432         tbuf.zbuf          = NULL;
433         tbuf.zbuf_float    = NULL;
434         for (a = 0; a < IB_MIPMAP_LEVELS; a++)
435                 tbuf.mipmap[a] = NULL;
436         
437         // set malloc flag
438         tbuf.mall               = ibuf2->mall;
439         tbuf.c_handle           = NULL;
440         tbuf.refcounter         = 0;
441
442         // for now don't duplicate metadata
443         tbuf.metadata = NULL;
444
445         *ibuf2 = tbuf;
446
447         IMB_display_buffer_invalidate(ibuf2);
448         IMB_colormanage_cache_data_free(ibuf2);
449
450         ibuf2->colormanage_flags &= ~ IMB_COLORMANAGED;
451
452         return(ibuf2);
453 }
454
455 #if 0 /* remove? - campbell */
456 /* support for cache limiting */
457
458 static void imbuf_cache_destructor(void *data)
459 {
460         ImBuf *ibuf = (ImBuf *) data;
461
462         imb_freerectImBuf(ibuf);
463         imb_freerectfloatImBuf(ibuf);
464         IMB_freezbufImBuf(ibuf);
465         IMB_freezbuffloatImBuf(ibuf);
466         freeencodedbufferImBuf(ibuf);
467
468         ibuf->c_handle = NULL;
469 }
470
471
472 static MEM_CacheLimiterC **get_imbuf_cache_limiter(void)
473 {
474         static MEM_CacheLimiterC *c = NULL;
475
476         if (!c)
477                 c = new_MEM_CacheLimiter(imbuf_cache_destructor, NULL);
478
479         return &c;
480 }
481 #endif