code_junkie

Colorizing images in Java

Nov 10th, 2011
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. protected LookupOp createColorizeOp(short R1, short G1, short B1) {
  2. short[] alpha = new short[256];
  3. short[] red = new short[256];
  4. short[] green = new short[256];
  5. short[] blue = new short[256];
  6.  
  7. int Y = 0.3*R + 0.59*G + 0.11*B
  8.  
  9. for (short i = 0; i < 256; i++) {
  10. alpha[i] = i;
  11. red[i] = (R1 + i*.3)/2;
  12. green[i] = (G1 + i*.59)/2;
  13. blue[i] = (B1 + i*.11)/2;
  14. }
  15.  
  16. short[][] data = new short[][] {
  17. red, green, blue, alpha
  18. };
  19.  
  20. LookupTable lookupTable = new ShortLookupTable(0, data);
  21. return new LookupOp(lookupTable, null);
  22. }
  23.  
  24. BufferedImageOp colorizeFilter = createColorizeOp(R1, G1, B1);
  25. BufferedImage targetImage = colorizeFilter.filter(sourceImage, null);
  26.  
  27. public class Colorize {
  28.  
  29. public static final int MAX_COLOR = 256;
  30.  
  31. public static final float LUMINANCE_RED = 0.2126f;
  32. public static final float LUMINANCE_GREEN = 0.7152f;
  33. public static final float LUMINANCE_BLUE = 0.0722f;
  34.  
  35. double hue = 180;
  36. double saturation = 50;
  37. double lightness = 0;
  38.  
  39. int [] lum_red_lookup;
  40. int [] lum_green_lookup;
  41. int [] lum_blue_lookup;
  42.  
  43. int [] final_red_lookup;
  44. int [] final_green_lookup;
  45. int [] final_blue_lookup;
  46.  
  47. public Colorize( int red, int green, int blue )
  48. {
  49. doInit();
  50. }
  51.  
  52. public Colorize( double t_hue, double t_sat, double t_bri )
  53. {
  54. hue = t_hue;
  55. saturation = t_sat;
  56. lightness = t_bri;
  57. doInit();
  58. }
  59.  
  60. public Colorize( double t_hue, double t_sat )
  61. {
  62. hue = t_hue;
  63. saturation = t_sat;
  64. doInit();
  65. }
  66.  
  67. public Colorize( double t_hue )
  68. {
  69. hue = t_hue;
  70. doInit();
  71. }
  72.  
  73. public Colorize()
  74. {
  75. doInit();
  76. }
  77.  
  78. private void doInit()
  79. {
  80. lum_red_lookup = new int [MAX_COLOR];
  81. lum_green_lookup = new int [MAX_COLOR];
  82. lum_blue_lookup = new int [MAX_COLOR];
  83.  
  84. double temp_hue = hue / 360f;
  85. double temp_sat = saturation / 100f;
  86.  
  87. final_red_lookup = new int [MAX_COLOR];
  88. final_green_lookup = new int [MAX_COLOR];
  89. final_blue_lookup = new int [MAX_COLOR];
  90.  
  91. for( int i = 0; i < MAX_COLOR; ++i )
  92. {
  93. lum_red_lookup [i] = ( int )( i * LUMINANCE_RED );
  94. lum_green_lookup[i] = ( int )( i * LUMINANCE_GREEN );
  95. lum_blue_lookup [i] = ( int )( i * LUMINANCE_BLUE );
  96.  
  97. double temp_light = (double)i / 255f;
  98.  
  99. Color color = new Color( Color.HSBtoRGB( (float)temp_hue,
  100. (float)temp_sat,
  101. (float)temp_light ) );
  102.  
  103. final_red_lookup [i] = ( int )( color.getRed() );
  104. final_green_lookup[i] = ( int )( color.getGreen() );
  105. final_blue_lookup [i] = ( int )( color.getBlue() );
  106.  
  107. double temp_light = (double)i / 255f;
  108.  
  109. Color color = new Color( Color.HSBtoRGB( (float)temp_hue,
  110. (float)temp_sat,
  111. (float)temp_light ) );
  112.  
  113. final_red_lookup [i] = ( int )( color.getRed() );
  114. final_green_lookup[i] = ( int )( color.getGreen() );
  115. final_blue_lookup [i] = ( int )( color.getBlue() );
  116.  
  117. while( width-- != 0 )
  118. {
  119. Color color = new Color( image.getRGB( width, height ) );
  120.  
  121. int lum = lum_red_lookup [color.getRed ()] +
  122. lum_green_lookup[color.getGreen()] +
  123. lum_blue_lookup [color.getBlue ()];
  124.  
  125. if( lightness > 0 )
  126. {
  127. lum = (int)((double)lum * (100f - lightness) / 100f);
  128. lum += 255f - (100f - lightness) * 255f / 100f;
  129. }
  130. else if( lightness < 0 )
  131. {
  132. lum = (int)(((double)lum * lightness + 100f) / 100f);
  133. }
  134.  
  135. Color final_color = new Color( final_red_lookup[lum],
  136. final_green_lookup[lum],
  137. final_blue_lookup[lum],
  138. color.getAlpha() );
  139.  
  140. image.setRGB( width, height, final_color.getRGB() );
  141.  
  142. }
Add Comment
Please, Sign In to add comment