Anti-Grain Geometry Tutorial
tutorial_shape_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_ellipse.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 = 200;
32  const int imageHeight = 200;
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 redColor (0xff, 0, 0, 0xff);
52 
53  // clear the buffer with transparent white color
54  rBase.clear(transparentWhiteColor);
55 
57  agg::scanline_p8 scanline;
58 
59  {
60  agg::ellipse eliipse (100, 100, 50, 80);
61 
62  ras.reset();
63  ras.add_path(eliipse);
64 
65  agg::render_scanlines_aa_solid(ras, scanline, rBase, redColor);
66  }
67 
68  // now write the image out for visualization
69  char fileName[1000] = { 0 };
70  if (argc > 1)
71  {
72  sprintf (fileName, "%s/", argv[1]);
73  }
74  strcat(fileName, "tutorial_shape_1.png");
75  writePng<RendererBaseType> (fileName, rBase);
76 
77  delete imageBuffer;
78  }
79  catch (TutorialException& ex)
80  {
81  printf ("%s\n", ex.getMessage());
82  return 1;
83  }
84  catch (...)
85  {
86  printf ("Unknown exception detected.\n");
87  return 1;
88  }
89 
90  return 0;
91 }