I used Ruby to read an image file and save that into a string.
partial_image100 = File.read("image.tga")
partial_image99 = File.read("image.tga")
partial_image98 = File.read("image.tga")
...
I read those images at one end of a distributed system. In another system I want to do a Tail operation. The system receives just the images.
I have around a 100 partial images. I want to do a Tail operation, like this:
tail -c +19 image100 >> image99
tail -c +19 image99 >> image98
tail -c +19 image97 >> image96
...
Basically it just removes the first 18 bytes of the partial image and append what is left to the next image.
The problem is that this is slow. Calling 100 unix commands from Ruby is slow. I want to refactor this so that this happen in Ruby world. Just in memory. No files.
How can I do this in Ruby?
Thanks
edit:
The images are stored in a hash like this:
{"27"=>"\u0000\u0000\u0002\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u000E\u0001\xD0\a\xD0\a\u0018 \xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF\u0000\xFF\xFF...
EDIT:
You have all the relevant code here: https://gist.github.com/989563
There are two files. The code and a hash object encoded in json in a file. When you run the code there will be two image files created at /tmp
- /tmp/image-tail-merger.tga – The output from the tail-merge algorithm
- /tmp/image-/time/.tga – the output from the in-memory-tail algorithm
Currently the in-memory algorithm fails because the generated image is a Picasso.
If you manage to make the in-memory-algorithm generate the same image that the tail-merge algorithm do then you have succeeded.
EDIT:
I got it right finally!!!
Here is the code
https://gist.github.com/989563
You might try something like this
EDIT: In your specific example you could do this to iterate through the entire image
If you want to remove the “tailed” bits permanently you can use
slice!to do an inline replace.Maybe a bit cleaner:
EDIT: Working code example https://gist.github.com/989563