@@ -40,8 +40,8 @@ bool Tileset::refresh_inactive_image() {
4040
4141bool Tileset::draw_tile (const Tile_State *ts, int x, int y, bool active) const {
4242 int index = (int )ts->id - _start_id + _offset;
43- int limit = (int )_num_tiles - _offset ;
44- if (_length > 0 && _length < limit ) { limit = _length + _offset; }
43+ int limit = (int )_num_tiles;
44+ if (_length > 0 ) { limit = MIN (limit, _length + _offset) ; }
4545 if (index < _offset || index >= limit) { return false ; }
4646
4747 Fl_RGB_Image *img = active ? _image : _inactive_image;
@@ -66,8 +66,8 @@ bool Tileset::draw_tile(const Tile_State *ts, int x, int y, bool active) const {
6666
6767bool Tileset::print_tile (const Tile_State *ts, int x, int y) const {
6868 int index = (int )ts->id - _start_id + _offset;
69- int limit = (int )_num_tiles - _offset ;
70- if (_length > 0 && _length < limit ) { limit = _length + _offset; }
69+ int limit = (int )_num_tiles;
70+ if (_length > 0 ) { limit = MIN (limit, _length + _offset) ; }
7171 if (index < _offset || index >= limit || !_image) { return false ; }
7272
7373 int wt = _image->w () / TILE_SIZE_2X;
@@ -194,7 +194,10 @@ static void convert_2bpp_row(uchar b1, uchar b2, Hue *row) {
194194Tileset::Result Tileset::parse_1bpp_data (size_t n, uchar *data) {
195195 n /= BYTES_PER_1BPP_TILE;
196196 _num_tiles = n;
197- if (_start_id + _num_tiles > NUM_TILES) { delete [] data; return (_result = TILESET_TOO_LARGE); }
197+
198+ int limit = (int )_num_tiles - _offset;
199+ if (_length > 0 ) { limit = MIN (limit, _length + _offset); }
200+ if (_start_id + limit > NUM_TILES) { delete [] data; return (_result = TILESET_TOO_LARGE); }
198201
199202 Fl_Image_Surface *surface = new Fl_Image_Surface (TILE_SIZE, (int )n * TILE_SIZE);
200203 surface->set_current ();
@@ -223,7 +226,10 @@ Tileset::Result Tileset::parse_1bpp_data(size_t n, uchar *data) {
223226Tileset::Result Tileset::parse_2bpp_data (size_t n, uchar *data) {
224227 n /= BYTES_PER_2BPP_TILE;
225228 _num_tiles = n;
226- if (_start_id + _num_tiles > NUM_TILES) { delete [] data; return (_result = TILESET_TOO_LARGE); }
229+
230+ int limit = (int )_num_tiles - _offset;
231+ if (_length > 0 ) { limit = MIN (limit, _length + _offset); }
232+ if (_start_id + limit > NUM_TILES) { delete [] data; return (_result = TILESET_TOO_LARGE); }
227233
228234 Fl_Image_Surface *surface = new Fl_Image_Surface (TILE_SIZE, (int )n * TILE_SIZE);
229235 surface->set_current ();
@@ -267,7 +273,10 @@ Tileset::Result Tileset::postprocess_graphics(Fl_RGB_Image *img) {
267273 w /= TILE_SIZE_2X;
268274 h /= TILE_SIZE_2X;
269275 _num_tiles = w * h;
270- if (_start_id + _num_tiles > NUM_TILES) { return (_result = TILESET_TOO_LARGE); }
276+
277+ int limit = (int )_num_tiles - _offset;
278+ if (_length > 0 ) { limit = MIN (limit, _length + _offset); }
279+ if (_start_id + limit > NUM_TILES) { return (_result = TILESET_TOO_LARGE); }
271280
272281 return (_result = TILESET_OK);
273282}
0 commit comments