Hi,

2009/1/29 Kamaljeet Saini <kamaljeet_singh_saini / hotmail.com>:
> Hi,
>
> I was able to get 1 binary files from dev. and tried the following code
> but .bmp goes bad still. Possible please to look into and advice. Binary
> files attached as zip file.
>
> require 'rmagick'
> file_name = "C:/1.txt" # the binary file
> image_file_name = "C:/1.bmp" # the expected file to be generated
> image = Magick::Image.new(800, 480)
> file = File.open(file_name,"rb").read
>
> one = []
> two = []
> three = []
>
> file.unpack('S*').each { |x|
>  three << (x & 0x1F) * 8
>  #puts "Three : #{three}"
>  two << ((x >> 5) & 0x1F) * 8
>  #puts "Two : #{two}"
>  one << ((x >> 10) & 0x1F) * 8
>  #puts "One : #{one}"
>
> }
>
> bufferscreen_height=480
> bufferscreen_width=800
> k=0
> for i in 0...bufferscreen_height
>  for j in 0...bufferscreen_width
>    q=Magick::Pixel.new(one[k],two[k],three[k],0)
>    #puts "i is : #{i} and j is : #{j}    R:#{one[k]} G:#{two[k]}
> B:#{three[k]}"
>    image.pixel_color(j,i,q)
>    k += 1
>  end
> end
> image.write(image_file_name)
>
The binary input file is 16bit rgb565 format and upside-down image.
Here is the modified code:

require 'rmagick'
file_name = "C:/1.txt" # the binary file
image_file_name = "C:/image1.bmp" # the expected file to be generated
image = Magick::Image.new(800, 480)
file = File.open(file_name,"rb").read

one = []
two = []
three = []

file.unpack('S*').each { |x|
  one << (((x & 0xF800) >> 11) << 3)
  two << (((x & 0x7E0) >> 5) << 2)
  three << ((x &0x1F)<< 3)
}

bufferscreen_height=480
bufferscreen_width=800
k=0
for i in 0...bufferscreen_height
  for j in 0...bufferscreen_width
    q=Magick::Pixel.new(one[k],two[k],three[k],0)
    image.pixel_color(j,bufferscreen_height-i,q)
    k += 1
 end
end
image.write(image_file_name)


Regards,

Park Heesob