Anti-Grain Geometry Tutorial
tutorial_linedrawing_3.cpp
1 /*
2  * Copyright (c) 2019 Heng Yuan
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <agg_conv_curve.h>
17 #include <agg_conv_dash.h>
18 #include <agg_conv_stroke.h>
19 #include <agg_pixfmt_rgba.h>
20 #include <agg_rasterizer_scanline_aa.h>
21 #include <agg_renderer_base.h>
22 #include <agg_renderer_scanline.h>
23 #include <agg_scanline_p.h>
24 
25 #include "path.h"
26 #include "writepng.h"
27 
28 int
29 main (int argc, const char* argv[])
30 {
31  try
32  {
33  const int imageWidth = 400;
34  const int imageHeight = 400;
35 
38 
39  const int pixelSize = PixelFormat::pix_width;
40 
41  agg::rendering_buffer renderBuffer;
42  PixelFormat pixFmt;
43  RendererBaseType rBase;
44 
45  unsigned char *imageBuffer = new unsigned char[imageWidth * imageHeight * pixelSize];
46 
47  // Hook everything up
48  renderBuffer.attach (imageBuffer, imageWidth, imageHeight, imageWidth * pixelSize);
49  pixFmt.attach(renderBuffer);
50  rBase.attach(pixFmt);
51 
52  const agg::rgba8 transparentWhiteColor (0xff, 0xff, 0xff, 0);
53  const agg::rgba8 greenColor (0, 0xff, 0, 0xff);
54  const agg::rgba8 redColor (0xff, 0, 0, 0xff);
55 
56  // clear the buffer with transparent white color
57  rBase.clear(transparentWhiteColor);
58 
60  agg::scanline_p8 scanline;
61 
62  ras.auto_close(false);
63 
64  CmdVertex dome[] =
65  {
66  { agg::path_cmd_move_to, 50, 350 },
67  { agg::path_cmd_curve3, 200, 50 },
68  { agg::path_cmd_curve3, 350, 350 },
69  };
70 
71  CmdVertexPath path (dome, sizeof(dome) / sizeof(CmdVertex));
72 
73  // draw a dashed dot caret
74  {
75  double strokeWidth = 5.0;
76 
77  agg::conv_dash<CmdVertexPath> dashPath (path);
78 
79  // a long dash
80  dashPath.add_dash(3 * strokeWidth, 3 * strokeWidth);
81  // a dot
82  dashPath.add_dash(1 * strokeWidth, 3 * strokeWidth);
83 
85 
86  strokePath.width(strokeWidth);
87  strokePath.line_cap(agg::butt_cap);
88  strokePath.line_join(agg::miter_join);
89  strokePath.miter_limit(strokeWidth);
90 
91  ras.reset();
92  ras.add_path(strokePath);
93 
94  agg::render_scanlines_aa_solid(ras, scanline, rBase, greenColor);
95  }
96 
97  // draw a dashed dot dome
98  {
99  double strokeWidth = 5.0;
100 
101  // generate a curved path
102  agg::conv_curve<CmdVertexPath> curvePath (path);
103  // generate a dashed curve path
104  agg::conv_dash<agg::conv_curve<CmdVertexPath>> dashPath (curvePath);
105 
106  // a long dash
107  dashPath.add_dash(3 * strokeWidth, 3 * strokeWidth);
108  // a dot
109  dashPath.add_dash(1 * strokeWidth, 3 * strokeWidth);
110 
111  // and then use this curved path to generate the stroke path
113 
114  strokePath.width(strokeWidth);
115  strokePath.line_cap(agg::square_cap);
116  strokePath.line_join(agg::miter_join);
117  strokePath.miter_limit(strokeWidth);
118 
119  ras.reset();
120  ras.add_path(strokePath);
121 
122  agg::render_scanlines_aa_solid(ras, scanline, rBase, redColor);
123  }
124 
125  // now write the image out for visualization
126  char fileName[1000] = { 0 };
127  if (argc > 1)
128  {
129  sprintf (fileName, "%s/", argv[1]);
130  }
131  strcat(fileName, "tutorial_linedrawing_3.png");
132  writePng<RendererBaseType> (fileName, rBase);
133 
134  delete imageBuffer;
135  }
136  catch (TutorialException& ex)
137  {
138  printf ("%s\n", ex.getMessage());
139  return 1;
140  }
141  catch (...)
142  {
143  printf ("Unknown exception detected.\n");
144  return 1;
145  }
146 
147  return 0;
148 }
Definition: path.h:76