Anti-Grain Geometry Tutorial
tutorial_linedrawing_2.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_stroke.h>
18 #include <agg_pixfmt_rgba.h>
19 #include <agg_rasterizer_scanline_aa.h>
20 #include <agg_renderer_base.h>
21 #include <agg_renderer_scanline.h>
22 #include <agg_scanline_p.h>
23 
24 #include "path.h"
25 #include "writepng.h"
26 
27 int
28 main (int argc, const char* argv[])
29 {
30  try
31  {
32  const int imageWidth = 100;
33  const int imageHeight = 100;
34 
37 
38  const int pixelSize = PixelFormat::pix_width;
39 
40  agg::rendering_buffer renderBuffer;
41  PixelFormat pixFmt;
42  RendererBaseType rBase;
43 
44  unsigned char *imageBuffer = new unsigned char[imageWidth * imageHeight * pixelSize];
45 
46  // Hook everything up
47  renderBuffer.attach (imageBuffer, imageWidth, imageHeight, imageWidth * pixelSize);
48  pixFmt.attach(renderBuffer);
49  rBase.attach(pixFmt);
50 
51  const agg::rgba8 transparentWhiteColor (0xff, 0xff, 0xff, 0);
52  const agg::rgba8 greenColor (0, 0xff, 0, 0xff);
53  const agg::rgba8 redColor (0xff, 0, 0, 0xff);
54 
55  // clear the buffer with transparent white color
56  rBase.clear(transparentWhiteColor);
57 
59  agg::scanline_p8 scanline;
60 
61  ras.auto_close(false);
62 
63  CmdVertex dome[] =
64  {
65  { agg::path_cmd_move_to, 10, 90 },
66  { agg::path_cmd_curve3, 50, 10 },
67  { agg::path_cmd_curve3, 90, 90 },
68  };
69 
70  CmdVertexPath path (dome, sizeof(dome) / sizeof(CmdVertex));
71 
72  // draw a green caret
73  {
74  // without conv_curve, path_cmd_curve3 are treated as
75  // path_cmd_line_to
76  //
77  // so we will see a green caret
78  agg::conv_stroke<CmdVertexPath> strokePath (path);
79 
80  double strokeWidth = 1.0;
81  strokePath.width(strokeWidth);
82  strokePath.line_cap(agg::square_cap);
83  strokePath.line_join(agg::miter_join);
84  strokePath.miter_limit(strokeWidth);
85 
86  ras.reset();
87  ras.add_path(strokePath);
88 
89  agg::render_scanlines_aa_solid(ras, scanline, rBase, greenColor);
90  }
91 
92  // draw a red dome
93  {
94  // generate a curved path
95  agg::conv_curve<CmdVertexPath> curvePath (path);
96  // and then use this curved path to generate the stroke path
98 
99  double strokeWidth = 2.0;
100  strokePath.width(strokeWidth);
101  strokePath.line_cap(agg::square_cap);
102  strokePath.line_join(agg::miter_join);
103  strokePath.miter_limit(strokeWidth);
104 
105  ras.reset();
106  ras.add_path(strokePath);
107 
108  agg::render_scanlines_aa_solid(ras, scanline, rBase, redColor);
109  }
110 
111  // now write the image out for visualization
112  char fileName[1000] = { 0 };
113  if (argc > 1)
114  {
115  sprintf (fileName, "%s/", argv[1]);
116  }
117  strcat(fileName, "tutorial_linedrawing_2.png");
118  writePng<RendererBaseType> (fileName, rBase);
119 
120  delete imageBuffer;
121  }
122  catch (TutorialException& ex)
123  {
124  printf ("%s\n", ex.getMessage());
125  return 1;
126  }
127  catch (...)
128  {
129  printf ("Unknown exception detected.\n");
130  return 1;
131  }
132 
133  return 0;
134 }
Definition: path.h:76