Current News
Archived News
Search News
Discussion Forum


Old Forum
Install Programs More Downloads...
Troubleshooting
Source Code
Format Specs.
Misc. Information
Non-SF Stuff
Links




ShadowFlare [Sat, 9 Sep 2006 08:40:54 +0000 (08:40 +0000)]
drawgrp/drawgrp.cpp
grpapi/grpapi.cpp

index 9b5e286..f446793 100644 (file)
@@ -3,6 +3,7 @@
 \r
 #define _CRT_RAND_S\r
 #include <stdlib.h>\r
+#include <stdio.h>\r
 #include "stdafx.h"\r
 \r
 \r
@@ -99,28 +100,35 @@ int main(int argc, char* argv[])
        }\r
        hGrp2 = hGrp;\r
        hGrp = CreateGrp(BI.pBuffer, GrpInfo.nFrames, GrpInfo.wMaxWidth, GrpInfo.wMaxHeight, &nGrpSize);\r
+       /*HANDLE hFile;\r
+       hFile = CreateFile("generated ultralisk.grp", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);\r
+       if (hFile != INVALID_HANDLE_VALUE) {\r
+               WriteFile(hFile, hGrp, nGrpSize, &j, 0);\r
+               CloseHandle(hFile);\r
+       }*/\r
        BI.nFrame = 0xFFFF;\r
-       for (i=13;i<14;i+=17) {\r
-               rect.left = rect.top = i;\r
+       j=0;\r
+       for (i=0;i<GrpInfo.nFrames;i+=1) {\r
+               rect.left = rect.top = 0;\r
                rect.right = rect.left + BI.nWidth;\r
                rect.bottom = rect.top + BI.nHeight;\r
                for (x = 0; x < BI.nWidth * BI.nHeight; x++)\r
                        BI.pBuffer[x] = -1;\r
-               for (j=0;j<(32 * 25);j++) {\r
+               //for (j=0;j<(1 * 1);j++) {\r
 //                     DrawGrp(hGrp,(HDC)&BI,i,0,i % (17*8),dwPalette,ALPHA_BLEND,0x401020);\r
-                       DrawGrp(hGrp2,(HDC)&BI,0,0,i % (17*8),0,USE_INDEX,0x401020);\r
-                       u = memcmp(BI.pBuffer, &BI.pBuffer[(i % (17*8)) * BI.nWidth * BI.nHeight], BI.nWidth * BI.nHeight * sizeof(short));\r
-                       DrawGrp(hGrp,(HDC)&BI,0,0,i % (17*8),0,USE_INDEX,0x401020);\r
-                       u = memcmp(BI.pBuffer, &BI.pBuffer[(i % (17*8)) * BI.nWidth * BI.nHeight], BI.nWidth * BI.nHeight * sizeof(short));\r
+                       //DrawGrp(hGrp2,(HDC)&BI,0,0,i,0,USE_INDEX,0x401020);\r
+                       //u = memcmp(BI.pBuffer, &BI.pBuffer[i * BI.nWidth * BI.nHeight], BI.nWidth * BI.nHeight * sizeof(short));\r
+                       DrawGrp(hGrp,(HDC)&BI,0,0,i,0,USE_INDEX,0x401020);\r
+                       u = memcmp(BI.pBuffer, &BI.pBuffer[i * BI.nWidth * BI.nHeight], BI.nWidth * BI.nHeight * sizeof(short));\r
                        for (x = 0; x < BI.nWidth; x++)\r
                                for (y = 0; y < BI.nHeight; y++) {\r
                                        clrPixel = BI.pBuffer[(y * BI.nWidth) + x];\r
-                                       if (clrPixel != -1) SetPixelV(hDC, i + x, i + y, dwPalette[clrPixel]);\r
+                                       if (clrPixel != -1) SetPixelV(hDC, x, y, dwPalette[clrPixel]);\r
                                }\r
-               }\r
+               //}\r
                if (u) {\r
-                       MessageBox(0, "Output of re-encoded graphic does not match original!", 0, 0);\r
-                       break;\r
+                       printf("Output of re-encoded graphic for frame %d does not match original!  Total %d\n", i, ++j);\r
+                       //break;\r
                }\r
                FillRect(hDC, &rect, (HBRUSH) (COLOR_WINDOW+1));\r
        }\r
index 3de6be8..2cb5e2e 100644 (file)
@@ -45,6 +45,7 @@ SETPIXELPROC MySetPixel = (SETPIXELPROC)SetPixelV;
 \r
 void __inline SetPix(HDC hDC, int X, int Y, COLORREF clrColor, DWORD *dwPalette, DWORD dwFlags, DWORD dwAlpha);\r
 void EncodeFrameData(signed short *lpImageData, WORD nFrame, GRPHEADER *lpGrpHeader, FRAMEHEADER *lpFrameHeader, FRAMEDATA *lpFrameData);\r
+BOOL VerifyRow(signed short *lpRawRow, int nWidth, LPBYTE lpEncRow, int nSize);\r
 \r
 extern HINSTANCE hStorm;\r
 \r
@@ -444,31 +445,37 @@ HANDLE GRPAPI WINAPI CreateGrp(signed short *lpImageData, WORD nFrames, WORD wMa
        FRAMEHEADER *lpFrameHeaders;\r
        FRAMEDATA *lpFrameData;\r
        LPBYTE lpGrpData;\r
-       int i, x, y, x1, x2, y1, y2;\r
+       int i, j, x, y, x1, x2, y1, y2;\r
+       DWORD nLastOffset;\r
 \r
        if (!lpImageData || !nGrpSize) return (HANDLE)-1;\r
 \r
        GrpHeader.nFrames = nFrames;\r
        GrpHeader.wMaxWidth = wMaxWidth;\r
        GrpHeader.wMaxHeight = wMaxHeight;\r
-       lpFrameHeaders = (FRAMEHEADER *)malloc((nFrames + 1) * sizeof(FRAMEHEADER));\r
+       lpFrameHeaders = (FRAMEHEADER *)malloc(nFrames * sizeof(FRAMEHEADER));\r
        lpFrameData = (FRAMEDATA *)malloc(nFrames * sizeof(FRAMEDATA));\r
 \r
-       for (i = 0; i <= nFrames; i++) {\r
+       for (i = 0; i < nFrames; i++) {\r
+               // Search for duplicate frames\r
+               for (j = 0; j < i; j++) {\r
+                       if (memcmp(&lpImageData[i * wMaxWidth * wMaxHeight],\r
+                                  &lpImageData[j * wMaxWidth * wMaxHeight],\r
+                                          wMaxWidth * wMaxHeight * sizeof(short)) == 0)\r
+                               break;\r
+               }\r
+\r
+               if (j < i) {\r
+                       memcpy(&lpFrameHeaders[i], &lpFrameHeaders[j], sizeof(FRAMEHEADER));\r
+                       continue;\r
+               }\r
+\r
                if (i == 0) {\r
                        lpFrameHeaders[i].Offset = sizeof(GRPHEADER) + nFrames * sizeof(FRAMEHEADER);\r
                }\r
                else {\r
-                       y = lpFrameHeaders[i-1].Height;\r
-                       if (y > 0) {\r
-                               y--;\r
-                               lpFrameHeaders[i].Offset = lpFrameHeaders[i-1].Offset + lpFrameData[i-1].lpRowOffsets[y] + lpFrameData[i-1].lpRowSizes[y];\r
-                       }\r
-                       else {\r
-                               lpFrameHeaders[i].Offset = lpFrameHeaders[i-1].Offset;\r
-                       }\r
+                       lpFrameHeaders[i].Offset = nLastOffset;\r
                }\r
-               if (i == nFrames) continue;\r
 \r
                // Scan frame to find dimensions of used part\r
                x1 = y1 = 0x10000;\r
@@ -489,37 +496,53 @@ HANDLE GRPAPI WINAPI CreateGrp(signed short *lpImageData, WORD nFrames, WORD wMa
                lpFrameHeaders[i].Height = y2 - y1 + 1;\r
 \r
                EncodeFrameData(lpImageData, i, &GrpHeader, &lpFrameHeaders[i], &lpFrameData[i]);\r
+\r
+               y = lpFrameHeaders[i].Height;\r
+               if (y > 0) {\r
+                       y--;\r
+                       nLastOffset = lpFrameHeaders[i].Offset + lpFrameData[i].lpRowOffsets[y] + lpFrameData[i].lpRowSizes[y];\r
+               }\r
+               else {\r
+                       nLastOffset = lpFrameHeaders[i].Offset;\r
+               }\r
        }\r
 \r
-       lpGrpData = (LPBYTE)malloc(lpFrameHeaders[nFrames].Offset);\r
+       lpGrpData = (LPBYTE)malloc(nLastOffset);\r
 \r
        // Write completed GRP to buffer\r
        memcpy(lpGrpData, &GrpHeader, sizeof(GRPHEADER));\r
        memcpy(lpGrpData + sizeof(GRPHEADER), lpFrameHeaders, nFrames * sizeof(FRAMEHEADER));\r
 \r
        for (i = 0; i < nFrames; i++) {\r
-               memcpy(lpGrpData + lpFrameHeaders[i].Offset, lpFrameData[i].lpRowOffsets, lpFrameHeaders[i].Height * sizeof(WORD));\r
+               if (lpFrameHeaders[i].Offset) {\r
+                       memcpy(lpGrpData + lpFrameHeaders[i].Offset, lpFrameData[i].lpRowOffsets, lpFrameHeaders[i].Height * sizeof(WORD));\r
 \r
-               for (y = 0; y < lpFrameHeaders[i].Height; y++) {\r
-                       memcpy(lpGrpData + lpFrameHeaders[i].Offset + lpFrameData[i].lpRowOffsets[y], lpFrameData[i].lpRowData[y], lpFrameData[i].lpRowSizes[y]);\r
-                       free(lpFrameData[i].lpRowData[y]);\r
-               }\r
+                       for (y = 0; y < lpFrameHeaders[i].Height; y++) {\r
+                               memcpy(lpGrpData + lpFrameHeaders[i].Offset + lpFrameData[i].lpRowOffsets[y], lpFrameData[i].lpRowData[y], lpFrameData[i].lpRowSizes[y]);\r
+                               free(lpFrameData[i].lpRowData[y]);\r
+                       }\r
+\r
+                       free(lpFrameData[i].lpRowOffsets);\r
+                       free(lpFrameData[i].lpRowSizes);\r
+                       free(lpFrameData[i].lpRowData);\r
 \r
-               free(lpFrameData[i].lpRowOffsets);\r
-               free(lpFrameData[i].lpRowSizes);\r
-               free(lpFrameData[i].lpRowData);\r
+                       for (j = i + 1; j < nFrames; j++) {\r
+                               if (lpFrameHeaders[i].Offset == lpFrameHeaders[j].Offset)\r
+                                       lpFrameHeaders[j].Offset = 0;\r
+                       }\r
+               }\r
        }\r
 \r
-       *nGrpSize = lpFrameHeaders[nFrames].Offset;\r
        free(lpFrameHeaders);\r
        free(lpFrameData);\r
 \r
+       *nGrpSize = nLastOffset;\r
        return (HANDLE)lpGrpData;\r
 }\r
 \r
 void EncodeFrameData(signed short *lpImageData, WORD nFrame, GRPHEADER *lpGrpHeader, FRAMEHEADER *lpFrameHeader, FRAMEDATA *lpFrameData)\r
 {\r
-       int x, y, i, nBufPos;\r
+       int x, y, i, nBufPos, nRepeat;\r
        LPBYTE lpRowBuf;\r
 \r
        lpFrameData->lpRowOffsets = (WORD *)malloc(lpFrameHeader->Height * sizeof(WORD));\r
@@ -530,48 +553,65 @@ void EncodeFrameData(signed short *lpImageData, WORD nFrame, GRPHEADER *lpGrpHea
        for (y = 0; y < lpFrameHeader->Height; y++) {\r
                i = nFrame * lpGrpHeader->wMaxWidth * lpGrpHeader->wMaxHeight + (lpFrameHeader->Top + y) * lpGrpHeader->wMaxWidth;\r
                nBufPos = 0;\r
-               if (lpFrameHeader->Width > 1) {\r
-                       for (x = lpFrameHeader->Left; x < lpFrameHeader->Left + lpFrameHeader->Width - 1; x++) {\r
-                               if (lpImageData[i+x] < 0) {\r
-                                       lpRowBuf[nBufPos] = 0x80;\r
-                                       for (; lpImageData[i+x] < 0 && x < lpFrameHeader->Left + lpFrameHeader->Width; x++) {\r
-                                               lpRowBuf[nBufPos]++;\r
+               if (lpFrameHeader->Width > 0) {\r
+                       for (x = lpFrameHeader->Left; x < lpFrameHeader->Left + lpFrameHeader->Width; x++) {\r
+                               if (x < lpFrameHeader->Left + lpFrameHeader->Width - 1) {\r
+                                       if (lpImageData[i+x] < 0) {\r
+                                               lpRowBuf[nBufPos] = 0x80;\r
+                                               for (; lpImageData[i+x] < 0 && x < lpFrameHeader->Left + lpFrameHeader->Width && lpRowBuf[nBufPos] < 0xFF; x++) {\r
+                                                       lpRowBuf[nBufPos]++;\r
+                                               }\r
+                                               x--;\r
+                                               nBufPos++;\r
+                                               continue;\r
                                        }\r
-                                       x--;\r
-                                       nBufPos++;\r
-                               }\r
-                               else if (lpImageData[i+x] == lpImageData[i+x+1]) {\r
-                                       lpRowBuf[nBufPos] = 0x41;\r
-                                       lpRowBuf[nBufPos+1] = (BYTE)lpImageData[i+x];\r
-                                       for (; lpImageData[i+x] == lpImageData[i+x+1] && x < lpFrameHeader->Left + lpFrameHeader->Width - 1; x++) {\r
-                                               lpRowBuf[nBufPos]++;\r
+\r
+                                       // Count repeating pixels, nRepeat = number of pixels - 1, ignore if there are less than 4 duplicates\r
+                                       for (nRepeat = 0; lpImageData[i+x+nRepeat] == lpImageData[i+x+nRepeat+1] && x+nRepeat < lpFrameHeader->Left + lpFrameHeader->Width - 1 && nRepeat < 0x3F; nRepeat++) {}\r
+\r
+                                       if (nRepeat > 2) {\r
+                                               lpRowBuf[nBufPos] = 0x41 + nRepeat;\r
+                                               lpRowBuf[nBufPos+1] = (BYTE)lpImageData[i+x];\r
+                                               x += nRepeat;\r
+                                               nBufPos += 2;\r
+                                       }\r
+                                       else {\r
+                                               lpRowBuf[nBufPos] = 0;\r
+                                               for (; lpImageData[i+x] >= 0 && x < lpFrameHeader->Left + lpFrameHeader->Width && lpRowBuf[nBufPos] < 0x3F; x++) {\r
+                                                       // Count repeating pixels, ignore if there are less than 4 duplicates\r
+                                                       for (nRepeat = 0; lpImageData[i+x+nRepeat] == lpImageData[i+x+nRepeat+1] && x+nRepeat < lpFrameHeader->Left + lpFrameHeader->Width - 1 && nRepeat < 3; nRepeat++) {}\r
+                                                       if (nRepeat > 2) break;\r
+\r
+                                                       lpRowBuf[nBufPos]++;\r
+                                                       lpRowBuf[nBufPos+lpRowBuf[nBufPos]] = (BYTE)lpImageData[i+x];\r
+                                               }\r
+                                               if (lpImageData[i+x] >= 0 && x == lpFrameHeader->Left + lpFrameHeader->Width - 1 && lpRowBuf[nBufPos] < 0x3F) {\r
+                                                       lpRowBuf[nBufPos]++;\r
+                                                       lpRowBuf[nBufPos+lpRowBuf[nBufPos]] = (BYTE)lpImageData[i+x];\r
+                                               }\r
+                                               x--;\r
+                                               nBufPos += 1 + lpRowBuf[nBufPos];\r
                                        }\r
-                                       nBufPos += 2;\r
                                }\r
                                else {\r
-                                       lpRowBuf[nBufPos] = 1;\r
-                                       lpRowBuf[nBufPos+1] = (BYTE)lpImageData[i+x];\r
-                                       x++;\r
-                                       for (; lpImageData[i+x] != lpImageData[i+x+1] && x < lpFrameHeader->Left + lpFrameHeader->Width - 1; x++) {\r
-                                               lpRowBuf[nBufPos]++;\r
-                                               lpRowBuf[nBufPos+lpRowBuf[nBufPos]] = (BYTE)lpImageData[i+x];\r
+                                       if (lpImageData[i+x] < 0) {\r
+                                               lpRowBuf[nBufPos] = 0x81;\r
+                                               nBufPos++;\r
+                                       }\r
+                                       else {\r
+                                               lpRowBuf[nBufPos] = 1;\r
+                                               lpRowBuf[nBufPos+1] = (BYTE)lpImageData[i+x];\r
+                                               nBufPos += 2;\r
                                        }\r
-                                       x--;\r
-                                       nBufPos += 1 + lpRowBuf[nBufPos];\r
                                }\r
                        }\r
                }\r
-               else if (lpFrameHeader->Width == 1){\r
-                       if (lpImageData[i] < 0) {\r
-                               lpRowBuf[nBufPos] = 0x81;\r
-                               nBufPos++;\r
-                       }\r
-                       else {\r
-                               lpRowBuf[nBufPos] = 1;\r
-                               lpRowBuf[nBufPos+1] = (BYTE)lpImageData[i+1];\r
-                               nBufPos += 2;\r
-                       }\r
+\r
+#ifdef _DEBUG\r
+               if (!VerifyRow(&lpImageData[i+lpFrameHeader->Left], lpFrameHeader->Width, lpRowBuf, nBufPos)) {\r
+                       nBufPos = nBufPos;\r
                }\r
+#endif\r
 \r
                if (y == 0) {\r
                        lpFrameData->lpRowOffsets[y] = lpFrameHeader->Height * sizeof(WORD);\r
@@ -587,3 +627,39 @@ void EncodeFrameData(signed short *lpImageData, WORD nFrame, GRPHEADER *lpGrpHea
 \r
        free(lpRowBuf);\r
 }\r
+\r
+#ifdef _DEBUG\r
+BOOL VerifyRow(signed short *lpRawRow, int nWidth, LPBYTE lpEncRow, int nSize)\r
+{\r
+       int i,x=0,ofs=0;\r
+       while (x < nWidth && ofs < nSize) {\r
+               if (!(lpEncRow[ofs] & 0x80)) {\r
+                       if (!(lpEncRow[ofs] & 0x40)) {\r
+                               for (i=1;i<=lpEncRow[ofs] && x<nWidth;i++) {\r
+                                       if (lpEncRow[ofs+i] != (BYTE)lpRawRow[x]) return FALSE;\r
+                                       x++;\r
+                               }\r
+                               ofs+=lpEncRow[ofs]+1;\r
+                       }\r
+                       else {\r
+                               for (i=0;i<lpEncRow[ofs]-64 && x<nWidth;i++) {\r
+                                       if (lpEncRow[ofs+1] != (BYTE)lpRawRow[x]) return FALSE;\r
+                                       x++;\r
+                               }\r
+                               ofs+=2;\r
+                       }\r
+               }\r
+               else {\r
+                       for (i=0;i<lpEncRow[ofs]-128 && x<nWidth;i++) {\r
+                               if (lpRawRow[x] >= 0) return FALSE;\r
+                       }\r
+                       x+=lpEncRow[ofs]-128;\r
+                       ofs++;\r
+               }\r
+       }\r
+\r
+       if (x != nWidth || ofs != nSize) return FALSE;\r
+\r
+       return TRUE;\r
+}\r
+#endif\r