Пример #1
0
        /*
         *
         * Programming Guide for DDS
         * https://msdn.microsoft.com/en-us/library/windows/desktop/bb943991(v=vs.85).aspx#File_Layout1
         *
         * File Structure
         *
         * DWORD               dwMagic;
         * DDS_HEADER          header;
         *
         * If the DDS_PIXELFORMAT dwFlags is set to DDPF_FOURCC and dwFourCC is set to "DX10" an additional DDS_HEADER_DXT10
         *
         * DDS_HEADER_DXT10    header10;
         *
         * A pointer to an array of bytes that contains the main surface data.
         *
         * BYTE bdata[]
         *
         * BYTE bdata2[]
         *
         */
        public static DDS read(Stream stream)
        {
            var dds = new DDS();

            int readIndex = 0;

            // Read headers

            DDS_Magic fileMagicData;

            readIndex += stream.ReadStruct(out fileMagicData);

            readIndex += stream.ReadStruct(out dds.ddsHeader);

            // Only read the ddsHeaderDXT10 when the FOURCC.DX10 is set
            if (dds.ddsHeader.ddspf.dwFourCC == DDS_PIXELFORMAT.FOURCC.DX10)
            {
                readIndex += stream.ReadStruct(out dds.ddsHeaderDXT10);
            }

            if (dds.ddsHeader.ddspf.dwFourCC != DDS_PIXELFORMAT.FOURCC.DXT1 ||
               (dds.ddsHeader.dwFlags & DDS_HEADER.Flags.DDSD_LINEARSIZE) == 0)
            {
                throw new NotImplementedException("Format not implemented");
            }

            // Initialize and read DXT1 blocks

            var size = dds.height * dds.width;

            dds.blocks = new DXT1Block[size];

            for (uint i = 0; i < size; i++)
            {
                DDS_DXT1Block block;
                readIndex += stream.ReadStruct(out block);

                dds.blocks[i] = block;
            }

            return dds;
        }
Пример #2
0
        public static void Run(string[] args)
        {
            if (args.Length < 2)
            {
                var appName = System.AppDomain.CurrentDomain.FriendlyName;

                Console.WriteLine("Invalid command. Syntax: " + appName + " <orig file> <dest file>");
            }
            else
            {
                // Intermediate format
                Image image = null;

                string file1 = args[0];
                string file2 = args[1];

                // Check source and destionation formats
                FORMAT fromFormat;
                FORMAT toFormat;

                if (file1.ToLower().Contains(".dds"))
                {
                    fromFormat = FORMAT.DDS;
                }
                else if (file1.ToLower().Contains(".bmp"))
                {
                    fromFormat = FORMAT.BMP;
                }
                else
                {
                    Console.WriteLine("Unknown file extension: " + file1);
                    return;
                }

                if (file2.ToLower().Contains(".dds"))
                {
                    toFormat = FORMAT.DDS;
                }
                else if (file2.ToLower().Contains(".bmp"))
                {
                    toFormat = FORMAT.BMP;
                }
                else
                {
                    Console.WriteLine("Unknown file extension: " + file2);
                    return;
                }

                try
                {
                    // Read into Intermediate format
                    using (var fileStream = new FileStream(file1, FileMode.Open))
                    {
                        if (fromFormat == FORMAT.DDS)
                        {
                            image = DDS.DDS.read(fileStream);
                        }
                        else if (fromFormat == FORMAT.BMP)
                        {
                            image = BMP.BMP.read(fileStream);
                        }

                        fileStream.Close();
                    }

                    // Save into final format
                    File.Delete(file2);
                    using (var fileStream = new FileStream(file2, FileMode.OpenOrCreate))
                    {
                        if (toFormat == FORMAT.DDS)
                        {
                            DDS.DDS dds = image;
                            dds.write(fileStream);
                        }
                        else if (toFormat == FORMAT.BMP)
                        {
                            BMP.BMP bmp = image;
                            bmp.write(fileStream);
                        }

                        fileStream.Close();
                    }
                }
                catch (FileNotFoundException e)
                {
                    Console.WriteLine("File not found! " + file1);
                }
            }
        }