unit Unittest;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,jpeg, StdCtrls, Buttons, ExtCtrls;

type
  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    Image1: TImage;
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private 宣言 }
  public
    { Public 宣言 }
  end;

var
  Form1: TForm1;

implementation

{$R *.res}

function GetJpegImage(fileName:PChar;var data:HGLOBAL):boolean;
var
 jImage:TJpegImage;
 bImage:TBitmap;
 pInt,pp:PInteger;
 i:Integer;

begin
 jImage:=TJpegImage.Create();
 bImage:=TBitmap.Create();
 JImage.Performance:=jpBestQuality;
 JImage.PixelFormat:=jf24Bit;
 //jImage.DIBNeeded();
 try
  {jpeg画像読み取り}
  JImage.LoadFromFile(fileName);

  {bmp画像にコンバート}
  bImage.Width:=jImage.Width;
  bImage.Height:=jImage.Height;
  bImage.Canvas.Draw(0,0,jImage);

  {バッファにメモリー割り当て}
  pInt:=PInteger(GlobalAlloc(GPTR,bImage.width*bImage.height*4+8));
  pp:=pInt;

  pp^:=jImage.width;
  inc(pp,1);
  pp^:=jImage.Height;
  inc(pp,1);

  for i:=0 to bImage.height-1 do
  begin
   CopyMemory(pp,bImage.scanLine[(bImage.height-1)-i],bImage.width*4);
   inc(pp,bImage.width);
  end;

  data:=cardinal(pInt);
  result:=true;

 except
  jImage.Free();
  bImage.Free();
  result:=false;
 end;

end;

function GetImage(fileName:PChar;var data:HGLOBAL):boolean;stdcall;
begin
 //windows.MessageBox(0,fileName,fileName,MB_OK);
 result:=getJpegImage(fileName,data);
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
 data:HGLOBAL;
 stream:TMemorystream;
 buffer:Pointer;
begin
 getImage('K:\CardWirth\Scenario\復讐のゴルダイン',data);
 stream:=TMemoryStream.Create();
 stream.WriteBuffer(buffer,GlobalSize(data));
 GlobalUnlock( data );
 GlobalFree( data );
 Image1.Picture.Bitmap.LoadFromStream(stream);
 stream.Free();
end;

end.
 