home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1997 May
/
VPR9705A.ISO
/
VPR_DATA
/
PROGRAM
/
Delphi
/
User_vcl
/
All3222
/
all.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1996-10-06
|
17KB
|
565 lines
{
Borland Delphi 2.0用のSusie32 Plug-In libraryを使ったコンポーネントALL32
Ver 2.2
by みけお (七田 隆司)
mikeo@airnet.or.jp
SGV02642@niftyserve.or.jp)
機能
このコンポーネントはビットマップファイル(*.BMP)、アイコンファイル(*.ICO)、
メタファイル(*.WMF,*.EMF)、JPEGファイル(*.JPG,*.JPE)、GIFファイル(*.GIF)、
TIFFファイル(*.TIF)、PICファイル(*.PIC)、PICT2ファイル(*.P2)、
MAGファイル(*.MAG)、PIファイル(*.PI)、XLD4ファイル(*.Q4)に対応してい
ます。
注意
これはDelphi1.0(J)では動作しません。
またOSはWindows95J、WindowsNTで動作確認しています。
減色処理をしていないので256色以下の環境では256色を超える画像を正しく表示でき
ません。
このプログラムはSusie32 Plug-in libraryを使用します。
Nifty Serve FWINMM LIB#5にSusie Plug-in libraryという名前で登録されています。
必要とするフォーマットが含まれているlibraryを取得してください。
Susie Plug-In libraryには32ビット版と16ビット版の2種類があるので注意して
下さい。
まずはじめに
必要とするファイルはIFJPEG.SPI,IFGIF.SPI,IFTIFF.SPI,IFPI.SPI,IFMAG.SPI,
IFPIC.SPI,IFPIC2.SPI,IFXLD4.SPIの8つです。
このファイルをWindowsのSystemディレクトリにコピーして下さい。(ただし決して
使わないということが予めわかっていればそのSPIファイルはコピーする必要はあり
ません。)
イメージを表示や変更させる際にこのSPIファイルがないと'IFJPEG.SPI not found'
などというダイアログが表示されます。(ただしBMP,ICO,WMF,EMFファイルの場合は
プラグインを使いませんのでこのような表示はされません。)
コンポーネントのインストールの方法
このAll.pasおよびAll.dcrを任意のディレクトリにコピーして下さい。
因みにAll.dcrファイルはコンポーネントのアイコンデータファイルです。
次に現在Delphiでプログラミング途中のものがあればメニューからファイル-全て
保存を実行してセーブしておくか、廃棄するかして下さい。廃棄するにはメニュー
からファイル-アプリケーションの新規作成を実行します。
次にメニューからコンポーネント-インストールを選択します。ここ出現するダイア
ログ上から追加ボタンを押しモジュールの追加というダイアログがでるので、参照ボタ
ンを押し、ALL.PASの場所に行きALL.PASをクリックし開くボタンを押します。
これでコンポーネントのインストールダイアログ上のインストールされたユニット
の中にallが含まれているはずです。OKボタンを押してダイアログから抜ければ
ALLが使えるようになります。
コンポーネントのアンインストールの方法
現在プログラミング途中のものがあればセーブするなり廃棄するなりしておいて下
さい。
Delphi上でコンポーネント-インストールを選択し、ダイアログ上のインストール
されたユニットの中のAllを選択し、削除ボタンを押し、OKボタンを押します。
次にAll.pas,All.dcrやwindowsのsystemディレクトリにコピーしたSusie関係のSPI
ファイルを削除します。
使用法
コンポーネントライブラリのSamplesページにAllの文字のアイコンで登録
されているはずなのでそれをフォームにおきます。
画像ファイルはString型PropertyのPictureNameの右をダブルクリックして
ダイアログ上で変更します。(設計時)
プログラムでは例えば
All1.PictureName := 'BmpFile.BMP';
All1.PictureName := 'JpegFile.JPG';
All1.PictureName := 'JpegFile.JPE';
All1.PictureName := 'GifFile.GIF';
All1.PictureName := 'TiffFile.TIF';
All1.PictureName := 'IconFile.ICO';
All1.PictureName := 'MetaFile.WMF';
All1.PictureName := 'MetaFile.EMF';
All1.PictureName := 'MagFile.MAG';
All1.PictureName := 'PicFile.PIC';
All1.PictureName := 'Pic2File.P2';
All1.PictureName := 'PiFile.PI';
All1.PictureName := 'XLD4File.Q4';
などのようにします。
Jpegファイルのみの限定仕様
特殊機能としてプレビューというのがあり1/4の大きさで高速に表示します。
これを使うにはPreview PropertyをTrueにします。
またプレビューを使ったときにもとのサイズがわからないのでOrgWidth,OrgHeight
という2つのInteger型Propertyを用意しました。これはプログラム上でリードのみ
可能です。
その他の仕様
MACバイナリを含んだファイルに対応しています。(先頭の128バイトをカット
します。)
PICファイル、PIC2ファイルについてはアスペクト比を考慮するような仕様に
変更しました。
プロパティについて
Property PictureName:String (リード・ライト可能)
表示するファイル名をフルパスネームで指定します。
PictureNameを指定した後、表示に失敗した場合はこの値は''の値に戻ります。
Property Error:Integer (リードオンリー)
PictureNameを指定した後、表示に失敗した場合には、エラー番号が入ります。
ほとんどがSusie Plug-Inと共通で、新たに追加したものは10:サポートされな
い拡張子、20:SPIファイルがないです。
最後に
TImageを親としているため通常は必要のないPictureプロパティが残っています。
このためPicture Propertyの右や画像エリアををダブルクリックするとTImageと
全く同じでBMPファイルのロードができます。(あまり意味がない)
しかしこれを使うと画像ロード後にBMPファイルとしてセーブすることができます。
これには
All1.Picture.SaveToFile('BMPファイル名.BMP');
とします。
}
unit all;
interface
uses
SysUtils, Windows, {Messages,} Classes, {Graphics,} {Controls,}
Forms, Dialogs, ExtCtrls, DsgnIntf;
type
TPictureName = String;
TPictureNameProperty = class(TStringProperty)
public
procedure Edit; override;
function GetAttributes: TPropertyAttributes; override;
end;
TAll = class(TImage)
constructor Create(AOwner: TComponent); override;
private
{ Private declarations }
FPictureName:TPictureName;
FPreView:Boolean;
FOrgWidth:Integer;
FOrgHeight:Integer;
FError:Integer;
Sort:Integer;
{0:BMP,1:JPEG,2:GIF,3:TIFF,4:PIC,5:PIC2,6:MAG,7:XLD4,8:PI}
procedure SetFile(NewFileName:TPictureName);
procedure SetPreView(NewValue:Boolean);
procedure Disp;
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
property PictureName:TPictureName read FPictureName write SetFile;
property PreView:Boolean read FPreView write SetPreView;
property OrgWidth:Integer read FOrgWidth;
property OrgHeight:Integer read FOrgHeight;
property AutoSize default True;
property Error:Integer read FError;
end;
type
TPictureInfoPtr = ^TPictureInfo;
TPictureInfo = record
Left,Top,Width,Height:LongInt;
x_density,y_density:WORD;
colorDepth:SmallInt;
hInfo:Integer;
end;
TBitmapInfoPtr = ^TBitmapInfo;
var
GetPictureInfo:
function(buf:PChar;len:Integer;flag:Integer;
pInfo:TPictureInfoPtr)
:Integer;stdcall;
GetPicture:
function(buf:PChar;len:LongInt;flag:LongInt;
pHBInfo,pHBm:PHandle;lpPrgressCallback:TFarProc;lData:LongInt)
:Integer;stdcall;
GetPreview:
function(buf:PChar;len:LongInt;flag:Integer;
pHBInfo,pHBm:PHandle;lpPrgressCallback:TFarProc;lData:LongInt)
:Integer;stdcall;
procedure Register;
implementation
{$R-}
constructor TAll.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
AutoSize := True;
FPictureName := '';
end;
procedure TAll.SetFile(NewFileName:TPictureName);
begin
FPictureName := NewFileName;
if NewFileName = '' then
begin
FError := 0;
Picture.Bitmap := nil;
Exit;
end;
Picture.Bitmap := nil;
Disp;
end;
procedure TAll.Disp;
var
Ret:Integer;
hBData:THandle;
hBInfo:THandle;
pBData:Pointer;
pBInfo:Pointer;
Info:TPictureInfo;
Bitmap:TBitmap;
Bitmap2:TBitmap;
FName:array[0..255] of Char;
h:THandle;
pp:Pointer;
Err:String;
LogPalette:PLogPalette;
NumColor:Integer;
i:Integer;
hPal,hOldPal:THandle;
Bits:Integer;
Flag256:Boolean;
Base:TWinControl;
s:String;
MacBinOffset:Integer;
begin
FError := 0;
s := UpperCase(ExtractFileExt(FPictureName));
if (s = '.BMP') or (s = '.ICO') or (s = '.WMF') or (s = '.EMF') then
begin
Sort := 0;
try
Picture.LoadFromFile(FPictureName);
except
on EInvalidGraphic do
begin
FError := 2;
Err := '未知のフォーマット';
ShowMessage(Format('%s [%s]',[Err,FPictureName]));
FPictureName := '';
Exit;
end;
end;
end
else if (s = '.JPG') or (s = '.JPE') then
begin
Sort := 1;
h := LoadLibrary('ifjpeg.spi');
end
else if s = '.GIF' then
begin
Sort := 2;
h := LoadLibrary('ifgif.spi');
end
else if s = '.TIF' then
begin
Sort := 3;
h := LoadLibrary('iftiff.spi');
end
else if s = '.PIC' then
begin
Sort := 4;
h := LoadLibrary('ifpic.spi');
end
else if s = '.P2' then
begin
Sort := 5;
h := LoadLibrary('ifpic2.spi');
end
else if s = '.MAG' then
begin
Sort := 6;
h := LoadLibrary('ifmag.spi');
end
else if s = '.Q4' then
begin
Sort := 7;
h := LoadLibrary('ifxld4.spi');
end
else if s = '.PI' then
begin
Sort := 8;
h := LoadLibrary('ifpi.spi');
end
else
begin
FError := 10; {サポートされない拡張子}
Err := 'サポートされない拡張子';
ShowMessage(Format('%s [%s]',[Err,FPictureName]));
FPictureName := '';
FPreView := False;
FOrgWidth := 0;
FOrgHeight := 0;
Exit;
end;
if (Sort<>1) then { not Jpeg }
begin
FPreView := False;
FOrgWidth := 0;
FOrgHeight := 0;
end;
if Sort = 0 then Exit;
case Sort of
1:s:='JPG';
2:s:='GIF';
3:s:='TIFF';
4:s:='PIC';
5:s:='PIC2';
6:s:='MAG';
7:s:='XLD4';
8:s:='PI';
end;
if h>HINSTANCE_ERROR then
begin
if (Sort = 1) or (Sort = 4) or (Sort = 5) then {Jpeg or PIC or PIC2}
begin
pp := GetProcAddress(h,'GetPictureInfo');
if pp<>nil then
@GetPictureInfo := pp;
pp := GetProcAddress(h,'GetPreview');
if pp<>nil then
@GetPreview := pp;
end;
pp := GetProcAddress(h,'GetPicture');
if pp<>nil then
@GetPicture := pp;
end
else
begin
Err := 'IF'+s+'.SPI not found';
ShowMessage(Format('%s [%s]',[Err,FPictureName]));
FError := 20; {SPIファイルがない}
FPictureName := '';
FreeLibrary(h);
Exit;
end;
StrPCopy(FName,FPictureName);
if Sort = 1 then
begin
MacBinOffset := 0;
Ret := GetPictureInfo(FName,0,0,@Info);
if Ret <> 0 then
begin
Ret := GetPictureInfo(FName,128,0,@Info);
if Ret = 0 then
MacBinOffset := 128;
end;
if Ret <> 0 then
begin
FError := Ret;
case Ret of
2:Err := '未知のフォーマット';
3:Err := 'データが壊れている';
4:Err := 'メモリーが確保出来ない';
5:Err := 'メモリーエラー';
6:Err := 'ファイルリードエラー';
else
Err := 'その他のエラー';
end;
ShowMessage(Format('%s [%s]',[Err,FPictureName]));
FPictureName := '';
FreeLibrary(h);
Exit;
end;
Bitmap := TBitmap.Create;
FOrgWidth := Info.Width;
FOrgHeight := Info.Height;
if FPreView then
begin
Bitmap.Width := Info.Width div 4;
if (Info.Width and 3)<>0 then Bitmap.Width := Bitmap.Width+1;
Bitmap.Height := Info.Height div 4;
if (Info.Height and 3)<>0 then Bitmap.Height := Bitmap.Height+1;
end
else
begin
Bitmap.Width := Info.Width;
Bitmap.Height := Info.Height;
end;
GlobalFree(Info.hInfo);
if FPreView then
Ret := GetPreview(@FName, MacBinOffset, 0, @hBInfo, @hBData, nil, 0)
else
Ret := GetPicture(@FName, MacBinOffset, 0, @hBInfo, @hBData, nil, 0);
pBData := GlobalLock(hBData);
pBInfo := GlobalLock(hBInfo);
SetDIBits(Canvas.Handle,Bitmap.Handle,
0,TBitmapInfoPtr(pBInfo)^.bmiHeader.biHeight,pBData,TBitmapInfoPtr(pBInfo)^, DIB_RGB_COLORS);
GlobalUnlock(hBData);
GlobalUnlock(hBInfo);
GlobalFree(hBData);
GlobalFree(hBInfo);
Picture.Graphic := Bitmap;
Bitmap.Free;
FreeLibrary(h);
end
else
begin
Ret := GetPicture(@FName, 0, 0, @hBInfo, @hBData, nil, 0);
if Ret = 2 then
Ret := GetPicture(@FName, 128, 0, @hBInfo, @hBData, nil, 0);
if Ret <> 0 then
begin
FError := Ret;
case Ret of
2:Err := '未知のフォーマット';
3:Err := 'データが壊れている';
4:Err := 'メモリーが確保出来ない';
5:Err := 'メモリーエラー';
6:Err := 'ファイルリードエラー';
else
Err := 'その他のエラー';
end;
ShowMessage(Format('%s [%s]',[Err,FPictureName]));
FPictureName := '';
FreeLibrary(h);
Exit;
end;
pBData := GlobalLock(hBData);
pBInfo := GlobalLock(hBInfo);
Bitmap := TBitmap.Create;
Bitmap.Width := TBitmapInfoPtr(pBInfo)^.bmiHeader.biWidth;
Bitmap.Height := TBitmapInfoPtr(pBInfo)^.bmiHeader.biHeight;
Bits := TBitmapInfoPtr(pBInfo)^.bmiHeader.biBitCount;
Flag256:=False;
h := GetDC(0);
if (Bits<=8) and (GetDeviceCaps(h,BitsPixel)=8) then
Flag256 := True;
ReleaseDC(0,h);
if Flag256 then
begin
NumColor := TBitmapInfoPtr(pBInfo)^.bmiHeader.biClrUsed;
if NumColor = 0 then
begin
if Bits = 8 then NumColor := 256
else if Bits = 4 then NumColor := 16
else if Bits = 1 then NumColor := 2;
end;
LogPalette := AllocMem(4+NumColor*4);
with LogPalette^ do
begin
palVersion := $0300;
palNumEntries := NumColor;
for i:=0 to NumColor-1 do
begin
palPalEntry[i].peRed := TBitmapInfoPtr(pBInfo)^.bmiColors[i].rgbRed;
palPalEntry[i].peGreen := TBitmapInfoPtr(pBInfo)^.bmiColors[i].rgbGreen;
palPalEntry[i].peBlue := TBitmapInfoPtr(pBInfo)^.bmiColors[i].rgbBlue;
PalPalEntry[i].peFlags := 0;
end;
end;
hPal := CreatePalette(LogPalette^);
hOldPal := SelectPalette(Canvas.Handle, hPal, false);
RealizePalette(Canvas.Handle);
Bitmap.Palette := hPal;
FreeMem(LogPalette);
end;
SetDIBits(Canvas.Handle,Bitmap.Handle,
0,TBitmapInfoPtr(pBInfo)^.bmiHeader.biHeight,pBData,TBitmapInfoPtr(pBInfo)^
,DIB_RGB_COLORS);
GlobalUnlock(hBData);
GlobalUnlock(hBInfo);
GlobalFree(hBData);
GlobalFree(hBInfo);
Picture.Graphic := Bitmap;
if (Sort = 4) or (Sort = 5) then {PIC or PIC2 file}
begin
Ret := GetPictureInfo(FName,0,0,@Info);
if (Info.x_density<>1) or (Info.y_density<>1) then
begin
Bitmap2 := TBitmap.Create;
if Info.x_density < Info.y_density then
begin
Bitmap2.Height := Bitmap.Height;
Bitmap2.Width := Bitmap.Width * Info.y_density div Info.x_density;
end
else
begin
Bitmap2.Width := Bitmap.Width;
Bitmap2.Height := Bitmap.Height * Info.x_density div Info.y_density;
end;
Bitmap2.Canvas.StretchDraw(Rect(0,0,Bitmap2.Width,Bitmap2.Height),Bitmap);
Bitmap2.Palette := Bitmap.Palette;
Picture.Graphic := Bitmap2;
Bitmap2.Free;
end;
end;
Bitmap.Free;
if Flag256 then
begin
base := Parent;
while not((base is TForm) or (base=nil)) do
base := base.Parent;
if base is TForm then
begin
SelectPalette(TForm(base).Canvas.Handle, hPal, false);
RealizePalette(TForm(base).Canvas.Handle);
SelectPalette(TForm(base).Canvas.Handle,hOldPal, false);
end;
end;
FreeLibrary(h);
end;
end;
procedure TAll.SetPreView(NewValue:Boolean);
begin
FPreView := NewValue;
if FPictureName <> '' then
Disp;
end;
procedure Register;
begin
RegisterPropertyEditor(TypeInfo(TPictureName), TAll, 'PictureName', TPictureNameProperty);
RegisterComponents('Samples', [TAll]);
end;
function TPictureNameProperty.GetAttributes: TPropertyAttributes;
begin
Result := [paDialog];
end;
procedure TPictureNameProperty.Edit;
var
OpenDialog1:TOpenDialog;
begin
OpenDialog1 := TOpenDialog.Create(Application);
if Length(GetStrValue)<>0 then
OpenDialog1.InitialDir := ExtractFilePath(GetStrValue);
OpenDialog1.Filter := 'All files|*.BMP;*.JPG;*.JPE;*.GIF;*.TIF;'+
'*.ICO;*.WMF;*.EMF;*.PIC;*.P2;*.MAG;*.Q4;*.PI'+
'|Bmp file(*.BMP)|*.BMP'+
'|Jpeg file(*.JPG;*.JPE)|*.JPG;*.JPE'+
'|Gif file(*.GIF)|*.GIF'+
'|Tiff file(*.TIF)|*.TIF'+
'|PIC file(*.PIC)|*.PIC'+
'|PIC2 file(*.P2)|*.P2'+
'|MAG file(*.MAG)|*.MAG'+
'|XLD4 file(*.Q4)|*.Q4'+
'|PI file(*.PI)|*.PI'+
'|ICON file(*.ICO)|*.ICO'+
'|Meta file(*.WMF;*.EMF)|*.WMF;*.EMF';
OpenDialog1.FileName := GetStrValue;
if OpenDialog1.Execute then
begin
SetStrValue(OpenDialog1.FileName);
end;
end;
end.