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