--- mstools/samples/mandel/dibmp.c 2018/08/09 18:20:41 1.1 +++ mstools/samples/mandel/dibmp.c 2018/08/09 18:23:42 1.1.1.3 @@ -23,7 +23,7 @@ #include "jtypes.h" #include "dibmp.h" -//#define CYCLETHRD +#define CYCLETHRD //#define DEBUG @@ -32,7 +32,7 @@ #define OutputDebugString(LPCSTR) #endif - +extern HPALETTE ghPal, ghPalOld; extern HWND ghwndMain; extern HWND ghwndClient; extern HANDLE hQuitEvent; @@ -46,11 +46,11 @@ BOOL bColorReplace(PBITMAPINFO); BOOL bColorCycle(HDC, RECT, PBYTE, PBITMAPINFO, PINFO); BOOL bCycle(HWND); BOOL bColor2Mono(HDC, PINFO, PBITMAPINFO); -INT iCreatePenFrPal(HDC, PVOID *); +INT iCreatePenFrPal(HDC, PVOID *, INT, HPALETTE *); +HPALETTE CopyPalette(HPALETTE); extern PINFO pGetInfoData(HWND); extern BOOL bReleaseInfoData(HWND); - /******************************Public*Routine******************************\ * * bChangeDIBColor @@ -92,6 +92,19 @@ BOOL bChangeDIBColor(HDC hDC, PINFO pInf return FALSE; } + if (pInfo->hPal) { + HPALETTE hPalOld; + int ii; + + hPalOld = SelectPalette(hDC, pInfo->hPal, FALSE); + ii = RealizePalette(hDC); + + if (ii){ + UpdateColors (hDC); + } + + } + // // Let the graphics engine to retrieve the dimension of the bitmap for us // GetDIBits uses the size to determine if it's BITMAPCOREINFO or BITMAPINFO @@ -175,6 +188,7 @@ BOOL bChangeDIBColor(HDC hDC, PINFO pInf pInfo->bUseMono = TRUE; break; case MM_SHIFT: + if (!bColorShift(pbmi)) { MessageBox(ghwndMain, "Failed in ColorShift!", "Error", MB_OK); bSuccess = FALSE; @@ -204,23 +218,28 @@ BOOL bChangeDIBColor(HDC hDC, PINFO pInf if (bCycle) pInfo->bSetDIBsToDevice = TRUE; +#if 0 if (pInfo->bSetDIBsToDevice && !pInfo->bUseMono) { //GetWindowRect(pInfo->hwnd, &rc); GetClientRect(pInfo->hwnd, &rc); //SetMapMode(hDC, MM_TEXT); - if (SetDIBitsToDevice(hDC, 0, 0, rc.right-rc.left, rc.bottom-rc.top, - 0, 0, 0, pbmi->bmiHeader.biHeight, (LPSTR)pBits, pbmi, DIB_RGB_COLORS) == 0) { - MessageBox(ghwndMain, "Failed in SetDIBitsToDevice!", "Error", MB_OK); - bSuccess = FALSE; - goto ErrExit4; - } + //if (SetDIBitsToDevice(hDC, 0, 0, rc.right-rc.left, rc.bottom-rc.top, + // 0, 0, 0, pbmi->bmiHeader.biHeight, (LPSTR)pBits, pbmi, DIB_RGB_COLORS) == 0) { + // MessageBox(ghwndMain, "Failed in SetDIBitsToDevice!", "Error", MB_OK); + // bSuccess = FALSE; + // goto ErrExit4; + //} } else { +#endif InvalidateRect(pInfo->hwnd, NULL, FALSE); - - } +// } if (bCycle) { + // + // This is commented out because I don't want the system colors + // be affected by color cycling + // //SetSystemPaletteUse(hDC, SYSPAL_NOSTATIC); bColorCycle(hDC, rc, pBits, pbmi, pInfo); //SetSystemPaletteUse(hDC, SYSPAL_STATIC); @@ -365,23 +384,26 @@ BOOL bColorReplace(PBITMAPINFO pbmi) BOOL bColorCycle(HDC hDC, RECT rc, PBYTE pBits, PBITMAPINFO pbmi, PINFO pInfo) { - INT iEntry, i; + INT iEntry, i, iNumClr; PLOGPALETTE plogPat; ULONG ulSize; BOOL bSuccess; HPALETTE hPal, hPalOld; PALETTEENTRY peTemp; - BYTE red, green, blue; + #ifdef CYCLETHRD DWORD dwWait; -#endif +#else MSG msg; +#endif BOOL bQuit; -UINT uRC; +//UINT uRC; bSuccess = TRUE; iEntry = 1<bmiHeader.biBitCount; + + iNumClr = 236; // 256 minus 20 reserved colors ulSize = sizeof(LOGPALETTE)+sizeof(PALETTEENTRY)*256; if ((plogPat = (PLOGPALETTE) GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, ulSize)) == NULL) { @@ -392,23 +414,34 @@ UINT uRC; plogPat->palVersion = 0x300; plogPat->palNumEntries = (WORD) 256; -#if 0 - for (i = 0; i < iEntry; i++) { - plogPat->palPalEntry[i].peBlue = pbmi->bmiColors[i].rgbBlue; - plogPat->palPalEntry[i].peGreen = pbmi->bmiColors[i].rgbGreen; - plogPat->palPalEntry[i].peRed = pbmi->bmiColors[i].rgbRed; - plogPat->palPalEntry[i].peFlags = PC_RESERVED; - } -#endif - for (i = 0; i < 256; i++) { - plogPat->palPalEntry[i].peBlue = blue; - plogPat->palPalEntry[i].peGreen = green; - plogPat->palPalEntry[i].peRed = red; - plogPat->palPalEntry[i].peFlags = PC_RESERVED; - - if (!(red += 32)) - if (!(green += 32)) - blue += 64; + + { + int iFirst, iSecond, iInc; + + iFirst = iNumClr/3; + iSecond = iFirst * 2; + iInc = (iNumClr <= 256) ? 3 : 1; + + for (i = 0; i < iNumClr; i++) { + if (i < iFirst) { + plogPat->palPalEntry[i+10].peBlue = 255-(i+1)*iInc; + plogPat->palPalEntry[i+10].peGreen = 0; + plogPat->palPalEntry[i+10].peRed = (i+1)*iInc; + plogPat->palPalEntry[i+10].peFlags = PC_RESERVED; + continue; + } + if (i < iSecond) { + plogPat->palPalEntry[i+10].peBlue = 0; + plogPat->palPalEntry[i+10].peGreen = (i+1-iFirst)*iInc; + plogPat->palPalEntry[i+10].peRed = 255-(i+1-iFirst)*iInc; + plogPat->palPalEntry[i+10].peFlags = PC_RESERVED; + continue; + } + plogPat->palPalEntry[i+10].peBlue = (i+1-iSecond)*iInc; + plogPat->palPalEntry[i+10].peGreen = 255-(i+1-iSecond)*iInc; + plogPat->palPalEntry[i+10].peRed = 0; + plogPat->palPalEntry[i+10].peFlags = PC_RESERVED; + } } if ((hPal = CreatePalette(plogPat)) == (HPALETTE) NULL) { @@ -417,9 +450,12 @@ UINT uRC; goto ErrExit2; } + pInfo->hCyclePal = hPal; + hPalOld = SelectPalette(hDC, hPal, FALSE); RealizePalette(hDC); + #ifndef CYCLETHRD bQuit = FALSE; @@ -466,34 +502,36 @@ UINT uRC; sprintf( gtext,"About to animate palette\n"); OutputDebugString( gtext ); - - peTemp = plogPat->palPalEntry[0]; - for (i = 0; i < 255; i++) { - plogPat->palPalEntry[i] = plogPat->palPalEntry[i+1]; + peTemp = plogPat->palPalEntry[10]; + for (i = 0; i < iNumClr; i++) { + plogPat->palPalEntry[i+10] = plogPat->palPalEntry[i+10+1]; } - plogPat->palPalEntry[i] = peTemp; + plogPat->palPalEntry[i-1+10] = peTemp; if (!AnimatePalette(hPal, 0, 256, plogPat->palPalEntry)) { sprintf( gtext,"Anmiate palette failed\n"); OutputDebugString( gtext ); } + Sleep(50); - if ((uRC = RealizePalette(hDC)) == -1) { +// +// This is not needed anymore starting from build 475 14-Jun-1993 +// +#if 0 + // + // Not realizing stops color-cycling after moving back into + // foreground + // + if ((uRC = RealizePalette(hDC)) == GDI_ERROR) { sprintf( gtext,"Realize palette failed\n"); OutputDebugString( gtext ); } - sprintf( gtext,"Realize palette = %d\n", uRC); - OutputDebugString( gtext ); if (!UpdateColors(hDC)) { sprintf( gtext,"Update Colors failed\n"); OutputDebugString( gtext ); } - - if (SetDIBitsToDevice(hDC, 0, 0, rc.right-rc.left, rc.bottom-rc.top, - 0, 0, 0, pbmi->bmiHeader.biHeight, (LPSTR)pBits, pbmi, DIB_PAL_COLORS) == 0) { - MessageBox(ghwndMain, "Failed in SetDIBitsToDevice!", "Error", MB_OK); - } +#endif } } @@ -579,6 +617,12 @@ BOOL bColor2Mono(HDC hDC, PINFO pInfo, P hDCMemDest = CreateCompatibleDC(hDC); hDCMemSrc = CreateCompatibleDC(hDC); + + if (pInfo->hPal) { + SelectPalette(hDCMemSrc, pInfo->hPal, FALSE); + RealizePalette(hDCMemSrc); + } + iEntry = 1<bmiHeader.biBitCount; SetBkColor(hDCMemSrc, RGB( pbmi->bmiColors[iCount].rgbRed, pbmi->bmiColors[iCount].rgbGreen, @@ -594,7 +638,7 @@ BOOL bColor2Mono(HDC hDC, PINFO pInfo, P hOldBmpSrc = SelectObject(hDCMemSrc, pInfo->hBmpSaved); BitBlt(hDCMemDest, 0, 0, iWidth, iHeight, hDCMemSrc, 0, 0, SRCCOPY); - SelectObject(hDCMemDest, hOldBmpDest); + pInfo->hBmpMono = SelectObject(hDCMemDest, hOldBmpDest); SelectObject(hDCMemSrc, hOldBmpSrc); DeleteDC(hDCMemDest); @@ -628,75 +672,230 @@ BOOL bColor2Mono(HDC hDC, PINFO pInfo, P * 13-Jun-1992 -by- Petrus Wong * Wrote it. \**************************************************************************/ - -INT iCreatePenFrPal(HDC hDC, PVOID *prghPen) +INT iCreatePenFrPal(HDC hDC, PVOID *prghPen, INT iWidth, HPALETTE *phPal) { - INT iNumClr, iResult, i; + INT iNumClr, iResult, i, iEntry; PLOGPALETTE plogPat; ULONG ulSize; - HPALETTE hPal, hPalOld; - BYTE red, green, blue; iResult = 0; if (!((GetDeviceCaps(hDC, RASTERCAPS)) & RC_PALETTE)) { - iNumClr = GetSystemPaletteEntries(hDC, 0, 0, NULL); + if (iNumClr = GetSystemPaletteEntries(hDC, 0, 0, NULL)) { + if (prghPen == NULL) { + return (iNumClr); + } + + if (iWidth < 0) { + MessageBox(GetFocus(), "Invalid pen width!", "Error", MB_OK); + return 0L; + } + + for (i = 0; i < iNumClr; i++) { + prghPen[i] = + (PVOID) CreatePen(PS_SOLID, iWidth, PALETTEINDEX(i)); + iResult = i; +#if 0 + SelectObject(hDC, (HPEN)prghPen[i]); + MoveToEx(hDC, i*iWidth, 0, NULL); + LineTo(hDC, i*iWidth, 300); +#endif + } + return iResult; + } + + // GetPaletteEntries returns 0 + iNumClr = 1 << GetDeviceCaps(hDC, BITSPIXEL); + + if (iNumClr <= 16) { + sprintf( gtext,"Assertion failed: iCreatePenFrPal() \n"); + OutputDebugString( gtext); + sprintf( gtext,"iNumClr <= 16! iNumClr = %d\n", iNumClr); + OutputDebugString( gtext); + return (iNumClr); + } + + // Assuming at least 16 bit device + // + iNumClr = 768; // COLORREF format: 0x00bbggrr r,g,b=[0,255] + if (prghPen == NULL) { return (iNumClr); } + if (iWidth < 0) { + MessageBox(GetFocus(), "Invalid pen width!", "Error", MB_OK); + return 0L; + } + for (i = 0; i < iNumClr; i++) { - prghPen[i] = (PVOID) CreatePen(PS_SOLID, 0, PALETTEINDEX(i)); + if (i < 256) { + prghPen[i] = + (PVOID) CreatePen(PS_SOLID, iWidth, RGB(i,0,255-i)); + iResult = i; +#if 0 + SelectObject(hDC, (HPEN)prghPen[i]); + MoveToEx(hDC, i*iWidth, 0, NULL); + LineTo(hDC, i*iWidth, 300); +#endif + continue; + } + + if (i < 512) { + prghPen[i] = + (PVOID) CreatePen(PS_SOLID, iWidth, RGB(511-i,i-256,0)); + iResult = i; +#if 0 + SelectObject(hDC, (HPEN)prghPen[i]); + MoveToEx(hDC, i*iWidth, 0, NULL); + LineTo(hDC, i*iWidth, 300); +#endif + continue; + } + + + prghPen[i] = + (PVOID) CreatePen(PS_SOLID, iWidth, RGB(0,767-i,i-512)); iResult = i; +#if 0 + SelectObject(hDC, (HPEN)prghPen[i]); + MoveToEx(hDC, i*iWidth, 0, NULL); + LineTo(hDC, i*iWidth, 300); +#endif } return iResult; } - iNumClr = GetDeviceCaps(hDC, NUMCOLORS); + // + // Palette managed device + // + iEntry = iNumClr = 1 << GetDeviceCaps(hDC, BITSPIXEL); - if (prghPen == NULL) - return iNumClr; + if (iNumClr > 256) { + sprintf( gtext,"Assertion failed: iCreatePenFrPal() \n"); + OutputDebugString( gtext); + sprintf( gtext,"iNumClr > 256! iNumClr = %d\n", iNumClr); + OutputDebugString( gtext); + iEntry=iNumClr = 256; + } ulSize = sizeof(LOGPALETTE)+sizeof(PALETTEENTRY)*iNumClr; + iNumClr -= GetDeviceCaps(hDC, NUMRESERVED); + + if (prghPen == NULL) + return iNumClr; + if ((plogPat = (PLOGPALETTE) GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, ulSize)) == NULL) { - MessageBox(ghwndMain, "Failed in Memory Allocation for plogPat!", "Error", MB_OK); + MessageBox(GetFocus(), "Failed in Memory Allocation for plogPat!", "Error", MB_OK); goto ErrExit1; } plogPat->palVersion = 0x300; - plogPat->palNumEntries = (WORD) iNumClr; - for (i = 0; i < iNumClr; i++) { - plogPat->palPalEntry[i].peBlue = blue; - plogPat->palPalEntry[i].peGreen = green; - plogPat->palPalEntry[i].peRed = red; - plogPat->palPalEntry[i].peFlags = PC_RESERVED; - - if (!(red += 32)) - if (!(green += 32)) - blue += 64; + plogPat->palNumEntries = (WORD) iEntry; + + { + int iFirst, iSecond, iInc; + + iFirst = iNumClr/3; + iSecond = iFirst * 2; + iInc = (iNumClr <= 256) ? 3 : 1; + + for (i = 0; i < iNumClr; i++) { + if (i < iFirst) { + plogPat->palPalEntry[i+10].peBlue = 255-(i+1)*iInc; + plogPat->palPalEntry[i+10].peGreen = 0; + plogPat->palPalEntry[i+10].peRed = (i+1)*iInc; + plogPat->palPalEntry[i+10].peFlags = PC_RESERVED; + continue; + } + if (i < iSecond) { + plogPat->palPalEntry[i+10].peBlue = 0; + plogPat->palPalEntry[i+10].peGreen = (i+1-iFirst)*iInc; + plogPat->palPalEntry[i+10].peRed = 255-(i+1-iFirst)*iInc; + plogPat->palPalEntry[i+10].peFlags = PC_RESERVED; + continue; + } + plogPat->palPalEntry[i+10].peBlue = (i+1-iSecond)*iInc; + plogPat->palPalEntry[i+10].peGreen = 255-(i+1-iSecond)*iInc; + plogPat->palPalEntry[i+10].peRed = 0; + plogPat->palPalEntry[i+10].peFlags = PC_RESERVED; + } } - if ((hPal = CreatePalette(plogPat)) == (HPALETTE) NULL) { - MessageBox(ghwndMain, "Failed in creating palette!", "Error", MB_OK); + DeleteObject(*phPal); + + if ((*phPal = CreatePalette(plogPat)) == (HPALETTE) NULL) { + MessageBox(GetFocus(), "Failed in creating palette!", "Error", MB_OK); goto ErrExit2; } - - hPalOld = SelectPalette(hDC, hPal, FALSE); - RealizePalette(hDC); + if (*phPal) { + SelectPalette(hDC, *phPal, FALSE); + RealizePalette(hDC); + } for (i = 0; i < iNumClr; i++) { - prghPen[i] = (PVOID) CreatePen(PS_SOLID, 0, PALETTEINDEX(i)); + HPEN hPen; + + hPen = CreatePen(PS_SOLID, iWidth, PALETTEINDEX(i+10)); + prghPen[i] = (PVOID) hPen; iResult = i; +#if 0 + SelectObject(hDC, hPen); + MoveToEx(hDC, i*iWidth, 0, NULL); + LineTo(hDC, i*iWidth, 300); +#endif } - SelectPalette(hDC, hPalOld, 0); - DeleteObject(hPal); ErrExit2: GlobalFree(plogPat); ErrExit1: return iResult; } + + +/******************************Public*Routine******************************\ +* +* HPALETTE CopyPalette +* +* Effects: +* +* Warnings: +* +* History: +* 18-Sep-1992 -by- Petrus Wong +* Wrote it. +\**************************************************************************/ + +HPALETTE CopyPalette(HPALETTE hPalSrc) +{ + PLOGPALETTE plogPal; + PBYTE pjTmp; + int iNumEntries=0; + HPALETTE hPal; + + if ((iNumEntries = GetPaletteEntries(hPalSrc, 0, iNumEntries, NULL)) == 0) { + MessageBox(ghwndMain, "No entry in palette to copy!", "Error", MB_OK); + return (HPALETTE) NULL; + } + + if ((plogPal = (PLOGPALETTE)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, + sizeof(DWORD) + sizeof(PALETTEENTRY)*iNumEntries )) == NULL) { + MessageBox(ghwndMain, "Failed in CopyPalette!", "Error", MB_OK); + return (HPALETTE) NULL; + } + + plogPal->palVersion = 0x300; + plogPal->palNumEntries = (WORD) iNumEntries; + pjTmp = (PBYTE) plogPal; + pjTmp += 8; + GetPaletteEntries(hPalSrc, 0, iNumEntries, (PPALETTEENTRY)pjTmp); + hPal = CreatePalette(plogPal); + + GlobalFree(plogPal); + + return hPal; +}