File: cairo-rotate.c

package info (click to toggle)
gthumb 3%3A3.11.2-0.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 29,364 kB
  • sloc: ansic: 145,227; cpp: 1,266; xml: 783; python: 369; yacc: 354; lex: 210; makefile: 76; sh: 46
file content (411 lines) | stat: -rw-r--r-- 11,040 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 *  GThumb
 *
 *  Copyright (C) 2011 The Free Software Foundation, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <math.h>
#include <gthumb.h>
#include "cairo-rotate.h"


void
_cairo_image_surface_rotate_get_cropping_parameters (cairo_surface_t *image,
						     double           angle,
						     double          *p1_plus_p2,
						     double          *p_min)
{
	double angle_rad;
	double cos_angle, sin_angle;
	double src_width, src_height;
	double t1, t2;

	if (angle < -90)
		angle += 180;
	else if (angle > 90)
		angle -= 180;

	angle_rad = fabs (angle) / 180.0 * G_PI;

	cos_angle = cos (angle_rad);
	sin_angle = sin (angle_rad);

	src_width  = cairo_image_surface_get_width  (image) - 1.0;
	src_height = cairo_image_surface_get_height (image) - 1.0;

	if (src_width > src_height) {
		t1 = cos_angle * src_width - sin_angle * src_height;
		t2 = sin_angle * src_width + cos_angle * src_height;

		*p1_plus_p2  = 1.0 + (t1 * src_height) / (t2 * src_width);

		*p_min = src_height / src_width * sin_angle * cos_angle + (*p1_plus_p2 - 1) * cos_angle * cos_angle;
	}
	else {
		t1 = cos_angle * src_height - sin_angle * src_width;
		t2 = sin_angle * src_height + cos_angle * src_width;

		*p1_plus_p2 = 1.0 + (t1 * src_width)  / (t2 * src_height);

		*p_min = src_width / src_height * sin_angle * cos_angle + (*p1_plus_p2 - 1) * cos_angle * cos_angle;
	}
}


void
_cairo_image_surface_rotate_get_cropping_region (cairo_surface_t       *image,
						 double                 angle,
						 double                 p1,
						 double                 p2,
						 cairo_rectangle_int_t *region)
{
	double angle_rad;
	double cos_angle, sin_angle;
	double src_width, src_height;
	double new_width;
	double xx1, yy1, xx2, yy2;

	if (angle < -90)
		angle += 180;
	else if (angle > 90)
		angle -= 180;

	p1    = CLAMP (p1,      0.0,  1.0);
	p2    = CLAMP (p2,      0.0,  1.0);

	angle_rad = fabs (angle) / 180.0 * G_PI;
	cos_angle = cos (angle_rad);
	sin_angle = sin (angle_rad);

	src_width  = cairo_image_surface_get_width  (image) - 1.0;
	src_height = cairo_image_surface_get_height (image) - 1.0;

	if (angle < 0) {

		/* This is to make the p1/p2 sliders behavour more user friendly */

		double t;

		t = p1;
		p1 = p2;
		p2 = t;
	}

	if (src_width > src_height) {
		xx1 = p1 * src_width * cos_angle + src_height * sin_angle;
		yy1 = p1 * src_width * sin_angle;
		xx2 = (1 - p2) * src_width * cos_angle;
		yy2 = (1 - p2) * src_width * sin_angle + src_height * cos_angle;
	}
	else {
		xx1 = p1       * src_height * sin_angle;
		yy1 = (1 - p1) * src_height * cos_angle;
		xx2 = (1 - p2) * src_height * sin_angle + src_width * cos_angle;
		yy2 = p2       * src_height * cos_angle + src_width * sin_angle;
	}

	if (angle < 0) {
		new_width = (cos_angle * src_width) + (sin_angle * src_height);
		xx1 = new_width - xx1;
		xx2 = new_width - xx2;
	}

	region->x = GDOUBLE_ROUND_TO_INT (MIN (xx1, xx2));
	region->y = GDOUBLE_ROUND_TO_INT (MIN (yy1, yy2));

	region->width  = GDOUBLE_ROUND_TO_INT (MAX (xx1, xx2)) - region->x + 1;
	region->height = GDOUBLE_ROUND_TO_INT (MAX (yy1, yy2)) - region->y + 1;
}


double
_cairo_image_surface_rotate_get_align_angle (gboolean  vertical,
					     GdkPoint *p1,
					     GdkPoint *p2)
{
	double angle;

	if (! vertical) {
		if (p1->y == p2->y)
			return 0.0;

		if (p2->x > p1->x)
			angle = -atan2 (p2->y - p1->y, p2->x - p1->x);
		else
			angle = -atan2 (p1->y - p2->y, p1->x - p2->x);
	}
	else {
		if (p1->x == p2->x)
			return 0.0;

		if (p2->y > p1->y)
			angle = atan2 (p2->x - p1->x, p2->y - p1->y);
		else
			angle = atan2 (p1->x - p2->x, p1->y - p2->y);
	}

	angle = angle * 180.0 / G_PI;
	angle = GDOUBLE_ROUND_TO_INT (angle * 10.0) / 10.0;

	return angle;
}


static cairo_surface_t*
rotate (cairo_surface_t *image,
	double           angle,
	gboolean         high_quality,
	guchar           r0,
	guchar           g0,
	guchar           b0,
	guchar           a0,
	GthAsyncTask    *task)
{
	cairo_surface_t *image_with_background;
	cairo_surface_t *rotated;
	double           angle_rad;
	double           cos_angle, sin_angle;
	double           src_width, src_height;
	int              new_width, new_height;
	int              src_rowstride, new_rowstride;
	int              xi, yi;
	double           x, y;
	double           x2, y2;
	int              x2min, y2min;
	int              x2max, y2max;
	double           fx, fy;
	guchar          *p_src, *p_new;
	guchar          *p_src2, *p_new2;
	guchar           r00, r01, r10, r11;
	guchar           g00, g01, g10, g11;
	guchar           b00, b01, b10, b11;
	guchar           a00, a01, a10, a11;
	double           half_new_width;
	double           half_new_height;
	double           half_src_width;
	double           half_src_height;
	int              tmp;
	guchar           r, g, b, a;
	guint32          pixel;

	angle = CLAMP (angle, -90.0, 90.0);
	angle_rad = angle / 180.0 * G_PI;
	cos_angle = cos (angle_rad);
	sin_angle = sin (angle_rad);
	src_width  = cairo_image_surface_get_width  (image);
	src_height = cairo_image_surface_get_height (image);
	new_width  = GDOUBLE_ROUND_TO_INT (      cos_angle  * src_width + fabs(sin_angle) * src_height);
	new_height = GDOUBLE_ROUND_TO_INT (fabs (sin_angle) * src_width +      cos_angle  * src_height);

	if (a0 == 0xff) {
		/* pre-multiply the background color */

		image_with_background = _cairo_image_surface_copy (image);
		p_src = _cairo_image_surface_flush_and_get_data (image);
		p_new = _cairo_image_surface_flush_and_get_data (image_with_background);
		src_rowstride = cairo_image_surface_get_stride (image);
		new_rowstride = cairo_image_surface_get_stride (image_with_background);

		cairo_surface_flush (image_with_background);
		for (yi = 0; yi < src_height; yi++) {
			p_src2 = p_src;
			p_new2 = p_new;
			for (xi = 0; xi < src_width; xi++) {
				a = p_src2[CAIRO_ALPHA];
				r = p_src2[CAIRO_RED] + _cairo_multiply_alpha (r0, 0xff - a);
				g = p_src2[CAIRO_GREEN] + _cairo_multiply_alpha (g0, 0xff - a);
				b = p_src2[CAIRO_BLUE] + _cairo_multiply_alpha (b0, 0xff - a);
				pixel = CAIRO_RGBA_TO_UINT32 (r, g, b, 0xff);
				memcpy (p_new2, &pixel, sizeof (guint32));

				p_new2 += 4;
				p_src2 += 4;
			}
			p_src += src_rowstride;
			p_new += new_rowstride;
		}
		cairo_surface_mark_dirty (image_with_background);
	}
	else
		image_with_background = cairo_surface_reference (image);

	/* create the rotated image */

	rotated = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, new_width, new_height);

	p_src = _cairo_image_surface_flush_and_get_data (image_with_background);
	p_new = _cairo_image_surface_flush_and_get_data (rotated);
	src_rowstride = cairo_image_surface_get_stride (image_with_background);
	new_rowstride = cairo_image_surface_get_stride (rotated);

/*
 * bilinear interpolation
 *            fx
 *    v00------------v01
 *    |        |      |
 * fy |--------v      |
 *    |               |
 *    |               |
 *    |               |
 *    v10------------v11
 */
#define INTERPOLATE(v, v00, v01, v10, v11, fx, fy) \
	tmp = (1.0 - (fy)) * \
	      ((1.0 - (fx)) * (v00) + (fx) * (v01)) \
              + \
              (fy) * \
              ((1.0 - (fx)) * (v10) + (fx) * (v11)); \
	v = CLAMP (tmp, 0, 255);

#define GET_VALUES(r, g, b, a, x, y) \
	if (x >= 0 && x < src_width && y >= 0 && y < src_height) { \
		p_src2 = p_src + src_rowstride * y + 4 * x; \
		r = p_src2[CAIRO_RED]; \
		g = p_src2[CAIRO_GREEN]; \
		b = p_src2[CAIRO_BLUE]; \
		a = p_src2[CAIRO_ALPHA]; \
	} \
	else { \
		r = r0; \
		g = g0; \
		b = b0; \
		a = a0; \
	}

	half_new_width = new_width / 2.0;
	half_new_height = new_height / 2.0;
	half_src_width = src_width / 2.0;
	half_src_height = src_height / 2.0;

	cairo_surface_flush (rotated);

	y = - half_new_height;
	for (yi = 0; yi < new_height; yi++) {
		if (task != NULL) {
			gboolean cancelled;
			double   progress;

			gth_async_task_get_data (task, NULL, &cancelled, NULL);
			if (cancelled)
				goto out;

			progress = (double) yi / new_height;
			gth_async_task_set_data (task, NULL, NULL, &progress);
		}

		p_new2 = p_new;

		x = - half_new_width;
		for (xi = 0; xi < new_width; xi++) {
			x2 = cos_angle * x - sin_angle * y + half_src_width;
			y2 = sin_angle * x + cos_angle * y + half_src_height;

			if (high_quality) {
				/* Bilinear interpolation. */

				x2min = (int) x2;
				y2min = (int) y2;
				x2max = x2min + 1;
				y2max = y2min + 1;

				GET_VALUES (r00, g00, b00, a00, x2min, y2min);
				GET_VALUES (r01, g01, b01, a01, x2max, y2min);
				GET_VALUES (r10, g10, b10, a10, x2min, y2max);
				GET_VALUES (r11, g11, b11, a11, x2max, y2max);

				fx = x2 - x2min;
				fy = y2 - y2min;

				INTERPOLATE (r, r00, r01, r10, r11, fx, fy);
				INTERPOLATE (g, g00, g01, g10, g11, fx, fy);
				INTERPOLATE (b, b00, b01, b10, b11, fx, fy);
				INTERPOLATE (a, a00, a01, a10, a11, fx, fy);

				pixel = CAIRO_RGBA_TO_UINT32 (r, g, b, a);
				memcpy (p_new2, &pixel, sizeof (guint32));
			}
			else {
				/* Nearest neighbor */

				x2min = GDOUBLE_ROUND_TO_INT (x2);
				y2min = GDOUBLE_ROUND_TO_INT (y2);

				GET_VALUES (p_new2[CAIRO_RED],
					    p_new2[CAIRO_GREEN],
					    p_new2[CAIRO_BLUE],
					    p_new2[CAIRO_ALPHA],
					    x2min,
					    y2min);
			}

			p_new2 += 4;
			x += 1.0;
		}

		p_new += new_rowstride;
		y += 1.0;
	}

	out:

	cairo_surface_mark_dirty (rotated);
	cairo_surface_destroy (image_with_background);

#undef INTERPOLATE
#undef GET_VALUES

	return rotated;
}


cairo_surface_t *
_cairo_image_surface_rotate (cairo_surface_t *image,
		    	     double           angle,
		    	     gboolean         high_quality,
		    	     GdkRGBA         *background_color,
		    	     GthAsyncTask    *task)
{
	cairo_surface_t *rotated;
	cairo_surface_t *tmp = NULL;

	if (angle >= 90.0) {
		image = tmp = _cairo_image_surface_transform (image, GTH_TRANSFORM_ROTATE_90);
		angle -= 90.0;
	}
	else if (angle <= -90.0) {
		image = tmp = _cairo_image_surface_transform (image, GTH_TRANSFORM_ROTATE_270);
		angle += 90.0;
	}

	if (angle != 0.0)
		rotated = rotate (image,
				  -angle,
				  high_quality,
				  background_color->red * 255.0,
				  background_color->green * 255.0,
				  background_color->blue * 255.0,
				  background_color->alpha * 255.0,
				  task);
	else
		rotated = cairo_surface_reference (image);

	if (tmp != NULL)
		cairo_surface_destroy (tmp);

	return rotated;
}