Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Transparent Pixel Support and Minor Fixes. #22

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

Stattek
Copy link

@Stattek Stattek commented Feb 9, 2025

Adds ability to render PNGs with transparent pixels (pixels that are at least 95% transparent will be rendered with 0 index in charset).

  • Update image to version 0.25.5
  • Run cargo fmt on src files.
  • Add new option to allow ANSI codes to be generated per character rather than when the color has changed, to make it easier to parse by other programs.
  • Add new charset that gives similar output to jp2a program.
  • Change default image output to 128 from 80, to be accurate with documentation.
  • Update readme.

Summary by Sourcery

New Features:

  • Support transparent pixels in PNG images, rendering pixels with at least 95% transparency with the minimum index in the charset.

Copy link

sourcery-ai bot commented Feb 9, 2025

Reviewer's Guide by Sourcery

This pull request introduces transparent pixel support and a new option for escaping ANSI color codes on each character. The changes include updates to the rendering logic, command-line options, charsets, and dependency versions, along with code formatting improvements.

Updated Class Diagram for ImageRenderer and RenderOptions

classDiagram
    class ImageRenderer {
      - resource: &DynamicImage
      - options: RenderOptions
      + get_char_for_pixel(pixel: Rgba<u8>, maximum: f64): &str
      + get_opacity_percent(pixel: Rgba<u8>): f64
      + get_grayscale(pixel: Rgba<u8>): f64
    }

    class RenderOptions {
      - width: Option<u32>
      - height: Option<u32>
      - colored: bool
      - escape_each_colored_char: bool
      - invert: bool
      - charset: &[&str]
      + escape_each_colored_char(escape: bool): Self
      + invert(invert: bool): Self
      + ...
    }

    ImageRenderer --> RenderOptions : uses
Loading

File-Level Changes

Change Details Files
Implement transparent pixel rendering logic
  • Added a new method to compute pixel opacity
  • Modified character selection to handle pixels with less than 95% opacity as transparent
  • Updated inline documentation for transparency handling
src/image_renderer.rs
Add option for per-character ANSI escape codes
  • Introduced a new command-line flag for escaping each colored character individually
  • Added a boolean field and a builder method in RenderOptions
  • Adjusted rendering logic to check for the new flag when writing ANSI codes
src/main.rs
src/renderer.rs
src/image_renderer.rs
Update default image output width
  • Changed the default width from 80 to 128 to align with documentation
src/main.rs
Update dependency and formatting
  • Bumped the image crate version from 0.24.6 to 0.25.5
  • Ran cargo fmt on source files to standardize formatting
  • Updated Cargo.lock accordingly
  • Added a new (empty) gif_renderer module
Cargo.toml
Cargo.lock
src/gif_renderer.rs
Enhance charset support
  • Added a new charset definition ('minimal') to provide a different rendering style
  • Updated charset mappings in the charset module
src/charsets.rs
Update documentation
  • Documented the new escape-each-char option in README.md
  • Clarified the default behavior for colored output
README.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Stattek - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Extract the 0.95 opacity threshold into a named constant to make its purpose clearer.
  • Abstract the repeated ANSI color prefix logic into a helper function to reduce duplication between writer and buffer handling.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

src/image_renderer.rs Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
Stattek and others added 2 commits February 8, 2025 22:15
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@@ -24,6 +29,10 @@ impl ImageRenderer<'_> {
}]
}

fn get_transparency_percent(&self, pixel: &Rgba<u8>) -> f64 {
pixel[3] as f64 / u8::MAX as f64
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using u8::MAX is smarter than plain 255.

Just a note to myself

@orhnk
Copy link
Owner

orhnk commented Feb 12, 2025

Thank you for your PR. I'm waiting for the question

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants