/*******************************************************************************
*                                                                       
*N  {calcimagestats.c}  --  Example of the calculation of image statistics. 
*It is assumed that the rasters already exists with raster blocks containing pixel data. 
*                                                                       
*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
* How to compile:: 
*       This program provides programming examples of 
*
*
*     1. Add {ibm ICU installation directory}/lib to your library paths
*	  2. Add {ibm ICU installation directory}/include to your include paths
*     3. Add {$SDEHOME}/lib to your library paths;
*     4. Compile and link with icuuc.lib and other needed SDE libraries
*E
*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*X  Legalese:
*
* Copyright � 2006-2007 ESRI
*
* All rights reserved under the copyright laws of the United States and 
* applicable international laws, treaties, and conventions.
*
* You may freely redistribute and use this sample code, with or without 
* modification, provided you include the original copyright notice and use 
* restrictions.  
*
* Disclaimer:  THE SAMPLE CODE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 
* WARRANTIES, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ESRI OR 
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
* OR BUSINESS INTERRUPTION) SUSTAINED BY YOU OR A THIRD PARTY, HOWEVER CAUSED 
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
* TORT ARISING IN ANY WAY OUT OF THE USE OF THIS SAMPLE CODE, EVEN IF ADVISED 
* OF THE POSSIBILITY OF SUCH DAMAGE.
* 
* For additional information, contact:
* Environmental Systems Research Institute, Inc.
* Attn: Contracts and Legal Services Department
* 380 New York Street
* Redlands, California, 92373
* USA
* 
* email: contracts@esri.com
*
*E
****************************************************************************/


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

void main(void)
{

	LONG rc, band, rastercolumn_id, number_of_bands,num_bins;
	SE_ERROR error;
	SE_CONNECTION connection;
	SE_RASBANDINFO *rasterbands;
	SE_RASCOLINFO rastercolumn;
	SE_RASTERINFO raster;
	SE_BIN_FUNCTION_TYPE bin_function_type;
	CHAR bandname[24],
	table[SE_MAX_TABLE_LEN],
	column[SE_MAX_COLUMN_LEN],
	*server,
	*instance,
	*database,
	*username,
	*password;

		/* Set the ArcSDE server connection parameters */ 

		server ="jolex";
		instance = "9000";
		database = NULL;
		username = "mark";
		password = "mark";

		/* Connect to the ArcSDE server */

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

		/* Set the business table and raster column */

		strcpy(table, "myrastertable");
		strcpy(column, "raster");

		/* Initialize SE_RASCOLINFO structure */

		rc = SE_rascolinfo_create (&rastercolumn);
		check_error (rc, NULL, NULL, "SE_rascolinfo_create");

		/* Initialize the SE_RASTERINFO structure */

		rc = SE_rasterinfo_create (&raster);
		check_error (rc,NULL, NULL, "SE_rasterinfo_create");

		/* Populate the SE_RASCOLINFO structure */

		rc = SE_rastercolumn_get_info_by_name (connection, table, column, rastercolumn);
		check_error (rc, connection, NULL, "SE_rastercolumn_get_info_by_name");

		/* Get the rastercolumn id */

		rc = SE_rascolinfo_get_id (rastercolumn, &rastercolumn_id);
		check_error (rc, NULL, NULL, "SE_rastercolumn_get_id");

		/* Populate the SE_RASTERINFO structure based on the raster column id */

		rc = SE_raster_get_info_by_id (connection, rastercolumn_id, 1, raster);
		check_error (rc, connection, NULL, "SE_raster_get_info_by_id");

		/* Populate the SE_RASBANDINFO list. */

		rc = SE_raster_get_bands ( connection, raster, &rasterbands, &number_of_bands);
		check_error (rc, connection, NULL, "SE_raster_get_bands");

		for(band=0; band<number_of_bands; band++) { 

	num_bins = 0;
	bin_function_type = SE_BIN_FUNCTION_AUTO;

		/* Set the bin function to automatic complete with bin table. */

		rc = SE_rasbandinfo_set_bin_function ( rasterbands[band], bin_function_type, 0, NULL);
		check_error (rc, NULL, NULL, "SE_rasbandinfo_set_bin_function");

		sprintf (bandname,"This is a rasterband \0");

		rc = SE_rasbandinfo_set_band_name (rasterbands[band], bandname);
		check_error (rc, NULL, NULL, "SE_rasbandinfo_set_band_name");

		/* Add the statistics to the rasterband. */

		rc = SE_rasterband_alter (connection, rasterbands[band]);
		check_error (rc, connection, NULL, "SE_rasterband_alter");

	}

		/* Fetched the altered band info */

		rc = SE_raster_get_bands ( connection, raster, &rasterbands, &number_of_bands);
		check_error (rc, connection, NULL, "SE_raster_get_bands");

		/* Display the image statistics of each band */

		for(band=0; band<number_of_bands; band++) { 

		print_image_statistics (rasterbands[band]);

		}

		/* Free the raster structures */

		SE_rascolinfo_free (rastercolumn);
		SE_rasterinfo_free (raster);
		SE_rasterband_free_info_list (number_of_bands, rasterbands);

		/* Release the connection */

		SE_connection_free (connection);

		return;

}