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
#![warn(missing_docs)]

//! Putting textures together, hopefully without wasting too much space.

extern crate framing;

use framing::{AsBytes, Image, Chunky};
use std::{mem, ptr};

/// Stores images, and automatically stitches them together.
///
/// While it's definitely okay to add the images in any order, to get any decent
/// space efficiency it's necessary to at least sort-of sort the frames in
/// terms of decreasing size. Particularly good orders are by `width * height`
/// and by `max(width, height)`, both in descending order.
pub struct Atlas<T> {
    bytes: Vec<u8>,
    scratch: Vec<u8>,
    width: usize,
    height: usize,
    blank: T,
    rects: Vec<Rect>
}

impl<T> Atlas<T> {
    /// Create a new, empty atlas.
    ///
    /// The blank pixel will be used to represent the space that exists between
    /// images, in the almost certain case that 100% space utilization is not
    /// achieved.
    pub fn new(blank: T) -> Self {
        Atlas {
            bytes: Vec::new(),
            scratch: Vec::new(),
            width: 0,
            height: 0,
            blank: blank,
            rects: Vec::new()
        }
    }

    /// Adds an image to the atlas, placing it appropriately.
    ///
    /// The return value is the location of the image within the atlas. If the
    /// image is of zero size, then the coordinates `(0, 0)` will be returned,
    /// which you most likely won't need to special-case, since it is
    /// *technically* valid.
    pub fn add<U>(&mut self, image: U) -> (usize, usize)
    where
        T: AsBytes + Clone + Sync + 'static,
        U: Image<Pixel = T> + Sync
    {
        let (w, h) = (image.width(), image.height());

        if w == 0 || h == 0 {
            return (0, 0);
        }

        if self.width == 0 || self.height == 0 {
            self.bytes.reserve(T::width() * w * h);
            self.width = w;
            self.height = h;

            for (_, _, pixel) in framing::iter(&image) {
                self.bytes.extend_from_slice(T::Bytes::from(pixel).as_ref())
            }

            return (0, 0);
        }

        let result = self.rects.iter()
            .enumerate()
            .filter(|&(_, rect)| w <= rect.w && h <= rect.h)
            .min_by_key(|&(_, rect)| {
                let (dw, dh) = (rect.w - w, rect.h - h);
                if dh < dw { dh } else { dw }
            })
            .map(|(i, rect)| (i, rect.clone()));

        if let Some((i, rect)) = result {
            self.rects.remove(i);

            if rect.w != w {
                self.rects.push(Rect {
                    x: rect.x + w,
                    y: rect.y,
                    w: rect.w - w,
                    h: h
                });
            }

            if rect.h != h {
                self.rects.push(Rect {
                    x: rect.x,
                    y: rect.y + h,
                    w: w,
                    h: rect.h - h
                });
            }

            if rect.w != w && rect.h != h {
                self.rects.push(Rect {
                    x: rect.x + w,
                    y: rect.y + h,
                    w: rect.w - w,
                    h: rect.h - h
                });
            }

            // The image fits!
            for y in 0..h {
            for x in 0..w {
                let i = T::width() * (self.width * (rect.y + y) + (rect.x + x));
                let p = T::Bytes::from(unsafe {
                    image.pixel(x, y)
                });

                unsafe {
                    ptr::copy_nonoverlapping(
                        p.as_ref().as_ptr(),
                        self.bytes.as_mut_ptr().offset(i as isize),
                        T::width()
                    )
                }
            }}

            (rect.x, rect.y)
        } else {
            // The image doesn't fit.

            if self.height <= self.width {
                // Our atlas is wider than it is tall, so the image is put at
                // the bottom of the atlas, to make it more square.

                if self.width > w {
                    self.rects.push(Rect {
                        x: w,
                        y: self.height,
                        w: self.width - w,
                        h: h
                    });
                } else if self.width < w {
                    self.rects.push(Rect {
                        x: self.width,
                        y: 0,
                        w: w - self.width,
                        h: self.height
                    });
                }

                if w <= self.width {
                    // The image is already wide enough.

                    self.bytes.reserve(T::width() * self.width * h);

                    for y in self.height..(self.height + h) {
                        for x in 0..w {
                            let pixel = T::Bytes::from(unsafe {
                                image.pixel(x, y)
                            });
                            self.bytes.extend_from_slice(pixel.as_ref());
                        }
                        for _ in w..self.width {
                            let pixel = T::Bytes::from(self.blank.clone());
                            self.bytes.extend_from_slice(pixel.as_ref());
                        }
                    }

                    self.height = self.height + h;
                } else {
                    // We need to make the image wider.

                    let cap = T::width() * (self.height + h) * w;
                    self.scratch.clear();
                    self.scratch.reserve(cap);

                    for chunk in self.bytes.chunks(T::width() * self.width) {
                        self.scratch.extend_from_slice(chunk);
                        for _ in self.width..w {
                            let pixel = T::Bytes::from(self.blank.clone());
                            self.scratch.extend_from_slice(pixel.as_ref());
                        }
                    }

                    for y in 0..h {
                        for x in 0..w {
                            let pixel = T::Bytes::from(unsafe {
                                image.pixel(x, y)
                            });
                            self.scratch.extend_from_slice(pixel.as_ref());
                        }
                    }

                    mem::swap(&mut self.bytes, &mut self.scratch);
                    self.width = w;
                    self.height = self.height + h;
                }

                (0, self.height)
            } else {
                // Our atlas is taller than it is wide, so the image is put to
                // the right of the atlas, to make it more square.

                if self.height > h {
                    self.rects.push(Rect {
                        x: self.width,
                        y: h,
                        w: w,
                        h: self.height - h
                    });
                } else if self.height < h {
                    self.rects.push(Rect {
                        x: 0,
                        y: self.height,
                        w: self.width,
                        h: h - self.height
                    });
                }

                let new_height = if self.height <= h { h } else { self.height };
                let new_width = self.width + w;

                let cap = T::width() * new_width * new_height;
                self.scratch.clear();
                self.scratch.reserve(cap);

                for (y, chunk) in
                    self.bytes
                        .chunks(T::width() * self.width)
                        .enumerate()
                {
                    self.scratch.extend_from_slice(chunk);
                    if y < h {
                        for x in 0..w {
                            let pixel = T::Bytes::from(unsafe {
                                image.pixel(x, y)
                            });
                            self.scratch.extend_from_slice(pixel.as_ref());
                        }
                    } else {
                        for _ in 0..w {
                            let pixel = T::Bytes::from(self.blank.clone());
                            self.scratch.extend_from_slice(pixel.as_ref());
                        }
                    }
                }

                for y in self.height..h {
                    for _ in 0..self.width {
                        let pixel = T::Bytes::from(self.blank.clone());
                        self.scratch.extend_from_slice(pixel.as_ref());
                    }
                    for x in 0..w {
                        let pixel = T::Bytes::from(unsafe {
                            image.pixel(x, y)
                        });
                        self.scratch.extend_from_slice(pixel.as_ref());
                    }
                }

                mem::swap(&mut self.bytes, &mut self.scratch);
                self.width = new_width;
                self.height = new_height;

                (self.width, 0)
            }
        }
    }
}

impl<T> Into<Chunky<T>> for Atlas<T> where T: AsBytes {
    fn into(self) -> Chunky<T> {
        Chunky::from_bytes(self.width, self.height, self.bytes)
    }
}

impl<T> Image for Atlas<T> where T: AsBytes {
    type Pixel = T;

    fn width(&self) -> usize {
        self.width
    }

    fn height(&self) -> usize {
        self.height
    }

    unsafe fn pixel(&self, x: usize, y: usize) -> Self::Pixel {
        let off = T::width() * (y * self.width + x);
        let mut bytes = T::Bytes::default();

        ptr::copy_nonoverlapping(
            self.bytes.as_ptr().offset(off as isize),
            bytes.as_mut().as_mut_ptr(),
            T::width()
        );

        bytes.into()
    }
}

#[derive(Clone)]
struct Rect {
    x: usize,
    y: usize,
    w: usize,
    h: usize
}