/*******************************************************************************
*                                                                       
*N  {insertimage2raster.c}  --  Example of how to insert an image into a raster. After you have *inserted an image into a raster, you can mosaic more images to that raster as long as they have 
*the same cell size, pixel alignment, number of bands, and pixel resolution.
*                                                                   
*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
* 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 "raster_source.h"
#include <stdlib.h>
#include <string.h>


void main (void) {

	SE_RASTERATTR raster_attrib;
	SE_STREAM stream;
	LONG rc,pixel_type,mosaic_mode;
	SE_INTERPOLATION_TYPE interpolation;
	SE_COMPRESSION_TYPE compression;
	CHAR raster_column[] = "raster", 
	name_value[] = "europe", 
	**columns,
	*server,
	*instance,
	*database,
	*username,
	*password,table[SE_MAX_TABLE_LEN],
	*filename,
	config_keyword[SE_MAX_CONFIG_KEYWORD_LEN],
	column[SE_MAX_COLUMN_LEN];
	SHORT num_columns,
	name_ind = SE_IS_NOT_NULL_VALUE,
	raster_ind = SE_IS_NOT_NULL_VALUE,|
	shape_ind = SE_IS_NOT_NULL_VALUE;
	CLIENT_DATA client_data;
	BSQMETADATA bsqmetadata;
	SE_ERROR error;
	SE_CONNECTION connection;

	/*****************/
	/* Set connection info */
	/*****************/

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

	/* Set the filenames for the BSQ file and the header file. */

	filename = "spain";

	/* Set the business table name. */

	strcpy(table, "myrastertable");

	/* Set the raster column */

	strcpy(column,"raster");

	/* Set the configuration keyword */

	strcpy(config_keyword, "defaults");

	/* Connect to the ArcSDE server */

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

	/* Create the business table  */

	create_business_table (connection, table, config_keyword);

	/* add the raster column to the business table */

	add_raster_column (connection, table, column, config_keyword);

	/* Create a stream that will be used to insert the raster image pixel data into */
	/* the raster column */

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

	/* Set up the stream insert */

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

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

	/* Bind the NAME column */

	rc = SE_stream_bind_input_column (stream,1,name_value,&name_ind);
	check_error (rc, NULL, stream, "SE_stream_bind_input_column");

	/* Set the compression type to LZ77 lossless */

	compression = SE_COMPRESSION_LZ77;

	/* Set the pyramid resampling interpolation bilinear */

	interpolation = SE_INTERPOLATION_BILINEAR;

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

	pixel_type = SE_PIXEL_TYPE_8BIT_U;

	/* Set the mosaic mode to none (this is an insert) */

	mosaic_mode = SE_MOSAIC_MODE_NONE;

	/* fetch the bsq image dimensions from the bsq header file */

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

	/* 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,2,(void *) raster_attrib,&raster_ind);
	check_error (rc, NULL, stream, "SE_stream_bind_input_column");

	/* Prepare the client data used to communicate pixel data between */
	/* the callback routine and the SE_stream_execute function */

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

	/* Insert the raster into the business table */

	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);
	SE_stream_free (stream);
	SE_connection_free (connection);


}