将图形保存到EEPROM,通过重复0x00和0xFF的过滤器压缩图形以节省空间[解决]

问题描述:

作为固件的一部分,我想将图形或图形保存到MCU的EEPROM中。空间不是很大,1K,但是它可以节省一些程序空间。是的,你可以分离图形的字形来节省空间,但它不易管理,你需要更多的代码来正确显示它。将图形保存到EEPROM,通过重复0x00和0xFF的过滤器压缩图形以节省空间[解决]

大多数单色GUI图形不会完全填满屏幕,并且包含空白空间或重复像素的分配。图像已被压缩,每个字节中的每一位代表8个像素。

我在128x32像素的小型显示器上显示图形。显示它并删除不相关的部分,工作得非常精细和高效。

所以我想出了这个想法来过滤这些重复,将其压缩一点。成功后,像这样的位图(见下文)是496个字节,用我的方法缩小了401个字节。


enter image description here


这听起来并不多但是在总规模下降了20%,真是太好了当只有1K的可用存储空间。字节数组的

例子:

PROGMEM const uint8_t TEP_DISPLAY [] = { /* 496 bytes */ 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 
0x06, 0x00, 0x80, 0x90, 0x00, 0x3E, 0x01, 0x80, 0x03, 0xC0, 0x01, 0x80, 0x00, 0x47, 0x0F, 0xFE, 
0x17, 0x01, 0xC0, 0x90, 0x00, 0x30, 0x00, 0x00, 0x03, 0x60, 0x01, 0x80, 0x01, 0x87, 0x10, 0x02, 
0x30, 0x83, 0xE3, 0xFC, 0x00, 0x61, 0xE7, 0x39, 0xB6, 0x6F, 0x0F, 0x00, 0x03, 0x07, 0x36, 0xDA, 
0x7F, 0xF0, 0x83, 0xFC, 0x7C, 0x7D, 0xB3, 0x6D, 0xB6, 0x61, 0x9B, 0x1F, 0x03, 0x87, 0x36, 0xDA, 
0x30, 0x43, 0xE1, 0xF8, 0x00, 0x61, 0xB3, 0x6D, 0xA7, 0xCF, 0xB3, 0x00, 0x01, 0x80, 0x36, 0xDA, 
0x13, 0x81, 0xC0, 0x60, 0x00, 0xC3, 0x66, 0x6D, 0xCC, 0x1B, 0x36, 0x00, 0x01, 0x07, 0x10, 0x02, 
0x03, 0x00, 0x80, 0x60, 0x00, 0xFB, 0x66, 0x39, 0x8C, 0x0F, 0x1E, 0x00, 0x02, 0x07, 0x0F, 0xFE, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1C, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x2A, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xA2, 0xD5, 0x54, 
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x02, 
0x00, 0xC0, 0x22, 0x00, 0x08, 0x00, 0x02, 0x20, 0x00, 0x82, 0x48, 0x20, 0x00, 0x08, 0x00, 0x00, 
0x40, 0xC0, 0x01, 0xE0, 0x00, 0x01, 0xC0, 0x1E, 0x00, 0x01, 0x50, 0x00, 0xFE, 0x00, 0x0C, 0x02, 
0x00, 0xC0, 0x20, 0x10, 0x08, 0x07, 0xC2, 0x01, 0x00, 0x80, 0x00, 0x21, 0x01, 0x08, 0x0E, 0x00, 
0x4F, 0xFC, 0x00, 0xFE, 0x00, 0x0F, 0x40, 0x3F, 0xF8, 0x03, 0xF8, 0x03, 0x01, 0x80, 0x0B, 0x02, 
0x1C, 0xC2, 0x21, 0x11, 0x08, 0x1C, 0x42, 0x40, 0x04, 0x84, 0x04, 0x21, 0x11, 0x08, 0x69, 0x80, 
0x59, 0xE2, 0x01, 0x11, 0x00, 0x18, 0x40, 0x55, 0x54, 0x05, 0x54, 0x03, 0x39, 0x80, 0x3B, 0x02, 
0x12, 0xD2, 0x21, 0x11, 0x08, 0x10, 0x42, 0x40, 0x04, 0x84, 0x04, 0x21, 0x7D, 0x08, 0x1E, 0x00, 
0x54, 0xCA, 0x01, 0x83, 0x00, 0x10, 0x40, 0x55, 0x54, 0x05, 0x54, 0x03, 0x11, 0x80, 0x3E, 0x02, 
0x12, 0x12, 0x21, 0x01, 0x08, 0x11, 0xC2, 0x40, 0x04, 0x84, 0x04, 0x21, 0x11, 0x08, 0x6B, 0x00, 
0x51, 0xE2, 0x01, 0x01, 0x00, 0x13, 0xC0, 0x47, 0xC4, 0x04, 0x44, 0x01, 0x11, 0x00, 0x09, 0x82, 
0x10, 0x02, 0x21, 0x01, 0x08, 0x71, 0x82, 0x40, 0x04, 0x84, 0x04, 0x23, 0x01, 0x88, 0x0B, 0x00, 
0x4F, 0xFC, 0x01, 0xFF, 0x00, 0xF0, 0x00, 0x3F, 0xF8, 0x05, 0x54, 0x01, 0x01, 0x00, 0x0E, 0x02, 
0x0F, 0xFC, 0x20, 0xFE, 0x08, 0x60, 0x02, 0x1F, 0xF0, 0x84, 0x04, 0x20, 0xFE, 0x08, 0x0C, 0x00, 
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 
}; 

还有一个问题,我认为这是在代码中的一个小错误,我无法检测到它(因为花了几天就可以考虑一下如何让它更小)。也许有人能指出我解决问题的正确方向。

问题

当有配发similarties的,255次以上的重复相同的,比如很多0xFF的重复的线或0×00重复的空的空间,会出现问题。在我的代码中,我采取了一些预防措施,以避免字节溢出,但它失败(现在不知道为什么)。我试图做的是当出现溢出时,记录它并重新开始计数。我不知道这是一个阅读功能或只是写功能的问题。

这是存储

At start address: 
----------------- 
<byte width> 
<byte heigth> 
<uint16 dataSize> 
<data> 
    <if data=0xFF> 
    <0xFF> 
    <repeat count> 
    </if> 
    <if data=0x00> 
    <0x00> 
    <repeat count> 
    </if> 
<else data> 
</data> 

这里的布局是我的代码:

<cut, see answer below> 

任何想法?


注:

我不希望使用任何昂贵的压缩方法,程序空间96%已经在使用我的方法似乎很好地工作,但与一些错误,我需要知道错误,没有其他压缩方法。它已经进行了一些压缩,一个字节中的位代表8个像素,并且只是想略微减少一点(但是在字节溢出时会出现错误)。


编辑18十月2017年

见我的回答及以下新的代码。

+0

不好的hairday家伙? -1解释为什么当你downvote。 – Codebeat

经过一段时间的睡眠,我重做了很多更好的结果和更少的代码,我太过于复杂。

我得到了令人印象深刻的结果,并考虑通过选择最重复的字节并将其记录到EEPROM'文件'中的检查方法来找到最佳'压缩'。

无论如何,这是我的代码,与第一个代码相比要好得多,也许它可以帮助其他人。这是非常轻量级的解决方案来节省一些字节。

例如,分辨率为128x32像素的空白屏幕或全屏幕屏幕仅导致9个字节,一半一半只有17个字节。我在'编译'之前的问题中显示的屏幕只有405字节,节省了大约100个字节。


这里是我重新代码:

uint8_t TOLEDdisplay::getCacheRawBits(uint16_t iAddress) 
{ 
    if(iAddress < cacheSize) 
    { return displayCache[ iAddress ]; } 

    return 0x00; 
} 

bool TOLEDdisplay::setCacheRawBits(uint16_t iAddress, uint8_t iBitByte) 
{ 
    if(iAddress < cacheSize) 
    { 
    displayCache[ iAddress ]=iBitByte; 
    return true; 
    } 

    return false; 
} 

uint16_t TOLEDdisplay::writeToEeprom(uint16_t iAddress) 
{ 
    if(cacheSize == 0 || width == 0 || height == 0) 
    { return 0; } 

    uint8_t iBits;    // Pixel * 8 = byte 
    uint8_t iFoundBits;   // 'Type' of detected 
    uint16_t iByteSize = 0;  // Total byte size of stream 
    uint8_t iCount = 0;  // Count of repeats found 
    bool  bSame;    // Boolean to evaluate repeats 

    // Write screen bounds, when read it back with readFromEeprom, 
    // these bounds need to match with current screen bounds. 
    EEPROM.write(iAddress++, width); 
    EEPROM.write(iAddress++, height); 

    // Reserve two bytes for stream size 
    uint16_t iSizeAddress = iAddress; 
    iAddress+=2; 

    // Write the cache content to the EEPROM 
    uint16_t i = 0; 
    while(i < cacheSize) 
    { 
    // Get a byte with bits 
    iBits = getCacheRawBits(i); 
    ++i; 

    // Find repeating lines or empty lines 
    if(iBits == 0xFF || iBits == 0x00) 
    { 
     iFoundBits = iBits; // Set found bits to detect changes 
     bSame  = true; // Set to true to able to start loop 
     iCount=1;   // Count this found one 

     // Loop to find duplicates 
     while(bSame && (iCount < 0xFF) && (i < cacheSize)) 
     { 
      iBits = getCacheRawBits(i); // Get next byte with bits 
      bSame = (iBits == iFoundBits); // Determine is repeat, the same 
      iCount+=bSame;     // Increment count when same is found 
      i+=bSame; 
     }  

     // Finally write result to EEPROM 
     EEPROM.write(iAddress++, iFoundBits); // type 
     // Write the amount 
     EEPROM.write(iAddress++, iCount); // count 

     // Goto main loop and find next if any 
    } 
    else { 
      // Write found value normally to EEPROM 
      EEPROM.write(iAddress++, iBits); 
     } 
    } 

    // Final EOF address is one pos back 
    --iAddress; 

    // Calculate stream size 
    iByteSize=iAddress-iSizeAddress; 
    uint8_t* pByteSize = (uint8_t*)&iByteSize; 

    // Write size of stream 
    EEPROM.write(iSizeAddress , *pByteSize++); 
    EEPROM.write(iSizeAddress+1, *pByteSize); 

    // return bytes written including width and height bytes (+2 bytes) 
    return iByteSize+2; 
} 

bool TOLEDdisplay::readFromEeprom(uint16_t iAddress) 
{ 
    uint8_t iBits; 
    uint8_t iRepeats; 
    uint16_t i  = 0; 
    uint8_t* pI  = (uint8_t*)&i; 

    uint8_t iWidth = EEPROM.read(iAddress++); 
    uint8_t iHeight = EEPROM.read(iAddress++); 

    // Read stream size, read two bytes 
    *pI = EEPROM.read(iAddress++); 
    *pI++; 
    *pI = EEPROM.read(iAddress++); 

    // Clear the screen 
    clear(); 

    // If an error (no image on EEPROM address) or screen bounds 
    // doesn't match, skip to continue 
    if(i == 0 || iWidth != width || iHeight != height) 
    { 

    update(true); // Set screen to blank 
    return false; 
    } 

    uint16_t iCacheAddress = 0; 

    while(i--) 
    { 
     // Get a byte with bits 
    iBits = EEPROM.read(iAddress++);  

     // Explode repeats if detected 
    if(iBits == 0xFF || iBits == 0x00) 
    { 
     // read amount of repeats 
     iRepeats = EEPROM.read(iAddress++); 

     // Explode it into cache 
     while(iRepeats--) 
     { setCacheRawBits(iCacheAddress++, iBits); } 
    } 
    else { 
      // Put value normally into cache 
      setCacheRawBits(iCacheAddress++, iBits); 
      } 
    } 

    // Done, update the screen 
    update(true); 

    // Return success 
    return true; 
} 

也许我必须添加一些EEPROM boundry检查,但现在它工作正常。

第一次通过循环时,bFFOverflowbZeroOverflow在未被初始化的情况下被访问。

但是,主要的问题是,在记录255 0或0xFF字节后,如果有更多,则将计数设置为1。但是,这应该是零,因为您在计算该字节的第255个副本之后检测到溢出。

因此,始终将bFFOverflowbZeroOverflow设置为0,写出计数。

+0

嗨,感谢您的快速回答。你是不对的,在检查条件之前,布尔变量是初始化的。而且我相信在数数方面你也不对。当发生溢出时,如果它是0x00或0xFF条件中的一个,则仍有一个溢出。然而,看看它tommorow ;-)谢谢 – Codebeat

+0

只是测试它在初始化部分更改布尔值为false,没有任何意义。 – Codebeat