Scroom 0.14-48-ga0fee447
Loading...
Searching...
No Matches
Scroom::Pnm Namespace Reference

Classes

class  AsciiSource
 
class  AsciiSource1bpp
 
class  BinarySource
 
class  Source
 

Enumerations

enum class  SourceType { Binary , Ascii , Ascii1bpp }
 

Functions

boost::optional< std::tuple< Scroom::TiledBitmap::BitmapMetaData, std::ifstream, SourceType > > open (const Scroom::Logger &logger, const std::string &fileName)
 

Enumeration Type Documentation

◆ SourceType

enum class Scroom::Pnm::SourceType
strong
Enumerator
Binary 
Ascii 
Ascii1bpp 

Function Documentation

◆ open()

boost::optional< std::tuple< Scroom::TiledBitmap::BitmapMetaData, std::ifstream, SourceType > > Scroom::Pnm::open ( const Scroom::Logger logger,
const std::string &  fileName 
)
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;
63 skipComments(pnm);
64 int width = 0;
65 int height = 0;
66 pnm >> width;
67 skipComments(pnm);
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{};
82 SourceType sourceType = SourceType::Binary;
83 if(fileType == "P1")
84 {
86 colormap->name = "Default";
87 colormap->colors = {Color(1.0, 1.0, 1.0), Color(0.0, 0.0, 0.0)};
89 sourceType = SourceType::Ascii1bpp;
90 }
91 else if(fileType == "P4")
92 {
94 // Netpbm convention: 0=white, 1=black
96 colormap->name = "Default";
97 colormap->colors = {Color(1.0, 1.0, 1.0), Color(0.0, 0.0, 0.0)};
99 }
100 else
101 {
102 skipComments(pnm);
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 {
112 bmd = {Greyscale, 8, 1, rect, {}, MonochromeColormapHelper::create(2)};
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 {
125 bmd = {Greyscale, 8, 1, rect, {}, MonochromeColormapHelper::create(2)};
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 }
Definition color.hh:37
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

Referenced by Pnm::open(), and Scroom::Pnm::Source::resetPresentation().

Here is the call graph for this function:
Here is the caller graph for this function: