From: Heesob Park Date: 2009-01-29T11:06:27+09:00 Subject: Re: Converting binary image file to bmp file using RMagick2.0 2009/1/29 Kamaljeet Saini : > The code sent work prefectly fine. Thanks you so much for this help. > > Possible to help to understand the following code. Understanding is > important i guess for future or any site that i can refer in regards to > the following code. > > - whats happeing in this "file.unpack('S*')." > - what this kind of code mean "0xF800" > - what this means ">> 11) << 3)" > > file.unpack('S*').each { |x| > one << (((x & 0xF800) >> 11) << 3) > two << (((x & 0x7E0) >> 5) << 2) > three << ((x &0x1F)<< 3) > } file.unpack('S*') is for converting byte array to 16bit integer array. In case of RBG565, the each 16bit consists of RRRRRGGGGGGBBBBB (R: Red, G: Green, B: Blue) First, extracting each color from rgb565 red = (rgb565 & 0xF800) >> 11 green = (rgb565 & 0x7E0) >> 5 blue = (rgb565 & 0x1F) Then, converting 16bit color to 24bit color by multiplying 8,4,8 r = red << 3 g = green << 2 b = blue << 3 > How do you come to know that "binary input file is 16bit rgb565 format" The 16bit image is either rgb555 or rgb565. If trying with rgb555 not works, it is rgb565. Regards, Park Heesob