Beispiel #1
0
        /// <inheritdoc />
        public byte[] Decode(IReadOnlyList <byte> input, DictionaryToken streamDictionary, int filterIndex)
        {
            var parameters = DecodeParameterResolver.GetFilterParameters(streamDictionary, filterIndex);

            var predictor = parameters.GetIntOrDefault(NameToken.Predictor, -1);

            var earlyChange = parameters.GetIntOrDefault(NameToken.EarlyChange, 1);

            if (predictor > 1)
            {
                var decompressed = Decode(input, earlyChange == 1);

                var colors           = Math.Min(parameters.GetIntOrDefault(NameToken.Colors, DefaultColors), 32);
                var bitsPerComponent = parameters.GetIntOrDefault(NameToken.BitsPerComponent, DefaultBitsPerComponent);
                var columns          = parameters.GetIntOrDefault(NameToken.Columns, DefaultColumns);

                var result = PngPredictor.Decode(decompressed, predictor, colors, bitsPerComponent, columns);

                return(result);
            }

            var data = Decode(input, earlyChange == 1);

            return(data);
        }
Beispiel #2
0
        /// <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);
            }
        }
Beispiel #3
0
        /// <inheritdoc />
        public byte[] Decode(IReadOnlyList <byte> input, DictionaryToken streamDictionary, int filterIndex)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            var parameters = DecodeParameterResolver.GetFilterParameters(streamDictionary, filterIndex);

            var predictor = parameters.GetIntOrDefault(NameToken.Predictor, -1);

            var bytes = input.ToArray();

            try
            {
                var decompressed = Decompress(bytes);

                if (predictor == -1)
                {
                    return(decompressed);
                }

                var colors           = Math.Min(parameters.GetIntOrDefault(NameToken.Colors, DefaultColors), 32);
                var bitsPerComponent = parameters.GetIntOrDefault(NameToken.BitsPerComponent, DefaultBitsPerComponent);
                var columns          = parameters.GetIntOrDefault(NameToken.Columns, DefaultColumns);

                var result = PngPredictor.Decode(decompressed, predictor, colors, bitsPerComponent, columns);

                return(result);
            }
            catch
            {
                // ignored.
            }

            return(bytes);
        }