private bool ApplyChunkIHDR(byte[] dataArray) { // verify length if (13 != dataArray.Length) { return(false); } byte[] sizeArray = new byte[4]; Array.Copy(dataArray, 0, sizeArray, 0, 4); width = PNGUtil.GetInt(sizeArray); Array.Copy(dataArray, 4, sizeArray, 0, 4); height = PNGUtil.GetInt(sizeArray); bitDepth = (int)dataArray[8]; colorType = (int)dataArray[9]; compressMethod = (int)dataArray[10]; filterMethod = (int)dataArray[11]; interlaceMethod = (int)dataArray[12]; if (bitDepth != 8) { errorMessage = "can't handle bit depth " + bitDepth; return(false); } if (colorType != 2) { errorMessage = "can't handle color type " + colorType; return(false); } if (compressMethod != 0) { errorMessage = "strange compress method " + compressMethod; return(false); } if (filterMethod != 0) { errorMessage = "strange filter method " + filterMethod; return(false); } if (interlaceMethod < 0 || 1 < interlaceMethod) { errorMessage = "strange interlace method " + interlaceMethod; return(false); } return(true); }
private bool AnalyzeChunk() { bool result = true; int length = 0; byte[] typeArray = new byte[4]; byte[] temp4Byte = new byte[4]; uint crc = 0; // length area Array.Copy(pngData, index, temp4Byte, 0, 4); length = PNGUtil.GetInt(temp4Byte); index += 4; // type area Array.Copy(pngData, index, typeArray, 0, 4); index += 4; // data area byte[] dataArray = new byte[length]; Array.Copy(pngData, index, dataArray, 0, length); index += length; // CRC area Array.Copy(pngData, index, temp4Byte, 0, 4); index += 4; crc = (uint)PNGUtil.GetInt(temp4Byte); // CRC check if (CRCChecker.CRC(typeArray, dataArray) == crc) { result = ApplyChunk(typeArray, dataArray); } else { // CRC invalid errorMessage = "CRC invalid"; result = false; } return(result); }