Ejemplo n.º 1
0
        public ImportedImage ImportImage(StreamReaderHelper stream, PdfDocument document)
        {
            try
            {
                stream.CurrentOffset = 0;
                int offsetImageData;
                if (TestBitmapFileHeader(stream, out offsetImageData))
                {
                    // Magic: TestBitmapFileHeader updates stream.CurrentOffset on success.

                    ImagePrivateDataBitmap ipd = new ImagePrivateDataBitmap(stream.Data, stream.Length);
                    ImportedImage          ii  = new ImportedImageBitmap(this, ipd, document);
                    if (TestBitmapInfoHeader(stream, ii, offsetImageData))
                    {
                        //stream.CurrentOffset = offsetImageData;
                        return(ii);
                    }
                }
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch (Exception)
            {
            }
            return(null);
        }
Ejemplo n.º 2
0
        internal override ImageData PrepareImageData()
        {
            ImagePrivateDataBitmap data      = (ImagePrivateDataBitmap)Data;
            ImageDataBitmap        imageData = new ImageDataBitmap(_document);

            //imageData.Data = data.Data;
            //imageData.Length = data.Length;

            data.CopyBitmap(imageData);

            return(imageData);
        }
Ejemplo n.º 3
0
        private bool TestBitmapInfoHeader(StreamReaderHelper stream, ImportedImage ii, int offset)
        {
            int size = (int)stream.GetDWord(0, false);

            if (size == 40 || size == 108 || size == 124) // sizeof BITMAPINFOHEADER == 40, sizeof BITMAPV4HEADER == 108, sizeof BITMAPV5HEADER == 124
            {
                uint width           = stream.GetDWord(4, false);
                int  height          = (int)stream.GetDWord(8, false);
                int  planes          = stream.GetWord(12, false);
                int  bitcount        = stream.GetWord(14, false);
                int  compression     = (int)stream.GetDWord(16, false);
                int  sizeImage       = (int)stream.GetDWord(20, false);
                int  xPelsPerMeter   = (int)stream.GetDWord(24, false);
                int  yPelsPerMeter   = (int)stream.GetDWord(28, false);
                uint colorsUsed      = stream.GetDWord(32, false);
                uint colorsImportant = stream.GetDWord(36, false);
                // TODO Integrity and plausibility checks.
                if (sizeImage != 0 && sizeImage + offset > stream.Length)
                {
                    return(false);
                }

                ImagePrivateDataBitmap privateData = (ImagePrivateDataBitmap)ii.Data;

                // Return true only for supported formats.
                if (compression == 0 || compression == 3) // BI_RGB == 0, BI_BITFIELDS == 3
                {
                    ((ImagePrivateDataBitmap)ii.Data).Offset             = offset;
                    ((ImagePrivateDataBitmap)ii.Data).ColorPaletteOffset = stream.CurrentOffset + size;
                    ii.Information.Width         = width;
                    ii.Information.Height        = (uint)Math.Abs(height);
                    ii.Information.HorizontalDPM = xPelsPerMeter;
                    ii.Information.VerticalDPM   = yPelsPerMeter;
                    privateData.FlippedImage     = height < 0;
                    if (planes == 1 && bitcount == 24)
                    {
                        // RGB24
                        ii.Information.ImageFormat = ImageInformation.ImageFormats.RGB24;

                        // TODO: Verify Mask if size >= 108 && compression == 3.
                        return(true);
                    }
                    if (planes == 1 && bitcount == 32)
                    {
                        // ARGB32
                        //ii.Information.ImageFormat = ImageInformation.ImageFormats.ARGB32;
                        ii.Information.ImageFormat = compression == 0 ?
                                                     ImageInformation.ImageFormats.RGB24 :
                                                     ImageInformation.ImageFormats.ARGB32;

                        // TODO: tell RGB from ARGB. Idea: assume RGB if alpha is always 0.

                        // TODO: Verify Mask if size >= 108 && compression == 3.
                        return(true);
                    }
                    if (planes == 1 && bitcount == 8)
                    {
                        // Palette8
                        ii.Information.ImageFormat = ImageInformation.ImageFormats.Palette8;
                        ii.Information.ColorsUsed  = colorsUsed;

                        return(true);
                    }
                    if (planes == 1 && bitcount == 4)
                    {
                        // Palette8
                        ii.Information.ImageFormat = ImageInformation.ImageFormats.Palette4;
                        ii.Information.ColorsUsed  = colorsUsed;

                        return(true);
                    }
                    if (planes == 1 && bitcount == 1)
                    {
                        // Palette8
                        ii.Information.ImageFormat = ImageInformation.ImageFormats.Palette1;
                        ii.Information.ColorsUsed  = colorsUsed;

                        return(true);
                    }
                    // TODO Implement more formats!
                }
            }
            return(false);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ImportedImageBitmap"/> class.
 /// </summary>
 public ImportedImageBitmap(IImageImporter importer, ImagePrivateDataBitmap data, PdfDocument document)
     : base(importer, data, document)
 {
 }