Anti-aliased line/circle rendering, grayscale PNG output, and Spring Boot-style actuator endpoints - #17
Anti-aliased line/circle rendering, grayscale PNG output, and Spring Boot-style actuator endpoints#17Grizaster wants to merge 13 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates Labelize’s rendering pipeline to preserve anti-aliasing (by improving circle/diagonal-line rasterization and switching PNG output to grayscale), and extends the Axum HTTP server with Spring Boot-style /manage/* actuator endpoints that expose health/info/env/metrics.
Changes:
- Added
/manage/health,/manage/info,/manage/env,/manage/metricsendpoints and basic request/render timing counters in theserveHTTP server. - Refactored the renderer to expose
draw_label_to_rgba, and implemented anti-aliased circle + diagonal-line drawing. - Changed PNG encoding from thresholded black/white to 8-bit grayscale; added
serde_jsonbehind theservefeature.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
src/main.rs |
Adds /manage/* endpoints, shared app state, and render/request metrics; switches HTTP PNG path to encode from an RGBA canvas. |
src/images/monochrome.rs |
Changes PNG encoding to grayscale (L8) by removing the binarization threshold. |
src/drawers/renderer.rs |
Introduces draw_label_to_rgba and implements anti-aliased circle and diagonal-line rendering. |
Cargo.toml |
Adds optional serde_json and wires it into the serve feature. |
Comments suppressed due to low confidence (1)
src/images/monochrome.rs:20
encode_pngis publicly re-exported (src/lib.rs:20) and lives underimages::monochrome, but it now encodes 8-bit grayscale (ExtendedColorType::L8) instead of a binary/monochrome image. That semantic change can break API expectations for downstream consumers who relied on 1-bit output.
Consider either (a) keeping the old thresholded encoder as encode_png_monochrome and introducing encode_png_grayscale, or (b) adding an explicit option/parameter to select the output mode.
pub fn encode_png(img: &RgbaImage, w: &mut impl Write) -> Result<(), LabelizeError> {
let (width, height) = img.dimensions();
let mut gray = image::GrayImage::new(width, height);
for y in 0..height {
for x in 0..width {
let pixel = img.get_pixel(x, y);
gray.put_pixel(x, y, image::Luma([pixel[0]]));
}
}
let encoder = image::codecs::png::PngEncoder::new(w);
use image::ImageEncoder;
encoder
.write_image(gray.as_raw(), width, height, image::ExtendedColorType::L8)
.map_err(|e| LabelizeError::Encode(format!("PNG encode error: {}", e)))
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@Grizaster Thanks for your contributions. Pls fix the ci. I will take a look this feature. |
Preview mode was rendering at 72dpmm without scaling ZPL coordinates, causing content to appear in the top-left corner at ~10% of canvas size. Now uses 4x supersampling: scales all element coordinates/sizes by 4x, renders at 4x dpmm, then downsamples with CatmullRom filter for smooth anti-aliased edges. Output is grayscale (no binary thresholding) to preserve anti-aliasing.
Adds /manage/health, /manage/info, /manage/env, /manage/metrics. Tracks render counts, preview/pdf counts, avg render time, uptime. Existing /health endpoint kept for backwards compatibility.
- Replace Bresenham line drawing with anti-aliased variants (draw_antialiased_line_segment_mut, draw_antialiased_polygon_mut) - Remove binary threshold in encode_png: output 8-bit grayscale instead of 1-bit monochrome, preserving anti-aliasing edges - Matches Labelary's default Grayscale output quality
- Thick diagonal lines (^GD t>1): manual parallelogram fill with AA edges instead of draw_antialiased_polygon_mut (which only draws outlines) - Circle (^GC): per-pixel distance-based coverage for both inner and outer edges, producing smooth anti-aliased ring edges - Remove preview mode (scale_label, supersampling) — no longer needed since normal rendering is now anti-aliased - Remove preview_renders metric - Clean up unused imports
- Update unit_png_encoder tests: assert grayscale passthrough instead of binary threshold behavior - Fix clippy manual_clamp warnings: use clamp() instead of max().min() - Increase dhlparceluk golden test tolerance from 5.5% to 6.0% to account for anti-aliasing pixel diff - cargo fmt
- Fix avg_ms metric: remove erroneous 1000x multiplication (was already in ms) - Increment failed_renders on all error paths (render, PNG encode, PDF encode) - Filter secrets from /manage/env: exclude vars containing TOKEN, KEY, PASSWORD, SECRET, CREDENTIAL - /manage/health: report real disk space via statvfs instead of hard-coded zeros - Regenerate all testdata/diffs after rendering changes (per AGENTS.md) - Add libc dependency for statvfs on Unix
|
@Grizaster This PR contains multiple features. Could you please split it into several smaller PRs? |
|
Closed in favor of two smaller PRs as requested:
|
Overview
This PR improves rendering quality to match commercial alternatives (e.g. Labelary) by adding anti-aliasing for all line and circle primitives, and adds Spring Boot-style management endpoints to the HTTP server.
Rendering Improvements
Anti-aliased diagonal lines (
^GD)draw_line_segment_mut) with Xiaolin Wu's algorithm (draw_antialiased_line_segment_mut) for smooth edgesdraw_polygon_mutapproach only drew the outline, not the filled shapeAnti-aliased circles (
^GC)draw_filled_circle_mutand the binary distance-check ring with a per-pixel coverage-based approachGrayscale PNG output
pixel > 128 ? 255 : 0) fromencode_pngX-Quality: GrayscaleoutputManagement Endpoints (Actuator)
Added Spring Boot-style actuator endpoints under
/manage/:GET /manage/healthGET /manage/infoGET /manage/envLABELIZE_*,RUST_*,TZ,LANG,LC_ALL,PATH,HOME,HOSTNAME,USER,SHELL,TERM)GET /manage/metricsMetrics are tracked via
AtomicU64counters andInstanttimer. The existing/healthendpoint is kept for backwards compatibility.Dependencies
serde_json(optional, behindservefeature) for structured JSON responsesFiles Changed
src/drawers/renderer.rs— anti-aliased line and circle renderingsrc/images/monochrome.rs— grayscale PNG encoding (removed binary threshold)src/main.rs— actuator endpoints, metrics trackingCargo.toml—serde_jsondependency