X-Git-Url: https://sfsrealm.hopto.org/projects/gitweb.cgi?a=blobdiff_plain;f=grpapi%2Fgrpapi.cpp;h=7cc25b00af60bda9bbfeba4d14403f030706c5ea;hb=03269fee39ef66bca887d4be10b93ab6995a12a4;hp=b286741e272fa4b8b33f8b5f28733509f3d4b645;hpb=2975ca34c4d19a7ba7003b7909cbccce56efa0a9;p=grpapi.git diff --git a/grpapi/grpapi.cpp b/grpapi/grpapi.cpp index b286741..7cc25b0 100644 --- a/grpapi/grpapi.cpp +++ b/grpapi/grpapi.cpp @@ -34,10 +34,18 @@ typedef struct { DWORD Offset; } FRAMEHEADER; +typedef struct { + WORD *lpRowOffsets; + WORD *lpRowSizes; + LPBYTE *lpRowData; +} FRAMEDATA; + GETPIXELPROC MyGetPixel = GetPixel; SETPIXELPROC MySetPixel = (SETPIXELPROC)SetPixelV; -void __inline SetPix(HDC hDC, int X, int Y, COLORREF clrColor, DWORD dwFlags, DWORD dwAlpha); +void __inline SetPix(HDC hDC, int X, int Y, COLORREF clrColor, DWORD *dwPalette, DWORD dwFlags, DWORD dwAlpha); +void EncodeFrameData(signed short *lpImageData, WORD nFrame, GRPHEADER *lpGrpHeader, FRAMEHEADER *lpFrameHeader, FRAMEDATA *lpFrameData); +BOOL VerifyRow(signed short *lpRawRow, int nWidth, LPBYTE lpEncRow, int nSize); extern HINSTANCE hStorm; @@ -147,7 +155,7 @@ HANDLE GRPAPI WINAPI LoadGrp(LPCSTR lpFileName) SFileCloseFile(hFile); return (HANDLE)-1; } - GrpFile = (char *)VirtualAlloc(0,fsz,MEM_COMMIT,PAGE_READWRITE); + GrpFile = (char *)malloc(fsz); if (GrpFile) { SFileSetFilePointer(hFile,0,0,FILE_BEGIN); SFileReadFile(hFile,GrpFile,fsz,0,0); @@ -163,7 +171,7 @@ HANDLE GRPAPI WINAPI LoadGrp(LPCSTR lpFileName) CloseHandle(hFile); return (HANDLE)-1; } - GrpFile = (char *)VirtualAlloc(0,fsz,MEM_COMMIT,PAGE_READWRITE); + GrpFile = (char *)malloc(fsz); if (GrpFile) { SetFilePointer(hFile,0,0,FILE_BEGIN); ReadFile(hFile,GrpFile,fsz,&tsz,0); @@ -177,13 +185,13 @@ HANDLE GRPAPI WINAPI LoadGrp(LPCSTR lpFileName) BOOL GRPAPI WINAPI DestroyGrp(HANDLE hGrp) { if (!hGrp || hGrp==INVALID_HANDLE_VALUE) return FALSE; - VirtualFree(hGrp,0,MEM_RELEASE); + free(hGrp); return TRUE; } BOOL GRPAPI WINAPI DrawGrp(HANDLE hGrp, HDC hdcDest, int nXDest, int nYDest, WORD nFrame, DWORD *dwPalette, DWORD dwFlags, DWORD dwAlpha) { - if (!hGrp || hGrp==INVALID_HANDLE_VALUE || hdcDest==0 || !dwPalette) return FALSE; + if (!hGrp || hGrp==INVALID_HANDLE_VALUE || hdcDest==0 || (!dwPalette && !(dwFlags&USE_INDEX))) return FALSE; GRPHEADER *GrpFile = (GRPHEADER *)hGrp; nFrame %= GrpFile->nFrames; FRAMEHEADER *GrpFrame = &((FRAMEHEADER *)(((char *)GrpFile)+6))[nFrame]; @@ -226,14 +234,14 @@ BOOL GRPAPI WINAPI DrawGrp(HANDLE hGrp, HDC hdcDest, int nXDest, int nYDest, WOR if (!(RowData[ofs] & 0x80)) { if (!(RowData[ofs] & 0x40)) { for (i=1;i<=RowData[ofs] && xWidth;i++) { - SetPix(hdcDest,nXDest+x,nYDest+y,dwPalette[RowData[ofs+i]],dwFlags,dwAlpha); + SetPix(hdcDest,nXDest+x,nYDest+y,RowData[ofs+i],dwPalette,dwFlags,dwAlpha); x++; } ofs+=RowData[ofs]+1; } else { for (i=0;iWidth;i++) { - SetPix(hdcDest,nXDest+x,nYDest+y,dwPalette[RowData[ofs+1]],dwFlags,dwAlpha); + SetPix(hdcDest,nXDest+x,nYDest+y,RowData[ofs+1],dwPalette,dwFlags,dwAlpha); x++; } ofs+=2; @@ -249,7 +257,7 @@ BOOL GRPAPI WINAPI DrawGrp(HANDLE hGrp, HDC hdcDest, int nXDest, int nYDest, WOR else { for (y=0;yHeight;y++) { for (x=0;xWidth;x++) { - SetPix(hdcDest,nXDest+x,nYDest+y,dwPalette[GrpRaw[y * GrpFrame->Width + x]],dwFlags,dwAlpha); + SetPix(hdcDest,nXDest+x,nYDest+y,GrpRaw[y * GrpFrame->Width + x],dwPalette,dwFlags,dwAlpha); } } } @@ -263,14 +271,14 @@ BOOL GRPAPI WINAPI DrawGrp(HANDLE hGrp, HDC hdcDest, int nXDest, int nYDest, WOR if (!(RowData[ofs] & 0x80)) { if (!(RowData[ofs] & 0x40)) { for (i=1;i<=RowData[ofs] && xWidth;i++) { - SetPix(hdcDest,Right-x,nYDest+y,dwPalette[RowData[ofs+i]],dwFlags,dwAlpha); + SetPix(hdcDest,Right-x,nYDest+y,RowData[ofs+i],dwPalette,dwFlags,dwAlpha); x++; } ofs+=RowData[ofs]+1; } else { for (i=0;iWidth;i++) { - SetPix(hdcDest,Right-x,nYDest+y,dwPalette[RowData[ofs+1]],dwFlags,dwAlpha); + SetPix(hdcDest,Right-x,nYDest+y,RowData[ofs+1],dwPalette,dwFlags,dwAlpha); x++; } ofs+=2; @@ -286,7 +294,7 @@ BOOL GRPAPI WINAPI DrawGrp(HANDLE hGrp, HDC hdcDest, int nXDest, int nYDest, WOR else { for (y=0;yHeight;y++) { for (x=0;xWidth;x++) { - SetPix(hdcDest,Right-x,nYDest+y,dwPalette[GrpRaw[y * GrpFrame->Width + x]],dwFlags,dwAlpha); + SetPix(hdcDest,Right-x,nYDest+y,GrpRaw[y * GrpFrame->Width + x],dwPalette,dwFlags,dwAlpha); } } } @@ -300,14 +308,14 @@ BOOL GRPAPI WINAPI DrawGrp(HANDLE hGrp, HDC hdcDest, int nXDest, int nYDest, WOR if (!(RowData[ofs] & 0x80)) { if (!(RowData[ofs] & 0x40)) { for (i=1;i<=RowData[ofs] && xWidth;i++) { - SetPix(hdcDest,nXDest+x,Bottom-y,dwPalette[RowData[ofs+i]],dwFlags,dwAlpha); + SetPix(hdcDest,nXDest+x,Bottom-y,RowData[ofs+i],dwPalette,dwFlags,dwAlpha); x++; } ofs+=RowData[ofs]+1; } else { for (i=0;iWidth;i++) { - SetPix(hdcDest,nXDest+x,Bottom-y,dwPalette[RowData[ofs+1]],dwFlags,dwAlpha); + SetPix(hdcDest,nXDest+x,Bottom-y,RowData[ofs+1],dwPalette,dwFlags,dwAlpha); x++; } ofs+=2; @@ -323,7 +331,7 @@ BOOL GRPAPI WINAPI DrawGrp(HANDLE hGrp, HDC hdcDest, int nXDest, int nYDest, WOR else { for (y=0;yHeight;y++) { for (x=0;xWidth;x++) { - SetPix(hdcDest,nXDest+x,Bottom-y,dwPalette[GrpRaw[y * GrpFrame->Width + x]],dwFlags,dwAlpha); + SetPix(hdcDest,nXDest+x,Bottom-y,GrpRaw[y * GrpFrame->Width + x],dwPalette,dwFlags,dwAlpha); } } } @@ -337,14 +345,14 @@ BOOL GRPAPI WINAPI DrawGrp(HANDLE hGrp, HDC hdcDest, int nXDest, int nYDest, WOR if (!(RowData[ofs] & 0x80)) { if (!(RowData[ofs] & 0x40)) { for (i=1;i<=RowData[ofs] && xWidth;i++) { - SetPix(hdcDest,Right-x,Bottom-y,dwPalette[RowData[ofs+i]],dwFlags,dwAlpha); + SetPix(hdcDest,Right-x,Bottom-y,RowData[ofs+i],dwPalette,dwFlags,dwAlpha); x++; } ofs+=RowData[ofs]+1; } else { for (i=0;iWidth;i++) { - SetPix(hdcDest,Right-x,Bottom-y,dwPalette[RowData[ofs+1]],dwFlags,dwAlpha); + SetPix(hdcDest,Right-x,Bottom-y,RowData[ofs+1],dwPalette,dwFlags,dwAlpha); x++; } ofs+=2; @@ -360,7 +368,7 @@ BOOL GRPAPI WINAPI DrawGrp(HANDLE hGrp, HDC hdcDest, int nXDest, int nYDest, WOR else { for (y=0;yHeight;y++) { for (x=0;xWidth;x++) { - SetPix(hdcDest,Right-x,Bottom-y,dwPalette[GrpRaw[y * GrpFrame->Width + x]],dwFlags,dwAlpha); + SetPix(hdcDest,Right-x,Bottom-y,GrpRaw[y * GrpFrame->Width + x],dwPalette,dwFlags,dwAlpha); } } } @@ -377,45 +385,323 @@ BOOL GRPAPI WINAPI GetGrpInfo(HANDLE hGrp, GRPHEADER *GrpInfo) void GRPAPI WINAPI SetFunctionGetPixel(GETPIXELPROC lpGetPixelProc) { - MyGetPixel = lpGetPixelProc; + if (!lpGetPixelProc) + MyGetPixel = GetPixel; + else + MyGetPixel = lpGetPixelProc; } void GRPAPI WINAPI SetFunctionSetPixel(SETPIXELPROC lpSetPixelProc) { - MySetPixel = lpSetPixelProc; + if (!lpSetPixelProc) + MySetPixel = (SETPIXELPROC)SetPixelV; + else + MySetPixel = lpSetPixelProc; } -void __inline SetPix(HDC hDC, int X, int Y, COLORREF clrColor, DWORD dwFlags, DWORD dwAlpha) +void __inline SetPix(HDC hDC, int X, int Y, COLORREF clrColor, DWORD *dwPalette, DWORD dwFlags, DWORD dwAlpha) { - if (dwFlags&SHADOW_COLOR) { - clrColor = (dwFlags >> 8) & 0x00FFFFFF; - } - if (dwFlags&ALPHA_BLEND) { - DWORD dwColor = MyGetPixel(hDC,X,Y); - - // Old alpha - /*((BYTE *)&dwColor)[0]*=1-((float)((BYTE *)&dwAlpha)[0]/256); - ((BYTE *)&dwColor)[1]*=1-((float)((BYTE *)&dwAlpha)[1]/256); - ((BYTE *)&dwColor)[2]*=1-((float)((BYTE *)&dwAlpha)[2]/256); - ((BYTE *)&clrColor)[0]*=(float)((BYTE *)&dwAlpha)[0]/256; - ((BYTE *)&clrColor)[1]*=(float)((BYTE *)&dwAlpha)[1]/256; - ((BYTE *)&clrColor)[2]*=(float)((BYTE *)&dwAlpha)[2]/256; - ((BYTE *)&clrColor)[0]+=((BYTE *)&dwColor)[0]; - ((BYTE *)&clrColor)[1]+=((BYTE *)&dwColor)[1]; - ((BYTE *)&clrColor)[2]+=((BYTE *)&dwColor)[2];*/ - - /* blendedcolor = - ( ( forecolor * ( 1 - alpha ) ) >> 8 ) - + ( ( backcolor * ( 256 - alpha ) ) >> 8 ) */ - ((BYTE *)&clrColor)[0] = - ( ( ((BYTE *)&clrColor)[0] * ( ((BYTE *)&dwAlpha)[0] + 1 ) ) >> 8 ) - + ( ( ((BYTE *)&dwColor)[0] * ( 256 - ((BYTE *)&dwAlpha)[0] ) ) >> 8 ); - ((BYTE *)&clrColor)[1] = - ( ( ((BYTE *)&clrColor)[1] * ( ((BYTE *)&dwAlpha)[1] + 1 ) ) >> 8 ) - + ( ( ((BYTE *)&dwColor)[1] * ( 256 - ((BYTE *)&dwAlpha)[1] ) ) >> 8 ); - ((BYTE *)&clrColor)[2] = - ( ( ((BYTE *)&clrColor)[2] * ( ((BYTE *)&dwAlpha)[2] + 1 ) ) >> 8 ) - + ( ( ((BYTE *)&dwColor)[2] * ( 256 - ((BYTE *)&dwAlpha)[2] ) ) >> 8 ); + if (!(dwFlags&USE_INDEX)) { + if (dwFlags&SHADOW_COLOR) { + clrColor = (dwFlags >> 8) & 0x00FFFFFF; + } + else { + clrColor = dwPalette[clrColor]; + } + if (dwFlags&ALPHA_BLEND) { + DWORD dwColor = MyGetPixel(hDC,X,Y); + + // Old alpha + /*((BYTE *)&dwColor)[0]*=1-((float)((BYTE *)&dwAlpha)[0]/256); + ((BYTE *)&dwColor)[1]*=1-((float)((BYTE *)&dwAlpha)[1]/256); + ((BYTE *)&dwColor)[2]*=1-((float)((BYTE *)&dwAlpha)[2]/256); + ((BYTE *)&clrColor)[0]*=(float)((BYTE *)&dwAlpha)[0]/256; + ((BYTE *)&clrColor)[1]*=(float)((BYTE *)&dwAlpha)[1]/256; + ((BYTE *)&clrColor)[2]*=(float)((BYTE *)&dwAlpha)[2]/256; + ((BYTE *)&clrColor)[0]+=((BYTE *)&dwColor)[0]; + ((BYTE *)&clrColor)[1]+=((BYTE *)&dwColor)[1]; + ((BYTE *)&clrColor)[2]+=((BYTE *)&dwColor)[2];*/ + + /* blendedcolor = + ( ( forecolor * ( 1 - alpha ) ) >> 8 ) + + ( ( backcolor * ( 256 - alpha ) ) >> 8 ) */ + ((BYTE *)&clrColor)[0] = + ( ( ((BYTE *)&clrColor)[0] * ( ((BYTE *)&dwAlpha)[0] + 1 ) ) >> 8 ) + + ( ( ((BYTE *)&dwColor)[0] * ( 256 - ((BYTE *)&dwAlpha)[0] ) ) >> 8 ); + ((BYTE *)&clrColor)[1] = + ( ( ((BYTE *)&clrColor)[1] * ( ((BYTE *)&dwAlpha)[1] + 1 ) ) >> 8 ) + + ( ( ((BYTE *)&dwColor)[1] * ( 256 - ((BYTE *)&dwAlpha)[1] ) ) >> 8 ); + ((BYTE *)&clrColor)[2] = + ( ( ((BYTE *)&clrColor)[2] * ( ((BYTE *)&dwAlpha)[2] + 1 ) ) >> 8 ) + + ( ( ((BYTE *)&dwColor)[2] * ( 256 - ((BYTE *)&dwAlpha)[2] ) ) >> 8 ); + } } MySetPixel(hDC,X,Y,clrColor); } + +HANDLE GRPAPI WINAPI CreateGrp(signed short *lpImageData, WORD nFrames, WORD wMaxWidth, WORD wMaxHeight, BOOL bNoCompress, DWORD *nGrpSize) +{ + GRPHEADER GrpHeader; + FRAMEHEADER *lpFrameHeaders; + FRAMEDATA *lpFrameData; + LPBYTE lpGrpData; + int i, j, x, y, x1, x2, y1, y2; + DWORD nLastOffset; + + if (!lpImageData || !nGrpSize) return (HANDLE)-1; + + GrpHeader.nFrames = nFrames; + GrpHeader.wMaxWidth = wMaxWidth; + GrpHeader.wMaxHeight = wMaxHeight; + lpFrameHeaders = (FRAMEHEADER *)malloc(nFrames * sizeof(FRAMEHEADER)); + lpFrameData = (FRAMEDATA *)malloc(nFrames * sizeof(FRAMEDATA)); + + for (i = 0; i < nFrames; i++) { + // Search for duplicate frames + for (j = 0; j < i; j++) { + if (memcmp(&lpImageData[i * wMaxWidth * wMaxHeight], + &lpImageData[j * wMaxWidth * wMaxHeight], + wMaxWidth * wMaxHeight * sizeof(short)) == 0) + break; + } + + if (j < i) { + memcpy(&lpFrameHeaders[i], &lpFrameHeaders[j], sizeof(FRAMEHEADER)); + lpFrameData[i].lpRowOffsets = 0; + continue; + } + + if (i == 0) { + lpFrameHeaders[i].Offset = sizeof(GRPHEADER) + nFrames * sizeof(FRAMEHEADER); + } + else { + lpFrameHeaders[i].Offset = nLastOffset; + } + + // Scan frame to find dimensions of used part + x1 = y1 = 0x10000; + x2 = y2 = -1; + for (y = 0; y < wMaxHeight; y++) { + for (x = 0; x < wMaxWidth; x++) { + if (lpImageData[i * wMaxWidth * wMaxHeight + y * wMaxWidth + x] >= 0) { + if (x < x1) x1 = x; + if (x > x2) x2 = x; + if (y < y1) y1 = y; + if (y > y2) y2 = y; + } + } + } + lpFrameHeaders[i].Left = x1; + lpFrameHeaders[i].Top = y1; + lpFrameHeaders[i].Width = x2 - x1 + 1; + lpFrameHeaders[i].Height = y2 - y1 + 1; + + if (!bNoCompress) { + EncodeFrameData(lpImageData, i, &GrpHeader, &lpFrameHeaders[i], &lpFrameData[i]); + + y = lpFrameHeaders[i].Height; + if (y > 0) { + y--; + nLastOffset = lpFrameHeaders[i].Offset + lpFrameData[i].lpRowOffsets[y] + lpFrameData[i].lpRowSizes[y]; + } + else { + nLastOffset = lpFrameHeaders[i].Offset; + } + } + else { + nLastOffset = lpFrameHeaders[i].Offset + lpFrameHeaders[i].Width * lpFrameHeaders[i].Height; + } + } + + lpGrpData = (LPBYTE)malloc(nLastOffset); + + // Write completed GRP to buffer + memcpy(lpGrpData, &GrpHeader, sizeof(GRPHEADER)); + memcpy(lpGrpData + sizeof(GRPHEADER), lpFrameHeaders, nFrames * sizeof(FRAMEHEADER)); + + for (i = 0; i < nFrames; i++) { + if (lpFrameData[i].lpRowOffsets) { + if (!bNoCompress) { + memcpy(lpGrpData + lpFrameHeaders[i].Offset, lpFrameData[i].lpRowOffsets, lpFrameHeaders[i].Height * sizeof(WORD)); + + for (y = 0; y < lpFrameHeaders[i].Height; y++) { + if (lpFrameData[i].lpRowData[y]) { + memcpy(lpGrpData + lpFrameHeaders[i].Offset + lpFrameData[i].lpRowOffsets[y], lpFrameData[i].lpRowData[y], lpFrameData[i].lpRowSizes[y]); + free(lpFrameData[i].lpRowData[y]); + } + } + + free(lpFrameData[i].lpRowOffsets); + free(lpFrameData[i].lpRowSizes); + free(lpFrameData[i].lpRowData); + } + else { + for (y = 0; y < lpFrameHeaders[i].Height; y++) { + for (x = 0; x < lpFrameHeaders[i].Width; x++) { + lpGrpData[lpFrameHeaders[i].Offset + y * lpFrameHeaders[i].Width + x] = + lpImageData[i * wMaxWidth * wMaxHeight + (lpFrameHeaders[i].Top + y) * wMaxWidth + lpFrameHeaders[i].Left + x]; + } + } + } + } + } + + free(lpFrameHeaders); + free(lpFrameData); + + *nGrpSize = nLastOffset; + return (HANDLE)lpGrpData; +} + +void EncodeFrameData(signed short *lpImageData, WORD nFrame, GRPHEADER *lpGrpHeader, FRAMEHEADER *lpFrameHeader, FRAMEDATA *lpFrameData) +{ + int x, y, i, j, nBufPos, nRepeat; + LPBYTE lpRowBuf; + WORD nLastOffset; + + lpFrameData->lpRowOffsets = (WORD *)malloc(lpFrameHeader->Height * sizeof(WORD)); + lpFrameData->lpRowSizes = (WORD *)malloc(lpFrameHeader->Height * sizeof(WORD)); + lpFrameData->lpRowData = (LPBYTE *)malloc(lpFrameHeader->Height * sizeof(LPBYTE)); + lpRowBuf = (LPBYTE)malloc(lpFrameHeader->Width * 2); + + for (y = 0; y < lpFrameHeader->Height; y++) { + i = nFrame * lpGrpHeader->wMaxWidth * lpGrpHeader->wMaxHeight + (lpFrameHeader->Top + y) * lpGrpHeader->wMaxWidth; + + /* Fails verification when using this. Bug in encoder or decoder? Doesn't provide much benefit. + // Search for duplicate rows (experimental) + for (x = 0; x < y; x++) { + j = nFrame * lpGrpHeader->wMaxWidth * lpGrpHeader->wMaxHeight + (lpFrameHeader->Top + x) * lpGrpHeader->wMaxWidth; + if (memcmp(&lpImageData[i+lpFrameHeader->Left], + &lpImageData[j+lpFrameHeader->Left], + lpGrpHeader->wMaxWidth * sizeof(short)) == 0) + break; + } + + if (x < y) { + lpFrameData->lpRowOffsets[y] = lpFrameData->lpRowOffsets[x]; + lpFrameData->lpRowSizes[y] = 0; + lpFrameData->lpRowData[y] = 0; + +#ifdef _DEBUG + if (!VerifyRow(&lpImageData[i+lpFrameHeader->Left], lpFrameHeader->Width, lpFrameData->lpRowData[x], lpFrameData->lpRowSizes[x])) { + nBufPos = nBufPos; + } +#endif + + continue; + } + */ + + nBufPos = 0; + if (lpFrameHeader->Width > 0) { + for (x = lpFrameHeader->Left; x < lpFrameHeader->Left + lpFrameHeader->Width; x++) { + if (x < lpFrameHeader->Left + lpFrameHeader->Width - 1) { + if (lpImageData[i+x] < 0) { + lpRowBuf[nBufPos] = 0x80; + for (; lpImageData[i+x] < 0 && x < lpFrameHeader->Left + lpFrameHeader->Width && lpRowBuf[nBufPos] < 0xFF; x++) { + lpRowBuf[nBufPos]++; + } + x--; + nBufPos++; + continue; + } + + // Count repeating pixels, nRepeat = number of pixels - 1, ignore if there are less than 4 duplicates + for (nRepeat = 0; lpImageData[i+x+nRepeat] == lpImageData[i+x+nRepeat+1] && x+nRepeat < lpFrameHeader->Left + lpFrameHeader->Width - 1 && nRepeat < 0x3E; nRepeat++) {} + + if (nRepeat > 2) { + lpRowBuf[nBufPos] = 0x41 + nRepeat; + lpRowBuf[nBufPos+1] = (BYTE)lpImageData[i+x]; + x += nRepeat; + nBufPos += 2; + } + else { + lpRowBuf[nBufPos] = 0; + for (; lpImageData[i+x] >= 0 && x < lpFrameHeader->Left + lpFrameHeader->Width && lpRowBuf[nBufPos] < 0x3F; x++) { + // Count repeating pixels, ignore if there are less than 4 duplicates + for (nRepeat = 0; lpImageData[i+x+nRepeat] == lpImageData[i+x+nRepeat+1] && x+nRepeat < lpFrameHeader->Left + lpFrameHeader->Width - 1 && nRepeat < 3; nRepeat++) {} + if (nRepeat > 2) break; + + lpRowBuf[nBufPos]++; + lpRowBuf[nBufPos+lpRowBuf[nBufPos]] = (BYTE)lpImageData[i+x]; + } + if (lpImageData[i+x] >= 0 && x == lpFrameHeader->Left + lpFrameHeader->Width - 1 && lpRowBuf[nBufPos] < 0x3F) { + lpRowBuf[nBufPos]++; + lpRowBuf[nBufPos+lpRowBuf[nBufPos]] = (BYTE)lpImageData[i+x]; + } + x--; + nBufPos += 1 + lpRowBuf[nBufPos]; + } + } + else { + if (lpImageData[i+x] < 0) { + lpRowBuf[nBufPos] = 0x81; + nBufPos++; + } + else { + lpRowBuf[nBufPos] = 1; + lpRowBuf[nBufPos+1] = (BYTE)lpImageData[i+x]; + nBufPos += 2; + } + } + } + } + +#ifdef _DEBUG + if (!VerifyRow(&lpImageData[i+lpFrameHeader->Left], lpFrameHeader->Width, lpRowBuf, nBufPos)) { + nBufPos = nBufPos; + } +#endif + + if (y == 0) { + lpFrameData->lpRowOffsets[y] = lpFrameHeader->Height * sizeof(WORD); + } + else { + lpFrameData->lpRowOffsets[y] = nLastOffset; + } + + nLastOffset = lpFrameData->lpRowOffsets[y] + nBufPos; + + lpFrameData->lpRowSizes[y] = nBufPos; + lpFrameData->lpRowData[y] = (LPBYTE)malloc(nBufPos); + memcpy(lpFrameData->lpRowData[y], lpRowBuf, nBufPos); + } + + free(lpRowBuf); +} + +#ifdef _DEBUG +BOOL VerifyRow(signed short *lpRawRow, int nWidth, LPBYTE lpEncRow, int nSize) +{ + int i,x=0,ofs=0; + while (x < nWidth && ofs < nSize) { + if (!(lpEncRow[ofs] & 0x80)) { + if (!(lpEncRow[ofs] & 0x40)) { + for (i=1;i<=lpEncRow[ofs] && x= 0) return FALSE; + } + x+=lpEncRow[ofs]-128; + ofs++; + } + } + + if (x != nWidth || ofs != nSize) return FALSE; + + return TRUE; +} +#endif