@@ -2042,6 +2042,28 @@ static BYTE* fixL5Light(BYTE* celBuf, size_t* celLen)
20422042}
20432043#endif // HELLFIRE
20442044
2045+ static void pushHead (BYTE** prevHead, BYTE** lastHead, BYTE *head)
2046+ {
2047+ if (*lastHead != NULL && *prevHead != NULL && head != NULL ) {
2048+ // check for [len0 col0 .. coln] [rle3 col] [len1 col00 .. colnn] -> [(len0 + 3 + len1) col0 .. coln col col col col00 .. colnn]
2049+ if (**lastHead == 0xBF - 3 && *head >= 0xBF && **prevHead >= 0xBF ) {
2050+ unsigned len = 3 + (256 - *head) + (256 - **prevHead);
2051+ if (len <= (256 - 0xBF )) {
2052+ **prevHead = 256 - len;
2053+ BYTE col = *((*lastHead) + 1 );
2054+ **lastHead = col;
2055+ *head = col;
2056+ *lastHead = *prevHead;
2057+ *prevHead = NULL ;
2058+ return ;
2059+ }
2060+ }
2061+ }
2062+
2063+ *prevHead = *lastHead;
2064+ *lastHead = head;
2065+ }
2066+
20452067static BYTE* EncodeCl2 (BYTE* pBuf, const BYTE* pSrc, int width, int height, BYTE transparentPixel)
20462068{
20472069 const int RLE_LEN = 3 ; // number of matching colors to switch from bmp encoding to RLE
@@ -2067,8 +2089,14 @@ static BYTE* EncodeCl2(BYTE* pBuf, const BYTE* pSrc, int width, int height, BYTE
20672089 BYTE colMatches = 0 ; // does not matter
20682090 bool alpha = false ;
20692091 bool first = false ; // true; - does not matter
2092+ BYTE* pPrevHead = NULL ;
2093+ BYTE* pLastHead = NULL ;
20702094 for (int i = 1 ; i <= height; i++) {
20712095 if (clipped && (i % CEL_BLOCK_HEIGHT) == 1 /* && (i / CEL_BLOCK_HEIGHT) * 2 < SUB_HEADER_SIZE*/ ) {
2096+ pushHead (&pPrevHead, &pLastHead, pHead);
2097+ // if (first) {
2098+ pLastHead = nullptr ;
2099+ // }
20722100 pHead = pBuf;
20732101 *(WORD*)(&pHeader[(i / CEL_BLOCK_HEIGHT) * 2 ]) = SwapLE16 ((WORD)((size_t )pHead - (size_t )pHeader)); // pHead - buf - SUB_HEADER_SIZE;
20742102
@@ -2089,6 +2117,10 @@ static BYTE* EncodeCl2(BYTE* pBuf, const BYTE* pSrc, int width, int height, BYTE
20892117 if (colMatches < RLE_LEN || *pHead == 0x80u ) {
20902118 // bmp encoding
20912119 if (/* alpha ||*/ *pHead <= 0xBFu || first) {
2120+ pushHead (&pPrevHead, &pLastHead, pHead);
2121+ if (first) {
2122+ pLastHead = NULL ;
2123+ }
20922124 pHead = pBuf;
20932125 pBuf++;
20942126 colMatches = 1 ;
@@ -2101,6 +2133,10 @@ static BYTE* EncodeCl2(BYTE* pBuf, const BYTE* pSrc, int width, int height, BYTE
21012133 memset (pBuf - (RLE_LEN - 1 ), 0 , RLE_LEN - 1 );
21022134 *pHead += RLE_LEN - 1 ;
21032135 if (*pHead != 0 ) {
2136+ pushHead (&pPrevHead, &pLastHead, pHead);
2137+ // if (first) {
2138+ // pLastHead = NULL;
2139+ // }
21042140 pHead = pBuf - (RLE_LEN - 1 );
21052141 }
21062142 *pHead = 0xBFu - (RLE_LEN - 1 );
@@ -2116,6 +2152,10 @@ static BYTE* EncodeCl2(BYTE* pBuf, const BYTE* pSrc, int width, int height, BYTE
21162152 } else {
21172153 // add transparent pixel
21182154 if (!alpha || *pHead == 0x7Fu ) {
2155+ pushHead (&pPrevHead, &pLastHead, pHead);
2156+ // if (first) {
2157+ // pLastHead = NULL;
2158+ // }
21192159 pHead = pBuf;
21202160 pBuf++;
21212161 }
@@ -2126,6 +2166,7 @@ static BYTE* EncodeCl2(BYTE* pBuf, const BYTE* pSrc, int width, int height, BYTE
21262166 }
21272167 pSrc -= BUFFER_WIDTH + width;
21282168 }
2169+ pushHead (&pPrevHead, &pLastHead, pHead);
21292170 return pBuf;
21302171}
21312172
0 commit comments