#define SDESERVER "myserver"
#define SDEINSTANCE "myinstance"
#define SDEDATABASE "mydatabase"
#define SDEUSER	"myuser"
#define SDEPASSWORD "mypassword"
#define FILENAME "france"

#include <stdlib.h>
#include <string.h>
#include "sdetype.h"
#include "sderaster.h"
#include "raster_source.h"

void main (void) {

	SE_RASTERATTR raster_attrib;
	SE_CONNECTION connection;
	SE_ERROR error;
	SE_STREAM stream;
	LONG row_id,
     	     rc, 
     	     pixel_type, 
     	     mosaic_mode, 
     	     interpolation,
     	     compression;

	BSQMETADATA bsqmetadata;
	CHAR table[SE_MAX_TABLE_LEN],
     	     *filename,
     	     **columns,
     	     *server,
     	     *instance,
     	     *database,
     	     *username,
     	     *password;
	SHORT num_columns,
      	      raster_ind = SE_IS_NOT_NULL_VALUE;
	CLIENT_DATA client_data;

	/* Set the ArcSDE server connection info. */
	
	server = SDESERVER;
	instance = SDEINSTANCE;
	database = SDEDATABASE;
	username = SDEUSER;
	password = SDEPASSWORD;



	/* Connect to the ArcSDE server. */

	rc = SE_connection_create (server,instance,database,username,password,&error,&connection);
	check_error (rc,connection,NULL,"SE_connection_create");

	/* Initialize the data stream. */

	rc = SE_stream_create (connection,&stream);
	check_error (rc, connection, NULL, "SE_stream_create");

	/* Configure the stream to update a row of the raster datasets business table */
	/* A mosiac is a type of update. Raster updates also include a complete replacement, */
	/* and a delete of a pixel region */

	num_columns = 1;
	columns = (CHAR **) malloc (num_columns * sizeof(CHAR *));
	columns[0] = "raster";

	strcpy(table,"myrastertable");
	row_id = 1;

	rc = SE_stream_update_row (stream, (const CHAR *) table, &row_id, num_columns, (const CHAR**)columns);
	check_error (rc, NULL, stream, "SE_stream_update_row");

	/* Fetch the BSQ metadata for the image to be mosaicked */

	filename = FILENAME;

	rc = readbsqmetadata (filename, &bsqmetadata);
	check_error (rc, NULL, NULL, "readbsqmetadata");

	/* Set the pixel depth to 8 bit unsigned */

	pixel_type = SE_PIXEL_TYPE_8BIT_U;

	/* Set the mosaic mode to none, a full replacement is performed. */

	mosaic_mode = SE_MOSAIC_MODE_NONE;

	/* Set the pyramid interpolation to bilinear resampling */

	interpolation = SE_INTERPOLATION_BILINEAR;

	/* Set the storage compression to LZ77 lossless */ 

	compression = SE_COMPRESSION_LZ77;

	/* Setup the raster attribute structure */

	prepare_raster_attribute ( connection,
        		           &raster_attrib,
             			   &client_data,
             			   pixel_type,
             			   bsqmetadata,
             			   interpolation,
             			   compression,
		                   mosaic_mode);

	/* Bind the RASTER column */

	rc = SE_stream_bind_input_column (stream,1,(void *) raster_attrib,&raster_ind);
	check_error (rc, NULL, stream, "SE_stream_bind_input_column");

	/* Prepare the client data */

	prepare_client_data ( &client_data, 
       			      pixel_type,
       			      bsqmetadata.pixelwidth,
			      filename);

	/* Execute the mosaic. */

	rc = SE_stream_execute (stream);
	check_error (rc, NULL, stream, "SE_stream_execute");

	/*********/
	/* cleanup */
	/*********/

	free(client_data.buffer);
	free(client_data.bitmaskbuffer);

	SE_rasterattr_free (raster_attrib);

	rc = SE_stream_free (stream);
	check_error (rc, NULL, stream, "SE_stream_free");

	SE_connection_free (connection);

	return;

}