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");
            }
        }
        public static void Apply(PlanarImage source, PlanarImage target)
        {
            IntPtr context = SwScale.sws_getContext(source.Width, source.Height, m_pixelTypeMapper[source.PixelType],
                                                    target.Width, target.Height, m_pixelTypeMapper[target.PixelType],
                                                    SwScale.ConvertionFlags.SWS_BICUBIC, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

            int result = SwScale.sws_scale(context, source.PixelDataPointer, source.Pitches, 0, source.Height, target.PixelDataPointer, target.Pitches);

            if (result != target.Height)
            {
                throw new InvalidOperationException();
            }

            SwScale.sws_freeContext(context);
        }
        public static Bitmap ToBitmap(PlanarImage source, PixelFormat format, ColorSpace colorspace = ColorSpace.DEFAULT)
        {
            IntPtr context = SwScale.sws_getContext(source.Width, source.Height, m_pixelTypeMapper[source.PixelType],
                                                    source.Width, source.Height, m_rgbMapper[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, pCoef, dstRange, brightness, contrast, saturation);
            }

            Bitmap     bmp  = new Bitmap(source.Width, source.Height, format);
            BitmapData data = bmp.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadWrite, format);

            unsafe
            {
                pInput[0] = (byte *)data.Scan0.ToPointer();

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

            bmp.UnlockBits(data);
            if (bmp.Palette != null && bmp.Palette.Entries != null && bmp.Palette.Entries.Length > 0)
            {
                ColorPalette cp = bmp.Palette;
                for (int i = 0; i < cp.Entries.Length; i++)
                {
                    cp.Entries[i] = Color.FromArgb(i, i, i);
                }
                bmp.Palette = cp;
            }

            SwScale.sws_freeContext(context);
            return(bmp);
        }