App

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

TBitmap.cpp
Go to the documentation of this file.
1
6 #include "TBitmap.hpp"
7
8 namespace app {
9
10 template class TBitmap<px_4x8bit, px_1x8bit> ;
11 template class TBitmap<px_1x8bit, px_1x8bit> ;
12
13 template<typename TPIXEL, typename TCHANNEL>
14 TBitmap<TCHANNEL, TCHANNEL> * TBitmap<TPIXEL, TCHANNEL>::getChannel(unsigned index) const {
15 if (data == NULL) {
16 return NULL;
17 }
18 if (index >= chCount) {
19 return NULL;
20 }
21 TBitmap<TCHANNEL, TCHANNEL> * res = new TBitmap<TCHANNEL, TCHANNEL>(width, height, MONO, premultiplied);
22 if (res->getData() == NULL) {
23 delete res;
24 return NULL;
25 }
26
27 TCHANNEL * s = (TCHANNEL *) data + index;
28 TCHANNEL * end = (TCHANNEL *) (data + width * height);
29 TCHANNEL * d = res->getData();
30
31 while (s < end) {
32 *d = *s;
33 s += 4;
34 d++;
35 }
36
37 return res;
38 }
39
40 template<typename TPIXEL, typename TCHANNEL>
41 TBitmap<TPIXEL, TCHANNEL>::~TBitmap() {
42 if (ownsData && (data != NULL)) {
43 free(data);
44 }
45 }
46
47 template<typename TPIXEL, typename TCHANNEL>
48 int TBitmap<TPIXEL, TCHANNEL>::premultiply() {
49 if (premultiplied) {
50 return 1;
51 }
52 if (chCount != 4) {
53 return 2;
54 }
55 if (data == NULL) {
56 return 3;
57 }
58 TCHANNEL * ptr = (TCHANNEL *) data;
59 TCHANNEL * ptrEnd = (TCHANNEL *) (data + width * height);
60 double alpha;
61 while (ptr < ptrEnd) {
62 alpha = *(ptr + 3) / (double) maxVal;
63 *ptr = *ptr * alpha + 0.5;
64 ptr++;
65 *ptr = *ptr * alpha + 0.5;
66 ptr++;
67 *ptr = *ptr * alpha + 0.5;
68 ptr++;
69 ptr++;
70 }
71 premultiplied = 1;
72 return 0;
73 }
74
75 template<typename TPIXEL, typename TCHANNEL>
76 int TBitmap<TPIXEL, TCHANNEL>::demultiply() {
77 if (!premultiplied) {
78 return 1;
79 }
80 if (chCount != 4) {
81 return 2;
82 }
83 if (data == NULL) {
84 return 3;
85 }
86 TCHANNEL * ptr = (TCHANNEL *) data;
87 TCHANNEL * ptrEnd = (TCHANNEL *) (data + width * height);
88 TCHANNEL tmp;
89 double alpha;
90 while (ptr < ptrEnd) {
91 tmp = *(ptr + 3);
92 if (tmp == 0) {
93 ptr += 4;
94 continue;
95 }
96 alpha = tmp / (double) maxVal;
97
98 *ptr = *ptr / alpha + 0.5;
99 ptr++;
100 *ptr = *ptr / alpha + 0.5;
101 ptr++;
102 *ptr = *ptr / alpha + 0.5;
103 ptr++;
104 ptr++;
105 }
106 return 0;
107 }
108
109 template<typename TPIXEL, typename TCHANNEL>
110 int TBitmap<TPIXEL, TCHANNEL>::copyTo(TBitmap<TPIXEL, TCHANNEL> &dst, Rect srcRect, Point srcOrig) const {
111 if (data == NULL) {
112 return 1;
113 }
114 if (dst.getData() == NULL) {
115 return 2;
116 }
117 if (type != dst.getType()) {
118 return 3;
119 }
120 PointerArea srcArea = PointerArea(data, getRect(), srcRect, bpp);
121 PointerArea dstArea = dst.getPointerArea();
122 int err = PointerArea::copy(dstArea, srcArea, srcOrig);
123 if (err) {
124 return 100 + err;
125 } else {
126 return 0;
127 }
128 }
129
130 template<typename TPIXEL, typename TCHANNEL> int TBitmap<TPIXEL, TCHANNEL>::fill(Rect area, TPIXEL * src) {
131 if (data == NULL) {
132 return 1;
133 }
134 PointerArea dstArea = PointerArea(data, getRect(), area, bpp);
135 int err = PointerArea::fill(dstArea, src);
136 if (err) {
137 return 100 + err;
138 } else {
139 return 0;
140 }
141 }
142
143 template<typename TPIXEL, typename TCHANNEL>
144 TBitmap<TPIXEL, TCHANNEL> * TBitmap<TPIXEL, TCHANNEL>::difference(TBitmap<TPIXEL, TCHANNEL> &other, int proportional) const {
145 if (data == NULL) {
146 return NULL;
147 }
148 if (other.getType() != type || other.getData() == NULL) {
149 return NULL;
150 }
151
152 TBitmap<TPIXEL, TCHANNEL> * res = new TBitmap<TPIXEL, TCHANNEL>(width, height, type, premultiplied);
153
154 TCHANNEL * s1 = (TCHANNEL *) data;
155 TCHANNEL * s2 = (TCHANNEL *) other.getData();
156 TCHANNEL * d = (TCHANNEL *) res->getData();
157 TCHANNEL * s1End = (TCHANNEL *) (data + width * height);
158
159 TCHANNEL minVal = 0;
160 TCHANNEL maxVal = ~minVal; // 111...111
161
162 if (!proportional) {
163 while (s1 < s1End) {
164 *d = (*s1 == *s2) ? maxVal : minVal;
165 s1++;
166 s2++;
167 d++;
168 }
169 } else {
170 while (s1 < s1End) {
171 *d = abs(*s1 - *s2);
172 s1++;
173 s2++;
174 d++;
175 }
176 }
177 return res;
178 }
179
180 } /* namespace app */