Exemple #1
0
        public byte[] GetBytes(ImageDestFormat destFormat, GammaEncoding gamma, int stride)
        {
            ImageSourceFormat srcFormat;

            if (HalfChannels.ContainsKey("R") && HalfChannels.ContainsKey("G") && HalfChannels.ContainsKey("B"))
            {
                srcFormat = HalfChannels.ContainsKey("A") ? ImageSourceFormat.HalfRGBA : ImageSourceFormat.HalfRGB;
            }
            else if (FloatChannels.ContainsKey("R") && FloatChannels.ContainsKey("G") && FloatChannels.ContainsKey("B"))
            {
                srcFormat = FloatChannels.ContainsKey("A") ? ImageSourceFormat.SingleRGBA : ImageSourceFormat.SingleRGB;
            }
            else
            {
                throw new EXRFormatException("Unrecognized EXR image format, did not contain half/single RGB color channels");
            }
            return(GetBytes(srcFormat, destFormat, gamma, stride));
        }
Exemple #2
0
        /// <summary>
        /// Gets RGB color channels as an interleaved array of floats. If includeAlpha is true,
        /// this will include the alpha channel in the array.
        /// </summary>
        public float[] GetFloats(ChannelConfiguration channels, bool premultiplied, GammaEncoding gamma, bool includeAlpha)
        {
            ImageSourceFormat srcFormat;

            if (HalfChannels.ContainsKey("R") && HalfChannels.ContainsKey("G") && HalfChannels.ContainsKey("B"))
            {
                srcFormat = includeAlpha ? ImageSourceFormat.HalfRGBA : ImageSourceFormat.HalfRGB;
            }
            else if (FloatChannels.ContainsKey("R") && FloatChannels.ContainsKey("G") && FloatChannels.ContainsKey("B"))
            {
                srcFormat = includeAlpha ? ImageSourceFormat.SingleRGBA : ImageSourceFormat.SingleRGB;
            }
            else
            {
                throw new EXRFormatException("Unrecognized EXR image format, did not contain half/single RGB color channels");
            }
            return(GetFloats(srcFormat, channels, premultiplied, gamma));
        }
Exemple #3
0
        public byte[] GetBytes(ImageSourceFormat srcFormat, ImageDestFormat destFormat, GammaEncoding gamma, int stride)
        {
            CheckHasData();

            int bytesPerPixel = EXRFile.GetBytesPerPixel(destFormat);
            int bitsPerPixel  = EXRFile.GetBitsPerPixel(destFormat);

            if (stride < bytesPerPixel * DataWindow.Width)
            {
                throw new ArgumentException("Stride was lower than minimum", "stride");
            }
            byte[] buffer = new byte[stride * DataWindow.Height];

            var padding = stride - bytesPerPixel * DataWindow.Width;

            bool isHalf           = srcFormat == ImageSourceFormat.HalfRGB || srcFormat == ImageSourceFormat.HalfRGBA;
            bool sourceAlpha      = false;
            bool destinationAlpha =
                destFormat == ImageDestFormat.BGRA16 ||
                destFormat == ImageDestFormat.BGRA32 ||
                destFormat == ImageDestFormat.BGRA8 ||
                destFormat == ImageDestFormat.PremultipliedBGRA16 ||
                destFormat == ImageDestFormat.PremultipliedBGRA32 ||
                destFormat == ImageDestFormat.PremultipliedBGRA8 ||
                destFormat == ImageDestFormat.PremultipliedRGBA16 ||
                destFormat == ImageDestFormat.PremultipliedRGBA32 ||
                destFormat == ImageDestFormat.PremultipliedRGBA8 ||
                destFormat == ImageDestFormat.RGBA16 ||
                destFormat == ImageDestFormat.RGBA32 ||
                destFormat == ImageDestFormat.RGBA8;
            bool premultiplied =
                destFormat == ImageDestFormat.PremultipliedBGRA16 ||
                destFormat == ImageDestFormat.PremultipliedBGRA32 ||
                destFormat == ImageDestFormat.PremultipliedBGRA8 ||
                destFormat == ImageDestFormat.PremultipliedRGBA16 ||
                destFormat == ImageDestFormat.PremultipliedRGBA32 ||
                destFormat == ImageDestFormat.PremultipliedRGBA8;
            bool bgra =
                destFormat == ImageDestFormat.BGR16 ||
                destFormat == ImageDestFormat.BGR32 ||
                destFormat == ImageDestFormat.BGR8 ||
                destFormat == ImageDestFormat.BGRA16 ||
                destFormat == ImageDestFormat.BGRA32 ||
                destFormat == ImageDestFormat.BGRA8 ||
                destFormat == ImageDestFormat.PremultipliedBGRA16 ||
                destFormat == ImageDestFormat.PremultipliedBGRA32 ||
                destFormat == ImageDestFormat.PremultipliedBGRA8;

            Half[]  hr, hg, hb, ha;
            float[] fr, fg, fb, fa;
            hr = hg = hb = ha = null;
            fr = fg = fb = fa = null;

            if (isHalf)
            {
                if (!HalfChannels.ContainsKey("R"))
                {
                    throw new ArgumentException("Half type channel R not found", "srcFormat");
                }
                if (!HalfChannels.ContainsKey("G"))
                {
                    throw new ArgumentException("Half type channel G not found", "srcFormat");
                }
                if (!HalfChannels.ContainsKey("B"))
                {
                    throw new ArgumentException("Half type channel B not found", "srcFormat");
                }
                hr = HalfChannels["R"];
                hg = HalfChannels["G"];
                hb = HalfChannels["B"];

                if (srcFormat == ImageSourceFormat.HalfRGBA)
                {
                    if (!HalfChannels.ContainsKey("A"))
                    {
                        throw new ArgumentException("Half type channel A not found", "srcFormat");
                    }
                    ha          = HalfChannels["A"];
                    sourceAlpha = true;
                }
            }
            else
            {
                if (!FloatChannels.ContainsKey("R"))
                {
                    throw new ArgumentException("Single type channel R not found", "srcFormat");
                }
                if (!FloatChannels.ContainsKey("G"))
                {
                    throw new ArgumentException("Single type channel G not found", "srcFormat");
                }
                if (!FloatChannels.ContainsKey("B"))
                {
                    throw new ArgumentException("Single type channel B not found", "srcFormat");
                }
                fr = FloatChannels["R"];
                fg = FloatChannels["G"];
                fb = FloatChannels["B"];

                if (srcFormat == ImageSourceFormat.HalfRGBA)
                {
                    if (!FloatChannels.ContainsKey("A"))
                    {
                        throw new ArgumentException("Single type channel A not found", "srcFormat");
                    }
                    fa          = FloatChannels["A"];
                    sourceAlpha = true;
                }
            }


            // !PARALLEL

            /*
             * int srcIndex = 0;
             * int destIndex = 0;
             *
             * BinaryWriter writer = new BinaryWriter(new MemoryStream(buffer));
             *
             * for (int y = 0; y < DataWindow.Height; y++, destIndex += padding) {
             *  GetScanlineBytes(bytesPerPixel, destIndex, srcIndex, isHalf, destinationAlpha, sourceAlpha,
             *      hr, hg, hb, ha, fr, fg, fb, fa,
             *      bitsPerPixel, gamma, premultiplied, bgra, buffer, writer);
             *  destIndex += DataWindow.Width * bytesPerPixel;
             *  srcIndex += DataWindow.Width;
             * }
             *
             * writer.Dispose();
             * writer.BaseStream.Dispose();
             */

            var actions = (from y in Enumerable.Range(0, DataWindow.Height)
                           select(Action)(() =>
            {
                var destIndex = stride * y;
                var srcIndex = DataWindow.Width * y;

                using var stream = new MemoryStream(buffer);
                using var writer = new BinaryWriter(stream);

                GetScanlineBytes(bytesPerPixel, destIndex, srcIndex, isHalf, destinationAlpha, sourceAlpha,
                                 hr, hg, hb, ha, fr, fg, fb, fa,
                                 bitsPerPixel, gamma, premultiplied, bgra, buffer, writer);
            })).ToArray();

            Parallel.Invoke(actions);


            return(buffer);
        }