discord / lilliput

Resize images and animated GIFs in Go
https://discord.com/blog/how-discord-resizes-150-million-images-every-day-with-go-and-c
Other
1.96k stars 123 forks source link

fix: Improved Error Handling and Grayscale Support #181

Closed skidder closed 2 months ago

skidder commented 2 months ago

Improves handling of boundary conditions and grayscale inputs. Key changes include:

  1. Function Refactoring:

    • Renamed opencv_copy_with_alpha_blending to opencv_copy_to_region_with_alpha
    • Updated opencv_copy_to_rect to opencv_copy_to_region These changes better reflect the functions' purposes and unify naming conventions.
  2. Enhanced Error Handling:

    • Implemented try-catch blocks in C++ functions to catch and log exceptions
    • Added new error codes for more specific error scenarios (e.g., resize failures, conversion issues)
    • Created a centralized error handling function in Go (handleOpenCVError)
    • Improved error reporting with detailed error messages
  3. Grayscale Image Support:

    • Added handling for 1-channel (grayscale) images in both main functions
    • Implemented appropriate color space conversions (e.g., GRAY2BGR, GRAY2BGRA)
  4. Input Validation and Boundary Checks:

    • Added checks for null and empty matrices
    • Implemented more robust boundary checks for copy operations
    • Added validation for input dimensions
  5. Channel Conversion Handling:

    • Added logic to handle mismatched channel counts between source and destination images
    • Implemented appropriate color space conversions (e.g., BGR2BGRA, BGRA2BGR)
  6. Performance Considerations:

    • Optimized matrix copying in some scenarios to avoid unnecessary clone() operations
  7. Go Wrapper Improvements:

    • Updated function calls to match new C++ function names
    • Enhanced error handling in Go code to utilize new error codes
  8. Code Structure and Readability:

    • Improved code organization for better readability and maintainability
    • Added comments to clarify complex operations