problemi bitmap monocromatica SetPixel / GetPixel ... Win32 codice C
Domanda
Questo è un po 'del mio codice maschera di bit (bitmap monocromatiche). Non c'è nessun problema con la funzione Bitmask_Create (). Ho provato con l'apertura, caricare e salvare le finestre monocromatico bitmap, e funziona benissimo. Tuttavia, le funzioni e GetPixel SetPixel che ho fatto non sembrano funzionare bene. In alcuni casi sembrano funzionare bene a seconda delle dimensioni bitmap.
Se qualcuno potrebbe aiutare, lo apprezzerei. Mi sta facendo impazzire. Grazie.
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;
}
Soluzione
Credo che la formula per il calcolo del passo è solo un po 'fuori. Funziona quando la larghezza è un multiplo di 8, ma non altrimenti. Prova:
nPitch = ((nWidth + 31) / 8) & ~3;
Altri suggerimenti
ho capito ... Ho avuto due test a ritroso per 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;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow