Lunarpages Web Hosting Forum

Author Topic: Perl byte shuffling  (Read 2333 times)

Offline MrPhil

  • Senior Moderator
  • Berserker Poster
  • *****
  • Posts: 6429
Perl byte shuffling
« on: September 27, 2018, 04:51:40 PM »
In Perl, I need to split a single string of an RGBA buffer and extract the RGB bytes into a new string (as well as the A bytes into a separate string). I have found several ways to do this, but they really slow down the code, to the point where handling RGBA takes about 60x as long as handling just (originally) RGB. We're talking image files on the order of 4.2Megapixel (17.28MB), so every bit helps.

The first cut is to use vec() to extract 1 byte at a time from the source and vec() to insert it into the (growing) target string. This is the fastest so far. vec() doesn't like doing 3 bytes at a time, so it's 3 calls per pixel (plus one for Alpha). I also tried concatenating the extracted byte onto the target string ( .= operator instead of the second vec() call), but this corrupted the string (too bad -- it was 3x faster).

I also tried unpack() with a template pattern '(A3x)*' for RGB and immediately pack() with pattern '(A3)*', but this was almost 3x slower! Codes other than 'A' were even slower, if they didn't hang Perl.

So, can anyone tell me a better way to take an input string
Code: [Select]
RGBaRGBaRGBa...and split it up into
Code: [Select]
strings in pure Perl, that will be faster than using vec()? The solution also needs to be adaptable for 16 bits per sample (48 bit RGB + 16 bit Alpha). Finally, I don't want to require another package (another prereq).

I've googled all day on this, and haven't found anything useful. If you can help with a suggestion, thanks!
Visit My Site

E-mail Me
-= From the ashes shall rise a sooty tern =-