Example #1
0
        private void Parse(BinaryReader reader)
        {
            DDSStruct header = new DDSStruct();

            Utils.PixelFormat pixelFormat = Utils.PixelFormat.UNKNOWN;
            byte[]            data        = null;

            if (ReadHeader(reader, ref header))
            {
                _isValid = true;
                // patches for stuff
                if (header.depth == 0)
                {
                    header.depth = 1;
                }

                uint blocksize = 0;
                pixelFormat = GetFormat(header, ref blocksize);
                if (pixelFormat == Utils.PixelFormat.UNKNOWN)
                {
                    throw new InvalidFileHeaderException();
                }

                data = ReadData(reader, header);
                if (data != null)
                {
                    byte[] rawData = Decompressor.Expand(header, data, pixelFormat);
                    _bitmap = CreateBitmap((int)header.width, (int)header.height, rawData);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Loads a DDS image from a file, and returns a Bitmap object of the image.
        /// </summary>
        /// <param name="file">The image file.</param>
        /// <param name="alpha">Preserve the alpha channel or not. (default: true)</param>
        /// <returns>The Bitmap representation of the image.</returns>
        public static Bitmap LoadImage(string file, bool alpha = true, Utils.PixelFormat pixel = Utils.PixelFormat.UNKNOWN)
        {
            byte[]   data = File.ReadAllBytes(file);
            DDSImage im   = new DDSImage(data, alpha, pixel);

            return(im.BitmapImage);
        }
Example #3
0
        public DDSImage(byte[] ddsImage, bool preserveAlpha = true, Utils.PixelFormat pixel = Utils.PixelFormat.UNKNOWN)
        {
            try
            {
                if (ddsImage == null)
                {
                    return;
                }

                if (ddsImage.Length == 0)
                {
                    return;
                }

                _alpha = preserveAlpha;

                using (MemoryStream stream = new MemoryStream(ddsImage.Length))
                {
                    stream.Write(ddsImage, 0, ddsImage.Length);
                    stream.Seek(0, SeekOrigin.Begin);

                    using (BinaryReader reader = new BinaryReader(stream))
                    {
                        Parse(reader, pixel);
                        stream.Close();
                        reader.Close();
                    }
                }
            }
            catch { }
        }
Example #4
0
        public DDSImage(CR2WBinaryReader reader, DDSStruct header, bool alpha)
        {
            Utils.PixelFormat pixelFormat = Utils.PixelFormat.UNKNOWN;
            byte[]            data        = null;

            if (header.depth == 0)
            {
                header.depth = 1;
            }

            _isValid = true;
            _alpha   = alpha;

            uint blocksize = 0;

            pixelFormat = GetFormat(header, ref blocksize);
            if (pixelFormat == Utils.PixelFormat.UNKNOWN)
            {
                throw new InvalidFileHeaderException();
            }

            data = ReadData(reader, header);
            if (data != null)
            {
                byte[] rawData = DDSDecompressor.Expand(header, data, pixelFormat);
                _bitmap = CreateBitmap((int)header.width, (int)header.height, rawData);
            }
        }
Example #5
0
        public DDSImage(Stream ddsImage, bool preserveAlpha = true, Utils.PixelFormat pixel = Utils.PixelFormat.UNKNOWN)
        {
            if (ddsImage == null)
            {
                return;
            }

            if (!ddsImage.CanRead)
            {
                return;
            }

            _alpha = preserveAlpha;

            using (BinaryReader reader = new BinaryReader(ddsImage))
            {
                Parse(reader, pixel);
            }
        }
Example #6
0
        private Utils.PixelFormat GetFormat(DDSStruct header, ref uint blocksize)
        {
            Utils.PixelFormat format = Utils.PixelFormat.UNKNOWN;
            if ((header.pixelformat.flags & Helper.DDPF_FOURCC) == Helper.DDPF_FOURCC)
            {
                blocksize = ((header.width + 3) / 4) * ((header.height + 3) / 4) * header.depth;

                switch (header.pixelformat.fourcc)
                {
                case Helper.FOURCC_DXT1:
                    format     = Utils.PixelFormat.DXT1;
                    blocksize *= 8;
                    break;

                case Helper.FOURCC_DXT2:
                    format     = Utils.PixelFormat.DXT2;
                    blocksize *= 16;
                    break;

                case Helper.FOURCC_DXT3:
                    format     = Utils.PixelFormat.DXT3;
                    blocksize *= 16;
                    break;

                case Helper.FOURCC_DXT4:
                    format     = Utils.PixelFormat.DXT4;
                    blocksize *= 16;
                    break;

                case Helper.FOURCC_DXT5:
                    format     = Utils.PixelFormat.DXT5;
                    blocksize *= 16;
                    break;

                case Helper.FOURCC_ATI1:
                    format     = Utils.PixelFormat.ATI1N;
                    blocksize *= 8;
                    break;

                case Helper.FOURCC_ATI2:
                    format     = Utils.PixelFormat.THREEDC;
                    blocksize *= 16;
                    break;

                case Helper.FOURCC_RXGB:
                    format     = Utils.PixelFormat.RXGB;
                    blocksize *= 16;
                    break;

                case Helper.FOURCC_DOLLARNULL:
                    format    = Utils.PixelFormat.A16B16G16R16;
                    blocksize = header.width * header.height * header.depth * 8;
                    break;

                case Helper.FOURCC_oNULL:
                    format    = Utils.PixelFormat.R16F;
                    blocksize = header.width * header.height * header.depth * 2;
                    break;

                case Helper.FOURCC_pNULL:
                    format    = Utils.PixelFormat.G16R16F;
                    blocksize = header.width * header.height * header.depth * 4;
                    break;

                case Helper.FOURCC_qNULL:
                    format    = Utils.PixelFormat.A16B16G16R16F;
                    blocksize = header.width * header.height * header.depth * 8;
                    break;

                case Helper.FOURCC_rNULL:
                    format    = Utils.PixelFormat.R32F;
                    blocksize = header.width * header.height * header.depth * 4;
                    break;

                case Helper.FOURCC_sNULL:
                    format    = Utils.PixelFormat.G32R32F;
                    blocksize = header.width * header.height * header.depth * 8;
                    break;

                case Helper.FOURCC_tNULL:
                    format    = Utils.PixelFormat.A32B32G32R32F;
                    blocksize = header.width * header.height * header.depth * 16;
                    break;

                default:
                    format     = Utils.PixelFormat.UNKNOWN;
                    blocksize *= 16;
                    break;
                }
            }
            else
            {
                // uncompressed image
                if ((header.pixelformat.flags & Helper.DDPF_LUMINANCE) == Helper.DDPF_LUMINANCE)
                {
                    if ((header.pixelformat.flags & Helper.DDPF_ALPHAPIXELS) == Helper.DDPF_ALPHAPIXELS)
                    {
                        format = Utils.PixelFormat.LUMINANCE_ALPHA;
                    }
                    else
                    {
                        format = Utils.PixelFormat.LUMINANCE;
                    }
                }
                else
                {
                    if ((header.pixelformat.flags & Helper.DDPF_ALPHAPIXELS) == Helper.DDPF_ALPHAPIXELS)
                    {
                        format = Utils.PixelFormat.RGBA;
                    }
                    else
                    {
                        format = Utils.PixelFormat.RGB;
                    }
                }

                blocksize = (header.width * header.height * header.depth * (header.pixelformat.rgbbitcount >> 3));
            }

            return(format);
        }
Example #7
0
        /// <summary>
        /// Loads a DDS image from a Stream, and returns a Bitmap object of the image.
        /// </summary>
        /// <param name="stream">The stream to read the image data from.</param>
        /// <param name="alpha">Preserve the alpha channel or not. (default: true)</param>
        /// <returns>The Bitmap representation of the image.</returns>
        public static Bitmap LoadImage(Stream stream, bool alpha = true, Utils.PixelFormat pixel = Utils.PixelFormat.UNKNOWN)
        {
            DDSImage im = new DDSImage(stream, alpha, pixel);

            return(im.BitmapImage);
        }
Example #8
0
        /// <summary>
        /// Loads a DDS image from a byte array, and returns a Bitmap object of the image.
        /// </summary>
        /// <param name="data">The image data, as a byte array.</param>
        /// <param name="alpha">Preserve the alpha channel or not. (default: true)</param>
        /// <returns>The Bitmap representation of the image.</returns>
        public static Bitmap LoadImage(byte[] data, bool alpha = true, Utils.PixelFormat pixel = Utils.PixelFormat.UNKNOWN)
        {
            DDSImage im = new DDSImage(data, false, pixel);

            return(im.BitmapImage);
        }