[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Converting binary image file to bmp file using RMagick2.0

Kamaljeet Saini

1/19/2009 6:05:00 PM

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

Attachments:
http://www.ruby-...attachment/3176/...

--
Posted via http://www.ruby-....

18 Answers

Kamaljeet Saini

1/19/2009 6:10:00 PM

0

fyi:

The source file "image1.txt" is 32 bit binary file and the file
"image1.bmp" that to be created needs to be 24 bit. This convertion
someone alreasy implemented in C# in our team from dev. side but we need
to do this using Ruby/RMagick.


--
Posted via http://www.ruby-....

Jan Dvorak

1/19/2009 10:08:00 PM

0

On Monday 19 January 2009 19:09:34 Kamaljeet Saini wrote:
> fyi:
>
> The source file "image1.txt" is 32 bit binary file and the file
> "image1.bmp" that to be created needs to be 24 bit. This convertion
> someone alreasy implemented in C# in our team from dev. side but we need
> to do this using Ruby/RMagick.

The source file you provided isn't really 32bit, but 16bit (probably in RGB565
mode)

Jan


Kamaljeet Saini

1/19/2009 10:37:00 PM

0

OK, if its 16 bit any way around to get it decoded and proper image out
in 24 bit.
--
Posted via http://www.ruby-....

Tim Hunter

1/19/2009 10:47:00 PM

0

Kamaljeet Saini wrote:
> OK, if its 16 bit any way around to get it decoded and proper image out
> in 24 bit.

You can write the .bmp file using a BMP3: prefix to force 24-bit format:

img.write("bmp3:myimage.bmp")

For more information about ImageMagick and BMP format, go here:
http://www.imagemagick.org/Usage/fo....

--
RMagick: http://rmagick.ruby...

Kamaljeet Saini

1/19/2009 11:25:00 PM

0

The file out still comes out incorrect. Please see attached file.

Attachments:
http://www.ruby-...attachment/3180/...

--
Posted via http://www.ruby-....

Kamaljeet Saini

1/20/2009 6:46:00 PM

0

Just to know and make sure that if its possible using Ruby + RMagick 2.0
to convert binary 32bit ".TEXT" file to 24 bit ".bmp" file ?

thanks

--
Posted via http://www.ruby-....

Heesob Park

1/21/2009 3:14:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

Hi,

2009/1/20 Kamaljeet Saini <kamaljeet_singh_saini@hotmail.com>:
> 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

Kamaljeet Saini

1/22/2009 4:35:00 PM

0

The above posting code worked fine for 704/480 binary to image file but
for the attached file the same code is not working. Its 800/480 binary
file. Possible to advice please. Im try to use the following code.

require 'rmagick'
file_name = "C:/image2.text" # the binary file
image_file_name = "C:/image2.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)
image.pixel_color(j,i,q)
k += 1
end
end
image.write(image_file_name)

Attachments:
http://www.ruby-...attachment/3194/i...

--
Posted via http://www.ruby-....

Heesob Park

1/23/2009 1:09:00 AM

0

2009/1/23 Kamaljeet Saini <kamaljeet_singh_saini@hotmail.com>
>
> The above posting code worked fine for 704/480 binary to image file but
> for the attached file the same code is not working. Its 800/480 binary
> file. Possible to advice please. Im try to use the following code.
>
> require 'rmagick'
> file_name = "C:/image2.text" # the binary file
> image_file_name = "C:/image2.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)
> image.pixel_color(j,i,q)
> k += 1
> end
> end
> image.write(image_file_name)
>
> Attachments:
> http://www.ruby-forum.com/attachment/3194/i...

I guess your input binary file is corrupted. It's size must be
768000(800x480x2) bytes but it is only 76801 bytes.

Regards,

Park Heesob

Kamaljeet Saini

1/28/2009 4:28:00 PM

0

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)

thanks

Attachments:
http://www.ruby-...attachment/...

--
Posted via http://www.ruby-....