static int GetPixel(byte[] image, int x, int y, int bitDepth, int bytesPerRow)
{
if (bitDepth == 8) {
int pos = bytesPerRow * y + x;
return image[pos] & 0xff;
}
else {
int pos = bytesPerRow * y + x / (8 / bitDepth);
int v = image[pos] >> (8 - bitDepth * (x % (8 / bitDepth))- bitDepth);
return v & ((1 << bitDepth) - 1);
}
}