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.
20 #include <math.h> /* for floor() */
22 i_img *i_rotate90(i_img *src, int degrees) {
29 /* essentially the same as flipxy(..., 2) except that it's not
31 targ = i_sametype(src, src->xsize, src->ysize);
32 if (src->type == i_direct_type) {
33 if (src->bits == i_8_bits) {
34 i_color *vals = mymalloc(src->xsize * sizeof(i_color));
35 for (y = 0; y < src->ysize; ++y) {
37 i_glin(src, 0, src->xsize, y, vals);
38 for (x = 0; x < src->xsize/2; ++x) {
40 vals[x] = vals[src->xsize - x - 1];
41 vals[src->xsize - x - 1] = tmp;
43 i_plin(targ, 0, src->xsize, src->ysize - y - 1, vals);
48 i_fcolor *vals = mymalloc(src->xsize * sizeof(i_fcolor));
49 for (y = 0; y < src->ysize; ++y) {
51 i_glinf(src, 0, src->xsize, y, vals);
52 for (x = 0; x < src->xsize/2; ++x) {
54 vals[x] = vals[src->xsize - x - 1];
55 vals[src->xsize - x - 1] = tmp;
57 i_plinf(targ, 0, src->xsize, src->ysize - y - 1, vals);
63 i_palidx *vals = mymalloc(src->xsize * sizeof(i_palidx));
65 for (y = 0; y < src->ysize; ++y) {
67 i_gpal(src, 0, src->xsize, y, vals);
68 for (x = 0; x < src->xsize/2; ++x) {
70 vals[x] = vals[src->xsize - x - 1];
71 vals[src->xsize - x - 1] = tmp;
73 i_ppal(targ, 0, src->xsize, src->ysize - y - 1, vals);
81 else if (degrees == 270 || degrees == 90) {
82 int tx, txstart, txinc;
83 int ty, tystart, tyinc;
88 tystart = src->xsize-1;
92 txstart = src->ysize-1;
97 targ = i_sametype(src, src->ysize, src->xsize);
98 if (src->type == i_direct_type) {
99 if (src->bits == i_8_bits) {
100 i_color *vals = mymalloc(src->xsize * sizeof(i_color));
103 for (y = 0; y < src->ysize; ++y) {
104 i_glin(src, 0, src->xsize, y, vals);
106 for (x = 0; x < src->xsize; ++x) {
107 i_ppix(targ, tx, ty, vals+x);
115 i_fcolor *vals = mymalloc(src->xsize * sizeof(i_fcolor));
118 for (y = 0; y < src->ysize; ++y) {
119 i_glinf(src, 0, src->xsize, y, vals);
121 for (x = 0; x < src->xsize; ++x) {
122 i_ppixf(targ, tx, ty, vals+x);
131 i_palidx *vals = mymalloc(src->xsize * sizeof(i_palidx));
134 for (y = 0; y < src->ysize; ++y) {
135 i_gpal(src, 0, src->xsize, y, vals);
137 for (x = 0; x < src->xsize; ++x) {
138 i_ppal(targ, tx, tx+1, ty, vals+x);
148 i_push_error(0, "i_rotate90() only rotates at 90, 180, or 270 degrees");
153 /* hopefully this will be inlined (it is with -O3 with gcc 2.95.4) */
154 /* linear interpolation */
155 static i_color interp_i_color(i_color before, i_color after, double pos,
161 for (ch = 0; ch < channels; ++ch)
162 out.channel[ch] = (1-pos) * before.channel[ch] + pos * after.channel[ch];
167 /* hopefully this will be inlined (it is with -O3 with gcc 2.95.4) */
168 /* linear interpolation */
169 static i_fcolor interp_i_fcolor(i_fcolor before, i_fcolor after, double pos,
175 for (ch = 0; ch < channels; ++ch)
176 out.channel[ch] = (1-pos) * before.channel[ch] + pos * after.channel[ch];
181 i_img *i_matrix_transform_bg(i_img *src, int xsize, int ysize, const double *matrix,
182 const i_color *backp, const i_fcolor *fbackp) {
183 i_img *result = i_sametype(src, xsize, ysize);
189 if (src->type == i_direct_type) {
190 if (src->bits == i_8_bits) {
191 i_color *vals = mymalloc(xsize * sizeof(i_color));
199 for (ch = 0; ch < src->channels; ++ch) {
200 fsamp = fbackp->channel[ch];
201 back.channel[ch] = fsamp < 0 ? 0 : fsamp > 1 ? 255 : fsamp * 255;
205 for (ch = 0; ch < src->channels; ++ch)
206 back.channel[ch] = 0;
209 for (y = 0; y < ysize; ++y) {
210 for (x = 0; x < xsize; ++x) {
211 /* dividing by sz gives us the ability to do perspective
213 sz = x * matrix[6] + y * matrix[7] + matrix[8];
214 if (fabs(sz) > 0.0000001) {
215 sx = (x * matrix[0] + y * matrix[1] + matrix[2]) / sz;
216 sy = (x * matrix[3] + y * matrix[4] + matrix[5]) / sz;
222 /* anything outside these ranges is either a broken co-ordinate
223 or outside the source */
224 if (fabs(sz) > 0.0000001
225 && sx >= -1 && sx < src->xsize
226 && sy >= -1 && sy < src->ysize) {
232 for (i = 0; i < 2; ++i)
233 for (j = 0; j < 2; ++j)
234 if (i_gpix(src, floor(sx)+i, floor(sy)+j, &c[j][i]))
236 for (j = 0; j < 2; ++j)
237 ci2[j] = interp_i_color(c[j][0], c[j][1], sx, src->channels);
238 vals[x] = interp_i_color(ci2[0], ci2[1], sy, src->channels);
242 for (i = 0; i < 2; ++i)
243 if (i_gpix(src, floor(sx)+i, sy, ci2+i))
245 vals[x] = interp_i_color(ci2[0], ci2[1], sx, src->channels);
251 for (i = 0; i < 2; ++i)
252 if (i_gpix(src, sx, floor(sy)+i, ci2+i))
254 vals[x] = interp_i_color(ci2[0], ci2[1], sy, src->channels);
257 /* all the world's an integer */
258 if (i_gpix(src, sx, sy, vals+x))
267 i_plin(result, 0, xsize, y, vals);
272 i_fcolor *vals = mymalloc(xsize * sizeof(i_fcolor));
279 for (ch = 0; ch < src->channels; ++ch)
280 back.channel[ch] = backp->channel[ch] / 255.0;
283 for (ch = 0; ch < src->channels; ++ch)
284 back.channel[ch] = 0;
287 for (y = 0; y < ysize; ++y) {
288 for (x = 0; x < xsize; ++x) {
289 /* dividing by sz gives us the ability to do perspective
291 sz = x * matrix[6] + y * matrix[7] + matrix[8];
292 if (fabs(sz) > 0.0000001) {
293 sx = (x * matrix[0] + y * matrix[1] + matrix[2]) / sz;
294 sy = (x * matrix[3] + y * matrix[4] + matrix[5]) / sz;
300 /* anything outside these ranges is either a broken co-ordinate
301 or outside the source */
302 if (fabs(sz) > 0.0000001
303 && sx >= -1 && sx < src->xsize
304 && sy >= -1 && sy < src->ysize) {
310 for (i = 0; i < 2; ++i)
311 for (j = 0; j < 2; ++j)
312 if (i_gpixf(src, floor(sx)+i, floor(sy)+j, &c[j][i]))
314 for (j = 0; j < 2; ++j)
315 ci2[j] = interp_i_fcolor(c[j][0], c[j][1], sx, src->channels);
316 vals[x] = interp_i_fcolor(ci2[0], ci2[1], sy, src->channels);
320 for (i = 0; i < 2; ++i)
321 if (i_gpixf(src, floor(sx)+i, sy, ci2+i))
323 vals[x] = interp_i_fcolor(ci2[0], ci2[1], sx, src->channels);
329 for (i = 0; i < 2; ++i)
330 if (i_gpixf(src, sx, floor(sy)+i, ci2+i))
332 vals[x] = interp_i_fcolor(ci2[0], ci2[1], sy, src->channels);
335 /* all the world's an integer */
336 if (i_gpixf(src, sx, sy, vals+x))
345 i_plinf(result, 0, xsize, y, vals);
351 /* don't interpolate for a palette based image */
352 i_palidx *vals = mymalloc(xsize * sizeof(i_palidx));
355 int minval = 256 * 4;
364 for (ch = 0; ch < src->channels; ++ch) {
365 fsamp = fbackp->channel[ch];
366 want_back.channel[ch] = fsamp < 0 ? 0 : fsamp > 1 ? 255 : fsamp * 255;
370 for (ch = 0; ch < src->channels; ++ch)
371 want_back.channel[ch] = 0;
374 /* find the closest color */
375 for (i = 0; i < i_colorcount(src); ++i) {
378 i_getcolors(src, i, &temp, 1);
380 for (ch = 0; ch < src->channels; ++ch) {
381 tempval += abs(want_back.channel[ch] - temp.channel[ch]);
383 if (tempval < minval) {
390 for (y = 0; y < ysize; ++y) {
391 for (x = 0; x < xsize; ++x) {
392 /* dividing by sz gives us the ability to do perspective
394 sz = x * matrix[6] + y * matrix[7] + matrix[8];
395 if (abs(sz) > 0.0000001) {
396 sx = (x * matrix[0] + y * matrix[1] + matrix[2]) / sz;
397 sy = (x * matrix[3] + y * matrix[4] + matrix[5]) / sz;
403 /* anything outside these ranges is either a broken co-ordinate
404 or outside the source */
405 if (abs(sz) > 0.0000001
406 && sx >= -0.5 && sx < src->xsize-0.5
407 && sy >= -0.5 && sy < src->ysize-0.5) {
409 /* all the world's an integer */
412 if (!i_gpal(src, ix, ix+1, iy, vals+x))
419 i_ppal(result, 0, xsize, y, vals);
427 i_img *i_matrix_transform(i_img *src, int xsize, int ysize, const double *matrix) {
428 return i_matrix_transform_bg(src, xsize, ysize, matrix, NULL, NULL);
432 i_matrix_mult(double *dest, const double *left, const double *right) {
436 for (i = 0; i < 3; ++i) {
437 for (j = 0; j < 3; ++j) {
439 for (k = 0; k < 3; ++k) {
440 accum += left[3*i+k] * right[3*k+j];
447 i_img *i_rotate_exact_bg(i_img *src, double amount,
448 const i_color *backp, const i_fcolor *fbackp) {
449 double xlate1[9] = { 0 };
451 double xlate2[9] = { 0 };
452 double temp[9], matrix[9];
453 int x1, x2, y1, y2, newxsize, newysize;
455 /* first translate the centre of the image to (0,0) */
457 xlate1[2] = src->xsize/2.0;
459 xlate1[5] = src->ysize/2.0;
462 /* rotate around (0.0) */
463 rotate[0] = cos(amount);
464 rotate[1] = sin(amount);
466 rotate[3] = -rotate[1];
467 rotate[4] = rotate[0];
473 x1 = ceil(abs(src->xsize * rotate[0] + src->ysize * rotate[1]));
474 x2 = ceil(abs(src->xsize * rotate[0] - src->ysize * rotate[1]));
475 y1 = ceil(abs(src->xsize * rotate[3] + src->ysize * rotate[4]));
476 y2 = ceil(abs(src->xsize * rotate[3] - src->ysize * rotate[4]));
477 newxsize = x1 > x2 ? x1 : x2;
478 newysize = y1 > y2 ? y1 : y2;
479 /* translate the centre back to the center of the image */
481 xlate2[2] = -newxsize/2;
483 xlate2[5] = -newysize/2;
485 i_matrix_mult(temp, xlate1, rotate);
486 i_matrix_mult(matrix, temp, xlate2);
488 return i_matrix_transform_bg(src, newxsize, newysize, matrix, backp, fbackp);
491 i_img *i_rotate_exact(i_img *src, double amount) {
492 return i_rotate_exact_bg(src, amount, NULL, NULL);
501 Tony Cook <tony@develop-help.com>