2010-04-06 11:02:26 +02:00
|
|
|
/* A sink that's not attached to anything, eg. find image average,
|
|
|
|
*
|
|
|
|
* 28/3/10
|
|
|
|
* - from im_iterate(), reworked for threadpool
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
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
|
2013-03-07 06:40:19 +01:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
02110-1301 USA
|
2010-04-06 11:02:26 +02:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2010-04-09 18:51:45 +02:00
|
|
|
#define VIPS_DEBUG
|
2010-04-18 00:02:34 +02:00
|
|
|
*/
|
2010-04-06 11:02:26 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif /*HAVE_CONFIG_H*/
|
2022-02-27 16:27:28 +01:00
|
|
|
#include <glib/gi18n-lib.h>
|
2010-04-06 11:02:26 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <vips/vips.h>
|
|
|
|
#include <vips/thread.h>
|
|
|
|
#include <vips/internal.h>
|
|
|
|
#include <vips/debug.h>
|
|
|
|
|
2011-03-28 15:08:42 +02:00
|
|
|
#include "sink.h"
|
|
|
|
|
2018-01-31 17:25:06 +01:00
|
|
|
/* A part of the image we are scanning.
|
|
|
|
*
|
|
|
|
* We can't let any threads fall too far behind as that would mess up seq
|
|
|
|
* image sources. Keep track of two areas moving down the image, and stall if
|
|
|
|
* the previous area still has active threads.
|
|
|
|
*/
|
|
|
|
typedef struct _SinkArea {
|
|
|
|
struct _Sink *sink;
|
|
|
|
|
|
|
|
VipsRect rect; /* Part of image this area covers */
|
|
|
|
VipsSemaphore n_thread; /* Number of threads scanning this area */
|
|
|
|
} SinkArea;
|
|
|
|
|
2010-04-06 11:02:26 +02:00
|
|
|
/* Per-call state.
|
|
|
|
*/
|
|
|
|
typedef struct _Sink {
|
2011-03-28 15:08:42 +02:00
|
|
|
SinkBase sink_base;
|
2010-04-06 11:02:26 +02:00
|
|
|
|
|
|
|
/* We need a temp "p" image between the source image and us to
|
|
|
|
* make sure we can't damage the original.
|
|
|
|
*/
|
|
|
|
VipsImage *t;
|
|
|
|
|
2020-02-03 14:54:01 +01:00
|
|
|
/* Mutex for serialising calls to VipsStartFn and VipsStopFn.
|
|
|
|
*/
|
|
|
|
GMutex *sslock;
|
|
|
|
|
2010-04-09 18:51:45 +02:00
|
|
|
/* Call params.
|
2010-04-06 11:02:26 +02:00
|
|
|
*/
|
2011-11-02 10:22:30 +01:00
|
|
|
VipsStartFn start_fn;
|
|
|
|
VipsGenerateFn generate_fn;
|
|
|
|
VipsStopFn stop_fn;
|
2010-04-06 11:02:26 +02:00
|
|
|
void *a;
|
|
|
|
void *b;
|
2018-01-31 17:25:06 +01:00
|
|
|
|
|
|
|
/* We are current scanning area, we'll delay starting a new
|
|
|
|
* area if old_area (the previous position) hasn't completed.
|
|
|
|
*/
|
|
|
|
SinkArea *area;
|
|
|
|
SinkArea *old_area;
|
|
|
|
|
2010-04-06 11:02:26 +02:00
|
|
|
} Sink;
|
|
|
|
|
2010-04-09 18:51:45 +02:00
|
|
|
/* Our per-thread state.
|
|
|
|
*/
|
|
|
|
typedef struct _SinkThreadState {
|
|
|
|
VipsThreadState parent_object;
|
|
|
|
|
|
|
|
/* Sequence value for this thread.
|
|
|
|
*/
|
|
|
|
void *seq;
|
|
|
|
|
|
|
|
/* The region we walk over sink.t copy. We can't use
|
|
|
|
* parent_object.reg, it's defined on the outer image.
|
|
|
|
*/
|
2011-03-03 15:49:12 +01:00
|
|
|
VipsRegion *reg;
|
2018-01-31 17:25:06 +01:00
|
|
|
|
|
|
|
/* The area we were allocated from.
|
|
|
|
*/
|
|
|
|
SinkArea *area;
|
|
|
|
|
2010-04-09 18:51:45 +02:00
|
|
|
} SinkThreadState;
|
|
|
|
|
|
|
|
typedef struct _SinkThreadStateClass {
|
|
|
|
VipsThreadStateClass parent_class;
|
|
|
|
|
|
|
|
} SinkThreadStateClass;
|
|
|
|
|
|
|
|
G_DEFINE_TYPE( SinkThreadState, sink_thread_state, VIPS_TYPE_THREAD_STATE );
|
2010-04-06 11:02:26 +02:00
|
|
|
|
2018-01-31 17:25:06 +01:00
|
|
|
static void
|
|
|
|
sink_area_free( SinkArea *area )
|
|
|
|
{
|
|
|
|
vips_semaphore_destroy( &area->n_thread );
|
2020-06-30 12:10:30 +02:00
|
|
|
g_free( area );
|
2018-01-31 17:25:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static SinkArea *
|
|
|
|
sink_area_new( Sink *sink )
|
|
|
|
{
|
|
|
|
SinkArea *area;
|
|
|
|
|
|
|
|
if( !(area = VIPS_NEW( NULL, SinkArea )) )
|
|
|
|
return( NULL );
|
|
|
|
area->sink = sink;
|
|
|
|
vips_semaphore_init( &area->n_thread, 0, "n_thread" );
|
|
|
|
|
|
|
|
return( area );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move an area to a position.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
sink_area_position( SinkArea *area, int top, int height )
|
|
|
|
{
|
|
|
|
Sink *sink = area->sink;
|
|
|
|
|
|
|
|
VipsRect all, rect;
|
|
|
|
|
|
|
|
all.left = 0;
|
|
|
|
all.top = 0;
|
|
|
|
all.width = sink->sink_base.im->Xsize;
|
|
|
|
all.height = sink->sink_base.im->Ysize;
|
|
|
|
|
|
|
|
rect.left = 0;
|
|
|
|
rect.top = top;
|
|
|
|
rect.width = sink->sink_base.im->Xsize;
|
|
|
|
rect.height = height;
|
|
|
|
|
|
|
|
vips_rect_intersectrect( &all, &rect, &area->rect );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Our VipsThreadpoolAllocate function ... move the thread to the next tile
|
|
|
|
* that needs doing. If we fill the current area, we block until the previous
|
|
|
|
* area is finished, then swap areas.
|
|
|
|
*
|
|
|
|
* If all tiles are done, we return FALSE to end iteration.
|
|
|
|
*/
|
|
|
|
static gboolean
|
|
|
|
sink_area_allocate_fn( VipsThreadState *state, void *a, gboolean *stop )
|
|
|
|
{
|
2020-02-11 12:44:33 +01:00
|
|
|
SinkThreadState *sstate = (SinkThreadState *) state;
|
2018-01-31 17:25:06 +01:00
|
|
|
Sink *sink = (Sink *) a;
|
|
|
|
SinkBase *sink_base = (SinkBase *) sink;
|
|
|
|
|
|
|
|
VipsRect image;
|
|
|
|
VipsRect tile;
|
|
|
|
|
|
|
|
VIPS_DEBUG_MSG( "sink_area_allocate_fn: %p\n", g_thread_self() );
|
|
|
|
|
|
|
|
/* Is the state x/y OK? New line or maybe new buffer or maybe even
|
|
|
|
* all done.
|
|
|
|
*/
|
|
|
|
if( sink_base->x >= sink->area->rect.width ) {
|
|
|
|
sink_base->x = 0;
|
|
|
|
sink_base->y += sink_base->tile_height;
|
|
|
|
|
|
|
|
if( sink_base->y >= VIPS_RECT_BOTTOM( &sink->area->rect ) ) {
|
|
|
|
/* Block until the previous area is done.
|
|
|
|
*/
|
|
|
|
if( sink->area->rect.top > 0 )
|
|
|
|
vips_semaphore_downn(
|
|
|
|
&sink->old_area->n_thread, 0 );
|
|
|
|
|
|
|
|
/* End of image?
|
|
|
|
*/
|
|
|
|
if( sink_base->y >= sink_base->im->Ysize ) {
|
|
|
|
*stop = TRUE;
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Swap buffers.
|
|
|
|
*/
|
|
|
|
VIPS_SWAP( SinkArea *,
|
|
|
|
sink->area, sink->old_area );
|
|
|
|
|
|
|
|
/* Position buf at the new y.
|
|
|
|
*/
|
|
|
|
sink_area_position( sink->area,
|
|
|
|
sink_base->y, sink_base->n_lines );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* x, y and buf are good: save params for thread.
|
|
|
|
*/
|
|
|
|
image.left = 0;
|
|
|
|
image.top = 0;
|
|
|
|
image.width = sink_base->im->Xsize;
|
|
|
|
image.height = sink_base->im->Ysize;
|
|
|
|
tile.left = sink_base->x;
|
|
|
|
tile.top = sink_base->y;
|
|
|
|
tile.width = sink_base->tile_width;
|
|
|
|
tile.height = sink_base->tile_height;
|
|
|
|
vips_rect_intersectrect( &image, &tile, &state->pos );
|
|
|
|
|
|
|
|
/* The thread needs to know which area it's writing to.
|
|
|
|
*/
|
2020-02-11 12:44:33 +01:00
|
|
|
sstate->area = sink->area;
|
2018-01-31 17:25:06 +01:00
|
|
|
|
|
|
|
VIPS_DEBUG_MSG( " %p allocated %d x %d:\n",
|
|
|
|
g_thread_self(), state->pos.left, state->pos.top );
|
|
|
|
|
|
|
|
/* Add to the number of writers on the area.
|
|
|
|
*/
|
|
|
|
vips_semaphore_upn( &sink->area->n_thread, -1 );
|
|
|
|
|
|
|
|
/* Move state on.
|
|
|
|
*/
|
|
|
|
sink_base->x += sink_base->tile_width;
|
|
|
|
|
|
|
|
/* Add the number of pixels we've just allocated to progress.
|
|
|
|
*/
|
|
|
|
sink_base->processed += state->pos.width * state->pos.height;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2010-04-09 18:51:45 +02:00
|
|
|
/* Call a thread's stop function.
|
|
|
|
*/
|
2010-04-06 11:02:26 +02:00
|
|
|
static int
|
2010-04-09 18:51:45 +02:00
|
|
|
sink_call_stop( Sink *sink, SinkThreadState *state )
|
2010-04-06 11:02:26 +02:00
|
|
|
{
|
2011-11-02 10:22:30 +01:00
|
|
|
if( state->seq && sink->stop_fn ) {
|
2020-02-03 14:54:01 +01:00
|
|
|
int result;
|
|
|
|
|
2010-04-09 18:51:45 +02:00
|
|
|
VIPS_DEBUG_MSG( "sink_call_stop: state = %p\n", state );
|
2010-04-06 11:02:26 +02:00
|
|
|
|
2020-02-03 14:54:01 +01:00
|
|
|
VIPS_GATE_START( "sink_call_stop: wait" );
|
|
|
|
|
|
|
|
g_mutex_lock( sink->sslock );
|
|
|
|
|
|
|
|
VIPS_GATE_STOP( "sink_call_stop: wait" );
|
|
|
|
|
|
|
|
result = sink->stop_fn( state->seq, sink->a, sink->b );
|
|
|
|
|
|
|
|
g_mutex_unlock( sink->sslock );
|
|
|
|
|
|
|
|
if( result ) {
|
2011-03-28 15:08:42 +02:00
|
|
|
SinkBase *sink_base = (SinkBase *) sink;
|
|
|
|
|
2011-03-02 15:13:05 +01:00
|
|
|
vips_error( "vips_sink",
|
2010-04-09 18:51:45 +02:00
|
|
|
_( "stop function failed for image \"%s\"" ),
|
2011-03-28 15:08:42 +02:00
|
|
|
sink_base->im->filename );
|
2010-04-09 18:51:45 +02:00
|
|
|
return( -1 );
|
|
|
|
}
|
2010-04-06 11:02:26 +02:00
|
|
|
|
2010-04-09 18:51:45 +02:00
|
|
|
state->seq = NULL;
|
|
|
|
}
|
2010-04-06 11:02:26 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2010-04-09 18:51:45 +02:00
|
|
|
static void
|
|
|
|
sink_thread_state_dispose( GObject *gobject )
|
|
|
|
{
|
|
|
|
SinkThreadState *state = (SinkThreadState *) gobject;
|
|
|
|
Sink *sink = (Sink *) ((VipsThreadState *) state)->a;
|
|
|
|
|
|
|
|
sink_call_stop( sink, state );
|
2011-03-16 19:26:32 +01:00
|
|
|
VIPS_UNREF( state->reg );
|
2010-04-09 18:51:45 +02:00
|
|
|
|
|
|
|
G_OBJECT_CLASS( sink_thread_state_parent_class )->dispose( gobject );
|
|
|
|
}
|
|
|
|
|
2010-04-06 11:02:26 +02:00
|
|
|
/* Call the start function for this thread, if necessary.
|
|
|
|
*/
|
|
|
|
static int
|
2010-04-09 18:51:45 +02:00
|
|
|
sink_call_start( Sink *sink, SinkThreadState *state )
|
2010-04-06 11:02:26 +02:00
|
|
|
{
|
2011-11-02 10:22:30 +01:00
|
|
|
if( !state->seq && sink->start_fn ) {
|
2010-04-09 18:51:45 +02:00
|
|
|
VIPS_DEBUG_MSG( "sink_call_start: state = %p\n", state );
|
|
|
|
|
2020-02-03 14:54:01 +01:00
|
|
|
VIPS_GATE_START( "sink_call_start: wait" );
|
|
|
|
|
|
|
|
g_mutex_lock( sink->sslock );
|
|
|
|
|
|
|
|
VIPS_GATE_STOP( "sink_call_start: wait" );
|
|
|
|
|
|
|
|
state->seq = sink->start_fn( sink->t, sink->a, sink->b );
|
|
|
|
|
|
|
|
g_mutex_unlock( sink->sslock );
|
2010-04-06 11:02:26 +02:00
|
|
|
|
2010-04-09 18:51:45 +02:00
|
|
|
if( !state->seq ) {
|
2011-03-28 15:08:42 +02:00
|
|
|
SinkBase *sink_base = (SinkBase *) sink;
|
|
|
|
|
2011-03-02 15:13:05 +01:00
|
|
|
vips_error( "vips_sink",
|
2010-04-06 11:02:26 +02:00
|
|
|
_( "start function failed for image \"%s\"" ),
|
2011-03-28 15:08:42 +02:00
|
|
|
sink_base->im->filename );
|
2010-04-06 11:02:26 +02:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-04-09 18:51:45 +02:00
|
|
|
sink_thread_state_build( VipsObject *object )
|
2010-04-06 11:02:26 +02:00
|
|
|
{
|
2010-04-09 18:51:45 +02:00
|
|
|
SinkThreadState *state = (SinkThreadState *) object;
|
|
|
|
Sink *sink = (Sink *) ((VipsThreadState *) state)->a;
|
2010-04-06 11:02:26 +02:00
|
|
|
|
2011-03-03 15:49:12 +01:00
|
|
|
if( !(state->reg = vips_region_new( sink->t )) ||
|
2010-04-09 18:51:45 +02:00
|
|
|
sink_call_start( sink, state ) )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
return( VIPS_OBJECT_CLASS(
|
|
|
|
sink_thread_state_parent_class )->build( object ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sink_thread_state_class_init( SinkThreadStateClass *class )
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
|
|
|
|
VipsObjectClass *object_class = VIPS_OBJECT_CLASS( class );
|
|
|
|
|
|
|
|
gobject_class->dispose = sink_thread_state_dispose;
|
|
|
|
|
|
|
|
object_class->build = sink_thread_state_build;
|
|
|
|
object_class->nickname = "sinkthreadstate";
|
|
|
|
object_class->description = _( "per-thread state for sink" );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sink_thread_state_init( SinkThreadState *state )
|
|
|
|
{
|
|
|
|
state->seq = NULL;
|
|
|
|
state->reg = NULL;
|
|
|
|
}
|
|
|
|
|
2011-11-02 11:27:33 +01:00
|
|
|
VipsThreadState *
|
|
|
|
vips_sink_thread_state_new( VipsImage *im, void *a )
|
2010-04-09 18:51:45 +02:00
|
|
|
{
|
|
|
|
return( VIPS_THREAD_STATE( vips_object_new(
|
|
|
|
sink_thread_state_get_type(),
|
|
|
|
vips_thread_state_set, im, a ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sink_free( Sink *sink )
|
|
|
|
{
|
2020-02-03 14:54:01 +01:00
|
|
|
VIPS_FREEF( vips_g_mutex_free, sink->sslock );
|
2018-01-31 17:25:06 +01:00
|
|
|
VIPS_FREEF( sink_area_free, sink->area );
|
|
|
|
VIPS_FREEF( sink_area_free, sink->old_area );
|
2011-03-02 15:13:05 +01:00
|
|
|
VIPS_FREEF( g_object_unref, sink->t );
|
2010-04-09 18:51:45 +02:00
|
|
|
}
|
2010-04-06 11:02:26 +02:00
|
|
|
|
2011-03-28 15:08:42 +02:00
|
|
|
void
|
|
|
|
vips_sink_base_init( SinkBase *sink_base, VipsImage *image )
|
|
|
|
{
|
2011-08-15 17:24:46 +02:00
|
|
|
/* Always clear kill before we start looping. See the
|
2012-11-09 14:08:59 +01:00
|
|
|
* call to vips_image_iskilled() below.
|
2011-08-15 17:24:46 +02:00
|
|
|
*/
|
|
|
|
vips_image_set_kill( image, FALSE );
|
|
|
|
|
2011-03-28 15:08:42 +02:00
|
|
|
sink_base->im = image;
|
|
|
|
sink_base->x = 0;
|
|
|
|
sink_base->y = 0;
|
|
|
|
|
|
|
|
vips_get_tile_size( image,
|
|
|
|
&sink_base->tile_width, &sink_base->tile_height,
|
2017-04-26 10:45:00 +02:00
|
|
|
&sink_base->n_lines );
|
2012-04-12 11:01:17 +02:00
|
|
|
|
2017-02-27 11:23:46 +01:00
|
|
|
sink_base->processed = 0;
|
2011-03-28 15:08:42 +02:00
|
|
|
}
|
|
|
|
|
2010-04-09 18:51:45 +02:00
|
|
|
static int
|
|
|
|
sink_init( Sink *sink,
|
2011-03-28 15:08:42 +02:00
|
|
|
VipsImage *image,
|
2011-11-02 10:22:30 +01:00
|
|
|
VipsStartFn start_fn, VipsGenerateFn generate_fn, VipsStopFn stop_fn,
|
2010-04-09 18:51:45 +02:00
|
|
|
void *a, void *b )
|
|
|
|
{
|
2011-11-02 10:22:30 +01:00
|
|
|
g_assert( generate_fn );
|
2011-06-02 16:34:57 +02:00
|
|
|
|
2011-03-28 15:08:42 +02:00
|
|
|
vips_sink_base_init( &sink->sink_base, image );
|
|
|
|
|
2010-04-09 18:51:45 +02:00
|
|
|
sink->t = NULL;
|
2020-02-03 14:54:01 +01:00
|
|
|
sink->sslock = vips_g_mutex_new();
|
2011-11-02 10:22:30 +01:00
|
|
|
sink->start_fn = start_fn;
|
|
|
|
sink->generate_fn = generate_fn;
|
|
|
|
sink->stop_fn = stop_fn;
|
2010-04-09 18:51:45 +02:00
|
|
|
sink->a = a;
|
|
|
|
sink->b = b;
|
|
|
|
|
2018-01-31 17:25:06 +01:00
|
|
|
sink->area = NULL;
|
|
|
|
sink->old_area = NULL;
|
|
|
|
|
2011-05-18 12:03:31 +02:00
|
|
|
if( !(sink->t = vips_image_new()) ||
|
2018-01-31 17:25:06 +01:00
|
|
|
!(sink->area = sink_area_new( sink )) ||
|
|
|
|
!(sink->old_area = sink_area_new( sink )) ||
|
2013-11-26 15:57:56 +01:00
|
|
|
vips_image_write( sink->sink_base.im, sink->t ) ) {
|
2010-04-09 18:51:45 +02:00
|
|
|
sink_free( sink );
|
|
|
|
return( -1 );
|
2010-04-06 11:02:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-04-09 18:51:45 +02:00
|
|
|
sink_work( VipsThreadState *state, void *a )
|
2010-04-06 11:02:26 +02:00
|
|
|
{
|
2010-04-09 18:51:45 +02:00
|
|
|
SinkThreadState *sstate = (SinkThreadState *) state;
|
2010-04-06 11:02:26 +02:00
|
|
|
Sink *sink = (Sink *) a;
|
2018-01-31 17:25:06 +01:00
|
|
|
SinkArea *area = sstate->area;
|
2010-04-06 11:02:26 +02:00
|
|
|
|
2018-01-31 17:25:06 +01:00
|
|
|
int result;
|
2010-04-06 11:02:26 +02:00
|
|
|
|
2018-01-31 17:25:06 +01:00
|
|
|
result = vips_region_prepare( sstate->reg, &state->pos );
|
|
|
|
if( !result )
|
|
|
|
result = sink->generate_fn( sstate->reg, sstate->seq,
|
|
|
|
sink->a, sink->b, &state->stop );
|
|
|
|
|
|
|
|
/* Tell the allocator we're done.
|
|
|
|
*/
|
|
|
|
vips_semaphore_upn( &area->n_thread, 1 );
|
|
|
|
|
|
|
|
return( result );
|
2010-04-06 11:02:26 +02:00
|
|
|
}
|
|
|
|
|
2011-03-28 15:08:42 +02:00
|
|
|
int
|
|
|
|
vips_sink_base_progress( void *a )
|
2010-04-06 11:02:26 +02:00
|
|
|
{
|
2011-03-28 15:08:42 +02:00
|
|
|
SinkBase *sink_base = (SinkBase *) a;
|
2010-04-06 11:02:26 +02:00
|
|
|
|
2012-04-12 11:01:17 +02:00
|
|
|
VIPS_DEBUG_MSG( "vips_sink_base_progress:\n" );
|
2011-03-07 16:52:53 +01:00
|
|
|
|
2010-04-06 11:02:26 +02:00
|
|
|
/* Trigger any eval callbacks on our source image and
|
|
|
|
* check for errors.
|
|
|
|
*/
|
2012-04-12 11:01:17 +02:00
|
|
|
vips_image_eval( sink_base->im, sink_base->processed );
|
2012-11-09 14:08:59 +01:00
|
|
|
if( vips_image_iskilled( sink_base->im ) )
|
2010-04-06 11:02:26 +02:00
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-30 14:31:00 +02:00
|
|
|
* vips_sink_tile: (method)
|
2010-04-06 11:02:26 +02:00
|
|
|
* @im: scan over this image
|
2010-04-16 17:48:30 +02:00
|
|
|
* @tile_width: tile width
|
|
|
|
* @tile_height: tile height
|
2011-11-02 10:22:30 +01:00
|
|
|
* @start_fn: start sequences with this function
|
|
|
|
* @generate_fn: generate pixels with this function
|
|
|
|
* @stop_fn: stop sequences with this function
|
2010-04-06 11:02:26 +02:00
|
|
|
* @a: user data
|
|
|
|
* @b: user data
|
|
|
|
*
|
2014-08-19 17:57:37 +02:00
|
|
|
* Loops over an image. @generate_fn is called for every
|
|
|
|
* pixel in the image, with
|
|
|
|
* the @reg argument being a region of calculated pixels.
|
2010-04-06 11:02:26 +02:00
|
|
|
*
|
2013-09-25 13:57:36 +02:00
|
|
|
* Each set of pixels is @tile_width by @tile_height pixels (less at the
|
|
|
|
* image edges). This is handy for things like writing a tiled TIFF image,
|
|
|
|
* where tiles have to be generated with a certain size.
|
2010-04-16 17:48:30 +02:00
|
|
|
*
|
|
|
|
* See also: vips_sink(), vips_get_tile_size().
|
2010-04-06 11:02:26 +02:00
|
|
|
*
|
|
|
|
* Returns: 0 on success, or -1 on error.
|
|
|
|
*/
|
|
|
|
int
|
2010-04-16 17:48:30 +02:00
|
|
|
vips_sink_tile( VipsImage *im,
|
|
|
|
int tile_width, int tile_height,
|
2011-11-02 10:22:30 +01:00
|
|
|
VipsStartFn start_fn, VipsGenerateFn generate_fn, VipsStopFn stop_fn,
|
2010-04-06 11:02:26 +02:00
|
|
|
void *a, void *b )
|
|
|
|
{
|
|
|
|
Sink sink;
|
|
|
|
int result;
|
|
|
|
|
2011-03-10 15:17:02 +01:00
|
|
|
g_assert( vips_object_sanity( VIPS_OBJECT( im ) ) );
|
2010-04-06 11:02:26 +02:00
|
|
|
|
|
|
|
/* We don't use this, but make sure it's set in case any old binaries
|
|
|
|
* are expecting it.
|
|
|
|
*/
|
2011-03-02 15:13:05 +01:00
|
|
|
im->Bbits = vips_format_sizeof( im->BandFmt ) << 3;
|
2010-04-06 11:02:26 +02:00
|
|
|
|
2011-11-02 10:22:30 +01:00
|
|
|
if( sink_init( &sink, im, start_fn, generate_fn, stop_fn, a, b ) )
|
2010-04-06 11:02:26 +02:00
|
|
|
return( -1 );
|
|
|
|
|
2010-04-16 17:48:30 +02:00
|
|
|
if( tile_width > 0 ) {
|
2011-03-28 15:08:42 +02:00
|
|
|
sink.sink_base.tile_width = tile_width;
|
|
|
|
sink.sink_base.tile_height = tile_height;
|
2010-04-16 17:48:30 +02:00
|
|
|
}
|
|
|
|
|
2011-03-31 19:04:23 +02:00
|
|
|
/* vips_sink_base_progress() signals progress on im, so we have to do
|
|
|
|
* pre/post on that too.
|
|
|
|
*/
|
|
|
|
vips_image_preeval( im );
|
2010-04-06 11:02:26 +02:00
|
|
|
|
2018-01-31 17:25:06 +01:00
|
|
|
sink_area_position( sink.area, 0, sink.sink_base.n_lines );
|
2010-04-06 11:02:26 +02:00
|
|
|
result = vips_threadpool_run( im,
|
2011-11-02 11:27:33 +01:00
|
|
|
vips_sink_thread_state_new,
|
2018-01-31 17:25:06 +01:00
|
|
|
sink_area_allocate_fn,
|
2010-04-09 18:51:45 +02:00
|
|
|
sink_work,
|
2011-03-28 15:08:42 +02:00
|
|
|
vips_sink_base_progress,
|
2010-04-09 18:51:45 +02:00
|
|
|
&sink );
|
2010-04-06 11:02:26 +02:00
|
|
|
|
2011-03-31 19:04:23 +02:00
|
|
|
vips_image_posteval( im );
|
2010-04-06 11:02:26 +02:00
|
|
|
|
|
|
|
sink_free( &sink );
|
|
|
|
|
|
|
|
return( result );
|
|
|
|
}
|
2010-04-16 17:48:30 +02:00
|
|
|
|
|
|
|
/**
|
2017-09-30 14:31:00 +02:00
|
|
|
* vips_sink: (method)
|
2010-04-16 17:48:30 +02:00
|
|
|
* @im: scan over this image
|
2011-11-02 10:22:30 +01:00
|
|
|
* @start_fn: start sequences with this function
|
|
|
|
* @generate_fn: generate pixels with this function
|
|
|
|
* @stop_fn: stop sequences with this function
|
2010-04-16 17:48:30 +02:00
|
|
|
* @a: user data
|
|
|
|
* @b: user data
|
|
|
|
*
|
2014-08-19 17:57:37 +02:00
|
|
|
* Loops over an image. @generate_fn is called for every pixel in
|
|
|
|
* the image, with
|
|
|
|
* the @reg argument being a region of calculated pixels. vips_sink() is
|
|
|
|
* used to implement operations like vips_avg() which have no image output.
|
2010-04-16 17:48:30 +02:00
|
|
|
*
|
|
|
|
* Each set of pixels is sized according to the requirements of the image
|
|
|
|
* pipeline that generated @im.
|
|
|
|
*
|
2011-03-25 10:52:36 +01:00
|
|
|
* See also: vips_image_generate(), vips_image_new().
|
2010-04-16 17:48:30 +02:00
|
|
|
*
|
|
|
|
* Returns: 0 on success, or -1 on error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vips_sink( VipsImage *im,
|
2011-11-02 10:22:30 +01:00
|
|
|
VipsStartFn start_fn, VipsGenerateFn generate_fn, VipsStopFn stop_fn,
|
2010-04-16 17:48:30 +02:00
|
|
|
void *a, void *b )
|
|
|
|
{
|
2011-11-02 10:22:30 +01:00
|
|
|
return( vips_sink_tile( im, -1, -1,
|
|
|
|
start_fn, generate_fn, stop_fn, a, b ) );
|
2010-04-16 17:48:30 +02:00
|
|
|
}
|