4 rotate.c - implements image rotations
8 i_img *i_rotate90(i_img *src, int degrees)
12 Implements basic 90 degree rotations of an image.
14 Other rotations will be added as tuits become available.
21 #include <math.h> /* for floor() */
23 i_img *i_rotate90(i_img *src, int degrees) {
30 /* essentially the same as flipxy(..., 2) except that it's not
32 targ = i_sametype(src, src->xsize, src->ysize);
33 if (src->type == i_direct_type) {
34 if (src->bits == i_8_bits) {
35 i_color *vals = mymalloc(src->xsize * sizeof(i_color));
36 for (y = 0; y < src->ysize; ++y) {
38 i_glin(src, 0, src->xsize, y, vals);
39 for (x = 0; x < src->xsize/2; ++x) {
41 vals[x] = vals[src->xsize - x - 1];
42 vals[src->xsize - x - 1] = tmp;
44 i_plin(targ, 0, src->xsize, src->ysize - y - 1, vals);
49 i_fcolor *vals = mymalloc(src->xsize * sizeof(i_fcolor));
50 for (y = 0; y < src->ysize; ++y) {
52 i_glinf(src, 0, src->xsize, y, vals);
53 for (x = 0; x < src->xsize/2; ++x) {
55 vals[x] = vals[src->xsize - x - 1];
56 vals[src->xsize - x - 1] = tmp;
58 i_plinf(targ, 0, src->xsize, src->ysize - y - 1, vals);
64 i_palidx *vals = mymalloc(src->xsize * sizeof(i_palidx));
66 for (y = 0; y < src->ysize; ++y) {
68 i_gpal(src, 0, src->xsize, y, vals);
69 for (x = 0; x < src->xsize/2; ++x) {
71 vals[x] = vals[src->xsize - x - 1];
72 vals[src->xsize - x - 1] = tmp;
74 i_ppal(targ, 0, src->xsize, src->ysize - y - 1, vals);
82 else if (degrees == 270 || degrees == 90) {
83 i_img_dim tx, txstart, txinc;
84 i_img_dim ty, tystart, tyinc;
89 tystart = src->xsize-1;
93 txstart = src->ysize-1;
98 targ = i_sametype(src, src->ysize, src->xsize);
99 if (src->type == i_direct_type) {
100 if (src->bits == i_8_bits) {
101 i_color *vals = mymalloc(src->xsize * sizeof(i_color));
104 for (y = 0; y < src->ysize; ++y) {
105 i_glin(src, 0, src->xsize, y, vals);
107 for (x = 0; x < src->xsize; ++x) {
108 i_ppix(targ, tx, ty, vals+x);
116 i_fcolor *vals = mymalloc(src->xsize * sizeof(i_fcolor));
119 for (y = 0; y < src->ysize; ++y) {
120 i_glinf(src, 0, src->xsize, y, vals);
122 for (x = 0; x < src->xsize; ++x) {
123 i_ppixf(targ, tx, ty, vals+x);
132 i_palidx *vals = mymalloc(src->xsize * sizeof(i_palidx));
135 for (y = 0; y < src->ysize; ++y) {
136 i_gpal(src, 0, src->xsize, y, vals);
138 for (x = 0; x < src->xsize; ++x) {
139 i_ppal(targ, tx, tx+1, ty, vals+x);
149 i_push_error(0, "i_rotate90() only rotates at 90, 180, or 270 degrees");
154 /* linear interpolation */
155 static i_color interp_i_color(i_color before, i_color after, double pos,
161 if (channels == 1 || channels == 3) {
162 for (ch = 0; ch < channels; ++ch)
163 out.channel[ch] = (1-pos) * before.channel[ch] + pos * after.channel[ch];
166 int total_cover = (1-pos) * before.channel[channels-1]
167 + pos * after.channel[channels-1];
169 total_cover = I_LIMIT_8(total_cover);
171 double before_alpha = before.channel[channels-1] / 255.0;
172 double after_alpha = after.channel[channels-1] / 255.0;
173 double total_alpha = before_alpha * (1-pos) + after_alpha * pos;
175 for (ch = 0; ch < channels-1; ++ch) {
176 int out_level = ((1-pos) * before.channel[ch] * before_alpha +
177 pos * after.channel[ch] * after_alpha + 0.5) / total_alpha;
179 out.channel[ch] = I_LIMIT_8(out_level);
183 out.channel[channels-1] = total_cover;
189 /* hopefully this will be inlined (it is with -O3 with gcc 2.95.4) */
190 /* linear interpolation */
191 static i_fcolor interp_i_fcolor(i_fcolor before, i_fcolor after, double pos,
197 if (channels == 1 || channels == 3) {
198 for (ch = 0; ch < channels; ++ch)
199 out.channel[ch] = (1-pos) * before.channel[ch] + pos * after.channel[ch];
202 double total_cover = (1-pos) * before.channel[channels-1]
203 + pos * after.channel[channels-1];
205 total_cover = I_LIMIT_DOUBLE(total_cover);
207 double before_alpha = before.channel[channels-1];
208 double after_alpha = after.channel[channels-1];
209 double total_alpha = before_alpha * (1-pos) + after_alpha * pos;
211 for (ch = 0; ch < channels-1; ++ch) {
212 double out_level = ((1-pos) * before.channel[ch] * before_alpha +
213 pos * after.channel[ch] * after_alpha) / total_alpha;
215 out.channel[ch] = I_LIMIT_DOUBLE(out_level);
219 out.channel[channels-1] = total_cover;
225 i_img *i_matrix_transform_bg(i_img *src, i_img_dim xsize, i_img_dim ysize, const double *matrix,
226 const i_color *backp, const i_fcolor *fbackp) {
227 i_img *result = i_sametype(src, xsize, ysize);
233 if (src->type == i_direct_type) {
234 if (src->bits == i_8_bits) {
235 i_color *vals = mymalloc(xsize * sizeof(i_color));
243 for (ch = 0; ch < src->channels; ++ch) {
244 fsamp = fbackp->channel[ch];
245 back.channel[ch] = fsamp < 0 ? 0 : fsamp > 1 ? 255 : fsamp * 255;
249 for (ch = 0; ch < src->channels; ++ch)
250 back.channel[ch] = 0;
253 for (y = 0; y < ysize; ++y) {
254 for (x = 0; x < xsize; ++x) {
255 /* dividing by sz gives us the ability to do perspective
257 sz = x * matrix[6] + y * matrix[7] + matrix[8];
258 if (fabs(sz) > 0.0000001) {
259 sx = (x * matrix[0] + y * matrix[1] + matrix[2]) / sz;
260 sy = (x * matrix[3] + y * matrix[4] + matrix[5]) / sz;
266 /* anything outside these ranges is either a broken co-ordinate
267 or outside the source */
268 if (fabs(sz) > 0.0000001
269 && sx >= -1 && sx < src->xsize
270 && sy >= -1 && sy < src->ysize) {
272 if (sx != (i_img_dim)sx) {
273 if (sy != (i_img_dim)sy) {
276 for (i = 0; i < 2; ++i)
277 for (j = 0; j < 2; ++j)
278 if (i_gpix(src, floor(sx)+i, floor(sy)+j, &c[j][i]))
280 for (j = 0; j < 2; ++j)
281 ci2[j] = interp_i_color(c[j][0], c[j][1], sx, src->channels);
282 vals[x] = interp_i_color(ci2[0], ci2[1], sy, src->channels);
286 for (i = 0; i < 2; ++i)
287 if (i_gpix(src, floor(sx)+i, sy, ci2+i))
289 vals[x] = interp_i_color(ci2[0], ci2[1], sx, src->channels);
293 if (sy != (i_img_dim)sy) {
295 for (i = 0; i < 2; ++i)
296 if (i_gpix(src, sx, floor(sy)+i, ci2+i))
298 vals[x] = interp_i_color(ci2[0], ci2[1], sy, src->channels);
301 /* all the world's an integer */
302 if (i_gpix(src, sx, sy, vals+x))
311 i_plin(result, 0, xsize, y, vals);
316 i_fcolor *vals = mymalloc(xsize * sizeof(i_fcolor));
323 for (ch = 0; ch < src->channels; ++ch)
324 back.channel[ch] = backp->channel[ch] / 255.0;
327 for (ch = 0; ch < src->channels; ++ch)
328 back.channel[ch] = 0;
331 for (y = 0; y < ysize; ++y) {
332 for (x = 0; x < xsize; ++x) {
333 /* dividing by sz gives us the ability to do perspective
335 sz = x * matrix[6] + y * matrix[7] + matrix[8];
336 if (fabs(sz) > 0.0000001) {
337 sx = (x * matrix[0] + y * matrix[1] + matrix[2]) / sz;
338 sy = (x * matrix[3] + y * matrix[4] + matrix[5]) / sz;
344 /* anything outside these ranges is either a broken co-ordinate
345 or outside the source */
346 if (fabs(sz) > 0.0000001
347 && sx >= -1 && sx < src->xsize
348 && sy >= -1 && sy < src->ysize) {
350 if (sx != (i_img_dim)sx) {
351 if (sy != (i_img_dim)sy) {
354 for (i = 0; i < 2; ++i)
355 for (j = 0; j < 2; ++j)
356 if (i_gpixf(src, floor(sx)+i, floor(sy)+j, &c[j][i]))
358 for (j = 0; j < 2; ++j)
359 ci2[j] = interp_i_fcolor(c[j][0], c[j][1], sx, src->channels);
360 vals[x] = interp_i_fcolor(ci2[0], ci2[1], sy, src->channels);
364 for (i = 0; i < 2; ++i)
365 if (i_gpixf(src, floor(sx)+i, sy, ci2+i))
367 vals[x] = interp_i_fcolor(ci2[0], ci2[1], sx, src->channels);
371 if (sy != (i_img_dim)sy) {
373 for (i = 0; i < 2; ++i)
374 if (i_gpixf(src, sx, floor(sy)+i, ci2+i))
376 vals[x] = interp_i_fcolor(ci2[0], ci2[1], sy, src->channels);
379 /* all the world's an integer */
380 if (i_gpixf(src, sx, sy, vals+x))
389 i_plinf(result, 0, xsize, y, vals);
395 /* don't interpolate for a palette based image */
396 i_palidx *vals = mymalloc(xsize * sizeof(i_palidx));
399 int minval = 256 * 4;
408 for (ch = 0; ch < src->channels; ++ch) {
409 fsamp = fbackp->channel[ch];
410 want_back.channel[ch] = fsamp < 0 ? 0 : fsamp > 1 ? 255 : fsamp * 255;
414 for (ch = 0; ch < src->channels; ++ch)
415 want_back.channel[ch] = 0;
418 /* find the closest color */
419 for (i = 0; i < i_colorcount(src); ++i) {
422 i_getcolors(src, i, &temp, 1);
424 for (ch = 0; ch < src->channels; ++ch) {
425 tempval += abs(want_back.channel[ch] - temp.channel[ch]);
427 if (tempval < minval) {
434 for (y = 0; y < ysize; ++y) {
435 for (x = 0; x < xsize; ++x) {
436 /* dividing by sz gives us the ability to do perspective
438 sz = x * matrix[6] + y * matrix[7] + matrix[8];
439 if (abs(sz) > 0.0000001) {
440 sx = (x * matrix[0] + y * matrix[1] + matrix[2]) / sz;
441 sy = (x * matrix[3] + y * matrix[4] + matrix[5]) / sz;
447 /* anything outside these ranges is either a broken co-ordinate
448 or outside the source */
449 if (abs(sz) > 0.0000001
450 && sx >= -0.5 && sx < src->xsize-0.5
451 && sy >= -0.5 && sy < src->ysize-0.5) {
453 /* all the world's an integer */
454 ix = (i_img_dim)(sx+0.5);
455 iy = (i_img_dim)(sy+0.5);
456 if (!i_gpal(src, ix, ix+1, iy, vals+x))
463 i_ppal(result, 0, xsize, y, vals);
471 i_img *i_matrix_transform(i_img *src, i_img_dim xsize, i_img_dim ysize, const double *matrix) {
472 return i_matrix_transform_bg(src, xsize, ysize, matrix, NULL, NULL);
476 i_matrix_mult(double *dest, const double *left, const double *right) {
480 for (i = 0; i < 3; ++i) {
481 for (j = 0; j < 3; ++j) {
483 for (k = 0; k < 3; ++k) {
484 accum += left[3*i+k] * right[3*k+j];
491 i_img *i_rotate_exact_bg(i_img *src, double amount,
492 const i_color *backp, const i_fcolor *fbackp) {
493 double xlate1[9] = { 0 };
495 double xlate2[9] = { 0 };
496 double temp[9], matrix[9];
497 i_img_dim x1, x2, y1, y2, newxsize, newysize;
499 /* first translate the centre of the image to (0,0) */
501 xlate1[2] = src->xsize/2.0;
503 xlate1[5] = src->ysize/2.0;
506 /* rotate around (0.0) */
507 rotate[0] = cos(amount);
508 rotate[1] = sin(amount);
510 rotate[3] = -rotate[1];
511 rotate[4] = rotate[0];
517 x1 = ceil(i_abs(src->xsize * rotate[0] + src->ysize * rotate[1]));
518 x2 = ceil(i_abs(src->xsize * rotate[0] - src->ysize * rotate[1]));
519 y1 = ceil(i_abs(src->xsize * rotate[3] + src->ysize * rotate[4]));
520 y2 = ceil(i_abs(src->xsize * rotate[3] - src->ysize * rotate[4]));
521 newxsize = x1 > x2 ? x1 : x2;
522 newysize = y1 > y2 ? y1 : y2;
523 /* translate the centre back to the center of the image */
525 xlate2[2] = -newxsize/2.0;
527 xlate2[5] = -newysize/2.0;
529 i_matrix_mult(temp, xlate1, rotate);
530 i_matrix_mult(matrix, temp, xlate2);
532 return i_matrix_transform_bg(src, newxsize, newysize, matrix, backp, fbackp);
535 i_img *i_rotate_exact(i_img *src, double amount) {
536 return i_rotate_exact_bg(src, amount, NULL, NULL);
545 Tony Cook <tony@develop-help.com>