Esempio n. 1
0
        public RasterImage GetPage(string key, int page)
        {
            if (_images.ContainsKey(key))
            {
                var image = _images[key];

                if (image != null)
                {
                    image.Page = page;
                    return(image.Clone());
                }

                return(null);
            }

            if (_imagePaths.ContainsKey(key))
            {
                if (File.Exists(_imagePaths[key]) == false)
                {
                    return(null);
                }

                using (var codecs = MainForm.GetRasterCodecs())
                {
                    codecs.Options.Load.AllPages = true;
                    RasterImage image = codecs.Load(_imagePaths[key], page);
                    image.ChangeViewPerspective(RasterViewPerspective.TopLeft);
                    return(image);
                }
            }


            return(null);
        }
Esempio n. 2
0
        private void InitClass(RasterImage image, ConversionColorFormat srcFormat)
        {
            _viewer           = new ImageViewer();
            _viewer.BackColor = Color.DarkGray;
            _viewer.ViewHorizontalAlignment = ControlAlignment.Near;
            _viewer.Dock = DockStyle.Fill;
            _pnlViewer.Controls.Add(_viewer);
            _viewer.BringToFront();
            _viewer.Zoom(ControlSizeMode.Fit, 1, _viewer.DefaultZoomOrigin);

            _cbFit.Checked = _viewer.SizeMode == ControlSizeMode.Fit;

            // Start up the Color conversion toolkit.
            RasterColorConverterEngine.Startup();
            RasterImage tempImage = image.Clone();

            if (tempImage.ViewPerspective != RasterViewPerspective.TopLeft)
            {
                tempImage.ChangeViewPerspective(RasterViewPerspective.TopLeft);
            }

            _width        = tempImage.Width;
            _height       = tempImage.Height;
            _bytesPerLine = tempImage.BytesPerLine;

            _orgBuffer = new byte[_bytesPerLine * _height];

            tempImage.Access();

            for (int y = 0; y < _height - 1; y++)
            {
                tempImage.GetRow(y, _orgBuffer, (y * _width * 3), _width * 3);
            }

            _viewer.Image = tempImage;

            tempImage.Release();

            _srcFormat = srcFormat;

            foreach (ConvertItem i in _convertItems)
            {
                _cbColorFormat.Items.Add(i);
                if (i.Format == ConversionColorFormat.Yuv)
                {
                    _cbColorFormat.SelectedItem = i;
                }
            }

            UpdateMyControls();
        }
Esempio n. 3
0
        public void StartGWire(int ExternalEnergy)
        {
            if (_gwireCommand != null)
            {
                return;
            }

            RasterImage image = _viewer.Image;

            try
            {
                if (_viewer.Floater != null)
                {
                    _viewer.Floater.Dispose();
                    _viewer.Floater = null;
                }
            }
            catch (Exception ex)
            {
                Messager.ShowError(this, ex);
            }
            finally
            {
                _mainForm.UpdateControls();
            }

            if (image.HasRegion)
            {
                image.SetRegion(null, null, RasterRegionCombineMode.Set);
            }

            if (image.ViewPerspective != RasterViewPerspective.TopLeft)
            {
                image.ChangeViewPerspective(RasterViewPerspective.TopLeft);
            }
            _gwireCommand  = new GWireCommand(image, ExternalEnergy);
            _gwirePrevPath = new List <Point>();
            _anchorPoints  = new List <Point>();

            _viewer.MouseDown  += new MouseEventHandler(ActiveViewerForm_MouseDown);
            _viewer.MouseMove  += new MouseEventHandler(ActiveViewerForm_MouseMove);
            _viewer.PostRender += new EventHandler <ImageViewerRenderEventArgs>(ActiveViewerForm_Paint);

            _gwireStarted      = true;
            _gwireSeedSelected = _gwireNewSeed = false;
        }
Esempio n. 4
0
        private RasterImage AddTextFooter(RasterImage image, string tag, int footerMaxHeight)
        {
            LeadRect bounds = LeadRect.Create(0, 0, image.Width, image.Height);

            using (var imageTag = CreateTextFooter(LeadSize.Create(bounds.Width, footerMaxHeight), tag))
            {
                bounds.Height += imageTag.Height;

                var bpp             = 24;
                var byteOrder       = RasterByteOrder.Bgr;
                var viewPerspective = RasterViewPerspective.TopLeft;

                if (image.ViewPerspective != viewPerspective)
                {
                    image.ChangeViewPerspective(viewPerspective);
                }

                if (image.BitsPerPixel != bpp || image.Order != byteOrder)
                {
                    var colorResCommand = new ColorResolutionCommand(
                        ColorResolutionCommandMode.InPlace,
                        bpp,
                        byteOrder,
                        RasterDitheringMethod.None,
                        ColorResolutionCommandPaletteFlags.Optimized,
                        null);
                    colorResCommand.Run(image);
                }

                RasterImage imageResult = new RasterImage(RasterMemoryFlags.Conventional, bounds.Width, bounds.Height, bpp, byteOrder, viewPerspective, null, null, 0);

                {
                    var combine = new CombineFastCommand(imageResult, bounds, LeadPoint.Create(0, 0), CombineFastCommandFlags.SourceCopy);
                    combine.Run(image);
                }

                {
                    var combine = new CombineFastCommand(imageResult, LeadRect.Create(bounds.X, image.Height, bounds.Width, bounds.Height - image.Height), LeadPoint.Create(0, 0), CombineFastCommandFlags.SourceCopy);
                    combine.Run(imageTag);
                }
                return(imageResult);
            }
        }
Esempio n. 5
0
        public RasterImage Get(string key)
        {
            if (_imagePaths.ContainsKey(key))
            {
                using (var codecs = MainForm.GetRasterCodecs())
                {
                    var         path  = _imagePaths[key];
                    RasterImage image = codecs.Load(path, 1);
                    image.ChangeViewPerspective(RasterViewPerspective.TopLeft);
                    return(image);
                }
            }

            if (_cache != null)
            {
                return(_cache.Get <RasterImage>(key, _region));
            }

            return(null);
        }
Esempio n. 6
0
        internal static RasterImage LoadImage(string fileName, bool allPages)
        {
            RasterImage image = null;

            try
            {
                using (RasterCodecs codecs = GetRasterCodecs())
                {
                    codecs.Options.Load.AllPages = allPages;
                    image = codecs.Load(fileName);
                    image.ChangeViewPerspective(RasterViewPerspective.TopLeft);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return(null);
            }

            return(image);
        }
        public static async Task <RasterImage> RasterImageFromFile(string file, bool loadAllPages = false, int firstPageNumber = 1, int lastPageNumber = 1)
        {
            RasterImage rasterImage = null;

            using (var codecs = new RasterCodecs())
            {
                codecs.Options.Png.Save.QualityFactor            = 10;
                codecs.Options.RasterizeDocument.Load.Resolution = 300;
                codecs.Options.Load.Resolution = 300;
                codecs.Options.Load.AllPages   = loadAllPages;
                using (var inputStream = LeadStream.Factory.OpenFile(file))
                {
                    rasterImage = await codecs.LoadAsync(inputStream, 0, CodecsLoadByteOrder.Bgr, firstPageNumber, lastPageNumber);

                    if (rasterImage.ViewPerspective != RasterViewPerspective.TopLeft)
                    {
                        rasterImage.ChangeViewPerspective(RasterViewPerspective.TopLeft);
                    }
                }
            }

            return(rasterImage);
        }
Esempio n. 8
0
        // Optionally resizes the image before saving it (always preserving the original aspect ratio)
        public static void ResizeImage(RasterImage image, int width, int height)
        {
            SizeCommand sizeCommand;
            int         resizeWidth;
            int         resizeHeight;

            RasterSizeFlags flags = RasterSizeFlags.Resample;

            if (image.BitsPerPixel == 1)
            {
                flags |= RasterSizeFlags.FavorBlack;
            }

            // First check if its a FAX image (with different resolution), if so, resize it too
            if (image.XResolution != 0 && image.YResolution != 0 && Math.Abs(image.XResolution - image.YResolution) > 2)
            {
                // Yes
                if (image.XResolution > image.YResolution)
                {
                    resizeWidth  = image.ImageWidth;
                    resizeHeight = (int)((double)image.ImageHeight * (double)image.XResolution / (double)image.YResolution);
                }
                else
                {
                    resizeHeight = image.ImageHeight;
                    resizeWidth  = (int)((double)image.ImageWidth * (double)image.YResolution / (double)image.XResolution);
                }

                sizeCommand = new SizeCommand(resizeWidth, resizeHeight, flags);
                sizeCommand.Run(image);

                image.XResolution = Math.Max(image.XResolution, image.YResolution);
                image.YResolution = image.XResolution;
            }

            // Check user resize options, and resize only if needed
            if ((width == 0 && height == 0) ||
                (image.ImageWidth <= width && image.ImageHeight <= height))
            {
                return;
            }

            resizeWidth  = width;
            resizeHeight = height;

            // If width or height is 0, means the other is a fixed value and the missing value must be calculated
            // saving the aspect ratio
            if (resizeHeight == 0)
            {
                resizeHeight = (int)((double)image.ImageHeight * (double)resizeWidth / (double)image.ImageWidth + 0.5);
            }
            else if (resizeWidth == 0)
            {
                resizeWidth = (int)((double)image.ImageWidth * (double)resizeHeight / (double)image.ImageHeight + 0.5);
            }

            // Calculate the destination size
            LeadRect rc = new LeadRect(0, 0, resizeWidth, resizeHeight);

            rc = RasterImage.CalculatePaintModeRectangle(
                image.ImageWidth,
                image.ImageHeight,
                rc,
                RasterPaintSizeMode.Fit,
                RasterPaintAlignMode.Near,
                RasterPaintAlignMode.Near);

            image.ChangeViewPerspective(RasterViewPerspective.TopLeft);
            sizeCommand = new SizeCommand(rc.Width, rc.Height, flags);
            sizeCommand.Run(image);

            // Note, if the image was 1BPP, ScaleToGray converts it to 8, the format of the returned image is dealt with
            // in PrepareToSave

            // Since we resized the image, the original DPI is not correct anymore
            image.XResolution = 96;
            image.YResolution = 96;
        }