~ubuntu-branches/ubuntu/gutsy/blender/gutsy-security

« back to all changes in this revision

Viewing changes to source/blender/imbuf/intern/allocimbuf.c

  • Committer: Bazaar Package Importer
  • Author(s): Lukas Fittl
  • Date: 2006-09-20 01:57:27 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20060920015727-gmoqlxwstx9wwqs3
Tags: 2.42a-1ubuntu1
* Merge from Debian unstable (Closes: Malone #55903). Remaining changes:
  - debian/genpot: Add python scripts from Lee June <blender@eyou.com> to
    generate a reasonable PO template from the sources. Since gettext is used
    in a highly nonstandard way, xgettext does not work for this job.
  - debian/rules: Call the scripts, generate po/blender.pot, and clean it up
    in the clean target.
  - Add a proper header to the generated PO template.
* debian/control: Build depend on libavformat-dev >= 3:0.cvs20060823-3.1,
  otherwise this package will FTBFS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * allocimbuf.c
3
3
 *
4
 
 * $Id: allocimbuf.c,v 1.8 2005/03/09 19:45:54 lukep Exp $
 
4
 * $Id: allocimbuf.c,v 1.13 2006/02/28 13:07:02 schlaile Exp $
5
5
 *
6
6
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
7
7
 *
43
43
 
44
44
#include "IMB_divers.h"
45
45
#include "IMB_allocimbuf.h"
 
46
#include "MEM_CacheLimiterC-Api.h"
46
47
 
47
48
static unsigned int dfltcmap[16] = {
48
49
        0x00000000, 0xffffffff, 0x777777ff, 0xccccccff, 
53
54
 
54
55
void imb_freeplanesImBuf(struct ImBuf * ibuf)
55
56
{
56
 
        if (ibuf==0) return;
 
57
        if (ibuf==NULL) return;
57
58
        if (ibuf->planes){
58
 
                if (ibuf->mall & IB_planes) free(ibuf->planes);
 
59
                if (ibuf->mall & IB_planes) MEM_freeN(ibuf->planes);
59
60
        }
60
61
        ibuf->planes = 0;
61
62
        ibuf->mall &= ~IB_planes;
62
63
}
63
64
 
 
65
void imb_freerectfloatImBuf(struct ImBuf * ibuf)
 
66
{
 
67
        if (ibuf==NULL) return;
 
68
        
 
69
        if (ibuf->rect_float) {
 
70
                if (ibuf->mall & IB_rectfloat) {
 
71
                        MEM_freeN(ibuf->rect_float);
 
72
                        ibuf->rect_float=NULL;
 
73
                }
 
74
        }
 
75
 
 
76
        ibuf->rect_float= NULL;
 
77
        ibuf->mall &= ~IB_rectfloat;
 
78
}
64
79
 
65
80
void imb_freerectImBuf(struct ImBuf * ibuf)
66
81
{
67
 
        if (ibuf==0) return;
68
 
        if (ibuf->rect){
69
 
                if (ibuf->mall & IB_rect) free(ibuf->rect);
 
82
        if (ibuf==NULL) return;
 
83
        
 
84
        if (ibuf->rect) {
 
85
                if (ibuf->mall & IB_rect) {
 
86
                        MEM_freeN(ibuf->rect);
 
87
                }
70
88
        }
71
 
        ibuf->rect=0;
 
89
        
 
90
        ibuf->rect= NULL;
72
91
        ibuf->mall &= ~IB_rect;
73
92
}
74
93
 
75
94
static void freeencodedbufferImBuf(struct ImBuf * ibuf)
76
95
{
77
 
        if (ibuf==0) return;
 
96
        if (ibuf==NULL) return;
78
97
        if (ibuf->encodedbuffer){
79
 
                if (ibuf->mall & IB_mem) free(ibuf->encodedbuffer);
 
98
                if (ibuf->mall & IB_mem) MEM_freeN(ibuf->encodedbuffer);
80
99
        }
81
100
        ibuf->encodedbuffer = 0;
82
101
        ibuf->encodedbuffersize = 0;
86
105
 
87
106
void IMB_freezbufImBuf(struct ImBuf * ibuf)
88
107
{
89
 
        if (ibuf==0) return;
 
108
        if (ibuf==NULL) return;
90
109
        if (ibuf->zbuf){
91
 
                if (ibuf->mall & IB_zbuf) free(ibuf->zbuf);
 
110
                if (ibuf->mall & IB_zbuf) MEM_freeN(ibuf->zbuf);
92
111
        }
93
 
        ibuf->zbuf=0;
 
112
        ibuf->zbuf= NULL;
94
113
        ibuf->mall &= ~IB_zbuf;
95
114
}
96
115
 
 
116
void IMB_freezbuffloatImBuf(struct ImBuf * ibuf)
 
117
{
 
118
        if (ibuf==NULL) return;
 
119
        if (ibuf->zbuf_float){
 
120
                if (ibuf->mall & IB_zbuffloat) MEM_freeN(ibuf->zbuf_float);
 
121
        }
 
122
        ibuf->zbuf_float= NULL;
 
123
        ibuf->mall &= ~IB_zbuffloat;
 
124
}
 
125
 
97
126
void IMB_freecmapImBuf(struct ImBuf * ibuf)
98
127
{
99
 
        if (ibuf == 0) return;
 
128
        if (ibuf==NULL) return;
100
129
        if (ibuf->cmap){
101
 
                if (ibuf->mall & IB_cmap) free(ibuf->cmap);
 
130
                if (ibuf->mall & IB_cmap) MEM_freeN(ibuf->cmap);
102
131
        }
103
132
        ibuf->cmap = 0;
104
133
        ibuf->mall &= ~IB_cmap;
105
134
}
106
135
 
107
 
 
108
136
void IMB_freeImBuf(struct ImBuf * ibuf)
109
137
{
110
138
        if (ibuf){
111
 
                imb_freeplanesImBuf(ibuf);
112
 
                imb_freerectImBuf(ibuf);
113
 
                IMB_freezbufImBuf(ibuf);
114
 
                IMB_freecmapImBuf(ibuf);
115
 
                freeencodedbufferImBuf(ibuf);
116
 
                free(ibuf);
 
139
                if (ibuf->refcounter > 0) {
 
140
                        ibuf->refcounter--;
 
141
                } else {
 
142
                        imb_freeplanesImBuf(ibuf);
 
143
                        imb_freerectImBuf(ibuf);
 
144
                        imb_freerectfloatImBuf(ibuf);
 
145
                        IMB_freezbufImBuf(ibuf);
 
146
                        IMB_freezbuffloatImBuf(ibuf);
 
147
                        IMB_freecmapImBuf(ibuf);
 
148
                        freeencodedbufferImBuf(ibuf);
 
149
                        IMB_cache_limiter_unmanage(ibuf);
 
150
                        MEM_freeN(ibuf);
 
151
                }
117
152
        }
118
153
}
119
154
 
 
155
void IMB_refImBuf(struct ImBuf * ibuf)
 
156
{
 
157
        ibuf->refcounter++;
 
158
}
 
159
 
120
160
short addzbufImBuf(struct ImBuf * ibuf)
121
161
{
122
162
        int size;
123
 
 
124
 
        if (ibuf==0) return(FALSE);
 
163
        
 
164
        if (ibuf==NULL) return(FALSE);
 
165
        
125
166
        IMB_freezbufImBuf(ibuf);
126
 
 
 
167
        
127
168
        size = ibuf->x * ibuf->y * sizeof(unsigned int);
128
 
        if ( (ibuf->zbuf = MEM_mallocN(size, "addzbufImBuf")) ){
 
169
        if ( (ibuf->zbuf = MEM_mapallocN(size, "addzbufImBuf")) ){
129
170
                ibuf->mall |= IB_zbuf;
 
171
                ibuf->flags |= IB_zbuf;
130
172
                return (TRUE);
131
173
        }
 
174
        
 
175
        return (FALSE);
 
176
}
132
177
 
 
178
short addzbuffloatImBuf(struct ImBuf * ibuf)
 
179
{
 
180
        int size;
 
181
        
 
182
        if (ibuf==NULL) return(FALSE);
 
183
        
 
184
        IMB_freezbuffloatImBuf(ibuf);
 
185
        
 
186
        size = ibuf->x * ibuf->y * sizeof(float);
 
187
        if ( (ibuf->zbuf_float = MEM_mapallocN(size, "addzbuffloatImBuf")) ){
 
188
                ibuf->mall |= IB_zbuffloat;
 
189
                ibuf->flags |= IB_zbuffloat;
 
190
                return (TRUE);
 
191
        }
 
192
        
133
193
        return (FALSE);
134
194
}
135
195
 
136
196
 
137
197
short imb_addencodedbufferImBuf(struct ImBuf * ibuf)
138
198
{
139
 
        if (ibuf==0) return(FALSE);
 
199
        if (ibuf==NULL) return(FALSE);
140
200
 
141
201
        freeencodedbufferImBuf(ibuf);
142
202
 
147
207
 
148
208
        if ( (ibuf->encodedbuffer = MEM_mallocN(ibuf->encodedbuffersize, "addencodedbufferImBuf") )){
149
209
                ibuf->mall |= IB_mem;
 
210
                ibuf->flags |= IB_mem;
150
211
                return (TRUE);
151
212
        }
152
213
 
159
220
        unsigned int newsize, encodedsize;
160
221
        void *newbuffer;
161
222
 
162
 
        if (ibuf==0) return(FALSE);
 
223
        if (ibuf==NULL) return(FALSE);
163
224
 
164
225
        if (ibuf->encodedbuffersize < ibuf->encodedsize) {
165
226
                printf("imb_enlargeencodedbufferImBuf: error in parameters\n");
186
247
        ibuf->encodedsize = encodedsize;
187
248
        ibuf->encodedbuffer = newbuffer;
188
249
        ibuf->mall |= IB_mem;
 
250
        ibuf->flags |= IB_mem;
189
251
 
190
252
        return (TRUE);
191
253
}
192
254
 
 
255
short imb_addrectfloatImBuf(struct ImBuf * ibuf)
 
256
{
 
257
        int size;
 
258
        
 
259
        if (ibuf==NULL) return(FALSE);
 
260
        
 
261
        imb_freerectfloatImBuf(ibuf);
 
262
        
 
263
        size = ibuf->x * ibuf->y;
 
264
        size = size * 4 * sizeof(float);
 
265
        
 
266
        if ( (ibuf->rect_float = MEM_mapallocN(size, "imb_addrectfloatImBuf")) ){
 
267
                ibuf->mall |= IB_rectfloat;
 
268
                ibuf->flags |= IB_rectfloat;
 
269
                return (TRUE);
 
270
        }
 
271
        
 
272
        return (FALSE);
 
273
}
193
274
 
 
275
/* question; why also add zbuf? */
194
276
short imb_addrectImBuf(struct ImBuf * ibuf)
195
277
{
196
278
        int size;
197
279
 
198
 
        if (ibuf==0) return(FALSE);
 
280
        if (ibuf==NULL) return(FALSE);
199
281
        imb_freerectImBuf(ibuf);
200
282
 
201
 
        size = ibuf->x * ibuf->y * sizeof(unsigned int);
202
 
        if ( (ibuf->rect = MEM_mallocN(size, "imb_addrectImBuf")) ){
 
283
        size = ibuf->x * ibuf->y;
 
284
        size = size * sizeof(unsigned int);
 
285
 
 
286
        if ( (ibuf->rect = MEM_mapallocN(size, "imb_addrectImBuf")) ){
203
287
                ibuf->mall |= IB_rect;
 
288
                ibuf->flags |= IB_rect;
204
289
                if (ibuf->depth > 32) return (addzbufImBuf(ibuf));
205
290
                else return (TRUE);
206
291
        }
213
298
{
214
299
        int min;
215
300
        
216
 
        if (ibuf==0) return(FALSE);
 
301
        if (ibuf==NULL) return(FALSE);
217
302
        IMB_freecmapImBuf(ibuf);
218
303
 
219
304
        imb_checkncols(ibuf);
224
309
                if (min > sizeof(dfltcmap)) min = sizeof(dfltcmap);
225
310
                memcpy(ibuf->cmap, dfltcmap, min);
226
311
                ibuf->mall |= IB_cmap;
 
312
                ibuf->flags |= IB_cmap;
227
313
                return (TRUE);
228
314
        }
229
315
 
238
324
        unsigned int **planes;
239
325
        unsigned int *point2;
240
326
 
241
 
        if (ibuf==0) return(FALSE);
 
327
        if (ibuf==NULL) return(FALSE);
242
328
        imb_freeplanesImBuf(ibuf);
243
329
 
244
330
        skipx = ((ibuf->x+31) >> 5);
259
345
                point2 += size;
260
346
        }
261
347
        ibuf->mall |= IB_planes;
 
348
        ibuf->flags |= IB_planes;
262
349
 
263
350
        return (TRUE);
264
351
}
265
352
 
266
353
 
267
 
struct ImBuf *IMB_allocImBuf(short x,short y,uchar d,unsigned int flags,uchar bitmap)
 
354
struct ImBuf *IMB_allocImBuf(short x, short y, uchar d, unsigned int flags, uchar bitmap)
268
355
{
269
356
        struct ImBuf *ibuf;
270
357
 
272
359
        if (bitmap) flags |= IB_planes;
273
360
 
274
361
        if (ibuf){
275
 
                ibuf->x=x;
276
 
                ibuf->y=y;
277
 
                ibuf->depth=d;
278
 
                ibuf->ftype=TGA;
 
362
                ibuf->x= x;
 
363
                ibuf->y= y;
 
364
                ibuf->depth= d;
 
365
                ibuf->ftype= TGA;
279
366
 
280
367
                if (flags & IB_rect){
281
368
                        if (imb_addrectImBuf(ibuf)==FALSE){
282
369
                                IMB_freeImBuf(ibuf);
283
 
                                return (0);
 
370
                                return NULL;
 
371
                        }
 
372
                }
 
373
                
 
374
                if (flags & IB_rectfloat){
 
375
                        if (imb_addrectfloatImBuf(ibuf)==FALSE){
 
376
                                IMB_freeImBuf(ibuf);
 
377
                                return NULL;
284
378
                        }
285
379
                }
286
380
                
287
381
                if (flags & IB_zbuf){
288
382
                        if (addzbufImBuf(ibuf)==FALSE){
289
383
                                IMB_freeImBuf(ibuf);
290
 
                                return (0);
 
384
                                return NULL;
 
385
                        }
 
386
                }
 
387
                
 
388
                if (flags & IB_zbuffloat){
 
389
                        if (addzbuffloatImBuf(ibuf)==FALSE){
 
390
                                IMB_freeImBuf(ibuf);
 
391
                                return NULL;
291
392
                        }
292
393
                }
293
394
                
294
395
                if (flags & IB_planes){
295
396
                        if (imb_addplanesImBuf(ibuf)==FALSE){
296
397
                                IMB_freeImBuf(ibuf);
297
 
                                return (0);
 
398
                                return NULL;
298
399
                        }
299
400
                }
300
401
        }
301
402
        return (ibuf);
302
403
}
303
404
 
 
405
/* does no zbuffers? */
304
406
struct ImBuf *IMB_dupImBuf(struct ImBuf *ibuf1)
305
407
{
306
408
        struct ImBuf *ibuf2, tbuf;
307
409
        int flags = 0;
308
410
        int x, y;
309
411
        
310
 
        if (ibuf1 == 0) return (0);
 
412
        if (ibuf1 == NULL) return NULL;
311
413
 
312
414
        if (ibuf1->rect) flags |= IB_rect;
 
415
        if (ibuf1->rect_float) flags |= IB_rectfloat;
313
416
        if (ibuf1->planes) flags |= IB_planes;
314
417
 
315
418
        x = ibuf1->x;
317
420
        if (ibuf1->flags & IB_fields) y *= 2;
318
421
        
319
422
        ibuf2 = IMB_allocImBuf(x, y, ibuf1->depth, flags, 0);
320
 
        if (ibuf2 == 0) return (0);
321
 
 
322
 
        if (flags & IB_rect) memcpy(ibuf2->rect,ibuf1->rect,x * y * sizeof(int));
323
 
        if (flags & IB_planes) memcpy(*(ibuf2->planes),*(ibuf1->planes),ibuf1->depth * ibuf1->skipx * y * sizeof(int));
 
423
        if (ibuf2 == NULL) return NULL;
 
424
 
 
425
        if (flags & IB_rect)
 
426
                memcpy(ibuf2->rect, ibuf1->rect, x * y * sizeof(int));
 
427
        
 
428
        if (flags & IB_rectfloat)
 
429
                memcpy(ibuf2->rect_float, ibuf1->rect_float, 4 * x * y * sizeof(float));
 
430
 
 
431
        if (flags & IB_planes) 
 
432
                memcpy(*(ibuf2->planes),*(ibuf1->planes),ibuf1->depth * ibuf1->skipx * y * sizeof(int));
324
433
 
325
434
        if (ibuf1->encodedbuffer) {
326
435
                ibuf2->encodedbuffersize = ibuf1->encodedbuffersize;
327
436
                if (imb_addencodedbufferImBuf(ibuf2) == FALSE) {
328
437
                        IMB_freeImBuf(ibuf2);
329
 
                        return(0);
 
438
                        return NULL;
330
439
                }
331
440
 
332
441
                memcpy(ibuf2->encodedbuffer, ibuf1->encodedbuffer, ibuf1->encodedsize);
333
442
        }
334
443
 
335
 
 
 
444
        /* silly trick to copy the entire contents of ibuf1 struct over to ibuf */
336
445
        tbuf = *ibuf1;
337
446
        
338
 
        // pointers goedzetten
 
447
        // fix pointers 
339
448
        tbuf.rect               = ibuf2->rect;
 
449
        tbuf.rect_float = ibuf2->rect_float;
340
450
        tbuf.planes             = ibuf2->planes;
341
451
        tbuf.cmap               = ibuf2->cmap;
342
452
        tbuf.encodedbuffer = ibuf2->encodedbuffer;
 
453
        tbuf.zbuf= NULL;
 
454
        tbuf.zbuf_float= NULL;
343
455
        
344
 
        // malloc flag goed zetten
 
456
        // set malloc flag
345
457
        tbuf.mall               = ibuf2->mall;
346
 
        
 
458
        tbuf.c_handle           = 0;
 
459
 
347
460
        *ibuf2 = tbuf;
348
461
        
349
462
        if (ibuf1->cmap){
353
466
 
354
467
        return(ibuf2);
355
468
}
 
469
 
 
470
/* support for cache limiting */
 
471
 
 
472
static void imbuf_cache_destructor(void * data)
 
473
{
 
474
        struct ImBuf * ibuf = (struct ImBuf*) data;
 
475
 
 
476
        imb_freeplanesImBuf(ibuf);
 
477
        imb_freerectImBuf(ibuf);
 
478
        imb_freerectfloatImBuf(ibuf);
 
479
        IMB_freezbufImBuf(ibuf);
 
480
        IMB_freezbuffloatImBuf(ibuf);
 
481
        IMB_freecmapImBuf(ibuf);
 
482
        freeencodedbufferImBuf(ibuf);
 
483
 
 
484
        ibuf->c_handle = 0;
 
485
}
 
486
 
 
487
static MEM_CacheLimiterC ** get_imbuf_cache_limiter()
 
488
{
 
489
        static MEM_CacheLimiterC * c = 0;
 
490
        if (!c) {
 
491
                c = new_MEM_CacheLimiter(imbuf_cache_destructor);
 
492
        }
 
493
        return &c;
 
494
}
 
495
 
 
496
void IMB_free_cache_limiter()
 
497
{
 
498
        delete_MEM_CacheLimiter(*get_imbuf_cache_limiter());
 
499
        *get_imbuf_cache_limiter() = 0;
 
500
}
 
501
 
 
502
void IMB_cache_limiter_insert(struct ImBuf * i)
 
503
{
 
504
        if (!i->c_handle) {
 
505
                i->c_handle = MEM_CacheLimiter_insert(
 
506
                        *get_imbuf_cache_limiter(), i);
 
507
                MEM_CacheLimiter_ref(i->c_handle);
 
508
                MEM_CacheLimiter_enforce_limits(
 
509
                        *get_imbuf_cache_limiter());
 
510
                MEM_CacheLimiter_unref(i->c_handle);
 
511
        }
 
512
}
 
513
 
 
514
void IMB_cache_limiter_unmanage(struct ImBuf * i)
 
515
{
 
516
        if (i->c_handle) {
 
517
                MEM_CacheLimiter_unmanage(i->c_handle);
 
518
                i->c_handle = 0;
 
519
        }
 
520
}
 
521
 
 
522
void IMB_cache_limiter_touch(struct ImBuf * i)
 
523
{
 
524
        if (i->c_handle) {
 
525
                MEM_CacheLimiter_touch(i->c_handle);
 
526
        }
 
527
}
 
528
 
 
529
void IMB_cache_limiter_ref(struct ImBuf * i)
 
530
{
 
531
        if (i->c_handle) {
 
532
                MEM_CacheLimiter_ref(i->c_handle);
 
533
        }
 
534
}
 
535
 
 
536
void IMB_cache_limiter_unref(struct ImBuf * i)
 
537
{
 
538
        if (i->c_handle) {
 
539
                MEM_CacheLimiter_unref(i->c_handle);
 
540
        }
 
541
}
 
542
 
 
543
int IMB_cache_limiter_get_refcount(struct ImBuf * i)
 
544
{
 
545
        if (i->c_handle) {
 
546
                return MEM_CacheLimiter_get_refcount(i->c_handle);
 
547
        }
 
548
        return 0;
 
549
}