private static byte[] buildFrame(int width, int height, BitStream cellMatrix, BitStream cellDeltaEncoding, BitStream pixelData) { int cellWidth = width / 4 + ((width % 4 != 0) ? 1 : 0); int cellHeight = height / 4 + ((height % 4 != 0) ? 1 : 0); byte[] frame = new byte[width * height]; uint[] matrix = cellMatrix.GetBuffer(); uint[] delta = cellDeltaEncoding.GetBuffer(); uint[] pixels = pixelData.GetBuffer(); byte[] matrix2 = byteArrayFromUintArray(matrix); byte[] delta2 = byteArrayFromUintArray(delta); byte[] pixels2 = byteArrayFromUintArray(pixels); byte[] totaldata = new byte[matrix2.Length + delta2.Length + pixels2.Length]; Array.Copy(matrix2, 0, totaldata, 0, matrix2.Length); Array.Copy(delta2, 0, totaldata, matrix2.Length, delta2.Length); Array.Copy(pixels2, 0, totaldata, matrix2.Length + delta2.Length, pixels2.Length); byte[] compressed = new byte[0x1000000]; int compLength = 0x1000000; UltimaXNA.Network.Compression.Pack(compressed, ref compLength, totaldata, totaldata.Length); int matrixIndex = 0, matrixOffset = 0; int deltaIndex = 0, deltaOffset = 0; int pixelsIndex = 0, pixelsOffset = 0; int cellIndex = 0; bool inEmptyCellSpan = true; bool frameComplete = false; while (!frameComplete) { int matrixCode = valueFromBitStream(matrix, ref matrixIndex, ref matrixOffset, 4); if (inEmptyCellSpan) { cellIndex += matrixCode; inEmptyCellSpan = false; } else { for (int i = 0; i < matrixCode; i++) { int cellX = (cellIndex % cellWidth) * 4; int cellY = (cellIndex / cellWidth) * 4; int cellW = ((width - cellX) >= 4) ? 4 : width - cellX; int cellH = ((height - cellY) >= 4) ? 4 : height - cellY; int deltaCode = valueFromBitStream(delta, ref deltaIndex, ref deltaOffset, cellW * cellH); for (int j = 0; j < cellW * cellH; j++) { if ((deltaCode & deltaBits[j]) != 0) { frame[cellX + (j % cellW) + ((cellY + (j / cellW)) * width)] = (byte)valueFromBitStream(pixels, ref pixelsIndex, ref pixelsOffset, 8); } } cellIndex++; } inEmptyCellSpan = true; } if (cellIndex == cellWidth * cellHeight) frameComplete = true; } return frame; }