Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.37 KB | None | 0 0
  1. unit ZazuBitmap;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Math,
  9.   FPimage, IntfGraphics, Graphics, GraphType,
  10.   ZazuTypes;
  11.  
  12. type
  13.   TZazuBitmap = class(TObject)
  14.   private
  15.     FData: PRGB32;
  16.     FWidth, FHeight: Integer;
  17.  
  18.     function ToRawImage: TRawImage;
  19.   public
  20.     property Width: Integer read FWidth;
  21.     property Height: Integer read FHeight;
  22.  
  23.     procedure SetSize(AWidth, AHeight: Integer);
  24.     procedure SetPixel(X, Y: Integer; Color: Integer);
  25.     function SaveToFile(const Path: String): Boolean;
  26.  
  27.     constructor Create(AWidth, AHeight: Integer); overload;
  28.     destructor Destroy; override;
  29.   end;
  30.  
  31. implementation
  32.  
  33. uses Dialogs;
  34.  
  35. procedure TZazuBitmap.SetSize(AWidth, AHeight: Integer);
  36. var
  37.   Size: UInt32;
  38.   NewData: PRGB32 = nil;
  39.   W, H, i: UInt32;
  40. begin
  41.   if (AWidth <> FWidth) or (AHeight <> FHeight) then
  42.   begin
  43.     Size := AWidth * AHeight;
  44.     if (Size > 0) then
  45.     begin
  46.       NewData := GetMem(Size * SizeOf(TRGB32)); { Create our new memory }
  47.       FillDWord(NewData[0], Size, 0);
  48.     end;
  49.  
  50.     if (Assigned(NewData)) and (Assigned(FData)) then
  51.     begin
  52.       W := Min(AWidth, FWidth);
  53.       H := Min(AHeight, FHeight);
  54.       for i := 0 to H-1 do
  55.         Move(FData[i * FWidth], NewData[i * AWidth], W * SizeOf(TRGB32)); { Move our old data onto the new data, row by row }
  56.       FreeMem(FData); { Free old memory since it's been copied to new }
  57.     end;
  58.  
  59.     FData := NewData;
  60.     FWidth := AWidth;
  61.     FHeight := AHeight;
  62.   end;
  63. end;
  64.  
  65. procedure TZazuBitmap.SetPixel(X, Y: Integer; Color: Integer);
  66. var
  67.   Pixel: Integer;
  68. begin
  69.   if (X >= 0) and (Y >= 0) and (X < FWidth) and (Y < FWidth) then
  70.   begin
  71.     Pixel := Y * FWidth + X;
  72.  
  73.     FData[Pixel].R := Color and $FF;
  74.     FData[Pixel].G := Color shr 8 and $FF;
  75.     FData[Pixel].B := Color shr 16 and $FF;
  76.   end;
  77. end;
  78.  
  79. function TZazuBitmap.ToRawImage: TRawImage;
  80. begin
  81.   with Result do
  82.   begin
  83.     Init;
  84.  
  85.     Description.PaletteColorCount := 0;
  86.     Description.MaskBitsPerPixel := 0;
  87.     Description.Width := FWidth;
  88.     Description.Height := FHeight;
  89.  
  90.     Description.Format := ricfRGBA;
  91.     Description.ByteOrder := riboLSBFirst;
  92.     Description.BitOrder := riboBitsInOrder;
  93.     Description.Depth := 24;
  94.     Description.BitsPerPixel := 32;
  95.     Description.LineOrder := riloTopToBottom;
  96.     Description.LineEnd := rileDWordBoundary;
  97.  
  98.     Description.RedPrec := 8;
  99.     Description.GreenPrec := 8;
  100.     Description.BluePrec := 8;
  101.     Description.AlphaPrec := 0;
  102.  
  103.     Description.RedShift := 16;
  104.     Description.GreenShift := 8;
  105.     Description.BlueShift := 0;
  106.  
  107.     DataSize := Description.Width * Description.Height * (Description.BitsPerPixel shr 3);
  108.     Data := PByte(FData);
  109.   end;
  110. end;
  111.  
  112. function TZazuBitmap.SaveToFile(const Path: String): Boolean;
  113. var
  114.   RawImage: TRawImage;
  115.   Bitmap: TLazIntfImage;
  116. begin
  117.   Result := True;
  118.   RawImage := Self.ToRawImage; { Don't free it's a record! }
  119.  
  120.   try
  121.     Bitmap := TLazIntfImage.Create(RawImage, False);
  122.     Bitmap.SaveToFile(Path);
  123.     Bitmap.Free;
  124.   except
  125.     Result := False;
  126.   end;
  127. end;
  128.  
  129. constructor TZazuBitmap.Create(AWidth, AHeight: Integer); overload;
  130. begin
  131.   inherited Create;
  132.   FWidth := 0;
  133.   FHeight := 0;
  134.  
  135.   SetSize(AWidth, AHeight);
  136. end;
  137.  
  138. destructor TZazuBitmap.Destroy;
  139. begin
  140.   if Assigned(FData) then
  141.     FreeMem(FData);
  142.  
  143.   inherited Destroy;
  144. end;
  145.  
  146. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement