File: rotmask.c

package info (click to toggle)
vips 8.14.1-3%2Bdeb12u2
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 35,912 kB
  • sloc: ansic: 165,449; cpp: 10,987; python: 4,462; xml: 4,212; sh: 471; perl: 40; makefile: 23
file content (155 lines) | stat: -rw-r--r-- 3,501 bytes parent folder | download
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
/* Functions to create offsets for rotating square masks.
 *
 * Author: N. Dessipris (Copyright, N. Dessipris 1991)
 * Written on: 08/05/1991
 * Modified on: 28/05/1991
 * 12/10/95 JC
 *	- small revisions, needs rewriting really
 * 7/8/96 JC
 *	- absolutely foul desp code revised
 *	- many bugs and mem leaks fixed
 * 1/3/99 JC
 *	- oops, fns were not preserving scale and offset
 * 1/12/10
 * 	- allow any size mask for the 90 degree rotates by using im_rot90().
 */

/*

    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 PIM_RINT 1
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n-lib.h>

#include <stdio.h>
#include <string.h>
#include <math.h>

#include <vips/vips.h>
#include <vips/vips7compat.h>

/* The type of the vips operations we support.
 */
typedef int (*vips_fn)( IMAGE *in, IMAGE *out );

/* Pass a mask through a vips operation, eg. im_rot90().
 */
static INTMASK *
vapplyimask( INTMASK *in, const char *name, vips_fn fn )
{
	IMAGE *x;
	IMAGE *t[2];
	DOUBLEMASK *d[2];
	INTMASK *out;

	if( !(x = im_open( name, "p" )) )
		return( NULL );
	if( !(d[0] = im_local_dmask( x, im_imask2dmask( in, name ) )) ||
		im_open_local_array( x, t, 2, name, "p" ) ||
		im_mask2vips( d[0], t[0] ) ||
		fn( t[0], t[1] ) ||
		!(d[1] = im_local_dmask( x, im_vips2mask( t[1], name ) )) ||
		!(out = im_dmask2imask( d[1], name )) ) {
		im_close( x );
		return( NULL );
	}
	im_close( x );

	out->scale = in->scale;
	out->offset = in->offset;

	return( out );
}

static DOUBLEMASK *
vapplydmask( DOUBLEMASK *in, const char *name, vips_fn fn )
{
	IMAGE *x;
	IMAGE *t[2];
	DOUBLEMASK *out;

	if( !(x = im_open( name, "p" )) )
		return( NULL );
	if( im_open_local_array( x, t, 2, name, "p" ) ||
		im_mask2vips( in, t[0] ) ||
		fn( t[0], t[1] ) ||
		!(out = im_vips2mask( t[1], name )) ) {
		im_close( x );
		return( NULL );
	}
	im_close( x );

	out->scale = in->scale;
	out->offset = in->offset;

	return( out );
}

INTMASK *
im_rotate_imask90( INTMASK *in, const char *filename )
{
	return( vapplyimask( in, filename, im_rot90 ) );
}

DOUBLEMASK *
im_rotate_dmask90( DOUBLEMASK *in, const char *filename )
{
	return( vapplydmask( in, filename, im_rot90 ) );
}

static int 
im_rot45( IMAGE *in, IMAGE *out )
{
	VipsImage *t;

	if( vips_rot45( in, &t, NULL ) )
		return( -1 );
	if( vips_image_write( t, out ) ) {
		g_object_unref( t );
		return( -1 );
	}
	g_object_unref( t );

	return( 0 );
}

INTMASK *
im_rotate_imask45( INTMASK *in, const char *filename )
{
	return( vapplyimask( in, filename, im_rot45 ) );
}

DOUBLEMASK *
im_rotate_dmask45( DOUBLEMASK *in, const char *filename )
{
	return( vapplydmask( in, filename, im_rot45 ) );
}