public BitmapImage GetPreview(EncodeJob job, int previewNumber)
        {
            Title title = this.Titles.FirstOrDefault(t => t.TitleNumber == job.Title);

            Validate.NotNull(title, "GetPreview: Title should not have been null. This is probably a bug.");

            // Creat the Expected Output Geometry details for libhb.
            hb_geometry_settings_s uiGeometry = new hb_geometry_settings_s
            {
                crop      = new[] { job.Cropping.Top, job.Cropping.Bottom, job.Cropping.Left, job.Cropping.Right },
                itu_par   = 0,
                keep      = (int)AnamorphicFactory.KeepSetting.HB_KEEP_WIDTH + (job.KeepDisplayAspect ? 0x04 : 0), // TODO Keep Width?
                maxWidth  = job.MaxWidth,
                maxHeight = job.MaxHeight,
                mode      = (int)(hb_anamorphic_mode_t)job.Anamorphic,
                modulus   = job.Modulus,
                geometry  = new hb_geometry_s
                {
                    height = job.Height,
                    width  = job.Width,
                    par    = job.Anamorphic != Anamorphic.Custom
                        ? new hb_rational_t {
                        den = title.ParVal.Height, num = title.ParVal.Width
                    }
                        : new hb_rational_t {
                        den = job.PixelAspectY, num = job.PixelAspectX
                    }
                }
            };

            // Sanatise the input.
            Geometry resultGeometry = AnamorphicFactory.CreateGeometry(job, title, AnamorphicFactory.KeepSetting.HB_KEEP_WIDTH); // TODO this keep isn't right.
            int      width          = resultGeometry.Width * resultGeometry.PAR.Num / resultGeometry.PAR.Den;
            int      height         = resultGeometry.Height;

            uiGeometry.geometry.height = resultGeometry.Height; // Prased the height now.
            int outputWidth  = width;
            int outputHeight = height;

            // Fetch the image data from LibHb
            IntPtr     resultingImageStuct = HBFunctions.hb_get_preview2(this.hbHandle, job.Title, previewNumber, ref uiGeometry, 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);

            var        bitmap     = new Bitmap(outputWidth, outputHeight);
            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, outputWidth, outputHeight), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);

            IntPtr ptr = bitmapData.Scan0; // Pointer to the first pixel.

            for (int i = 0; i < image.height; i++)
            {
                try
                {
                    Marshal.Copy(managedBuffer, i * stride_width, ptr, stride_width);
                    ptr = IntPtr.Add(ptr, outputWidth * 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);

            // 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);

            // Create a Bitmap Image for display.
            using (var memoryStream = new MemoryStream())
            {
                try
                {
                    bitmap.Save(memoryStream, ImageFormat.Bmp);
                }
                finally
                {
                    bitmap.Dispose();
                }

                var wpfBitmap = new BitmapImage();
                wpfBitmap.BeginInit();
                wpfBitmap.CacheOption  = BitmapCacheOption.OnLoad;
                wpfBitmap.StreamSource = memoryStream;
                wpfBitmap.EndInit();
                wpfBitmap.Freeze();

                return(wpfBitmap);
            }
        }
Exemple #2
0
        /// <summary>
        /// Gets the language object for the given code.
        /// </summary>
        /// <param name="code">The ISO-639-2 code for the language.</param>
        /// <returns>Object that describes the language.</returns>
        public static Language Get(string code)
        {
            iso639_lang_t language = InteropUtilities.ToStructureFromPtr <iso639_lang_t>(HBFunctions.lang_for_code2(code));

            return(HandBrakeUnitConversionHelpers.NativeToLanguage(language));
        }