/**************************  carga_imagen_campo  ****************************
	Función para efectuar la carga desde disco de una imagen del campo.
	Se le pasan a esta función los siguientes argumentos:
		nombre: nombre del fichero que quiero cargar.
		buffer: le paso el buffer donde quiero almacenar la imagen a cargar.
				Esto define el tamaño del mismo.
	Esta función únicamente carga la imagen en el buffer seleccionado, no la
	muestra en el display.
****************************************************************************/
int carga_imagen_campo(char *nombre, MIL_ID buffer)
{
	FILE *fich;
#if !defined (__BORLANDC__)
	char  mensaje[1024];
    char *fn = "carga_imagen_campo";
#endif

	/*	Compruebo que la imagen solicitada (el archivo) existe. */

	fich = fopen(nombre, "rb");
	if (fich == NULL)  {
#if !defined (__BORLANDC__)
		sprintf(mensaje, "No puedo leer la imagen %s", nombre);
		printf(fn, mensaje);
#endif
		return -1;
	}
	fclose(fich);

	//	Si el archivo existe, lo importo, almacenándolo en el buffer solicitado.
	MbufImport(nombre, M_DEFAULT, M_LOAD, M_NULL, &buffer);

	return  0;
}
// Se reserva memoria, se carga y se presenta la imagen "nomFich"
// Se presupone que no hay otra imagen cargada con el mismo numero de filtro ("numFiltro")
//  m_control es el control al dialogo
//  bMostrar indica si debe de mostrarse la imagen en pantalla o solo cargar la imagen en memoria
// Se inicializa M_overlay_normal
// Se deja el zoom a 0.5
bool ControlImagenes::CargarImagen(char* nomFich, CStatic&	m_control, int numFiltro, bool bMostrar)
{

	// Comprobaciones
	struct stat stFileInfo;
	if (stat(nomFich,&stFileInfo) != 0)
		return false;
    if (m_Milimagen[numFiltro-1] != NULL)
        return false;

    long lSizeX, lSizeY;
    MbufDiskInquire(nomFich,M_SIZE_X, &lSizeX);
    MbufDiskInquire(nomFich,M_SIZE_Y, &lSizeY);
    MbufAlloc2d(M_DEFAULT_HOST, lSizeX,lSizeY, 16 + M_UNSIGNED, M_IMAGE + M_DISP + M_PROC, &m_Milimagen[numFiltro-1]);
    if (MbufImport(nomFich,M_TIFF,M_LOAD,M_NULL,&m_Milimagen[numFiltro-1]) == M_NULL)
//    if (MbufImport(nomFich,M_TIFF,M_RESTORE,M_DEFAULT_HOST,&m_Milimagen[numFiltro-1]) == M_NULL)
        return false;
    if (m_Milimagen[numFiltro-1] == NULL)
        return false;

    m_numImagenes++;

	if (bMostrar)
	{
		MdispSelectWindow(m_Mildisplay,m_Milimagen[numFiltro-1],m_control);
		// Inicializar Overlay la primera vez que se carga una imagen
		// Esto se hace aqui porque no funciona si no hay un buffer asociado con el display
		// ha de usarse la variable global M_overlay_normal para que funcionen las funciones graficas
		if (M_overlay_normal == M_NULL)
		{
			MdispInquire(m_Mildisplay, M_OVERLAY_ID, &M_overlay_normal);

			// Relleno el buffer overlay con el color TRANSPARENTE
//			MbufClear(M_overlay_normal, TRANSPARENTE );
		}
        // TRUCO para evitar que los rectangulos se pinten con un reborde negro. Pero la carga de imagenes es mas lenta
        // Se deja el zoom a 0.5
        // Los rectangulos se pintan con un reborde negro cuando el zoom esta entre 0.5 y 1 en MIL 8
        // por razones desconocidas. Este truco funciona, pero tampoco se sabe porque
//        MdispZoom(m_Mildisplay, 0.5, 0.5);
        //En la funcion llamante, se debe actualizar correctamente el zoom
	}
    // Solo una vez cada vez que se cargan todas las imagenes
    if (numFiltro == 2)
    {
        // Recuperamos las dimensiones de la imagen
        MbufInquire(m_Milimagen[1],M_SIZE_X,&m_nAnchoImagen);
        MbufInquire(m_Milimagen[1],M_SIZE_Y,&m_nAltoImagen);
	}

    return true;
}
bool MdispGtkView::newDoc()
   {
    // Set buffer attributes
   if(((MdispGtkApp*)dispGtkApp())->m_numberOfDigitizer)
      {
      m_bufferAttributes=M_IMAGE+M_DISP+M_GRAB+M_PROC;
      m_imageSizeX = ((MdispGtkApp*)dispGtkApp())->m_digitizerSizeX;
      m_imageSizeY = ((MdispGtkApp*)dispGtkApp())->m_digitizerSizeY;
      m_NbBands    = ((MdispGtkApp*)dispGtkApp())->m_digitizerNbBands;

      // Allocate a buffer [CALL TO MIL]
      MbufAllocColor(((MdispGtkApp*)dispGtkApp())->m_MilSystem,
                     m_NbBands,
                     m_imageSizeX,
                     m_imageSizeY,
                     M_DEF_IMAGE_TYPE,
                     m_bufferAttributes,
                     &m_MilImage);

      // Clear the buffer [CALL TO MIL] 
      MbufClear(m_MilImage,M_COLOR_BLACK);
      }
   else
      {
      //Import image in buffer [CALL TO MIL]
      MbufImport(IMAGE_FILE,M_DEFAULT,M_RESTORE,((MdispGtkApp*)dispGtkApp())->m_MilSystem,&m_MilImage);

      // Set SizeX and SizeY variable to the size of the buffer [CALL TO MIL]
      if (m_MilImage) 
         {
         m_imageSizeX   = MbufInquire(m_MilImage, M_SIZE_X, M_NULL);
         m_imageSizeY   = MbufInquire(m_MilImage, M_SIZE_Y, M_NULL);
         m_NbBands      = MbufInquire(m_MilImage, M_SIZE_BAND, M_NULL);
         }
      }

   UpdateContentSize();

   // If not able to allocate a buffer, do not create a new document
   if(!m_MilImage)
      return false;

   Initialize();

   return true;
   }
bool MdispGtkView::load(const char *filename)
   {
   //Import image in buffer [CALL TO MIL]
   MbufImport(filename,M_DEFAULT,M_RESTORE,((MdispGtkApp*)dispGtkApp())->m_MilSystem,&m_MilImage);

   // Set SizeX and SizeY variable to the size of the buffer [CALL TO MIL]
   if (m_MilImage)
      {
      Initialize();
      m_imageSizeX = MbufInquire(m_MilImage,M_SIZE_X,M_NULL);
      m_imageSizeY = MbufInquire(m_MilImage,M_SIZE_Y,M_NULL);
      m_filename = g_strdup(g_path_get_basename(filename));
      UpdateContentSize();
      return true;
      }
   else
      {
      return false;
      }
   }