Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RLE code might be wrong #3

Open
X-Ryl669 opened this issue Jun 27, 2016 · 4 comments
Open

RLE code might be wrong #3

X-Ryl669 opened this issue Jun 27, 2016 · 4 comments

Comments

@X-Ryl669
Copy link
Contributor

There is no escape code in the RLE encoding code in PHP if the "pixel/color" to be encoded has a value higher than 0x80 and happens only once. In that case, the PHP code will output the pixel -as-is-. However, when decoding, it'll be skipped because it'll be seen as "RLE length" code, and will repeat the next pixel (pix & 0x7F) times.

Also, to speed up the PHP image encoding code, it'll be better to preallocate the output array and access it with iterators, instead of constantly growing it.

@davidgfnet
Copy link
Owner

Can you provide an input that can exploit that? I'm not seeing what you say sorry, I might be too tired.
And yes, the routine is massively slow and I have a couple of ideas on how to speed it up, I'll fix it in the coming days (since it makes my screen to time out when my server is doing other stuff and PHP gets CPU bottlenecked).
Thanks!

@X-Ryl669
Copy link
Contributor Author

X-Ryl669 commented Jun 28, 2016

This is what I get:
Code

$buf = [255, 255, 255, 128, 120, 112, 0, 0, 0, 0, 0, 230, 0, 30, 32, 32, 33, 33, 33, 34, 34, 34, 34, 128, 128, 128, 128, 128];
echo "In\n";
foreach($buf as $b)
   printf("%02x ", $b);
echo "\n";

echo "Out\n";
$out = img_compress($buf);
foreach($out as $b)
   printf("%02x ", $b);
echo "\n";

Result

In
ff ff ff 80 78 70 00 00 00 00 00 e6 00 1e 20 20 21 21 21 22 22 22 22 80 80 80 80 80
Out
85 ff ff ff 80 78 70 04 00 87 e6 00 1e 20 20 21 21 21 03 22 04 80

Then, looking at the output stream, I see many missed opportunities to compress, ideal with the decoder expectations would be:

02 ff 82 80 78 70 04 00 84 e6 00 1e 20 20 02 21 03 22 04 80

If the first bytes in the input picture are not repeating at least 4 times, then the first byte in the output stream is wrong.

TLDR: I'm not sure about the line $encoderle = ($bytec > 3); which should read, $encoderle = ($bytec > 2); to me, which gives the best output and does decompress to the input sequence too.

@davidgfnet
Copy link
Owner

So let me get this clear: You are just saying that the compressor is not optimal, but it works, right?
Cause your original post seemed to imply that the compressor/decompressor would break under certain circumstances (inputs).
But yeah you are right, the "> 3" should be a ">= 3", that's a typo there that generates suboptimal encodings. Also since we don't bother to encode 1 or 2 bytes repeated sequences (for obvious reasons) we could expand the encoding lenght by subtracting a 2 and therefore allowing 129 byte RLEs (that might improve compression for large uniform colors).
Also note that the PHP always outputs a 60KB image regardless of the compression level (pads with zeros) due to the fact that the image doesn't have a header with the content length. That's definetly an improvement we could add to the STM32 firmware.

@X-Ryl669
Copy link
Contributor Author

First question, yes it works suboptimal. The first test I give it was with quasi random picture, and it failed but because it overflowed, but the error was between the keyboard and the chair.
Second remark, yes it could be a definitive improvement since it takes time to transmit it to both CPU (ESP is limited by STM32 SPI's speed anyway).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants