+ if (override) {
+ rc = loadoverride(override, palette);
+ if (rc < 0)
+ return -1;
+ ret = 0;
+ }
+
+ /* If we literally have no palette data at all, may as well fail. */
+ if (ret < 0)
+ tool_err(-1, "no palette available.");
+ return ret;
+}
+
+/* Return true iff a divides b. */
+static bool divides(unsigned a, unsigned b)
+{
+ if (b == 0)
+ return true;
+ if (a == 0)
+ return false;
+
+ return b % a == 0;
+}
+
+/* Set n bits starting from offset in the bitmap. */
+static void
+set_bits(unsigned char *bitmap, unsigned long offset, unsigned long n)
+{
+ if (offset % CHAR_BIT) {
+ bitmap[offset/CHAR_BIT] |= (n >= CHAR_BIT ? -1u : (1u << n) - 1)
+ << offset%CHAR_BIT;
+
+ n -= MIN(n, CHAR_BIT - offset%CHAR_BIT);
+ offset += CHAR_BIT - offset%CHAR_BIT;
+ }
+
+ if (n > CHAR_BIT) {
+ memset(&bitmap[offset/CHAR_BIT], -1, n/CHAR_BIT);
+
+ offset += n - n%CHAR_BIT;
+ n %= CHAR_BIT;
+ }
+
+ if (n) {
+ bitmap[offset/CHAR_BIT] |= (1u << n) - 1;
+ }
+}
+
+static int decode_frame(struct lbx_image *img, unsigned n,
+ unsigned char *pixels, unsigned char *pixel_mask)
+{
+ unsigned x, y;
+ long rc;
+
+ rc = lbx_img_seek(img, n);
+ if (rc < 0) {
+ tool_err(-1, "frame %u: invalid frame: %s\n", n, lbx_errmsg());