public static Color[,] BufferToPixels(byte[] buffer, double senseHatGamma) { var pixels = new Color[8, 8]; byte[] senseHatInverseGammaTable = GammaCalc.Get5To8BitInvertedGamma(senseHatGamma).ToArray(); int bufferIndex = 0; for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { var r = senseHatInverseGammaTable[buffer[bufferIndex]]; var g = senseHatInverseGammaTable[buffer[bufferIndex + 8]]; var b = senseHatInverseGammaTable[buffer[bufferIndex + 16]]; bufferIndex++; pixels[x, y] = Color.FromArgb(255, r, g, b); } bufferIndex += 16; } return(pixels); }
public static byte[] PixelsToBuffer(Color[,] pixels, double senseHatGamma) { byte[] buffer = new byte[8 * 8 * 3]; // (3 for R,G,B) byte[] senseHatGammaTable = GammaCalc.Get5BitGamma(senseHatGamma).ToArray(); int index = 0; for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { Color color = pixels[x, y]; buffer[index] = senseHatGammaTable[color.R >> 3]; buffer[index + 8] = senseHatGammaTable[color.G >> 3]; buffer[index + 16] = senseHatGammaTable[color.B >> 3]; index++; } index += 16; // Step to the next row. } return(buffer); }
public static Image BufferToImage(byte[] buffer, double senseHatGamma) { var image = new Image(8, 8); byte[] senseHatInverseGammaTable = GammaCalc.Get5To8BitInvertedGamma(senseHatGamma).ToArray(); int bufferIndex = 0; for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { var r = senseHatInverseGammaTable[buffer[bufferIndex]]; var g = senseHatInverseGammaTable[buffer[bufferIndex + 8]]; var b = senseHatInverseGammaTable[buffer[bufferIndex + 16]]; bufferIndex++; image[x, y] = Color.FromArgb(255, r, g, b); } bufferIndex += 16; } return(image); }
public static double Best5BitGammaMatch(byte[] wantedGammaTable, double start, double stop, double step) { var gammaValue = start; double bestMatchGammaValue = 0; int bestMatchGammaFailCount = int.MaxValue; do { byte[] myGamma = GammaCalc.Get5BitGamma(gammaValue).ToArray(); int failCount = 0; for (int i = 0; i < myGamma.Length; i++) { if (myGamma[i] != wantedGammaTable[i]) { failCount++; } } if (failCount < bestMatchGammaFailCount) { bestMatchGammaValue = gammaValue; bestMatchGammaFailCount = failCount; if (failCount == 0) { break; } } gammaValue += step; }while (gammaValue < stop); return(bestMatchGammaValue); }