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
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#pragma once
#include <fstream>
#include <ostream>
#include <vector>
#include <stdint.h>
namespace VideoStitch {
namespace Util {
/**
* @brief A class that reads PNM images (PBM, PGM, PPM).
*/
class PnmReader {
public:
/**
* Reads an image from @a filename.
* @param filename Input file name.
* @param w On return, contains the width of the image.
* @param h On return, contains the height of the image.
* @param data On return, contains the data for the image, in RGB format (RGBA is @a pad is true).
* @param err If not NULL, error messages are written there.
* @param pad If true, @data will be RGBA.
*/
static bool read(const char *filename, int64_t &w, int64_t &h, std::vector<unsigned char> &data,
std::ostream *err = NULL, bool pad = false);
/**
* Reads an image from an already open file.
* @param ifs Input stream.
* @param w On return, contains the width of the image.
* @param h On return, contains the height of the image.
* @param data On return, contains the data for the image, in RGB format (RGBA is @a pad is true).
* @param err If not NULL, error messages are written there.
* @param pad If true, @data will be RGBA.
*/
static bool read(std::ifstream &ifs, int64_t &w, int64_t &h, std::vector<unsigned char> &data,
std::ostream *err = NULL, bool pad = false);
private:
enum PixType { AsciiPBM, AsciiPGM, AsciiPPM, BinPBM, BinPGM, BinPPM };
static bool _readCommentWidthHeight(std::ifstream &ifs, int64_t &width, int64_t &height, std::ostream *err);
// binary RGB
template <PixType type>
static bool _read(std::ifstream &ifs, int64_t &w, int64_t &h, std::vector<unsigned char> &data, bool pad,
std::ostream *err);
template <PixType type>
static void _readPixel(std::ifstream &ifs, std::vector<unsigned char> &data);
static const size_t bufSize = 512;
};
/**
* @brief PNM writer class.
*/
class PpmWriter {
public:
/**
* Open a Ppm file for writing and write the header.
* @param filename Name of the file to open.
* @param w Image width.
* @param h image height.
* @param err Output stream for errors.
* @return NULL on error.
*/
static std::ofstream *openPpm(const char *filename, int64_t w, int64_t h, std::ostream *err = NULL);
/**
* Open a Pam file for writing and write the header.
* @param filename Name of the file to open.
* @param w Image width.
* @param h image height.
* @param err Output stream for errors.
* @return NULL on error.
*/
static std::ofstream *openPam(const char *filename, int64_t w, int64_t h, std::ostream *err = NULL);
/**
* Open a Pgm file for writing and write the header.
* @param filename Name of the file to open.
* @param w Image width.
* @param h image height.
* @param err Output stream for errors.
* @return NULL on error.
*/
static std::ofstream *openPgm(const char *filename, int64_t w, int64_t h, std::ostream *err = NULL);
private:
static std::ofstream *openGeneric(const char *filename, std::ostream *err = NULL);
};
} // namespace Util
} // namespace VideoStitch