Canvas.xml 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <?xml-stylesheet type="text/xsl" href="../Xsl/doc2html.xsl"?>
  3. <doc>
  4. <title>Canvas Class Tips</title>
  5. <chapter>
  6. <title>Canvas Class Tips</title>
  7. <par>
  8. You can find few code snippets that show how to use
  9. <ref>TImagingCanvas</ref> class to draw onto images.
  10. </par>
  11. <scap>Canvas Creation And Updating</scap>
  12. <par>
  13. You can create canvas for both <ref>TBaseImage</ref>
  14. class descendants and <ref>TImageData</ref> structure.
  15. </par>
  16. <code>
  17. uses
  18. Imaging, ImagingTypes, ImagingClasses, ImagingCanvases;
  19. var
  20. ImgData: TImageData;
  21. ImgObj: TSingleImage;
  22. Canvas: TImagingCanvas;
  23. begin
  24. // Load image to TImageData struct
  25. InitImage(ImgData);
  26. LoadImageFromFile('umajo.png', ImgData);
  27. // Load image to TSingleImage object
  28. ImgObj := TSingleImage.CreateFromFile('umajo2.png');
  29. // Create canvas for ImgData
  30. Canvas := TImagingCanvas.CreateForData(@ImgData);
  31. // Do some drawing
  32. ...
  33. // Resize image, you must then update canvas
  34. ResizeImage(ImgData, 600, 400, rfBicubic);
  35. Canvas.UpdateCanvasState;
  36. // Draw some more
  37. ...
  38. // Recreate canvas, now for high level object
  39. Canvas.CreateForImage(ImgObj);
  40. // Do some drawing
  41. ...
  42. // Change image format, you must then update canvas
  43. ImgObj.Format := ifGray8;
  44. Canvas.UpdateCanvasState;
  45. // Draw some more
  46. ...
  47. // Free all
  48. FreeImage(ImgData);
  49. Canvas.Free;
  50. ImgObj.Free;
  51. end;
  52. </code>
  53. <scap>Drawing Primitives And Filling</scap>
  54. <par>
  55. Now you will see how to draw lines, ellipses, and
  56. rectangles.
  57. </par>
  58. <code>
  59. Canvas := TImagingCanvas.CreateForImage(Image);
  60. ...
  61. // You can set colors as 32bit or FP 128bit, they're automatically converted
  62. Canvas.FillColor32 := $FF808040;
  63. Canvas.PenColorFP := ColorFP(1.0, 0.6, 0.6, 1.0);
  64. // Set fill mode and pen mode to solid
  65. Canvas.PenMode := pmSolid;
  66. Canvas.FillMode := fmSolid;
  67. // Now set some pixels and draw lines using pen
  68. Canvas.Pixels32[200, 200] := $80FF0000;
  69. Canvas.VertLine(20, 0, Image.Height);
  70. Canvas.HorzLine(0, Image.Widht, 20);
  71. Canvas.Line(10, 10, 90, 100);
  72. // Draw filled rectangle and ellipse with outline
  73. Canvas.Rectangle(Canvas.ClipRect);
  74. Canvas.Ellipse(Canvas.ClipRect);
  75. // Draw outlined rectangle and ellipse
  76. Canvas.FillMode := fmClear;
  77. Canvas.Rectangle(Rect(0, 0, 50, 100));
  78. Canvas.Ellipse(Rect(0, 0, 50, 100));
  79. // Clear whole canvas (uses fill color)
  80. Canvas.FillColor32 := $FF000000;
  81. Canvas.Clear
  82. </code>
  83. <scap>Drawing Image</scap>
  84. <par>
  85. You can draw part of the canvas on another canvas.
  86. Blending with custom source and dest factors is supported with some
  87. best known combinations predefined (like alpha blending).
  88. Stretching part of the canvas is also possible with
  89. optional filtering (bilinear, bicubic, nearest).
  90. </par>
  91. <code>
  92. Canvas := TImagingCanvas.CreateForImage(Image);
  93. ...
  94. // Stretch part of canvas onto another one with alpha blending and bicubic filtering
  95. Canvas.StretchDrawAlpha(SrcRect, DestCanvas, DestRect, rfBicubic);
  96. // Draw part of canvas onto another one with custom combination of blend factors
  97. Canvas.DrawBlend(SrcRect, DestCanvas, DestX, DestY, bfDstColor, bfSrcAlpha);
  98. // Draw part of canvas onto another one with additive blending
  99. Canvas.DrawAdd(SrcRect, DestCanvas, DestX, DestY);
  100. // Draw part of canvas onto another one with additive blending (using factors)
  101. Canvas.DrawBlend(SrcRect, DestCanvas, DestX, DestY, bfOne, bfOne);
  102. // Stretch part of canvas onto another one with addtive blending and bilinear filtering (default param)
  103. Canvas.StretchDrawAdd(SrcRect, DestCanvas, DestRect);
  104. </code>
  105. <scap>Effects</scap>
  106. <par>
  107. Canvas class also allows you to apply linear and nonlinear filters
  108. and use point transforms. You can always use your own kernels
  109. and functions but there are also some predefined.
  110. </par>
  111. <code>
  112. Canvas := TImagingCanvas.CreateForImage(Image);
  113. ...
  114. // 3x3 Gaussian blurr
  115. Canvas.ApplyConvolution3x3(FilterGaussian3x3);
  116. // 7x7 Median filter
  117. Canvas.ApplyMedianFilter(7);
  118. // Modify contrast and brightness
  119. Canvas.ModifyContrastBrightness(20, -10);
  120. // Adjust gamma for each color channel
  121. Canvas.GammaCorection(0.9, 1.3, 0.75);
  122. </code>
  123. <par>
  124. Look at <link url="..\Demos\Pascal.xml#ibrowser">VCL Image Browser Demo</link>
  125. and <link url="..\Demos\Pascal.xml#lclimager">LCL Imager Demo</link>
  126. for more Canvas examples.
  127. General info on canvas usage is in
  128. <link url="..\Usage\CanvasUsage.xml">Using Canvas Class</link>
  129. </par>
  130. </chapter>
  131. </doc>