6 Replies Latest reply: Oct 6, 2010 4:17 PM by 796367 RSS

    Need conversion mechanism for converting 24 bit image to 16 bit

    802846
      In my java project I need to convert 24 bit image to 16 bit image. I don't have any clue. Please provide me the option to implement it.

      Thanks,
      jai
        • 1. Re: Need conversion mechanism for converting 24 bit image to 16 bit
          DarrylBurke
          Cross posted
          http://www.coderanch.com/t/512391/java/java/conversion-mechanism-converting-bit-image

          db
          • 2. Re: Need conversion mechanism for converting 24 bit image to 16 bit
            jduprez
            In my java project I need to convert 24 bit image to 16 bit image.
            I don't have any clue.
            Here is a clue (this is the Wikipedia entry for image formats and encoding schemes): http://en.wikipedia.org/wiki/Image_format
            Please provide me the option to implement it.
            Turning pixel color encoding from 24 bits to 16 bits involves a loss of precision. You need to think more (or give us more info) about your requirements (which kind of precision matters, which kind of loss is acceptable).

            Best regards,

            J.

            Edited by: jduprez on Oct 4, 2010 1:50 PM
            Note that the Wikipedia entry doesn't mention "16 bits" to be common. A superficial search shows that among the "popular" formats only WIndows' BMP format may use 16 bits per pixel (PNG allows 16 bits per channel, which is not the same thing, and allows plain 16-bits pixels in greyscale). I really suggest that you give more details about your homework project :o)
            • 3. Re: Need conversion mechanism for converting 24 bit image to 16 bit
              DarrylBurke
              Another cross post
              http://www.java-forums.org/new-java/33114-need-conversion-mechanism-converting-24-bit-image-16-bit.html

              db
              • 4. Re: Need conversion mechanism for converting 24 bit image to 16 bit
                796367
                0xFFFF -- ALPHA, RED, GREEN, BLUE.
                • 5. Re: Need conversion mechanism for converting 24 bit image to 16 bit
                  EJP
                  0xFFFF -- ALPHA, RED, GREEN, BLUE.
                  That doesn't tell me much, and I know what you're talking about. I doubt that it tells the OP anything useful whatsoever.
                  • 6. Re: Need conversion mechanism for converting 24 bit image to 16 bit
                    796367
                    That doesn't tell me much, and I know what you're talking about. I doubt that it tells the OP anything useful whatsoever.
                    Sorry EJP, it was a mouthful I know. Maybe this will help instead, a ColorConversionOps class to work on *32 bit > 16 bit > 32 bit colors*. To help clarify where the 16 bit color input and output goes, some of the methods return or require parameters of type short. Also take notice the bitmask for variables of type short vs the bitmasks for int. AND pay particular attention to the unsigned right bit-shifting of the most-significant-digit.
                    public class ColorConversionOps
                    {
                      public static short convert32_16(int rgb) {
                        int a = ((rgb & 0xF0000000) >>> 16);
                        int r = ((rgb & 0x00F00000) >>  12);
                        int g = ((rgb & 0x0000F000) >>   8);
                        int b = ((rgb & 0x000000F0) >>   4);
                        return (short)( a | r | g | b);
                      }
                    
                      public static int convert16_32(short rgb) {
                        int a = ((rgb & 0xF000) << 16);
                        int r = ((rgb & 0x0F00) << 12);
                        int g = ((rgb & 0x00F0) <<  8);
                        int b = ((rgb & 0x000F) <<  4);
                        return (a | r | g | b);
                      }
                    
                      public static void printRGB_32(int rgb) {
                        System.out.println("a="+((rgb & 0xFF000000) >>> 24));
                        System.out.println("r="+((rgb & 0x00FF0000) >>  16));
                        System.out.println("g="+((rgb & 0x0000FF00) >>  8));
                        System.out.println("b="+ (rgb & 0x000000FF));
                        System.out.println();
                      }
                    
                      public static void printRGB_16(short rgb) {
                        System.out.println("a="+((rgb & 0xF000) >>> 12));
                        System.out.println("r="+((rgb & 0x0F00) >>  8));
                        System.out.println("g="+((rgb & 0x00F0) >>  4));
                        System.out.println("b="+ (rgb & 0x000F));
                        System.out.println();
                      }
                    }
                    Now to make the conversion from 32 bit color to 16 bit color and back. NOTICE there willl be a loss of precision -- just like someone else has already said.
                    public class Test
                    {
                      public static void main(String[] args)
                      {
                        int rgb = 0xFFFFFFFF;
                        ColorConversionOps.printRGB_32(rgb);
                        //a=255
                        //r=255
                        //g=255
                        //b=255
                    
                        rgb = ColorConversionOps.convert32_16(rgb);
                        ColorConversionOps.printRGB_16((short)rgb);
                        //a=15
                        //r=15
                        //g=15
                        //b=15
                    
                        rgb = ColorConversionOps.convert16_32((short)rgb);
                        ColorConversionOps.printRGB_32(rgb);
                        //a=240
                        //r=240
                        //g=240
                        //b=240
                      }
                    }
                    And now we see, the reason 16bit color is so unpopular to the rest of the world, but not for us here!