37 FILE *infile = fopen(inpath,
"r");
39 printf(
"pam.c: couldn't open input file: %s\n", inpath);
54 if (!fgets(line,
sizeof(line), infile)) {
55 printf(
"pam.c: unexpected EOF\n");
66 size_t linelen = strlen(line);
67 for (
int idx = 0; idx < linelen; idx++) {
68 if (line[idx] ==
' ') {
71 printf(
"pam.c: More than two tokens, %s:%d\n", inpath, linenumber);
76 if (line[idx] ==
'\n')
80 if (!strcmp(tok0,
"P7"))
83 if (!strcmp(tok0,
"ENDHDR"))
86 if (!strcmp(tok0,
"WIDTH") && tok1) {
87 pam->
width = atoi(tok1);
91 if (!strcmp(tok0,
"HEIGHT") && tok1) {
96 if (!strcmp(tok0,
"DEPTH") && tok1) {
97 pam->
depth = atoi(tok1);
101 if (!strcmp(tok0,
"MAXVAL") && tok1) {
106 if (!strcmp(tok0,
"TUPLTYPE") && tok1) {
107 if (!strcmp(tok1,
"GRAYSCALE_ALPHA")) {
112 if (!strcmp(tok1,
"RGB_ALPHA")) {
117 if (!strcmp(tok1,
"RGB")) {
122 if (!strcmp(tok1,
"GRAYSCALE")) {
127 printf(
"pam.c: unrecognized tupl type %s\n", tok1);
131 printf(
"pam.c: unrecognized attribute %s\n", tok0);
136 printf(
"pam.c: missing required metadata field\n");
140 assert(pam->
maxval == 255);
145 printf(
"pam.c: couldn't read body\n");
160 FILE *f = fopen(outpath,
"w+");
164 const char *tupl = NULL;
167 tupl =
"GRAYSCALE_ALPHA";
182 fprintf(f,
"P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
185 if (len != fwrite(pam->
data, 1, len, f)) {
222 if (type == in->
type)
226 assert(in->
maxval == 255);
241 assert(in->
depth == 3);
242 for (
int y = 0; y < h; y++) {
243 for (
int x = 0; x < w; x++) {
244 out->
data[y*4*w + 4*x + 0] = in->
data[y*3*w + 3*x + 0];
245 out->
data[y*4*w + 4*x + 1] = in->
data[y*3*w + 3*x + 1];
246 out->
data[y*4*w + 4*x + 2] = in->
data[y*3*w + 3*x + 2];
247 out->
data[y*4*w + 4*x + 3] = 255;
251 printf(
"pam.c unsupported type %d\n", in->
type);
void pam_destroy(pam_t *pam)
pam_t * pam_create_from_file(const char *inpath)
pam_t * pam_convert(pam_t *in, int type)
pam_t * pam_copy(pam_t *pam)
int pam_write_file(pam_t *pam, const char *outpath)