Browse Source

+ support for conversion between indexed and RGBA images inside TFPMemoryImage
* TFPCustomImage moved near other TFPCustomImage mathods
* ColTransparent added to palette every time it is created : after an action on UsePalette
before it was just in constructor so not presente after false then true assigned to UsePalette.

mazen 22 năm trước cách đây
mục cha
commit
ffaf16733a
1 tập tin đã thay đổi với 98 bổ sung41 xóa
  1. 98 41
      fcl/image/fpimage.inc

+ 98 - 41
fcl/image/fpimage.inc

@@ -201,15 +201,20 @@ begin
   result := assigned(FPalette);
 end;
 
-procedure TFPCustomImage.SetUsePalette (Value : boolean);
+procedure TFPCustomImage.SetUsePalette(Value:boolean);
 begin
-  if Value <> assigned(FPalette) then
-    if Value then
-      FPalette := TFPPalette.Create (0)
+  if Value <> assigned(FPalette)
+  then
+    if Value
+    then
+      begin
+        FPalette := TFPPalette.Create (0);
+        FPalette.Add (colTransparent);    
+      end
     else
       begin
-      FPalette.Free;
-      FPalette := nil;
+        FPalette.Free;
+        FPalette := nil;
       end;
 end;
 
@@ -240,14 +245,47 @@ begin
     FonProgress(Sender,Stage,PercentDone,RedrawNow,R,Msg,Continue);
 end;                         
                          
+Procedure TFPCustomImage.Assign(Source: TPersistent);
+
+Var
+  Src : TFPCustomImage;
+  X,Y : Integer;
+
+begin
+  If Source is TFPCustomImage then
+    begin
+    Src:=TFPCustomImage(Source);
+    // Copy extra info
+    FExtra.Assign(Src.Fextra);
+    // Copy palette if needed.
+    UsePalette:=Src.UsePalette;
+    If UsePalette then
+      begin
+      Palette.Count:=0;
+      Palette.Build(Src);
+      end;
+    // Copy image.  
+    SetSize(Src.Width,Src.height);
+    If UsePalette then
+      For x:=0 to Src.Width-1 do
+        For y:=0 to src.Height-1 do
+          pixels[X,Y]:=src.pixels[X,Y]
+    else  
+      For x:=0 to Src.Width-1 do
+        For y:=0 to src.Height-1 do
+          self[X,Y]:=src[X,Y];
+    end
+  else  
+    Inherited Assign(Source);
+end;
 
 { TFPMemoryImage }
 
 constructor TFPMemoryImage.Create (AWidth,AHeight:integer);
 begin
   inherited create (AWidth,AHeight);
-  UsePalette := True;
-  Palette.Add (colTransparent);
+{Default behavior is to use palette as suggested by Michael}
+  SetUsePalette(True);
 end;
 
 destructor TFPMemoryImage.Destroy;
@@ -258,11 +296,29 @@ begin
   inherited Destroy;
 end;
 
+function TFPMemoryImage.GetInternalColor(x,y:integer):TFPColor;
+  begin
+    if Assigned(FPalette)
+    then
+      Result:=inherited GetInternalColor(x,y)
+    else
+      Result:=PFPColorArray(FData)^[y*FWidth+x];
+  end;
+
 function TFPMemoryImage.GetInternalPixel (x,y:integer) : integer;
 begin
   result := FData^[y*FWidth+x];
 end;
 
+procedure TFPMemoryImage.SetInternalColor (x,y:integer; const Value:TFPColor);
+  begin
+    if Assigned(FPalette)
+    then
+      inherited SetInternalColor(x,y,Value)
+    else
+      PFPColorArray(FData)^[y*FWidth+x]:=Value;
+  end;
+
 procedure TFPMemoryImage.SetInternalPixel (x,y:integer; Value:integer);
 begin
   FData^[y*FWidth+x] := Value;
@@ -283,7 +339,12 @@ begin
   if (AWidth <> Width) or (AHeight <> Height) then
     begin
     old := Height * Width;
-    r := SizeOf(integer)*AWidth*AHeight;
+    r:=AWidth*AHeight;
+    if Assigned(FPalette)
+    then
+      r:=SizeOf(integer)*r
+    else
+      r:=SizeOf(TFPColor)*r;
     if r = 0 then
       NewData := nil
     else
@@ -308,39 +369,35 @@ begin
     end;
 end;
 
-
-Procedure TFPCustomImage.Assign(Source: TPersistent);
-
-Var
-  Src : TFPCustomImage;
-  X,Y : Integer;
-
+procedure TFPMemoryImage.SetUsePalette(Value:boolean);
+var
+  OldColors:PFPColorArray;
+  OldPixels:PIntegerArray;
+  r,c:Integer;
 begin
-  If Source is TFPCustomImage then
-    begin
-    Src:=TFPCustomImage(Source);
-    // Copy extra info
-    FExtra.Assign(Src.Fextra);
-    // Copy palette if needed.
-    UsePalette:=Src.UsePalette;
-    If UsePalette then
+  if Value<>assigned(FPalette)
+  then
+    if Value
+    then
       begin
-      Palette.Count:=0;
-      Palette.Build(Src);
+        FPalette:=TFPPalette.Create(0);
+        FPalette.Add(colTransparent);    
+        OldColors:=PFPColorArray(FData);
+        GetMem(FData,FWidth*FHeight*SizeOf(Integer));
+        for r:=0 to FHeight-1 do
+          for c:=0 to FWidth-1 do
+            Colors[c,r]:=OldColors^[r*FWidth+c];
+        FreeMem(OldColors);
+      end
+    else
+      begin
+        OldPixels:=PIntegerArray(FData);
+        GetMem(FData,FWidth*FHeight*SizeOf(TFPColor));
+        for r:=0 to FHeight-1 do
+          for c:=0 to FWidth-1 do
+            Colors[c,r]:=FPalette.Color[OldPixels^[r*FWidth+c]];
+        FreeMem(OldPixels);
+        FPalette.Free;
+        FPalette:=nil;
       end;
-    // Copy image.  
-    SetSize(Src.Width,Src.height);
-    If UsePalette then
-      For x:=0 to Src.Width-1 do
-        For y:=0 to src.Height-1 do
-          pixels[X,Y]:=src.pixels[X,Y]
-    else  
-      For x:=0 to Src.Width-1 do
-        For y:=0 to src.Height-1 do
-          self[X,Y]:=src[X,Y];
-    end
-  else  
-    Inherited Assign(Source);
 end;
-
-