Shared Samples

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

Job imageCopy : shows how to read, create and write raster data

by Technical Evangelist on ‎09-08-2015 10:26 AM - edited on ‎03-29-2016 11:27 AM by Anonymous (666 Views)

Example Type

Job

 

 

Purpose

 

This example shows how to read, create and write raster data

 

Files

 

imagecopy.c                (C source code)

imagecopy.eml            (EML script)

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

 

 

Description

 

The imageCopy example is intended to show the most basic of raster access functionality. It will read the Eimg_LayerStack of an existing IMAGINE image, create a new layerstack and then write the raster data to that layerstack. It will in effect copy the image.

 

Command Line Options

 

job imagecopy -in<inputfile> -out<outputfile>

 

-in[put]

 This is the necessary input raster image to be copied.

 -out[put]

 This is the output image name.

 

 

Interface

 

 demo.jpg

 

Output

 

The output is an exact copy of the input image.

 

Source Code

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

 

/**********************************************************************
***********************************************************************
 Job: imageCopy

 Purpose: This example shows how to read, create and write raster data. 

 Calling syntax:

 job imagecopy -in<inputfile> -out<outputfile>

 e.g.
 job imagecopy -in "$IMAGINE_HOME/examples/xs_truecolor_sub.img" 
			   -out "$IMAGINE_HOME/examples/temp.img";

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

/*
** System Includes
*/ 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>

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

/*
** Definition error checking macros 
*/
#define EFS1( msg, arg ) EERR_FATAL_SET_1( __LINE__, msg, arg )
#define EFS0( msg ) EERR_FATAL_SET_0( __LINE__, msg ) 
#define EWS0( msg ) EERR_WARNING_SET_0(__LINE__, msg)
#define ES0(e, msg ) EERR_SHOW_0( e, EERR_DEBUG, __LINE__, msg )

/*
** Global Functions 
*/
static void imageCopy(int, char **);
static void set_input( int, char **);
static void set_output( int, char **);


/*
** Erdas Argument commands 
*/
Earg_Cmd command[] = {
    set_input,	"-in[put] %s",	"",	EARG_NO_ERR,
    set_output,	"-out[put] %s",	"",	EARG_NO_ERR,
	imageCopy,	"imagecopy",	"",	EARG_NO_ERR,
    EARG_END
};


/*
** Global variables used 
*/
static Eint_InitToolkitData *erdinit = NULL;
static Eerr_ErrorReportingMode reportingmode = EERR_ENDUSER;
static Emet_MeterInfo *meterinfo = NULL;
static char *input = NULL;
static char *output = NULL;
static Emsc_Boolean logpixeltypeset = EMSC_FALSE;
static Egda_PixelType pixeltypeout;
/*
** Initialize to failure and set to success.
*/
static int rc = -1;


/*
** main program for read/write
*/
int
main (
	  int argc,
	  char **argv
)
{
    EERR_INIT("ImageCopy", &lclerr, lclerr);
    /*
    **  Initialize the ERDAS toolkit
    */
    erdinit = eint_InitToolkit((Emsc_Opaque **) &lclerr);
	EERR_CHECK(lclerr, EFS0("Unable to initialize the toolkit"));
  
    /*
    ** Initialize the Job to the session manager  
    */
    esmg_JobInit("ImageCopy", &lclerr);
	EERR_CHECK(lclerr, EFS0("Unable to initialize the job!"));
  
    /*
    **  Parse the command line, and perform the read/write
    */
    earg_DoArgs(command, argc, argv, &lclerr);
    EERR_CHECK(lclerr, EFS0("There was an error on the command line!"));
  

cleanup:
	ES0(lclerr, "Error reported by main");
    /*
    **  Disconnect from the session manager
    */
    esmg_JobEnd (&lclerr);
/* return rc; */  
    return 0; 
}

/*
** Input file 
*/
static void 
set_input(
	int argc, 
	char **argv
)
{ 
    input = estr_Duplicate(argv[1]);
    return;
}


/*
** Output file 
*/

static void 
set_output(
	int argc, 
	char **argv
)
{
    output = estr_Duplicate(argv[1]);
    return;
}


static void 
imageCopy(
	int argc, 
	char **argv
)
{
	EERR_INIT("ImageCopy", &lclerr, lclerr);

    long i,j,row,layer, length;
    long npixelw,npixelh,nlayers;
    char *outlayername = NULL, *tempstring = NULL;
    Eimg_Layer *inlayer = NULL, *outlayer = NULL;
    Eimg_PixelRect *pixelline = NULL;     
    Eimg_LayerStack *inlayerstack = NULL;   
    Estr_StringList *layernames = NULL;    
    
    
	EERR_CHECK(!input, EFS0("You did not use -in[put] option to specify the input file!"));
	EERR_CHECK(!output, EFS0("You did not use -out[put] option to specify the output file!"));


	/*
    **  Set up meter information structure
    */
    meterinfo = emet_MeterInfoCreate("ImageCopy", "meter", NULL, &lclerr);
    EERR_CHECK( lclerr, EWS0( "Error meter info function" ));

	/*
    ** Get layer names 
    */   
    layernames = eimg_LayerGetNames(input, erdinit, &lclerr);
    EERR_CHECK( lclerr, EFS0( "Error getting the layernames" )); 
  
    /*
    **  Check that the input file has at least one raster layer
    */
    nlayers = layernames->count;
  
    /*
    **  Initialize progress meter to 0 % completed
    */    
    emet_MeterInfoPrint(meterinfo, 0.0, &lclerr);
    EERR_CHECK(lclerr, EFS0(" Error with meter printinfo"));

    /*
    **  Now read through all the lines of all the layers
    */
    for (layer = 0; layer < nlayers; layer++) 
	{ 
        /*
        ** Open existing layer in input file
        */
        inlayer = eimg_LayerOpen(layernames->strings[layer], erdinit,
            &lclerr, EIMG_LAYER_OPTION_READONLY, EIMG_LAYER_OPTION_END);
        EERR_CHECK(lclerr, EFS0(" Error with Eimg_LayerOpen"));     
    
        npixelw = inlayer->width;
        npixelh = inlayer->height;
    
        /*
        ** Create new layer in output file
        */
        tempstring = strchr(layernames->strings[layer], '(');
		length = strlen(output) + strlen(tempstring) + 1;
        outlayername = (char *) emsc_New(length, char);
        strcpy(outlayername,output);
        strcat(outlayername,tempstring);
		tempstring = NULL;
        outlayer = eimg_LayerCreate(outlayername, inlayer->layerType,
            inlayer->pixelType, npixelw, npixelh, erdinit, &lclerr,
            EIMG_LAYER_OPTION_COMPUTE_STATS_ON_CLOSE,
            EIMG_LAYER_OPTION_END);  
        EERR_CHECK(lclerr, EFS0(" Error with eimg_LayerCreate"));

		emsc_Free(outlayername);
		outlayername = NULL;
    
        /*
        ** Allocate space for the output line of data
        */
        pixelline = eimg_PixelRectCreate(npixelw, 1L, outlayer->pixelType,
            &lclerr);  
        EERR_CHECK(lclerr, EFS0(" Error with PixelRectCreate"));   
    
        /*
        ** Set up range and state message for progress meter
        */
        tempstring = estr_Sprintf(NULL, "Copying layer number %d of %d",
            &lclerr, layer+1, nlayers);
        EERR_CHECK(lclerr, EFS0(" Error with estr_Spintf"));   
    
        /* 
        ** Change task name on meter 
        */ 
        emet_MeterInfoChangeTaskName(meterinfo, tempstring, &lclerr);
        EERR_CHECK(lclerr, EFS0(" Error with meter change name"));
    
        /* 
        ** Delete information 
        */ 
        emsc_Free(tempstring);
		tempstring = NULL;
    
        /* 
        ** Meter information set 
        */ 
        emet_MeterInfoSet(meterinfo, (100.0*layer)/nlayers,
            (100.0*(layer+0.5))/nlayers,&lclerr);
        EERR_CHECK(lclerr, EFS0(" Error with meter information set"));
    
        /*
        **   Read each line of input to a line of output.
        */
        for (row = 0; row < npixelh; row++)  
		{
            /*
            **  Read a line from the input layer
            */
            eimg_LayerRead(inlayer, 0, row, npixelw, 1, pixelline, &lclerr);
            EERR_CHECK(lclerr, EFS0(" Error with LayerRead"));
            /*
            ** Write the line to the output layer
            */
            eimg_LayerWrite(outlayer, 0, row, npixelw, 1, pixelline, &lclerr); 
            EERR_CHECK(lclerr, EFS0(" Error with Layer Write"));
            /*
            **  Show percentage completion
            */
            emet_MeterInfoPrint(meterinfo, row*100.0/npixelh, &lclerr); 
            EERR_CHECK(lclerr, EFS0(" Error with meter info print"));
        }
    
        /*
        ** Close the input layer
        */
        eimg_LayerClose(inlayer, &lclerr);   
		inlayer = NULL;
        EERR_CHECK(lclerr, EFS0(" Error with Layer Close Input"));
    
        /* 
        ** Create the title for the meter bar
        */   
        tempstring = estr_Sprintf(NULL,
            "Computing statistics for layer number %d of %d",
            &lclerr,layer+1, nlayers);
        EERR_CHECK(lclerr, EFS0(" Error estr_Sprintf function")); 
    
        /*
        ** Change the task bar on the meter 
        */
        emet_MeterInfoChangeTaskName(meterinfo, tempstring, &lclerr);  
        EERR_CHECK(lclerr, EFS0(" Error with changing meter info task name"));  
    
        /* Delete taskname title pointer */
        emsc_Free(tempstring); 
		tempstring = NULL;
    
        /* 
        ** Set the meter info set 
        */   
        emet_MeterInfoSet(meterinfo,(100.0*(layer+0.5))/nlayers,
            (100.0*(layer+1.0))/nlayers, &lclerr); 
        EERR_CHECK(lclerr, EFS0(" Error setting meter once again!"));
    
        /*
        ** Close current outlayer 
        */
        eimg_LayerClose(outlayer, &lclerr);
		outlayer = NULL;
        EERR_CHECK(lclerr, EFS0(" Error closing layer!"));
    
        /*
        ** Deallocate storage for line of output pixels
        */
        eimg_PixelRectDelete(pixelline, &lclerr); 
		pixelline = NULL;
        EERR_CHECK(lclerr, EFS0(" Error deallocating storage pixels!"));
    } 

    /*
    ** Meter function print at 100% 
    */
    emet_MeterInfoPrint(meterinfo, 100.0, &lclerr); 
    EERR_CHECK(lclerr, EFS0(" Error with meterinfor Print!"));
  
cleanup:
    ES0(lclerr, "Error reported by imageCopy");

	emsc_Free(outlayername);
	emsc_Free(tempstring);
	emsc_Free(input);
	emsc_Free(output);

	if (inlayer)
		eimg_LayerClose(inlayer,&lclerr);
    
    if (outlayer) 
		eimg_LayerClose(outlayer,&lclerr);

	if(pixelline)
		eimg_PixelRectDelete(pixelline,&lclerr); 

	EERR_DELETE(lclerr);

    return;
}        


 

 

 

 

Overview
Contributors