-
-
Notifications
You must be signed in to change notification settings - Fork 649
/
rot.c
491 lines (401 loc) · 9.74 KB
/
rot.c
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* rotate by 0/90/180/270 degrees
*
* Copyright: 1991, N. Dessipris
* Written on: 28/10/91
* Updated on: 2/4/92, J.Cupitt
* bugs in im_la90rot fixed, now works for any type.
* 19/7/93 JC
* - IM_CODING_LABQ allowed now
* 15/11/94 JC
* - name changed
* - memory leaks fixed
* 8/2/95 JC
* - oops! memory allocation problem fixed
* 18/5/95 JC
* - IM_MAXLINES increased
* 13/8/96 JC
* - rewritten for partials
* 6/11/02 JC
* - speed-up ... replace memcpy() with a loop for small pixels
* 14/4/04
* - sets Xoffset / Yoffset
* 24/3/09
* - added IM_CODING_RAD support
* 1/2/10
* - cleanups
* - gtkdoc
* 4/11/11
* - rewrite as a class
* 7/3/17
* - added 90/180/270 convenience functions
* 10/11/22 alantudyk
* - swapped memcpy() in d180 for a loop
*/
/*
This file is part of VIPS.
VIPS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
/*
#define VIPS_DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n-lib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vips/vips.h>
#include <vips/internal.h>
#include <vips/debug.h>
#include "pconversion.h"
typedef struct _VipsRot {
VipsConversion parent_instance;
/* The input image.
*/
VipsImage *in;
/* Rotate by ...
*/
VipsAngle angle;
} VipsRot;
typedef VipsConversionClass VipsRotClass;
G_DEFINE_TYPE(VipsRot, vips_rot, VIPS_TYPE_CONVERSION);
static int
vips_rot90_gen(VipsRegion *out_region,
void *seq, void *a, void *b, gboolean *stop)
{
VipsRegion *ir = (VipsRegion *) seq;
VipsImage *in = (VipsImage *) a;
/* Output area.
*/
VipsRect *r = &out_region->valid;
int le = r->left;
int ri = VIPS_RECT_RIGHT(r);
int to = r->top;
int bo = VIPS_RECT_BOTTOM(r);
int x, y, i;
/* Pixel geometry.
*/
int ps, ls;
/* Find the area of the input image we need.
*/
VipsRect need;
need.left = to;
need.top = in->Ysize - ri;
need.width = r->height;
need.height = r->width;
if (vips_region_prepare(ir, &need))
return -1;
/* Find PEL size and line skip for ir.
*/
ps = VIPS_IMAGE_SIZEOF_PEL(in);
ls = VIPS_REGION_LSKIP(ir);
/* Rotate the bit we now have.
*/
for (y = to; y < bo; y++) {
/* Start of this output line.
*/
VipsPel *q = VIPS_REGION_ADDR(out_region, le, y);
/* Corresponding position in ir.
*/
VipsPel *p = VIPS_REGION_ADDR(ir,
need.left + y - to,
need.top + need.height - 1);
for (x = le; x < ri; x++) {
for (i = 0; i < ps; i++)
q[i] = p[i];
q += ps;
p -= ls;
}
}
return 0;
}
static int
vips_rot180_gen(VipsRegion *out_region,
void *seq, void *a, void *b, gboolean *stop)
{
VipsRegion *ir = (VipsRegion *) seq;
VipsImage *in = (VipsImage *) a;
/* Output area.
*/
VipsRect *r = &out_region->valid;
int le = r->left;
int ri = VIPS_RECT_RIGHT(r);
int to = r->top;
int bo = VIPS_RECT_BOTTOM(r);
int x, y, i;
/* Pixel geometry.
*/
int ps;
/* Find the area of the input image we need.
*/
VipsRect need;
need.left = in->Xsize - ri;
need.top = in->Ysize - bo;
need.width = r->width;
need.height = r->height;
if (vips_region_prepare(ir, &need))
return -1;
/* Find PEL size and line skip for ir.
*/
ps = VIPS_IMAGE_SIZEOF_PEL(in);
/* Rotate the bit we now have.
*/
for (y = to; y < bo; y++) {
/* Start of this output line.
*/
VipsPel *q = VIPS_REGION_ADDR(out_region, le, y);
/* Corresponding position in ir.
*/
VipsPel *p = VIPS_REGION_ADDR(ir,
need.left + need.width - 1,
need.top + need.height - (y - to) - 1);
/* Blap across!
*/
for (x = le; x < ri; x++) {
for (i = 0; i < ps; i++)
q[i] = p[i];
q += ps;
p -= ps;
}
}
return 0;
}
static int
vips_rot270_gen(VipsRegion *out_region,
void *seq, void *a, void *b, gboolean *stop)
{
VipsRegion *ir = (VipsRegion *) seq;
VipsImage *in = (VipsImage *) a;
/* Output area.
*/
VipsRect *r = &out_region->valid;
int le = r->left;
int ri = VIPS_RECT_RIGHT(r);
int to = r->top;
int bo = VIPS_RECT_BOTTOM(r);
int x, y, i;
/* Pixel geometry.
*/
int ps, ls;
/* Find the area of the input image we need.
*/
VipsRect need;
need.left = in->Xsize - bo;
need.top = le;
need.width = r->height;
need.height = r->width;
if (vips_region_prepare(ir, &need))
return -1;
/* Find PEL size and line skip for ir.
*/
ps = VIPS_IMAGE_SIZEOF_PEL(in);
ls = VIPS_REGION_LSKIP(ir);
/* Rotate the bit we now have.
*/
for (y = to; y < bo; y++) {
/* Start of this output line.
*/
VipsPel *q = VIPS_REGION_ADDR(out_region, le, y);
/* Corresponding position in ir.
*/
VipsPel *p = VIPS_REGION_ADDR(ir,
need.left + need.width - (y - to) - 1,
need.top);
for (x = le; x < ri; x++) {
for (i = 0; i < ps; i++)
q[i] = p[i];
q += ps;
p += ls;
}
}
return 0;
}
static int
vips_rot_build(VipsObject *object)
{
VipsConversion *conversion = VIPS_CONVERSION(object);
VipsRot *rot = (VipsRot *) object;
VipsGenerateFn generate_fn;
VipsDemandStyle hint;
if (VIPS_OBJECT_CLASS(vips_rot_parent_class)->build(object))
return -1;
if (rot->angle == VIPS_ANGLE_D0)
return vips_image_write(rot->in, conversion->out);
if (vips_image_pio_input(rot->in))
return -1;
hint = rot->angle == VIPS_ANGLE_D180
? VIPS_DEMAND_STYLE_THINSTRIP
: VIPS_DEMAND_STYLE_SMALLTILE;
if (vips_image_pipelinev(conversion->out, hint, rot->in, NULL))
return -1;
switch (rot->angle) {
case VIPS_ANGLE_D90:
generate_fn = vips_rot90_gen;
conversion->out->Xsize = rot->in->Ysize;
conversion->out->Ysize = rot->in->Xsize;
conversion->out->Xoffset = rot->in->Ysize;
conversion->out->Yoffset = 0;
break;
case VIPS_ANGLE_D180:
generate_fn = vips_rot180_gen;
conversion->out->Xoffset = rot->in->Xsize;
conversion->out->Yoffset = rot->in->Ysize;
break;
case VIPS_ANGLE_D270:
generate_fn = vips_rot270_gen;
conversion->out->Xsize = rot->in->Ysize;
conversion->out->Ysize = rot->in->Xsize;
conversion->out->Xoffset = 0;
conversion->out->Yoffset = rot->in->Xsize;
break;
default:
g_assert_not_reached();
/* Stop compiler warnings.
*/
generate_fn = NULL;
}
if (vips_image_generate(conversion->out,
vips_start_one, generate_fn, vips_stop_one,
rot->in, rot))
return -1;
return 0;
}
static void
vips_rot_class_init(VipsRotClass *class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(class);
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS(class);
VIPS_DEBUG_MSG("vips_rot_class_init\n");
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
vobject_class->nickname = "rot";
vobject_class->description = _("rotate an image");
vobject_class->build = vips_rot_build;
VIPS_ARG_IMAGE(class, "in", 1,
_("Input"),
_("Input image"),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET(VipsRot, in));
VIPS_ARG_ENUM(class, "angle", 6,
_("Angle"),
_("Angle to rotate image"),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET(VipsRot, angle),
VIPS_TYPE_ANGLE, VIPS_ANGLE_D90);
}
static void
vips_rot_init(VipsRot *rot)
{
}
static int
vips_rotv(VipsImage *in, VipsImage **out, VipsAngle angle, va_list ap)
{
return vips_call_split("rot", ap, in, out, angle);
}
/**
* vips_rot: (method)
* @in: input image
* @out: (out): output image
* @angle: rotation angle
* @...: %NULL-terminated list of optional named arguments
*
* Rotate @in by a multiple of 90 degrees.
*
* Use vips_similarity() to rotate by an arbitrary angle. vips_rot45() is
* useful for rotating convolution masks by 45 degrees.
*
* See also: vips_flip(), vips_similarity(), vips_rot45().
*
* Returns: 0 on success, -1 on error
*/
int
vips_rot(VipsImage *in, VipsImage **out, VipsAngle angle, ...)
{
va_list ap;
int result;
va_start(ap, angle);
result = vips_rotv(in, out, angle, ap);
va_end(ap);
return result;
}
/**
* vips_rot90:
* @in: input image
* @out: output image
* @...: %NULL-terminated list of optional named arguments
*
* Rotate @in by 90 degrees clockwise. A convenience function over vips_rot().
*
* See also: vips_rot().
*
* Returns: 0 on success, -1 on error
*/
int
vips_rot90(VipsImage *in, VipsImage **out, ...)
{
va_list ap;
int result;
va_start(ap, out);
result = vips_rotv(in, out, VIPS_ANGLE_D90, ap);
va_end(ap);
return result;
}
/**
* vips_rot180: (method)
* @in: input image
* @out: (out): output image
* @...: %NULL-terminated list of optional named arguments
*
* Rotate @in by 180 degrees. A convenience function over vips_rot().
*
* See also: vips_rot().
*
* Returns: 0 on success, -1 on error
*/
int
vips_rot180(VipsImage *in, VipsImage **out, ...)
{
va_list ap;
int result;
va_start(ap, out);
result = vips_rotv(in, out, VIPS_ANGLE_D180, ap);
va_end(ap);
return result;
}
/**
* vips_rot270: (method)
* @in: input image
* @out: (out): output image
* @...: %NULL-terminated list of optional named arguments
*
* Rotate @in by 270 degrees clockwise. A convenience function over vips_rot().
*
* See also: vips_rot().
*
* Returns: 0 on success, -1 on error
*/
int
vips_rot270(VipsImage *in, VipsImage **out, ...)
{
va_list ap;
int result;
va_start(ap, out);
result = vips_rotv(in, out, VIPS_ANGLE_D270, ap);
va_end(ap);
return result;
}