File: transpose3d.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 (226 lines) | stat: -rw-r--r-- 5,344 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
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
/* vips_transpose3d
 *
 * 30/4/18
 * 	- from grid.c 
 */

/*

    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

 */

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

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

#include <vips/vips.h>

#include "pconversion.h"

typedef struct _VipsTranspose3d {
	VipsConversion parent_instance;

	VipsImage *in;

	int page_height;

} VipsTranspose3d;

typedef VipsConversionClass VipsTranspose3dClass;

G_DEFINE_TYPE( VipsTranspose3d, vips_transpose3d, VIPS_TYPE_CONVERSION );

static int
vips_transpose3d_gen( VipsRegion *or, void *vseq, void *a, void *b,
	gboolean *stop )
{
	VipsRegion *ir = (VipsRegion *) vseq;
	VipsImage *in = (VipsImage *) a;
	VipsTranspose3d *transpose3d = (VipsTranspose3d *) b;
	VipsRect *r = &or->valid;

	int output_page_height = in->Ysize / transpose3d->page_height;

	int y;
	VipsRect tile;
	
	tile = *r;
	tile.height = 1;

	for( y = 0; y < r->height; y++ ) { 
		/* y in output.
		 */
		int yo = r->top + y;

		/* On output page.
		 */
		int yop = yo / output_page_height;

		/* Line on output page.
		 */
		int yol = yo % output_page_height;

		/* y of input page.
		 */
		int yip = yol * transpose3d->page_height;

		/* y of input line.
		 */
		int yi = yip + yop;

		tile.top = yi;

		/* Render into or.
		 */
		if( vips_region_prepare_to( ir, or, &tile, tile.left, yo ) )
			return( -1 );
	}

	return( 0 );
}

static int
vips_transpose3d_build( VipsObject *object )
{
	VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
	VipsConversion *conversion = VIPS_CONVERSION( object );
	VipsTranspose3d *transpose3d = (VipsTranspose3d *) object;

	VipsImage *in;

	if( VIPS_OBJECT_CLASS( vips_transpose3d_parent_class )->
		build( object ) )
		return( -1 );

	in = transpose3d->in;

	if( vips_check_coding_known( class->nickname, in ) ||
		vips_image_pio_input( in ) )
		return( -1 );

	if( !vips_object_argument_isset( object, "page_height" ) ) {
		if( vips_image_get_int( in, 
			VIPS_META_PAGE_HEIGHT, &transpose3d->page_height ) ) 
			return( -1 );
	}

	if( transpose3d->page_height <= 0 ||
		in->Ysize % transpose3d->page_height != 0 )  {
		vips_error( class->nickname, "%s", _( "bad page_height" ) );
		return( -1 );
	}

	if( vips_image_pipelinev( conversion->out, 
		VIPS_DEMAND_STYLE_SMALLTILE, in, NULL ) )
		return( -1 );
	vips_image_set_int( conversion->out, 
		VIPS_META_PAGE_HEIGHT, in->Ysize / transpose3d->page_height );

	if( vips_image_generate( conversion->out,
		vips_start_one, vips_transpose3d_gen, vips_stop_one, 
		in, transpose3d ) )
		return( -1 );

	return( 0 );
}

static void
vips_transpose3d_class_init( VipsTranspose3dClass *class )
{
	GObjectClass *gobject_class = G_OBJECT_CLASS( class );
	VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );

	gobject_class->set_property = vips_object_set_property;
	gobject_class->get_property = vips_object_get_property;

	vobject_class->nickname = "transpose3d";
	vobject_class->description = _( "transpose3d an image" );
	vobject_class->build = vips_transpose3d_build;

	VIPS_ARG_IMAGE( class, "in", 1, 
		_( "Input" ), 
		_( "Input image" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsTranspose3d, in ) );

	VIPS_ARG_INT( class, "page_height", 3, 
		_( "Page height" ), 
		_( "Height of each input page" ),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET( VipsTranspose3d, page_height ),
		0, 10000000, 0 );

}

static void
vips_transpose3d_init( VipsTranspose3d *transpose3d )
{
}

/**
 * vips_transpose3d: (method)
 * @in: input image
 * @out: (out): output image
 * @...: %NULL-terminated list of optional named arguments
 *
 * Optional arguments:
 *
 * * @page_height: %gint, size of each input page
 *
 * Transpose a volumetric image. 
 *
 * Volumetric images are very tall, thin images, with the metadata item
 * #VIPS_META_PAGE_HEIGHT set to the height of each sub-image. 
 *
 * This operation swaps the two major dimensions, so that page N in the
 * output contains the Nth scanline, in order, from each input page.
 *
 * You can override the #VIPS_META_PAGE_HEIGHT metadata item with the optional
 * @page_height parameter. 
 *
 * #VIPS_META_PAGE_HEIGHT in the output image is the number of pages in the
 * input image. 
 *
 * See also: vips_grid().
 *
 * Returns: 0 on success, -1 on error
 */
int
vips_transpose3d( VipsImage *in, VipsImage **out, ... )
{
	va_list ap;
	int result;

	va_start( ap, out );
	result = vips_call_split( "transpose3d", ap, in, out );
	va_end( ap );

	return( result );
}