File: pngfile.h

package info (click to toggle)
aoflagger 2.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,232 kB
  • sloc: cpp: 61,805; python: 60; sh: 23; makefile: 8
file content (143 lines) | stat: -rw-r--r-- 4,249 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/** @file
 * This is the header file for the PngFile class.
 * @author André Offringa <offringa@gmail.com>
 */
#ifndef PNGFILE_H
#define PNGFILE_H

#include <string>

#include <png.h>

#include "../baseexception.h"

/**
 * This class wraps the libpng library. It can save an Image2D class to a .png file.
 * @see Image2D
 */
class PngFile {
	public:
		/**
		 * Construct a new png file with a filename, a width and a height.
		 * @param filename Name of the png file.
		 * @param width Width of the image
		 * @param height Height of the image
		 */
		PngFile(const std::string &filename, unsigned width, unsigned height);
		
		/**
		 * Destructor.
		 */
		~PngFile();
		
		/**
		 * Start writing.
		 * @throws IOException if something goes wrong.
		 */
		void BeginWrite();
		
		/**
		 * Closes the image.
		 */
		void Close();
		
		/**
		 * Returns the size of one pixel in bytes.
		 * @return Size of one pixel in bytes.
		 */
		int PixelSize() const throw() { return _pixelSize; }
		
		/**
		 * Clears the entire image.
		 * @param colorR Red background value.
		 * @param colorG Green background value.
		 * @param colorB Blue background value.
		 * @param colorA Alfa background value.
		 */
		void Clear(int colorR=255, int colorG=255, int colorB=255, int colorA=255);

		/**
		 * Sets a pixel in the image to a specific color.
		 * @param x x-coordinate.
		 * @param y y-coordinate.
		 * @param colorR Red value.
		 * @param colorG Green value.
		 * @param colorB Blue value.
		 * @param colorA Alfa value.
		 */
		void PlotPixel(unsigned x, unsigned y, int colorR, int colorG, int colorB, int colorA)
		{
			_row_pointers[y][x*_pixelSize] = colorR;
			_row_pointers[y][x*_pixelSize+1] = colorG;
			_row_pointers[y][x*_pixelSize+2] = colorB;
			_row_pointers[y][x*_pixelSize+3] = colorA;
		}

		/**
		 * Sets a square in the image to a specific color.
		 * @param x x-coordinate.
		 * @param y y-coordinate.
		 * @param colorR Red value.
		 * @param colorG Green value.
		 * @param colorB Blue value.
		 * @param colorA Alfa value.
		 */
		void PlotDatapoint(unsigned x, unsigned y, int colorR, int colorG, int colorB, int colorA);
		
		/**
		 * Retrieve the array of row pointers.
		 * @return an array of row pointers.
		 */
		png_bytep *RowPointers() const throw() { return _row_pointers; }
		
		/**
		 * Sets all pixels in the rowpointers to match the image.
     */
		void SetFromImage(const class Image2D &image, const class ColorMap &colorMap, long double normalizeFactor, long double zeroLevel = 0.0);

		/**
		 * Write an image directly to disk. The image will be normalized.
		 * @param image Image containing the data.
		 * @param filename Name of the file to write.
		 * @throws IOException if writing fails.
		 */
		static void Save(const class Image2D &image, const std::string &filename);
		
		/**
		 * Write an image directly to disk by using a specific colormap. The image will be normalized.
		 * @param image Image containing the data.
		 * @param filename Name of the file to write.
		 * @param colorMap ColorMap to use.
		 * @throws IOException if writing fails.
		 */
		static void Save(const class Image2D &image, const std::string &filename, const class ColorMap &colorMap);
		
		/**
		 * Write an image directly to disk by using a specific colormap. The image will be normalized with a specified
		 * factor.
		 * @param image Image containing the data.
		 * @param filename Name of the file to write.
		 * @param colorMap ColorMap to use.
		 * @param normalizeFactor Factor to use for normalisation.
		 * @throws IOException if writing fails.
		 */
		static void Save(const class Image2D &image, const std::string &filename, const class ColorMap &colorMap, long double normalizeFactor, long double zeroLevel = 0.0);
		
		/**
		 * Fill this instance with the values of the image by using the color map, and save it to disk.
		 * @param image Image to use.
		 * @param colorMap Color map to use.
		 * @throws IOException if writing fails.
		 */
		void Save(const class Image2D &image, const class ColorMap &colorMap);
	private:
		const std::string _filename;
		const unsigned _width, _height;
		png_bytep *_row_pointers;
		png_structp _png_ptr;
		png_infop _info_ptr;
		FILE *_fp;
		const int _pixelSize;
};

#endif