1. package org.jr.awt.image;
  2. /**
  3. * Copyright: Copyright (c) 2002-2004
  4. * Company: JavaResearch(http://www.javaresearch.org)
  5. * 最后更新日期:2003年2月13日
  6. * @author Cherami
  7. */
  8. import java.awt.image.*;
  9. /**
  10. * 灰度变换模型。
  11. * @since 0.1
  12. */
  13. public class GrayModel
  14. extends ColorModel {
  15. /**
  16. * 最大值灰度模式。
  17. * 即取原RGB三色中的最大值作为灰度值。
  18. */
  19. public static final int CS_MAX = 0;
  20. /**
  21. * 浮点平均灰度模式。
  22. * 即取原RGB三色的比例和作为灰度值。
  23. * 计算公式为:R*0.3+G*0.59+B*0.11
  24. */
  25. public static final int CS_FLOAT = 1;
  26. /**
  27. * 绝对平均灰度模式。
  28. * 即取原RGB三色的和的平均值作为灰度值。
  29. * 计算公式为:(R+G+B)/3
  30. */
  31. public static final int CS_AVERAGE = 2;
  32. ColorModel sourceModel;
  33. int modelStyle;
  34. /**
  35. * 构造方法,根据原颜色变换模型构造灰度模型,使用最大值灰度模式。
  36. * @param sourceModel 原颜色变换模型
  37. */
  38. public GrayModel(ColorModel sourceModel) {
  39. super(sourceModel.getPixelSize());
  40. this.sourceModel = sourceModel;
  41. modelStyle = 0;
  42. }
  43. /**
  44. * 构造方法,根据原颜色变换模型和指定的灰度模式构造灰度模型。
  45. * @param sourceModel 原颜色变换模型
  46. * @param style 灰度模式
  47. */
  48. public GrayModel(ColorModel sourceModel, int style) {
  49. super(sourceModel.getPixelSize());
  50. this.sourceModel = sourceModel;
  51. modelStyle = style;
  52. }
  53. /**
  54. * 设置灰度模式。
  55. * @param style 灰度模式
  56. */
  57. public void setGrayStyle(int style) {
  58. modelStyle = style;
  59. }
  60. /**
  61. * 根据灰度模式得到灰度值
  62. * @param pixel 象素的原RGB值
  63. * @return 变换后的灰度值
  64. */
  65. protected int getGrayLevel(int pixel) {
  66. if (modelStyle == CS_MAX) {
  67. return Math.max(sourceModel.getRed(pixel),
  68. Math.max(sourceModel.getGreen(pixel),
  69. sourceModel.getBlue(pixel)));
  70. }
  71. else if (modelStyle == CS_FLOAT) {
  72. return (int) (sourceModel.getRed(pixel) * 0.3 +
  73. sourceModel.getGreen(pixel) * 0.59 +
  74. sourceModel.getBlue(pixel) * 0.11);
  75. }
  76. else if (modelStyle == CS_AVERAGE) {
  77. return (int) ( (sourceModel.getRed(pixel) + sourceModel.getGreen(pixel) +
  78. sourceModel.getBlue(pixel)) / 3);
  79. }
  80. else {
  81. return Math.max(sourceModel.getRed(pixel),
  82. Math.max(sourceModel.getGreen(pixel),
  83. sourceModel.getBlue(pixel)));
  84. }
  85. }
  86. /**
  87. * 得到Alpha值
  88. * @param pixel 象素的原RGB值
  89. * @return 返回原色彩模型的Alpha值
  90. */
  91. public int getAlpha(int pixel) {
  92. return sourceModel.getAlpha(pixel);
  93. }
  94. /**
  95. * 得到红色分量值
  96. * @param pixel 象素的原RGB值
  97. * @return 根据灰度模型变换后的灰度值
  98. */
  99. public int getRed(int pixel) {
  100. return getGrayLevel(pixel);
  101. }
  102. /**
  103. * 得到绿色分量值
  104. * @param pixel 象素的原RGB值
  105. * @return 根据灰度模型变换后的灰度值
  106. */
  107. public int getGreen(int pixel) {
  108. return getGrayLevel(pixel);
  109. }
  110. /**
  111. * 得到蓝色分量值
  112. * @param pixel 象素的原RGB值
  113. * @return 根据灰度模型变换后的灰度值
  114. */
  115. public int getBlue(int pixel) {
  116. return getGrayLevel(pixel);
  117. }
  118. /**
  119. * 得到变换后的RGB值
  120. * @param pixel 象素的原RGB值
  121. * @return 根据灰度模型变换后的RGB值
  122. */
  123. public int getRGB(int pixel) {
  124. int gray = getGrayLevel(pixel);
  125. return (getAlpha(pixel) << 24) + (gray << 16) + (gray << 8) + gray;
  126. }
  127. }