Skip to main content

tex_packer_core/
offline.rs

1use image::{DynamicImage, RgbaImage};
2
3use crate::config::OfflineConfig;
4use crate::error::Result;
5use crate::model::{Atlas, PackStats, PageId, Rect};
6
7/// A decoded image and its logical atlas key.
8#[derive(Debug)]
9pub struct InputImage {
10    pub key: String,
11    pub image: DynamicImage,
12}
13
14/// A size-only input with optional source reconstruction metadata.
15#[derive(Debug, Clone)]
16pub struct LayoutItem {
17    pub key: String,
18    pub w: u32,
19    pub h: u32,
20    pub source: Option<Rect>,
21    pub source_size: Option<(u32, u32)>,
22    pub trimmed: bool,
23}
24
25/// Rendered pixels for one page in the accompanying atlas.
26pub struct RenderedPage {
27    pub(crate) page_id: PageId,
28    pub(crate) rgba: RgbaImage,
29}
30
31impl RenderedPage {
32    pub fn page_id(&self) -> PageId {
33        self.page_id
34    }
35
36    pub fn rgba(&self) -> &RgbaImage {
37        &self.rgba
38    }
39
40    pub fn into_rgba(self) -> RgbaImage {
41        self.rgba
42    }
43}
44
45/// A validated atlas and its rendered page payloads.
46pub struct PackOutput {
47    pub(crate) atlas: Atlas,
48    pub(crate) pages: Vec<RenderedPage>,
49}
50
51impl PackOutput {
52    pub fn atlas(&self) -> &Atlas {
53        &self.atlas
54    }
55
56    pub fn pages(&self) -> &[RenderedPage] {
57        &self.pages
58    }
59
60    pub fn into_parts(self) -> (Atlas, Vec<RenderedPage>) {
61        (self.atlas, self.pages)
62    }
63
64    pub fn stats(&self) -> PackStats {
65        self.atlas.stats()
66    }
67}
68
69/// Stateful entry point for all offline packing workflows.
70pub struct OfflinePacker {
71    config: OfflineConfig,
72}
73
74impl OfflinePacker {
75    pub fn new(config: OfflineConfig) -> Self {
76        Self { config }
77    }
78
79    pub fn config(&self) -> &OfflineConfig {
80        &self.config
81    }
82
83    /// Packs decoded images and renders the resulting atlas pages.
84    pub fn pack_images(&self, inputs: Vec<InputImage>) -> Result<PackOutput> {
85        crate::pipeline::pack_images_impl(inputs, &self.config)
86    }
87
88    /// Packs decoded images without rendering page pixels.
89    pub fn layout_images(&self, inputs: Vec<InputImage>) -> Result<Atlas> {
90        crate::pipeline::layout_images_impl(inputs, &self.config)
91    }
92
93    /// Packs caller-supplied layout records without pixel preprocessing or deduplication.
94    pub fn pack_layout(&self, items: Vec<LayoutItem>) -> Result<Atlas> {
95        crate::pipeline::pack_layout_impl(items, &self.config)
96    }
97}