private static void ReadFromDecoderStream(CcittFaxDecoderStream decoderStream, byte[] result) { var pos = 0; int read; while ((read = decoderStream.Read(result, pos, result.Length - pos)) > -1) { pos += read; if (pos >= result.Length) { break; } } decoderStream.Close(); }
/// <inheritdoc /> public byte[] Decode(IReadOnlyList <byte> input, DictionaryToken streamDictionary, int filterIndex) { var decodeParms = DecodeParameterResolver.GetFilterParameters(streamDictionary, filterIndex); var cols = decodeParms.GetIntOrDefault(NameToken.Columns, 1728); var rows = decodeParms.GetIntOrDefault(NameToken.Rows, 0); var height = streamDictionary.GetIntOrDefault(NameToken.Height, NameToken.H, 0); if (rows > 0 && height > 0) { // PDFBOX-771, PDFBOX-3727: rows in DecodeParms sometimes contains an incorrect value rows = height; } else { // at least one of the values has to have a valid value rows = Math.Max(rows, height); } var k = decodeParms.GetIntOrDefault(NameToken.K, 0); var encodedByteAlign = decodeParms.GetBooleanOrDefault(NameToken.EncodedByteAlign, false); var compressionType = DetermineCompressionType(input, k); using (var stream = new CcittFaxDecoderStream(new MemoryStream(input.ToArray()), cols, compressionType, encodedByteAlign)) { var arraySize = (cols + 7) / 8 * rows; var decompressed = new byte[arraySize]; ReadFromDecoderStream(stream, decompressed); // we expect black to be 1, if not invert the bitmap var blackIsOne = decodeParms.GetBooleanOrDefault(NameToken.BlackIs1, false); if (!blackIsOne) { InvertBitmap(decompressed); } return(decompressed); } }