Code

Filter effects: support for SourceAlpha and BackgroundAlpha
[inkscape.git] / src / display / nr-filter-slot.cpp
1 #define __NR_FILTER_SLOT_CPP__
3 /*
4  * A container class for filter slots. Allows for simple getting and
5  * setting images in filter slots without having to bother with
6  * table indexes and such.
7  *
8  * Author:
9  *   Niko Kiirala <niko@kiirala.com>
10  *
11  * Copyright (C) 2006 Niko Kiirala
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #include <assert.h>
18 #include "display/nr-arena-item.h"
19 #include "display/nr-filter-types.h"
20 #include "display/nr-filter-slot.h"
21 #include "display/nr-filter-getalpha.h"
22 #include "libnr/nr-pixblock.h"
23 #include "libnr/nr-blit.h"
25 namespace NR {
27 FilterSlot::FilterSlot(int slots, NRArenaItem const *item)
28 {
29     _slot_count = ((slots > 0) ? slots : 2);
30     _slot = new NRPixBlock*[_slot_count];
31     _slot_number = new int[_slot_count];
33     for (int i = 0 ; i < _slot_count ; i++) {
34         _slot[i] = NULL;
35         _slot_number[i] = NR_FILTER_SLOT_NOT_SET;
36     }
38     _last_out = -1;
40     _arena_item = item;
41 }
43 FilterSlot::~FilterSlot()
44 {
45     for (int i = 0 ; i < _slot_count ; i++) {
46         if (_slot[i]) {
47             nr_pixblock_release(_slot[i]);
48             delete _slot[i];
49         }
50     }
51     delete[] _slot;
52     delete[] _slot_number;
53 }
55 NRPixBlock *FilterSlot::get(int slot_nr)
56 {
57     int index = _get_index(slot_nr);
58     assert(index >= 0);
60     /* If we didn't have the specified image, but we could create it
61      * from the other information we have, let's do that */
62     if (_slot[index] == NULL
63         && (slot_nr == NR_FILTER_SOURCEALPHA
64             || slot_nr == NR_FILTER_BACKGROUNDIMAGE
65             || slot_nr == NR_FILTER_BACKGROUNDALPHA
66             || slot_nr == NR_FILTER_FILLPAINT
67             || slot_nr == NR_FILTER_STROKEPAINT))
68     {
69         /* If needed, fetch background */
70         if (slot_nr == NR_FILTER_BACKGROUNDIMAGE) {
71             NRPixBlock *pb;
72             pb = nr_arena_item_get_background(_arena_item);
73             if (pb) {
74                 pb->empty = false;
75                 this->set(NR_FILTER_BACKGROUNDIMAGE, pb);
76             } else {
77                 NRPixBlock *source = this->get(NR_FILTER_SOURCEGRAPHIC);
78                 pb = new NRPixBlock();
79                 if (!pb) return NULL; // Allocation failed
80                 nr_pixblock_setup_fast(pb, source->mode,
81                                        source->area.x0, source->area.y0,
82                                        source->area.x1, source->area.y1, true);
83                 if (pb->size != NR_PIXBLOCK_SIZE_TINY && pb->data.px == NULL) {
84                     // allocation failed
85                     delete pb;
86                     return NULL;
87                 }
88                 pb->empty = FALSE;
89                 this->set(NR_FILTER_BACKGROUNDIMAGE, pb);
90             }
91         } else if (slot_nr == NR_FILTER_SOURCEALPHA) {
92             /* If only a alpha channel is needed, strip it from full image */
93             NRPixBlock *src = get(NR_FILTER_SOURCEGRAPHIC);
94             NRPixBlock *sa = filter_get_alpha(src);
95             set(NR_FILTER_SOURCEALPHA, sa);
96         } else if (slot_nr == NR_FILTER_BACKGROUNDALPHA) {
97             NRPixBlock *src = get(NR_FILTER_BACKGROUNDIMAGE);
98             NRPixBlock *ba = filter_get_alpha(src);
99             set(NR_FILTER_BACKGROUNDALPHA, ba);
100         } else if (slot_nr == NR_FILTER_FILLPAINT) {
101             /* When a paint is needed, fetch it from arena item */
102             // TODO
103         } else if (slot_nr == NR_FILTER_STROKEPAINT) {
104             // TODO
105         }
106     }
108     assert(slot_nr == NR_FILTER_SLOT_NOT_SET ||_slot_number[index] == slot_nr);
109     return _slot[index];
112 void FilterSlot::set(int slot_nr, NRPixBlock *pb)
114     int index = _get_index(slot_nr);
115     assert(index >= 0);
116     assert(slot_nr == NR_FILTER_SLOT_NOT_SET ||_slot_number[index] == slot_nr);
118     if(_slot[index]) {
119         nr_pixblock_release(_slot[index]);
120         delete _slot[index];
121     }
122     _slot[index] = pb;
123     _last_out = index;
126 int FilterSlot::get_slot_count()
128     int seek = _slot_count;
129     do {
130         seek--;
131     } while (!_slot[seek] && _slot_number[seek] == NR_FILTER_SLOT_NOT_SET);
133     return seek + 1;
136 int FilterSlot::_get_index(int slot_nr)
138     assert(slot_nr >= 0 ||
139            slot_nr == NR_FILTER_SLOT_NOT_SET ||
140            slot_nr == NR_FILTER_SOURCEGRAPHIC ||
141            slot_nr == NR_FILTER_SOURCEALPHA ||
142            slot_nr == NR_FILTER_BACKGROUNDIMAGE ||
143            slot_nr == NR_FILTER_BACKGROUNDALPHA ||
144            slot_nr == NR_FILTER_FILLPAINT ||
145            slot_nr == NR_FILTER_STROKEPAINT);
147     int index = -1;
148     if (slot_nr == NR_FILTER_SLOT_NOT_SET) {
149         return _last_out;
150     }
151     /* Search, if the slot already exists */
152     for (int i = 0 ; i < _slot_count ; i++) {
153         if (_slot_number[i] == slot_nr) {
154             index = i;
155             break;
156         }
157     }
159     /* If the slot doesn't already exist, create it */
160     if (index == -1) {
161         int seek = _slot_count;
162         do {
163             seek--;
164         } while (_slot_number[seek] == NR_FILTER_SLOT_NOT_SET && seek >= 0);
165         /* If there is no space for more slots, create more space */
166         if (seek == _slot_count - 1) {
167             NRPixBlock **new_slot = new NRPixBlock*[_slot_count * 2];
168             int *new_number = new int[_slot_count * 2];
169             for (int i = 0 ; i < _slot_count ; i++) {
170                 new_slot[i] = _slot[i];
171                 new_number[i] = _slot_number[i];
172             }
173             for (int i = _slot_count ; i < _slot_count * 2 ; i++) {
174                 new_slot[i] = NULL;
175                 new_number[i] = NR_FILTER_SLOT_NOT_SET;
176             }
177             delete[] _slot;
178             delete[] _slot_number;
179             _slot = new_slot;
180             _slot_number = new_number;
181             _slot_count *= 2;
182         }
183         /* Now that there is space, create the slot */
184         _slot_number[seek + 1] = slot_nr;
185         index = seek + 1;
186     }
187     return index;
192 /*
193   Local Variables:
194   mode:c++
195   c-file-style:"stroustrup"
196   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
197   indent-tabs-mode:nil
198   fill-column:99
199   End:
200 */
201 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :