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 [Tue, 9 Oct 2007 02:27:12 +0000 (02:27 +0000)]
grpapi/grpapi.cpp

index 20d0c06..f35e7f5 100644 (file)
@@ -38,14 +38,15 @@ typedef struct {
        WORD *lpRowOffsets;\r
        WORD *lpRowSizes;\r
        LPBYTE *lpRowData;\r
+       DWORD Size;\r
 } FRAMEDATA;\r
 \r
 GETPIXELPROC MyGetPixel = GetPixel;\r
 SETPIXELPROC MySetPixel = (SETPIXELPROC)SetPixelV;\r
 \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
+void EncodeFrameData(signed short *lpImageData, WORD nFrame, GRPHEADER *lpGrpHeader, FRAMEHEADER *lpFrameHeader, FRAMEDATA *lpFrameData, BOOL bNoCompress);\r
+BOOL VerifyRow(signed short *lpRawRow, int nWidth, LPBYTE lpEncRow, int nSize, BOOL bNoCompress);\r
 \r
 extern HINSTANCE hStorm;\r
 \r
@@ -512,12 +513,7 @@ HANDLE GRPAPI WINAPI CreateGrp(signed short *lpImageData, WORD nFrames, WORD wMa
                        continue;\r
                }\r
 \r
-               if (i == 0) {\r
-                       lpFrameHeaders[i].Offset = sizeof(GRPHEADER) + nFrames * sizeof(FRAMEHEADER);\r
-               }\r
-               else {\r
-                       lpFrameHeaders[i].Offset = nLastOffset;\r
-               }\r
+               lpFrameHeaders[i].Offset = nLastOffset;\r
 \r
                // Scan frame to find dimensions of used part\r
                x1 = y1 = 0x10000;\r
@@ -543,21 +539,8 @@ HANDLE GRPAPI WINAPI CreateGrp(signed short *lpImageData, WORD nFrames, WORD wMa
                lpFrameHeaders[i].Width = x2;\r
                lpFrameHeaders[i].Height = y2;\r
 \r
-               if (!bNoCompress) {\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
-               else {\r
-                       nLastOffset = lpFrameHeaders[i].Offset + lpFrameHeaders[i].Width * lpFrameHeaders[i].Height;\r
-               }\r
+               EncodeFrameData(lpImageData, i, &GrpHeader, &lpFrameHeaders[i], &lpFrameData[i], bNoCompress);\r
+               nLastOffset = lpFrameHeaders[i].Offset + lpFrameData[i].Size;\r
        }\r
 \r
        lpGrpData = (LPBYTE)malloc(nLastOffset);\r
@@ -568,28 +551,19 @@ HANDLE GRPAPI WINAPI CreateGrp(signed short *lpImageData, WORD nFrames, WORD wMa
 \r
        for (i = 0; i < nFrames; i++) {\r
                if (lpFrameData[i].lpRowOffsets) {\r
-                       if (!bNoCompress) {\r
+                       if (!bNoCompress)\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
-                                       if (lpFrameData[i].lpRowData[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
-\r
-                               free(lpFrameData[i].lpRowOffsets);\r
-                               free(lpFrameData[i].lpRowSizes);\r
-                               free(lpFrameData[i].lpRowData);\r
-                       }\r
-                       else {\r
-                               for (y = 0; y < lpFrameHeaders[i].Height; y++) {\r
-                                       for (x = 0; x < lpFrameHeaders[i].Width; x++) {\r
-                                               lpGrpData[lpFrameHeaders[i].Offset + y * lpFrameHeaders[i].Width + x] =\r
-                                                       (BYTE)lpImageData[i * wMaxWidth * wMaxHeight + (lpFrameHeaders[i].Top + y) * wMaxWidth + lpFrameHeaders[i].Left + x];\r
-                                       }\r
+                       for (y = 0; y < lpFrameHeaders[i].Height; y++) {\r
+                               if (lpFrameData[i].lpRowData[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
+\r
+                       free(lpFrameData[i].lpRowOffsets);\r
+                       free(lpFrameData[i].lpRowSizes);\r
+                       free(lpFrameData[i].lpRowData);\r
                }\r
        }\r
 \r
@@ -600,119 +574,122 @@ HANDLE GRPAPI WINAPI CreateGrp(signed short *lpImageData, WORD nFrames, WORD wMa
        return (HANDLE)lpGrpData;\r
 }\r
 \r
-void EncodeFrameData(signed short *lpImageData, WORD nFrame, GRPHEADER *lpGrpHeader, FRAMEHEADER *lpFrameHeader, FRAMEDATA *lpFrameData)\r
+void EncodeFrameData(signed short *lpImageData, WORD nFrame, GRPHEADER *lpGrpHeader, FRAMEHEADER *lpFrameHeader, FRAMEDATA *lpFrameData, BOOL bNoCompress)\r
 {\r
-       int x, y, i, nBufPos, nRepeat;\r
+       int x, y, i, j, nBufPos, nRepeat;\r
        LPBYTE lpRowBuf;\r
-       WORD nLastOffset;\r
+       WORD nLastOffset = 0;\r
 \r
        lpFrameData->lpRowOffsets = (WORD *)malloc(lpFrameHeader->Height * sizeof(WORD));\r
        lpFrameData->lpRowSizes = (WORD *)malloc(lpFrameHeader->Height * sizeof(WORD));\r
        lpFrameData->lpRowData = (LPBYTE *)malloc(lpFrameHeader->Height * sizeof(LPBYTE));\r
        lpRowBuf = (LPBYTE)malloc(lpFrameHeader->Width * 2);\r
-       nLastOffset = lpFrameHeader->Height * sizeof(WORD);\r
+\r
+       if (!bNoCompress)\r
+               nLastOffset = lpFrameHeader->Height * sizeof(WORD);\r
 \r
        for (y = 0; y < lpFrameHeader->Height; y++) {\r
                i = nFrame * lpGrpHeader->wMaxWidth * lpGrpHeader->wMaxHeight + (lpFrameHeader->Top + y) * lpGrpHeader->wMaxWidth;\r
 \r
-               /* Fails verification when using this.  Bug in encoder or decoder?  Doesn't provide much benefit.\r
-               // Search for duplicate rows (experimental)\r
-               for (x = 0; x < y; x++) {\r
-                       j = nFrame * lpGrpHeader->wMaxWidth * lpGrpHeader->wMaxHeight + (lpFrameHeader->Top + x) * lpGrpHeader->wMaxWidth;\r
-                       if (memcmp(&lpImageData[i+lpFrameHeader->Left],\r
-                                  &lpImageData[j+lpFrameHeader->Left],\r
-                                  lpGrpHeader->wMaxWidth * sizeof(short)) == 0)\r
-                               break;\r
-               }\r
+               if (!bNoCompress) {\r
+                       // Search for duplicate rows\r
+                       for (x = 0; x < y; x++) {\r
+                               j = nFrame * lpGrpHeader->wMaxWidth * lpGrpHeader->wMaxHeight + (lpFrameHeader->Top + x) * lpGrpHeader->wMaxWidth;\r
+                               if (memcmp(&lpImageData[i+lpFrameHeader->Left],\r
+                                                  &lpImageData[j+lpFrameHeader->Left],\r
+                                                  lpGrpHeader->wMaxWidth * sizeof(short)) == 0)\r
+                                       break;\r
+                       }\r
 \r
-               if (x < y) {\r
-                       lpFrameData->lpRowOffsets[y] = lpFrameData->lpRowOffsets[x];\r
-                       lpFrameData->lpRowSizes[y] = 0;\r
-                       lpFrameData->lpRowData[y] = 0;\r
+                       if (x < y) {\r
+                               lpFrameData->lpRowOffsets[y] = lpFrameData->lpRowOffsets[x];\r
+                               lpFrameData->lpRowSizes[y] = 0;\r
+                               lpFrameData->lpRowData[y] = 0;\r
 \r
 #ifdef _DEBUG\r
-                       if (!VerifyRow(&lpImageData[i+lpFrameHeader->Left], lpFrameHeader->Width, lpFrameData->lpRowData[x], lpFrameData->lpRowSizes[x])) {\r
-                               nBufPos = nBufPos;\r
-                       }\r
+                               if (!VerifyRow(&lpImageData[i+lpFrameHeader->Left], lpFrameHeader->Width, lpFrameData->lpRowData[x], lpFrameData->lpRowSizes[x], bNoCompress)) {\r
+                                       nBufPos = nBufPos;\r
+                               }\r
 #endif\r
 \r
-                       continue;\r
+                               continue;\r
+                       }\r
                }\r
-               */\r
 \r
                nBufPos = 0;\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
+                               if (!bNoCompress) {\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
+                                                       if (nLastOffset + nBufPos + 1 <= 0xFFFF)\r
+                                                               nBufPos++;\r
+                                                       continue;\r
                                                }\r
-                                               x--;\r
-                                               if (nLastOffset + nBufPos + 1 <= 0xFFFF)\r
-                                                       nBufPos++;\r
-                                               continue;\r
-                                       }\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 < 0x3E; nRepeat++) {}\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 < 0x3E; nRepeat++) {}\r
 \r
-                                       if (nRepeat > 2) {\r
-                                               lpRowBuf[nBufPos] = 0x41 + nRepeat;\r
-                                               lpRowBuf[nBufPos+1] = (BYTE)lpImageData[i+x];\r
-                                               x += nRepeat;\r
-                                               if (nLastOffset + nBufPos + 2 <= 0xFFFF)\r
-                                                       nBufPos += 2;\r
+                                               if (nRepeat > 2) {\r
+                                                       lpRowBuf[nBufPos] = 0x41 + nRepeat;\r
+                                                       lpRowBuf[nBufPos+1] = (BYTE)(lpImageData[i+x]);\r
+                                                       x += nRepeat;\r
+                                                       if (nLastOffset + nBufPos + 2 <= 0xFFFF)\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
+                                                       if (nLastOffset + nBufPos + 1 + lpRowBuf[nBufPos] <= 0xFFFF)\r
+                                                               nBufPos += 1 + lpRowBuf[nBufPos];\r
+                                               }\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
+                                               if (lpImageData[i+x] < 0) {\r
+                                                       lpRowBuf[nBufPos] = 0x81;\r
+                                                       if (nLastOffset + nBufPos + 1 <= 0xFFFF)\r
+                                                               nBufPos++;\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
+                                               else {\r
+                                                       lpRowBuf[nBufPos] = 1;\r
+                                                       lpRowBuf[nBufPos+1] = (BYTE)(lpImageData[i+x]);\r
+                                                       if (nLastOffset + nBufPos + 2 <= 0xFFFF)\r
+                                                               nBufPos += 2;\r
                                                }\r
-                                               x--;\r
-                                               if (nLastOffset + nBufPos + 1 + lpRowBuf[nBufPos] <= 0xFFFF)\r
-                                                       nBufPos += 1 + lpRowBuf[nBufPos];\r
                                        }\r
                                }\r
                                else {\r
-                                       if (lpImageData[i+x] < 0) {\r
-                                               lpRowBuf[nBufPos] = 0x81;\r
-                                               if (nLastOffset + nBufPos + 1 <= 0xFFFF)\r
-                                                       nBufPos++;\r
-                                       }\r
-                                       else {\r
-                                               lpRowBuf[nBufPos] = 1;\r
-                                               lpRowBuf[nBufPos+1] = (BYTE)lpImageData[i+x];\r
-                                               if (nLastOffset + nBufPos + 2 <= 0xFFFF)\r
-                                                       nBufPos += 2;\r
-                                       }\r
+                                       lpRowBuf[nBufPos] = (BYTE)(lpImageData[i+x]);\r
+                                       if (nLastOffset + nBufPos + 1 <= 0xFFFF)\r
+                                               nBufPos++;\r
                                }\r
                        }\r
                }\r
 \r
 #ifdef _DEBUG\r
-               if (!VerifyRow(&lpImageData[i+lpFrameHeader->Left], lpFrameHeader->Width, lpRowBuf, nBufPos)) {\r
+               if (!VerifyRow(&lpImageData[i+lpFrameHeader->Left], lpFrameHeader->Width, lpRowBuf, nBufPos, bNoCompress)) {\r
                        nBufPos = nBufPos;\r
                }\r
 #endif\r
 \r
-               if (y == 0) {\r
-                       lpFrameData->lpRowOffsets[y] = lpFrameHeader->Height * sizeof(WORD);\r
-               }\r
-               else {\r
-                       lpFrameData->lpRowOffsets[y] = nLastOffset;\r
-               }\r
-\r
+               lpFrameData->lpRowOffsets[y] = nLastOffset;\r
                nLastOffset = lpFrameData->lpRowOffsets[y] + nBufPos;\r
 \r
                lpFrameData->lpRowSizes[y] = nBufPos;\r
@@ -720,35 +697,44 @@ void EncodeFrameData(signed short *lpImageData, WORD nFrame, GRPHEADER *lpGrpHea
                memcpy(lpFrameData->lpRowData[y], lpRowBuf, nBufPos);\r
        }\r
 \r
+       lpFrameData->Size = nLastOffset;\r
+\r
        free(lpRowBuf);\r
 }\r
 \r
 #ifdef _DEBUG\r
-BOOL VerifyRow(signed short *lpRawRow, int nWidth, LPBYTE lpEncRow, int nSize)\r
+BOOL VerifyRow(signed short *lpRawRow, int nWidth, LPBYTE lpEncRow, int nSize, BOOL bNoCompress)\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
+               if (!bNoCompress) {\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
-                               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
+                               for (i=0;i<lpEncRow[ofs]-128 && x<nWidth;i++) {\r
+                                       if (lpRawRow[x] >= 0) return FALSE;\r
                                }\r
-                               ofs+=2;\r
+                               x+=lpEncRow[ofs]-128;\r
+                               ofs++;\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
+                       if (lpEncRow[ofs] != (BYTE)lpRawRow[x]) return FALSE;\r
+                       x++;\r
                        ofs++;\r
                }\r
        }\r