Paul Heidmann Fractal Example  1.0
pngFactory.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 // Copyright (C) 2014 Paul S. Heidmann
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // For a copy of the GNU General Public License see
16 // <http://www.gnu.org/licenses/>.
17 //
18 // Author contact info:
19 // Paul Heidmann
20 // paul@heidmann.com
21 
22 #include <list>
23 #include <tuple>
24 #include <string>
25 #include <cstdint>
26 #include <boost/utility.hpp>
27 #include <boost/tuple/tuple.hpp>
28 
29 #include <png.h>
30 
31 namespace png
32 {
33 
34 /// \brief This class provides an interface into libpng that is not
35 /// completely insane (like libpng's native interface). The
36 /// code in this class is fairly ugly.
37 class pngFactory : private boost::noncopyable
38 {
39 public:
40 
41  pngFactory() = delete;
42 
43  /// \brief This is the constructor that must be used to create
44  /// instances of this class.
45  /// \param xResolution The horizontal resolution of the image.
46  /// \param yResolution The vertical resolution of the image.
47  /// \param pngOutFileName The filename of the final PNG image.
48  pngFactory( const std::uint16_t xResolution,
49  const std::uint16_t yResolution,
50  const std::string& pngOutFileName );
51 
52  /// \brief The class destructor.
53  ~pngFactory();
54 
55  /// \brief This method is called to add a pixel to the PNG image.
56  /// \param x The X coordinate of the pixel.
57  /// \param y The Y coordinate of the pixel.
58  /// \param red The red component of the color of the pixel.
59  /// \param green The green component of the color of the pixel.
60  /// \param blue The blue component of the color of the pixel.
61  void addPixel(
62  const long double x,
63  const long double y,
64  const std::uint8_t red,
65  const std::uint8_t green,
66  const std::uint8_t blue );
67 
68  /// \brief This method returns to the caller the horizontal resolution
69  /// of the image.
70  /// \return The X resolution of the image.
71  const std::uint16_t getXRes( void ) const;
72 
73  /// \brief This method returns to the caller the vertical resolution
74  /// of the image.
75  /// \return The Y resolution of the image.
76  const std::uint16_t getYRes( void ) const;
77 
78 private:
79 
80  /// \brief This type is used to define variables that hold single
81  /// pixels.
82  typedef std::tuple<
83  long double,
84  long double,
85  std::uint8_t,
86  std::uint8_t,
87  std::uint8_t> pixelType;
88 
89  /// \brief This type is used to access rows in the PNG image.
90  typedef std::uint8_t* pngRowPtrType;
91 
92  /// \brief This attribute holds the name of the file into which the
93  /// final PNG image is to placed.
94  const std::string pFileName;
95 
96  /// \brief This attribute holds the X resolution of the PNG image.
97  const std::uint16_t xRes;
98 
99  /// \brief This attribute holds the Y resolution of the PNG image.
100  const std::uint16_t yRes;
101 
102  /// \brief This attribute holds the smallest x coordinate the caller
103  /// has specified.
104  long double xMin;
105 
106  /// \brief This attribute holds the largest x coordinate the caller
107  /// has specified.
108  long double xMax;
109 
110  /// \brief This attribute holds the smallest y coordinate the caller
111  /// has specified.
112  long double yMin;
113 
114  /// \brief This attribute holds the largest y coordinate the caller
115  /// has specified.
116  long double yMax;
117 
118  /// \brief This attribute holds the scalar used to translate caller
119  /// specified x coordinates into the image coordinate space.
120  long double xTranslationFactor;
121 
122  /// \brief This attribute holds the scalar used to translate caller
123  /// specified y coordinates into the image coordinate space.
124  long double yTranslationFactor;
125 
126  /// \brief This attribute holds all caller specified pixels.
127  std::list<png::pngFactory::pixelType> pxlLst;
128 
129  /// \brief This attribute holds a row pointer used to access the
130  /// PNG image.
132 
133  /// \brief This attribute holds the libpng defined PNG structure used
134  /// when generating the PNG image.
135  ::png_structp pngStructPtr;
136 
137  /// \brief This attribute holds the libpng defined PNG info structure used
138  /// when generating the PNG image.
139  ::png_infop pngInfoPtr;
140 
141  /// \brief This method allocates space from the heap to hold the PNG
142  /// image.
143  void allocatePngImageSpace( void );
144 
145  /// \brief This method deallocates the PNG image space.
146  void deallocatePngImageSpace( void );
147 
148  /// \brief This method writes the PNG image out to disk.
149  void writePngFile( void );
150 
151  /// \brief This method is called to update the minimums and maximums,
152  /// as tracked by this instance.
153  void updateMinMax( const png::pngFactory::pixelType& p );
154 
155  /// \brief This method is called to determine the minimums and maximums,
156  /// as tracked by this instance.
157  void findMinMax( void );
158 
159  /// \brief This method maps a single pixel from the user specified
160  /// coordinate plane to pixel space.
161  /// \param p The pixel to translate.
163 
164  /// \brief This method maps all pixels from the user specified
165  /// coordinate plane to pixel space.
166  void translateAllPixels( void );
167 
168  /// \brief This method renders all pixels.
169  void renderAllPixels( void );
170 };
171 
172 };
void translatePixel(png::pngFactory::pixelType &p) const
This method maps a single pixel from the user specified coordinate plane to pixel space...
Definition: pngFactory.cpp:165
long double xTranslationFactor
This attribute holds the scalar used to translate caller specified x coordinates into the image coord...
Definition: pngFactory.hpp:120
long double xMin
This attribute holds the smallest x coordinate the caller has specified.
Definition: pngFactory.hpp:104
const std::uint16_t xRes
This attribute holds the X resolution of the PNG image.
Definition: pngFactory.hpp:97
std::tuple< long double, long double, std::uint8_t, std::uint8_t, std::uint8_t > pixelType
This type is used to define variables that hold single pixels.
Definition: pngFactory.hpp:87
::png_structp pngStructPtr
This attribute holds the libpng defined PNG structure used when generating the PNG image...
Definition: pngFactory.hpp:135
~pngFactory()
The class destructor.
Definition: pngFactory.cpp:45
void renderAllPixels(void)
This method renders all pixels.
Definition: pngFactory.cpp:187
std::list< png::pngFactory::pixelType > pxlLst
This attribute holds all caller specified pixels.
Definition: pngFactory.hpp:127
const std::string pFileName
This attribute holds the name of the file into which the final PNG image is to placed.
Definition: pngFactory.hpp:94
void updateMinMax(const png::pngFactory::pixelType &p)
This method is called to update the minimums and maximums, as tracked by this instance.
Definition: pngFactory.cpp:139
const std::uint16_t yRes
This attribute holds the Y resolution of the PNG image.
Definition: pngFactory.hpp:100
void writePngFile(void)
This method writes the PNG image out to disk.
Definition: pngFactory.cpp:96
const std::uint16_t getXRes(void) const
This method returns to the caller the horizontal resolution of the image.
Definition: pngFactory.cpp:70
void translateAllPixels(void)
This method maps all pixels from the user specified coordinate plane to pixel space.
Definition: pngFactory.cpp:172
long double yTranslationFactor
This attribute holds the scalar used to translate caller specified y coordinates into the image coord...
Definition: pngFactory.hpp:124
std::uint8_t * pngRowPtrType
This type is used to access rows in the PNG image.
Definition: pngFactory.hpp:90
This class provides an interface into libpng that is not completely insane (like libpng&#39;s native inte...
Definition: pngFactory.hpp:37
pngFactory()=delete
long double yMax
This attribute holds the largest y coordinate the caller has specified.
Definition: pngFactory.hpp:116
void findMinMax(void)
This method is called to determine the minimums and maximums, as tracked by this instance.
Definition: pngFactory.cpp:154
void deallocatePngImageSpace(void)
This method deallocates the PNG image space.
Definition: pngFactory.cpp:89
void addPixel(const long double x, const long double y, const std::uint8_t red, const std::uint8_t green, const std::uint8_t blue)
This method is called to add a pixel to the PNG image.
Definition: pngFactory.cpp:54
long double yMin
This attribute holds the smallest y coordinate the caller has specified.
Definition: pngFactory.hpp:112
const std::uint16_t getYRes(void) const
This method returns to the caller the vertical resolution of the image.
Definition: pngFactory.cpp:75
::png_infop pngInfoPtr
This attribute holds the libpng defined PNG info structure used when generating the PNG image...
Definition: pngFactory.hpp:139
long double xMax
This attribute holds the largest x coordinate the caller has specified.
Definition: pngFactory.hpp:108
png::pngFactory::pngRowPtrType * pngRowPointers
This attribute holds a row pointer used to access the PNG image.
Definition: pngFactory.hpp:131
void allocatePngImageSpace(void)
This method allocates space from the heap to hold the PNG image.
Definition: pngFactory.cpp:80