hopefully avoid coverity complaining about a float vs int comparison
[imager.git] / rubthru.im
CommitLineData
fe415ad2
TC
1#include "imager.h"
2
3static int
4rubthru_targ_noalpha(i_img *im, i_img *src,
8d14daab
TC
5 i_img_dim tx, i_img_dim ty,
6 i_img_dim src_minx, i_img_dim src_miny,
7 i_img_dim src_maxx, i_img_dim src_maxy) {
98747309 8 i_img_dim x, y, tty;
fe415ad2
TC
9 int alphachan;
10 int ch;
8d14daab 11 i_img_dim width = src_maxx - src_minx;
9b1ec2b8 12 int want_channels;
fe415ad2
TC
13
14 i_clear_error();
15
9b1ec2b8
TC
16 if (im->channels == 3 && (src->channels == 4 || src->channels == 2)) {
17 want_channels = 4;
fe415ad2
TC
18 alphachan = 3;
19 }
9b1ec2b8
TC
20 else if (im->channels == 1 && (src->channels == 4 || src->channels == 2)) {
21 want_channels = 2;
fe415ad2
TC
22 alphachan = 1;
23 }
24 else {
9b1ec2b8
TC
25 i_copyto(im, src, src_minx, src_miny, src_maxx, src_maxy, tx, ty);
26 return 1;
fe415ad2
TC
27 }
28
29#code im->bits <= 8 && src->bits <= 8
30 IM_WORK_T alpha;
9b1ec2b8
TC
31 IM_COLOR *src_line, *dest_line;
32
33 src_line = mymalloc(sizeof(IM_COLOR) * width);
34 dest_line = mymalloc(sizeof(IM_COLOR) * width);
fe415ad2
TC
35
36 tty = ty;
37 for(y = src_miny; y < src_maxy; y++) {
9b1ec2b8
TC
38 IM_COLOR *srcp = src_line;
39 IM_COLOR *destp = dest_line;
9b1ec2b8
TC
40 IM_GLIN(src, src_minx, src_maxx, y, src_line);
41 IM_GLIN(im, tx, tx + width, tty, dest_line);
42 if (src->channels != want_channels)
43 IM_ADAPT_COLORS(want_channels, src->channels, src_line, width);
44
fe415ad2 45 for(x = src_minx; x < src_maxx; x++) {
9b1ec2b8
TC
46 alpha = srcp->channel[alphachan];
47 for (ch = 0; ch < im->channels; ++ch) {
48 IM_WORK_T samp = (alpha * srcp->channel[ch]
49 + (IM_SAMPLE_MAX - alpha) * destp->channel[ch])/IM_SAMPLE_MAX;
50 destp->channel[ch] = IM_LIMIT(samp);
fe415ad2 51 }
9b1ec2b8
TC
52 ++srcp;
53 ++destp;
fe415ad2 54 }
9b1ec2b8 55 IM_PLIN(im, tx, tx + width, tty, dest_line);
fe415ad2
TC
56 tty++;
57 }
9b1ec2b8
TC
58 myfree(src_line);
59 myfree(dest_line);
fe415ad2 60#/code
9b1ec2b8 61
fe415ad2
TC
62 return 1;
63}
64
65static int
8d14daab
TC
66rubthru_targ_alpha(i_img *im, i_img *src, i_img_dim tx, i_img_dim ty,
67 i_img_dim src_minx, i_img_dim src_miny,
68 i_img_dim src_maxx, i_img_dim src_maxy) {
69 i_img_dim x, y, ttx, tty;
9b1ec2b8 70 int want_channels;
fe415ad2
TC
71 int alphachan;
72 int ch;
73 int targ_alpha_chan;
8d14daab 74 i_img_dim width = src_maxx - src_minx;
fe415ad2 75
9b1ec2b8 76 if (im->channels == 4 && (src->channels == 4 || src->channels == 2)) {
fe415ad2 77 alphachan = 3;
9b1ec2b8 78 want_channels = 4;
fe415ad2 79 }
9b1ec2b8 80 else if (im->channels == 2 && (src->channels == 4 || src->channels == 2)) {
fe415ad2 81 alphachan = 1;
9b1ec2b8 82 want_channels = 2;
fe415ad2
TC
83 }
84 else {
9b1ec2b8
TC
85 i_copyto(im, src, src_minx, src_miny, src_maxx, src_maxy, tx, ty);
86 return 1;
fe415ad2
TC
87 }
88
89 targ_alpha_chan = im->channels - 1;
90
91#code im->bits <= 8 && src->bits <= 8
92 IM_WORK_T src_alpha, orig_alpha, dest_alpha, remains;
9b1ec2b8
TC
93 IM_COLOR *src_line, *dest_line;
94
95 src_line = mymalloc(sizeof(IM_COLOR) * width);
96 dest_line = mymalloc(sizeof(IM_COLOR) * width);
fe415ad2
TC
97
98 tty = ty;
99 for(y = src_miny; y < src_maxy; y++) {
8d14daab 100 i_img_dim min_x, max_x;
9b1ec2b8
TC
101 IM_COLOR *srcp = src_line;
102 IM_COLOR *destp = dest_line;
103 IM_GLIN(src, src_minx, src_maxx, y, src_line);
104 if (src->channels != want_channels)
105 IM_ADAPT_COLORS(want_channels, src->channels, src_line, width);
106 min_x = src_minx;
107 max_x = src_maxx;
108
109 while (min_x < max_x && srcp->channel[alphachan] == 0) {
110 ++min_x;
111 ++srcp;
112 }
6ba09ab9 113 while (max_x > min_x && src_line[max_x-1 - src_minx].channel[alphachan] == 0) {
9b1ec2b8
TC
114 --max_x;
115 }
116
117 if (max_x > min_x) {
8d14daab
TC
118 i_img_dim work_left = tx + min_x - src_minx;
119 i_img_dim work_width = max_x - min_x;
9b1ec2b8
TC
120 ttx = work_left;
121 IM_GLIN(im, work_left, work_left + work_width, tty, dest_line);
122
17d9fe35 123 for(x = min_x; x < max_x; x++) {
9b1ec2b8
TC
124 src_alpha = srcp->channel[alphachan];
125 if (src_alpha) {
126 remains = IM_SAMPLE_MAX - src_alpha;
127 orig_alpha = destp->channel[targ_alpha_chan];
128 dest_alpha = src_alpha + (remains * orig_alpha) / IM_SAMPLE_MAX;
129
130 for (ch = 0; ch < im->channels-1; ++ch) {
131 IM_WORK_T samp =
132 ( src_alpha * srcp->channel[ch]
133 + remains * destp->channel[ch] * orig_alpha / IM_SAMPLE_MAX
134 ) / dest_alpha;
135 destp->channel[ch] = IM_LIMIT(samp);
136 }
137 /* dest's alpha */
138 destp->channel[targ_alpha_chan] = dest_alpha;
139 }
140 ++srcp;
141 ++destp;
142 ttx++;
fe415ad2 143 }
9b1ec2b8 144 IM_PLIN(im, work_left, work_left + work_width, tty, dest_line);
fe415ad2
TC
145 }
146 tty++;
147 }
9b1ec2b8
TC
148 myfree(dest_line);
149 myfree(src_line);
fe415ad2
TC
150#/code
151 return 1;
152}
153
154/*
5715f7c3 155=item i_rubthru(C<im>, C<src>, C<tx>, C<ty>, C<src_minx>, C<src_miny>, C<src_maxx>, C<src_maxy>)
fe415ad2
TC
156
157=category Image
158
5715f7c3
TC
159Takes the sub image C<src>[C<src_minx>, C<src_maxx>)[C<src_miny>, C<src_maxy>)> and
160overlays it at (C<tx>,C<ty>) on the image object.
fe415ad2 161
5715f7c3
TC
162The alpha channel of each pixel in C<src> is used to control how much
163the existing color in C<im> is replaced, if it is 255 then the color
164is completely replaced, if it is 0 then the original color is left
fe415ad2
TC
165unmodified.
166
167=cut
168*/
169
170int
8d14daab
TC
171i_rubthru(i_img *im, i_img *src, i_img_dim tx, i_img_dim ty, i_img_dim src_minx, i_img_dim src_miny,
172 i_img_dim src_maxx, i_img_dim src_maxy) {
9b1ec2b8
TC
173 if (src_minx < 0) {
174 tx -= src_minx;
175 src_minx = 0;
176 }
177 if (src_miny < 0) {
178 ty -= src_miny;
179 src_miny = 0;
180 }
181 if (tx < 0) {
182 src_minx -= tx;
183 tx = 0;
184 }
185 if (ty < 0) {
186 src_miny -= ty;
187 ty = 0;
188 }
189 if (src_maxx > src->xsize) {
190 src_maxx = src->xsize;
191 }
192 if (src_maxy > src->ysize) {
193 src_maxy = src->ysize;
194 }
195 if (tx >= im->xsize || ty >= im->ysize
196 || src_minx >= src_maxx || src_miny >= src_maxy) {
197 i_clear_error();
0d670555
TC
198 /* just do nothing, attempting to rubthrough outside the target isn't
199 worth being an error */
200 return 1;
9b1ec2b8
TC
201 }
202
fe415ad2
TC
203 if (im->channels == 1 || im->channels == 3)
204 return rubthru_targ_noalpha(im, src, tx, ty, src_minx, src_miny,
205 src_maxx, src_maxy);
206 else
207 return rubthru_targ_alpha(im, src, tx, ty, src_minx, src_miny,
208 src_maxx, src_maxy);
209}