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

Produce lossless WebP images #265

Merged
merged 14 commits into from
Aug 17, 2024
Merged

Produce lossless WebP images #265

merged 14 commits into from
Aug 17, 2024

Conversation

rob93c
Copy link
Member

@rob93c rob93c commented Aug 11, 2024

This pull request changes the image conversion flow to rely on Scrimage instead of Scalr + Pngtastic.
The image produced is now a lossless WebP.

Open points:

  • Scrimage methods can be chained together, for the time being they are separated to preserve the existing code structure
  • Image conversion now takes longer because of how Scrimage works
  • It doesn't seem possible to send WebP with a caption

Summary by CodeRabbit

  • New Features

    • Transitioned image processing from PNG to WebP format, enhancing efficiency and modernising the media handling capabilities.
    • Integrated the Scrimage library for improved image conversion functionality.
  • Documentation

    • Updated the README to reflect changes in image conversion libraries, replacing previous libraries with Scrimage.
  • Bug Fixes

    • Adjusted image validation logic in tests to accommodate changes in expected output formats and dimensions.
  • Chores

    • Added new logging configurations for improved monitoring of image processing operations.

Summary by CodeRabbit

  • New Features

    • Updated image processing capabilities to support WebP format.
    • Enhanced documentation reflecting changes in image processing libraries.
  • Bug Fixes

    • Adjusted image validation logic to reflect updated specifications and correct expected output dimensions.
  • Documentation

    • Revised the README to include the latest libraries used for image conversion.
  • Chores

    • Improved logging configuration for better monitoring of image processing actions.

@rob93c rob93c added documentation Improvements or additions to documentation enhancement New feature or request dependencies Pull requests that update a dependency file labels Aug 11, 2024
@rob93c rob93c self-assigned this Aug 11, 2024
Copy link

coderabbitai bot commented Aug 11, 2024

Walkthrough

Walkthrough

The recent updates to the project enhance image processing capabilities by transitioning from PNG to WebP format, streamlining library usage, and improving logging functionalities. These changes, reflected in both code and documentation, indicate a shift towards the Scrimage library for better performance and efficiency in image handling.

Changes

File Change Summary
README.md, build.gradle, gradle/libs.versions.toml Updated image processing libraries by removing imgscalr and Pngtastic, and adding Scrimage.
src/main/java/com/github/stickerifier/.../MediaHelper.java Transitioned methods to support WebP format, replacing PNG handling and updating image processing logic.
src/main/resources/logback.xml Modified logging configuration, adding a logger for com.sksamuel.scrimage at warn level.
src/test/java/com/github/stickerifier/.../MediaHelperTest.java Adjusted test assertions for expected image formats and dimensions to align with new WebP handling.
src/main/java/com/github/stickerifier/.../Stickerify.java, src/main/java/com/github/stickerifier/.../MediaConstraints.java Enhanced methods and renamed constants to improve clarity and functionality.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant MediaHelper
    participant Scrimage
    User->>MediaHelper: Request Image Processing
    MediaHelper->>Scrimage: Convert to WebP
    Scrimage-->>MediaHelper: Return WebP Image
    MediaHelper-->>User: Deliver WebP Image
Loading

Poem

🐰 In fields of code I hop and play,
New libraries brighten up the day.
From PNG to WebP we’ve leaped,
With Scrimage close, our progress steeped.
So let’s rejoice, with carrots and cheer,
For smoother images, we hold dear! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@rob93c
Copy link
Member Author

rob93c commented Aug 11, 2024

@coderabbitai review

Copy link

coderabbitai bot commented Aug 11, 2024

Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (1)
src/test/java/com/github/stickerifier/stickerify/media/MediaHelperTest.java (1)

52-52: Update assertion message to reflect .webp extension.

The assertion message in MediaHelperTest.java still mentions "image's extension must be png", which is inconsistent with the expected extension ".webp". Please update the message to reflect the correct extension.

  • File: src/test/java/com/github/stickerifier/stickerify/media/MediaHelperTest.java
  • Line: 52
Analysis chain

Update image extension to .webp is correct.

The change from .png to .webp reflects the transition to WebP images. Ensure all relevant parts of the codebase are updated to use .webp.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all references to image extensions are updated to `.webp`.

# Test: Search for `.png` references. Expect: No occurrences of `.png` for image files.
rg --type java 'image\'s extension must be png'

Length of output: 93


Script:

#!/bin/bash
# Description: Verify all references to image extensions are updated to `.webp`.

# Test: Search for `.png` references. Expect: No occurrences of `.png` for image files.
rg --type java "image's extension must be png"

Length of output: 221

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7958655 and 428965e.

Files ignored due to path filters (1)
  • src/test/resources/detailed.jpg is excluded by !**/*.jpg
Files selected for processing (6)
  • README.md (1 hunks)
  • build.gradle (1 hunks)
  • gradle/libs.versions.toml (2 hunks)
  • src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java (8 hunks)
  • src/main/resources/logback.xml (1 hunks)
  • src/test/java/com/github/stickerifier/stickerify/media/MediaHelperTest.java (2 hunks)
Files skipped from review due to trivial changes (1)
  • README.md
Additional comments not posted (11)
src/main/resources/logback.xml (1)

15-15: Logging configuration looks good.

The addition of a logger for com.sksamuel.scrimage at the warn level is appropriate for monitoring significant issues related to the new Scrimage library.

gradle/libs.versions.toml (1)

3-3: Dependency updates are appropriate.

The addition of scrimage-core and scrimage-webp dependencies aligns with the transition to the Scrimage library for image processing tasks. The versioning is correctly specified.

Also applies to: 22-23

build.gradle (1)

23-24: Dependency changes are appropriate.

The removal of libs.imgscalr and the addition of libs.scrimage.core and libs.scrimage.webp reflect the intended transition to the Scrimage library for improved image processing.

src/test/java/com/github/stickerifier/stickerify/media/MediaHelperTest.java (2)

116-116: Verify the change in expected PSD image height.

The expected height has been adjusted from 384 to 383. Ensure this change reflects the correct expected output.

Verification successful

Change in expected PSD image height is due to library switch.

The adjustment of the expected PSD image height from 384 to 383 is a result of switching the image processing to the scrimage library, which handles image dimensions differently. This change is intentional and aligns with the updated processing logic.

  • Commit: 25f1c354e4761688239fb85dd256c5009687c7a0 by Roberto Cella
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the change in expected height for PSD images.

# Test: Check for any documentation or code comments explaining the change.
rg --type java 'resizePsdImage'

Length of output: 149


Script:

#!/bin/bash
# Description: Search for changes in the implementation of `resizePsdImage` or related methods.

# Look for the implementation of `resizePsdImage` in the codebase.
ast-grep --lang java --pattern 'void resizePsdImage() { $$$ }'

# Search for any recent changes in the repository that might explain the height change.
git log -p -S 'assertImageConsistency(result, 512, 383)' -- src/test/java/com/github/stickerifier/stickerify/media/MediaHelperTest.java

Length of output: 1882


108-108: Verify the change in expected TIFF image height.

The expected height has been adjusted from 342 to 341. Ensure this change reflects the correct expected output.

src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java (6)

22-23: New imports for Scrimage are appropriate.

The addition of ImmutableImage and WebpWriter imports is necessary for handling WebP images with Scrimage.


182-191: Use of ImmutableImage is appropriate.

The change to return ImmutableImage aligns with the use of Scrimage. Ensure compatibility across the codebase.

Verification successful

Compatibility with ImmutableImage confirmed.

The usage of ImmutableImage is consistent across multiple methods in MediaHelper.java, indicating that the codebase is updated to handle ImmutableImage objects appropriately.

  • Methods using ImmutableImage include convertToWebp, isImageCompliant, resizeImage, and createWebpFile.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify compatibility with `ImmutableImage`.

# Test: Search for `ImmutableImage` usage to ensure all related code is updated.
rg --type java 'ImmutableImage'

Length of output: 1080


232-233: Verify image compliance logic.

Ensure that the compliance checks for WebP and PNG images accurately reflect the requirements.

Verification successful

Image compliance logic is correctly implemented.

The isImageCompliant method accurately checks for PNG and WebP formats and ensures size compliance using the isSizeCompliant method, which verifies dimensions against a MAX_SIZE boundary. The logic is consistent with expected requirements for these image formats.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the compliance logic for WebP and PNG images.

# Test: Search for `isImageCompliant` implementation details to ensure accuracy.
rg --type java 'isImageCompliant'

Length of output: 325


Script:

#!/bin/bash
# Description: Locate the implementation of the `isSizeCompliant` method.

# Search for the `isSizeCompliant` method to understand the size compliance logic.
rg --type java 'isSizeCompliant'

Length of output: 503


Script:

#!/bin/bash
# Description: Extract the implementation of the `isSizeCompliant` method.

# Use `ast-grep` to find and display the implementation of the `isSizeCompliant` method.
ast-grep --lang java --pattern 'private static boolean isSizeCompliant(int width, int height) { $$$ }'

Length of output: 497


268-285: Verify WebP file creation logic.

Ensure that the logic for creating WebP files is correct and meets the requirements.

Verification successful

WebP file creation logic is correct and meets requirements.

The createWebpFile method correctly implements the logic for creating a WebP file with maximum lossless compression and checks the file size against the required threshold. The use of createTempFile, WebpWriter.MAX_LOSSLESS_COMPRESSION, and isFileSizeLowerThan ensures that the process is handled appropriately.

  • createTempFile: Creates a temporary file with the specified type.
  • WebpWriter.MAX_LOSSLESS_COMPRESSION: Utilised for maximum compression.
  • isFileSizeLowerThan: Validates file size compliance.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the correctness of the WebP file creation logic.

# Test: Search for `createWebpFile` implementation details to ensure accuracy.
rg --type java 'createWebpFile'

Length of output: 308


Script:

#!/bin/bash
# Description: Gather details about the `createTempFile` method, `WebpWriter.MAX_LOSSLESS_COMPRESSION`, and `isFileSizeLowerThan`.

# Search for `createTempFile` method definition.
rg --type java 'createTempFile'

# Search for `WebpWriter.MAX_LOSSLESS_COMPRESSION` usage or definition.
rg --type java 'WebpWriter\.MAX_LOSSLESS_COMPRESSION'

# Search for `isFileSizeLowerThan` method definition.
rg --type java 'isFileSizeLowerThan'

Length of output: 1677


214-221: Verify WebP conversion logic.

Ensure that the logic for converting images to WebP format is correct and meets the requirements.

Verification successful

WebP conversion logic is correctly implemented.

The convertToWebp method, along with createWebpFile and resizeImage, correctly handles image conversion to WebP format. The process ensures compliance with size requirements and handles exceptions appropriately. No issues found with the logic.

  • convertToWebp: Checks compliance and decides on conversion.
  • createWebpFile: Converts and checks file size.
  • resizeImage: Resizes images to meet maximum size constraints.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the correctness of the WebP conversion logic.

# Test: Search for `convertToWebp` implementation details to ensure accuracy.
rg --type java 'convertToWebp'

Length of output: 371


Script:

#!/bin/bash
# Extract the full implementation of the `convertToWebp` method from MediaHelper.java
ast-grep --lang java --pattern $'private static File convertToWebp($_, $_, $_) throws TelegramApiException {
  $$$
}' 

Length of output: 118


Script:

#!/bin/bash
# Attempt to extract the full implementation of the `convertToWebp` method using a more flexible pattern
rg --type java -A 20 'private static File convertToWebp' 

Length of output: 2412


Script:

#!/bin/bash
# Extract the implementation details of `createWebpFile` and `resizeImage` methods
rg --type java -A 20 'createWebpFile'
rg --type java -A 20 'resizeImage'

Length of output: 11625


81-81: Transition to WebP conversion is correct.

The change from convertToPng to convertToWebp reflects the new image format. Ensure all related logic is updated for WebP.

Verification successful

Transition to WebP conversion is correctly implemented.

The convertToWebp method includes logic to handle image compliance and file size checks before converting to WebP, ensuring proper functionality. No further issues were found.

  • Method convertToWebp is correctly defined and used in MediaHelper.java.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the integration of WebP conversion logic.

# Test: Search for `convertToWebp` usage to ensure it's correctly implemented.
rg --type java 'convertToWebp'

Length of output: 371


Script:

#!/bin/bash
# Description: Extract the implementation details of `convertToWebp` to verify logic.

# Extract the implementation of the `convertToWebp` method.
ast-grep --lang java --pattern $'private static File convertToWebp($_, $_, $_) throws $_ {
  $$$
}' --json

Length of output: 110


Script:

#!/bin/bash
# Description: Extract lines around the usage and declaration of `convertToWebp` to verify its implementation.

# Extract lines around the usage of `convertToWebp` in MediaHelper.java
rg --type java -A 10 -B 10 'convertToWebp' src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java

Length of output: 1522

@rob93c
Copy link
Member Author

rob93c commented Aug 17, 2024

@coderabbitai review

Copy link

coderabbitai bot commented Aug 17, 2024

Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 428965e and 2943af3.

Files selected for processing (8)
  • build.gradle (1 hunks)
  • gradle/libs.versions.toml (1 hunks)
  • src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java (1 hunks)
  • src/main/java/com/github/stickerifier/stickerify/media/MediaConstraints.java (1 hunks)
  • src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java (10 hunks)
  • src/main/java/com/github/stickerifier/stickerify/telegram/Answer.java (1 hunks)
  • src/main/resources/logback.xml (2 hunks)
  • src/test/java/com/github/stickerifier/stickerify/media/MediaHelperTest.java (3 hunks)
Files skipped from review due to trivial changes (2)
  • src/main/java/com/github/stickerifier/stickerify/media/MediaConstraints.java
  • src/main/java/com/github/stickerifier/stickerify/telegram/Answer.java
Files skipped from review as they are similar to previous changes (5)
  • build.gradle
  • gradle/libs.versions.toml
  • src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java
  • src/main/resources/logback.xml
  • src/test/java/com/github/stickerifier/stickerify/media/MediaHelperTest.java
Additional comments not posted (1)
src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java (1)

134-134: Consider the implications of disabling content type detection.

The addition of .disableContentTypeDetection(true) in the SendDocument chain modifies how the document's content type is detected by Telegram. This can be useful for sending non-standard file types or ensuring consistent behaviour across different Telegram clients. However, be aware that it might affect how the document is processed or displayed on the client side. Ensure this aligns with the intended use case.

@rob93c rob93c marked this pull request as ready for review August 17, 2024 11:24
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2943af3 and 192d35b.

Files selected for processing (2)
  • src/main/java/com/github/stickerifier/stickerify/media/MediaConstraints.java (1 hunks)
  • src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java (11 hunks)
Files skipped from review as they are similar to previous changes (2)
  • src/main/java/com/github/stickerifier/stickerify/media/MediaConstraints.java
  • src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java

@rob93c rob93c merged commit 6eeb794 into main Aug 17, 2024
6 checks passed
@rob93c rob93c deleted the convert-images-with-scrimage branch August 17, 2024 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant