4 rotate.im - 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) {
35 IM_COLOR *vals = mymalloc(src->xsize * sizeof(IM_COLOR));
36 for (y = 0; y < src->ysize; ++y) {
38 IM_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 IM_PLIN(targ, 0, src->xsize, src->ysize - y - 1, vals);
50 i_palidx *vals = mymalloc(src->xsize * sizeof(i_palidx));
52 for (y = 0; y < src->ysize; ++y) {
54 i_gpal(src, 0, src->xsize, y, vals);
55 for (x = 0; x < src->xsize/2; ++x) {
57 vals[x] = vals[src->xsize - x - 1];
58 vals[src->xsize - x - 1] = tmp;
60 i_ppal(targ, 0, src->xsize, src->ysize - y - 1, vals);
68 else if (degrees == 270 || degrees == 90) {
69 i_img_dim tx, txstart, txinc;
70 i_img_dim ty, tystart, tyinc;
75 tystart = src->xsize-1;
79 txstart = src->ysize-1;
84 targ = i_sametype(src, src->ysize, src->xsize);
85 if (src->type == i_direct_type) {
87 IM_COLOR *vals = mymalloc(src->xsize * sizeof(IM_COLOR));
90 for (y = 0; y < src->ysize; ++y) {
91 IM_GLIN(src, 0, src->xsize, y, vals);
93 for (x = 0; x < src->xsize; ++x) {
94 IM_PPIX(targ, tx, ty, vals+x);
103 i_palidx *vals = mymalloc(src->xsize * sizeof(i_palidx));
106 for (y = 0; y < src->ysize; ++y) {
107 i_gpal(src, 0, src->xsize, y, vals);
109 for (x = 0; x < src->xsize; ++x) {
110 i_ppal(targ, tx, tx+1, ty, vals+x);
120 i_push_error(0, "i_rotate90() only rotates at 90, 180, or 270 degrees");
125 /* linear interpolation */
126 static i_color interp_i_color(i_color before, i_color after, double pos,
132 if (channels == 1 || channels == 3) {
133 for (ch = 0; ch < channels; ++ch)
134 out.channel[ch] = (1-pos) * before.channel[ch] + pos * after.channel[ch];
137 int total_cover = (1-pos) * before.channel[channels-1]
138 + pos * after.channel[channels-1];
140 total_cover = I_LIMIT_8(total_cover);
142 double before_alpha = before.channel[channels-1] / 255.0;
143 double after_alpha = after.channel[channels-1] / 255.0;
144 double total_alpha = before_alpha * (1-pos) + after_alpha * pos;
146 for (ch = 0; ch < channels-1; ++ch) {
147 int out_level = ((1-pos) * before.channel[ch] * before_alpha +
148 pos * after.channel[ch] * after_alpha) / total_alpha + 0.5;
150 out.channel[ch] = I_LIMIT_8(out_level);
154 out.channel[channels-1] = total_cover;
160 /* hopefully this will be inlined (it is with -O3 with gcc 2.95.4) */
161 /* linear interpolation */
162 static i_fcolor interp_i_fcolor(i_fcolor before, i_fcolor after, double pos,
168 if (channels == 1 || channels == 3) {
169 for (ch = 0; ch < channels; ++ch)
170 out.channel[ch] = (1-pos) * before.channel[ch] + pos * after.channel[ch];
173 double total_cover = (1-pos) * before.channel[channels-1]
174 + pos * after.channel[channels-1];
176 total_cover = I_LIMIT_DOUBLE(total_cover);
178 double before_alpha = before.channel[channels-1];
179 double after_alpha = after.channel[channels-1];
180 double total_alpha = before_alpha * (1-pos) + after_alpha * pos;
182 for (ch = 0; ch < channels-1; ++ch) {
183 double out_level = ((1-pos) * before.channel[ch] * before_alpha +
184 pos * after.channel[ch] * after_alpha) / total_alpha;
186 out.channel[ch] = I_LIMIT_DOUBLE(out_level);
190 out.channel[channels-1] = total_cover;
196 i_img *i_matrix_transform_bg(i_img *src, i_img_dim xsize, i_img_dim ysize, const double *matrix,
197 const i_color *backp, const i_fcolor *fbackp) {
198 i_img *result = i_sametype(src, xsize, ysize);
204 if (src->type == i_direct_type) {
206 IM_COLOR *vals = mymalloc(xsize * sizeof(IM_COLOR));
215 for (ch = 0; ch < src->channels; ++ch) {
216 fsamp = fbackp->channel[ch];
217 back.channel[ch] = fsamp < 0 ? 0 : fsamp > 1 ? 255 : fsamp * 255;
221 #define interp_i_color interp_i_fcolor
226 for (ch = 0; ch < src->channels; ++ch)
227 back.channel[ch] = backp->channel[ch] / 255.0;
231 for (ch = 0; ch < src->channels; ++ch)
232 back.channel[ch] = 0;
235 for (y = 0; y < ysize; ++y) {
236 for (x = 0; x < xsize; ++x) {
237 /* dividing by sz gives us the ability to do perspective
239 sz = x * matrix[6] + y * matrix[7] + matrix[8];
240 if (fabs(sz) > 0.0000001) {
241 sx = (x * matrix[0] + y * matrix[1] + matrix[2]) / sz;
242 sy = (x * matrix[3] + y * matrix[4] + matrix[5]) / sz;
248 /* anything outside these ranges is either a broken co-ordinate
249 or outside the source */
250 if (fabs(sz) > 0.0000001
251 && sx >= -1 && sx < src->xsize
252 && sy >= -1 && sy < src->ysize) {
254 if (sx != (i_img_dim)sx) {
255 if (sy != (i_img_dim)sy) {
258 for (i = 0; i < 2; ++i)
259 for (j = 0; j < 2; ++j)
260 if (IM_GPIX(src, floor(sx)+i, floor(sy)+j, &c[j][i]))
262 for (j = 0; j < 2; ++j)
263 ci2[j] = interp_i_color(c[j][0], c[j][1], sx, src->channels);
264 vals[x] = interp_i_color(ci2[0], ci2[1], sy, src->channels);
268 for (i = 0; i < 2; ++i)
269 if (IM_GPIX(src, floor(sx)+i, sy, ci2+i))
271 vals[x] = interp_i_color(ci2[0], ci2[1], sx, src->channels);
275 if (sy != (i_img_dim)sy) {
277 for (i = 0; i < 2; ++i)
278 if (IM_GPIX(src, sx, floor(sy)+i, ci2+i))
280 vals[x] = interp_i_color(ci2[0], ci2[1], sy, src->channels);
283 /* all the world's an integer */
284 if (IM_GPIX(src, sx, sy, vals+x))
293 IM_PLIN(result, 0, xsize, y, vals);
296 #undef interp_i_color
300 /* don't interpolate for a palette based image */
301 i_palidx *vals = mymalloc(xsize * sizeof(i_palidx));
304 int minval = 256 * 4;
313 for (ch = 0; ch < src->channels; ++ch) {
314 fsamp = fbackp->channel[ch];
315 want_back.channel[ch] = fsamp < 0 ? 0 : fsamp > 1 ? 255 : fsamp * 255;
319 for (ch = 0; ch < src->channels; ++ch)
320 want_back.channel[ch] = 0;
323 /* find the closest color */
324 for (i = 0; i < i_colorcount(src); ++i) {
327 i_getcolors(src, i, &temp, 1);
329 for (ch = 0; ch < src->channels; ++ch) {
330 tempval += abs(want_back.channel[ch] - temp.channel[ch]);
332 if (tempval < minval) {
339 for (y = 0; y < ysize; ++y) {
340 for (x = 0; x < xsize; ++x) {
341 /* dividing by sz gives us the ability to do perspective
343 sz = x * matrix[6] + y * matrix[7] + matrix[8];
344 if (abs(sz) > 0.0000001) {
345 sx = (x * matrix[0] + y * matrix[1] + matrix[2]) / sz;
346 sy = (x * matrix[3] + y * matrix[4] + matrix[5]) / sz;
352 /* anything outside these ranges is either a broken co-ordinate
353 or outside the source */
354 if (abs(sz) > 0.0000001
355 && sx >= -0.5 && sx < src->xsize-0.5
356 && sy >= -0.5 && sy < src->ysize-0.5) {
358 /* all the world's an integer */
359 ix = (i_img_dim)(sx+0.5);
360 iy = (i_img_dim)(sy+0.5);
361 if (!i_gpal(src, ix, ix+1, iy, vals+x))
368 i_ppal(result, 0, xsize, y, vals);
376 i_img *i_matrix_transform(i_img *src, i_img_dim xsize, i_img_dim ysize, const double *matrix) {
377 return i_matrix_transform_bg(src, xsize, ysize, matrix, NULL, NULL);
381 i_matrix_mult(double *dest, const double *left, const double *right) {
385 for (i = 0; i < 3; ++i) {
386 for (j = 0; j < 3; ++j) {
388 for (k = 0; k < 3; ++k) {
389 accum += left[3*i+k] * right[3*k+j];
396 i_img *i_rotate_exact_bg(i_img *src, double amount,
397 const i_color *backp, const i_fcolor *fbackp) {
398 double xlate1[9] = { 0 };
400 double xlate2[9] = { 0 };
401 double temp[9], matrix[9];
402 i_img_dim x1, x2, y1, y2, newxsize, newysize;
404 /* first translate the centre of the image to (0,0) */
406 xlate1[2] = src->xsize/2.0;
408 xlate1[5] = src->ysize/2.0;
411 /* rotate around (0.0) */
412 rotate[0] = cos(amount);
413 rotate[1] = sin(amount);
415 rotate[3] = -rotate[1];
416 rotate[4] = rotate[0];
422 x1 = ceil(fabs(src->xsize * rotate[0] + src->ysize * rotate[1]));
423 x2 = ceil(fabs(src->xsize * rotate[0] - src->ysize * rotate[1]));
424 y1 = ceil(fabs(src->xsize * rotate[3] + src->ysize * rotate[4]));
425 y2 = ceil(fabs(src->xsize * rotate[3] - src->ysize * rotate[4]));
426 newxsize = x1 > x2 ? x1 : x2;
427 newysize = y1 > y2 ? y1 : y2;
428 /* translate the centre back to the center of the image */
430 xlate2[2] = -newxsize/2.0;
432 xlate2[5] = -newysize/2.0;
434 i_matrix_mult(temp, xlate1, rotate);
435 i_matrix_mult(matrix, temp, xlate2);
437 return i_matrix_transform_bg(src, newxsize, newysize, matrix, backp, fbackp);
440 i_img *i_rotate_exact(i_img *src, double amount) {
441 return i_rotate_exact_bg(src, amount, NULL, NULL);
450 Tony Cook <tony@develop-help.com>