diff --git a/include/gfx/main.hpp b/include/gfx/main.hpp index e4bedb13..32bc4802 100644 --- a/include/gfx/main.hpp +++ b/include/gfx/main.hpp @@ -40,6 +40,8 @@ struct Options { uint16_t top; uint16_t width; uint16_t height; + uint32_t right() const { return left + width * 8; } + uint32_t bottom() const { return top + height * 8; } } inputSlice{0, 0, 0, 0}; // -L (margins in clockwise order, like CSS) std::array maxNbTiles{UINT16_MAX, 0}; // -N uint16_t nbPalettes = 8; // -n diff --git a/src/gfx/main.cpp b/src/gfx/main.cpp index 9de3857e..1c50c4b7 100644 --- a/src/gfx/main.cpp +++ b/src/gfx/main.cpp @@ -863,7 +863,7 @@ int main(int argc, char *argv[]) { } fprintf( stderr, - "\tInput image slice: %" PRIu32 "x%" PRIu32 " pixels starting at (%" PRId32 ", %" PRId32 + "\tInput image slice: %" PRIu16 "x%" PRIu16 " pixels starting at (%" PRIu16 ", %" PRIu16 ")\n", options.inputSlice.width, options.inputSlice.height, diff --git a/src/gfx/process.cpp b/src/gfx/process.cpp index 369a4c86..826d585b 100644 --- a/src/gfx/process.cpp +++ b/src/gfx/process.cpp @@ -226,6 +226,30 @@ public: if (options.inputSlice.height == 0 && height % 8 != 0) { fatal("Image height (%" PRIu32 " pixels) is not a multiple of 8!", height); } + if (options.inputSlice.right() > width || options.inputSlice.bottom() > height) { + error( + "Image slice ((%" PRIu16 ", %" PRIu16 ") to (%" PRIu32 ", %" PRIu32 + ")) is outside the image bounds (%" PRIu32 "x%" PRIu32 ")!", + options.inputSlice.left, + options.inputSlice.top, + options.inputSlice.right(), + options.inputSlice.bottom(), + width, + height + ); + if (options.inputSlice.width % 8 == 0 && options.inputSlice.height % 8 == 0) { + fprintf( + stderr, + "note: Did you mean the slice \"%" PRIu32 ",%" PRIu32 ":%" PRId32 ",%" PRId32 + "\"? (width and height are in tiles, not pixels!)\n", + options.inputSlice.left, + options.inputSlice.top, + options.inputSlice.width / 8, + options.inputSlice.height / 8 + ); + } + giveUp(); + } pixels.resize(static_cast(width) * static_cast(height)); diff --git a/test/gfx/bad_slice.err b/test/gfx/bad_slice.err new file mode 100644 index 00000000..f3f7e357 --- /dev/null +++ b/test/gfx/bad_slice.err @@ -0,0 +1,3 @@ +error: Image slice ((2, 2) to (130, 130)) is outside the image bounds (20x20)! +note: Did you mean the slice "2,2:2,2"? (width and height are in tiles, not pixels!) +Conversion aborted after 1 error diff --git a/test/gfx/bad_slice.flags b/test/gfx/bad_slice.flags new file mode 100644 index 00000000..f55c2775 --- /dev/null +++ b/test/gfx/bad_slice.flags @@ -0,0 +1 @@ +-L 2,2:16,16 diff --git a/test/gfx/bad_slice.png b/test/gfx/bad_slice.png new file mode 100644 index 00000000..8a0bd2d5 Binary files /dev/null and b/test/gfx/bad_slice.png differ diff --git a/test/gfx/oob_slice_wh.err b/test/gfx/oob_slice_wh.err new file mode 100644 index 00000000..b49ccf3f --- /dev/null +++ b/test/gfx/oob_slice_wh.err @@ -0,0 +1,2 @@ +error: Image slice ((2, 2) to (34, 18)) is outside the image bounds (20x20)! +Conversion aborted after 1 error diff --git a/test/gfx/oob_slice_wh.flags b/test/gfx/oob_slice_wh.flags new file mode 100644 index 00000000..d820a521 --- /dev/null +++ b/test/gfx/oob_slice_wh.flags @@ -0,0 +1 @@ +-L 2,2:4,2 diff --git a/test/gfx/oob_slice_wh.png b/test/gfx/oob_slice_wh.png new file mode 100644 index 00000000..8a0bd2d5 Binary files /dev/null and b/test/gfx/oob_slice_wh.png differ diff --git a/test/gfx/oob_slice_xy.err b/test/gfx/oob_slice_xy.err new file mode 100644 index 00000000..9a9e02ed --- /dev/null +++ b/test/gfx/oob_slice_xy.err @@ -0,0 +1,2 @@ +error: Image slice ((2, 99) to (10, 107)) is outside the image bounds (20x20)! +Conversion aborted after 1 error diff --git a/test/gfx/oob_slice_xy.flags b/test/gfx/oob_slice_xy.flags new file mode 100644 index 00000000..2cd08ba5 --- /dev/null +++ b/test/gfx/oob_slice_xy.flags @@ -0,0 +1 @@ +-L 2,99:1,1 diff --git a/test/gfx/oob_slice_xy.png b/test/gfx/oob_slice_xy.png new file mode 100644 index 00000000..8a0bd2d5 Binary files /dev/null and b/test/gfx/oob_slice_xy.png differ