Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit ZazuBitmap;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, Math,
- FPimage, IntfGraphics, Graphics, GraphType,
- ZazuTypes;
- type
- TZazuBitmap = class(TObject)
- private
- FData: PRGB32;
- FWidth, FHeight: Integer;
- function ToRawImage: TRawImage;
- public
- property Width: Integer read FWidth;
- property Height: Integer read FHeight;
- procedure SetSize(AWidth, AHeight: Integer);
- procedure SetPixel(X, Y: Integer; Color: Integer);
- function SaveToFile(const Path: String): Boolean;
- constructor Create(AWidth, AHeight: Integer); overload;
- destructor Destroy; override;
- end;
- implementation
- uses Dialogs;
- procedure TZazuBitmap.SetSize(AWidth, AHeight: Integer);
- var
- Size: UInt32;
- NewData: PRGB32 = nil;
- W, H, i: UInt32;
- begin
- if (AWidth <> FWidth) or (AHeight <> FHeight) then
- begin
- Size := AWidth * AHeight;
- if (Size > 0) then
- begin
- NewData := GetMem(Size * SizeOf(TRGB32)); { Create our new memory }
- FillDWord(NewData[0], Size, 0);
- end;
- if (Assigned(NewData)) and (Assigned(FData)) then
- begin
- W := Min(AWidth, FWidth);
- H := Min(AHeight, FHeight);
- for i := 0 to H-1 do
- Move(FData[i * FWidth], NewData[i * AWidth], W * SizeOf(TRGB32)); { Move our old data onto the new data, row by row }
- FreeMem(FData); { Free old memory since it's been copied to new }
- end;
- FData := NewData;
- FWidth := AWidth;
- FHeight := AHeight;
- end;
- end;
- procedure TZazuBitmap.SetPixel(X, Y: Integer; Color: Integer);
- var
- Pixel: Integer;
- begin
- if (X >= 0) and (Y >= 0) and (X < FWidth) and (Y < FWidth) then
- begin
- Pixel := Y * FWidth + X;
- FData[Pixel].R := Color and $FF;
- FData[Pixel].G := Color shr 8 and $FF;
- FData[Pixel].B := Color shr 16 and $FF;
- end;
- end;
- function TZazuBitmap.ToRawImage: TRawImage;
- begin
- with Result do
- begin
- Init;
- Description.PaletteColorCount := 0;
- Description.MaskBitsPerPixel := 0;
- Description.Width := FWidth;
- Description.Height := FHeight;
- Description.Format := ricfRGBA;
- Description.ByteOrder := riboLSBFirst;
- Description.BitOrder := riboBitsInOrder;
- Description.Depth := 24;
- Description.BitsPerPixel := 32;
- Description.LineOrder := riloTopToBottom;
- Description.LineEnd := rileDWordBoundary;
- Description.RedPrec := 8;
- Description.GreenPrec := 8;
- Description.BluePrec := 8;
- Description.AlphaPrec := 0;
- Description.RedShift := 16;
- Description.GreenShift := 8;
- Description.BlueShift := 0;
- DataSize := Description.Width * Description.Height * (Description.BitsPerPixel shr 3);
- Data := PByte(FData);
- end;
- end;
- function TZazuBitmap.SaveToFile(const Path: String): Boolean;
- var
- RawImage: TRawImage;
- Bitmap: TLazIntfImage;
- begin
- Result := True;
- RawImage := Self.ToRawImage; { Don't free it's a record! }
- try
- Bitmap := TLazIntfImage.Create(RawImage, False);
- Bitmap.SaveToFile(Path);
- Bitmap.Free;
- except
- Result := False;
- end;
- end;
- constructor TZazuBitmap.Create(AWidth, AHeight: Integer); overload;
- begin
- inherited Create;
- FWidth := 0;
- FHeight := 0;
- SetSize(AWidth, AHeight);
- end;
- destructor TZazuBitmap.Destroy;
- begin
- if Assigned(FData) then
- FreeMem(FData);
- inherited Destroy;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement