Colorizing images in Java

前端 未结 4 1875
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 23:52

I\'m working on some code to colorize an image in Java. Basically what I\'d like to do is something along the lines of GIMP\'s colorize command, so that if I have a Buffered

4条回答
  •  萌比男神i
    2020-12-04 00:43

    I wanted to do the exact same thing as the question poster wanted to do but the above conversion did not remove colors like the GIMP does (ie green with a red overlay made an unpleasant brown color etc). So I downloaded the source code for GIMP and converted the c code over to Java.

    Posting it in this thread just in case anyone else wants to do the same (since it is the first thread that comes up in Google). The conversion still changes the white color when it should not, it's probably a casting issue from double to int. The class converts a BufferedImage in-place.

    public class Colorize {
    
    public static final int MAX_COLOR = 256;
    
    public static final float LUMINANCE_RED   = 0.2126f;
    public static final float LUMINANCE_GREEN = 0.7152f;
    public static final float LUMINANCE_BLUE  = 0.0722f;
    
    double hue        = 180;
    double saturation =  50;
    double lightness  =   0;
    
    int [] lum_red_lookup;
    int [] lum_green_lookup;
    int [] lum_blue_lookup;
    
    int [] final_red_lookup;
    int [] final_green_lookup;
    int [] final_blue_lookup;
    
    public Colorize( int red, int green, int blue )
    {
       doInit();
    }
    
    public Colorize( double t_hue, double t_sat, double t_bri )
    {
       hue = t_hue;
       saturation = t_sat;
       lightness = t_bri;
       doInit();
    }
    
    public Colorize( double t_hue, double t_sat )
    {
       hue = t_hue;
       saturation = t_sat;
       doInit();
    }
    
    public Colorize( double t_hue )
    {
       hue = t_hue;
       doInit();
    }
    
    public Colorize()
    {
       doInit();
    }
    
    private void doInit()
    {
       lum_red_lookup   = new int [MAX_COLOR];
       lum_green_lookup = new int [MAX_COLOR];
       lum_blue_lookup  = new int [MAX_COLOR];
    
       double temp_hue = hue / 360f;
       double temp_sat = saturation / 100f;
    
       final_red_lookup   = new int [MAX_COLOR];
       final_green_lookup = new int [MAX_COLOR];
       final_blue_lookup  = new int [MAX_COLOR];
    
       for( int i = 0; i < MAX_COLOR; ++i )
       {
          lum_red_lookup  [i] = ( int )( i * LUMINANCE_RED );
          lum_green_lookup[i] = ( int )( i * LUMINANCE_GREEN );
          lum_blue_lookup [i] = ( int )( i * LUMINANCE_BLUE );
    
          double temp_light = (double)i / 255f;
    
          Color color = new Color( Color.HSBtoRGB( (float)temp_hue, 
                                                   (float)temp_sat, 
                                                   (float)temp_light ) );
    
          final_red_lookup  [i] = ( int )( color.getRed() );
          final_green_lookup[i] = ( int )( color.getGreen() );
          final_blue_lookup [i] = ( int )( color.getBlue() );
       }
    }
    
    public void doColorize( BufferedImage image )
    {
       int height = image.getHeight();
       int width;
    
       while( height-- != 0 )
       {
          width = image.getWidth();
    
          while( width-- != 0 )
          {
             Color color = new Color( image.getRGB( width, height ) );
    
             int lum = lum_red_lookup  [color.getRed  ()] +
                       lum_green_lookup[color.getGreen()] +
                       lum_blue_lookup [color.getBlue ()];
    
             if( lightness > 0 )
             {
                lum = (int)((double)lum * (100f - lightness) / 100f);
                lum += 255f - (100f - lightness) * 255f / 100f;
             }
             else if( lightness < 0 )
             {
                lum = (int)(((double)lum * lightness + 100f) / 100f);
             }
    
             Color final_color = new Color( final_red_lookup[lum],
                                            final_green_lookup[lum],
                                            final_blue_lookup[lum],
                                            color.getAlpha() );
    
             image.setRGB( width, height, final_color.getRGB() );
    
          }
       }
    }
    

提交回复
热议问题