| 1 |
|
| /* |
| 2 |
|
| =head1 NAME |
| 3 |
|
| |
| 4 |
|
| rotate.im - implements image rotations |
| 5 |
|
| |
| 6 |
|
| =head1 SYNOPSIS |
| 7 |
|
| |
| 8 |
|
| i_img *i_rotate90(i_img *src, int degrees) |
| 9 |
|
| |
| 10 |
|
| =head1 DESCRIPTION |
| 11 |
|
| |
| 12 |
|
| Implements basic 90 degree rotations of an image. |
| 13 |
|
| |
| 14 |
|
| Other rotations will be added as tuits become available. |
| 15 |
|
| |
| 16 |
|
| =cut |
| 17 |
|
| */ |
| 18 |
|
| |
| 19 |
|
| #include "imager.h" |
| 20 |
|
| #include "imageri.h" |
| 21 |
|
| #include <math.h> /* for floor() */ |
| 22 |
|
| |
| 23 |
|
| #define ROT_DEBUG(x) |
| 24 |
|
| |
| 25 |
28 |
| i_img *i_rotate90(i_img *src, int degrees) { |
| 26 |
|
| i_img *targ; |
| 27 |
|
| i_img_dim x, y; |
| 28 |
|
| |
| 29 |
28 |
| i_clear_error(); |
| 30 |
|
| |
| 31 |
28 |
▶ | if (degrees == 180) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (degrees == 180) { |
5 |
23 |
28 |
|
| 32 |
|
| /* essentially the same as flipxy(..., 2) except that it's not |
| 33 |
|
| done in place */ |
| 34 |
5 |
| targ = i_sametype(src, src->xsize, src->ysize); |
| 35 |
5 |
▶ | if (!targ) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!targ) |
0 |
5 |
5 |
|
| 36 |
0 |
| return NULL; |
| 37 |
5 |
▶ | if (src->type == i_direct_type) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (src->type == i_direct_type) { |
3 |
2 |
5 |
|
| 38 |
3 |
▶ | #code src->bits <= 8 |
Branch: 1 branch
| Branch | True | False | Total |
| #code src->bits <= 8 |
3 |
0 |
3 |
|
| 39 |
3 |
| IM_COLOR *vals = mymalloc(src->xsize * sizeof(IM_COLOR)); |
| 40 |
291 |
▶ | for (y = 0; y < src->ysize; ++y) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (y = 0; y < src->ysize; ++y) { |
288 |
3 |
291 |
| for (y = 0; y < src->ysize; ++y) { |
0 |
0 |
0 |
|
| 41 |
|
| IM_COLOR tmp; |
| 42 |
288 |
| IM_GLIN(src, 0, src->xsize, y, vals); |
| 43 |
15274 |
▶ | for (x = 0; x < src->xsize/2; ++x) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (x = 0; x < src->xsize/2; ++x) { |
14986 |
288 |
15274 |
| for (x = 0; x < src->xsize/2; ++x) { |
0 |
0 |
0 |
|
| 44 |
14986 |
| tmp = vals[x]; |
| 45 |
14986 |
| vals[x] = vals[src->xsize - x - 1]; |
| 46 |
14986 |
| vals[src->xsize - x - 1] = tmp; |
| 47 |
|
| } |
| 48 |
288 |
| IM_PLIN(targ, 0, src->xsize, src->ysize - y - 1, vals); |
| 49 |
|
| } |
| 50 |
3 |
| myfree(vals); |
| 51 |
|
| #/code |
| 52 |
|
| } |
| 53 |
|
| else { |
| 54 |
2 |
| i_palidx *vals = mymalloc(src->xsize * sizeof(i_palidx)); |
| 55 |
|
| |
| 56 |
172 |
▶ | for (y = 0; y < src->ysize; ++y) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (y = 0; y < src->ysize; ++y) { |
170 |
2 |
172 |
|
| 57 |
|
| i_palidx tmp; |
| 58 |
170 |
▶ | i_gpal(src, 0, src->xsize, y, vals); |
Branch: 1 branch
| Branch | True | False | Total |
| i_gpal(src, 0, src->xsize, y, vals); |
170 |
0 |
170 |
|
| 59 |
10200 |
▶ | for (x = 0; x < src->xsize/2; ++x) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = 0; x < src->xsize/2; ++x) { |
10030 |
170 |
10200 |
|
| 60 |
10030 |
| tmp = vals[x]; |
| 61 |
10030 |
| vals[x] = vals[src->xsize - x - 1]; |
| 62 |
10030 |
| vals[src->xsize - x - 1] = tmp; |
| 63 |
|
| } |
| 64 |
170 |
▶ | i_ppal(targ, 0, src->xsize, src->ysize - y - 1, vals); |
Branch: 1 branch
| Branch | True | False | Total |
| i_ppal(targ, 0, src->xsize, src->ysize - y - 1, vals); |
170 |
0 |
170 |
|
| 65 |
|
| } |
| 66 |
|
| |
| 67 |
2 |
| myfree(vals); |
| 68 |
|
| } |
| 69 |
|
| |
| 70 |
5 |
| return targ; |
| 71 |
|
| } |
| 72 |
23 |
▶ | else if (degrees == 270 || degrees == 90) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (degrees == 270 || degrees == 90) { |
13 |
10 |
23 |
| else if (degrees == 270 || degrees == 90) { |
13 |
0 |
13 |
|
| 73 |
|
| i_img_dim tx, txstart, txinc; |
| 74 |
|
| i_img_dim ty, tystart, tyinc; |
| 75 |
|
| |
| 76 |
23 |
▶ | if (degrees == 270) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (degrees == 270) { |
10 |
13 |
23 |
|
| 77 |
10 |
| txstart = 0; |
| 78 |
10 |
| txinc = 1; |
| 79 |
10 |
| tystart = src->xsize-1; |
| 80 |
10 |
| tyinc = -1; |
| 81 |
|
| } |
| 82 |
|
| else { |
| 83 |
13 |
| txstart = src->ysize-1; |
| 84 |
13 |
| txinc = -1; |
| 85 |
13 |
| tystart = 0; |
| 86 |
13 |
| tyinc = 1; |
| 87 |
|
| } |
| 88 |
23 |
| targ = i_sametype(src, src->ysize, src->xsize); |
| 89 |
23 |
▶ | if (!targ) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!targ) |
0 |
23 |
23 |
|
| 90 |
0 |
| return NULL; |
| 91 |
23 |
▶ | if (src->type == i_direct_type) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (src->type == i_direct_type) { |
15 |
8 |
23 |
|
| 92 |
15 |
▶ | #code src->bits <= 8 |
Branch: 1 branch
| Branch | True | False | Total |
| #code src->bits <= 8 |
14 |
1 |
15 |
|
| 93 |
15 |
| IM_COLOR *vals = mymalloc(src->xsize * sizeof(IM_COLOR)); |
| 94 |
|
| |
| 95 |
15 |
| tx = txstart; |
| 96 |
2183 |
▶ | for (y = 0; y < src->ysize; ++y) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (y = 0; y < src->ysize; ++y) { |
1768 |
14 |
1782 |
| for (y = 0; y < src->ysize; ++y) { |
400 |
1 |
401 |
|
| 97 |
2168 |
| IM_GLIN(src, 0, src->xsize, y, vals); |
| 98 |
2168 |
| ty = tystart; |
| 99 |
305028 |
▶ | for (x = 0; x < src->xsize; ++x) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (x = 0; x < src->xsize; ++x) { |
222860 |
1768 |
224628 |
| for (x = 0; x < src->xsize; ++x) { |
80000 |
400 |
80400 |
|
| 100 |
302860 |
| IM_PPIX(targ, tx, ty, vals+x); |
| 101 |
302860 |
| ty += tyinc; |
| 102 |
|
| } |
| 103 |
2168 |
| tx += txinc; |
| 104 |
|
| } |
| 105 |
15 |
| myfree(vals); |
| 106 |
|
| #/code |
| 107 |
|
| } |
| 108 |
|
| else { |
| 109 |
8 |
| i_palidx *vals = mymalloc(src->xsize * sizeof(i_palidx)); |
| 110 |
|
| |
| 111 |
8 |
| tx = txstart; |
| 112 |
820 |
▶ | for (y = 0; y < src->ysize; ++y) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (y = 0; y < src->ysize; ++y) { |
812 |
8 |
820 |
|
| 113 |
812 |
▶ | i_gpal(src, 0, src->xsize, y, vals); |
Branch: 1 branch
| Branch | True | False | Total |
| i_gpal(src, 0, src->xsize, y, vals); |
812 |
0 |
812 |
|
| 114 |
812 |
| ty = tystart; |
| 115 |
81052 |
▶ | for (x = 0; x < src->xsize; ++x) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = 0; x < src->xsize; ++x) { |
80240 |
812 |
81052 |
|
| 116 |
80240 |
▶ | i_ppal(targ, tx, tx+1, ty, vals+x); |
Branch: 1 branch
| Branch | True | False | Total |
| i_ppal(targ, tx, tx+1, ty, vals+x); |
80240 |
0 |
80240 |
|
| 117 |
80240 |
| ty += tyinc; |
| 118 |
|
| } |
| 119 |
812 |
| tx += txinc; |
| 120 |
|
| } |
| 121 |
8 |
| myfree(vals); |
| 122 |
|
| } |
| 123 |
23 |
| return targ; |
| 124 |
|
| } |
| 125 |
|
| else { |
| 126 |
0 |
| i_push_error(0, "i_rotate90() only rotates at 90, 180, or 270 degrees"); |
| 127 |
0 |
| return NULL; |
| 128 |
|
| } |
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
|
| /* linear interpolation */ |
| 132 |
182452 |
| static i_color interp_i_color(i_color before, i_color after, double pos, |
| 133 |
|
| int channels) { |
| 134 |
|
| i_color out; |
| 135 |
|
| int ch; |
| 136 |
|
| |
| 137 |
182452 |
▶ | if (channels == 1 || channels == 3) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (channels == 1 || channels == 3) { |
182452 |
0 |
182452 |
| if (channels == 1 || channels == 3) { |
172006 |
10446 |
182452 |
|
| 138 |
688024 |
▶ | for (ch = 0; ch < channels; ++ch) |
Branch: 1 branch
| Branch | True | False | Total |
| for (ch = 0; ch < channels; ++ch) |
516018 |
172006 |
688024 |
|
| 139 |
516018 |
| out.channel[ch] = ((1-pos) * before.channel[ch] + pos * after.channel[ch]) + 0.5; |
| 140 |
|
| } |
| 141 |
|
| else { |
| 142 |
10446 |
| int total_cover = (1-pos) * before.channel[channels-1] |
| 143 |
10446 |
| + pos * after.channel[channels-1]; |
| 144 |
|
| |
| 145 |
10446 |
▶ | total_cover = I_LIMIT_8(total_cover); |
Branch: 1 branch
| Branch | True | False | Total |
| total_cover = I_LIMIT_8(total_cover); |
10446 |
0 |
10446 |
|
| 146 |
10446 |
▶ | if (total_cover) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (total_cover) { |
10260 |
186 |
10446 |
|
| 147 |
10260 |
| double before_alpha = before.channel[channels-1] / 255.0; |
| 148 |
10260 |
| double after_alpha = after.channel[channels-1] / 255.0; |
| 149 |
10260 |
| double total_alpha = before_alpha * (1-pos) + after_alpha * pos; |
| 150 |
|
| |
| 151 |
41040 |
▶ | for (ch = 0; ch < channels-1; ++ch) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ch = 0; ch < channels-1; ++ch) { |
30780 |
10260 |
41040 |
|
| 152 |
30780 |
| int out_level = ((1-pos) * before.channel[ch] * before_alpha + |
| 153 |
30780 |
| pos * after.channel[ch] * after_alpha) / total_alpha + 0.5; |
| 154 |
|
| |
| 155 |
30780 |
▶ | out.channel[ch] = I_LIMIT_8(out_level); |
Branch: 1 branch
| Branch | True | False | Total |
| out.channel[ch] = I_LIMIT_8(out_level); |
30780 |
0 |
30780 |
|
| 156 |
|
| } |
| 157 |
|
| } |
| 158 |
|
| else { |
| 159 |
744 |
▶ | for (ch = 0; ch < channels-1; ++ch) |
Branch: 1 branch
| Branch | True | False | Total |
| for (ch = 0; ch < channels-1; ++ch) |
558 |
186 |
744 |
|
| 160 |
558 |
| out.channel[ch] = 0; |
| 161 |
|
| } |
| 162 |
|
| |
| 163 |
10446 |
| out.channel[channels-1] = total_cover; |
| 164 |
|
| } |
| 165 |
|
| |
| 166 |
182452 |
| return out; |
| 167 |
|
| } |
| 168 |
|
| |
| 169 |
|
| /* hopefully this will be inlined (it is with -O3 with gcc 2.95.4) */ |
| 170 |
|
| /* linear interpolation */ |
| 171 |
61430 |
| static i_fcolor interp_i_fcolor(i_fcolor before, i_fcolor after, double pos, |
| 172 |
|
| int channels) { |
| 173 |
|
| i_fcolor out; |
| 174 |
|
| int ch; |
| 175 |
|
| |
| 176 |
61430 |
▶ | if (channels == 1 || channels == 3) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (channels == 1 || channels == 3) { |
61430 |
0 |
61430 |
| if (channels == 1 || channels == 3) { |
61430 |
0 |
61430 |
|
| 177 |
245720 |
▶ | for (ch = 0; ch < channels; ++ch) |
Branch: 1 branch
| Branch | True | False | Total |
| for (ch = 0; ch < channels; ++ch) |
184290 |
61430 |
245720 |
|
| 178 |
184290 |
| out.channel[ch] = (1-pos) * before.channel[ch] + pos * after.channel[ch]; |
| 179 |
|
| } |
| 180 |
|
| else { |
| 181 |
0 |
| double total_cover = (1-pos) * before.channel[channels-1] |
| 182 |
0 |
| + pos * after.channel[channels-1]; |
| 183 |
|
| |
| 184 |
0 |
▶ | total_cover = I_LIMIT_DOUBLE(total_cover); |
Branch: 2 branches
| Branch | True | False | Total |
| total_cover = I_LIMIT_DOUBLE(total_cover); |
0 |
0 |
0 |
| total_cover = I_LIMIT_DOUBLE(total_cover); |
0 |
0 |
0 |
|
| 185 |
0 |
▶ | if (total_cover) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (total_cover) { |
0 |
0 |
0 |
|
| 186 |
0 |
| double before_alpha = before.channel[channels-1]; |
| 187 |
0 |
| double after_alpha = after.channel[channels-1]; |
| 188 |
0 |
| double total_alpha = before_alpha * (1-pos) + after_alpha * pos; |
| 189 |
|
| |
| 190 |
0 |
▶ | for (ch = 0; ch < channels-1; ++ch) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ch = 0; ch < channels-1; ++ch) { |
0 |
0 |
0 |
|
| 191 |
0 |
| double out_level = ((1-pos) * before.channel[ch] * before_alpha + |
| 192 |
0 |
| pos * after.channel[ch] * after_alpha) / total_alpha; |
| 193 |
|
| |
| 194 |
0 |
▶ | out.channel[ch] = I_LIMIT_DOUBLE(out_level); |
Branch: 2 branches
| Branch | True | False | Total |
| out.channel[ch] = I_LIMIT_DOUBLE(out_level); |
0 |
0 |
0 |
| out.channel[ch] = I_LIMIT_DOUBLE(out_level); |
0 |
0 |
0 |
|
| 195 |
|
| } |
| 196 |
|
| } |
| 197 |
|
| else { |
| 198 |
0 |
▶ | for (ch = 0; ch < channels-1; ++ch) |
Branch: 1 branch
| Branch | True | False | Total |
| for (ch = 0; ch < channels-1; ++ch) |
0 |
0 |
0 |
|
| 199 |
0 |
| out.channel[ch] = 0; |
| 200 |
|
| } |
| 201 |
|
| |
| 202 |
0 |
| out.channel[channels-1] = total_cover; |
| 203 |
|
| } |
| 204 |
|
| |
| 205 |
61430 |
| return out; |
| 206 |
|
| } |
| 207 |
|
| |
| 208 |
13 |
| i_img *i_matrix_transform_bg(i_img *src, i_img_dim xsize, i_img_dim ysize, const double *matrix, |
| 209 |
|
| const i_color *backp, const i_fcolor *fbackp) { |
| 210 |
13 |
| i_img *result = i_sametype(src, xsize, ysize); |
| 211 |
|
| i_img_dim x, y; |
| 212 |
|
| int ch; |
| 213 |
|
| i_img_dim i, j; |
| 214 |
|
| double sx, sy, sz; |
| 215 |
|
| |
| 216 |
13 |
▶ | if (!result) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!result) |
0 |
13 |
13 |
|
| 217 |
0 |
| return NULL; |
| 218 |
|
| |
| 219 |
13 |
▶ | if (src->type == i_direct_type) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (src->type == i_direct_type) { |
12 |
1 |
13 |
|
| 220 |
12 |
▶ | #code src->bits <= 8 |
Branch: 1 branch
| Branch | True | False | Total |
| #code src->bits <= 8 |
10 |
2 |
12 |
|
| 221 |
12 |
| IM_COLOR *vals = mymalloc(xsize * sizeof(IM_COLOR)); |
| 222 |
|
| IM_COLOR back; |
| 223 |
|
| |
| 224 |
|
| #ifdef IM_EIGHT_BIT |
| 225 |
10 |
▶ | if (backp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (backp) { |
5 |
5 |
10 |
|
| 226 |
5 |
| back = *backp; |
| 227 |
|
| } |
| 228 |
5 |
▶ | else if (fbackp) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (fbackp) { |
0 |
5 |
5 |
|
| 229 |
0 |
▶ | for (ch = 0; ch < src->channels; ++ch) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ch = 0; ch < src->channels; ++ch) { |
0 |
0 |
0 |
|
| 230 |
|
| i_fsample_t fsamp; |
| 231 |
0 |
| fsamp = fbackp->channel[ch]; |
| 232 |
0 |
▶ | back.channel[ch] = fsamp < 0 ? 0 : fsamp > 1 ? 255 : fsamp * 255; |
Branch: 2 branches
| Branch | True | False | Total |
| back.channel[ch] = fsamp < 0 ? 0 : fsamp > 1 ? 255 : fsamp * 255; |
0 |
0 |
0 |
| back.channel[ch] = fsamp < 0 ? 0 : fsamp > 1 ? 255 : fsamp * 255; |
0 |
0 |
0 |
|
| 233 |
|
| } |
| 234 |
|
| } |
| 235 |
|
| #else |
| 236 |
|
| #define interp_i_color interp_i_fcolor |
| 237 |
2 |
▶ | if (fbackp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fbackp) { |
0 |
2 |
2 |
|
| 238 |
0 |
| back = *fbackp; |
| 239 |
|
| } |
| 240 |
2 |
▶ | else if (backp) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (backp) { |
1 |
1 |
2 |
|
| 241 |
4 |
▶ | for (ch = 0; ch < src->channels; ++ch) |
Branch: 1 branch
| Branch | True | False | Total |
| for (ch = 0; ch < src->channels; ++ch) |
3 |
1 |
4 |
|
| 242 |
3 |
| back.channel[ch] = backp->channel[ch] / 255.0; |
| 243 |
|
| } |
| 244 |
|
| #endif |
| 245 |
|
| else { |
| 246 |
26 |
▶ | for (ch = 0; ch < src->channels; ++ch) |
Branch: 2 branches
| Branch | True | False | Total |
| for (ch = 0; ch < src->channels; ++ch) |
17 |
5 |
22 |
| for (ch = 0; ch < src->channels; ++ch) |
3 |
1 |
4 |
|
| 247 |
20 |
| back.channel[ch] = 0; |
| 248 |
|
| } |
| 249 |
|
| |
| 250 |
1014 |
▶ | for (y = 0; y < ysize; ++y) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (y = 0; y < ysize; ++y) { |
792 |
10 |
802 |
| for (y = 0; y < ysize; ++y) { |
210 |
2 |
212 |
|
| 251 |
123560 |
▶ | for (x = 0; x < xsize; ++x) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (x = 0; x < xsize; ++x) { |
95048 |
792 |
95840 |
| for (x = 0; x < xsize; ++x) { |
27510 |
210 |
27720 |
|
| 252 |
|
| /* dividing by sz gives us the ability to do perspective |
| 253 |
|
| transforms */ |
| 254 |
122558 |
| sz = x * matrix[6] + y * matrix[7] + matrix[8]; |
| 255 |
122558 |
▶ | if (fabs(sz) > 0.0000001) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (fabs(sz) > 0.0000001) { |
95048 |
0 |
95048 |
| if (fabs(sz) > 0.0000001) { |
27510 |
0 |
27510 |
|
| 256 |
122558 |
| sx = (x * matrix[0] + y * matrix[1] + matrix[2]) / sz; |
| 257 |
122558 |
| sy = (x * matrix[3] + y * matrix[4] + matrix[5]) / sz; |
| 258 |
|
| } |
| 259 |
|
| else { |
| 260 |
0 |
| sx = sy = 0; |
| 261 |
|
| } |
| 262 |
|
| |
| 263 |
|
| /* anything outside these ranges is either a broken co-ordinate |
| 264 |
|
| or outside the source */ |
| 265 |
122558 |
▶ | if (fabs(sz) > 0.0000001 |
Branch: 2 branches
| Branch | True | False | Total |
| if (fabs(sz) > 0.0000001 |
95048 |
0 |
95048 |
| if (fabs(sz) > 0.0000001 |
27510 |
0 |
27510 |
|
| 266 |
122558 |
▶ | && sx >= -1 && sx < src->xsize |
Branch: 4 branches
| Branch | True | False | Total |
| && sx >= -1 && sx < src->xsize |
92026 |
3022 |
95048 |
| && sx >= -1 && sx < src->xsize |
85774 |
6252 |
92026 |
| && sx >= -1 && sx < src->xsize |
26348 |
1162 |
27510 |
| && sx >= -1 && sx < src->xsize |
25186 |
1162 |
26348 |
|
| 267 |
205352 |
▶ | && sy >= -1 && sy < src->ysize) { |
Branch: 4 branches
| Branch | True | False | Total |
| && sy >= -1 && sy < src->ysize) { |
79844 |
5930 |
85774 |
| && sy >= -1 && sy < src->ysize) { |
73914 |
5930 |
79844 |
| && sy >= -1 && sy < src->ysize) { |
22832 |
2354 |
25186 |
| && sy >= -1 && sy < src->ysize) { |
20478 |
2354 |
22832 |
|
| 268 |
94392 |
| double fsx = floor(sx); |
| 269 |
94392 |
| double fsy = floor(sy); |
| 270 |
94392 |
| i_img_dim bx = fsx; |
| 271 |
94392 |
| i_img_dim by = fsy; |
| 272 |
|
| |
| 273 |
|
| ROT_DEBUG(fprintf(stderr, "map " i_DFp " to %g,%g\n", i_DFcp(x, y), sx, sy)); |
| 274 |
94392 |
▶ | if (sx != fsx) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (sx != fsx) { |
69826 |
4088 |
73914 |
| if (sx != fsx) { |
20478 |
0 |
20478 |
|
| 275 |
90304 |
| double dx = sx - fsx; |
| 276 |
90304 |
▶ | if (sy != fsy) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (sy != fsy) { |
55974 |
13852 |
69826 |
| if (sy != fsy) { |
20476 |
2 |
20478 |
|
| 277 |
|
| IM_COLOR c[2][2]; |
| 278 |
|
| IM_COLOR ci2[2]; |
| 279 |
76450 |
| double dy = sy - fsy; |
| 280 |
|
| ROT_DEBUG(fprintf(stderr, " both non-int\n")); |
| 281 |
229350 |
▶ | for (i = 0; i < 2; ++i) |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < 2; ++i) |
111948 |
55974 |
167922 |
| for (i = 0; i < 2; ++i) |
40952 |
20476 |
61428 |
|
| 282 |
458700 |
▶ | for (j = 0; j < 2; ++j) |
Branch: 2 branches
| Branch | True | False | Total |
| for (j = 0; j < 2; ++j) |
223896 |
111948 |
335844 |
| for (j = 0; j < 2; ++j) |
81904 |
40952 |
122856 |
|
| 283 |
305800 |
▶ | if (IM_GPIX(src, bx+i, by+j, &c[j][i])) |
Branch: 2 branches
| Branch | True | False | Total |
| if (IM_GPIX(src, bx+i, by+j, &c[j][i])) |
3865 |
220031 |
223896 |
| if (IM_GPIX(src, bx+i, by+j, &c[j][i])) |
1660 |
80244 |
81904 |
|
| 284 |
5525 |
| c[j][i] = back; |
| 285 |
229350 |
▶ | for (j = 0; j < 2; ++j) |
Branch: 2 branches
| Branch | True | False | Total |
| for (j = 0; j < 2; ++j) |
111948 |
55974 |
167922 |
| for (j = 0; j < 2; ++j) |
40952 |
20476 |
61428 |
|
| 286 |
152900 |
| ci2[j] = interp_i_color(c[j][0], c[j][1], dx, src->channels); |
| 287 |
76450 |
| vals[x] = interp_i_color(ci2[0], ci2[1], dy, src->channels); |
| 288 |
|
| } |
| 289 |
|
| else { |
| 290 |
|
| IM_COLOR ci2[2]; |
| 291 |
|
| ROT_DEBUG(fprintf(stderr, " y int, x non-int\n")); |
| 292 |
41562 |
▶ | for (i = 0; i < 2; ++i) |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < 2; ++i) |
27704 |
13852 |
41556 |
| for (i = 0; i < 2; ++i) |
4 |
2 |
6 |
|
| 293 |
27708 |
▶ | if (IM_GPIX(src, bx+i, sy, ci2+i)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (IM_GPIX(src, bx+i, sy, ci2+i)) |
171 |
27533 |
27704 |
| if (IM_GPIX(src, bx+i, sy, ci2+i)) |
0 |
4 |
4 |
|
| 294 |
171 |
| ci2[i] = back; |
| 295 |
13854 |
| vals[x] = interp_i_color(ci2[0], ci2[1], dx, src->channels); |
| 296 |
|
| } |
| 297 |
|
| } |
| 298 |
|
| else { |
| 299 |
4088 |
▶ | if (sy != fsy) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (sy != fsy) { |
678 |
3410 |
4088 |
| if (sy != fsy) { |
0 |
0 |
0 |
|
| 300 |
|
| IM_COLOR ci2[2]; |
| 301 |
678 |
| double dy = sy - fsy; |
| 302 |
|
| ROT_DEBUG(fprintf(stderr, " x int, y non-int\n")); |
| 303 |
2034 |
▶ | for (i = 0; i < 2; ++i) |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < 2; ++i) |
1356 |
678 |
2034 |
| for (i = 0; i < 2; ++i) |
0 |
0 |
0 |
|
| 304 |
1356 |
▶ | if (IM_GPIX(src, bx, by+i, ci2+i)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (IM_GPIX(src, bx, by+i, ci2+i)) |
1 |
1355 |
1356 |
| if (IM_GPIX(src, bx, by+i, ci2+i)) |
0 |
0 |
0 |
|
| 305 |
1 |
| ci2[i] = back; |
| 306 |
678 |
| vals[x] = interp_i_color(ci2[0], ci2[1], dy, src->channels); |
| 307 |
|
| } |
| 308 |
|
| else { |
| 309 |
|
| ROT_DEBUG(fprintf(stderr, " both int\n")); |
| 310 |
|
| /* all the world's an integer */ |
| 311 |
3410 |
▶ | if (IM_GPIX(src, bx, by, vals+x)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (IM_GPIX(src, bx, by, vals+x)) |
0 |
3410 |
3410 |
| if (IM_GPIX(src, bx, by, vals+x)) |
0 |
0 |
0 |
|
| 312 |
0 |
| vals[x] = back; |
| 313 |
|
| } |
| 314 |
|
| } |
| 315 |
|
| } |
| 316 |
|
| else { |
| 317 |
28166 |
| vals[x] = back; |
| 318 |
|
| } |
| 319 |
|
| } |
| 320 |
1002 |
| IM_PLIN(result, 0, xsize, y, vals); |
| 321 |
|
| } |
| 322 |
12 |
| myfree(vals); |
| 323 |
|
| #undef interp_i_color |
| 324 |
|
| #/code |
| 325 |
|
| } |
| 326 |
|
| else { |
| 327 |
|
| /* don't interpolate for a palette based image */ |
| 328 |
1 |
| i_palidx *vals = mymalloc(xsize * sizeof(i_palidx)); |
| 329 |
1 |
| i_palidx back = 0; |
| 330 |
1 |
| int minval = 256 * 4; |
| 331 |
|
| i_img_dim ix, iy; |
| 332 |
|
| i_color want_back; |
| 333 |
|
| i_fsample_t fsamp; |
| 334 |
|
| |
| 335 |
1 |
▶ | if (backp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (backp) { |
0 |
1 |
1 |
|
| 336 |
0 |
| want_back = *backp; |
| 337 |
|
| } |
| 338 |
1 |
▶ | else if (fbackp) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (fbackp) { |
0 |
1 |
1 |
|
| 339 |
0 |
▶ | for (ch = 0; ch < src->channels; ++ch) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ch = 0; ch < src->channels; ++ch) { |
0 |
0 |
0 |
|
| 340 |
0 |
| fsamp = fbackp->channel[ch]; |
| 341 |
0 |
▶ | want_back.channel[ch] = fsamp < 0 ? 0 : fsamp > 1 ? 255 : fsamp * 255; |
Branch: 2 branches
| Branch | True | False | Total |
| want_back.channel[ch] = fsamp < 0 ? 0 : fsamp > 1 ? 255 : fsamp * 255; |
0 |
0 |
0 |
| want_back.channel[ch] = fsamp < 0 ? 0 : fsamp > 1 ? 255 : fsamp * 255; |
0 |
0 |
0 |
|
| 342 |
|
| } |
| 343 |
|
| } |
| 344 |
|
| else { |
| 345 |
4 |
▶ | for (ch = 0; ch < src->channels; ++ch) |
Branch: 1 branch
| Branch | True | False | Total |
| for (ch = 0; ch < src->channels; ++ch) |
3 |
1 |
4 |
|
| 346 |
3 |
| want_back.channel[ch] = 0; |
| 347 |
|
| } |
| 348 |
|
| |
| 349 |
|
| /* find the closest color */ |
| 350 |
57 |
▶ | for (i = 0; i < i_colorcount(src); ++i) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < i_colorcount(src); ++i) { |
57 |
0 |
57 |
| for (i = 0; i < i_colorcount(src); ++i) { |
56 |
1 |
57 |
|
| 351 |
|
| i_color temp; |
| 352 |
|
| int tempval; |
| 353 |
56 |
▶ | i_getcolors(src, i, &temp, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| i_getcolors(src, i, &temp, 1); |
56 |
0 |
56 |
|
| 354 |
56 |
| tempval = 0; |
| 355 |
224 |
▶ | for (ch = 0; ch < src->channels; ++ch) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ch = 0; ch < src->channels; ++ch) { |
168 |
56 |
224 |
|
| 356 |
168 |
| tempval += abs(want_back.channel[ch] - temp.channel[ch]); |
| 357 |
|
| } |
| 358 |
56 |
▶ | if (tempval < minval) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tempval < minval) { |
1 |
55 |
56 |
|
| 359 |
1 |
| back = i; |
| 360 |
1 |
| minval = tempval; |
| 361 |
|
| } |
| 362 |
|
| } |
| 363 |
|
| |
| 364 |
106 |
▶ | for (y = 0; y < ysize; ++y) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (y = 0; y < ysize; ++y) { |
105 |
1 |
106 |
|
| 365 |
13860 |
▶ | for (x = 0; x < xsize; ++x) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = 0; x < xsize; ++x) { |
13755 |
105 |
13860 |
|
| 366 |
|
| /* dividing by sz gives us the ability to do perspective |
| 367 |
|
| transforms */ |
| 368 |
13755 |
| sz = x * matrix[6] + y * matrix[7] + matrix[8]; |
| 369 |
13755 |
▶ | if (fabs(sz) > 0.0000001) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fabs(sz) > 0.0000001) { |
13755 |
0 |
13755 |
|
| 370 |
13755 |
| sx = (x * matrix[0] + y * matrix[1] + matrix[2]) / sz; |
| 371 |
13755 |
| sy = (x * matrix[3] + y * matrix[4] + matrix[5]) / sz; |
| 372 |
|
| } |
| 373 |
|
| else { |
| 374 |
0 |
| sx = sy = 0; |
| 375 |
|
| } |
| 376 |
|
| |
| 377 |
|
| /* anything outside these ranges is either a broken co-ordinate |
| 378 |
|
| or outside the source */ |
| 379 |
13755 |
▶ | if (fabs(sz) > 0.0000001 |
Branch: 1 branch
| Branch | True | False | Total |
| if (fabs(sz) > 0.0000001 |
13755 |
0 |
13755 |
|
| 380 |
13755 |
▶ | && sx >= -0.5 && sx < src->xsize-0.5 |
Branch: 2 branches
| Branch | True | False | Total |
| && sx >= -0.5 && sx < src->xsize-0.5 |
13130 |
625 |
13755 |
| && sx >= -0.5 && sx < src->xsize-0.5 |
12505 |
625 |
13130 |
|
| 381 |
12505 |
▶ | && sy >= -0.5 && sy < src->ysize-0.5) { |
Branch: 2 branches
| Branch | True | False | Total |
| && sy >= -0.5 && sy < src->ysize-0.5) { |
11266 |
1239 |
12505 |
| && sy >= -0.5 && sy < src->ysize-0.5) { |
10027 |
1239 |
11266 |
|
| 382 |
|
| |
| 383 |
|
| /* all the world's an integer */ |
| 384 |
10027 |
| ix = (i_img_dim)(sx+0.5); |
| 385 |
10027 |
| iy = (i_img_dim)(sy+0.5); |
| 386 |
10027 |
▶ | if (!i_gpal(src, ix, ix+1, iy, vals+x)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!i_gpal(src, ix, ix+1, iy, vals+x)) |
10027 |
0 |
10027 |
| if (!i_gpal(src, ix, ix+1, iy, vals+x)) |
0 |
10027 |
10027 |
|
| 387 |
0 |
| vals[i] = back; |
| 388 |
|
| } |
| 389 |
|
| else { |
| 390 |
3728 |
| vals[x] = back; |
| 391 |
|
| } |
| 392 |
|
| } |
| 393 |
105 |
▶ | i_ppal(result, 0, xsize, y, vals); |
Branch: 1 branch
| Branch | True | False | Total |
| i_ppal(result, 0, xsize, y, vals); |
105 |
0 |
105 |
|
| 394 |
|
| } |
| 395 |
1 |
| myfree(vals); |
| 396 |
|
| } |
| 397 |
|
| |
| 398 |
13 |
| return result; |
| 399 |
|
| } |
| 400 |
|
| |
| 401 |
0 |
| i_img *i_matrix_transform(i_img *src, i_img_dim xsize, i_img_dim ysize, const double *matrix) { |
| 402 |
0 |
| return i_matrix_transform_bg(src, xsize, ysize, matrix, NULL, NULL); |
| 403 |
|
| } |
| 404 |
|
| |
| 405 |
|
| static void |
| 406 |
20 |
| i_matrix_mult(double *dest, const double *left, const double *right) { |
| 407 |
|
| int i, j, k; |
| 408 |
|
| double accum; |
| 409 |
|
| |
| 410 |
80 |
▶ | for (i = 0; i < 3; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 3; ++i) { |
60 |
20 |
80 |
|
| 411 |
240 |
▶ | for (j = 0; j < 3; ++j) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < 3; ++j) { |
180 |
60 |
240 |
|
| 412 |
180 |
| accum = 0.0; |
| 413 |
720 |
▶ | for (k = 0; k < 3; ++k) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < 3; ++k) { |
540 |
180 |
720 |
|
| 414 |
540 |
| accum += left[3*i+k] * right[3*k+j]; |
| 415 |
|
| } |
| 416 |
180 |
| dest[3*i+j] = accum; |
| 417 |
|
| } |
| 418 |
|
| } |
| 419 |
20 |
| } |
| 420 |
|
| |
| 421 |
|
| #define numfmt "%23g" |
| 422 |
|
| |
| 423 |
|
| ROT_DEBUG(static void dump_mat(const char *name, double *f) { |
| 424 |
|
| fprintf(stderr, "%s:\n " numfmt " " numfmt " " numfmt "\n" |
| 425 |
|
| " " numfmt " " numfmt " " numfmt "\n" |
| 426 |
|
| " " numfmt " " numfmt " " numfmt "\n", |
| 427 |
|
| name, f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8]); |
| 428 |
|
| }) |
| 429 |
|
| |
| 430 |
10 |
| i_img *i_rotate_exact_bg(i_img *src, double amount, |
| 431 |
|
| const i_color *backp, const i_fcolor *fbackp) { |
| 432 |
10 |
| double xlate1[9] = { 0 }; |
| 433 |
|
| double rotate[9]; |
| 434 |
10 |
| double xlate2[9] = { 0 }; |
| 435 |
|
| double temp[9], matrix[9]; |
| 436 |
|
| i_img_dim x1, x2, y1, y2, newxsize, newysize; |
| 437 |
|
| |
| 438 |
|
| ROT_DEBUG(fprintf(stderr, "rotate angle %.20g\n", amount)); |
| 439 |
|
| |
| 440 |
|
| /* first translate the centre of the image to (0,0) */ |
| 441 |
10 |
| xlate1[0] = 1; |
| 442 |
10 |
| xlate1[2] = (src->xsize-1)/2.0; |
| 443 |
10 |
| xlate1[4] = 1; |
| 444 |
10 |
| xlate1[5] = (src->ysize-1)/2.0; |
| 445 |
10 |
| xlate1[8] = 1; |
| 446 |
|
| |
| 447 |
|
| ROT_DEBUG(dump_mat("xlate1", xlate1)); |
| 448 |
|
| |
| 449 |
|
| /* rotate around (0.0) */ |
| 450 |
10 |
| rotate[0] = cos(amount); |
| 451 |
10 |
| rotate[1] = sin(amount); |
| 452 |
10 |
| rotate[2] = 0; |
| 453 |
10 |
| rotate[3] = -rotate[1]; |
| 454 |
10 |
| rotate[4] = rotate[0]; |
| 455 |
10 |
| rotate[5] = 0; |
| 456 |
10 |
| rotate[6] = 0; |
| 457 |
10 |
| rotate[7] = 0; |
| 458 |
10 |
| rotate[8] = 1; |
| 459 |
|
| |
| 460 |
|
| ROT_DEBUG(dump_mat("rotate", rotate)); |
| 461 |
|
| |
| 462 |
|
| ROT_DEBUG(fprintf(stderr, "cos %g sin %g\n", rotate[0], rotate[1])); |
| 463 |
|
| |
| 464 |
10 |
| x1 = ceil(fabs(src->xsize * rotate[0] + src->ysize * rotate[1]) - 0.0001); |
| 465 |
10 |
| x2 = ceil(fabs(src->xsize * rotate[0] - src->ysize * rotate[1]) - 0.0001); |
| 466 |
10 |
| y1 = ceil(fabs(src->xsize * rotate[3] + src->ysize * rotate[4]) - 0.0001); |
| 467 |
10 |
| y2 = ceil(fabs(src->xsize * rotate[3] - src->ysize * rotate[4]) - 0.0001); |
| 468 |
|
| ROT_DEBUG(fprintf(stderr, "x1 y1 " i_DFp " x2 y2 " i_DFp "\n", i_DFcp(x1, y1), i_DFcp(x2, y2))); |
| 469 |
10 |
| newxsize = x1 > x2 ? x1 : x2; |
| 470 |
10 |
| newysize = y1 > y2 ? y1 : y2; |
| 471 |
|
| /* translate the centre back to the center of the image */ |
| 472 |
10 |
| xlate2[0] = 1; |
| 473 |
10 |
| xlate2[2] = -(newxsize-1)/2.0; |
| 474 |
10 |
| xlate2[4] = 1; |
| 475 |
10 |
| xlate2[5] = -(newysize-1)/2.0; |
| 476 |
10 |
| xlate2[8] = 1; |
| 477 |
|
| |
| 478 |
|
| ROT_DEBUG(dump_mat("xlate2", xlate2)); |
| 479 |
|
| |
| 480 |
10 |
| i_matrix_mult(temp, xlate1, rotate); |
| 481 |
10 |
| i_matrix_mult(matrix, temp, xlate2); |
| 482 |
|
| |
| 483 |
|
| ROT_DEBUG(dump_mat("matrxi", matrix)); |
| 484 |
|
| |
| 485 |
10 |
| return i_matrix_transform_bg(src, newxsize, newysize, matrix, backp, fbackp); |
| 486 |
|
| } |
| 487 |
|
| |
| 488 |
0 |
| i_img *i_rotate_exact(i_img *src, double amount) { |
| 489 |
0 |
| return i_rotate_exact_bg(src, amount, NULL, NULL); |
| 490 |
|
| } |
| 491 |
|
| |
| 492 |
|
| |
| 493 |
|
| /* |
| 494 |
|
| =back |
| 495 |
|
| |
| 496 |
|
| =head1 AUTHOR |
| 497 |
|
| |
| 498 |
|
| Tony Cook <tony@develop-help.com> |
| 499 |
|
| |
| 500 |
|
| =head1 SEE ALSO |
| 501 |
|
| |
| 502 |
|
| Imager(3) |
| 503 |
|
| |
| 504 |
|
| =cut |
| 505 |
|
| */ |