Anti-Grain Geometry Tutorial
tutorial_rendererbase.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_renderer_base.h>
18 
19 #include "writepng.h"
20 
21 int
22 main (int argc, const char* argv[])
23 {
24  try
25  {
26  const int imageWidth = 100;
27  const int imageHeight = 100;
28 
31 
32  const int pixelSize = PixelFormat::pix_width;
33 
34  agg::rendering_buffer renderBuffer;
35  PixelFormat pixFmt;
36  RendererBaseType rBase;
37 
38  unsigned char *imageBuffer = new unsigned char[imageWidth * imageHeight * pixelSize];
39 
40  // Hook everything up
41  renderBuffer.attach (imageBuffer, imageWidth, imageHeight, imageWidth * pixelSize);
42  pixFmt.attach(renderBuffer);
43  rBase.attach(pixFmt);
44 
45  const agg::rgba8 transparentWhiteColor (0xff, 0xff, 0xff, 0);
46  const agg::rgba8 greenColor (0, 0xff, 0, 0xff);
47 
48  // clear the buffer with transparent white color
49  rBase.clear(transparentWhiteColor);
50 
51  // The code below simply illustrates that it is very straightforward
52  // to directly manipulate the image buffer. That said, one hardly
53  // ever needs to do so.
54 
55  // top border
56  for (int x = 0; x < imageWidth; ++x)
57  {
58  rBase.copy_pixel(x, 0, greenColor);
59  }
60  // bottom border
61  for (int x = 0; x < imageWidth; ++x)
62  {
63  rBase.copy_pixel(x, imageHeight - 1, greenColor);
64  }
65  // left border
66  for (int y = 0; y < imageHeight; ++y)
67  {
68  rBase.copy_pixel(0, y, greenColor);
69  }
70  // right border
71  for (int y = 0; y < imageHeight; ++y)
72  {
73  rBase.copy_pixel(imageWidth - 1, y, greenColor);
74  }
75 
76  // now write the image out for visualization
77  char fileName[1000] = { 0 };
78  if (argc > 1)
79  {
80  sprintf (fileName, "%s/", argv[1]);
81  }
82  strcat(fileName, "tutorial_rendererbase.png");
83  writePng<RendererBaseType> (fileName, rBase);
84 
85  delete imageBuffer;
86  }
87  catch (TutorialException& ex)
88  {
89  printf ("%s\n", ex.getMessage());
90  return 1;
91  }
92  catch (...)
93  {
94  printf ("Unknown exception detected.\n");
95  return 1;
96  }
97 
98  return 0;
99 }