You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

187 lines
6.4 KiB

#include "pnm.h"
#include <string>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <map>
#ifdef _WIN32
#include <io.h>
#include <windows.h>
#include <fcntl.h>
#endif
#include "../../../environment/Environment.h"
// pnm
// The PNM format is just an abstraction of the PBM, PGM, and PPM formats. I.e. the name "PNM" refers collectively to PBM, PGM, and PPM.
// pam has the advanced header
RGBAPNMObject * readPPM(const char* fileName) {
RGBAPNMObject *data=new RGBAPNMObject;
// open the file to read just the header reading
FILE* fr = fopen((Environment::getResourceDir() + "/" + fileName).c_str(), "rb");
if (!fr) {
std::cout << "Can't open " << fileName << std::endl;
return nullptr;
}
// formatted read of header
char fileType[11];
int res=0;
res = fscanf(fr, "%9s", fileType);
data->magicNum=fileType;
//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) {
bits = 1; // PBM
} else if (strncmp(fileType, "P5" , 2) == 0) {
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
}
//std::cout << "bits:" << bits << std::endl;
// read the rest of header
unsigned int width, height;
if (pamType) {
width = static_cast<unsigned int>(std::stoi(pamHeader["WIDTH"]));
height = static_cast<unsigned int>(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;
if (pamType) {
maximum = static_cast<unsigned int>(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) << "(" << bits/8 << ")" << "/" << data->maxColVal << std::endl;
// allocate array for pixels
unsigned char* pixels = static_cast<unsigned char*>(tlsf_malloc(size)); // used to be: new char[size];
//unsigned char* pixels = reinterpret_cast<unsigned char*>(tlsf_calloc(size, sizeof(unsigned char))); // used to be: new char[size];
// unformatted read of binary pixel data
res = fread(pixels, 1, size,fr);
data->m_Ptr = pixels;
// close file
fclose(fr);
// return the array
return data;
} // end of readPPM
void writePBM4(const char *filename, const RGBAPNMObject &data) {
FILE *fp = fopen(filename, "wb"); /* b - binary mode */
fprintf(fp, "P6\n%d %d\n255\n", data.width, data.height);
for (unsigned int j = 0; j < data.height; ++j) {
for (unsigned int i = 0; i < data.width; ++i) {
static unsigned char color[1];
unsigned int pos = ((i * 4) + (j * 4 * 1024));
//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] = data.m_Ptr[pos + 3]; /* alpha */
fwrite(color, 1, 1, fp);
}
}
fclose(fp);
}
void writePGM5(const char *filename, const RGBAPNMObject &data) {
FILE *fp = fopen(filename, "wb"); /* b - binary mode */
fprintf(fp, "P5\n%d %d\n255\n", data.width, data.height);
for (unsigned int j = 0; j < data.height; ++j) {
for (unsigned int i = 0; i < data.width; ++i) {
static unsigned char color[1];
unsigned int pos = ((i * 4) + (j * 4 * 1024));
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[3] = data.m_Ptr[pos + 3]; /* alpha */
fwrite(color, 1, 1, fp);
}
}
fclose(fp);
}
void writePPM6(const char *filename, const RGBAPNMObject &data) {
FILE *fp = fopen(filename, "wb"); /* b - binary mode */
fprintf(fp, "P6\n%d %d\n255\n", data.width, data.height);
for (unsigned int j = 0; j < data.height; ++j) {
for (unsigned int i = 0; i < data.width; ++i) {
static unsigned char color[3];
unsigned int pos = ((i * 4) + (j * 4 * 1024));
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[3] = data.m_Ptr[pos + 3]; /* alpha */
fwrite(color, 1, 3, fp);
}
}
fclose(fp);
}
void writePPM8(const char *filename, const RGBAPNMObject &data) {
FILE *fp = fopen(filename, "wb"); /* b - binary mode */
fprintf(fp, "P8\n%d %d\n255\n", data.width, data.height);
for (unsigned int j = 0; j < data.height; ++j) {
for (unsigned int i = 0; i < data.width; ++i) {
static unsigned char color[4];
unsigned int pos = ((i * 4) + (j * 4 * 1024));
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[3] = data.m_Ptr[pos + 3]; /* alpha */
fwrite(color, 1, 4, fp);
}
}
fclose(fp);
}