Add PAM support to map command

Still no alpha support (yet), even though PAM supports it.
This commit is contained in:
Ron Nazarov 2023-09-30 15:11:37 +01:00
parent 215d95f863
commit e6c9d88d60
Signed by: noisytoot
GPG key ID: 1D43EF4F4492268B

View file

@ -30,6 +30,12 @@
#define BUFSIZE 512
enum image_format {
IMGFMT_PPM_TEXT,
IMGFMT_PPM_BINARY,
IMGFMT_PAM,
};
#define is_newline(c) ((c) == '\n' || (c) == '\r')
#define is_whitespace(c) (is_newline(c) || (c) == '\t' || (c) == ' ')
@ -256,10 +262,16 @@ colour *read_colourmap(FILE *stream, mts_header *header) {
return colourmap;
}
void write_ppm(FILE *f, image *img, bool text) {
fprintf(f, "P%c\n%d %d\n255\n",
text ? '3' : '6',
img->width, img->height);
void write_pnm(FILE *f, image *img, enum image_format imgfmt) {
bool text = imgfmt == IMGFMT_PPM_TEXT;
if (imgfmt == IMGFMT_PAM)
fprintf(f, "P7\nWIDTH %d\nHEIGHT %d\nDEPTH 3\n"
"MAXVAL 255\nTUPLTYPE RGB\nENDHDR\n",
img->width, img->height);
else
fprintf(f, "P%c\n%d %d\n255\n",
text ? '3' : '6',
img->width, img->height);
for (uint16_t x = 0; x < img->width; x++) {
for (uint16_t z = 0; z < img->height; z++) {
colour c = img->colourmap[img->pixmap[img->width * x + z]];
@ -375,7 +387,7 @@ int cmd_count(int argc, char *argv[], mts *schematic) {
int cmd_map(int argc, char *argv[], mts *schematic) {
image img;
bool text;
enum image_format imgfmt;
int y = atoi(argv[1]);
if (y < 0 || y >= schematic->header.size_y) {
fprintf(stderr, "Depth must be between 0 and %d\n", schematic->header.size_y - 1);
@ -397,15 +409,17 @@ int cmd_map(int argc, char *argv[], mts *schematic) {
if (argc > 2) {
char *format = argv[2];
if (!strcmp(format, "ppm-binary"))
text = false;
imgfmt = IMGFMT_PPM_BINARY;
else if (!strcmp(format, "ppm-text"))
text = true;
imgfmt = IMGFMT_PPM_TEXT;
else if (!strcmp(format, "pam"))
imgfmt = IMGFMT_PAM;
else {
fprintf(stderr, "Unknown format: %s (expected ppm-binary or ppm-text)\n", argv[2]);
fprintf(stderr, "Unknown format: %s (expected ppm-binary, ppm-text, or pam)\n", argv[2]);
return 1;
}
} else text = true;
write_ppm(stdout, &img, text);
} else imgfmt = IMGFMT_PPM_TEXT;
write_pnm(stdout, &img, imgfmt);
free(img.pixmap);
free(img.colourmap);
return 0;
@ -432,9 +446,9 @@ struct {
{ "count", 1, "<name>", cmd_count, true,
"Count all nodes of a certain type by name." },
{ "map", 1, "<depth> [<format>]", cmd_map, true,
"Make a PPM map of all nodes in a single layer.\n"
"Make an image map of all nodes in a single layer.\n"
"Takes name->colour map from stdin.\n"
"<format> may be ppm-text or ppm-binary, defaults to ppm-text." },
"<format> may be ppm-text, ppm-binary, or pam. Defaults to ppm-text." },
};
void usage(int status) {