56 {
57 try
58 {
59 std::ifstream pnm(fileName);
60 pnm.exceptions(std::ifstream::eofbit | std::ifstream::failbit | std::ifstream::badbit);
61 std::string fileType;
62 pnm >> fileType;
64 int width = 0;
65 int height = 0;
66 pnm >> width;
68 pnm >> height;
69 if(width < 0)
70 {
71 throw std::invalid_argument("Width cannot be negative");
72 }
73 if(height < 0)
74 {
75 throw std::invalid_argument("Height cannot be negative");
76 }
77
78 logger->debug("This bitmap has size {}*{}", width, height);
79
80 const auto rect = Scroom::Utils::make_rect<int>(0, 0, width, height);
81 BitmapMetaData bmd{};
83 if(fileType == "P1")
84 {
89 sourceType = SourceType::Ascii1bpp;
90 }
91 else if(fileType == "P4")
92 {
94
99 }
100 else
101 {
103 int maxVal = 0;
104 pnm >> maxVal;
105 if(maxVal != 255)
106 {
107 throw std::invalid_argument("Only 8bpp is supported");
108 }
109
110 if(fileType == "P2")
111 {
113 sourceType = SourceType::Ascii;
114 }
115 else if(fileType == "P3")
116 {
117 bmd = {
RGB, 8, 3,
rect, {},
nullptr};
118 sourceType = SourceType::Ascii;
119 }
120 else
121 {
123 if(fileType == "P5")
124 {
126 }
127 else if(fileType == "P6")
128 {
129 bmd = {
RGB, 8, 3,
rect, {},
nullptr};
130 }
131 else
132 {
133 throw std::invalid_argument("Unsupported file type: " + fileType);
134 }
135 }
136 }
137 return std::make_tuple(bmd, std::move(pnm), sourceType);
138 }
139 catch(const std::exception& ex)
140 {
141 logger->error(
"While processing file {}: {}", fileName, ex.what());
142 return {};
143 }
144 }
static Ptr create(int numberOfColors)
Definition colormap-helpers.cc:51
static Colormap::Ptr create()
Definition colormappable.hh:46
static Ptr create(int numberOfColors)
Definition colormap-helpers.cc:68
Colormap::Ptr const colormap
Definition colormaphelpers_test.cc:55
SourceType
Definition pnmsource.hh:24
const std::string Greyscale
Definition layerspecforbitmap.cc:53
const std::string Colormapped
Definition layerspecforbitmap.cc:54
const std::string RGB
Definition layerspecforbitmap.cc:51
Scroom::Logger logger
Definition callbacks.cc:54
void consumeDataBoundary(std::istream &s)
Definition pnmsource.cc:35
void skipComments(std::istream &s)
Definition pnmsource.cc:27
const auto rect
Definition rectangletests.cc:335