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

#include <vcl.h>
#include <windows.h>
#define min(a, b)  (((a) < (b)) ? (a) : (b)) 
#pragma hdrstop

#include "tsplash.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TSplash *Splash;
//---------------------------------------------------------------------------
__fastcall TSplash::TSplash(TComponent* Owner)
        : TForm(Owner)
{
  pressKeyToClose = false;
}
//---------------------------------------------------------------------------
void __fastcall TSplash::GetNextSpan(PByteArray row, int SpanLeft, int *SpanRight, TColor *SpanColor, int y)
{
  int column;
  TColor TempColor;

  column = SpanLeft;
  TempColor = MaskColor(SpanLeft,y);
  while (  column <= ImageMap->Width && TempColor == MaskColor(column,y) ) column = column + 1;;

  *SpanRight = column - 1;
  *SpanColor = TempColor;
}
//---------------------------------------------------------------------------
TColor __fastcall TSplash::MaskColor(int x,int y)
{
  if ( ImageMap->Canvas->Pixels[x][y] == 0x00FF00FF ) return clFuchsia; else return clBlack;
}
//---------------------------------------------------------------------------
void __fastcall TSplash::FormCreate(TObject *Sender)
{
  int y;
  PByteArray row;
  HRGN YRgn;
  HRGN SpanRgn;
  int SpanLeft;
  int SpanRight;
  TColor SpanColor;

  Canvas->Brush->Style = bsClear;
  ImageMap = Image1->Picture->Bitmap;
  WindowRgn = CreateRectRgn(0, 0, 0, 0);
  for (y=0;y<ImageMap->Height;y++) {
    YRgn = CreateRectRgn(0, y, 0, y);
//    row = (PByteArray)ImageMap->ScanLine[y];

    SpanLeft = 0;
    SpanRight = 0;
    do {
      GetNextSpan(row, SpanLeft, &SpanRight, &SpanColor,y);
      if ( SpanColor != clFuchsia ) {
        SpanRgn = CreateRectRgn(SpanLeft, y, SpanRight + 1, y + 1);
        CombineRgn(YRgn, YRgn, SpanRgn, RGN_OR);
        DeleteObject(SpanRgn);
      }
      SpanLeft = SpanRight + 1;
    } while ( SpanRight < (ImageMap->Width-1));
    CombineRgn(WindowRgn, WindowRgn, YRgn, RGN_OR);
    DeleteObject(YRgn);
  }
  SetWindowRgn(Handle,WindowRgn,true);
  DeleteObject(WindowRgn);
}
//---------------------------------------------------------------------------

void __fastcall TSplash::FormPaint(TObject *Sender)
{
  Canvas->Draw(0, 0, ImageMap);
}
//---------------------------------------------------------------------------



void __fastcall TSplash::Image1Click(TObject *Sender)
{
  if (pressKeyToClose) Close();        
        
}
//---------------------------------------------------------------------------

