Monochrome Bitmap SetPixel / GetPixel Probleme ... Win32 C-Code
Frage
Dies ist einige meiner bitmask Code (monochrom Bitmaps). Es gibt kein Problem mit der Bitmask_Create () Funktion. Ich habe es mit Öffnung getestet, Laden und Speichern von Windows-Bitmaps monochrom, und es funktioniert super. die GetPixel und SetPixel Funktionen, die ich gemacht habe scheinen nicht zu Arbeit richtig jedoch. In einigen Fällen scheinen sie zu funktionieren, je nach Bitmap Dimensionen.
Wenn jemand helfen könnte, würde ich es zu schätzen wissen. Es macht mich verrückt. Danke.
typedef struct _GL_BITMASK GL_BITMASK;
struct _GL_BITMASK {
int nWidth; // Width in pixels
int nHeight; // Height in pixels
int nPitch; // Width of scanline in bytes (may have extra padding to align to DWORD)
BYTE *pData; // Pointer to the first byte of the first scanline (top down)
};
int BitMask_GetPixel(GL_BITMASK *pBitMask, int x, int y)
{
INT nElement = ((y * pBitMask->nPitch) + (x / 8));
PBYTE pElement = pBitMask->pData + nElement;
BYTE bMask = 1 << (7 - (x % 8));
return *pElement & bMask;
}
void BitMask_SetPixel(GL_BITMASK *pBitMask, int x, int y, int nPixelColor)
{
INT nElement = x / 8;
INT nScanLineOffset = y * pBitMask->nPitch;
PBYTE pElement = pBitMask->pData + nScanLineOffset + nElement;
BYTE bMask = 1 << (7 - (x % 8));
if(*pElement & bMask)
{
if(!nPixelColor) return;
else *pElement ^= bMask;
}
else
{
if(nPixelColor) return;
else *pElement |= bMask;
}
}
GL_BITMASK *BitMask_Create(INT nWidth, INT nHeight)
{
GL_BITMASK *pBitMask;
int nPitch;
nPitch = ((nWidth / 8) + 3) & ~3;
pBitMask = (GL_BITMASK *)GlobalAlloc(GMEM_FIXED, (nPitch * nHeight) + sizeof(GL_BITMASK));
if(!pBitMask)
return (GL_BITMASK *)NULL;
pBitMask->nPitch = nPitch;
pBitMask->nWidth = nWidth;
pBitMask->nHeight = nHeight;
pBitMask->pData = (PBYTE)pBitMask + sizeof(GL_BITMASK);
return pBitMask;
}
Lösung
Ich denke, Ihre Formel für die Berechnung der Steigung ist nur ein wenig. Es funktioniert, wenn die Breite ein Vielfaches von 8 ist, aber sonst nicht. Versuchen Sie:
nPitch = ((nWidth + 31) / 8) & ~3;
Andere Tipps
ich es herausgefunden ... Ich hatte die beiden Tests nach hinten für nPixelColor in SetPixel ()
if(*pElement & bMask)
{
if(nPixelColor) return; // this was !nPixelColor
else *pElement ^= bMask;
}
else
{
if(!nPixelColor) return; // this was nPixelColor
else *pElement |= bMask;
}