Anti-Grain Geometry Tutorial
tutorial_linedrawing_4.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 
28 
29 void
30 drawLine (RendererBaseType& rBase, double x1, double y1, double x2, double y2, double lineWidth, agg::rgba8 color)
31 {
33  agg::scanline_p8 scanline;
34 
35  ras.auto_close(false);
36 
37  double linePoints[] = { x1, y1, x2, y2 };
38  SimplePath path (linePoints, sizeof(linePoints) / sizeof(double));
39  agg::conv_stroke<SimplePath> strokePath (path);
40 
41  strokePath.width(lineWidth);
42  strokePath.line_cap(agg::butt_cap);
43  strokePath.line_join(agg::miter_join);
44  strokePath.miter_limit(lineWidth);
45 
46  ras.reset();
47  ras.add_path(strokePath);
48 
49  agg::render_scanlines_aa_solid(ras, scanline, rBase, color);
50 }
51 
52 int
53 main (int argc, const char* argv[])
54 {
55  try
56  {
57  const int imageWidth = 100;
58  const int imageHeight = 25;
59 
60  const int pixelSize = PixelFormat::pix_width;
61 
62  agg::rendering_buffer renderBuffer;
63  PixelFormat pixFmt;
64  RendererBaseType rBase;
65 
66  unsigned char *imageBuffer = new unsigned char[imageWidth * imageHeight * pixelSize];
67 
68  // Hook everything up
69  renderBuffer.attach (imageBuffer, imageWidth, imageHeight, imageWidth * pixelSize);
70  pixFmt.attach(renderBuffer);
71  rBase.attach(pixFmt);
72 
73  const agg::rgba8 whiteColor (0xff, 0xff, 0xff, 0xff);
74  const agg::rgba8 blackColor (0, 0, 0, 0xff);
75 
76  // clear the buffer with transparent white color
77  rBase.clear(whiteColor);
78 
79  // draw a bunch horizontal lines
80  drawLine (rBase, 0, 5, 100, 5, 0.5, blackColor);
81  drawLine (rBase, 0, 8.1, 100, 8.1, 0.5, blackColor);
82  drawLine (rBase, 0, 11.3, 100, 11.3, 0.5, blackColor);
83  drawLine (rBase, 0, 14.5, 100, 14.5, 0.5, blackColor);
84  drawLine (rBase, 0, 17.7, 100, 17.7, 0.5, blackColor);
85 
86  // now write the image out for visualization
87  char fileName[1000] = { 0 };
88  if (argc > 1)
89  {
90  sprintf (fileName, "%s/", argv[1]);
91  }
92  strcat(fileName, "tutorial_linedrawing_4.png");
93  writePng<RendererBaseType> (fileName, rBase);
94 
95  delete imageBuffer;
96  }
97  catch (TutorialException& ex)
98  {
99  printf ("%s\n", ex.getMessage());
100  return 1;
101  }
102  catch (...)
103  {
104  printf ("Unknown exception detected.\n");
105  return 1;
106  }
107 
108  return 0;
109 }