public RawPreviewData GetPreview(JsonEncodeObject settings, int previewNumber) { // Fetch the image data from LibHb string taskJson = JsonSerializer.Serialize(settings, JsonSettings.Options); IntPtr resultingImageStruct = HBFunctions.hb_get_preview3_json(this.Handle, previewNumber, taskJson); hb_image_s image = InteropUtilities.ToStructureFromPtr <hb_image_s>(resultingImageStruct); // Copy the filled image buffer to a managed array. int stride_width = image.plane[0].stride; int stride_height = image.plane[0].height_stride; int imageBufferSize = stride_width * stride_height; byte[] managedBuffer = new byte[imageBufferSize]; Marshal.Copy(image.plane[0].data, managedBuffer, 0, imageBufferSize); RawPreviewData preview = new RawPreviewData(managedBuffer, stride_width, stride_height, image.width, image.height); // Close the image so we don't leak memory. IntPtr nativeJobPtrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr))); Marshal.WriteIntPtr(nativeJobPtrPtr, resultingImageStruct); HBFunctions.hb_image_close(nativeJobPtrPtr); Marshal.FreeHGlobal(nativeJobPtrPtr); return(preview); }
public static Bitmap ConvertByteArrayToBitmap(RawPreviewData previewData) { var bitmap = new Bitmap(previewData.Width, previewData.Height); BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, previewData.Width, previewData.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb); IntPtr targetPtr = bitmapData.Scan0; // Pointer to the first pixel. try { for (int i = 0; i < previewData.Height; i++) { Marshal.Copy(previewData.RawBitmapData, i * previewData.StrideWidth, targetPtr, previewData.Width * 4); targetPtr = IntPtr.Add(targetPtr, previewData.Width * 4); } bitmap.UnlockBits(bitmapData); } catch (Exception exc) { Debug.WriteLine(exc); } return(bitmap); }
public static Bitmap ConvertByteArrayToBitmap(RawPreviewData previewData) { var bitmap = new Bitmap(previewData.Width, previewData.Height); BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, previewData.Width, previewData.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb); IntPtr ptr = bitmapData.Scan0; // Pointer to the first pixel. for (int i = 0; i < previewData.Height; i++) { try { Marshal.Copy(previewData.RawBitmapData, i * previewData.StrideWidth, ptr, previewData.StrideWidth); ptr = IntPtr.Add(ptr, previewData.Width * 4); } catch (Exception exc) { Debug.WriteLine(exc); // In theory, this will allow a partial image display if this happens. TODO add better logging of this. } } bitmap.UnlockBits(bitmapData); return(bitmap); }
public RawPreviewData GetPreview(PreviewSettings settings, int previewNumber, bool deinterlace) { SourceTitle title = this.Titles.TitleList.FirstOrDefault(t => t.Index == settings.TitleNumber); // Create the Expected Output Geometry details for libhb. hb_geometry_settings_s uiGeometry = new hb_geometry_settings_s { crop = new[] { settings.Cropping.Top, settings.Cropping.Bottom, settings.Cropping.Left, settings.Cropping.Right }, itu_par = 0, keep = (int)AnamorphicFactory.KeepSetting.HB_KEEP_WIDTH + (settings.KeepDisplayAspect ? 0x04 : 0), // TODO Keep Width? maxWidth = settings.MaxWidth, maxHeight = settings.MaxHeight, mode = (int)(hb_anamorphic_mode_t)settings.Anamorphic, modulus = settings.Modulus ?? 16, geometry = new hb_geometry_s { height = settings.Height, width = settings.Width, par = settings.Anamorphic != Anamorphic.Custom && settings.Anamorphic != Anamorphic.Automatic ? new hb_rational_t { den = title.Geometry.PAR.Den, num = title.Geometry.PAR.Num } : new hb_rational_t { den = settings.PixelAspectY, num = settings.PixelAspectX } } }; // Fetch the image data from LibHb IntPtr resultingImageStuct = hbFunctions.hb_get_preview2(this.Handle, settings.TitleNumber, previewNumber, ref uiGeometry, deinterlace ? 1 : 0); hb_image_s image = InteropUtilities.ToStructureFromPtr <hb_image_s>(resultingImageStuct); // Copy the filled image buffer to a managed array. int stride_width = image.plane[0].stride; int stride_height = image.plane[0].height_stride; int imageBufferSize = stride_width * stride_height; // int imageBufferSize = outputWidth * outputHeight * 4; byte[] managedBuffer = new byte[imageBufferSize]; Marshal.Copy(image.plane[0].data, managedBuffer, 0, imageBufferSize); RawPreviewData preview = new RawPreviewData(managedBuffer, stride_width, stride_height, image.width, image.height); // Close the image so we don't leak memory. IntPtr nativeJobPtrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr))); Marshal.WriteIntPtr(nativeJobPtrPtr, resultingImageStuct); hbFunctions.hb_image_close(nativeJobPtrPtr); Marshal.FreeHGlobal(nativeJobPtrPtr); return(preview); }
/// <summary> /// Get a Preview image for the current job and preview number. /// </summary> /// <param name="job"> /// The job. /// </param> /// <param name="preview"> /// The preview. /// </param> /// <returns> /// The <see cref="BitmapImage"/>. /// </returns> public BitmapImage GetPreview(EncodeTask job, int preview) { if (this.instance == null) { return(null); } BitmapImage bitmapImage = null; try { EncodeTaskFactory factory = new EncodeTaskFactory(this.userSettingService); JsonEncodeObject jobDict = factory.Create(job, HBConfigurationFactory.Create()); RawPreviewData bitmapData = this.instance.GetPreview(jobDict, preview); bitmapImage = BitmapUtilities.ConvertToBitmapImage(BitmapUtilities.ConvertByteArrayToBitmap(bitmapData)); } catch (AccessViolationException e) { Debug.WriteLine(e); } return(bitmapImage); }
/// <summary> /// Get a Preview image for the current job and preview number. /// </summary> /// <param name="job"> /// The job. /// </param> /// <param name="preview"> /// The preview. /// </param> /// <returns> /// The <see cref="BitmapImage"/>. /// </returns> public BitmapImage GetPreview(EncodeTask job, int preview) { if (this.instance == null) { return(null); } BitmapImage bitmapImage = null; try { PreviewSettings settings = new PreviewSettings { Cropping = new Cropping(job.Cropping), MaxWidth = job.MaxWidth ?? 0, MaxHeight = job.MaxHeight ?? 0, KeepDisplayAspect = job.KeepDisplayAspect, TitleNumber = job.Title, Anamorphic = job.Anamorphic, Modulus = job.Modulus, Width = job.Width ?? 0, Height = job.Height ?? 0, PixelAspectX = job.PixelAspectX, PixelAspectY = job.PixelAspectY }; RawPreviewData bitmapData = this.instance.GetPreview(settings, preview, job.DeinterlaceFilter != DeinterlaceFilter.Off); bitmapImage = BitmapUtilities.ConvertToBitmapImage(BitmapUtilities.ConvertByteArrayToBitmap(bitmapData)); } catch (AccessViolationException e) { Debug.WriteLine(e); } return(bitmapImage); }