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 00:40:32 +0000 (00:40 +0000)]
drawgrp/drawgrp.cpp
grpapi/grpapi.cpp
grpapi/grpapi.def
grpapi/grpapi.h
grpapi/grpapi_no-lib.cpp
grpapi/grpapi_no-lib.h

index d342525..9b5e286 100644 (file)
@@ -1,6 +1,8 @@
 // drawgrp.cpp : Defines the entry point for the console application.\r
 //\r
 \r
+#define _CRT_RAND_S\r
+#include <stdlib.h>\r
 #include "stdafx.h"\r
 \r
 \r
@@ -20,17 +22,24 @@ int main(int argc, char* argv[])
 struct BufferInfo {\r
        WORD nWidth;\r
        WORD nHeight;\r
-       LPDWORD pBuffer;\r
+       signed short *pBuffer;\r
+       WORD nFrame;\r
 };\r
 \r
 COLORREF WINAPI ReadPixelFromBuffer(BufferInfo *pBI, int X, int Y)\r
 {\r
-       return pBI->pBuffer[(Y * pBI->nWidth) + X];\r
+       if (pBI->nFrame == 0xFFFF)\r
+               return pBI->pBuffer[(Y * pBI->nWidth) + X];\r
+       else\r
+               return pBI->pBuffer[(pBI->nFrame * pBI->nWidth * pBI->nHeight) + (Y * pBI->nWidth) + X];\r
 }\r
 \r
 void WINAPI WritePixelToBuffer(BufferInfo *pBI, int X, int Y, COLORREF clrColor)\r
 {\r
-       pBI->pBuffer[(Y * pBI->nWidth) + X] = clrColor;\r
+       if (pBI->nFrame == 0xFFFF)\r
+               pBI->pBuffer[(Y * pBI->nWidth) + X] = (signed short)clrColor;\r
+       else\r
+               pBI->pBuffer[(pBI->nFrame * pBI->nWidth * pBI->nHeight) + (Y * pBI->nWidth) + X] = (signed short)clrColor;\r
 }\r
 \r
 int main(int argc, char* argv[])\r
@@ -49,11 +58,9 @@ int main(int argc, char* argv[])
                memcpy(buffer,"Patch_rt.mpq",13);\r
                SFileOpenArchive(buffer,3000,0,&hMPQ3);\r
        }\r
-       SetFunctionGetPixel((GETPIXELPROC)ReadPixelFromBuffer);\r
-       SetFunctionSetPixel((SETPIXELPROC)WritePixelToBuffer);\r
        BufferInfo BI;\r
        LoadPalette("tileset\\Jungle.wpe",dwPalette);\r
-       HANDLE hGrp;\r
+       HANDLE hGrp, hGrp2;\r
        if (argc>1)\r
                hGrp = LoadGrp(argv[1]);\r
        else\r
@@ -63,32 +70,58 @@ int main(int argc, char* argv[])
        if (GetGrpInfo(hGrp,&GrpInfo)==0) {GrpInfo.nFrames=0;GrpInfo.wMaxWidth=0;GrpInfo.wMaxHeight=0;}\r
        BI.nWidth = GrpInfo.wMaxWidth;\r
        BI.nHeight = GrpInfo.wMaxHeight;\r
-       BI.pBuffer = (DWORD *)malloc(BI.nWidth * BI.nHeight * sizeof(DWORD));\r
+       BI.pBuffer = (signed short *)malloc(GrpInfo.nFrames * BI.nWidth * BI.nHeight * sizeof(short));\r
        WORD i,x,y;\r
-       DWORD j;\r
-       for (i = 0; i < BI.nWidth * BI.nHeight; i++)\r
-               BI.pBuffer[i] = 0xFFFFFFFF;\r
+       DWORD j, nGrpSize;\r
+       unsigned int u,v;\r
+       memset(BI.pBuffer, 0xFF, GrpInfo.nFrames * BI.nWidth * BI.nHeight * sizeof(short));\r
        //for (DWORD j=0;j<16;j++){\r
        /*for (WORD i=0;i<GrpInfo.nFrames;i++) {\r
                DrawGrp(hGrp,hDC,0,0,i,dwPalette,ALPHA_BLEND,0x401020);\r
        }*/\r
-       COLORREF clrPixel;\r
+       signed short clrPixel;\r
        RECT rect;\r
-       for (i=13;i<400;i+=17) {\r
+       for (i=0;i<GrpInfo.nFrames;i++) {\r
+               for (j=0;j<8;j++)\r
+               {\r
+                       rand_s(&u);\r
+                       rand_s(&v);\r
+                       u = u % 800;\r
+                       v = v % 600;\r
+                       //DrawGrp(hGrp,hDC,u,v,i,dwPalette,ALPHA_BLEND,0x404040);\r
+               }\r
+       }\r
+       SetFunctionGetPixel((GETPIXELPROC)ReadPixelFromBuffer);\r
+       SetFunctionSetPixel((SETPIXELPROC)WritePixelToBuffer);\r
+       for (i=0;i<GrpInfo.nFrames;i++) {\r
+               BI.nFrame = i;\r
+               DrawGrp(hGrp,(HDC)&BI,0,0,i,0,USE_INDEX,0);\r
+       }\r
+       hGrp2 = hGrp;\r
+       hGrp = CreateGrp(BI.pBuffer, GrpInfo.nFrames, GrpInfo.wMaxWidth, GrpInfo.wMaxHeight, &nGrpSize);\r
+       BI.nFrame = 0xFFFF;\r
+       for (i=13;i<14;i+=17) {\r
                rect.left = rect.top = i;\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] = 0xFFFFFFFF;\r
-               for (j=0;j<32;j++) {\r
+                       BI.pBuffer[x] = -1;\r
+               for (j=0;j<(32 * 25);j++) {\r
 //                     DrawGrp(hGrp,(HDC)&BI,i,0,i % (17*8),dwPalette,ALPHA_BLEND,0x401020);\r
-                       DrawGrp(hGrp,(HDC)&BI,0,0,i % (17*8),dwPalette,USE_INDEX,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
                        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 != 0xFFFFFFFF) SetPixelV(hDC, i + x, i + y, dwPalette[clrPixel]);\r
+                                       if (clrPixel != -1) SetPixelV(hDC, i + x, i + y, dwPalette[clrPixel]);\r
                                }\r
                }\r
+               if (u) {\r
+                       MessageBox(0, "Output of re-encoded graphic does not match original!", 0, 0);\r
+                       break;\r
+               }\r
                FillRect(hDC, &rect, (HBRUSH) (COLOR_WINDOW+1));\r
        }\r
        for (i=4;i<400;i+=17) {\r
@@ -96,14 +129,14 @@ int main(int argc, char* argv[])
                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] = 0xFFFFFFFF;\r
+                       BI.pBuffer[x] = -1;\r
                for (j=0;j<32;j++) {\r
 //                     DrawGrp(hGrp,(HDC)&BI,400-i,0,i % (17*8),dwPalette,HORIZONTAL_FLIP|ALPHA_BLEND,0x401020);\r
-                       DrawGrp(hGrp,(HDC)&BI,0,0,i % (17*8),dwPalette,HORIZONTAL_FLIP|USE_INDEX,0x401020);\r
+                       DrawGrp(hGrp,(HDC)&BI,0,0,i % (17*8),0,HORIZONTAL_FLIP|USE_INDEX,0x401020);\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 != 0xFFFFFFFF) SetPixelV(hDC, 400-i + x, 400-i + y, dwPalette[clrPixel]);\r
+                                       if (clrPixel != -1) SetPixelV(hDC, 400-i + x, 400-i + y, dwPalette[clrPixel]);\r
                                }\r
                }\r
                FillRect(hDC, &rect, (HBRUSH) (COLOR_WINDOW+1));\r
@@ -111,12 +144,13 @@ int main(int argc, char* argv[])
        for (i = 0; i < BI.nWidth; i++)\r
                for (j = 0; j < BI.nHeight; j++) {\r
                        clrPixel = BI.pBuffer[(j * BI.nWidth) + i];\r
-                       if (clrPixel != 0xFFFFFFFF) SetPixelV(hDC, 400 + i, 300 + j, dwPalette[clrPixel]);\r
+                       if (clrPixel != -1) SetPixelV(hDC, 400 + i, 300 + j, dwPalette[clrPixel]);\r
                }\r
        //}\r
        ReleaseDC(0,hDC);\r
        free(BI.pBuffer);\r
     DestroyGrp(hGrp);\r
+    DestroyGrp(hGrp2);\r
        if (SFileCloseArchive!=0) {\r
                if (hMPQ3!=0) SFileCloseArchive(hMPQ3);\r
                if (hMPQ2!=0) SFileCloseArchive(hMPQ2);\r
index 84420e6..3de6be8 100644 (file)
@@ -34,10 +34,17 @@ typedef struct {
        DWORD Offset;\r
 } FRAMEHEADER;\r
 \r
+typedef struct {\r
+       WORD *lpRowOffsets;\r
+       WORD *lpRowSizes;\r
+       LPBYTE *lpRowData;\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
 \r
 extern HINSTANCE hStorm;\r
 \r
@@ -183,7 +190,7 @@ BOOL GRPAPI WINAPI DestroyGrp(HANDLE hGrp)
 \r
 BOOL GRPAPI WINAPI DrawGrp(HANDLE hGrp, HDC hdcDest, int nXDest, int nYDest, WORD nFrame, DWORD *dwPalette, DWORD dwFlags, DWORD dwAlpha)\r
 {\r
-       if (!hGrp || hGrp==INVALID_HANDLE_VALUE || hdcDest==0 || !dwPalette) return FALSE;\r
+       if (!hGrp || hGrp==INVALID_HANDLE_VALUE || hdcDest==0 || (!dwPalette && !(dwFlags&USE_INDEX))) return FALSE;\r
        GRPHEADER *GrpFile = (GRPHEADER *)hGrp;\r
        nFrame %= GrpFile->nFrames;\r
        FRAMEHEADER *GrpFrame = &((FRAMEHEADER *)(((char *)GrpFile)+6))[nFrame];\r
@@ -430,3 +437,153 @@ void __inline SetPix(HDC hDC, int X, int Y, COLORREF clrColor, DWORD *dwPalette,
        }\r
        MySetPixel(hDC,X,Y,clrColor);\r
 }\r
+\r
+HANDLE GRPAPI WINAPI CreateGrp(signed short *lpImageData, WORD nFrames, WORD wMaxWidth, WORD wMaxHeight, DWORD *nGrpSize)\r
+{\r
+       GRPHEADER GrpHeader;\r
+       FRAMEHEADER *lpFrameHeaders;\r
+       FRAMEDATA *lpFrameData;\r
+       LPBYTE lpGrpData;\r
+       int i, x, y, x1, x2, y1, y2;\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
+       lpFrameData = (FRAMEDATA *)malloc(nFrames * sizeof(FRAMEDATA));\r
+\r
+       for (i = 0; i <= nFrames; i++) {\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
+               }\r
+               if (i == nFrames) continue;\r
+\r
+               // Scan frame to find dimensions of used part\r
+               x1 = y1 = 0x10000;\r
+               x2 = y2 = -1;\r
+               for (y = 0; y < wMaxHeight; y++) {\r
+                       for (x = 0; x < wMaxWidth; x++) {\r
+                               if (lpImageData[i * wMaxWidth * wMaxHeight + y * wMaxWidth + x] >= 0) {\r
+                                       if (x < x1) x1 = x;\r
+                                       if (x > x2) x2 = x;\r
+                                       if (y < y1) y1 = y;\r
+                                       if (y > y2) y2 = y;\r
+                               }\r
+                       }\r
+               }\r
+               lpFrameHeaders[i].Left = x1;\r
+               lpFrameHeaders[i].Top = y1;\r
+               lpFrameHeaders[i].Width = x2 - x1 + 1;\r
+               lpFrameHeaders[i].Height = y2 - y1 + 1;\r
+\r
+               EncodeFrameData(lpImageData, i, &GrpHeader, &lpFrameHeaders[i], &lpFrameData[i]);\r
+       }\r
+\r
+       lpGrpData = (LPBYTE)malloc(lpFrameHeaders[nFrames].Offset);\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
+\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
+\r
+       *nGrpSize = lpFrameHeaders[nFrames].Offset;\r
+       free(lpFrameHeaders);\r
+       free(lpFrameData);\r
+\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
+       LPBYTE lpRowBuf;\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
+\r
+       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
+                                       }\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
+                                       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
+                                       }\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
+\r
+               if (y == 0) {\r
+                       lpFrameData->lpRowOffsets[y] = lpFrameHeader->Height * sizeof(WORD);\r
+               }\r
+               else {\r
+                       lpFrameData->lpRowOffsets[y] = lpFrameData->lpRowOffsets[y-1] + lpFrameData->lpRowSizes[y-1];\r
+               }\r
+\r
+               lpFrameData->lpRowSizes[y] = nBufPos;\r
+               lpFrameData->lpRowData[y] = (LPBYTE)malloc(nBufPos);\r
+               memcpy(lpFrameData->lpRowData[y], lpRowBuf, nBufPos);\r
+       }\r
+\r
+       free(lpRowBuf);\r
+}\r
index 131acb6..c791491 100644 (file)
@@ -9,3 +9,4 @@ EXPORTS
        SetFunctionGetPixel @8\r
        SetFunctionSetPixel @9\r
        SetMpqDll           @10\r
+       CreateGrp           @11\r
index 8a61f43..73070c8 100644 (file)
@@ -63,6 +63,8 @@ BOOL   GRPAPI WINAPI DestroyGrp(HANDLE hGrp);
 BOOL   GRPAPI WINAPI DrawGrp(HANDLE hGrp, HDC hdcDest, int nXDest, int nYDest, WORD nFrame, DWORD *dwPalette, DWORD dwFlags, DWORD dwAlpha);\r
 BOOL   GRPAPI WINAPI GetGrpInfo(HANDLE hGrp, GRPHEADER *GrpInfo);\r
 \r
+HANDLE GRPAPI WINAPI CreateGrp(signed short *lpImageData, WORD nFrames, WORD wMaxWidth, WORD wMaxHeight, DWORD *nGrpSize);\r
+\r
 typedef COLORREF (WINAPI* GETPIXELPROC)(\r
   HDC hDC, // same value as hdcDest from DrawGrp,\r
               // does not need to be used as an HDC,\r
index 909d1c5..0b056ce 100644 (file)
@@ -12,6 +12,7 @@ funcLoadGrp LoadGrp = 0;
 funcDestroyGrp DestroyGrp = 0;\r
 funcDrawGrp DrawGrp = 0;\r
 funcGetGrpInfo GetGrpInfo = 0;\r
+funcCreateGrp CreateGrp = 0;\r
 funcSetFunctionGetPixel SetFunctionGetPixel = 0;\r
 funcSetFunctionSetPixel SetFunctionSetPixel = 0;\r
 funcSetMpqDll SetMpqDll = 0;\r
@@ -29,6 +30,7 @@ GRPAPIMODULE::GRPAPIMODULE()
                DestroyGrp = (funcDestroyGrp)GetProcAddress(hGrpApi,"DestroyGrp");\r
                DrawGrp = (funcDrawGrp)GetProcAddress(hGrpApi,"DrawGrp");\r
                GetGrpInfo = (funcGetGrpInfo)GetProcAddress(hGrpApi,"GetGrpInfo");\r
+               CreateGrp = (funcCreateGrp)GetProcAddress(hGrpApi,"CreateGrp");\r
                SetFunctionGetPixel = (funcSetFunctionGetPixel)GetProcAddress(hGrpApi,"SetFunctionGetPixel");\r
                SetFunctionSetPixel = (funcSetFunctionSetPixel)GetProcAddress(hGrpApi,"SetFunctionSetPixel");\r
                SetMpqDll = (funcSetMpqDll)GetProcAddress(hGrpApi,"SetMpqDll");\r
@@ -44,6 +46,7 @@ GRPAPIMODULE::~GRPAPIMODULE()
        DestroyGrp = 0;\r
        DrawGrp = 0;\r
        GetGrpInfo = 0;\r
+       CreateGrp = 0;\r
        SetFunctionGetPixel = 0;\r
        SetFunctionSetPixel = 0;\r
        SetMpqDll = 0;\r
index ac1b3d6..190beb5 100644 (file)
@@ -66,6 +66,9 @@ extern funcDestroyGrp DestroyGrp;
 extern funcDrawGrp DrawGrp;\r
 extern funcGetGrpInfo GetGrpInfo;\r
 \r
+typedef HANDLE (WINAPI* funcCreateGrp)(signed short *lpImageData, WORD nFrames, WORD wMaxWidth, WORD wMaxHeight, DWORD *nGrpSize);\r
+extern funcCreateGrp CreateGrp;\r
+\r
 typedef COLORREF (WINAPI* GETPIXELPROC)(\r
   HDC hDC, // same value as hdcDest from DrawGrp,\r
               // does not need to be used as an HDC,\r