Пример #1
0
        public override byte[] Decode(Bytes.Buffer data, PdfDirectObject parameters, IDictionary <PdfName, PdfDirectObject> header)
        {
            var imageParams      = header;
            var dictHeight       = ((IPdfNumber)(header[PdfName.Height] ?? header[PdfName.H])).IntValue;
            var dictWidth        = ((IPdfNumber)(header[PdfName.Width] ?? header[PdfName.W])).IntValue;
            var bitsPerComponent = ((IPdfNumber)imageParams[PdfName.BitsPerComponent])?.IntValue ?? 8;
            var flag             = imageParams[PdfName.ImageMask] as PdfBoolean;
            var jpegOptions      = new JpegOptions(decodeTransform: null, colorTransform: null);

            // Checking if values need to be transformed before conversion.
            var decodeObj = imageParams[PdfName.Decode] ?? imageParams[PdfName.D];
            var decodeArr = decodeObj?.Resolve() as PdfArray;

            if (false && decodeArr != null)
            {
                var decode          = decodeArr.Select(p => ((IPdfNumber)p).IntValue).ToArray();
                var decodeArrLength = decodeArr.Count;
                var transform       = new int[decodeArr.Count];
                var transformNeeded = false;
                var maxValue        = (1 << bitsPerComponent) - 1;
                for (var i = 0; i < decodeArrLength; i += 2)
                {
                    transform[i]     = ((decode[i + 1] - decode[i]) * 256) | 0;
                    transform[i + 1] = (decode[i] * maxValue) | 0;
                    if (transform[i] != 256 || transform[i + 1] != 0)
                    {
                        transformNeeded = true;
                    }
                }
                if (transformNeeded)
                {
                    jpegOptions.DecodeTransform = transform;
                }
            }
            // Fetching the 'ColorTransform' entry, if it exists.
            if (parameters is PdfDictionary paramDict)
            {
                var colorTransform = paramDict[PdfName.ColorTransform];
                if (colorTransform is IPdfNumber number)
                {
                    jpegOptions.ColorTransform = number.IntValue;
                }
            }
            var jpegImage = new JpegImage(jpegOptions);

            jpegImage.Parse(data.GetBuffer());
            var buffer = jpegImage.GetData(width: dictWidth, height: dictHeight, forceRGB: false, isSourcePDF: true);

            return(buffer);
        }