aboutsummaryrefslogtreecommitdiffstats
path: root/transform.c
blob: d58e4845f587408e29c477d7f53bc84b177ada42 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "common.h"

enum transformation trans = TRANS_NONE;

enum transformation
multiply_transformations(enum transformation augment, enum transformation augend)
{
	enum transformation ret = augment;
	if (augment & TRANS_TRANSPOSE)
		ret -= augend;
	else
		ret += augend;
	return (ret & 3u) | ((augment ^ augend) & ~3u);
}

static GdkPixbuf *
make_negative_raster(GdkPixbuf *raster)
{
	GdkPixbuf *new_raster;
	uint8_t *pixels;
	int width, height, rowstride, channels, x, y;

	new_raster = gdk_pixbuf_copy(raster);
	if (!new_raster)
		exit(1);

	pixels    = gdk_pixbuf_get_pixels(new_raster);
	width     = gdk_pixbuf_get_width(new_raster);
	height    = gdk_pixbuf_get_height(new_raster);
	rowstride = gdk_pixbuf_get_rowstride(new_raster);
	channels  = gdk_pixbuf_get_n_channels(new_raster);

	for (y = 0; y < height; y++) {
		for (x = 0; x < width * channels; x += channels) {
			/* TODO
			 *    use libcolour to convert to CIE LChuv
			 *    (or CIE LChab which isn't supported yet),
			 *    then invert the lightness and the hue
			 *    and then convert back to sRGB; however
			 *    we must account for the fact that some
			 *    pixels may be out of gamut
			 *
			 * TODO
			 *    this may be a bit slow, so we to use
			 *    opencl if available and otherwise use
			 *    libcolour_convert_en_masse as much as
			 *    possible and cache the negative for
			 *    each file
			 *
			 * TODO
			 *    it shall be a configuration in Prefernces
			 *    what model is used for colour inversion
			 */
			pixels[x + 0] = 255 - pixels[x + 0];
			pixels[x + 1] = 255 - pixels[x + 1];
			pixels[x + 2] = 255 - pixels[x + 2];
		}
		pixels = &pixels[rowstride];
	}

	return new_raster;
}

static GdkPixbuf *
apply_rotation_and_reflection(enum transformation t, GdkPixbuf *raster)
{
	GdkPixbuf *new_raster, *newer_raster;

	switch (t & 7u) {
	case TRANS_NONE:
		return NULL;
	case TRANS_CLOCKWISE:
		new_raster = gdk_pixbuf_rotate_simple(raster, GDK_PIXBUF_ROTATE_CLOCKWISE);
		break;
	case TRANS_180_DEGREES:
		new_raster = gdk_pixbuf_rotate_simple(raster, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
		break;
	case TRANS_ANTICLOCKWISE:
		new_raster = gdk_pixbuf_rotate_simple(raster, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
		break;
	case TRANS_FLIP:
	case TRANS_TRANSPOSE:
		new_raster = gdk_pixbuf_flip(raster, 0);
		break;
	case TRANS_FLOP:
	case TRANS_ANTITRANSPOSE:
		new_raster = gdk_pixbuf_flip(raster, 1);
		break;
	}
	if (!new_raster)
		exit(1);

	switch (t & 7u) {
	case TRANS_TRANSPOSE:
	case TRANS_ANTITRANSPOSE:
		newer_raster = gdk_pixbuf_rotate_simple(new_raster, GDK_PIXBUF_ROTATE_CLOCKWISE);
		if (!newer_raster)
			exit(1);
		g_object_unref(new_raster);
		return newer_raster;
	default:
		return new_raster;
	}
}

GdkPixbuf *
transform_raster(GdkPixbuf *raster, enum transformation t)
{
	GdkPixbuf *rotated;
	if (t & TRANS_NEGATIVE)
		raster = make_negative_raster(raster);
	else
		g_object_ref(raster);
	rotated = apply_rotation_and_reflection(t, raster);
	if (!rotated)
		return raster;
	g_object_unref(raster);
	return rotated;
}

int
apply_transformations(struct image *image, struct image *image_out)
{
	enum transformation t;

	t = apply_embedded_orientation ? multiply_transformations(trans, image->embedded_transformation) : trans;

	if (t == TRANS_NONE)
		return 0;

	if (image->raster)
		image_out->raster = transform_raster(image->raster, t);

	image_out->vector = image->vector;

	if (swaps_dimensions(t)) {
		image_out->width = image->height;
		image_out->height = image->width;
		image_out->has_x_dpi = image->has_y_dpi;
		image_out->has_y_dpi = image->has_x_dpi;
		image_out->x_dpi = image->y_dpi;
		image_out->y_dpi = image->x_dpi;
	} else {
		image_out->width = image->width;
		image_out->height = image->height;
		image_out->has_x_dpi = image->has_x_dpi;
		image_out->has_y_dpi = image->has_y_dpi;
		image_out->x_dpi = image->x_dpi;
		image_out->y_dpi = image->y_dpi;
	}
	image_out->embedded_transformation = image->embedded_transformation;
	image_out->has_transformer = get_affine_matrix(&image_out->transformer, t, image->width, image->height);

	return 1;
}

enum transformation
inverse_transformation(enum transformation t)
{
	return ((4u - t) & 3u) | (t & ~3u);
}

enum transformation
reset_transformations_without_redraw(void)
{
	enum transformation augment = inverse_transformation(trans & 7u);
	trans &= ~7u;
	return augment;
}

int
add_transformation_without_redraw(enum transformation augment)
{
	enum transformation old = trans;
	trans = multiply_transformations(augment, trans);
	return trans != old;
}

int
swaps_dimensions(enum transformation t)
{
	switch (t & 7u) {
	case TRANS_NONE:
	case TRANS_180_DEGREES:
	case TRANS_FLIP:
	case TRANS_FLOP:
		return 0;
	case TRANS_CLOCKWISE:
	case TRANS_ANTICLOCKWISE:
	case TRANS_TRANSPOSE:
	case TRANS_ANTITRANSPOSE:
		return 1;
	}
	abort();
}

void
transform_point(enum transformation t, double *x, double *y, double *w, double *h)
{
	double x1 = *x, y1 = *y, x2 = *w, y2 = *h;
	switch (t & 7u) {
	case TRANS_NONE:
		break;
	case TRANS_CLOCKWISE:
		*x = y2 - y1;
		*y = x1;
		break;
	case TRANS_180_DEGREES:
		*x = x2 - x1;
		*y = y2 - y1;
		break;
	case TRANS_ANTICLOCKWISE:
		*x = y1;
		*y = x2 - x1;
		break;
	case TRANS_TRANSPOSE:
		*x = y1;
		*y = x1;
		break;
	case TRANS_FLIP:
		*y = y2 - y1;
		break;
	case TRANS_ANTITRANSPOSE:
		*x = y2 - y1;
		*y = x2 - x1;
		break;
	case TRANS_FLOP:
		*x = x2 - x1;
		break;
	}
	if (swaps_dimensions(t)) {
		*w = y2;
		*h = x2;
	}
}

int
get_affine_matrix(cairo_matrix_t *matrix_out, enum transformation t, int width, int height)
{
	double w = (double)width;
	double h = (double)height;
	switch (t & 7u) {
	case TRANS_NONE:           cairo_matrix_init(matrix_out, +1.0,  0.0,  0.0, +1.0, 0.0, 0.0);  return 0;
	case TRANS_CLOCKWISE:      cairo_matrix_init(matrix_out,  0.0, +1.0, -1.0,  0.0, h,   0.0);  break;
	case TRANS_180_DEGREES:    cairo_matrix_init(matrix_out, -1.0,  0.0,  0.0, -1.0, w,   h);    break;
	case TRANS_ANTICLOCKWISE:  cairo_matrix_init(matrix_out,  0.0, -1.0, +1.0,  0.0, 0.0, w);    break;
	case TRANS_TRANSPOSE:      cairo_matrix_init(matrix_out,  0.0, +1.0, +1.0,  0.0, 0.0, 0.0);  break;
	case TRANS_FLOP:           cairo_matrix_init(matrix_out, -1.0,  0.0,  0.0, +1.0, w,   0.0);  break;
	case TRANS_ANTITRANSPOSE:  cairo_matrix_init(matrix_out,  0.0, -1.0, -1.0,  0.0, h,   w);    break;
	case TRANS_FLIP:           cairo_matrix_init(matrix_out, +1.0,  0.0,  0.0, -1.0, 0.0, h);    break;
	}
	return 1;
}

enum transformation
orientation_to_transformation(int orientation)
{
	switch (orientation) {
#define X(I, E, T) case I: return E
	LIST_ORIENTATIONS(X, ;);
	default:
		return TRANS_NONE;
#undef X
	}
}