Anti-Grain Geometry Tutorial
tutorial_shape_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_pixfmt_rgba.h>
17 #include <agg_rasterizer_scanline_aa.h>
18 #include <agg_renderer_base.h>
19 #include <agg_renderer_scanline.h>
20 #include <agg_rounded_rect.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 greenColor (0, 0xff, 0, 0xff);
52  const agg::rgba8 blueColor (0, 0, 0xff, 0xff);
53 
54  // clear the buffer with transparent white color
55  rBase.clear(transparentWhiteColor);
56 
58  agg::scanline_p8 scanline;
59 
60  {
61  agg::rounded_rect rect1 (5, 5, 195, 195, 0);
62  ras.reset();
63  ras.add_path(rect1);
64  agg::render_scanlines_aa_solid(ras, scanline, rBase, blueColor);
65 
66  agg::rounded_rect rect2 (20, 60, 170, 110, 10);
67  ras.reset();
68  ras.add_path(rect2);
69  agg::render_scanlines_aa_solid(ras, scanline, rBase, greenColor);
70  }
71 
72  // now write the image out for visualization
73  char fileName[1000] = { 0 };
74  if (argc > 1)
75  {
76  sprintf (fileName, "%s/", argv[1]);
77  }
78  strcat(fileName, "tutorial_shape_2.png");
79  writePng<RendererBaseType> (fileName, rBase);
80 
81  delete imageBuffer;
82  }
83  catch (TutorialException& ex)
84  {
85  printf ("%s\n", ex.getMessage());
86  return 1;
87  }
88  catch (...)
89  {
90  printf ("Unknown exception detected.\n");
91  return 1;
92  }
93 
94  return 0;
95 }