📄 jvpcx.pas
字号:
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: JvPcx.PAS, released on 2001-02-28.
The Initial Developer of the Original Code is S閎astien Buysse [sbuysse att buypin dott com]
Portions created by S閎astien Buysse are Copyright (C) 2001 S閎astien Buysse.
All Rights Reserved.
Contributor(s): Michael Beck [mbeck att bigfoot dott com].
Andreas Hausladen [Andreas dott Hausladen att gmx dott de] (complete rewrite)
You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.sourceforge.net
Known Issues:
-----------------------------------------------------------------------------}
// $Id: JvPcx.pas,v 1.24 2005/02/17 10:20:45 marquardt Exp $
unit JvPcx;
{$I jvcl.inc}
interface
uses
{$IFDEF UNITVERSIONING}
JclUnitVersioning,
{$ENDIF UNITVERSIONING}
{$IFDEF MSWINDOWS}
Windows,
{$ENDIF MSWINDOWS}
{$IFDEF UNIX}
QWindows,
{$ENDIF UNIX}
Graphics, Controls, Forms,
{$IFDEF VisualCLX}
Qt,
{$ENDIF VisualCLX}
SysUtils, Classes,
JvTypes, JvJCLUtils;
type
EPcxError = class(EJVCLException);
TJvPcx = class(TBitmap)
public
procedure LoadFromResourceName(Instance: THandle; const ResName: string; ResType: PChar);
{$IFDEF MSWINDOWS}
procedure LoadFromResourceID(Instance: THandle; ResID: Integer; ResType: PChar);
{$ENDIF MSWINDOWS}
procedure LoadFromStream(Stream: TStream); override;
procedure SaveToStream(Stream: TStream); override;
end;
{$IFDEF UNITVERSIONING}
const
UnitVersioning: TUnitVersionInfo = (
RCSfile: '$RCSfile: JvPcx.pas,v $';
Revision: '$Revision: 1.24 $';
Date: '$Date: 2005/02/17 10:20:45 $';
LogPath: 'JVCL\run'
);
{$ENDIF UNITVERSIONING}
implementation
uses
JvResources;
procedure TJvPcx.LoadFromResourceName(Instance: THandle;
const ResName: string; ResType: PChar);
var
Stream: TStream;
begin
if ResType = RT_BITMAP then
inherited LoadFromResourceName(Instance, ResName)
else
begin
Stream := TResourceStream.Create(Instance, ResName, ResType);
try
LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
end;
{$IFDEF MSWINDOWS}
procedure TJvPcx.LoadFromResourceID(Instance: THandle; ResID: Integer;
ResType: PChar);
var
Stream: TStream;
begin
if ResType = RT_BITMAP then
inherited LoadFromResourceID(Instance, ResID)
else
begin
Stream := TResourceStream.CreateFromID(Instance, ResID, ResType);
try
LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
end;
{$ENDIF MSWINDOWS}
type
PPcxPalette = ^TPcxPalette;
TPcxPalette = packed record
Red: Byte;
Green: Byte;
Blue: Byte;
end;
PPcxPaletteArray = ^TPcxPaletteArray;
TPcxPaletteArray = array [0..255] of TPcxPalette;
TPcxPalette256 = packed record
Id: Byte; // $0C
Items: array [0..255] of TPcxPalette;
end;
TPcxHeader = packed record
Id: Byte; // $0A
Version: Byte; // 5 = 3.0
Compressed: Boolean;
Bpp: Byte;
x0, y0: Word;
x1, y1: Word;
dpiX: Word;
dpiY: Word;
Palette16: array [0..15] of TPcxPalette;
Reserved1: Byte;
Planes: Byte;
BytesPerLine: Word;
PaletteType: Word; // 1: color or s/w 2: grayscaled
ScreenWidth: Word; // 0
ScreenHeight: Word; // 0
Reserved2: array [0..53] of Byte;
end;
{$IFDEF VisualCLX}
const
pf4bit = pf8bit;
pf24bit = pf32bit;
PixelFormatMap: array [pf1bit..pf32bit] of Integer = (1, 8, 16, 32);
type
TPrivateBitmap = class(TGraphic)
protected
{$IF defined(LINUX) or defined(COMPILER7_UP) or declared(PatchedVCLX)}
FPixelFormat: TPixelFormat;
FTransparentMode: TTransparentMode;
{$IFEND}
FImage: QImageH;
end;
function GetBitmapImage(Bitmap: TBitmap): QImageH;
begin
if Assigned(Bitmap) then
Result := TPrivateBitmap(Bitmap).FImage
else
Result := nil;
end;
{$ENDIF VisualCLX}
procedure ReadPalette(Bitmap: TJvPcx; ColorNum: Integer; PcxPalette: PPcxPalette);
var
I: Integer;
P: PPcxPaletteArray;
{$IFDEF VCL}
RPal: TMaxLogPalette;
{$ENDIF VCL}
{$IFDEF VisualCLX}
ColorTbl: PRGBQuadArray;
{$ENDIF VisualCLX}
begin
P := PPcxPaletteArray(PcxPalette);
{$IFDEF VCL}
RPal.palVersion := $300;
RPal.palNumEntries := ColorNum;
for I := 0 to ColorNum - 1 do
begin
RPal.palPalEntry[I].peRed := P[I].Red;
RPal.palPalEntry[I].peGreen := P[I].Green;
RPal.palPalEntry[I].peBlue := P[I].Blue;
RPal.palPalEntry[I].peFlags := 0;
end;
Bitmap.Palette := CreatePalette(PLogPalette(@RPal)^);
Bitmap.PaletteModified := True;
{$ENDIF VCL}
{$IFDEF VisualCLX}
Bitmap.ImageNeeded;
QImage_setNumColors(GetBitmapImage(Bitmap), ColorNum);
ColorTbl := Bitmap.ColorTable;
for I := 0 to ColorNum - 1 do
begin
with ColorTbl[I] do
begin
rgbRed := P[I].Red;
rgbGreen := P[I].Green;
rgbBlue := P[I].Blue;
rgbReserved := 0;
end;
end;
{$ENDIF VisualCLX}
end;
procedure WritePalette(Bitmap: TJvPcx; ColorNum: Integer; PcxPalette: PPcxPalette);
var
I: Integer;
P: PPcxPaletteArray;
{$IFDEF VCL}
RPal: array [0..256] of TPaletteEntry;
{$ENDIF VCL}
{$IFDEF VisualCLX}
ColorTbl: PRGBQuadArray;
{$ENDIF VisualCLX}
begin
P := PPcxPaletteArray(PcxPalette);
FillChar(P[0], ColorNum * SizeOf(TPcxPalette), 0);
{$IFDEF VCL}
if Bitmap.Palette <> 0 then
begin
GetPaletteEntries(Bitmap.Palette, 0, ColorNum, RPal);
for I := 0 to ColorNum - 1 do
begin
P[I].Red := RPal[I].peRed;
P[I].Green := RPal[I].peGreen;
P[I].Blue := RPal[I].peBlue;
end;
end;
{$ENDIF VCL}
{$IFDEF VisualCLX}
Bitmap.ImageNeeded;
if ColorNum > QImage_numColors(GetBitmapImage(Bitmap)) then
ColorNum := QImage_numColors(GetBitmapImage(Bitmap));
ColorTbl := Bitmap.ColorTable;
for I := 0 to ColorNum - 1 do
with ColorTbl[I] do
begin
P[I].Red := rgbRed;
P[I].Green := rgbGreen;
P[I].Blue := rgbBlue;
end;
{$ENDIF VisualCLX}
end;
procedure TJvPcx.LoadFromStream(Stream: TStream);
var
Header: TPcxHeader;
BytesRead, BytesPerRasterLine: Integer;
Decompressed: TMemoryStream;
ByteLine: PByteArray;
Line: PJvRGBArray;
Palette256: TPcxPalette256;
Buffer: array [0..MaxPixelCount] of Byte;
Buffer2, Buffer3, Buffer4: PByteArray; // position in Buffer
B: Byte;
ByteNum, BitNum: Integer;
X, Y: Integer;
begin
Width := 0;
Height := 0;
{$IFDEF VCL}
Palette := 0;
IgnorePalette := False;
{$ENDIF VCL}
Monochrome := False;
BytesRead := Stream.Read(Header, SizeOf(Header));
// is it a valid header
if (BytesRead <> SizeOf(Header)) or (Header.Id <> $0A) or
(Header.BytesPerLine mod 2 = 1) then // BytesPerLine must be even
raise EPcxError.CreateRes(@RsEPcxInvalid);
// set pixel format before resizing the bitmap to reduce bitmap reallocations
case Header.Bpp of
1:
case Header.Planes of
1:
begin
PixelFormat := pf1bit;
Monochrome := True;
{$IFDEF VCL}
IgnorePalette := True;
{$ENDIF VCL}
end;
4:
PixelFormat := pf4bit; // VisualCLX: redirected const
else
raise EPcxError.CreateRes(@RsEPcxUnknownFormat);
end;
8:
case Header.Planes of
1:
PixelFormat := pf8bit;
3:
begin
PixelFormat := pf24bit; // VisualCLX: redirected const
{$IFDEF VCL}
IgnorePalette := True;
{$ENDIF VCL}
end;
else
raise EPcxError.CreateRes(@RsEPcxUnknownFormat);
end;
end;
{$IFDEF VCL}
Width := Header.x1 - Header.x0 + 1;
Height := Header.y1 - Header.y0 + 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -