//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "convertir.h"
#include "FileCtrl.hpp"
#include "jpeg.hpp"
#include "GIFImg.hpp"
#include "pngimage.hpp"
#include "StrUtils.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm15 *Form15;

String carpeta;
//---------------------------------------------------------------------------
__fastcall TForm15::TForm15(TComponent* Owner)
	: TForm(Owner)
{
 carpeta="vacia";
}
//---------------------------------------------------------------------------
void __fastcall TForm15::Button4Click(TObject *Sender)
{

 carpeta=GetCurrentDir();

 if (SelectDirectory(carpeta, TSelectDirOpts() << sdAllowCreate << sdPerformCreate << sdPrompt,NULL)) {
   ShowMessage("Los archivos convertidos se guardaran en: "+carpeta);
   procesar->Enabled=true;
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm15::Button1Click(TObject *Sender)
{
 int i;

 if (abrir->Execute()) {
  for (i = 0; i < abrir->Files->Count; i++) {
    lista->Items->Add(abrir->Files->Strings[i]);
  }

 }
}
//---------------------------------------------------------------------------
void __fastcall TForm15::procesarClick(TObject *Sender)
{
	int i;
	String extension;
	TJPEGImage* jpg;
	TPngImage* png;
	TGIFImage* gif;
	TPicture* bmp;

	bmp=new TPicture;
	gif=new TGIFImage;
	jpg=new TJPEGImage;
	png=new TPngImage;

	for (i = 0; i < lista->Items->Count; i++) {
		extension=ExtractFileExt(lista->Items->Strings[i]).UpperCase();
		if (extension==".BMP") {
			bmp->LoadFromFile(lista->Items->Strings[i]);
		} else
		if (extension==".GIF") {
			gif->LoadFromFile(lista->Items->Strings[i]);
			bmp->Bitmap->Assign(gif);
		} else
		if (extension==".PNG") {
			png->LoadFromFile(lista->Items->Strings[i]);
			bmp->Bitmap->Assign(png);
		} else
		if (extension==".JPG") {
			jpg->LoadFromFile(lista->Items->Strings[i]);
			bmp->Bitmap->Assign(jpg);
		}

		switch (formato->ItemIndex) {
			case 0:bmp->SaveToFile(carpeta+"/"+ExtractFileName(lista->Items->Strings[i]).SubString(0,Pos(".",ExtractFileName(lista->Items->Strings[i]))-1)+".bmp");break;
			case 1:jpg->Assign(bmp->Bitmap);jpg->SaveToFile(carpeta+"/"+ExtractFileName(lista->Items->Strings[i]).SubString(0,Pos(".",ExtractFileName(lista->Items->Strings[i]))-1)+".jpg");break;
			case 2:png->Assign(bmp->Bitmap);png->SaveToFile(carpeta+"/"+ExtractFileName(lista->Items->Strings[i]).SubString(0,Pos(".",ExtractFileName(lista->Items->Strings[i]))-1)+".png");break;
			case 3:gif->Assign(bmp->Bitmap);gif->SaveToFile(carpeta+"/"+ExtractFileName(lista->Items->Strings[i]).SubString(0,Pos(".",ExtractFileName(lista->Items->Strings[i]))-1)+".gif");break;
		default:
			;
		}
		ponpalo(i);
	}
	bmp->Free();
	gif->Free();
	png->Free();
	jpg->Free();
    ponpalo(100);
	ShowMessage("Conversion finalizada");
}
//---------------------------------------------------------------------------
void __fastcall TForm15::ponpalo(int pos){
   palo->Position=(pos*100)/lista->Items->Count;
}
void __fastcall TForm15::Button3Click(TObject *Sender)
{
  Form15->Hide();
}
//---------------------------------------------------------------------------
