diff --git a/crates/formats/src/export/mod.rs b/crates/formats/src/export/mod.rs index b8246fb3..748f3ff0 100644 --- a/crates/formats/src/export/mod.rs +++ b/crates/formats/src/export/mod.rs @@ -614,6 +614,7 @@ pub fn list_outputs(project: &Project, ts: usize, mesh_name: &str, opts: &Export let grid = set.tile_grid(); let (block_resolution, block_size) = (set.block_resolution(), set.block_size()); let scale = |side| scale_export_side(side, block_resolution, opts.size); + check_scaled_size(set, (scale(block_size.0), scale(block_size.1)))?; for (i, map) in opts.preset.maps.iter().enumerate() { if !map.channels.iter().any(|(_, s)| source_present(set, s)) { continue; @@ -923,6 +924,9 @@ pub fn render_planned_maps(input: &ExportInput, options: &ExportOptions, planned return Ok(Vec::new()); } + // Before any image buffer is allocated + check_scaled_size(set, (scale_export_side(tile_res, tile_res, options.size), scale_export_side(tile_height, tile_res, options.size)))?; + // Padding stays inside each UV tile let padding_maps = |max_dist| PaddingMaps::new(set.uv_padding, input.coverage, (width, height), max_dist, (tile_res, tile_height), input.seams); let padding = match options.padding { @@ -1052,6 +1056,14 @@ fn tile_side(set: &TextureSet, tile: u32, block_resolution: u32) -> u32 { ((block_resolution as f32 * set.tile_scale(tile)).round() as u32).clamp(1, block_resolution.max(1)) } +/// Refuse an export size that scales a non-square set's UV tile past the largest image side +fn check_scaled_size(set: &TextureSet, (width, height): (u32, u32)) -> Result<()> { + if width.max(height) > MAX_IMAGE_SIZE { + return Err(FormatError::Invalid(format!("export size makes texture set \"{}\" {width}×{height}, over {MAX_IMAGE_SIZE} texels a side", set.name))); + } + Ok(()) +} + /// Scale an image side relative to the source block, keeping a pixel for any nonempty source. pub fn scale_export_side(source_side: u32, block_resolution: u32, export_size: Option) -> u32 { let export_size = export_size.unwrap_or(block_resolution); diff --git a/crates/formats/src/export/tests.rs b/crates/formats/src/export/tests.rs index 44bd39b7..5747fe97 100644 --- a/crates/formats/src/export/tests.rs +++ b/crates/formats/src/export/tests.rs @@ -1169,6 +1169,18 @@ fn psd_holds_the_merged_image_planar() { assert!(samples[2 * 64 * 64..].as_chunks::<2>().0.iter().all(|sample| *sample == [128, 0])); } +#[test] +fn an_export_size_that_scales_a_tall_set_past_the_largest_side_is_refused() { + let mut fx = Fx::new(&default_channels()); + fx.project.texture_sets[0].set_size(RES, RES * 4); + let mut options = one_map(FileFormat::Png, 8, roughness()); + options.size = Some(MAX_IMAGE_SIZE / 2); + assert!(list_outputs(&fx.project, 0, "sphere", &options).is_err(), "the height would be twice the largest side"); + + options.size = Some(MAX_IMAGE_SIZE / 4); + assert_eq!(list_outputs(&fx.project, 0, "sphere", &options).unwrap().len(), 1, "a tall export within the largest side is kept"); +} + #[test] fn psd_plan_rejects_a_version_one_atlas_over_its_dimension_limit() { let mut fx = Fx::new(&default_channels());