From: Heesob Park Date: 2009-01-21T12:13:41+09:00 Subject: Re: Converting binary image file to bmp file using RMagick2.0 --001636e90f6cbf1bea0460f59007 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hi, 2009/1/20 Kamaljeet Saini : > We are trying to convert "image1.txt" file which is a binary file to > "image1.bmp" file usng Ruby + RMagick2.0 but not able to get the > required output that is "Menu.bmp". Following the attached code that we > have reached so far. Look forward to get some help as how to get an > output like "Menu.bmp" from the file called "image1.txt" > > I was not able to attached all the three files ( image1.txt, image1.bmp > and Menu.bmp) So just attached the source file "image1.txt" and if you > run the following case, it wll generate "image1.bmp" > > require 'rmagick' > file_name = "C:/image1.text" # the binary file > image_file_name = "C:/image1.bmp" # the expected file to be generated > image = Magick::Image.new(704, 480) > file = File.read(file_name) > > #=begin > one = [] > two = [] > three = [] > > p = 0 > l = 1 > file.each_byte { |x| > case l > when 1 > one << x > #getOne=((moveOne << 1 + 16) >> 11 + 16) > #one << getOne > l += 1 > when 2 > two << x > #getTwo=((moveTwo << 6 + 16) >> 11 + 16) > #two << getTwo > l += 1 > when 3 > three << x > #getThree=((moveThree << 11 + 16) >> 11 + 16) > #three << getThree > p += 1 > #puts "#{one} #{two} #{three}" > l = 1 > end > } > > > bufferscreen_height=480 > bufferscreen_width=704 > k=0 > for i in 0..bufferscreen_height > #puts "Height: #{i} \n\n" > for j in 0..bufferscreen_width > #puts "Width: #{j} \n\n" > #q=Magick::Pixel.new(25,75,34, 0) > > q=Magick::Pixel.new(one[k],two[k],three[k],0) > #q=Magick::Pixel.new(((one[k] << 17) >> 27), ((two[k] << 22) >> 27), > ((three[k] << 27) >> 27), 0) > #puts "R:#{one[k]} G:#{two[k]} B:#{three[k]}" > #puts "\n\n" > #y, z = p.divmod(704) > image.pixel_color(j,i,q) > > k += 1 > end > end > image.write(image_file_name) > #=end > Here is a working code for your 16 bit input file: require 'rmagick' file_name = "C:/image1.text" # the binary file image_file_name = "C:/image1.bmp" # the expected file to be generated image = Magick::Image.new(704, 480) file = File.open(file_name,"rb").read one = [] two = [] three = [] file.unpack('S*').each { |x| three << (x & 0x1F) * 8 two << ((x >> 5) & 0x1F) * 8 one << ((x >> 10) & 0x1F) * 8 } bufferscreen_height=480 bufferscreen_width=704 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,i,q) k += 1 end end image.write(image_file_name) Regards, Park Heesob --001636e90f6cbf1bea0460f59007--