Class BrightnessContrastOp

java.lang.Object
cloud.opencode.base.image.color.BrightnessContrastOp

public final class BrightnessContrastOp extends Object
Brightness and Contrast Adjustment Operations 亮度与对比度调整操作工具类

Provides pixel-level brightness and contrast adjustments using multiplicative factor-based transformations.

提供基于乘法因子的像素级亮度和对比度调整。

Features | 主要功能:

  • Brightness adjustment via multiplicative factor - 基于乘法因子的亮度调整
  • Contrast adjustment centered at 128 - 以 128 为中心的对比度调整
  • Preserves alpha channel - 保留 Alpha 通道

Usage Examples | 使用示例:

// Brighten image by 50%
BufferedImage bright = BrightnessContrastOp.brightness(image, 1.5);

// Increase contrast
BufferedImage highContrast = BrightnessContrastOp.contrast(image, 2.0);

Performance | 性能特性:

  • Time: O(n) where n = pixel count - 时间: O(n),n 为像素数量
  • Space: O(n) for output image - 空间: O(n) 用于输出图像

Security | 安全性:

  • Thread-safe: Yes (stateless) - 线程安全: 是(无状态)
  • Null-safe: No - 空值安全: 否
Since:
JDK 25, opencode-base-image V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • brightness

      public static BufferedImage brightness(BufferedImage image, double factor)
      Adjust brightness of an image by multiplying each RGB component by the given factor. 通过将每个 RGB 分量乘以给定因子来调整图像亮度。

      A factor > 1 brightens, < 1 darkens, and exactly 1 leaves unchanged. Factor = 0 produces a fully black image (alpha preserved).

      因子 > 1 变亮,< 1 变暗,等于 1 不变。 因子 = 0 生成全黑图像(Alpha 通道保持不变)。

      Examples | 示例:

      brightness(image, 1.0)  // no change | 不变
      brightness(image, 2.0)  // twice as bright | 亮度翻倍
      brightness(image, 0.5)  // half brightness | 亮度减半
      brightness(image, 0.0)  // all black | 全黑
      
      Parameters:
      image - the source image | 源图像
      factor - the brightness factor (must be >= 0) | 亮度因子(必须 >= 0)
      Returns:
      the brightness-adjusted image | 亮度调整后的图像
      Throws:
      NullPointerException - if image is null | 当图像为 null 时抛出
      ImageOperationException - if factor is negative, NaN, or infinite | 当因子为负数、NaN 或无穷时抛出
    • contrast

      public static BufferedImage contrast(BufferedImage image, double factor)
      Adjust contrast of an image by scaling RGB components around the midpoint 128. 通过围绕中间值 128 缩放 RGB 分量来调整图像对比度。

      Formula: newVal = clamp((oldVal - 128) * factor + 128, 0, 255)

      A factor > 1 increases contrast, < 1 decreases it, and exactly 1 leaves unchanged.

      因子 > 1 增加对比度,< 1 降低对比度,等于 1 不变。

      Examples | 示例:

      contrast(image, 1.0)  // no change | 不变
      contrast(image, 2.0)  // high contrast | 高对比度
      contrast(image, 0.5)  // low contrast | 低对比度
      contrast(image, 0.0)  // flat gray (128) | 纯灰色 (128)
      
      Parameters:
      image - the source image | 源图像
      factor - the contrast factor (must be >= 0) | 对比度因子(必须 >= 0)
      Returns:
      the contrast-adjusted image | 对比度调整后的图像
      Throws:
      NullPointerException - if image is null | 当图像为 null 时抛出
      ImageOperationException - if factor is negative, NaN, or infinite | 当因子为负数、NaN 或无穷时抛出