Shared Samples

Try out source code samples stored in Bitbucket repositories.
Showing results for 
Search instead for 
Do you mean 

Job subset

by Technical Evangelist on ‎09-08-2015 10:38 AM - edited on ‎03-29-2016 11:33 AM by Anonymous (704 Views)

Example Type

Job

 

 

Purpose

 

This example shows how to subset an image

 

Files

 

subset.c           (C source code)

subset.eml      (EML script)

subset.dsp       (Visual C++ Project)(Windows Only)

 

 

 

 

Command Line Options

 

job subset <inputfilename>  <outputfilename> -s[ubset] <subset>  -p[ixeltype] <pixeltype>

 

-s[ubset]

 The upper left x, upper left y,  lower right x,  lower right y (new image size).

 

-p[ixeltype]

 The new pixel type of the image.

 

Interface

 demo.jpg

 

Source Code

https://bitbucket.org/HGDN/hgdn_erdas_imagine/src/e1ade3b7d148b0f2f1aa6427044dfa4bce6226ee/Job_subse...

 

 

/*****************************************************************************

subset_sm.c

Illustrates a toolkit program designed to be run under the Imagine
Session Manager as a job.

Structures and functions introduced:
esmg_JobInit, esmg_JobEnd, esmg_JobState, esmg_JobProgress functions

Calling syntax:

subset_sm <inputfile> <outputfile> [ -s[ubset] <ulx> <uly> <lrx> <lry> ]
	[ -p[ixeltype] <pixeltype> ]

Produce the subset of the file <inputfile>, defined by upper left corner 
coordinates ( <ulx>, <uly> ) and lower right corner coordinates
( <lrx>, <lry> ), and write the results to <outputfile>.  If -subset
is not given, the entire file is copied. The data type (e.g. signed
8-bit integer, etc) of the output file is the same as the input file
unless the -p parameter is given, in which case the output data type
is <pixeltype>.

*************************************************************************/

/*
** Toolkit Includes
*/
#include <eint.h>
#include <eimg.h>
#include <eerr.h>
#include <earg.h>
#include <emsc.h>
#include <esmg.h>

/*
**     Function declarations
*/
static void set_subset( int, char ** );
static void set_pixeltype( int, char ** );
static void subset( int, char ** );
static void SubsetLayer( char *, char *, Eerr_ErrorReport **err );

/*
**     Global variable declarations
*/
static Eint_InitToolkitData *erdinit = NULL;
static Egda_PixelType pixeltypeout = EGDA_TYPE_INVALID;
static long ulx=0, uly=0, lrx=0, lry=0;

/*
** Command Line Parameter Format
*/
static Earg_Cmd format[] = 
{
    subset,		"subset %s %s", 	"",EARG_NO_ERR,
    set_subset,		"-s[ubset] %d %d %d %d","",EARG_NO_ERR,
    set_pixeltype,	"-p[ixeltype] %s",	"",EARG_NO_ERR,
    EARG_END
};

 
/*
** Main Program
*/
main( argc, argv )
int argc;
char  **argv;
{
	EERR_INIT( "main", &lcl, lcl );
	
	/*
	** Initialize the ERDAS toolkit
	*/
	erdinit=eint_InitToolkit( (Emsc_Opaque **)&lcl );
	if( lcl ) {
		EERR_FATAL_SET_0( __LINE__, "Error initializing toolkit" );
		EERR_SHOW( lcl, EERR_DEBUG );
		return -1;
	}

	/*
	** Connect to the Session Manager
	*/
	esmg_JobInit( "File Subset", &lcl );
	if( lcl ) {
		EERR_FATAL_SET_0( __LINE__,
			"Error connecting to Session Manager" );
		EERR_SHOW( lcl, EERR_DEBUG );
		return -1;
	}
	
	/*
	** Process command-line arguments
	*/
	earg_DoArgs( format, argc, argv, &lcl );
	if( lcl ) {
		EERR_FATAL_SET_0( __LINE__,
			"Error processing command-line arguments" );
		EERR_SHOW( lcl, EERR_DEBUG );
		return -1;
	}

	/*
	** Disconnect from the Session Manager
	*/
	esmg_JobEnd( &lcl );
	EERR_DELETE( lcl );
	
	return 0;
}

static void
set_pixeltype( argc, argv )
int argc;
char **argv;
{
	EERR_INIT( "set_pixeltype", &lcl, lcl );

	pixeltypeout = epxm_StringToType( argv[1], &lcl );
	if( lcl ) {
		EERR_FATAL_SET_1( __LINE__, "Invalid pixel type: %s",
				  argv[1] );
		EERR_SHOW( lcl, EERR_DEBUG );
		return;
	}
}

static void
set_subset( argc, argv )
int argc;
char **argv;
{
	EERR_INIT( "set_subset", &lcl, lcl );

	ulx = atol( argv[1] );
	uly = atol( argv[2] );
	lrx = atol( argv[3] );
	lry = atol( argv[4] );
}


static void
subset( argc, argv )
int argc;
char **argv;
{
	Estr_StringList *layernames;
	int nlayers, i;
	char *taskstring;
	EERR_INIT( "subset", &lcl, lcl );

	/*
	** Get layer names from input file
	*/
	layernames = eimg_LayerGetNames( argv[1], erdinit, &lcl );
	if( !layernames || lcl ) {
		EERR_FATAL_SET_1( __LINE__,
			"Error reading layer names for %s", argv[1] );
		EERR_SHOW( lcl, EERR_DEBUG );
		return;
	}

	/*
	** Make sure input file has at least one raster layer
	*/
	nlayers = layernames->count;
	if( nlayers==0 ) {
		EERR_FATAL_SET_1( __LINE__,
			"%s contains no raster layers", argv[1] );
		EERR_SHOW( lcl, EERR_DEBUG );
		return;
	}

	for( i=0; i<nlayers; i++ ) {
		/*
		** Change meter text
		*/
		taskstring = estr_Sprintf( NULL,
			"Subsetting layer %d of %d", &lcl,
			i+1, nlayers );
		EERR_DELETE( lcl );

		esmg_JobState( taskstring, &lcl );
		EERR_DELETE( lcl );

		emsc_Free( taskstring );
		
		SubsetLayer( layernames->strings[i], argv[2], &lcl );
		if( lcl ) {
			EERR_FATAL_SET_1( __LINE__,
				"Error subsetting layer %d", i );
			EERR_SHOW( lcl, EERR_DEBUG );
			return;
		}
	}

	estr_StringListDelete( layernames, &lcl );
	EERR_DELETE( lcl );		/* Ignore any errors here */
}


static void
SubsetLayer( inputlayername, outputfilename, err )
char *inputlayername;
char *outputfilename;
Eerr_ErrorReport **err;
{
	Eimg_Layer *layerin, *layerout;
	Eimg_PixelRect *pixrect;
	long row;
	long outwidth, outheight;
	char *outlayername;
	EERR_INIT( "SubsetLayer", err, lcl );

	/*
	** Open the input layer
	*/
	layerin = eimg_LayerOpen( inputlayername, erdinit, &lcl,
		EIMG_LAYER_OPTION_READONLY, EIMG_LAYER_OPTION_END );
	if( lcl ) {
		EERR_FATAL_SET_1( __LINE__, "Error opening %s",
			inputlayername );
		return;
	}

	/*
	** Make sure we've got a proper subset
	*/
	ulx = EMSC_MAX( ulx, 0 );
	uly = EMSC_MAX( uly, 0 );
	lrx = EMSC_MIN( lrx, layerin->width-1 );
	lry = EMSC_MIN( lry, layerin->height-1 );

	/*
	** If lower right position was not specified, set it equal to the
	** lower right corner of the input
	*/
	if( lrx==0 ) lrx = layerin->width-1;
	if( lry==0 ) lry = layerin->height-1;

	outwidth = lrx - ulx + 1;
	outheight = lry - uly + 1;

	/*
	** If output pixel type has not been specified, set it equal to the
	** input pixel type
	*/
	if( pixeltypeout==EGDA_TYPE_INVALID )
		pixeltypeout = layerin->pixelType;
	
	/*
	** Allocate a pixelrect equal to one line of data
	*/
	pixrect = eimg_PixelRectCreate( outwidth, 1, pixeltypeout, &lcl );
	if( lcl ) {
		EERR_FATAL_SET_0( __LINE__, "Error allocating pixelrect" );
		return;
	}

	/*
	** Derive the output layer name and create the output layer
	*/
	outlayername = estr_StringCat( NULL, outputfilename,
		strchr( inputlayername, '(' ) );
	layerout = eimg_LayerCreate( outlayername,
		layerin->layerType, pixeltypeout,
		outwidth, outheight, erdinit, &lcl,
		EIMG_LAYER_OPTION_COMPUTE_STATS_ON_CLOSE,
		EIMG_LAYER_OPTION_END );
	if( lcl ) {
		EERR_FATAL_SET_1( __LINE__, "Error creating layer %s",
				  outlayername );
		return;
	}

	emsc_Free( outlayername );
	
	/*
	** For each row in the layer...
	*/
	for( row=0; row<outheight; row++ ) {
		/*
		** Read a row from the input layer
		*/
		eimg_LayerRead( layerin, ulx, row+uly, outwidth, 1, pixrect,
			        &lcl );
		if( lcl ) {
			EERR_FATAL_SET_0( __LINE__,
				"Error reading layer data" );
			return;
		}
		
		/*
		** Write the row to the output layer
		*/
		eimg_LayerWrite( layerout, 0, row, outwidth, 1, pixrect,
			        &lcl );
		if( lcl ) {
			EERR_FATAL_SET_0( __LINE__,
				"Error writing layer data" );
			return;
		}

		/*
		** Show percent completion
		*/
		esmg_JobProgress( (long)(row*100.0/outheight+.5), &lcl );
		EERR_DELETE( lcl );
	}

	/*
	** Close the layers
	*/
	eimg_LayerClose( layerin, &lcl );
	if( lcl ) {
		EERR_FATAL_SET_0( __LINE__, "Error closing input layer" );
		return;
	}

	eimg_LayerClose( layerout, &lcl );
	if( lcl ) {
		EERR_FATAL_SET_0( __LINE__, "Error closing output layer" );
		return;
	}

	/*
	** Deallocate storage
	*/
	eimg_PixelRectDelete( pixrect, &lcl );
	EERR_DELETE( lcl );	/* Ignore this error */
}

 

Overview
Contributors