Example #1
0
        private async Task UpdateResultsAsync(ImageAnalyzer img)
        {
            try
            {
                DetectedFaceCollection.Clear();

                if (img.DetectedFaces.Any())
                {
                    // extract crops from the image
                    IList <DetectedFaceViewModel> detectedFaceViewModels = GetDetectedFaceViewModels(img.DetectedFaces, img.IdentifiedPersons);
                    var stream = img.ImageUrl == null ? await img.GetImageStreamCallback() : new MemoryStream(await new HttpClient().GetByteArrayAsync(img.ImageUrl));

                    using (stream)
                    {
                        var faces = await Util.GetImageCrops(detectedFaceViewModels, i => i.FaceRectangle.ToRect(), stream.AsRandomAccessStream());

                        if (faces != null)
                        {
                            DetectedFaceCollection.AddRange(faces);
                        }
                    }
                }

                CurrentDetectedFace          = DetectedFaceCollection.FirstOrDefault();
                this.notFoundGrid.Visibility = DetectedFaceCollection.Any() ? Visibility.Collapsed : Visibility.Visible;
            }
            finally
            {
                this.progressRing.IsActive = false;
            }
        }
Example #2
0
        /// <summary>
        /// Enqueues a frame for presentation.
        /// This method is thread safe and can be called from any thread.
        /// When the texture is presented and not needed anymore, the release callback is called.
        /// It's an error to modify the texture after calling this method, before the release callback is called.
        /// </summary>
        /// <param name="address">CPU virtual address of the texture data</param>
        /// <param name="width">Texture width</param>
        /// <param name="height">Texture height</param>
        /// <param name="stride">Texture stride for linear texture, should be zero otherwise</param>
        /// <param name="isLinear">Indicates if the texture is linear, normally false</param>
        /// <param name="gobBlocksInY">GOB blocks in the Y direction, for block linear textures</param>
        /// <param name="format">Texture format</param>
        /// <param name="bytesPerPixel">Texture format bytes per pixel (must match the format)</param>
        /// <param name="crop">Texture crop region</param>
        /// <param name="acquireCallback">Texture acquire callback</param>
        /// <param name="releaseCallback">Texture release callback</param>
        /// <param name="userObj">User defined object passed to the release callback</param>
        public void EnqueueFrameThreadSafe(
            ulong address,
            int width,
            int height,
            int stride,
            bool isLinear,
            int gobBlocksInY,
            Format format,
            int bytesPerPixel,
            ImageCrop crop,
            Action <GpuContext, object> acquireCallback,
            Action <object> releaseCallback,
            object userObj)
        {
            FormatInfo formatInfo = new FormatInfo(format, 1, 1, bytesPerPixel, 4);

            TextureInfo info = new TextureInfo(
                address,
                width,
                height,
                1,
                1,
                1,
                1,
                stride,
                isLinear,
                gobBlocksInY,
                1,
                1,
                Target.Texture2D,
                formatInfo);

            _frameQueue.Enqueue(new PresentationTexture(info, crop, acquireCallback, releaseCallback, userObj));
        }
Example #3
0
        private async Task CropAndSave(byte[] content, int visitorId, ImageCrop crop)
        {
            var webImage        = new WebImage(content);
            var visitorPictures = new List <VisitorPicture>();

            double rx = webImage.Width / crop.w;
            double ry = webImage.Height / crop.h;

            var smallImage = CropImage(webImage, rx, ry, crop.smallSelection);
            await _visitorPictureRepository.AddOrUpdateAsync(visitorId, PictureType.Small, smallImage.GetBytes());

            visitorPictures.Add(new VisitorPicture
            {
                Content     = smallImage.GetBytes(),
                PictureType = PictureType.Small,
                VisitorId   = visitorId
            });

            var bigImage = CropImage(webImage, rx, ry, crop.bigSelection);
            await _visitorPictureRepository.AddOrUpdateAsync(visitorId, PictureType.Big, bigImage.GetBytes());

            visitorPictures.Add(new VisitorPicture
            {
                Content     = bigImage.GetBytes(),
                PictureType = PictureType.Big,
                VisitorId   = visitorId
            });

            VisitorsNotificationHub.NotifyVisitorPicturesChanged(visitorPictures);
        }
Example #4
0
        public void Present(ITexture texture, ImageCrop crop, Action <object> swapBuffersCallback)
        {
            GL.Disable(EnableCap.FramebufferSrgb);

            if (_sizeChanged)
            {
                if (_stagingFrameBuffer != 0)
                {
                    GL.DeleteTextures(_stagingTextures.Length, _stagingTextures);
                    GL.DeleteFramebuffer(_stagingFrameBuffer);
                }

                CreateStagingFramebuffer();
                _sizeChanged = false;
            }

            (int oldDrawFramebufferHandle, int oldReadFramebufferHandle) = ((Pipeline)_renderer.Pipeline).GetBoundFramebuffers();

            CopyTextureToFrameBufferRGB(_stagingFrameBuffer, GetCopyFramebufferHandleLazy(), (TextureView)texture, crop, swapBuffersCallback);

            GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, oldReadFramebufferHandle);
            GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, oldDrawFramebufferHandle);

            GL.Enable(EnableCap.FramebufferSrgb);

            // Restore unpack alignment to 4, as performance overlays such as RTSS may change this to load their resources.
            GL.PixelStore(PixelStoreParameter.UnpackAlignment, 4);
        }
Example #5
0
 private void OnPictureBoxMouseDown(object sender, MouseEventArgs e)
 {
     if (Panning)
     {
         PanStart = e.Location;
     }
     else if (Cropping)
     {
         if (e.Button == System.Windows.Forms.MouseButtons.Left)
         {
             pictureBox.Refresh();
             if (ImageCrop != null)
             {
                 ImageCrop.Dispose();
             }
             Bitmap bm = new Bitmap(Image.Width, Image.Height);
             //bm.MakeTransparent();
             ImageCrop = bm;
             CropX     = e.X;
             CropY     = e.Y;
             int newX = (int)Math.Round(CropX * ZoomFactor + ViewRectangle.X);
             int newY = (int)Math.Round(CropY * ZoomFactor + ViewRectangle.Y);
             CropRectangle     = new Rectangle(newX, newY, 0, 0);
             CropPen           = new Pen(Color.Black, 1);
             CropPen.DashStyle = DashStyle.Dash;
             CropPen.Width     = getDpiAdjustedCropLineWidth();
             CropPen.Color     = Color.Tomato;
         }
     }
 }
Example #6
0
        public void TestByRegion(string resourcePath)
        {
            // Load source image
            using Image sourceImage = TestImage.FromResource(resourcePath);

            Assert.IsNotNull(sourceImage);

            Rectangle region = new(0, 0, sourceImage.Width / 2, sourceImage.Height / 2);

            // Crop region of image
            using Image croppedImage = ImageCrop.ByRegion(sourceImage, region);

            Assert.IsNotNull(croppedImage);

            // Check size
            Assert.AreEqual(region.Width, croppedImage.Width);
            Assert.AreEqual(region.Height, croppedImage.Height);

            // Get bytes for each image
            byte[] sourceBytes  = ImageBytes.FromImage(sourceImage);
            byte[] croppedBytes = ImageBytes.FromImage(sourceImage);

            // Compare to ensure correct region cropped
            int limit = croppedImage.Width * croppedImage.Height;

            for (int i = 0; i < limit; i++)
            {
                Assert.AreEqual(sourceBytes[i], croppedBytes[i]);
            }
        }
        public JsonResult Crop(ImageCrop img)
        {
            int     x            = Convert.ToInt32(img.CorX);
            int     y            = Convert.ToInt32(img.CorY);
            int     h            = Convert.ToInt32(img.CorH);
            int     w            = Convert.ToInt32(img.CorW);
            Usuario a            = user.BuscarUsuarioPorID(img.userID);
            string  nombreImagen = img.NombreImagen;

            string ruta       = "~/Uploads/users/";
            string sourceFile = Request.MapPath(ruta + nombreImagen);
            Image  oImagen    = Bitmap.FromFile(sourceFile);

            Bitmap bmp = new Bitmap(w, h, oImagen.PixelFormat);
            var    g   = Graphics.FromImage(bmp);

            g.DrawImage(oImagen, new Rectangle(0, 0, w, h), new Rectangle(x, y, w, h), GraphicsUnit.Pixel);
            System.Drawing.Imaging.ImageFormat frm = oImagen.RawFormat;
            oImagen.Dispose();
            string destFile = Request.MapPath(ruta + nombreImagen);

            util.insertImageUser(img.userID, nombreImagen);
            bmp.Save(destFile, frm);
            string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/');

            return(Json(baseUrl + ruta.Substring(1) + nombreImagen));
        }
Example #8
0
        private void PostFrameBuffer(Layer layer, BufferItem item)
        {
            int frameBufferWidth  = item.GraphicBuffer.Object.Width;
            int frameBufferHeight = item.GraphicBuffer.Object.Height;

            int nvMapHandle = item.GraphicBuffer.Object.Buffer.Surfaces[0].NvMapHandle;

            if (nvMapHandle == 0)
            {
                nvMapHandle = item.GraphicBuffer.Object.Buffer.NvMapId;
            }

            int bufferOffset = item.GraphicBuffer.Object.Buffer.Surfaces[0].Offset;

            NvMapHandle map = NvMapDeviceFile.GetMapFromHandle(layer.Owner, nvMapHandle);

            ulong frameBufferAddress = (ulong)(map.Address + bufferOffset);

            Format format = ConvertColorFormat(item.GraphicBuffer.Object.Buffer.Surfaces[0].ColorFormat);

            int bytesPerPixel =
                format == Format.B5G6R5Unorm ||
                format == Format.R4G4B4A4Unorm ? 2 : 4;

            int gobBlocksInY = 1 << item.GraphicBuffer.Object.Buffer.Surfaces[0].BlockHeightLog2;

            // Note: Rotation is being ignored.
            Rect cropRect = item.Crop;

            bool flipX = item.Transform.HasFlag(NativeWindowTransform.FlipX);
            bool flipY = item.Transform.HasFlag(NativeWindowTransform.FlipY);

            ImageCrop crop = new ImageCrop(
                cropRect.Left,
                cropRect.Right,
                cropRect.Top,
                cropRect.Bottom,
                flipX,
                flipY);

            TextureCallbackInformation textureCallbackInformation = new TextureCallbackInformation
            {
                Layer = layer,
                Item  = item,
            };

            _device.Gpu.Window.EnqueueFrameThreadSafe(
                frameBufferAddress,
                frameBufferWidth,
                frameBufferHeight,
                0,
                false,
                gobBlocksInY,
                format,
                bytesPerPixel,
                crop,
                AcquireBuffer,
                ReleaseBuffer,
                textureCallbackInformation);
        }
Example #9
0
        public void Present(ITexture texture, ImageCrop crop)
        {
            GL.Disable(EnableCap.FramebufferSrgb);

            CopyTextureToFrameBufferRGB(0, GetCopyFramebufferHandleLazy(), (TextureView)texture, crop);

            GL.Enable(EnableCap.FramebufferSrgb);
        }
Example #10
0
        public void Present(ITexture texture, ImageCrop crop, Action swapBuffersCallback)
        {
            // If there's already a frame in the pipeline, wait for it to be presented first.
            // This is a multithread rate limit - we can't be more than one frame behind the command queue.

            _renderer.WaitForFrame();
            _renderer.New <WindowPresentCommand>().Set(new TableRef <ThreadedTexture>(_renderer, texture as ThreadedTexture), crop, new TableRef <Action>(_renderer, swapBuffersCallback));
            _renderer.QueueCommand();
        }
Example #11
0
        /// <summary>
        ///  Format a video crop argument for ffmpeg.
        /// </summary>
        public static string formatCropArg(string file, ImageSize size, ImageCrop crop)
        {
            ImageSize outSize = getResolutionToUseWithCrop(file, size, crop);

            string cropArg =
                $"crop={outSize.Width - crop.Left - crop.Right}:{outSize.Height - crop.Top - crop.Bottom}:{crop.Left}:{crop.Top}"; // {3}

            return(cropArg);
        }
Example #12
0
        public async Task UploadPicture()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            int       visitorId = 0;
            ImageCrop imageCrop = null;
            string    root      = HttpContext.Current.Server.MapPath("~/App_Data");
            var       directory = new DirectoryInfo(root);

            if (!directory.Exists)
            {
                directory.Create();
            }

            var provider = new MultipartFormDataStreamProvider(root);

            await Request.Content.ReadAsMultipartAsync(provider);

            var serializer = new Newtonsoft.Json.JsonSerializer();

            foreach (var key in provider.FormData.AllKeys)
            {
                var val = provider.FormData.GetValues(key)[0];

                if (key == "visitorId")
                {
                    visitorId = (int)serializer.Deserialize(new StringReader(val), typeof(int));
                }
                else if (key == "image-crop")
                {
                    imageCrop = (ImageCrop)serializer.Deserialize(new StringReader(val), typeof(ImageCrop));
                }
            }

            if (visitorId <= 0)
            {
                throw new ArgumentException("visitorId");
            }

            if (imageCrop == null)
            {
                throw new ArgumentNullException("imageCrop");
            }

            foreach (var fileData in provider.FileData)
            {
                byte[] file = File.ReadAllBytes(fileData.LocalFileName);

                await CropAndSave(file, visitorId, imageCrop);

                File.Delete(fileData.LocalFileName);
            }
        }
Example #13
0
 /// <summary>
 /// Creates a new instance of the presentation texture.
 /// </summary>
 /// <param name="info">Information of the texture to be presented</param>
 /// <param name="crop">Texture crop region</param>
 /// <param name="callback">Texture release callback</param>
 /// <param name="userObj">User defined object passed to the release callback, can be used to identify the texture</param>
 public PresentationTexture(
     TextureInfo info,
     ImageCrop crop,
     Action <object> callback,
     object userObj)
 {
     Info     = info;
     Crop     = crop;
     Callback = callback;
     UserObj  = userObj;
 }
Example #14
0
        public void Present(ITexture texture, ImageCrop crop, Action swapBuffersCallback)
        {
            GL.Disable(EnableCap.FramebufferSrgb);

            CopyTextureToFrameBufferRGB(0, GetCopyFramebufferHandleLazy(), (TextureView)texture, crop, swapBuffersCallback);

            GL.Enable(EnableCap.FramebufferSrgb);

            // Restore unpack alignment to 4, as performance overlays such as RTSS may change this to load their resources.
            GL.PixelStore(PixelStoreParameter.UnpackAlignment, 4);
        }
Example #15
0
        /// <summary>
        ///  Format a video crop argument for ffmpeg.
        /// </summary>
        public static string formatCropArg(string file, ImageSize size, ImageCrop crop)
        {
            ImageSize outSize = UtilsVideo.getResolutionToUseWithCrop(file, size, crop);

            string cropArg = String.Format("crop={0}:{1}:{2}:{3}",
                                           outSize.Width - crop.Left - crop.Right,  // {0}
                                           outSize.Height - crop.Top - crop.Bottom, // {1}
                                           crop.Left,                               // {2}
                                           crop.Top);                               // {3}

            return(cropArg);
        }
Example #16
0
 /// <summary>
 /// Creates a new instance of the presentation texture.
 /// </summary>
 /// <param name="info">Information of the texture to be presented</param>
 /// <param name="crop">Texture crop region</param>
 /// <param name="acquireCallback">Texture acquire callback</param>
 /// <param name="releaseCallback">Texture release callback</param>
 /// <param name="userObj">User defined object passed to the release callback, can be used to identify the texture</param>
 public PresentationTexture(
     TextureInfo info,
     ImageCrop crop,
     Action <GpuContext, object> acquireCallback,
     Action <object> releaseCallback,
     object userObj)
 {
     Info            = info;
     Crop            = crop;
     AcquireCallback = acquireCallback;
     ReleaseCallback = releaseCallback;
     UserObj         = userObj;
 }
Example #17
0
        /// <summary>
        ///  Format a video size (scale) argument for ffmpeg.
        /// </summary>
        public static string formatVideoSizeArg(string inFile, ImageSize size, ImageCrop crop, int width_multiple,
                                                int height_multiple)
        {
            string    videoSizeArg = "";
            ImageSize outSize      = size;

            outSize.Width  = UtilsCommon.getNearestMultiple(outSize.Width, width_multiple);
            outSize.Height = UtilsCommon.getNearestMultiple(outSize.Height, height_multiple);

            videoSizeArg = $"scale={outSize.Width}:{outSize.Height}"; // {1}

            return(videoSizeArg);
        }
Example #18
0
        /// <summary>
        /// Convert the input video using the specified options.
        ///
        /// Note:
        /// h.264 and .mp4 have timing/cutting issues. h.264 only cuts on the last keyframe,
        /// which could be several seconds before the time that you actually want to cut.
        ///
        /// When cutting an .mp4 (even with MPEG4 video and MP3 audio), the cut will take place
        /// ~0.5 seconds before it should.
        ///
        /// (Is this still true?)
        /// </summary>
        public static void convertVideo(string inFile, string audioStream, DateTime startTime, DateTime endTime,
                                        ImageSize size, ImageCrop crop, int bitrateVideo, int bitrateAudio, VideoCodec videoCodec,
                                        AudioCodec audioCodec,
                                        Profilex264 profile, Presetx264 preset, string outFile, DialogProgress dialogProgress)
        {
            string videoMapArg = formatVideoMapArg();
            string audioMapArg = formatAudioMapArg(audioStream);

            string videoCodecArg = formatVideoCodecArg(videoCodec);

            string presetArg          = formatPresetFileArg(preset);
            string keyframeOptionsArg = formatKeyframeOptionsArg(videoCodec);
            string profileArg         = formatProfileFileArg(profile);

            string videoSizeArg    = formatVideoSizeArg(inFile, size, crop, 16, 2);
            string videoBitrateArg = $"-b:v {bitrateVideo}k";

            string audioCodecArg   = formatAudioCodecArg(audioCodec);
            string audioBitrateArg = formatAudioBitrateArg(bitrateAudio);

            string timeArg = formatStartTimeAndDurationArg(startTime, endTime);

            string cropArg = formatCropArg(inFile, size, crop);

            string threadsArg = "-threads 0";

            string ffmpegConvertArgs = "";

            // Good ffmpeg resource: http://howto-pages.org/ffmpeg/
            // 0:0 is assumed to be the video stream
            // Audio stream: 0:n where n is the number of the audio stream (usually 1)
            //
            // Example format:
            // -y -i "G:\Temp\input.mkv" -ac 2 -map 0:v:0 -map 0:a:0 -codec:v libx264 -preset superfast -g 6 -keyint_min 6
            // -fpre "E:\subs2srs\subs2srs\bin\Release\Utils\ffmpeg\presets\libx264-ipod640.ffpreset"
            // -b:v 800k -codec:a aac -b:a 128k -ss 00:03:32.420 -t 00:02:03.650 -vf "scale 352:202, crop=352:202:0:0" -threads 0
            // "C:\Documents and Settings\cb4960\Local Settings\Temp\~subs2srs_temp.mp4"
            ffmpegConvertArgs =
                $"-y -i \"{inFile}\" -ac 2 {videoMapArg} {audioMapArg} {videoCodecArg} {presetArg} {keyframeOptionsArg} {profileArg} {videoBitrateArg} {audioCodecArg} {audioBitrateArg}" +
                $" {timeArg} -vf \"{videoSizeArg}, {cropArg}\" {threadsArg} \"{outFile}\" "; // {14}


            if (dialogProgress == null)
            {
                UtilsCommon.startFFmpeg(ffmpegConvertArgs, true, true);
            }
            else
            {
                UtilsCommon.startFFmpegProgress(ffmpegConvertArgs, dialogProgress);
            }
        }
Example #19
0
    protected void ProcessImage()
    {
        // Setup the source file name and the output file name
        string sourceImageFileName = this.imgSource.ImageUrl;
        string outputImageFileName = "~/repository/output/Ex_A_211.jpg";

        // Setup the image crop filter
        ImageCrop imageCrop = null;

        switch (this.ddlRectangle.SelectedIndex)
        {
        case 0:
            // Select whole image
            imageCrop = new ImageCrop();
            break;

        case 1:
            // x=80, y=80, width=340, height=190 (Pixel)
            imageCrop = new ImageCrop(new Rectangle(80, 80, 340, 190));
            break;

        case 2:
            // x=-20, y=-20, width=540, height=390 (Pixel)
            imageCrop = new ImageCrop(new Rectangle(-20, -20, 540, 390));
            break;

        case 3:
            // x=-0.5, y=--1, width=5, height=5 (Inch / 96 DPI)
            imageCrop = ImageCrop.Calculate(GfxUnit.Inch, new RectangleF(-0.5F, -1F, 5F, 5F));
            break;

        case 4:
            // x=-1.5, y=--5, width=10.5, height=10.5 (Cm / 96 DPI)
            imageCrop = ImageCrop.Calculate(GfxUnit.Cm, new RectangleF(-1.5F, -5F, 10.5F, 10.5F), 96F);
            break;
        }

        // Set the canvas color
        imageCrop.CanvasColor = System.Drawing.ColorTranslator.FromHtml(this.txtCanvasColor.Text);

        // Process the image
        imageCrop.SaveProcessedImageToFileSystem(sourceImageFileName, outputImageFileName);

        // Update the displayed image (add a timestamp parameter to the query URL to ensure that the image is reloaded by the browser)
        this.imgOutput.ImageUrl = outputImageFileName + "?timestamp=" + DateTime.UtcNow.Ticks.ToString();

        // Display the generated image
        this.phOutputContainer.Visible = true;
    }
Example #20
0
        /// <summary>
        /// Enqueues a frame for presentation.
        /// This method is thread safe and can be called from any thread.
        /// When the texture is presented and not needed anymore, the release callback is called.
        /// It's an error to modify the texture after calling this method, before the release callback is called.
        /// </summary>
        /// <param name="address">CPU virtual address of the texture data</param>
        /// <param name="width">Texture width</param>
        /// <param name="height">Texture height</param>
        /// <param name="stride">Texture stride for linear texture, should be zero otherwise</param>
        /// <param name="isLinear">Indicates if the texture is linear, normally false</param>
        /// <param name="gobBlocksInY">GOB blocks in the Y direction, for block linear textures</param>
        /// <param name="format">Texture format</param>
        /// <param name="bytesPerPixel">Texture format bytes per pixel (must match the format)</param>
        /// <param name="crop">Texture crop region</param>
        /// <param name="acquireCallback">Texture acquire callback</param>
        /// <param name="releaseCallback">Texture release callback</param>
        /// <param name="userObj">User defined object passed to the release callback</param>
        public void EnqueueFrameThreadSafe(
            ulong address,
            int width,
            int height,
            int stride,
            bool isLinear,
            int gobBlocksInY,
            Format format,
            int bytesPerPixel,
            ImageCrop crop,
            Action <GpuContext, object> acquireCallback,
            Action <object> releaseCallback,
            object userObj)
        {
            FormatInfo formatInfo = new FormatInfo(format, 1, 1, bytesPerPixel, 4);

            TextureInfo info = new TextureInfo(
                0UL,
                width,
                height,
                1,
                1,
                1,
                1,
                stride,
                isLinear,
                gobBlocksInY,
                1,
                1,
                Target.Texture2D,
                formatInfo);

            int size = SizeCalculator.GetBlockLinearTextureSize(
                width,
                height,
                1,
                1,
                1,
                1,
                1,
                bytesPerPixel,
                gobBlocksInY,
                1,
                1).TotalSize;

            MultiRange range = new MultiRange(address, (ulong)size);

            _frameQueue.Enqueue(new PresentationTexture(info, range, crop, acquireCallback, releaseCallback, userObj));
        }
Example #21
0
 /// <summary>
 /// Creates a new instance of the presentation texture.
 /// </summary>
 /// <param name="cache">Texture cache used to look for the texture to be presented</param>
 /// <param name="info">Information of the texture to be presented</param>
 /// <param name="range">Physical memory locations where the texture data is located</param>
 /// <param name="crop">Texture crop region</param>
 /// <param name="acquireCallback">Texture acquire callback</param>
 /// <param name="releaseCallback">Texture release callback</param>
 /// <param name="userObj">User defined object passed to the release callback, can be used to identify the texture</param>
 public PresentationTexture(
     TextureCache cache,
     TextureInfo info,
     MultiRange range,
     ImageCrop crop,
     Action <GpuContext, object> acquireCallback,
     Action <object> releaseCallback,
     object userObj)
 {
     Cache           = cache;
     Info            = info;
     Range           = range;
     Crop            = crop;
     AcquireCallback = acquireCallback;
     ReleaseCallback = releaseCallback;
     UserObj         = userObj;
 }
Example #22
0
        private void Crop()
        {
            try
            {
                // create options and image crop
                var options = new Options(this.CropWidth, this.CropHeight)
                {
                    Debug = true
                };

                var crop = new ImageCrop(options);

                var watch = Stopwatch.StartNew();
                // load the source image
                using (var bitmap = SKBitmap.Decode(this.SourceImagePath))
                {
                    // calculate the best crop area
                    var result = crop.Crop(bitmap);
                    watch.Stop();

                    this.DebugImage = this.CreateImageSource(result.DebugInfo.Output);

                    watch.Start();

                    // crop the image
                    SKRect cropRect = new SKRect(result.Area.Left, result.Area.Top, result.Area.Right, result.Area.Bottom);
                    using (SKBitmap croppedBitmap = new SKBitmap((int)cropRect.Width, (int)cropRect.Height))
                        using (SKCanvas canvas = new SKCanvas(croppedBitmap))
                        {
                            SKRect source = new SKRect(cropRect.Left, cropRect.Top,
                                                       cropRect.Right, cropRect.Bottom);
                            SKRect dest = new SKRect(0, 0, cropRect.Width, cropRect.Height);
                            canvas.DrawBitmap(bitmap, source, dest);
                            watch.Stop();

                            this.CroppedImage = this.CreateImageSource(croppedBitmap);
                        };
                }

                this.ErrorText = null;
            }
            catch (Exception e)
            {
                this.ErrorText = e.Message;
            }
        }
    protected void ProcessImage()
    {
        // Setup the source file name and the output file name
        string sourceImageFileName = this.imgSource.ImageUrl;
        string outputImageFileName = "~/repository/output/Ex_A_211.jpg";

        // Setup the image crop filter
        ImageCrop imageCrop = null;

        switch (this.ddlRectangle.SelectedIndex)
        {
            case 0:
                // Select whole image
                imageCrop = new ImageCrop();
                break;
            case 1:
                // x=80, y=80, width=340, height=190 (Pixel)
                imageCrop = new ImageCrop(new Rectangle(80, 80, 340, 190));
                break;
            case 2:
                // x=-20, y=-20, width=540, height=390 (Pixel)
                imageCrop = new ImageCrop(new Rectangle(-20, -20, 540, 390));
                break;
            case 3:
                // x=-0.5, y=--1, width=5, height=5 (Inch / 96 DPI)
                imageCrop = ImageCrop.Calculate(GfxUnit.Inch, new RectangleF(-0.5F, -1F, 5F, 5F));
                break;
            case 4:
                // x=-1.5, y=--5, width=10.5, height=10.5 (Cm / 96 DPI)
                imageCrop = ImageCrop.Calculate(GfxUnit.Cm, new RectangleF(-1.5F, -5F, 10.5F, 10.5F), 96F);
                break;
        }

        // Set the canvas color
        imageCrop.CanvasColor = System.Drawing.ColorTranslator.FromHtml(this.txtCanvasColor.Text);

        // Process the image
        imageCrop.SaveProcessedImageToFileSystem(sourceImageFileName, outputImageFileName);

        // Update the displayed image (add a timestamp parameter to the query URL to ensure that the image is reloaded by the browser)
        this.imgOutput.ImageUrl = outputImageFileName + "?timestamp=" + DateTime.UtcNow.Ticks.ToString();

        // Display the generated image
        this.phOutputContainer.Visible = true;
    }
Example #24
0
        /// <summary>
        /// Presents a texture on the queue.
        /// If the queue is empty, then no texture is presented.
        /// </summary>
        /// <param name="swapBuffersCallback">Callback method to call when a new texture should be presented on the screen</param>
        public void Present(Action <object> swapBuffersCallback)
        {
            _context.AdvanceSequence();

            if (_frameQueue.TryDequeue(out PresentationTexture pt))
            {
                pt.AcquireCallback(_context, pt.UserObj);

                Texture texture = pt.Cache.FindOrCreateTexture(null, TextureSearchFlags.WithUpscale, pt.Info, 0, null, pt.Range);

                texture.SynchronizeMemory();

                ImageCrop crop = pt.Crop;

                if (texture.Info.Width > pt.Info.Width || texture.Info.Height > pt.Info.Height)
                {
                    int top    = crop.Top;
                    int bottom = crop.Bottom;
                    int left   = crop.Left;
                    int right  = crop.Right;

                    if (top == 0 && bottom == 0)
                    {
                        bottom = Math.Min(texture.Info.Height, pt.Info.Height);
                    }

                    if (left == 0 && right == 0)
                    {
                        right = Math.Min(texture.Info.Width, pt.Info.Width);
                    }

                    crop = new ImageCrop(left, right, top, bottom, crop.FlipX, crop.FlipY, crop.IsStretched, crop.AspectRatioX, crop.AspectRatioY);
                }

                _context.Renderer.Window.Present(texture.HostTexture, crop, swapBuffersCallback);

                pt.ReleaseCallback(pt.UserObj);
            }
        }
Example #25
0
 private void resetCropping()
 {
     Cropping = false;
     if (Panning)
     {
         Cursor = Cursors.Hand;
     }
     else
     {
         Cursor = Cursors.Default;
     }
     if (CropPen != null)
     {
         CropPen.Dispose();
         CropPen = null;
     }
     if (ImageCrop != null)
     {
         ImageCrop.Dispose();
         ImageCrop = null;
     }
     pictureBox.Image = Image;
     pictureBox.Invalidate();
 }
Example #26
0
        private void CopyTextureToFrameBufferRGB(int drawFramebuffer, int readFramebuffer, TextureView view, ImageCrop crop)
        {
            (int oldDrawFramebufferHandle, int oldReadFramebufferHandle) = ((Pipeline)_renderer.Pipeline).GetBoundFramebuffers();

            GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, drawFramebuffer);
            GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, readFramebuffer);

            TextureView viewConverted = view.Format.IsBgr() ? _renderer.TextureCopy.BgraSwap(view) : view;

            GL.FramebufferTexture(
                FramebufferTarget.ReadFramebuffer,
                FramebufferAttachment.ColorAttachment0,
                viewConverted.Handle,
                0);

            GL.ReadBuffer(ReadBufferMode.ColorAttachment0);

            GL.Disable(EnableCap.RasterizerDiscard);
            GL.Disable(IndexedEnableCap.ScissorTest, 0);

            GL.Clear(ClearBufferMask.ColorBufferBit);

            int   srcX0, srcX1, srcY0, srcY1;
            float scale = view.ScaleFactor;

            if (crop.Left == 0 && crop.Right == 0)
            {
                srcX0 = 0;
                srcX1 = (int)(view.Width / scale);
            }
            else
            {
                srcX0 = crop.Left;
                srcX1 = crop.Right;
            }

            if (crop.Top == 0 && crop.Bottom == 0)
            {
                srcY0 = 0;
                srcY1 = (int)(view.Height / scale);
            }
            else
            {
                srcY0 = crop.Top;
                srcY1 = crop.Bottom;
            }

            if (scale != 1f)
            {
                srcX0 = (int)(srcX0 * scale);
                srcY0 = (int)(srcY0 * scale);
                srcX1 = (int)Math.Ceiling(srcX1 * scale);
                srcY1 = (int)Math.Ceiling(srcY1 * scale);
            }

            float ratioX = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _height * crop.AspectRatioX / (_width * crop.AspectRatioY));
            float ratioY = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _width * crop.AspectRatioY / (_height * crop.AspectRatioX));

            int dstWidth  = (int)(_width * ratioX);
            int dstHeight = (int)(_height * ratioY);

            int dstPaddingX = (_width - dstWidth) / 2;
            int dstPaddingY = (_height - dstHeight) / 2;

            int dstX0 = crop.FlipX ? _width - dstPaddingX : dstPaddingX;
            int dstX1 = crop.FlipX ? dstPaddingX : _width - dstPaddingX;

            int dstY0 = crop.FlipY ? dstPaddingY : _height - dstPaddingY;
            int dstY1 = crop.FlipY ? _height - dstPaddingY : dstPaddingY;

            if (ScreenCaptureRequested)
            {
                CaptureFrame(srcX0, srcY0, srcX1, srcY1, view.Format.IsBgr(), crop.FlipX, crop.FlipY);

                ScreenCaptureRequested = false;
            }

            GL.BlitFramebuffer(
                srcX0,
                srcY0,
                srcX1,
                srcY1,
                dstX0,
                dstY0,
                dstX1,
                dstY1,
                ClearBufferMask.ColorBufferBit,
                BlitFramebufferFilter.Linear);

            // Remove Alpha channel
            GL.ColorMask(false, false, false, true);
            GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            GL.Clear(ClearBufferMask.ColorBufferBit);

            for (int i = 0; i < Constants.MaxRenderTargets; i++)
            {
                ((Pipeline)_renderer.Pipeline).RestoreComponentMask(i);
            }

            GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, oldReadFramebufferHandle);
            GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, oldDrawFramebufferHandle);

            ((Pipeline)_renderer.Pipeline).RestoreScissor0Enable();
            ((Pipeline)_renderer.Pipeline).RestoreRasterizerDiscard();

            if (viewConverted != view)
            {
                viewConverted.Dispose();
            }
        }
Example #27
0
        /// <summary>
        /// Enqueues a frame for presentation.
        /// This method is thread safe and can be called from any thread.
        /// When the texture is presented and not needed anymore, the release callback is called.
        /// It's an error to modify the texture after calling this method, before the release callback is called.
        /// </summary>
        /// <param name="pid">Process ID of the process that owns the texture pointed to by <paramref name="address"/></param>
        /// <param name="address">CPU virtual address of the texture data</param>
        /// <param name="width">Texture width</param>
        /// <param name="height">Texture height</param>
        /// <param name="stride">Texture stride for linear texture, should be zero otherwise</param>
        /// <param name="isLinear">Indicates if the texture is linear, normally false</param>
        /// <param name="gobBlocksInY">GOB blocks in the Y direction, for block linear textures</param>
        /// <param name="format">Texture format</param>
        /// <param name="bytesPerPixel">Texture format bytes per pixel (must match the format)</param>
        /// <param name="crop">Texture crop region</param>
        /// <param name="acquireCallback">Texture acquire callback</param>
        /// <param name="releaseCallback">Texture release callback</param>
        /// <param name="userObj">User defined object passed to the release callback</param>
        /// <exception cref="ArgumentException">Thrown when <paramref name="pid"/> is invalid</exception>
        public void EnqueueFrameThreadSafe(
            long pid,
            ulong address,
            int width,
            int height,
            int stride,
            bool isLinear,
            int gobBlocksInY,
            Format format,
            int bytesPerPixel,
            ImageCrop crop,
            Action <GpuContext, object> acquireCallback,
            Action <object> releaseCallback,
            object userObj)
        {
            if (!_context.PhysicalMemoryRegistry.TryGetValue(pid, out var physicalMemory))
            {
                throw new ArgumentException("The PID is invalid or the process was not registered", nameof(pid));
            }

            FormatInfo formatInfo = new FormatInfo(format, 1, 1, bytesPerPixel, 4);

            TextureInfo info = new TextureInfo(
                0UL,
                width,
                height,
                1,
                1,
                1,
                1,
                stride,
                isLinear,
                gobBlocksInY,
                1,
                1,
                Target.Texture2D,
                formatInfo);

            int size = SizeCalculator.GetBlockLinearTextureSize(
                width,
                height,
                1,
                1,
                1,
                1,
                1,
                bytesPerPixel,
                gobBlocksInY,
                1,
                1).TotalSize;

            MultiRange range = new MultiRange(address, (ulong)size);

            _frameQueue.Enqueue(new PresentationTexture(
                                    physicalMemory.TextureCache,
                                    info,
                                    range,
                                    crop,
                                    acquireCallback,
                                    releaseCallback,
                                    userObj));
        }
Example #28
0
        private void PostFrameBuffer(Layer layer, BufferItem item)
        {
            int frameBufferWidth  = item.GraphicBuffer.Object.Width;
            int frameBufferHeight = item.GraphicBuffer.Object.Height;

            int nvMapHandle = item.GraphicBuffer.Object.Buffer.Surfaces[0].NvMapHandle;

            if (nvMapHandle == 0)
            {
                nvMapHandle = item.GraphicBuffer.Object.Buffer.NvMapId;
            }

            ulong bufferOffset = (ulong)item.GraphicBuffer.Object.Buffer.Surfaces[0].Offset;

            NvMapHandle map = NvMapDeviceFile.GetMapFromHandle(layer.Owner, nvMapHandle);

            ulong frameBufferAddress = map.Address + bufferOffset;

            Format format = ConvertColorFormat(item.GraphicBuffer.Object.Buffer.Surfaces[0].ColorFormat);

            int bytesPerPixel =
                format == Format.B5G6R5Unorm ||
                format == Format.R4G4B4A4Unorm ? 2 : 4;

            int gobBlocksInY = 1 << item.GraphicBuffer.Object.Buffer.Surfaces[0].BlockHeightLog2;

            // Note: Rotation is being ignored.
            Rect cropRect = item.Crop;

            bool flipX = item.Transform.HasFlag(NativeWindowTransform.FlipX);
            bool flipY = item.Transform.HasFlag(NativeWindowTransform.FlipY);

            AspectRatio aspectRatio = _device.Configuration.AspectRatio;
            bool        isStretched = aspectRatio == AspectRatio.Stretched;

            ImageCrop crop = new ImageCrop(
                cropRect.Left,
                cropRect.Right,
                cropRect.Top,
                cropRect.Bottom,
                flipX,
                flipY,
                isStretched,
                aspectRatio.ToFloatX(),
                aspectRatio.ToFloatY());

            TextureCallbackInformation textureCallbackInformation = new TextureCallbackInformation
            {
                Layer = layer,
                Item  = item
            };

            _device.Gpu.Window.EnqueueFrameThreadSafe(
                layer.Owner,
                frameBufferAddress,
                frameBufferWidth,
                frameBufferHeight,
                0,
                false,
                gobBlocksInY,
                format,
                bytesPerPixel,
                crop,
                AcquireBuffer,
                ReleaseBuffer,
                textureCallbackInformation);

            if (item.Fence.FenceCount == 0)
            {
                _device.Gpu.Window.SignalFrameReady();
                _device.Gpu.GPFifo.Interrupt();
            }
            else
            {
                item.Fence.RegisterCallback(_device.Gpu, (x) =>
                {
                    _device.Gpu.Window.SignalFrameReady();
                    _device.Gpu.GPFifo.Interrupt();
                });
            }
        }
Example #29
0
        private void CopyTextureToFrameBufferRGB(int drawFramebuffer, int readFramebuffer, TextureView view, ImageCrop crop)
        {
            bool[] oldFramebufferColorWritemask = new bool[4];

            (int oldDrawFramebufferHandle, int oldReadFramebufferHandle) = ((Pipeline)_renderer.Pipeline).GetBoundFramebuffers();

            GL.GetBoolean(GetIndexedPName.ColorWritemask, drawFramebuffer, oldFramebufferColorWritemask);

            GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, drawFramebuffer);
            GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, readFramebuffer);

            GL.FramebufferTexture(
                FramebufferTarget.ReadFramebuffer,
                FramebufferAttachment.ColorAttachment0,
                view.Handle,
                0);

            GL.ReadBuffer(ReadBufferMode.ColorAttachment0);

            GL.Disable(EnableCap.RasterizerDiscard);
            GL.Disable(IndexedEnableCap.ScissorTest, 0);

            GL.Clear(ClearBufferMask.ColorBufferBit);

            int srcX0, srcX1, srcY0, srcY1;

            if (crop.Left == 0 && crop.Right == 0)
            {
                srcX0 = 0;
                srcX1 = view.Width;
            }
            else
            {
                srcX0 = crop.Left;
                srcX1 = crop.Right;
            }

            if (crop.Top == 0 && crop.Bottom == 0)
            {
                srcY0 = 0;
                srcY1 = view.Height;
            }
            else
            {
                srcY0 = crop.Top;
                srcY1 = crop.Bottom;
            }

            float ratioX = MathF.Min(1f, (_height * (float)NativeWidth) / ((float)NativeHeight * _width));
            float ratioY = MathF.Min(1f, (_width * (float)NativeHeight) / ((float)NativeWidth * _height));

            int dstWidth  = (int)(_width * ratioX);
            int dstHeight = (int)(_height * ratioY);

            int dstPaddingX = (_width - dstWidth) / 2;
            int dstPaddingY = (_height - dstHeight) / 2;

            int dstX0 = crop.FlipX ? _width - dstPaddingX : dstPaddingX;
            int dstX1 = crop.FlipX ? dstPaddingX : _width - dstPaddingX;

            int dstY0 = crop.FlipY ? dstPaddingY : _height - dstPaddingY;
            int dstY1 = crop.FlipY ? _height - dstPaddingY : dstPaddingY;

            GL.BlitFramebuffer(
                srcX0,
                srcY0,
                srcX1,
                srcY1,
                dstX0,
                dstY0,
                dstX1,
                dstY1,
                ClearBufferMask.ColorBufferBit,
                BlitFramebufferFilter.Linear);

            // Remove Alpha channel
            GL.ColorMask(drawFramebuffer, false, false, false, true);
            GL.ClearBuffer(ClearBuffer.Color, 0, new float[] { 0.0f, 0.0f, 0.0f, 1.0f });
            GL.ColorMask(drawFramebuffer,
                         oldFramebufferColorWritemask[0],
                         oldFramebufferColorWritemask[1],
                         oldFramebufferColorWritemask[2],
                         oldFramebufferColorWritemask[3]);

            GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, oldReadFramebufferHandle);
            GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, oldDrawFramebufferHandle);

            ((Pipeline)_renderer.Pipeline).RestoreScissor0Enable();
            ((Pipeline)_renderer.Pipeline).RestoreRasterizerDiscard();
        }
Example #30
0
        /// <summary>
        /// Take a snapshot of a video at the provided time.
        /// </summary>
        public static void takeSnapshotFromVideo(string inFile, DateTime snapTime, ImageSize size, ImageCrop crop,
                                                 string outFile)
        {
            string startTimeArg = UtilsVideo.formatStartTimeArg(snapTime);
            string videoSizeArg = UtilsVideo.formatVideoSizeArg(inFile, size, crop, 2, 2);
            string cropArg      = UtilsVideo.formatCropArg(inFile, size, crop);

            string ffmpegSnapshotProgArgs = "";

            // Example format:
            // -y -an -ss 00:03:33.370 -i "G:\Temp\input.mkv" -s 358x202 -f image2 -vf crop=358:202:0:0 -vframes 1 "output.jpg"
            ffmpegSnapshotProgArgs =
                $"-y -an {startTimeArg} -i \"{inFile}\" -f image2 -vf \"{videoSizeArg}, {cropArg}\" -vframes 1 \"{outFile}\""; // {4}

            UtilsCommon.startFFmpeg(ffmpegSnapshotProgArgs, false, true);
        }
Example #31
0
        /// <summary>
        /// Take a snapshot of a video at the provided time.
        /// </summary>
        public static void takeSnapshotFromVideo(string inFile, DateTime snapTime, ImageSize size, ImageCrop crop, string outFile)
        {
            string startTimeArg = UtilsVideo.formatStartTimeArg(snapTime);
            string videoSizeArg = UtilsVideo.formatVideoSizeArg(inFile, size, crop, 2, 2);
            string cropArg      = UtilsVideo.formatCropArg(inFile, size, crop);

            string ffmpegSnapshotProgArgs = "";

            // Example format:
            // -y -an -ss 00:03:33.370 -i "G:\Temp\input.mkv" -s 358x202 -f image2 -vf crop=358:202:0:0 -vframes 1 "output.jpg"
            ffmpegSnapshotProgArgs = String.Format("-y -an {0} -i \"{1}\" -f image2 -vf \"{2}, {3}\" -vframes 1 \"{4}\"",
                                                                 // Time to take snapshot at
                                                   startTimeArg, // {0}

                                                                 // Filename
                                                   inFile,       // {1}

                                                                 // Filters
                                                   videoSizeArg, // {2}
                                                   cropArg,      // {3}

                                                                 // Output name
                                                   outFile);     // {4}

            UtilsCommon.startFFmpeg(ffmpegSnapshotProgArgs, false, true);
        }