Beispiel #1
0
 public void Extract(Stream output)
 {
     using (var file = MemoryMappedFile.CreateFromFile(this.ParentFile, FileMode.Open))
     {
         using (var viewstream = file.CreateViewStream((PageOffset * 4096) + 9, ZSize, MemoryMappedFileAccess.Read))
         {
             //TODO: Finish this once we have a proper dds reader/writer
             byte Dxt = BitConverter.GetBytes(Type)[0];
             uint fmt = 0;
             if (Dxt == 7)
             {
                 fmt = 1;
             }
             else if (Dxt == 8)
             {
                 fmt = 4;
             }
             else if (Dxt == 10)
             {
                 fmt = 4;
             }
             else if (Dxt == 13)
             {
                 fmt = 3;
             }
             else if (Dxt == 14)
             {
                 fmt = 6;
             }
             else if (Dxt == 15)
             {
                 fmt = 4;
             }
             else if (Dxt == 253)
             {
                 fmt = 0;
             }
             else if (Dxt == 0)
             {
                 fmt = 0;
             }
             else
             {
                 throw new Exception("Invalid image!");
             }
             var  cubemap = (Type == 3 || Type == 0) && (SliceCount == 6);
             uint depth   = 0;
             if (SliceCount > 1 && Type == 4)
             {
                 depth = SliceCount;
             }
             if (Type == 3 && Dxt == 253)
             {
                 BaseAlignment = 32;
             }
             var header = new DDSHeader().generate(
                 BaseWidth,
                 BaseHeight,
                 TotalMipsCount,
                 fmt,
                 BaseAlignment,
                 IsCube == 1,
                 depth)
                          .Concat(BitConverter.GetBytes((Int32)0)).ToArray();
             output.Write(header, 0, header.Length);
             if (!(SliceCount == 6 && (Type == 253 || Type == 0)))
             {
                 using (var zs = new ZlibStream(viewstream, CompressionMode.Decompress))
                 {
                     zs.CopyTo(output);
                 }
             }
         }
     }
 }
Beispiel #2
0
        /// <summary>
        /// Convert a CBitmapTexture's image to a DDS image
        /// </summary>
        /// <returns>A proper dds file</returns>
        public static Bitmap Xbm2Dds(CR2WChunk imagechunk)
        {
            try
            {
                var image       = ((CBitmapTexture)(imagechunk.data)).Image;
                var compression = imagechunk.GetVariableByName("compression").ToString();
                var width       = uint.Parse(imagechunk.GetVariableByName("width").ToString());
                var height      = uint.Parse(imagechunk.GetVariableByName("height").ToString());
                var mips        = imagechunk.GetVariableByName("residentMipIndex") != null?uint.Parse(imagechunk.GetVariableByName("residentMipIndex").ToString()) : 0;

                var tempfile = new MemoryStream();

                var dxt = DDSHeader.Format_DXT1;
                switch (compression)
                {
                case "TCM_DXTNoAlpha":
                    dxt = DDSHeader.Format_DXT1;
                    break;

                case "TCM_DXTAlpha":
                    dxt = DDSHeader.Format_DXT5;
                    break;

                case "TCM_NormalsHigh":
                    dxt = DDSHeader.Format_DXT5;
                    break;

                case "TCM_Normals":
                    dxt = DDSHeader.Format_DXT1;
                    break;

                case "TCM_NormalsGloss":
                    dxt = DDSHeader.Format_DXT5;
                    break;

                case "TCM_QualityControl":
                    dxt = DDSHeader.Format_DXT5;
                    break;

                default:
                    throw new Exception("Invalid compression type! [" + compression + "]");
                }

                DDSHeader ddsheader = new DDSHeader();
                using (var bw = new BinaryWriter(tempfile))
                {
                    bw.Write(ddsheader.generate(width, height, mips, dxt).ToArray());
                    //bw.Write(image.Bytes);  // First 20 bytes is garbage
                    bw.Write((new ArraySegment <byte>(image.Bytes, 20, image.Bytes.Length - 20)).ToArray());
                }
                tempfile.Flush();
#if DEBUG
                //File.WriteAllBytes(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\asd.dds",tempfile.ToArray());
#endif
                return(new DdsImage(tempfile.ToArray()).BitmapImage);
            }
            catch (Exception e)
            {
                string            message = e.Message;
                string            caption = "Error!";
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                MessageBox.Show(message, caption, buttons);
                return(null);
            }
        }
Beispiel #3
0
 public void Extract(Stream output)
 {
     using (var file = MemoryMappedFile.CreateFromFile(this.ParentFile, FileMode.Open))
     {
         using (var viewstream = file.CreateViewStream((Offset * 4096) + 9, ZSize, MemoryMappedFileAccess.Read))
         {
             uint fmt = 0;
             if (Dxt == 7)
             {
                 fmt = 1;
             }
             else if (Dxt == 8)
             {
                 fmt = 4;
             }
             else if (Dxt == 10)
             {
                 fmt = 4;
             }
             else if (Dxt == 13)
             {
                 fmt = 3;
             }
             else if (Dxt == 14)
             {
                 fmt = 6;
             }
             else if (Dxt == 15)
             {
                 fmt = 4;
             }
             else if (Dxt == 253)
             {
                 fmt = 0;
             }
             else if (Dxt == 0)
             {
                 fmt = 0;
             }
             else
             {
                 throw new Exception("Invalid image!");
             }
             var  cubemap = (Type == 3 || Type == 0) && (Typeinfo == 6);
             uint depth   = 0;
             if (Typeinfo > 1 && Type == 4)
             {
                 depth = Typeinfo;
             }
             if (Type == 3 && Dxt == 253)
             {
                 Bpp = 32;
             }
             var header = new DDSHeader().generate(Width, Height, 1, fmt, Bpp, cubemap, depth)
                          .Concat(new[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }).ToArray();
             output.Write(header, 0, header.Length);
             if (!(Typeinfo == 6 && (Dxt == 253 || Dxt == 0)))
             {
                 var zlib = new ZlibStream(viewstream, CompressionMode.Decompress);
                 zlib.CopyTo(output);
             }
         }
     }
 }