Ejemplo n.º 1
0
        /// <summary>
        /// Converts planar image to another pixel alignment type
        /// </summary>
        /// <param name="newPixelType"></param>
        /// <returns></returns>
        public PlanarImage Convert(PixelAlignmentType newPixelType)
        {
            PlanarImage result = new PlanarImage(this.Width, this.Height, newPixelType);

            ConverterResizer.Apply(this, result);
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Performs both conversion and resize in a single operation
        /// </summary>
        /// <param name="newPixelType"></param>
        /// <param name="newSize"></param>
        /// <returns></returns>
        public PlanarImage ConvertAndResize(PixelAlignmentType newPixelType, Size newSize)
        {
            PlanarImage result = new PlanarImage(newSize.Width, newSize.Height, newPixelType);

            ConverterResizer.Apply(this, result);
            return(result);
        }
        public static PlanarImage FromBitmap(Bitmap source, PixelAlignmentType format, ColorSpace colorspace = ColorSpace.DEFAULT)
        {
            IntPtr context = SwScale.sws_getContext(source.Width, source.Height, m_rgbMapper[source.PixelFormat],
                                                    source.Width, source.Height, m_pixelTypeMapper[format],
                                                    SwScale.ConvertionFlags.SWS_BICUBIC, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

            int *pCoef = SwScale.sws_getCoefficients(colorspace);
            int *inv_table;
            int *table;
            int  srcRange, dstRange, brightness, contrast, saturation;

            int result = SwScale.sws_getColorspaceDetails(context, out inv_table, out srcRange, out table, out dstRange, out brightness, out contrast, out saturation);

            if (result != -1)
            {
                result = SwScale.sws_setColorspaceDetails(context, pCoef, srcRange, table, dstRange, brightness, contrast, saturation);
            }

            PlanarImage yuv  = new PlanarImage(source.Width, source.Height, format);
            BitmapData  data = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, source.PixelFormat);

            unsafe
            {
                pInput[0] = (byte *)data.Scan0.ToPointer();
                result    = SwScale.sws_scale(context, pInput, new int[] { data.Stride, 0, 0, 0 }, 0, source.Height, yuv.PixelDataPointer, yuv.Pitches);
                if (result != yuv.Height)
                {
                    throw new InvalidOperationException();
                }
            }

            source.UnlockBits(data);
            SwScale.sws_freeContext(context);
            return(yuv);
        }
        public ConverterResizer(Size sourceSize, PixelAlignmentType sourceType, Size targetSize, PixelAlignmentType targetType)
        {
            if (SwScale.sws_isSupportedInput(m_pixelTypeMapper[m_sourceType]) == 0)
            {
                throw new InvalidOperationException("Unsupported source type " + sourceType);
            }

            if (SwScale.sws_isSupportedOutput(m_pixelTypeMapper[targetType]) == 0)
            {
                throw new InvalidOperationException("Unsupported target type " + targetType);
            }

            m_sourceSize = sourceSize;
            m_targetSize = targetSize;
            m_sourceType = sourceType;
            m_targetType = targetType;

            m_context = SwScale.sws_getContext(m_sourceSize.Width, m_sourceSize.Height, m_pixelTypeMapper[m_sourceType],
                                               m_targetSize.Width, m_targetSize.Height, m_pixelTypeMapper[m_targetType],
                                               SwScale.ConvertionFlags.SWS_BICUBIC, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

            if (m_context == IntPtr.Zero)
            {
                throw new InvalidOperationException("Context initialization failed. Supplied arguments may be invalid");
            }
        }
Ejemplo n.º 5
0
        public ConverterResizer(Size sourceSize, PixelAlignmentType sourceType, Size targetSize, PixelAlignmentType targetType)
        {
            if (SwScale.sws_isSupportedInput(m_pixelTypeMapper[m_sourceType]) == 0)
            {
                throw new InvalidOperationException("Unsupported source type " + sourceType);
            }

            if (SwScale.sws_isSupportedOutput(m_pixelTypeMapper[targetType]) == 0)
            {
                throw new InvalidOperationException("Unsupported target type " + targetType);
            }

            m_sourceSize = sourceSize;
            m_targetSize = targetSize;
            m_sourceType = sourceType;
            m_targetType = targetType;

            m_context = SwScale.sws_getContext(m_sourceSize.Width, m_sourceSize.Height, m_pixelTypeMapper[m_sourceType],
                                            m_targetSize.Width, m_targetSize.Height, m_pixelTypeMapper[m_targetType],
                                            SwScale.ConvertionFlags.SWS_BICUBIC, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

            if (m_context == IntPtr.Zero)
            {
                throw new InvalidOperationException("Context initialization failed. Supplied arguments may be invalid");
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Initializes new instance of planar image with no pixel data
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="pixelType"></param>
        public PlanarImage(int width, int height, PixelAlignmentType pixelType)
        {
            Width = width;
            Height = height;
            PixelType = pixelType;
            Init();

            Planes = new IntPtr[PlaneSizes.Length];
            for (int i = 0; i < PlaneSizes.Length; i++)
            {
                Planes[i] = Marshal.AllocHGlobal(PlaneSizes[i] * sizeof(byte));
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Initializes new instance of planar image with no pixel data
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="pixelType"></param>
        public PlanarImage(int width, int height, PixelAlignmentType pixelType)
        {
            Width     = width;
            Height    = height;
            PixelType = pixelType;
            Init();

            Planes = new IntPtr[PlaneSizes.Length];
            for (int i = 0; i < PlaneSizes.Length; i++)
            {
                Planes[i] = Marshal.AllocHGlobal(PlaneSizes[i] * sizeof(byte));
            }
        }
Ejemplo n.º 8
0
        public bool SetupSurface(int videoWidth, int videoHeight, FrameFormat format)
        {
            this.pixelType = ConvertToPixelFormat(format);
            if (pixelType == PixelAlignmentType.NotSupported)
            {
                return(false);
            }

            this.width  = videoWidth;
            this.height = videoHeight;
            switch (format)
            {
            case FrameFormat.YV12:
            case FrameFormat.NV12:
                this.frameSize = this.width * this.height * 3 / 2;
                break;

            case FrameFormat.YUY2:
            case FrameFormat.UYVY:
            case FrameFormat.RGB15:                            // rgb555
            case FrameFormat.RGB16:                            // rgb565
                this.frameSize = this.width * this.height * 2; // 每个像素2字节
                break;

            case FrameFormat.RGB24:
                this.frameSize = this.width * this.height * 3;     // 每个像素3字节

                break;

            case FrameFormat.RGB32:
            case FrameFormat.ARGB32:
                this.frameSize = this.width * this.height * 4;     // 每个像素4字节
                break;

            default:
                return(false);
            }

            this.imageSource     = new WriteableBitmap(videoWidth, videoHeight, DPI_X, DPI_Y, System.Windows.Media.PixelFormats.Bgr32, null);
            this.imageSourceRect = new Int32Rect(0, 0, videoWidth, videoHeight);

            System.Drawing.Size size = new System.Drawing.Size(videoWidth, videoHeight);
            this.converter = new ConverterResizer(size, pixelType, size, PixelAlignmentType.BGRA);

            this.NotifyImageSourceChanged();

            return(true);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Initializes new instance of planar image from pointer to flat memory buffer containing all pixel planes
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="pixelType"></param>
        /// <param name="pixelData"></param>
        public PlanarImage(int width, int height, PixelAlignmentType pixelType, IntPtr pixelData, int pixelDataSize)
            : this(width, height, pixelType)
        {
            if (pixelDataSize < TotalImageLength)
            {
                throw new InvalidOperationException("Insufficient memory buffer size.");
            }

            RtlMoveMemory(Planes[0], pixelData, PlaneSizes[0]);
            if (NumberOfPlanes > 1)
            {
                RtlMoveMemory(Planes[1], pixelData + PlaneSizes[0], PlaneSizes[1]);
                if (NumberOfPlanes > 2)
                {
                    RtlMoveMemory(Planes[2], pixelData + PlaneSizes[0] + PlaneSizes[1], PlaneSizes[2]);
                }
            }
        }
        public bool SetupSurface(int videoWidth, int videoHeight, FrameFormat format)
        {
            this.pixelType = ConvertToPixelFormat(format);
            if (pixelType == PixelAlignmentType.NotSupported)
            {
                return false;
            }

            this.width = videoWidth;
            this.height = videoHeight;
            switch (format)
            {
                case FrameFormat.YV12:
                case FrameFormat.NV12:
                    this.frameSize = this.width * this.height * 3 / 2;
                    break;

                case FrameFormat.YUY2:
                case FrameFormat.UYVY:
                case FrameFormat.RGB15: // rgb555
                case FrameFormat.RGB16: // rgb565
                    this.frameSize = this.width * this.height * 2; // 每个像素2字节
                    break;

                case FrameFormat.RGB32:
                case FrameFormat.ARGB32:
                    this.frameSize = this.width * this.height * 4; // 每个像素4字节
                    break;

                default:
                    return false;
            }

            this.imageSource = new WriteableBitmap(videoWidth, videoHeight, DPI_X, DPI_Y, System.Windows.Media.PixelFormats.Bgr32, null);
            this.imageSourceRect = new Int32Rect(0, 0, videoWidth, videoHeight);

            System.Drawing.Size size = new System.Drawing.Size(videoWidth, videoHeight);
            this.converter = new ConverterResizer(size, pixelType, size, PixelAlignmentType.BGRA);

            this.NotifyImageSourceChanged();

            return true;
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Initializes new instance of planar image with pixel data
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="pixelType"></param>
        /// <param name="planes"></param>
        public PlanarImage(int width, int height, PixelAlignmentType pixelType, IntPtr[] planes)
        {
            Width = width;
            Height = height;
            PixelType = pixelType;
            Init();

            Planes = new IntPtr[PlaneSizes.Length];
            for (int i = 0; i < PlaneSizes.Length; i++)
            {
                Planes[i] = Marshal.AllocHGlobal(PlaneSizes[i] * sizeof(byte));
                RtlMoveMemory(Planes[i], planes[i], PlaneSizes[i]);
            }

            if (this.PixelType == PixelAlignmentType.YV12)
            {
                SwapUVPlanes();
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Initializes new instance of planar image with pixel data
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="pixelType"></param>
        /// <param name="planes"></param>
        public PlanarImage(int width, int height, PixelAlignmentType pixelType, IntPtr[] planes)
        {
            Width     = width;
            Height    = height;
            PixelType = pixelType;
            Init();

            Planes = new IntPtr[PlaneSizes.Length];
            for (int i = 0; i < PlaneSizes.Length; i++)
            {
                Planes[i] = Marshal.AllocHGlobal(PlaneSizes[i] * sizeof(byte));
                RtlMoveMemory(Planes[i], planes[i], PlaneSizes[i]);
            }

            if (this.PixelType == PixelAlignmentType.YV12)
            {
                SwapUVPlanes();
            }
        }
Ejemplo n.º 13
0
        public static PlanarImage FromBitmap(Bitmap source, PixelAlignmentType format, ColorSpace colorspace = ColorSpace.DEFAULT)
        {
            IntPtr context = SwScale.sws_getContext(source.Width, source.Height, m_rgbMapper[source.PixelFormat],
                                             source.Width, source.Height, m_pixelTypeMapper[format],
                                             SwScale.ConvertionFlags.SWS_BICUBIC, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

            int* pCoef = SwScale.sws_getCoefficients(colorspace);
            int* inv_table;
            int* table;
            int srcRange, dstRange, brightness, contrast, saturation;

            int result = SwScale.sws_getColorspaceDetails(context, out inv_table, out srcRange, out table, out dstRange, out brightness, out contrast, out saturation);
            if (result != -1)
            {
                result = SwScale.sws_setColorspaceDetails(context, pCoef, srcRange, table, dstRange, brightness, contrast, saturation);
            }

            PlanarImage yuv = new PlanarImage(source.Width, source.Height, format);
            BitmapData data = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, source.PixelFormat);

            unsafe
            {
                pInput[0] = (byte*)data.Scan0.ToPointer();
                result = SwScale.sws_scale(context, pInput, new int[] { data.Stride, 0, 0, 0 }, 0, source.Height, yuv.PixelDataPointer, yuv.Pitches);
                if (result != yuv.Height)
                {
                    throw new InvalidOperationException();
                }
            }

            source.UnlockBits(data);
            SwScale.sws_freeContext(context);
            return yuv;
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Performs both conversion and resize in a single operation
 /// </summary>
 /// <param name="newPixelType"></param>
 /// <param name="newSize"></param>
 /// <returns></returns>
 public PlanarImage ConvertAndResize(PixelAlignmentType newPixelType, Size newSize)
 {
     PlanarImage result = new PlanarImage(newSize.Width, newSize.Height, newPixelType);
     ConverterResizer.Apply(this, result);
     return result;
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Converts planar image to another pixel alignment type
 /// </summary>
 /// <param name="newPixelType"></param>
 /// <returns></returns>
 public PlanarImage Convert(PixelAlignmentType newPixelType)
 {
     PlanarImage result = new PlanarImage(this.Width, this.Height, newPixelType);
     ConverterResizer.Apply(this, result);
     return result;
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Converts System.Drawing.Bitmap object into planar image
 /// </summary>
 /// <param name="bitmap"></param>
 /// <param name="pixelType"></param>
 /// <returns></returns>
 public static PlanarImage Load(Bitmap bitmap, PixelAlignmentType pixelType)
 {
     VerifyIsSupported(bitmap.PixelFormat);
     return ConverterResizer.FromBitmap(bitmap, pixelType);
 }
Ejemplo n.º 17
0
        /// <summary>
        /// Initializes new instance of planar image from pointer to flat memory buffer containing all pixel planes
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="pixelType"></param>
        /// <param name="pixelData"></param>
        public PlanarImage(int width, int height, PixelAlignmentType pixelType, IntPtr pixelData, int pixelDataSize)
            : this(width, height, pixelType)
        {
            if (pixelDataSize < TotalImageLength)
            {
                throw new InvalidOperationException("Insufficient memory buffer size.");
            }

            RtlMoveMemory(Planes[0], pixelData, PlaneSizes[0]);
            if (NumberOfPlanes > 1)
            {
                RtlMoveMemory(Planes[1], pixelData + PlaneSizes[0], PlaneSizes[1]);
                if (NumberOfPlanes > 2)
                {
                    RtlMoveMemory(Planes[2], pixelData + PlaneSizes[0] + PlaneSizes[1], PlaneSizes[2]);
                }
            }
        }
Ejemplo n.º 18
0
 /// <summary>
 /// Converts System.Drawing.Bitmap object into planar image
 /// </summary>
 /// <param name="bitmap"></param>
 /// <param name="pixelType"></param>
 /// <returns></returns>
 public static PlanarImage Load(Bitmap bitmap, PixelAlignmentType pixelType)
 {
     VerifyIsSupported(bitmap.PixelFormat);
     return(ConverterResizer.FromBitmap(bitmap, pixelType));
 }