<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../Xsl/doc2html.xsl"?>
<doc>
  <title>Canvas Class Tips</title>
  <chapter>
    <title>Canvas Class Tips</title>
    <par>
      You can find few code snippets that show how to use
      <ref>TImagingCanvas</ref> class to draw onto images.
    </par>
    
    <scap>Canvas Creation And Updating</scap>
    <par>
      You can create canvas for both <ref>TBaseImage</ref>
      class descendants and <ref>TImageData</ref> structure.
    </par>
    <code>
  uses
    Imaging, ImagingTypes, ImagingClasses, ImagingCanvases;
  var
    ImgData: TImageData;
    ImgObj: TSingleImage;
    Canvas: TImagingCanvas;
  begin
    // Load image to TImageData struct
    InitImage(ImgData);
    LoadImageFromFile('umajo.png', ImgData);
    // Load image to TSingleImage object
    ImgObj := TSingleImage.CreateFromFile('umajo2.png');
    
    // Create canvas for ImgData
    Canvas := TImagingCanvas.CreateForData(@ImgData);
    // Do some drawing
    ...
    // Resize image, you must then update canvas
    ResizeImage(ImgData, 600, 400, rfBicubic);
    Canvas.UpdateCanvasState;
    // Draw some more
    ...
    
    // Recreate canvas, now for high level object
    Canvas.CreateForImage(ImgObj);
    // Do some drawing
    ...
    // Change image format, you must then update canvas
    ImgObj.Format := ifGray8;
    Canvas.UpdateCanvasState;
    // Draw some more
    ...
    
    
    // Free all
    FreeImage(ImgData);
    Canvas.Free;
    ImgObj.Free;
  end;
    </code>
    
    <scap>Drawing Primitives And Filling</scap>
    <par>
      Now you will see how to draw lines, ellipses, and
      rectangles.
    </par>
    <code>
    Canvas := TImagingCanvas.CreateForImage(Image);
    ...
    // You can set colors as 32bit or FP 128bit, they're automatically converted
    Canvas.FillColor32 := $FF808040;
    Canvas.PenColorFP := ColorFP(1.0, 0.6, 0.6, 1.0);
    // Set fill mode and pen mode to solid
    Canvas.PenMode := pmSolid;
    Canvas.FillMode := fmSolid;
    
    // Now set some pixels and draw lines using pen
    Canvas.Pixels32[200, 200] := $80FF0000;
    Canvas.VertLine(20, 0, Image.Height);
    Canvas.HorzLine(0, Image.Widht, 20);
    Canvas.Line(10, 10, 90, 100);
    
    // Draw filled rectangle and ellipse with outline
    Canvas.Rectangle(Canvas.ClipRect);
    Canvas.Ellipse(Canvas.ClipRect);
    
    // Draw outlined rectangle and ellipse
    Canvas.FillMode := fmClear;
    Canvas.Rectangle(Rect(0, 0, 50, 100));
    Canvas.Ellipse(Rect(0, 0, 50, 100));

    // Clear whole canvas (uses fill color)
    Canvas.FillColor32 := $FF000000;
    Canvas.Clear
    </code>
    
    <scap>Drawing Image</scap>
    <par>
      You can draw part of the canvas on another canvas.
      Blending with custom source and dest factors is supported with some
      best known combinations predefined (like alpha blending).
      Stretching part of the canvas is also possible with
      optional filtering (bilinear, bicubic, nearest).
    </par>
    <code>
    Canvas := TImagingCanvas.CreateForImage(Image);
    ...
    // Stretch part of canvas onto another one with alpha blending and bicubic filtering
    Canvas.StretchDrawAlpha(SrcRect, DestCanvas, DestRect, rfBicubic);
    // Draw part of canvas onto another one with custom combination of blend factors
    Canvas.DrawBlend(SrcRect, DestCanvas, DestX, DestY, bfDstColor, bfSrcAlpha);
    // Draw part of canvas onto another one with additive blending
    Canvas.DrawAdd(SrcRect, DestCanvas, DestX, DestY);
    // Draw part of canvas onto another one with additive blending (using factors)
    Canvas.DrawBlend(SrcRect, DestCanvas, DestX, DestY, bfOne, bfOne);
    // Stretch part of canvas onto another one with addtive blending and bilinear filtering (default param)
    Canvas.StretchDrawAdd(SrcRect, DestCanvas, DestRect);
    </code>
    
    <scap>Effects</scap>
    <par>
      Canvas class also allows you to apply linear and nonlinear filters
      and use point transforms. You can always use your own kernels
      and functions but there are also some predefined.
    </par>
    <code>
    Canvas := TImagingCanvas.CreateForImage(Image);
    ...
    // 3x3 Gaussian blurr
    Canvas.ApplyConvolution3x3(FilterGaussian3x3);
    // 7x7 Median filter
    Canvas.ApplyMedianFilter(7);
    // Modify contrast and brightness
    Canvas.ModifyContrastBrightness(20, -10);
    // Adjust gamma for each color channel
    Canvas.GammaCorection(0.9, 1.3, 0.75);
    </code>
    
    <par>
      Look at <link url="..\Demos\Pascal.xml#ibrowser">VCL Image Browser Demo</link>
      and <link url="..\Demos\Pascal.xml#lclimager">LCL Imager Demo</link>
      for more Canvas examples.
      General info on canvas usage is in
      <link url="..\Usage\CanvasUsage.xml">Using Canvas Class</link>
    </par>


  </chapter>
</doc>
