/**************************************************************/
/*                                                            */
/* get_tile_envelope - Query the user for the image envelope  */
/*                     to be displayed. Convert the envelope  */
/*                     into tile coordinates.                 */
/*                                                            */
/*              raster_attrib <SE_RASTERATTR> - the raster    */
/*                            attribute structure.            */
/*              tileenv <TILEENV*> - the tile envelope        */
/*                                   structure.               */
/*                                                            */
/**************************************************************/

LONG get_tile_envelope (SE_RASTERATTR rasterattr,
                        TILEENV* tileenv) {

	LFLOAT cell_size,
               minx,
               miny,
               maxx,
               maxy,
               coord_offset_x,
               coord_offset_y,
               coord_tile_width,
               coord_tile_height;
	SE_ENVELOPE env;
	LONG rc, 
       	     pixel_width, 
             pixel_height, 
             pixel_offset_x, 
             pixel_offset_y, 
             num_bands, 
             tile_width, 
             tile_height;

	/* Prompt for the image window */

	printf ("Enter the coordinate extent of the image to be displayed\n");
	printf ("Usage: minx, miny, maxx, maxy\n");

	/* Fetch the coordinates from the user */

	scanf("%lf,%lf,%lf,%lf",&minx,&miny,&maxx,&maxy);

	/* make sure the min are less than the max */

	if ((minx >= maxx) || (miny >= maxy)) {
		printf("Invalid entry: The minimum X must be less than and not equal to the maximum X \n");
		printf("               The minimum Y must be less than and not equal to the maximum Y \n");
 		return(SE_FAILURE);
	}

	/* Prompt for the pyramid level */

	printf("Enter the pyramid level \n");
	printf("Usage: level\n");

	/* Fetch the pyramid level from the user */ 

	scanf("%d",&tileenv->level);

	/**************************************************************/
	/*                                                            */
	/*   Convert the real word coordinates to tile coordinates.   */
	/*                                                            */
	/**************************************************************/

	/* Get the coordinate extent of the current level. */

	rc = SE_rasterattr_get_extent_by_level (rasterattr, 
                                                &env, 
                                                &coord_offset_x, 
                                                &coord_offset_y, 
                                                tileenv->level);
	check_error ( rc, NULL, NULL, "SE_rasterattr_get_extent_by_level");

	/* Get the pixel dimensions of the current level. */

	rc = SE_rasterattr_get_image_size_by_level(rasterattr, 
                                                   &pixel_width, 
                                                   &pixel_height, 
                                                   &pixel_offset_x,
                                                   &pixel_offset_y,
                                                   &num_bands,
                                                   tileenv->level);
	check_error ( rc, NULL, NULL, "SE_rasterattr_get_image_size_by_level");

	/* make sure the user enter extent overlaps the image extent */

	if (minx > env.maxx || maxx < env.minx || miny > env.maxy || maxy < env.miny) {
    		printf("Invalid entry: The coordinate extent enter does not overlap image extent\n");
    		return(SE_FAILURE); 
	}

	/* calculate the pixel cell size in world coordinates */

	cell_size = (env.maxx - env.minx) / (pixel_width - 1);

	/* Get the tile size expressed in pixels */

	rc = SE_rasterattr_get_tile_size (rasterattr, &tile_width, &tile_height);
	check_error ( rc, NULL, NULL, "SE_rasterattr_get_tile_size");

	/* compute the real world coordinate width and height */

	coord_tile_width = cell_size * tile_width;
	coord_tile_height = cell_size * tile_height;

	/* compute the tile coordinates */

	tileenv->minx = (LONG) ((minx - (env.minx - coord_offset_x)) / coord_tile_width); 
	tileenv->maxx = (LONG) ((maxx - (env.minx - coord_offset_x)) / coord_tile_width);
	tileenv->miny = (LONG) (((env.maxy + coord_offset_y) - maxy) / coord_tile_height);
	tileenv->maxy = (LONG) (((env.maxy + coord_offset_y) - miny) / coord_tile_height);

	return(SE_SUCCESS);

} /* get_tile_envelope */