1use crate::config::PackerConfig;
2use crate::error::{Result, TexPackerError};
3use crate::model::Frame;
4use crate::runtime::{AtlasSession, RuntimeStats, RuntimeStrategy};
5use image::{Rgba, RgbaImage};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub struct UpdateRegion {
10 pub page_id: usize,
12 pub x: u32,
14 pub y: u32,
16 pub width: u32,
18 pub height: u32,
20}
21
22impl UpdateRegion {
23 pub fn empty() -> Self {
25 Self {
26 page_id: 0,
27 x: 0,
28 y: 0,
29 width: 0,
30 height: 0,
31 }
32 }
33
34 pub fn is_empty(&self) -> bool {
36 self.width == 0 || self.height == 0
37 }
38
39 pub fn area(&self) -> u64 {
41 (self.width as u64) * (self.height as u64)
42 }
43}
44
45pub struct RuntimeAtlas {
50 session: AtlasSession,
51 pages: Vec<RgbaImage>,
52 background_color: Rgba<u8>,
53}
54
55impl RuntimeAtlas {
56 pub fn new(cfg: PackerConfig, strategy: RuntimeStrategy) -> Self {
58 Self {
59 session: AtlasSession::new(cfg, strategy),
60 pages: Vec::new(),
61 background_color: Rgba([0, 0, 0, 0]), }
63 }
64
65 pub fn with_background_color(mut self, color: Rgba<u8>) -> Self {
67 self.background_color = color;
68 self
69 }
70
71 pub fn append_with_image(
74 &mut self,
75 key: String,
76 image: &RgbaImage,
77 ) -> Result<(usize, Frame<String>, UpdateRegion)> {
78 let (w, h) = image.dimensions();
79 let (page_id, frame) = self.session.append(key, w, h)?;
80
81 self.ensure_page(page_id);
83
84 let update_region = self.blit_to_page(page_id, &frame, image)?;
86
87 Ok((page_id, frame, update_region))
88 }
89
90 pub fn append(&mut self, key: String, w: u32, h: u32) -> Result<(usize, Frame<String>)> {
93 self.session.append(key, w, h)
94 }
95
96 pub fn evict_with_clear(
99 &mut self,
100 page_id: usize,
101 key: &str,
102 clear: bool,
103 ) -> Option<UpdateRegion> {
104 let slot_region = if clear {
106 self.session
107 .get_reserved_slot(key)
108 .map(|(pid, slot)| UpdateRegion {
109 page_id: pid,
110 x: slot.x,
111 y: slot.y,
112 width: slot.w,
113 height: slot.h,
114 })
115 } else {
116 None
117 };
118
119 if self.session.evict(page_id, key) {
121 if clear {
123 if let Some(region) = slot_region {
124 self.clear_region(region);
125 return Some(region);
126 }
127 }
128 Some(UpdateRegion::empty())
129 } else {
130 None
131 }
132 }
133
134 pub fn evict_by_key_with_clear(&mut self, key: &str, clear: bool) -> Option<UpdateRegion> {
136 let slot_region = if clear {
138 self.session
139 .get_reserved_slot(key)
140 .map(|(page_id, slot)| UpdateRegion {
141 page_id,
142 x: slot.x,
143 y: slot.y,
144 width: slot.w,
145 height: slot.h,
146 })
147 } else {
148 None
149 };
150
151 if self.session.evict_by_key(key) {
152 if clear {
153 if let Some(region) = slot_region {
154 self.clear_region(region);
155 return Some(region);
156 }
157 }
158 Some(UpdateRegion::empty())
159 } else {
160 None
161 }
162 }
163
164 pub fn get_page_image(&self, page_id: usize) -> Option<&RgbaImage> {
166 self.pages.get(page_id)
167 }
168
169 pub fn get_page_image_mut(&mut self, page_id: usize) -> Option<&mut RgbaImage> {
171 self.pages.get_mut(page_id)
172 }
173
174 pub fn num_pages(&self) -> usize {
176 self.pages.len()
177 }
178
179 pub fn get_frame(&self, key: &str) -> Option<(usize, &Frame<String>)> {
181 self.session.get_frame(key)
182 }
183
184 pub fn contains(&self, key: &str) -> bool {
185 self.session.contains(key)
186 }
187
188 pub fn keys(&self) -> Vec<&str> {
189 self.session.keys()
190 }
191
192 pub fn texture_count(&self) -> usize {
193 self.session.texture_count()
194 }
195
196 pub fn stats(&self) -> RuntimeStats {
197 self.session.stats()
198 }
199
200 pub fn snapshot_atlas(&self) -> crate::model::Atlas<String> {
201 self.session.snapshot_atlas()
202 }
203
204 fn ensure_page(&mut self, page_id: usize) {
206 while self.pages.len() <= page_id {
207 let page_img = RgbaImage::from_pixel(
208 self.session.cfg.max_width,
209 self.session.cfg.max_height,
210 self.background_color,
211 );
212 self.pages.push(page_img);
213 }
214 }
215
216 fn blit_to_page(
218 &mut self,
219 page_id: usize,
220 frame: &Frame<String>,
221 image: &RgbaImage,
222 ) -> Result<UpdateRegion> {
223 let page = self
224 .pages
225 .get_mut(page_id)
226 .ok_or_else(|| TexPackerError::InvalidConfig("Page not found".into()))?;
227
228 let (src_w, src_h) = image.dimensions();
229 let dst_x = frame.frame.x;
230 let dst_y = frame.frame.y;
231
232 let extrude = self.session.cfg.texture_extrusion;
234 let outlines = self.session.cfg.texture_outlines;
235 crate::compositing::blit_rgba(
236 image,
237 page,
238 dst_x,
239 dst_y,
240 0,
241 0,
242 src_w,
243 src_h,
244 frame.rotated,
245 extrude,
246 outlines,
247 );
248
249 let start_x = dst_x.saturating_sub(extrude);
251 let start_y = dst_y.saturating_sub(extrude);
252 let mut width = frame.frame.w + extrude.saturating_mul(2);
253 let mut height = frame.frame.h + extrude.saturating_mul(2);
254 if start_x + width > page.width() {
256 width = page.width() - start_x;
257 }
258 if start_y + height > page.height() {
259 height = page.height() - start_y;
260 }
261
262 Ok(UpdateRegion {
263 page_id,
264 x: start_x,
265 y: start_y,
266 width,
267 height,
268 })
269 }
270
271 fn clear_region(&mut self, region: UpdateRegion) {
273 if let Some(page) = self.pages.get_mut(region.page_id) {
274 for y in region.y..(region.y + region.height).min(page.height()) {
275 for x in region.x..(region.x + region.width).min(page.width()) {
276 page.put_pixel(x, y, self.background_color);
277 }
278 }
279 }
280 }
281}