32 #ifndef SPL_pnmCodec_hpp 33 #define SPL_pnmCodec_hpp 39 #include <SPL/config.hpp> 58 pnmMagicTxtPbm = 0x5031,
59 pnmMagicTxtPgm = 0x5032,
60 pnmMagicTxtPpm = 0x5033,
61 pnmMagicBinPbm = 0x5034,
62 pnmMagicBinPgm = 0x5035,
63 pnmMagicBinPpm = 0x5036,
124 int pnmMinVal(
bool sgnd,
int maxVal);
125 int pnmMaxVal(
bool sgnd,
int maxVal);
133 return (1UL << n) - 1;
137 T pnmClipVal(T x, T minVal, T maxVal)
139 assert(minVal <= maxVal);
142 }
else if (x > maxVal) {
175 long pnmGetTxtInt(std::istream& in,
bool sgnd,
int& status);
180 long pnmGetBinInt(std::istream& in,
int wordSize,
bool sgnd,
int& status);
185 int pnmPutBinInt(std::ostream& out,
int wordSize,
bool sgnd,
long val);
194 template <
class GetData>
195 int pnmEncode(std::ostream& outStream,
int width,
int height,
int numComps,
196 int maxVal,
bool sgnd, GetData& getData,
bool binaryFormat)
201 header.
magic = binaryFormat ? pnmMagicBinPbm : pnmMagicTxtPbm;
203 header.
magic = binaryFormat ? pnmMagicBinPgm : pnmMagicTxtPgm;
205 }
else if (numComps == 3) {
206 header.
magic = binaryFormat ? pnmMagicBinPpm : pnmMagicTxtPpm;
210 header.
width = width;
227 template <
class GetData>
236 for (
int y = header.
height - 1; y >= 0; --y) {
237 if (fmt == pnmFmtBin) {
238 if (type == pnmTypePbm) {
241 for (
int x = 0; x < header.
width; ++x) {
243 val = (val << 1) | (b & 1);
246 if (!out.put(static_cast<unsigned char>(val))) {
254 assert(numBits <= 8);
256 if (!out.put(static_cast<unsigned char>(val))) {
261 for (
int x = 0; x < header.
width; ++x) {
262 for (
int c = 0; c < numComps; ++c) {
272 for (
int x = 0; x < header.
width; ++x) {
273 for (
int c = 0; c < numComps; ++c) {
276 std::ostringstream buf;
278 int n = buf.str().length();
283 if (lineLen && prec > 1) {
292 if (fmt == pnmFmtTxt && lineLen) {
308 template <
class Initialize>
309 int pnmDecode(std::istream& inStream, Initialize& initialize)
315 #if defined(PNM_DEBUG) 317 <<
"magic " << std::hex << header.
magic <<
" " 318 << std::dec <<
" width " << header.
width <<
" " 319 <<
"height " << header.
height <<
" " 320 <<
"maxVal " << header.
maxVal <<
"\n" 324 typename Initialize::PutData
putData;
336 template <
class PutData>
344 int minVal = pnmMinVal(header.
sgnd, header.
maxVal);
345 int maxVal = pnmMaxVal(header.
sgnd, header.
maxVal);
347 for (
int y = header.
height - 1; y >= 0; --y) {
348 if (type == pnmTypePbm) {
349 if (fmt == pnmFmtBin) {
350 for (
int x = 0; x < header.
width;) {
352 if ((c = in.get()) == std::char_traits<char>::eof()) {
356 while (n > 0 && x < header.
width) {
365 for (
int x = 0; x < header.
width; ++x) {
375 if (fmt == pnmFmtBin) {
376 for (
int x = 0; x < header.
width; ++x) {
377 for (
int c = 0; c < numComps; ++c) {
386 if (val < minVal || val > maxVal) {
387 std::cerr <<
"warning: clipping out of range sample value\n";
389 val = pnmClipVal<int>(val, minVal, maxVal);
391 assert(val >= minVal && val <= maxVal);
398 for (
int x = 0; x < header.
width; ++x) {
399 for (
int c = 0; c < numComps; ++c) {
407 if (val < minVal || val > maxVal) {
408 std::cerr <<
"warning: clipping out of range sample value\n";
410 val = pnmClipVal<int>(val, minVal, maxVal);
412 assert(val >= minVal && val <= maxVal);
int pnmPutBinInt(std::ostream &out, int wordSize, bool sgnd, long val)
Write an integer from the specified stream.
Definition: pnmCodec.cpp:68
long pnmGetTxtInt(std::istream &in, bool sgnd, int &status)
Read an integer from the specified stream.
Definition: pnmCodec.cpp:140
int pnmGetNumComps(PnmType type)
Get the number of components from the PNM type.
Definition: pnmCodec.cpp:294
Definition: Arcball.hpp:48
int pnmGetHeader(std::istream &in, PnmHeader &header)
Read a PNM header from the specified stream.
Definition: pnmCodec.cpp:92
int pnmPutHeader(std::ostream &out, PnmHeader &header)
Write a PNM header to the specified stream.
Definition: pnmCodec.cpp:53
PnmFmt pnmGetFmt(PnmMagic magic)
Determine the format (i.e., text or binary) from magic number.
Definition: pnmCodec.cpp:273
const int pnmMaxLineLen
The maximum line length to be produced when encoding in text format.
Definition: pnmCodec.hpp:83
int putData(std::ostream &out, PnmHeader &header, GetData &getData)
Write the actual image data to a stream.
Definition: pnmCodec.hpp:228
PnmFmt
The format of the PNM data (i.e., binary or text).
Definition: pnmCodec.hpp:76
long pnmOnes(int n)
Get an integer whose representation in binary consists of the specified number of ones...
Definition: pnmCodec.hpp:131
long pnmGetBinInt(std::istream &in, int wordSize, bool sgnd, int &status)
Read an integer from the specified stream.
Definition: pnmCodec.cpp:222
int pnmMaxValToPrec(int maxVal)
Determine the precision from the maximum value.
Definition: pnmCodec.cpp:304
PnmType
The type of the PNM data.
Definition: pnmCodec.hpp:68
int getData(std::istream &in, PnmHeader &header, PutData &putData)
Read the actual image data from the specified stream.
Definition: pnmCodec.hpp:337
int pnmDecode(std::istream &inStream, Initialize &initialize)
Read data encoded in the PNM format from the specified stream.
Definition: pnmCodec.hpp:309
int pnmGetTxtBit(std::istream &in)
Read a bit from the specified stream.
Definition: pnmCodec.cpp:119
int pnmGetChar(std::istream &in)
Read a character from the specified stream.
Definition: pnmCodec.cpp:202
PnmMagic
The signature values that can appear at the start of the header.
Definition: pnmCodec.hpp:56
PnmType pnmGetType(PnmMagic magic)
Determine the type (i.e., PGM or PPM) from the magic number.
Definition: pnmCodec.cpp:249
int pnmEncode(std::ostream &outStream, int width, int height, int numComps, int maxVal, bool sgnd, GetData &getData, bool binaryFormat)
Write data encoded in the PNM format to the specified stream.
Definition: pnmCodec.hpp:195