+static const struct img_format {
+ img_output_func *output;
+ char name[4];
+ bool enabled;
+} formats[] = {
+#if HAVE_LIBPNG
+ { img_output_png, "png", 1 },
+#endif
+ { img_output_pam, "pam", 1 },
+ { img_output_ppm, "ppm", 1 },
+ { img_output_pbm, "pbm", 1 },
+};
+
+static int lookup_format(const char *fmt)
+{
+ for (size_t i = 0; i < sizeof formats / sizeof formats[0]; i++) {
+ assert(!formats[i].name[sizeof formats[i].name - 1]);
+
+ if (!fmt && formats[i].enabled)
+ return i;
+
+ if (strcmp(formats[i].name, fmt))
+ continue;
+
+ if (!formats[i].enabled) {
+ tool_err(-1, "%s support disabled at build time", fmt);
+ return -1;
+ }
+
+ return i;
+ }
+
+ tool_err(-1, "unknown format %s", fmt);
+ return -1;
+}
+
+bool img_is_masked(unsigned char **mask, unsigned width, unsigned height)
+{
+ unsigned x, y;
+
+ for (x = y = 0; y < height; ++x < width || (x = 0, y++)) {
+ if (mask[y][x] == 0)
+ return true;
+ }
+
+ return false;
+}
+