library cwconv;

uses
  SysUtils,
  Classes,
  windows,
  Graphics,
  convinf in 'convinf.pas';

{$R *.res}

(*画像をポインタの形に置き換える*)
function createImage(var bitmap:TBitmap):PCardinal;
var
 x:Integer;
 car:PCardinal;
 buffer:Pointer;
 stream:TMemoryStream;
begin
 stream:=TMemoryStream.Create();

 try
  bitmap.SaveToStream(stream);
  stream.Position:=0;
  x:=stream.Size;
  car:=PCardinal(GlobalAlloc(GPTR,x));
  buffer:=Pointer(car);
  stream.ReadBuffer(buffer^,x);
 finally
  stream.Free;
  bitmap.Free;
 end;

  result:=car;

end;

(*カードワースからの要求を受け取り、画像をポインタで返す*)
function GetImage(fileName:PChar;var data:PCardinal):boolean;stdcall;
var
 bitmap:TBitmap;
 director:TImageDirector;
begin

 bitmap:=TBitmap.Create();
 bitmap.PixelFormat:=pf32bit;

 director:=TImageDirector.Create();
 director.basedir:=ExtractFileDir(fileName);
 director.loadImage(fileName,bitmap);
 if(bitmap.Width*bitmap.Height<>0)then
 begin
  bitmap.Transparent:=false;
  data:=createImage(bitmap);
  result:=true;
 end
 else
  result:=false;

 director.Free();

end;
(*エディター用の読み込み・微妙に早い？*)
function GotImage(fileName:PChar;var data:PCardinal):boolean;stdcall;
var
 bitmap:TBitmap;
 director:TImageDirector;
begin

 bitmap:=TBitmap.Create();
 bitmap.PixelFormat:=pf32bit;

 director:=TImageDirector.Create();
 director.basedir:=ExtractFileDir(fileName);
 director.animation:=false;
 director.isSound:=false;
 director.isSave:=false;
 director.searched:=true;
 director.visibleDialog:=true;
 director.loadImage(fileName,bitmap);

 if(bitmap.Width*bitmap.Height<>0)then
 begin
  bitmap.Transparent:=false;
  data:=createImage(bitmap);
  result:=true;
 end
 else
  result:=false;

 director.Free();

end;

exports
GetImage name 'GetImage',
GotImage name 'GotImage';

//begin
end.





