r/ArduinoProjects • • Dec 25 '23

Compressing Bitmaps

Hi everyone!

First time here so I'm not sure how exactly to use this subreddit.

I've been programming on a Nano, soldered onto my PCB for a project. I have a loading screen animation for the project, but the bitmaps for the OLED screen take up the majority of the Arduino's space. They are set in the PROGMEM, but this added with the main code is 117% of its memory. Is there any way I can compress the bitmaps down and use those? I've heard the term RLE thrown around in forums but I have no clue how to use it, so there has to be another way.

Any help would be greatly appreciated. Have a great day!

0 Upvotes

8 comments sorted by

1

u/totorodad Dec 25 '23

If you’re willing to learn some on programming then Run Length Encoding isn’t hard. Prescan your images/frames for runs of a single color and encode that. An example storage data type might be: (Color, length). Where color is a char and length is 1 or 2 bytes.

So if you’re storing every raw color of your frames then you can then you can use RLE to scan ahead of time (compress) your frames and store the frames as data which represents whole single color swathes.

So frames that are mostly one color for instance are candidates for RLE compression. Frames that “cost” more to encode as RLE you should have a header which tells your run time decompressor to interpret the frame as is and that the data is “raw”. This isn’t always a win.

There is also a way in Arduino to put your data in non-volatile flash memory. Google F(). Which means the data for your images/frames are stored in memory not used by your program in SRAM.

Depending on your Arduino you may also look at pulling data for your frames from a larger storage media like an sd card or nvram.

1

u/thork_the_stork Dec 26 '23

Hi! Sorry for replying late, time zones and all that.

I've been trying to use RLE, and I do know programming a bit, but I just have no clue where to use it here. I keep trying different ways but it hasn't been working out. Do you have any resources to point me in the right direction here?

The way I've been trying so far is shown below (i need to save the large arrays in progmem for more efficiency, but I'm just trying to get it working first):

#include "Arduino.h"
include <U8g2lib.h>
char characters [] = {0x00,0xff,0x03,0xfc,0x7f,0x00,0xc0,0xff,0x7f,0x00,0xe0,0xfb,0xff,0x3f,0x00,0xf8,0x00,0xfe,0xff,0x1f,0x00,0x3c,0x00,0xf0,0xff,0x0f,0x00,0x1e,0x00,0xf0,0xff,0x0f,0x00,0x0f,0x00,0xc0,0xff,0x00,0x80,0x07,0x00,0x7f,0x00,0x80,0x03,0x00,0x80,0x3f,0x00,0x80,0x3f,0x00,0x1f,0x00,0x80,0x1f,0x00,0x80,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x3f,0x00,0xff,0x03,0x00,0x80,0xff,0x03,0x00,0x80,0x7f,0x00,0x3f,0x00,0x1f,0x00,0x1e,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x0f,0x00,0x0f,0x00,0x07,0x00};
int numbers [] = {198,2,1,1,1,10,1,4,1,10,1,1,3,1,10,1,1,1,1,1,11,1,1,1,1,1,11,1,1,1,1,1,11,1,1,1,1,11,1,1,2,1,11,1,1,1,1,1,14,1,1,15,1,14,1,1,14,1,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,1,13,1,1,1,13,1,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,151}; U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
void setup() { u8g2.begin(); Serial.begin(9600); int counter = 0; unsigned char mainframe [] = {};
for (int i = 0; i < 9; i++) { for (int j = 0; j < numbers[i]; j++) { Serial.println(characters[i]); mainframe[counter] =  characters[i]; counter++; } } u8g2.clearBuffer();          // clear the internal memory u8g2.drawXBMP(0, 0, 128, 64, mainframe); // draw frame of the animation u8g2.sendBuffer(); }
void loop() { // put your main code here, to run repeatedly: }

1

u/totorodad Dec 26 '23

#include "Arduino.h"
include <U8g2lib.h>
char characters [] = {0x00,0xff,0x03,0xfc,0x7f,0x00,0xc0,0xff,0x7f,0x00,0xe0,0xfb,0xff,0x3f,0x00,0xf8,0x00,0xfe,0xff,0x1f,0x00,0x3c,0x00,0xf0,0xff,0x0f,0x00,0x1e,0x00,0xf0,0xff,0x0f,0x00,0x0f,0x00,0xc0,0xff,0x00,0x80,0x07,0x00,0x7f,0x00,0x80,0x03,0x00,0x80,0x3f,0x00,0x80,0x3f,0x00,0x1f,0x00,0x80,0x1f,0x00,0x80,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x3f,0x00,0xff,0x03,0x00,0x80,0xff,0x03,0x00,0x80,0x7f,0x00,0x3f,0x00,0x1f,0x00,0x1e,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x0f,0x00,0x0f,0x00,0x07,0x00};
int numbers [] = {198,2,1,1,1,10,1,4,1,10,1,1,3,1,10,1,1,1,1,1,11,1,1,1,1,1,11,1,1,1,1,1,11,1,1,1,1,11,1,1,2,1,11,1,1,1,1,1,14,1,1,15,1,14,1,1,14,1,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,1,13,1,1,1,13,1,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,15,1,151}; U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
void setup() { u8g2.begin(); Serial.begin(9600); int counter = 0; unsigned char mainframe [] = {};
for (int i = 0; i < 9; i++) { for (int j = 0; j < numbers[i]; j++) { Serial.println(characters[i]); mainframe[counter] = characters[i]; counter++; } } u8g2.clearBuffer(); // clear the internal memory u8g2.drawXBMP(0, 0, 128, 64, mainframe); // draw frame of the animation u8g2.sendBuffer(); }
void loop() { // put your main code here, to run repeatedly: }

Looks like you've got the basic concept for decompressing. You need to make sure you allocate a proper array for Arduino that does not break the 32768 bytes per array limit imposed by the compiler. It also looks like you've got some code outside the setup() and loop() functions which is a no-go. Since your display interface is u8g2 check out this nice example.

1

u/DCorboy Dec 25 '23

Are you sure they’re the correct resolution? If so, another approach you can use is (pre) converting all your data to 565 color encoding (2 bytes/pixel) saving 1/3 over 24-bit color.

RLE will be beneficial if you have a lot of contiguous color, like a black background.

1

u/thork_the_stork Dec 26 '23

The images I'm using are black and white, and its a byte per pixel.

I'm trying RLE, but i don't know where to begin...

1

u/EmielDeBil Dec 26 '23

You can also add an SD card reader or ROM chips to the arduino and control them to expand your memory.

Note that unpacking compressed data will result in more data in memory again, unless you procees it and immeduatejly get rid of it again.

The Arduino is not really made for projects that need more than :2kb. The Arduino mega doubles the RAM, and newer stuff like the esp32 are limitless in comparison to the duino. I would start here.

1

u/thork_the_stork Dec 26 '23

I wish I realised this before hand, but my PCB's already been made/soldered.

But I'll note removing data once used, thank you!