1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
use std::num::Wrapping;
use std::u16;

use crate::{AllocatorOptions, DEFAULT_OPTIONS, Allocation, AllocId, Size, Rectangle, point2, size2};

const BIN_BITS: u32 = 12;
const ITEM_BITS: u32 = 12;
const GEN_BITS: u32 = 8;

const BIN_MASK: u32   = (1 << BIN_BITS) - 1;
const ITEM_MASK: u32  = ((1 << ITEM_BITS) - 1) << BIN_BITS;
const GEN_MASK: u32   = ((1 << GEN_BITS) - 1) << (BIN_BITS + ITEM_BITS);

const MAX_ITEMS_PER_BIN: u16 = (ITEM_MASK >> 12) as u16;
const MAX_BIN_COUNT: usize = BIN_MASK as usize;
const MAX_SHELF_COUNT: usize = u16::MAX as usize;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
struct BucketIndex(u16);

impl BucketIndex {
    fn to_usize(self) -> usize {
        self.0 as usize
    }

    const INVALID: Self = BucketIndex(u16::MAX);
}

#[derive(Clone)]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
struct Shelf {
    x: u16,
    y: u16,
    height: u16,
    bucket_width: u16,

    first_bucket: BucketIndex,
}

#[derive(Clone)]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
struct Bucket {
    x: u16,
    free_space: u16,

    next: BucketIndex,

    /// Buckets are cleared when their reference count goes back to zero.
    refcount: u16,
    /// Similar to refcount except that the counter is not decremented
    /// when an item is deallocated. We only use this so that allocation
    /// ids are unique within a bucket.
    item_count: u16,
    shelf: u16,
    generation: Wrapping<u8>,
}

/// A faster but less precise Shelf-packing dynamic texture atlas allocator, inspired by https://github.com/mapbox/shelf-pack/
///
/// Items are accumulated into buckets which are laid out in rows (shelves) of variable height.
/// When allocating we first look for a suitable bucket. If none is found, a new shelf of the desired height
/// is pushed.
///
/// Lifetime isn't tracked at item granularity. Instead, items are grouped into buckets and deallocation happens
/// per bucket when all items of the buckets are removed.
/// When the top-most shelf is empty, it is removed, potentially cascading into garbage-collecting the next
/// shelf, etc.
///
/// This allocator works well when there are a lot of small items with similar sizes (typically, glyph atlases).
#[derive(Clone)]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
pub struct BucketedAtlasAllocator {
    shelves: Vec<Shelf>,
    buckets: Vec<Bucket>,
    available_height: u16,
    width: u16,
    height: u16,
    first_unallocated_bucket: BucketIndex,
    flip_xy: bool,
    alignment: Size,
    current_column: u16,
    column_width: u16,
    num_columns: u16,
    allocated_space: i32,
}

impl BucketedAtlasAllocator {
    /// Create an atlas allocator with provided options.
    pub fn with_options(size: Size, options: &AllocatorOptions) -> Self {
        assert!(size.width < u16::MAX as i32);
        assert!(size.height < u16::MAX as i32);

        let (width, height, shelf_alignment) = if options.vertical_shelves {
            (size.height as u16, size.width as u16, options.alignment.height as u16)
        } else {
            (size.width as u16, size.height as u16, options.alignment.width as u16)
        };

        let mut column_width = width / (options.num_columns as u16);
        column_width = column_width - column_width % shelf_alignment;

        BucketedAtlasAllocator {
            shelves: Vec::new(),
            buckets: Vec::new(),
            available_height: height,
            width,
            height,
            first_unallocated_bucket: BucketIndex::INVALID,
            flip_xy: options.vertical_shelves,
            alignment: options.alignment,
            current_column: 0,
            num_columns: options.num_columns as u16,
            column_width,
            allocated_space: 0,
        }
    }

    /// Create an atlas allocator with default options.
    pub fn new(size: Size) -> Self {
        Self::with_options(size, &DEFAULT_OPTIONS)
    }

    pub fn clear(&mut self) {
        self.shelves.clear();
        self.buckets.clear();
        self.first_unallocated_bucket = BucketIndex::INVALID;
        self.available_height = self.height;
        self.current_column = 0;
        self.allocated_space = 0;
    }

    pub fn size(&self) -> Size {
        let (w, h) = convert_coordinates(self.flip_xy, self.width, self.height);
        size2(w as i32, h as i32)
    }

    pub fn grow(&mut self, new_size: Size) {
        assert!(new_size.width < u16::MAX as i32);
        assert!(new_size.height < u16::MAX as i32);

        let (new_width, new_height) = if self.flip_xy {
            (new_size.height as u16, new_size.width as u16)
        } else {
            (new_size.width as u16, new_size.height as u16)
        };

        assert!(new_width >= self.width);
        assert!(new_height >= self.height);

        self.available_height += new_height - self.height;
        self.width = new_width;
        self.height = new_height;

        if self.num_columns == 1 {
            // Add as many new buckets as possible to the existing shelves.
            let additional_width = self.width - self.column_width;

            let len = self.shelves.len();

            for shelf_index in 0..len {
                let shelf = &self.shelves[shelf_index];
                let mut x = self.column_width;
                let bucket_width = shelf.bucket_width;

                let max_new_buckets = (MAX_BIN_COUNT - self.buckets.len()) as u16;
                let mut num_buckets_to_add = additional_width / bucket_width;
                num_buckets_to_add = num_buckets_to_add.min(max_new_buckets);

                let mut bucket_next = shelf.first_bucket;

                for _ in 0..num_buckets_to_add {
                    let bucket = Bucket {
                        next: bucket_next,
                        x,
                        free_space: bucket_width,
                        refcount: 0,
                        shelf: shelf_index as u16,
                        generation: Wrapping(0),
                        item_count: 0,
                    };

                    x += bucket_width;

                    let bucket_index = self.add_bucket(bucket);

                    bucket_next = bucket_index;
                }

                self.shelves[shelf_index].first_bucket = bucket_next;
            }

            // Resize the existing column.
            self.column_width = self.width;
        } else {
            // Add as many new columns as possible.
            self.num_columns = self.width / self.column_width;
        }
    }

    pub fn is_empty(&self) -> bool {
        self.shelves.is_empty()
    }

    /// Allocate a rectangle in the atlas.
    pub fn allocate(&mut self, mut requested_size: Size) -> Option<Allocation> {
        if requested_size.is_empty()
            || requested_size.width > std::u16::MAX as i32
            || requested_size.height > std::u16::MAX as i32 {
            return None;
        }

        adjust_size(self.alignment.width, &mut requested_size.width);
        adjust_size(self.alignment.height, &mut requested_size.height);

        if requested_size.width > self.column_width as i32 || requested_size.height > self.height as i32 {
            return None;
        }

        let (w, h) = convert_coordinates(self.flip_xy, requested_size.width as u16, requested_size.height as u16);

        let mut selected_shelf = std::usize::MAX;
        let mut selected_bucket = BucketIndex::INVALID;
        let mut best_waste = u16::MAX;

        let can_add_shelf = (self.available_height >= h || self.current_column + 1 < self.num_columns)
            && self.shelves.len() < MAX_SHELF_COUNT
            && self.buckets.len() < MAX_BIN_COUNT;

        'shelves: for (shelf_index, shelf) in self.shelves.iter().enumerate() {
            if shelf.height < h || shelf.bucket_width < w {
                continue;
            }

            let y_waste = shelf.height - h;
            if y_waste > best_waste || (can_add_shelf && y_waste > h) {
                continue;
            }

            let mut bucket_index = shelf.first_bucket;
            while bucket_index != BucketIndex::INVALID {
                let bucket = &self.buckets[bucket_index.to_usize()];

                if bucket.free_space >= w && bucket.item_count < MAX_ITEMS_PER_BIN {
                    if y_waste == 0 && bucket.free_space == w {
                        selected_shelf = shelf_index;
                        selected_bucket = bucket_index;

                        break 'shelves;
                    }

                    if y_waste < best_waste {
                        best_waste = y_waste;
                        selected_shelf = shelf_index;
                        selected_bucket = bucket_index;
                        break;
                    }
                }

                bucket_index = bucket.next;
            }
        }

        if selected_bucket == BucketIndex::INVALID {
            if can_add_shelf {
                selected_shelf = self.add_shelf(w, h);
                selected_bucket = self.shelves[selected_shelf].first_bucket;
            } else {
                // Attempt to merge some empty shelves to make a big enough spot.
                let selected = self.coalesce_shelves(w, h);
                selected_shelf = selected.0;
                selected_bucket = selected.1;
            }
        }

        if selected_bucket != BucketIndex::INVALID {
            return self.alloc_from_bucket(selected_shelf, selected_bucket, w);
        }

        return  None;
    }

    /// Deallocate a rectangle in the atlas.
    ///
    /// Space is only reclaimed when all items of the same bucket are deallocated.
    pub fn deallocate(&mut self, id: AllocId) {
        if self.deallocate_from_bucket(id) {
            self.cleanup_shelves();
        }

        self.check()
    }

    /// Amount of occupied space in the atlas.
    pub fn allocated_space(&self) -> i32 {
        self.allocated_space
    }

    /// How much space is available for future allocations.
    pub fn free_space(&self) -> i32 {
        (self.width as i32 * self.height as i32) - self.allocated_space
    }

    fn alloc_from_bucket(&mut self, shelf_index: usize, bucket_index: BucketIndex, width: u16) -> Option<Allocation> {
        let shelf = &mut self.shelves[shelf_index];
        let bucket = &mut self.buckets[bucket_index.to_usize()];

        debug_assert!(bucket.free_space >= width);

        let min_x = bucket.x + shelf.bucket_width - bucket.free_space;
        let min_y = shelf.y;
        let max_x = min_x + width;
        let max_y = min_y + shelf.height;

        let (min_x, min_y) = convert_coordinates(self.flip_xy, min_x, min_y);
        let (max_x, max_y) = convert_coordinates(self.flip_xy, max_x, max_y);

        bucket.free_space -= width;
        bucket.refcount += 1;
        bucket.item_count += 1;

        let id = AllocId(
            (bucket_index.0 as u32) & BIN_MASK
            | ((bucket.item_count as u32) << 12) & ITEM_MASK
            | (bucket.generation.0 as u32) << 24
        );

        let rectangle = Rectangle {
            min: point2(min_x as i32, min_y as i32),
            max: point2(max_x as i32, max_y as i32),
        };

        self.allocated_space += rectangle.size().area();

        self.check();

        Some(Allocation { id, rectangle })
    }

    fn add_bucket(&mut self, mut bucket: Bucket) -> BucketIndex {
        let mut bucket_index = self.first_unallocated_bucket;

        if bucket_index == BucketIndex::INVALID {
            bucket_index = BucketIndex(self.buckets.len() as u16);
            self.buckets.push(bucket);
        } else {
            let idx = bucket_index.to_usize();
            bucket.generation = self.buckets[idx].generation + Wrapping(1);
            self.first_unallocated_bucket = self.buckets[idx].next;
            self.buckets[idx] = bucket;
        }

        bucket_index
    }

    fn add_shelf(&mut self, width: u16, height: u16) -> usize {

        let can_add_column = self.current_column + 1 < self.num_columns;

        if self.available_height != 0 && self.available_height < height && can_add_column {
            // We have room to add a shelf in a new column but current one doesn't have
            // enough available space. First add a shelf to fill the current column's
            // remaining height.
            self.add_shelf(0, self.available_height);
            debug_assert_eq!(self.available_height, 0);
        }

        if self.available_height == 0 && can_add_column {
            self.current_column += 1;
            self.available_height = self.height;
        }

        let height = shelf_height(height).min(self.available_height);
        let num_buckets = self.num_buckets(width, height);
        let mut bucket_width = self.column_width / num_buckets;
        bucket_width = bucket_width - (bucket_width % self.alignment.width as u16); // TODO
        let y = self.height - self.available_height;
        self.available_height -= height;

        let shelf_index = self.shelves.len();

        // Initialize the buckets for our new shelf.
        let mut x = self.current_column * self.column_width;
        let mut bucket_next = BucketIndex::INVALID;
        for _ in 0..num_buckets {
            let bucket = Bucket {
                next: bucket_next,
                x,
                free_space: bucket_width,
                refcount: 0,
                shelf: shelf_index as u16,
                generation: Wrapping(0),
                item_count: 0,
            };

            x += bucket_width;

            let bucket_index = self.add_bucket(bucket);

            bucket_next = bucket_index;
        }

        self.shelves.push(Shelf {
            x: self.current_column * self.column_width,
            y,
            height,
            bucket_width,
            first_bucket: bucket_next,
        });

        shelf_index
    }

    /// Find a sequence of consecutive shelves that can be coalesced into a single one
    /// tall enough to fit the provided size.
    ///
    /// If such a sequence is found, grow the height of first shelf and squash the other
    /// ones to zero.
    /// The squashed shelves are not removed, their height is just set to zero so no item
    /// can go in, and they will be garbage-collected whenever there's no shelf above them.
    /// For simplicity, the bucket width is not modified.

    fn coalesce_shelves(&mut self, w: u16, h: u16) -> (usize, BucketIndex) {
        let len = self.shelves.len();
        let mut coalesce_range = None;
        let mut coalesced_height = 0;

        'outer: for shelf_index in 0..len {
            if self.shelves[shelf_index].bucket_width < w {
                continue;
            }
            if !self.shelf_is_empty(shelf_index) {
                continue;
            }
            let shelf_x = self.shelves[shelf_index].x;
            coalesced_height = self.shelves[shelf_index].height;
            for i in 1..3 {
                if self.shelves[shelf_index + i].x != shelf_x {
                    // Can't coalesce shelves from different columns.
                    continue 'outer;
                }

                if shelf_index + i >= len {
                    break 'outer;
                }

                if !self.shelf_is_empty(shelf_index + i) {
                    continue 'outer;
                }

                coalesced_height += self.shelves[shelf_index + i].height;

                if coalesced_height >= h {
                    coalesce_range = Some(shelf_index .. (shelf_index + i + 1));
                    break 'outer;
                }
            }
        }

        if let Some(range) = coalesce_range {
            let y_top = self.shelves[range.start].y + coalesced_height;
            for i in range.start + 1 .. range.end {
                self.shelves[i].y = y_top;
                self.shelves[i].height = 0;
            }

            let shelf_index = range.start;
            let shelf = &mut self.shelves[shelf_index];
            shelf.height = coalesced_height;

            return (shelf_index, shelf.first_bucket);
        }

        (0, BucketIndex::INVALID)
    }

    fn num_buckets(&self, width: u16, height: u16) -> u16 {
        match self.column_width / u16::max(width, height) {
            0 ..= 4 => 1,
            5 ..= 16 => 2,
            17 ..= 32 => 4,
            n => (n /16 - 1).next_power_of_two(),
        }.min((MAX_BIN_COUNT - self.buckets.len()) as u16)
    }

    /// Returns true if we should garbage-collect the shelves as a result of
    /// removing this element (we deallocated the last item from the bucket on
    /// the top-most shelf).
    fn deallocate_from_bucket(&mut self, id: AllocId) -> bool {
        let bucket_index = (id.0 & BIN_MASK) as usize;
        let generation = ((id.0 & GEN_MASK) >> 24 ) as u8;

        let bucket = &mut self.buckets[bucket_index];

        let expected_generation = bucket.generation.0;
        assert_eq!(generation, expected_generation);

        assert!(bucket.refcount > 0);
        bucket.refcount -= 1;

        let shelf = &self.shelves[bucket.shelf as usize];

        let bucket_is_empty = bucket.refcount == 0;
        if bucket_is_empty {
            self.allocated_space -= (shelf.bucket_width - bucket.free_space) as i32 * shelf.height as i32;
            bucket.free_space = shelf.bucket_width;
        }

        bucket_is_empty && bucket.shelf as usize == self.shelves.len() - 1
    }

    fn cleanup_shelves(&mut self) {
        while self.shelves.len() > 0 {
            {
                let shelf = self.shelves.last().unwrap();
                let mut bucket_index = shelf.first_bucket;
                let mut last_bucket = shelf.first_bucket;

                while bucket_index != BucketIndex::INVALID {
                    let bucket = &self.buckets[bucket_index.to_usize()];

                    if bucket.refcount != 0 {
                        return;
                    }

                    last_bucket = bucket_index;
                    bucket_index = bucket.next;
                }

                // We didn't run into any bucket on this shelf with live elements,
                // this means we can remove it.

                // Can't have a shelf with no buckets.
                debug_assert!(last_bucket != BucketIndex::INVALID);
                // Add the buckets to the free list.
                self.buckets[last_bucket.to_usize()].next = self.first_unallocated_bucket;
                self.first_unallocated_bucket = shelf.first_bucket;

                if shelf.y == 0 && self.current_column > 0 {
                    self.current_column -= 1;
                    let prev_shelf = &self.shelves[self.shelves.len() - 2];
                    self.available_height = self.height - (prev_shelf.y + prev_shelf.height);
                } else {
                    // Reclaim the height of the shelf.
                    self.available_height += shelf.height;
                }
            }

            self.shelves.pop();
        }
    }

    fn shelf_is_empty(&self, idx: usize) -> bool {
        let shelf = &self.shelves[idx];
        let mut bucket_index = shelf.first_bucket;

        while bucket_index != BucketIndex::INVALID {
            let bucket = &self.buckets[bucket_index.to_usize()];

            if bucket.refcount != 0 {
                return false;
            }

            bucket_index = bucket.next;
        }

        true
    }


    /// Dump a visual representation of the atlas in SVG format.
    pub fn dump_svg(&self, output: &mut dyn std::io::Write) -> std::io::Result<()> {
        use svg_fmt::*;

        writeln!(
            output,
            "{}",
            BeginSvg {
                w: self.width as f32,
                h: self.height as f32
            }
        )?;

        self.dump_into_svg(None, output)?;

        writeln!(output, "{}", EndSvg)
    }


    #[cfg(not(feature = "checks"))]
    fn check(&self) {}

    #[cfg(feature = "checks")]
    fn check(&self) {
        let mut h = 0;
        for shelf in &self.shelves {
            h += shelf.height;
        }
        h += self.available_height;

        // Total height must be a multiple of the actual height, up to height * num_columns.
        assert_eq!(h % self.height, 0);
        assert!(h <= self.height * self.num_columns);
        assert!(h >= self.height);

        assert_eq!(self.is_empty(), self.allocated_space() == 0)
    }

    /// Dump a visual representation of the atlas in SVG, omitting the beginning and end of the
    /// SVG document, so that it can be included in a larger document.
    ///
    /// If a rectangle is provided, translate and scale the output to fit it.
    pub fn dump_into_svg(&self, rect: Option<&Rectangle>, output: &mut dyn std::io::Write) -> std::io::Result<()> {
        use svg_fmt::*;

        let (sx, sy, tx, ty) = if let Some(rect) = rect {
            (
                rect.size().width as f32 / self.width as f32,
                rect.size().height as f32 / self.height as f32,
                rect.min.x as f32,
                rect.min.y as f32,
            )
        } else {
            (1.0, 1.0, 0.0, 0.0)
        };

        writeln!(
            output,
            r#"    {}"#,
            rectangle(tx, ty, self.width as f32 * sx, self.height as f32 * sy)
                .fill(rgb(40, 40, 40))
                .stroke(Stroke::Color(black(), 1.0))
        )?;


        for shelf in &self.shelves {
            let mut bucket_index = shelf.first_bucket;

            let y = shelf.y as f32 * sy;
            let h = shelf.height as f32 * sy;
            while bucket_index != BucketIndex::INVALID {
                let bucket = &self.buckets[bucket_index.to_usize()];

                let x = bucket.x as f32 * sx;
                let w = (shelf.bucket_width - bucket.free_space) as f32 * sx;

                {
                    let (x, y) = if self.flip_xy { (y, x) } else { (x, y) };
                    let (w, h) = if self.flip_xy { (h, w) } else { (w, h) };

                    writeln!(
                        output,
                        r#"    {}"#,
                        rectangle(x + tx, y + ty, w, h)
                            .fill(rgb(70, 70, 180))
                            .stroke(Stroke::Color(black(), 1.0))
                    )?;
                }

                if bucket.free_space > 0 {
                    let x_free = x + w;
                    let w_free = bucket.free_space as f32 * sx;

                    let (x_free, y) = if self.flip_xy { (y, x_free) } else { (x_free, y) };
                    let (w_free, h) = if self.flip_xy { (h, w_free) } else { (w_free, h) };

                    writeln!(
                        output,
                        r#"    {}"#,
                        rectangle(x_free + tx, y + ty, w_free, h)
                            .fill(rgb(50, 50, 50))
                            .stroke(Stroke::Color(black(), 1.0))
                    )?;
                }

                bucket_index = bucket.next;
            }
        }

        Ok(())
    }
}

fn convert_coordinates(flip_xy: bool, x: u16, y: u16) -> (u16, u16) {
    if flip_xy {
        (y, x)
    } else {
        (x, y)
    }
}


fn shelf_height(mut size: u16) -> u16 {
    let alignment = match size {
        0 ..= 31 => 8,
        32 ..= 127 => 16,
        128 ..= 511 => 32,
        _ => 64,
    };

    let rem = size % alignment;
    if rem > 0 {
        size += alignment - rem;
    }

    size
}

fn adjust_size(alignment: i32, size: &mut i32) {
    let rem = *size % alignment;
    if rem > 0 {
        *size += alignment - rem;
    }
}

#[test]
fn atlas_basic() {
    let mut atlas = BucketedAtlasAllocator::new(size2(1000, 1000));

    let full = atlas.allocate(size2(1000, 1000)).unwrap().id;
    assert!(atlas.allocate(size2(1, 1)).is_none());

    atlas.deallocate(full);
    let a = atlas.allocate(size2(10, 10)).unwrap().id;
    let b = atlas.allocate(size2(50, 30)).unwrap().id;
    let c = atlas.allocate(size2(12, 45)).unwrap().id;
    let d = atlas.allocate(size2(60, 45)).unwrap().id;
    let e = atlas.allocate(size2(1, 1)).unwrap().id;
    let f = atlas.allocate(size2(128, 128)).unwrap().id;
    let g = atlas.allocate(size2(256, 256)).unwrap().id;

    atlas.deallocate(b);
    atlas.deallocate(f);
    atlas.deallocate(c);
    atlas.deallocate(e);
    let h = atlas.allocate(size2(500, 200)).unwrap().id;
    atlas.deallocate(a);
    let i = atlas.allocate(size2(500, 200)).unwrap().id;
    atlas.deallocate(g);
    atlas.deallocate(h);
    atlas.deallocate(d);
    atlas.deallocate(i);

    let full = atlas.allocate(size2(1000, 1000)).unwrap().id;
    assert!(atlas.allocate(size2(1, 1)).is_none());
    atlas.deallocate(full);
}

#[test]
fn test_coalesce_shelves() {
    let mut atlas = BucketedAtlasAllocator::new(size2(256, 256));

    // Allocate 7 shelves (leaving 32px of remaining space on top).
    let mut ids = Vec::new();
    for _ in 0..7 {
        for _ in 0..8 {
            ids.push(atlas.allocate(size2(32, 32)).unwrap().id)
        }
    }

    // Free the first shelf.
    for i in 0..8 {
        atlas.deallocate(ids[i]);
    }

    // Free the 3rd and 4th shelf.
    for i in 16..32 {
        atlas.deallocate(ids[i]);
    }

    // Not enough space left in existing shelves and above.
    // even coalescing is not sufficient.
    assert!(atlas.allocate(size2(70, 70)).is_none());

    // Not enough space left in existing shelves and above.
    // The 3rd and 4th row can be coalesced to fit this allocation, though.
    let id = atlas.allocate(size2(64, 64)).unwrap().id;

    // Deallocate everything
    for i in 8..16 {
        atlas.deallocate(ids[i]);
    }

    atlas.deallocate(id);

    for i in 32..56 {
        atlas.deallocate(ids[i]);
    }

    //dump_svg(&atlas, &mut std::fs::File::create("tmp.svg").expect("!!"));

    assert!(atlas.is_empty());
    assert_eq!(atlas.allocated_space(), 0);
}

#[test]
fn grow_vertically() {
    let mut atlas = BucketedAtlasAllocator::new(size2(256, 256));

    // Allocate 7 shelves (leaving 32px of remaining space on top).
    let mut ids = Vec::new();
    for _ in 0..7 {
        for _ in 0..8 {
            ids.push(atlas.allocate(size2(32, 32)).unwrap().id)
        }
    }

    // Free the first shelf.
    for i in 0..8 {
        atlas.deallocate(ids[i]);
    }

    // Free the 3rd and 4th shelf.
    for i in 16..32 {
        atlas.deallocate(ids[i]);
    }

    // Not enough space left in existing shelves and above.
    // even coalescing is not sufficient.
    assert!(atlas.allocate(size2(70, 70)).is_none());

    // Grow just enough vertically to fit the previous region
    atlas.grow(size2(256, 256 + 70 - 32));

    // Allocation should succeed now
    assert!(atlas.allocate(size2(70, 70)).is_some());
}

#[test]
fn grow_horizontally() {
    let mut atlas = BucketedAtlasAllocator::new(size2(256, 256));

    // Allocate 7 shelves (leaving 32px of remaining space on top).
    let mut ids = Vec::new();
    for _ in 0..7 {
        for _ in 0..8 {
            ids.push(atlas.allocate(size2(32, 32)).unwrap().id)
        }
    }

    // Free the first shelf.
    for i in 0..8 {
        atlas.deallocate(ids[i]);
    }

    // Free the 3rd and 4th shelf.
    for i in 16..32 {
        atlas.deallocate(ids[i]);
    }

    // Not enough space left in existing shelves and above.
    // even coalescing is not sufficient.
    assert!(atlas.allocate(size2(512, 32)).is_none());

    // Grow just enough horizontally to add more buckets
    atlas.grow(size2(256 * 2, 256));

    // Allocation should succeed now
    assert!(atlas.allocate(size2(512, 32)).is_some());
}

#[test]
fn grow_to_fit_allocation() {
    let mut atlas = BucketedAtlasAllocator::new(size2(32, 32));

    // Allocate a shelve to make sure we have a non-empty atlas to test the update.
    atlas.allocate(size2(32, 32)).unwrap();

    // Try to make a big allocation that doesn't fit.
    let big_allocation = size2(256, 256);

    assert!(atlas.allocate(big_allocation).is_none());

    // Grow to make enough space for the wanted allocation plus the original shelf.
    atlas.grow(size2(256, 32 + 256));

    // Adding to the original shelf should succeed.
    assert!(atlas.allocate(size2(32, 32)).is_some());

    // Big allocation should also succeed now.
    assert!(atlas.allocate(big_allocation).is_some());
}

#[test]
fn columns() {
    let mut atlas = BucketedAtlasAllocator::with_options(size2(64, 64), &AllocatorOptions {
        num_columns: 2,
        ..DEFAULT_OPTIONS
    });

    let a = atlas.allocate(size2(24, 46)).unwrap();
    let b = atlas.allocate(size2(24, 32)).unwrap();
    let c = atlas.allocate(size2(24, 32)).unwrap();

    fn in_range(val: i32, range: std::ops::Range<i32>) -> bool {
        let ok = val >= range.start && val < range.end;

        if !ok {
            println!("{:?} not in {:?}", val, range);
        }

        ok
    }

    assert!(in_range(a.rectangle.min.x, 0..32));
    assert!(in_range(a.rectangle.max.x, 0..32));
    assert!(in_range(b.rectangle.min.x, 32..64));
    assert!(in_range(b.rectangle.max.x, 32..64));
    assert!(in_range(c.rectangle.min.x, 32..64));
    assert!(in_range(c.rectangle.max.x, 32..64));

    atlas.deallocate(b.id);
    atlas.deallocate(c.id);
    atlas.deallocate(a.id);

    assert!(atlas.is_empty());
    assert_eq!(atlas.allocated_space(), 0);

    let a = atlas.allocate(size2(24, 46)).unwrap();
    let b = atlas.allocate(size2(24, 32)).unwrap();
    let c = atlas.allocate(size2(24, 32)).unwrap();
    let d = atlas.allocate(size2(24, 8)).unwrap();

    assert_eq!(a.rectangle.min.x, 0);
    assert_eq!(b.rectangle.min.x, 32);
    assert_eq!(c.rectangle.min.x, 32);
    assert_eq!(d.rectangle.min.x, 0);
}

#[test]
fn vertical() {
    let mut atlas = BucketedAtlasAllocator::with_options(size2(128, 256), &AllocatorOptions {
        num_columns: 2,
        vertical_shelves: true,
        ..DEFAULT_OPTIONS
    });

    assert_eq!(atlas.size(), size2(128, 256));

    let a = atlas.allocate(size2(32, 16)).unwrap();
    let b = atlas.allocate(size2(16, 32)).unwrap();

    assert!(a.rectangle.size().width >= 32);
    assert!(a.rectangle.size().height >= 16);

    assert!(b.rectangle.size().width >= 16);
    assert!(b.rectangle.size().height >= 32);

    let c = atlas.allocate(size2(128, 128)).unwrap();

    atlas.deallocate(a.id);
    atlas.deallocate(b.id);
    atlas.deallocate(c.id);

    assert!(atlas.is_empty());
    assert_eq!(atlas.allocated_space(), 0);
}

#[test]
fn clear() {
    let mut atlas = BucketedAtlasAllocator::new(size2(2048, 2048));

    // Run a workload a few hundred times to make sure clearing properly resets everything.
    for _ in 0..500 {
        atlas.clear();
        assert_eq!(atlas.allocated_space(), 0);

        atlas.allocate(size2(8, 2)).unwrap();
        atlas.allocate(size2(2, 8)).unwrap();
        atlas.allocate(size2(16, 512)).unwrap();
        atlas.allocate(size2(34, 34)).unwrap();
        atlas.allocate(size2(34, 34)).unwrap();
        atlas.allocate(size2(34, 34)).unwrap();
        atlas.allocate(size2(34, 34)).unwrap();
        atlas.allocate(size2(2, 8)).unwrap();
        atlas.allocate(size2(2, 8)).unwrap();
        atlas.allocate(size2(8, 2)).unwrap();
        atlas.allocate(size2(2, 8)).unwrap();
        atlas.allocate(size2(8, 2)).unwrap();
        atlas.allocate(size2(8, 8)).unwrap();
        atlas.allocate(size2(8, 8)).unwrap();
        atlas.allocate(size2(8, 8)).unwrap();
        atlas.allocate(size2(8, 8)).unwrap();
        atlas.allocate(size2(82, 80)).unwrap();
        atlas.allocate(size2(56, 56)).unwrap();
        atlas.allocate(size2(64, 66)).unwrap();
        atlas.allocate(size2(32, 32)).unwrap();
        atlas.allocate(size2(32, 32)).unwrap();
        atlas.allocate(size2(32, 32)).unwrap();
        atlas.allocate(size2(32, 32)).unwrap();
        atlas.allocate(size2(32, 32)).unwrap();
        atlas.allocate(size2(32, 32)).unwrap();
        atlas.allocate(size2(32, 32)).unwrap();
        atlas.allocate(size2(32, 32)).unwrap();
        atlas.allocate(size2(32, 32)).unwrap();
        atlas.allocate(size2(40, 40)).unwrap();
        atlas.allocate(size2(32, 32)).unwrap();
        atlas.allocate(size2(256, 52)).unwrap();
        atlas.allocate(size2(32, 32)).unwrap();
        atlas.allocate(size2(256, 52)).unwrap();
        atlas.allocate(size2(256, 52)).unwrap();
        atlas.allocate(size2(256, 52)).unwrap();
        atlas.allocate(size2(256, 52)).unwrap();
        atlas.allocate(size2(256, 52)).unwrap();
        atlas.allocate(size2(256, 52)).unwrap();
        atlas.allocate(size2(155, 52)).unwrap();
        atlas.allocate(size2(256, 52)).unwrap();
        atlas.allocate(size2(32, 32)).unwrap();
        atlas.allocate(size2(32, 32)).unwrap();
        atlas.allocate(size2(32, 32)).unwrap();
        atlas.allocate(size2(24, 24)).unwrap();
        atlas.allocate(size2(64, 64)).unwrap();
        atlas.allocate(size2(32, 32)).unwrap();
        atlas.allocate(size2(84, 84)).unwrap();
        atlas.allocate(size2(32, 32)).unwrap();
        atlas.allocate(size2(8, 2)).unwrap();
        atlas.allocate(size2(34, 34)).unwrap();
        atlas.allocate(size2(34, 34)).unwrap();
        atlas.allocate(size2(192, 192)).unwrap();
        atlas.allocate(size2(192, 192)).unwrap();
        atlas.allocate(size2(52, 52)).unwrap();
        atlas.allocate(size2(144, 144)).unwrap();
        atlas.allocate(size2(192, 192)).unwrap();
        atlas.allocate(size2(32, 32)).unwrap();
        atlas.allocate(size2(144, 144)).unwrap();
        atlas.allocate(size2(24, 24)).unwrap();
        atlas.allocate(size2(192, 192)).unwrap();
        atlas.allocate(size2(192, 192)).unwrap();
        atlas.allocate(size2(432, 243)).unwrap();
        atlas.allocate(size2(32, 32)).unwrap();
        atlas.allocate(size2(8, 2)).unwrap();
        atlas.allocate(size2(2, 8)).unwrap();
        atlas.allocate(size2(9, 9)).unwrap();
        atlas.allocate(size2(14, 14)).unwrap();
        atlas.allocate(size2(14, 14)).unwrap();
        atlas.allocate(size2(14, 14)).unwrap();
        atlas.allocate(size2(14, 14)).unwrap();
        atlas.allocate(size2(8, 8)).unwrap();
        atlas.allocate(size2(27, 27)).unwrap();
        atlas.allocate(size2(27, 27)).unwrap();
        atlas.allocate(size2(27, 27)).unwrap();
        atlas.allocate(size2(27, 27)).unwrap();
        atlas.allocate(size2(11, 12)).unwrap();
        atlas.allocate(size2(29, 28)).unwrap();
        atlas.allocate(size2(32, 32)).unwrap();
    }
}


#[test]
fn fuzz_01() {
    let mut atlas = BucketedAtlasAllocator::new(size2(1000, 1000));

    assert!(atlas.allocate(size2(65280, 1)).is_none());
    assert!(atlas.allocate(size2(1, 65280)).is_none());
}

#[test]
fn fuzz_02() {
    let mut atlas = BucketedAtlasAllocator::new(size2(1000, 1000));

    assert!(atlas.allocate(size2(255, 65599)).is_none());
}

#[test]
fn fuzz_03() {
    let mut atlas = BucketedAtlasAllocator::new(size2(1000, 1000));

    let sizes = &[
        size2(999, 128),
        size2(168492810, 10),
        size2(45, 96),
        size2(-16711926, 0),
    ];

    let mut allocations = Vec::new();
    let mut allocated_space = 0;

    for size in sizes {
        if let Some(alloc) = atlas.allocate(*size) {
            allocations.push(alloc);
            allocated_space += alloc.rectangle.area();
            assert_eq!(allocated_space, atlas.allocated_space());
        }
    }

    for alloc in &allocations {
        atlas.deallocate(alloc.id);

        allocated_space -= alloc.rectangle.area();
        assert_eq!(allocated_space, atlas.allocated_space());
    }

    assert_eq!(atlas.allocated_space(), 0);
}

#[test]
fn fuzz_04() {
    let mut atlas = BucketedAtlasAllocator::new(size2(1000, 1000));

    assert!(atlas.allocate(size2(2560, 2147483647)).is_none());
}

#[test]
fn fuzz_05() {
    let mut atlas = BucketedAtlasAllocator::new(size2(2048, 2048));

    assert!(atlas.allocate(size2(0, -1978597547)).is_none());
}