Hi,
I have a packed string of raw binary pixel data that I want to
manipulate. However, I'm facing a few problems.
Firstly, the only way I've managed to unpack the data so far is with
"pixels.unpack('b*')". It works great, but comes out in one massive
string.
Is there a way to:
a)put this into an array with 8 bits in each element, eg. ['00000000',
'01010101'...]
b)unpack small sections of a few bites
The other problem is that I don't know of an efficient way to manipulate
this data without having to unpack\pack massive strings each time.
Hi,
I have a packed string of raw binary pixel data that I want to
manipulate. However, I'm facing a few problems.
Firstly, the only way I've managed to unpack the data so far is with
"pixels.unpack('b*')". It works great, but comes out in one massive
string.
Is there a way to:
a)put this into an array with 8 bits in each element, eg. ['00000000',
'01010101'...]
b)unpack small sections of a few bites
The other problem is that I don't know of an efficient way to manipulate
this data without having to unpack\pack massive strings each time.
Are there any solutions to these?
--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama
I don't have a 1.9 handy so I can't tell you how to do it there. But the basic lesson should be, that you can leave your data in a String and manipulate it directly there.
Kind regards
robert
···
On 14.06.2008 13:56, Lucas L. wrote:
Dave Bass wrote:
"Pixels".each_byte { |b| printf("%08b\n", b) }
That's good for the first problem, thanks.
Now for the more difficult one of efficiently manipulating small portions of the packed binary data.
How can I replace portions of a packed binary string this way?
I know with a normal string it is easy:
irb(main):001:0> s = "abcdef"
=> "abcdef"
irb(main):002:0> s[0..2] = "cba"
=> "cba"
irb(main):003:0> s
=> "cbadef"
A string is just bytes in a row with special characteristics/methods.
Todd
···
On Sun, Jun 15, 2008 at 12:16 AM, Lucas L. <lucaslevin@gmail.com> wrote:
How can I replace portions of a packed binary string this way?
I know with a normal string it is easy:
irb(main):001:0> s = "abcdef"
=> "abcdef"
irb(main):002:0> s[0..2] = "cba"
=> "cba"
irb(main):003:0> s
=> "cbadef"
On Sun, Jun 15, 2008 at 12:16 AM, Lucas L. <lucaslevin@gmail.com> wrote:
How can I replace portions of a packed binary string this way?
I know with a normal string it is easy:
irb(main):001:0> s = "abcdef"
=> "abcdef"
irb(main):002:0> s[0..2] = "cba"
=> "cba"
irb(main):003:0> s
=> "cbadef"
But I can't figure out how to do it with binary.
Look closely at Robert's irb line number 9.
A string is just bytes in a row with special characteristics/methods.
Todd
You'll have to bear with me on this, I struggle with this sort of this.
I don't really know what using | achieves.
And a string may be bytes normally, but mine is a sequence of bits (well
is it in my flawed understanding). I'm using Gtk::Pixbuf.pixels, if that
helps at all.
How can I replace portions of a packed binary string this way?
I know with a normal string it is easy:
irb(main):001:0> s = "abcdef"
=> "abcdef"
irb(main):002:0> s[0..2] = "cba"
=> "cba"
irb(main):003:0> s
=> "cbadef"
But I can't figure out how to do it with binary.
Look closely at Robert's irb line number 9.
A string is just bytes in a row with special characteristics/methods.
You'll have to bear with me on this, I struggle with this sort of this.
I don't really know what using | achieves.
It's the bitwise OR operator.
irb(main):001:0> 1 | 2
=> 3
And a string may be bytes normally, but mine is a sequence of bits (well is it in my flawed understanding). I'm using Gtk::Pixbuf.pixels, if that helps at all.
I do not know what Gtk::Pixbuf.pixels returns. But if it is a String you can manipulate it like was have show before. Can you post what "p your_pixbuf.pixels" and "p your_pixbuf.pixels.class" print?
Kind regards
robert
···
On 15.06.2008 11:08, Lucas L. wrote:
On Sun, Jun 15, 2008 at 12:16 AM, Lucas L. <lucaslevin@gmail.com> wrote:
your_pixbuf.pixels" and "p your_pixbuf.pixels.class" print?
Now we're getting somewhere. I was using puts to print pixels, and all I
got was a bunch of question marks. p returns a massive String
(pixels.class shows String). The string itself is uncompressed pixel
data, a byte per channel of RGBA. The output is a massive string of
"\000\000\000\000\000\000\000\000..." with "\377" where a pixel is
drawn, which I'm assumming is 0xFF in the alpha channel (why is \377
0xFF?).
I agree with Paul, you have trouble with the basics. If you try things out on a smaller scale in IRB you'll probably see more easily how everything works.
Kind regards
robert
···
On 15.06.2008 12:00, Lucas L. wrote:
Robert Klemme wrote:
It's the bitwise OR operator.
I don't really know why he uses it though.
Can you post what "p
your_pixbuf.pixels" and "p your_pixbuf.pixels.class" print?
Now we're getting somewhere. I was using puts to print pixels, and all I got was a bunch of question marks. p returns a massive String (pixels.class shows String). The string itself is uncompressed pixel data, a byte per channel of RGBA. The output is a massive string of "\000\000\000\000\000\000\000\000..." with "\377" where a pixel is drawn, which I'm assumming is 0xFF in the alpha channel (why is \377 0xFF?).