home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 November / CDVD1105.ISO / Software / Freeware / programare / graphics32 / GR32_ByteMaps.pas < prev    next >
Pascal/Delphi Source File  |  2005-02-24  |  8KB  |  340 lines

  1. unit GR32_ByteMaps;
  2.  
  3. (* ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is Graphics32
  17.  *
  18.  * The Initial Developer of the Original Code is
  19.  * Alex A. Denisov
  20.  *
  21.  * Portions created by the Initial Developer are Copyright (C) 2000-2004
  22.  * the Initial Developer. All Rights Reserved.
  23.  *
  24.  * Contributor(s):
  25.  *
  26.  * ***** END LICENSE BLOCK ***** *)
  27.  
  28. interface
  29.  
  30. {$I GR32.inc}
  31.  
  32. uses
  33.   {$IFDEF CLX}
  34.   Qt, Types, {$IFDEF LINUX}Libc, {$ENDIF}
  35.   QControls, QGraphics,
  36.   {$ELSE}
  37.   Windows, Controls, Graphics,
  38.   {$ENDIF}
  39.   Classes, SysUtils, GR32, GR32_Transforms;
  40.  
  41. type
  42.   TConversionType = (ctRed, ctGreen, ctBlue, ctAlpha, ctUniformRGB, ctWeightedRGB);
  43.  
  44.   TByteMap = class(TCustomMap)
  45.   private
  46.     FBytes: TArrayOfByte;
  47.     function GetValue(X, Y: Integer): Byte;
  48.     function GetValPtr(X, Y: Integer): PByte;
  49.     procedure SetValue(X, Y: Integer; Value: Byte);
  50.   protected
  51.     procedure AssignTo(Dst: TPersistent); override;
  52.     procedure ChangeSize(var Width, Height: Integer; NewWidth, NewHeight: Integer); override;
  53.   public
  54.     destructor Destroy; override;
  55.     procedure Assign(Source: TPersistent); override;
  56.     function  Empty: Boolean; override;
  57.     procedure Clear(FillValue: Byte);
  58.     procedure ReadFrom(Source: TBitmap32; Conversion: TConversionType);
  59.     procedure WriteTo(Dest: TBitmap32; Conversion: TConversionType); overload;
  60.     procedure WriteTo(Dest: TBitmap32; const Palette: TPalette32); overload;
  61.     property Bytes: TArrayOfByte read FBytes;
  62.     property ValPtr[X, Y: Integer]: PByte read GetValPtr;
  63.     property Value[X, Y: Integer]: Byte read GetValue write SetValue; default;
  64.   end;
  65.  
  66. implementation
  67.  
  68. uses Math;
  69.  
  70. { TByteMap }
  71.  
  72. procedure TByteMap.Assign(Source: TPersistent);
  73. begin
  74.   BeginUpdate;
  75.   try
  76.     if Source is TByteMap then
  77.     begin
  78.       inherited SetSize(TByteMap(Source).Width, TByteMap(Source).Height);
  79.       FBytes := Copy(TByteMap(Source).Bytes, 0, Width * Height);
  80.     end
  81.     else if Source is TBitmap32 then
  82.       ReadFrom(TBitmap32(Source), ctWeightedRGB)
  83.     else
  84.       inherited;
  85.   finally
  86.     EndUpdate;
  87.     Changed;
  88.   end;
  89. end;
  90.  
  91. procedure TByteMap.AssignTo(Dst: TPersistent);
  92. begin
  93.   if Dst is TBitmap32 then WriteTo(TBitmap32(Dst), ctUniformRGB)
  94.   else inherited;
  95. end;
  96.  
  97. procedure TByteMap.ChangeSize(var Width, Height: Integer; NewWidth, NewHeight: Integer);
  98. begin
  99.   SetLength(FBytes, NewWidth * NewHeight);
  100.   Width := NewWidth;
  101.   Height := NewHeight;
  102. end;
  103.  
  104. procedure TByteMap.Clear(FillValue: Byte);
  105. begin
  106.   FillChar(Bytes[0], Width * Height, FillValue);
  107.   Changed;
  108. end;
  109.  
  110. destructor TByteMap.Destroy;
  111. begin
  112.   FBytes := nil;
  113.   inherited;
  114. end;
  115.  
  116. function TByteMap.Empty: Boolean;
  117. begin
  118.   Result := not Assigned(Bytes);
  119. end;
  120.  
  121. function TByteMap.GetValPtr(X, Y: Integer): PByte;
  122. begin
  123.   Result := @Bytes[X + Y * Width];
  124. end;
  125.  
  126. function TByteMap.GetValue(X, Y: Integer): Byte;
  127. begin
  128.   Result := Bytes[X + Y * Width];
  129. end;
  130.  
  131. procedure TByteMap.ReadFrom(Source: TBitmap32; Conversion: TConversionType);
  132. var
  133.   W, H, I, N: Integer;
  134.   SrcC: PColor32;
  135.   SrcB, DstB: PByte;
  136.   Value: TColor32;
  137. begin
  138.   BeginUpdate;
  139.   try
  140.     SetSize(Source.Width, Source.Height);
  141.     if Empty then Exit;
  142.  
  143.     W := Source.Width;
  144.     H := Source.Height;
  145.     N := W * H - 1;
  146.     SrcC := Source.PixelPtr[0, 0];
  147.     SrcB := Pointer(SrcC);
  148.     DstB := @Bytes[0];
  149.     case Conversion of
  150.  
  151.       ctRed:
  152.         begin
  153.           Inc(SrcB, 2);
  154.           for I := 0 to N do
  155.           begin
  156.             DstB^ := SrcB^;
  157.             Inc(DstB);
  158.             Inc(SrcB, 4);
  159.           end;
  160.         end;
  161.  
  162.       ctGreen:
  163.         begin
  164.           Inc(SrcB, 1);
  165.           for I := 0 to N do
  166.           begin
  167.             DstB^ := SrcB^;
  168.             Inc(DstB);
  169.             Inc(SrcB, 4);
  170.           end;
  171.         end;
  172.  
  173.       ctBlue:
  174.         begin
  175.           for I := 0 to N do
  176.           begin
  177.             DstB^ := SrcB^;
  178.             Inc(DstB);
  179.             Inc(SrcB, 4);
  180.           end;
  181.         end;
  182.  
  183.       ctAlpha:
  184.         begin
  185.           Inc(SrcB, 3);
  186.           for I := 0 to N do
  187.           begin
  188.             DstB^ := SrcB^;
  189.             Inc(DstB);
  190.             Inc(SrcB, 4);
  191.           end;
  192.         end;
  193.  
  194.       ctUniformRGB:
  195.         begin
  196.           for I := 0 to N do
  197.           begin
  198.             Value := SrcC^;
  199.             Value := (Value and $00FF0000) shr 16 + (Value and $0000FF00) shr 8 +
  200.               (Value and $000000FF);
  201.             Value := Value div 3;
  202.             DstB^ := Value;
  203.             Inc(DstB);
  204.             Inc(SrcC);
  205.           end;
  206.         end;
  207.  
  208.       ctWeightedRGB:
  209.         begin
  210.           for I := 0 to N do
  211.           begin
  212.             DstB^ := Intensity(SrcC^);
  213.             Inc(DstB);
  214.             Inc(SrcC);
  215.           end;
  216.         end;
  217.     end;
  218.   finally
  219.     EndUpdate;
  220.     Changed;
  221.   end;
  222. end;
  223.  
  224. procedure TByteMap.SetValue(X, Y: Integer; Value: Byte);
  225. begin
  226.   Bytes[X + Y * Width] := Value;
  227. end;
  228.  
  229. procedure TByteMap.WriteTo(Dest: TBitmap32; Conversion: TConversionType);
  230. var
  231.   W, H, I, N: Integer;
  232.   DstC: PColor32;
  233.   DstB, SrcB: PByte;
  234.   Resized: Boolean;
  235. begin
  236.   Dest.BeginUpdate;
  237.   Resized := False;
  238.   try
  239.     Resized := Dest.SetSize(Width, Height);
  240.     if Empty then Exit;
  241.  
  242.     W := Width;
  243.     H := Height;
  244.     N := W * H - 1;
  245.     DstC := Dest.PixelPtr[0, 0];
  246.     DstB := Pointer(DstC);
  247.     SrcB := @Bytes[0];
  248.     case Conversion of
  249.  
  250.       ctRed:
  251.         begin
  252.           Inc(DstB, 2);
  253.           for I := 0 to N do
  254.           begin
  255.             DstB^ := SrcB^;
  256.             Inc(DstB, 4);
  257.             Inc(SrcB);
  258.           end;
  259.         end;
  260.  
  261.       ctGreen:
  262.         begin
  263.           Inc(DstB, 1);
  264.           for I := 0 to N do
  265.           begin
  266.             DstB^ := SrcB^;
  267.             Inc(DstB, 4);
  268.             Inc(SrcB);
  269.           end;
  270.         end;
  271.  
  272.       ctBlue:
  273.         begin
  274.           for I := 0 to N do
  275.           begin
  276.             DstB^ := SrcB^;
  277.             Inc(DstB, 4);
  278.             Inc(SrcB);
  279.           end;
  280.         end;
  281.  
  282.       ctAlpha:
  283.         begin
  284.           Inc(DstB, 3);
  285.           for I := 0 to N do
  286.           begin
  287.             DstB^ := SrcB^;
  288.             Inc(DstB, 4);
  289.             Inc(SrcB);
  290.           end;
  291.         end;
  292.  
  293.       ctUniformRGB, ctWeightedRGB:
  294.         begin
  295.           for I := 0 to N do
  296.           begin
  297.             DstC^ := Gray32(SrcB^);
  298.             Inc(DstC);
  299.             Inc(SrcB);
  300.           end;
  301.         end;
  302.     end;
  303.   finally
  304.     Dest.EndUpdate;
  305.     Dest.Changed;
  306.     if Resized then Dest.Resized;
  307.   end;
  308. end;
  309.  
  310. procedure TByteMap.WriteTo(Dest: TBitmap32; const Palette: TPalette32);
  311. var
  312.   W, H, I, N: Integer;
  313.   DstC: PColor32;
  314.   SrcB: PByte;
  315. begin
  316.   Dest.BeginUpdate;
  317.   try
  318.     Dest.SetSize(Width, Height);
  319.     if Empty then Exit;
  320.  
  321.     W := Width;
  322.     H := Height;
  323.     N := W * H - 1;
  324.     DstC := Dest.PixelPtr[0, 0];
  325.     SrcB := @Bytes[0];
  326.  
  327.     for I := 0 to N do
  328.     begin
  329.       DstC^ := Palette[SrcB^];
  330.       Inc(DstC);
  331.       Inc(SrcB);
  332.     end;
  333.   finally
  334.     Dest.EndUpdate;
  335.     Dest.Changed;
  336.   end;
  337. end;
  338.  
  339. end.
  340.