+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 long npixels = (unsigned long) width * height;
+ unsigned long mask_sz = npixels / CHAR_BIT + (npixels % CHAR_BIT != 0);
+
+ for (unsigned long i = 0; i < mask_sz; i++) {
+ if (i+1 < mask_sz) {
+ if (mask[i] != (unsigned char)-1)
+ return true;
+ } else {
+ unsigned char test = (1u << npixels % CHAR_BIT) - 1;
+
+ if ((mask[i] & test) != test)
+ return true;
+ }
+ }
+
+ return false;
+}
+