System.Drawing.Bitmap.GuessPixelFormat C# (CSharp) Method

GuessPixelFormat() private method

private GuessPixelFormat ( ) : void
return void
        private void GuessPixelFormat()
        {
            bool hasAlpha;
            CGColorSpace colorSpace;
            int bitsPerComponent;
            bool premultiplied = false;
            int bitsPerPixel = 0;
            CGImageAlphaInfo alphaInfo;

            var image = NativeCGImage;

            if (image == null) {
                throw new ArgumentException (" image is invalid! " );
            }

            alphaInfo = image.AlphaInfo;
            hasAlpha = ((alphaInfo == CGImageAlphaInfo.PremultipliedLast) || (alphaInfo == CGImageAlphaInfo.PremultipliedFirst) || (alphaInfo == CGImageAlphaInfo.Last) || (alphaInfo == CGImageAlphaInfo.First) ? true : false);

            imageSize.Width = (int)image.Width;
            imageSize.Height = (int)image.Height;

            // Not sure yet if we need to keep the original image information
            // before we change it internally.  TODO look at what windows does
            // and follow that.
            bitsPerComponent = (int)image.BitsPerComponent;
            bitsPerPixel = (int)image.BitsPerPixel;

            colorSpace = image.ColorSpace;

            if (colorSpace != null)
            {
                if (colorSpace.Model == CGColorSpaceModel.RGB) {
                    if (bitsPerPixel == 32) {
                        if (hasAlpha) {
                            if (alphaInfo == CGImageAlphaInfo.PremultipliedFirst)
                            {
                                premultiplied = true;
                                pixelFormat = PixelFormat.Format32bppPArgb;
                            }

                            if (alphaInfo == CGImageAlphaInfo.First)
                                pixelFormat = PixelFormat.Format32bppArgb;

                            if (alphaInfo == CGImageAlphaInfo.Last)
                                pixelFormat = PixelFormat.Format32bppRgb;

                            if (alphaInfo == CGImageAlphaInfo.PremultipliedLast)
                            {
                                premultiplied = true;
                                pixelFormat = PixelFormat.Format32bppRgb;
                            }

                        } else {
                            pixelFormat = PixelFormat.Format24bppRgb;
                        }
                    } else {
                        // Right now microsoft looks like it is using Format32bppRGB for other
                        // need more test cases to verify
                        pixelFormat = PixelFormat.Format32bppArgb;
                    }
                } else {
                    // Right now microsoft looks like it is using Format32bppRGB for other
                    // MonoChrome is set to 32bpppArgb
                    // need more test cases to verify
                    pixelFormat = PixelFormat.Format32bppArgb;
                }

            }
            else
            {
                // need more test cases to verify
                pixelFormat = PixelFormat.Format32bppArgb;

            }
        }