Browse Source

adjust pixel-centered coords

Johann 6 years ago
parent
commit
4ef650fbcf
3 changed files with 41 additions and 33 deletions
  1. 15 13
      lazpaint/tools/utoollayer.pas
  2. 5 3
      lazpaint/tools/utoolselect.pas
  3. 21 17
      lazpaint/tools/utoolvectorial.pas

+ 15 - 13
lazpaint/tools/utoollayer.pas

@@ -320,9 +320,10 @@ begin
       m := AffineMatrixTranslation(-x,-y)*m;
   end else m := AffineMatrixIdentity;
 
-  ab := TAffineBox.AffineBox(BitmapToVirtualScreen(m*PointF(FLayerBounds.Left-0.499,FLayerBounds.Top-0.499)),
-            BitmapToVirtualScreen(m*PointF(FLayerBounds.Right-0.501,FLayerBounds.Top-0.499)),
-            BitmapToVirtualScreen(m*PointF(FLayerBounds.Left-0.499,FLayerBounds.Bottom-0.501)));
+  m := AffineMatrixTranslation(-0.5,-0.5)*m;
+  ab := TAffineBox.AffineBox(BitmapToVirtualScreen(m*PointF(FLayerBounds.Left+0.001,FLayerBounds.Top+0.001)),
+            BitmapToVirtualScreen(m*PointF(FLayerBounds.Right-0.001,FLayerBounds.Top+0.001)),
+            BitmapToVirtualScreen(m*PointF(FLayerBounds.Left+0.001,FLayerBounds.Bottom-0.001)));
   ptsF := ab.AsPolygon;
   setlength(pts, length(ptsF));
   for i := 0 to high(pts) do
@@ -597,9 +598,6 @@ var
   ptsRect: TRect;
 begin
   idx := Manager.Image.CurrentLayerIndex;
-  with Manager.Image.LayerOffset[idx] do
-    Result:= NicePoint(VirtualScreen,BitmapToVirtualScreen(TransformCenter-PointF(X,Y)));
-
   if not FOriginalBoundsDefined then
   begin
     if Manager.Image.LayerOriginalDefined[idx] then
@@ -618,10 +616,14 @@ begin
   m := Manager.Image.LayerOriginalMatrix[idx];
   with Manager.Image.LayerOffset[idx] do
     m := AffineMatrixTranslation(-x,-y)*m;
+  m := AffineMatrixTranslation(-0.5,-0.5)*m;
+
+  with Manager.Image.LayerOffset[idx] do
+    Result:= NicePoint(VirtualScreen,BitmapToVirtualScreen(TransformCenter-PointF(X,Y)));
 
-  ab := TAffineBox.AffineBox(BitmapToVirtualScreen(m*PointF(FOriginalBounds.Left-0.499,FOriginalBounds.Top-0.499)),
-            BitmapToVirtualScreen(m*PointF(FOriginalBounds.Right-0.501,FOriginalBounds.Top-0.499)),
-            BitmapToVirtualScreen(m*PointF(FOriginalBounds.Left-0.499,FOriginalBounds.Bottom-0.501)));
+  ab := TAffineBox.AffineBox(BitmapToVirtualScreen(m*PointF(FOriginalBounds.Left+0.001,FOriginalBounds.Top+0.001)),
+            BitmapToVirtualScreen(m*PointF(FOriginalBounds.Right-0.001,FOriginalBounds.Top+0.001)),
+            BitmapToVirtualScreen(m*PointF(FOriginalBounds.Left+0.001,FOriginalBounds.Bottom-0.001)));
   ptsF := ab.AsPolygon;
   setlength(pts, length(ptsF));
   for i := 0 to high(pts) do
@@ -689,9 +691,9 @@ begin
   result := EmptyRect;
   NeedOriginal;
   Manager.Image.LayerOriginalMatrix[Manager.Image.CurrentLayerIndex] :=
-    AffineMatrixTranslation(TransformCenter.X,TransformCenter.Y)*
+    AffineMatrixTranslation(TransformCenter.X+0.5,TransformCenter.Y+0.5)*
     AffineMatrixScale(FActualZoom,FActualZoom)*
-    AffineMatrixTranslation(-TransformCenter.X,-TransformCenter.Y)*
+    AffineMatrixTranslation(-TransformCenter.X-0.5,-TransformCenter.Y-0.5)*
     FInitialOriginalMatrix;
 end;
 
@@ -798,9 +800,9 @@ begin
   result := EmptyRect;
   NeedOriginal;
   Manager.Image.LayerOriginalMatrix[Manager.Image.CurrentLayerIndex] :=
-    AffineMatrixTranslation(TransformCenter.X,TransformCenter.Y)*
+    AffineMatrixTranslation(TransformCenter.X+0.5,TransformCenter.Y+0.5)*
     AffineMatrixRotationDeg(FActualAngle)*
-    AffineMatrixTranslation(-TransformCenter.X,-TransformCenter.Y)*
+    AffineMatrixTranslation(-TransformCenter.X-0.5,-TransformCenter.Y-0.5)*
     FInitialOriginalMatrix;
 end;
 

+ 5 - 3
lazpaint/tools/utoolselect.pas

@@ -272,7 +272,8 @@ begin
 
   if BigImage and FQuickDefine then
   begin
-    ab := TCustomRectShape(FShape).GetAffineBox(FEditor.Matrix, true);
+    ab := TCustomRectShape(FShape).GetAffineBox(
+      AffineMatrixTranslation(0.5,0.5)*FEditor.Matrix*AffineMatrixTranslation(-0.5,-0.5), false);
     abBounds := ab.RectBounds;
     abBounds.Inflate(1,1);
     result := RectUnion(result, abBounds);
@@ -281,7 +282,7 @@ begin
       ptsF := ab.AsPolygon;
       setlength(pts, length(ptsF));
       for i := 0 to high(ptsF) do
-        pts[i] := ptsF[i].Round;
+        pts[i] := (ptsF[i]+PointF(0.5,0.5)).Round;
       VirtualScreen.DrawPolygonAntialias(pts,BGRAWhite,BGRABlack,FrameDashLength);
     end;
   end;
@@ -314,7 +315,8 @@ begin
 
   if BigImage and FQuickDefine then
   begin
-    ab := TCustomRectShape(FShape).GetAffineBox(FEditor.Matrix, true);
+    ab := TCustomRectShape(FShape).GetAffineBox(
+      AffineMatrixTranslation(0.5,0.5)*FEditor.Matrix*AffineMatrixTranslation(-0.5,-0.5), false);
     abBounds := ab.RectBounds;
     abBounds.Inflate(1,1);
     result := RectUnion(result, abBounds);

+ 21 - 17
lazpaint/tools/utoolvectorial.pas

@@ -47,7 +47,7 @@ type
     function RoundCoordinate(ptF: TPointF): TPointF; virtual;
     function GetIsSelectingTool: boolean; override;
     function UpdateShape(toolDest: TBGRABitmap): TRect; virtual;
-    function VectorTransform: TAffineMatrix;
+    function VectorTransform(APixelCentered: boolean): TAffineMatrix;
     procedure UpdateCursor(ACursor: TOriginalEditorCursor);
     function FixLayerOffset: boolean; override;
     function DoToolDown({%H-}toolDest: TBGRABitmap; {%H-}pt: TPoint; {%H-}ptF: TPointF; rightBtn: boolean): TRect; override;
@@ -811,7 +811,9 @@ begin
     xAxis := BitmapToVirtualScreen(PointF(-X+1,-Y));
     yAxis := BitmapToVirtualScreen(PointF(-X,-Y+1));
   end;
-  viewMatrix := AffineMatrix(xAxis-orig,yAxis-orig,orig);
+  viewMatrix := AffineMatrixTranslation(0.5,0.5)
+                *AffineMatrix(xAxis-orig,yAxis-orig,orig)
+                *AffineMatrixTranslation(-0.5,-0.5);
 
   if not Assigned(FSelectionRect) and
     (IsVectorOriginal or (IsGradientOriginal and FIsEditingGradient)) then
@@ -1206,7 +1208,7 @@ var
   matrix: TAffineMatrix;
 begin
   toolDest := GetToolDrawingLayer;
-  matrix := VectorTransform;
+  matrix := VectorTransform(false);
   r := (matrix*TAffineBox.AffineBox(ABounds)).RectBounds;
   UpdateShape(toolDest);
   Action.NotifyChange(toolDest, r);
@@ -1305,7 +1307,7 @@ var
   addDiff: TAddShapeToVectorOriginalDifference;
 begin
   layerId := Manager.Image.LayerId[Manager.Image.CurrentLayerIndex];
-  transf := VectorTransform;
+  transf := VectorTransform(false);
   diff := TComposedImageDifference.Create;
   replaceDiff := TReplaceLayerByVectorOriginalDifference.Create(Manager.Image.CurrentState,Manager.Image.CurrentLayerIndex,
                    Manager.Image.LayerOriginalClass[Manager.Image.CurrentLayerIndex]=TVectorOriginal);
@@ -1335,7 +1337,7 @@ begin
     begin
       CancelAction;
       if FShape.Usermode = vsuCreate then FShape.Usermode:= vsuEdit;
-      rF := FShape.GetRenderBounds(rect(0,0,Manager.Image.Width,Manager.Image.Height), VectorTransform);
+      rF := FShape.GetRenderBounds(rect(0,0,Manager.Image.Width,Manager.Image.Height), VectorTransform(false));
       if rF.IntersectsWith(rectF(0,0,Manager.Image.Width,Manager.Image.Height)) then
       begin
         if UseOriginal then
@@ -1493,7 +1495,7 @@ var
 begin
   result := FPreviousUpdateBounds;
   RestoreBackupDrawingLayer;
-  matrix := VectorTransform;
+  matrix := VectorTransform(false);
   draft := (FRightDown or FLeftDown) and SlowShape;
   newBounds := GetCustomShapeBounds(toolDest.ClipRect,matrix,draft);
   result := RectUnion(result, newBounds);
@@ -1503,12 +1505,14 @@ begin
   FPreviousUpdateBounds := newBounds;
 end;
 
-function TVectorialTool.VectorTransform: TAffineMatrix;
+function TVectorialTool.VectorTransform(APixelCentered: boolean): TAffineMatrix;
 begin
   if not UseOriginal then
     result := AffineMatrixIdentity
   else
     result := Manager.Image.LayerOriginalMatrix[Manager.Image.CurrentLayerIndex];
+  if APixelCentered then
+    result := MatrixForPixelCentered(result);
 end;
 
 procedure TVectorialTool.UpdateCursor(ACursor: TOriginalEditorCursor);
@@ -1535,11 +1539,11 @@ begin
     FLastPos := AffineMatrixTranslation(X,Y)*ptF;
   if Assigned(FShape) then
   begin
-    viewPt := FEditor.Matrix*AffineMatrixInverse(VectorTransform)*FLastPos;
+    viewPt := FEditor.Matrix*AffineMatrixInverse(VectorTransform(true))*FLastPos;
     FEditor.MouseDown(rightBtn, FShiftState, viewPt.X,viewPt.Y, cur, handled);
     if not handled and Assigned(FShape) then
     begin
-      shapePt := AffineMatrixInverse(VectorTransform)*FLastPos;
+      shapePt := AffineMatrixInverse(VectorTransform(true))*FLastPos;
       FShape.MouseDown(rightBtn, FShiftState, shapePt.X,shapePt.Y, cur, handled);
     end;
     UpdateCursor(cur);
@@ -1575,9 +1579,9 @@ begin
       FQuickDefineEndPoint := FQuickDefineStartPoint;
       FShape.BeginUpdate;
         QuickDefineShape(FQuickDefineStartPoint,FQuickDefineEndPoint);
-        FLastShapeTransform := AffineMatrixInverse(VectorTransform);
+        FLastShapeTransform := AffineMatrixInverse(VectorTransform(false));
         FShape.Transform(FLastShapeTransform);
-        shapePt := AffineMatrixInverse(VectorTransform)*FLastPos;
+        shapePt := AffineMatrixInverse(VectorTransform(true))*FLastPos;
         handled := false;
         FShape.MouseMove(FShiftState, shapePt.X,shapePt.Y, cur, handled);
         AssignShapeStyle(FLastShapeTransform);
@@ -1613,18 +1617,18 @@ begin
     end;
     FShape.BeginUpdate;
       QuickDefineShape(FQuickDefineStartPoint, FQuickDefineEndPoint);
-      FLastShapeTransform := AffineMatrixInverse(VectorTransform);
+      FLastShapeTransform := AffineMatrixInverse(VectorTransform(false));
       FShape.Transform(FLastShapeTransform);
       AssignShapeStyle(FLastShapeTransform);
     FShape.EndUpdate;
     result := OnlyRenderChange;
   end else
   begin
-    viewPt := FEditor.Matrix*AffineMatrixInverse(VectorTransform)*FLastPos;
+    viewPt := FEditor.Matrix*AffineMatrixInverse(VectorTransform(true))*FLastPos;
     FEditor.MouseMove(FShiftState, viewPt.X,viewPt.Y, cur, handled);
     if not handled and Assigned(FShape) then
     begin
-      shapePt := AffineMatrixInverse(VectorTransform)*FLastPos;
+      shapePt := AffineMatrixInverse(VectorTransform(true))*FLastPos;
       FShape.MouseMove(FShiftState, shapePt.X,shapePt.Y, cur, handled);
     end;
     UpdateCursor(cur);
@@ -1672,11 +1676,11 @@ begin
     QuickDefineEnd;
   end else
   begin
-    viewPt := FEditor.Matrix*AffineMatrixInverse(VectorTransform)*FLastPos;
+    viewPt := FEditor.Matrix*AffineMatrixInverse(VectorTransform(true))*FLastPos;
     FEditor.MouseUp(wasRight, FShiftState, viewPt.X,viewPt.Y, cur, handled);
     if not handled and Assigned(FShape) then
     begin
-      shapePt := AffineMatrixInverse(VectorTransform)*FLastPos;
+      shapePt := AffineMatrixInverse(VectorTransform(true))*FLastPos;
       FShape.MouseUp(wasRight, FShiftState, shapePt.X,shapePt.Y, cur, handled);
     end;
     UpdateCursor(cur);
@@ -1837,7 +1841,7 @@ begin
       xAxis := BitmapToVirtualScreen(PointF(1,0));
       yAxis := BitmapToVirtualScreen(PointF(0,1));
     end;
-    FEditor.Matrix := AffineMatrix(xAxis-orig,yAxis-orig,orig)*VectorTransform;
+    FEditor.Matrix := AffineMatrix(xAxis-orig,yAxis-orig,orig)*VectorTransform(true);
     FEditor.Clear;
     FEditor.PointSize := DoScaleX(PointSize, OriginalDPI);
     if Assigned(FShape) then FShape.ConfigureEditor(FEditor);