Browse Source

pam (P7) support

undefined
Odilitime 4 years ago
parent
commit
32fb614c80
1 changed files with 44 additions and 8 deletions
  1. 44
    8
      src/pnm.cpp

+ 44
- 8
src/pnm.cpp View File

@@ -3,6 +3,7 @@
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <map>

#include "environment/Environment.h"

@@ -28,6 +29,8 @@ RGBAPNMObject * readPPM(const char* fileName) {
//std::cout << "fileType:" << fileType << std::endl;
unsigned char bits = 0;
bool pamType = false;
std::map<std::string, std::string> pamHeader;

// check to see if it's a PPM image file
if (strncmp(fileType, "P4" , 2) == 0) {
@@ -36,6 +39,32 @@ RGBAPNMObject * readPPM(const char* fileName) {
bits = 8; // PGM
} else if (strncmp(fileType, "P6" , 2) == 0) {
bits = 24; // PPM
} else if (strncmp(fileType, "P7" , 2) == 0) {
pamType = true;
bool headerDone = false;
char cLine[80];
char *read;
while(!headerDone) {
//read = getline(&cLine, &len, fr);
read = fgets(cLine, 80, fr);
//res = fscanf(fr, "%s\n", cLine);
std::string line(cLine);
line.erase(line.find_last_not_of(" \t\n\r\f\v") + 1);
//std::cout << "read " << line << std::endl;
size_t spacePos = line.find(" ");
std::string key(line.substr(0, spacePos));
std::string val(line.substr(spacePos + 1));
//std::cout << "read [" << key << "]=[" << val << "]" << std::endl;
pamHeader[key]=val;
if (read == NULL) headerDone = true;
if (line == "ENDHDR") headerDone = true;
}
bits = std::stoi(pamHeader["DEPTH"]) * 8; // 4 * 8 = 32
if (pamHeader["TUPLTYPE"] != "RGB_ALPHA") {
std::cout << "Unsupported TUPLTYPE: " << pamHeader["TUPLTYPE"] << std::endl;
return nullptr;
}
//
} else if (strncmp(fileType, "P8" , 2) == 0) {
bits = 32; // PPM
}
@@ -43,22 +72,31 @@ RGBAPNMObject * readPPM(const char* fileName) {

// read the rest of header
unsigned int width, height;
res = fscanf(fr, "%u\n %u\n", &width, &height);
if (pamType) {
width = std::stoi(pamHeader["WIDTH"]);
height = std::stoi(pamHeader["HEIGHT"]);
} else {
res = fscanf(fr, "%u\n %u\n", &width, &height);
}
data->width = width;
data->height = height;

// read maximum
data->maxColVal = 255; // just set a default
if (bits != 1) {
unsigned int maximum;
res = fscanf(fr, "%u\n", &maximum);
data->maxColVal = height;
if (pamType) {
maximum = std::stoi(pamHeader["MAXVAL"]);
} else {
res = fscanf(fr, "%u\n", &maximum);
}
data->maxColVal = maximum;
}
unsigned int size = width * height;
if (bits != 1) {
size *= bits/8;
}
std::cout << "read " << fileName << " pnm/pam " << width << "," << height << "@" << static_cast<int>(bits) << "(" << static_cast<int>(bits/8) << ")" << "/" << data->maxColVal << std::endl;

// allocate array for pixels
char* pixels = reinterpret_cast<char*>(tlsf_calloc(size, sizeof(char))); // used to be: new char[size];
@@ -84,8 +122,7 @@ void writePBM4(const char *filename, const RGBAPNMObject &data) {
//color[0] = data.m_Ptr[pos + 0]; /* red */
//color[1] = data.m_Ptr[pos + 1]; /* green */
//color[2] = data.m_Ptr[pos + 2]; /* blue */
color[0] = static_cast<unsigned char>(data.m_Ptr[pos + 3]); /*
alpha */
color[0] = static_cast<unsigned char>(data.m_Ptr[pos + 3]); /* alpha */
fwrite(color, 1, 1, fp);
}
}
@@ -99,8 +136,7 @@ void writePGM5(const char *filename, const RGBAPNMObject &data) {
for (unsigned int i = 0; i < data.width; ++i) {
static unsigned char color[1];
unsigned int pos = ((i * 4) + (j * 4 * 1024));
color[0] = static_cast<unsigned char>(data.m_Ptr[pos + 0]); /* red
*/
color[0] = static_cast<unsigned char>(data.m_Ptr[pos + 0]); /* red */
//color[1] = data.m_Ptr[pos + 1]; /* green */
//color[2] = data.m_Ptr[pos + 2]; /* blue */
//color[3] = data.m_Ptr[pos + 3]; /* alpha */

Loading…
Cancel
Save