31 FILE *infile = fopen(inpath,
"r");
33 printf(
"pam.c: couldn't open input file: %s\n", inpath);
48 if (!fgets(line,
sizeof(line), infile)) {
49 printf(
"pam.c: unexpected EOF\n");
60 size_t linelen = strlen(line);
61 for (
int idx = 0; idx < linelen; idx++) {
62 if (line[idx] ==
' ') {
65 printf(
"pam.c: More than two tokens, %s:%d\n", inpath, linenumber);
70 if (line[idx] ==
'\n')
74 if (!strcmp(tok0,
"P7"))
77 if (!strcmp(tok0,
"ENDHDR"))
80 if (!strcmp(tok0,
"WIDTH") && tok1) {
81 pam->
width = atoi(tok1);
85 if (!strcmp(tok0,
"HEIGHT") && tok1) {
90 if (!strcmp(tok0,
"DEPTH") && tok1) {
91 pam->
depth = atoi(tok1);
95 if (!strcmp(tok0,
"MAXVAL") && tok1) {
100 if (!strcmp(tok0,
"TUPLTYPE") && tok1) {
101 if (!strcmp(tok1,
"GRAYSCALE_ALPHA")) {
106 if (!strcmp(tok1,
"RGB_ALPHA")) {
111 if (!strcmp(tok1,
"RGB")) {
116 if (!strcmp(tok1,
"GRAYSCALE")) {
121 printf(
"pam.c: unrecognized tupl type %s\n", tok1);
125 printf(
"pam.c: unrecognized attribute %s\n", tok0);
130 printf(
"pam.c: missing required metadata field\n");
134 assert(pam->
maxval == 255);
139 printf(
"pam.c: couldn't read body\n");
154 FILE *f = fopen(outpath,
"w+");
158 const char *tupl = NULL;
161 tupl =
"GRAYSCALE_ALPHA";
176 fprintf(f,
"P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
179 if (len != fwrite(pam->
data, 1, len, f)) {
216 if (type == in->
type)
220 assert(in->
maxval == 255);
235 assert(in->
depth == 3);
236 for (
int y = 0; y < h; y++) {
237 for (
int x = 0; x < w; x++) {
238 out->
data[y*4*w + 4*x + 0] = in->
data[y*3*w + 3*x + 0];
239 out->
data[y*4*w + 4*x + 1] = in->
data[y*3*w + 3*x + 1];
240 out->
data[y*4*w + 4*x + 2] = in->
data[y*3*w + 3*x + 2];
241 out->
data[y*4*w + 4*x + 3] = 255;
245 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)
static TTYPENAME *TFN() copy(TTYPENAME *hash)