Files
hcie-rust-v3.05/hcie-io/examples/create_base_test.rs
T
2026-07-09 02:59:53 +03:00

214 lines
7.2 KiB
Rust

use hcie_io::{Layer, BlendMode};
use std::path::Path;
fn main() {
let w = 800;
let h = 800;
let px_count = (w * h) as usize;
println!("Generating base PSD test layers...");
// 1. Background: Striped Pattern
let mut bg_pixels = vec![0u8; px_count * 4];
for y in 0..h {
for x in 0..w {
let idx = (y * w + x) as usize * 4;
// Diagonal striped pattern
if (x + y) % 64 < 32 {
bg_pixels[idx] = 235;
bg_pixels[idx + 1] = 235;
bg_pixels[idx + 2] = 238;
bg_pixels[idx + 3] = 255;
} else {
bg_pixels[idx] = 215;
bg_pixels[idx + 1] = 215;
bg_pixels[idx + 2] = 218;
bg_pixels[idx + 3] = 255;
}
}
}
let mut bg_layer = Layer::from_rgba("Background Grid", w, h, bg_pixels);
bg_layer.blend_mode = BlendMode::Normal;
// 2. Teal Circle Layer (Solid edges)
let mut circle_pixels = vec![0u8; px_count * 4];
let cx1 = 300.0f32;
let cy1 = 300.0f32;
let r1 = 150.0f32;
for y in 0..h {
for x in 0..w {
let idx = (y * w + x) as usize * 4;
let dx = x as f32 - cx1;
let dy = y as f32 - cy1;
let dist = (dx * dx + dy * dy).sqrt();
if dist < r1 {
// Antialiasing edge
let alpha = if r1 - dist < 1.0 {
((r1 - dist) * 255.0) as u8
} else {
255
};
circle_pixels[idx] = 0;
circle_pixels[idx + 1] = 180;
circle_pixels[idx + 2] = 180;
circle_pixels[idx + 3] = alpha;
}
}
}
let mut circle_layer = Layer::from_rgba("Teal Circle", w, h, circle_pixels);
circle_layer.blend_mode = BlendMode::Normal;
// 3. Pink Rectangle Layer (Solid edges)
let mut rect_pixels = vec![0u8; px_count * 4];
let rx1 = 450;
let ry1 = 450;
let rx2 = 700;
let ry2 = 700;
for y in 0..h {
for x in 0..w {
let idx = (y * w + x) as usize * 4;
if x >= rx1 && x <= rx2 && y >= ry1 && y <= ry2 {
rect_pixels[idx] = 230;
rect_pixels[idx + 1] = 0;
rect_pixels[idx + 2] = 120;
rect_pixels[idx + 3] = 255;
}
}
}
let mut rect_layer = Layer::from_rgba("Pink Rectangle", w, h, rect_pixels);
rect_layer.blend_mode = BlendMode::Normal;
// 4. Yellow Ring Layer (Testing inner vs outer bounds)
let mut ring_pixels = vec![0u8; px_count * 4];
let cx2 = 550.0f32;
let cy2 = 250.0f32;
let rout = 120.0f32;
let rinner = 60.0f32;
for y in 0..h {
for x in 0..w {
let idx = (y * w + x) as usize * 4;
let dx = x as f32 - cx2;
let dy = y as f32 - cy2;
let dist = (dx * dx + dy * dy).sqrt();
if dist >= rinner && dist <= rout {
let mut alpha = 255u8;
if dist - rinner < 1.0 {
alpha = alpha.min(((dist - rinner) * 255.0) as u8);
}
if rout - dist < 1.0 {
alpha = alpha.min(((rout - dist) * 255.0) as u8);
}
ring_pixels[idx] = 230;
ring_pixels[idx + 1] = 200;
ring_pixels[idx + 2] = 0;
ring_pixels[idx + 3] = alpha;
}
}
}
let mut ring_layer = Layer::from_rgba("Yellow Ring", w, h, ring_pixels);
ring_layer.blend_mode = BlendMode::Normal;
// 5. White Star/Plus Shape Layer
let mut plus_pixels = vec![0u8; px_count * 4];
let cx3 = 250;
let cy3 = 550;
let thickness = 20;
let length = 90;
for y in 0..h {
for x in 0..w {
let idx = (y * w + x) as usize * 4;
let dx = (x as i32 - cx3).abs();
let dy = (y as i32 - cy3).abs();
let in_horiz = dx <= length && dy <= thickness;
let in_vert = dy <= length && dx <= thickness;
if in_horiz || in_vert {
plus_pixels[idx] = 255;
plus_pixels[idx + 1] = 255;
plus_pixels[idx + 2] = 255;
plus_pixels[idx + 3] = 255;
}
}
}
let mut plus_layer = Layer::from_rgba("White Plus Shape", w, h, plus_pixels);
plus_layer.blend_mode = BlendMode::Normal;
// 6. Soft Orange Glow Layer (Fuzzy/smooth edges)
let mut soft_pixels = vec![0u8; px_count * 4];
let cx4 = 200.0f32;
let cy4 = 200.0f32;
let soft_radius = 100.0f32;
for y in 0..h {
for x in 0..w {
let idx = (y * w + x) as usize * 4;
let dx = x as f32 - cx4;
let dy = y as f32 - cy4;
let dist = (dx * dx + dy * dy).sqrt();
if dist < soft_radius {
// Smooth cubic falloff for extremely soft edges
let t = dist / soft_radius;
let factor = 1.0 - t * t * (3.0 - 2.0 * t);
let alpha = (factor * 255.0).round().clamp(0.0, 255.0) as u8;
if alpha > 0 {
soft_pixels[idx] = 255;
soft_pixels[idx + 1] = 100;
soft_pixels[idx + 2] = 0;
soft_pixels[idx + 3] = alpha;
}
}
}
}
let mut soft_layer = Layer::from_rgba("Soft Orange Shape", w, h, soft_pixels);
soft_layer.blend_mode = BlendMode::Normal;
let layers = vec![
bg_layer,
circle_layer,
rect_layer,
ring_layer,
plus_layer,
soft_layer,
];
// Compute composite for merged image
let comp_layers: Vec<hcie_composite::Layer> = layers.iter().map(|l| hcie_composite::Layer {
name: l.name.clone(),
layer_type: l.layer_type.clone(),
data: l.data.clone(),
pixels: l.pixels.clone(),
width: l.width,
height: l.height,
visible: l.visible,
opacity: l.opacity,
blend_mode: l.blend_mode,
locked: l.locked,
dirty: l.dirty,
id: l.id,
parent_id: l.parent_id,
styles: l.styles.clone(),
clipping_mask: l.clipping_mask,
collapsed: l.collapsed,
adjustment: l.adjustment.clone(),
mask_pixels: l.mask_pixels.clone(),
mask_bounds: l.mask_bounds,
mask_default_color: l.mask_default_color,
curve_points: l.curve_points.clone(),
fill_opacity: l.fill_opacity,
effects: l.effects.clone(),
effects_dirty: std::sync::atomic::AtomicBool::new(false),
effects_cache: std::sync::Mutex::new(None),
adjustment_raw: None,
is_section_divider: false,
image_resources_raw: None,
}).collect();
println!("Compositing layers...");
let composited = hcie_composite::composite_layers(&comp_layers, w, h);
let output_psd = "/home/hc/Pictures/_psd_stil_test/base_test_generated.psd";
println!("Saving to {}...", output_psd);
match hcie_psd::save_psd(&layers, w, h, &composited, Path::new(output_psd)) {
Ok(()) => println!("Successfully created and saved base PSD test file."),
Err(e) => eprintln!("Failed to save base PSD test file: {}", e),
}
}