Inheritance: IComparable
        public bool Equals(ImageSize p)
        {
            // If parameter is null return false:
            if ((object)p == null)
            {
                return false;
            }

            // Return true if the fields match:
            return (this.width == p.width) && (this.height == p.height);
        }
Exemple #2
0
        private void PropertiesPopup()
        {
            AmaroK86.ImageFormat.ImageSize imgSize = tex2D.imgList.Max(image => image.imgSize);

            string LOD;

            if (String.IsNullOrEmpty(tex2D.LODGroup))
            {
                LOD = "No LODGroup (Uses World)";
            }
            else
            {
                LOD = tex2D.LODGroup;
            }
            string arc;

            if (String.IsNullOrEmpty(tex2D.arcName))
            {
                arc = "\nPCC Stored";
            }
            else
            {
                arc = "\nTexture Cache File: " + tex2D.arcName + ".tfc";
            }

            string mesg = "Information about: " + tex2D.texName;

            mesg += "\nFormat: " + tex2D.texFormat;
            mesg += "\nWidth: " + imgSize.width + ", Height: " + imgSize.height;
            mesg += "\nLODGroup: " + LOD;
            mesg += arc;
            mesg += "\nOriginal Location: " + tex2D.allPccs[0];
            for (int i = 1; i < tex2D.allPccs.Count; i++)
            {
                mesg += "\nAlso found in: " + tex2D.allPccs[i];
            }

            MessageBox.Show(mesg);
        }
        public TGA(string fileName, byte[] data)
            : base(fileName, data) // used when importing file
        {
            // saving tga header data in TGAHeader structure
            GCHandle handle = GCHandle.Alloc(headData, GCHandleType.Pinned);
            TGAHeader = (_TGAHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(_TGAHeader));
            handle.Free();

            imgSize = new ImageSize((uint)TGAHeader.width, (uint)TGAHeader.height);
            BPP = TGAHeader.bits / 8;

            if (RLECompressed)
                RLEDecompress(); // decompress
            else
                imgData = imgData.Take(TGAHeader.width * TGAHeader.height * (int)BPP).ToArray(); // get rid of tga footer

            if (verticalFlipped)
                flipVertically();

            format = subtype();
        }
        public TGA(string fileName, ImageSize imgSize, string format, byte[] rawData) // used when extracting raw data from me3 archive
            : base(fileName, imgSize, format)
        {
            TGAHeader.imagetype = 0x2; // RGB Image type
            TGAHeader.bits = (byte)(rawData.Length / (imgSize.width * imgSize.height) * 8);
            TGAHeader.width = (short)imgSize.width;
            TGAHeader.height = (short)imgSize.height;
            //TGAHeader.colourmapbits = 32;
            TGAHeader.descriptor = 0x00; // 0x20 = flips the image vertically
            BPP = TGAHeader.bits / 8;

            switch (format)
            {
                case "PF_G8":
                case "G8":
                    {
                        imgData = new byte[rawData.Length];
                        for (int i = 0; i < imgSize.height; i++)
                        {
                            for (int j = 0; j < imgSize.width; j++)
                            {
                                imgData[(imgSize.width * i) + j] = rawData[(imgSize.width * i) + j];
                            }
                        }
                        TGAHeader.imagetype = 0x3;
                    }
                    break;

                case "PF_A8R8G8B8":
                case "A8R8G8B8":
                    {
                        TGAHeader.descriptor |= 0x08; // 0x08 = 8-bit alpha bits
                        imgData = new byte[rawData.Length];
                        int intBPP = (int)BPP;

                        for (int i = 0; i < imgSize.height; i++)
                        {
                            for (int j = 0; j < imgSize.width; j++)
                            {
                                for (int k = 0; k < intBPP; k++)
                                    imgData[(intBPP * imgSize.width * (imgSize.height - 1 - i)) + (j * intBPP) + k] = rawData[(imgSize.width * intBPP * i) + (j * intBPP) + k];
                                /*imgData[(4 * imgSize.width * (imgSize.height - 1 - i)) + (j * 4) + 1] = rawData[(imgSize.width * 4 * i) + (j * 4) + 1];
                                imgData[(4 * imgSize.width * (imgSize.height - 1 - i)) + (j * 4) + 2] = rawData[(imgSize.width * 4 * i) + (j * 4) + 2];
                                imgData[(4 * imgSize.width * (imgSize.height - 1 - i)) + (j * 4) + 3] = rawData[(imgSize.width * 4 * i) + (j * 4) + 3];*/
                            }
                        }
                        verticalFlipped = true;
                        flipVertically();
                    }
                    break;
                default: throw new FormatException("Invalid TGA format");
            }

            // convert TGAHeader to byte array
            int headSize = Marshal.SizeOf(TGAHeader);
            headData = new byte[headSize];
            IntPtr ptr = Marshal.AllocHGlobal(headSize);
            Marshal.StructureToPtr(TGAHeader, ptr, true);
            Marshal.Copy(ptr, headData, 0, headData.Length);
            Marshal.FreeHGlobal(ptr);
        }
        public DDS(string fileName, byte[] data)
            : base(fileName, data) // call the base constructor
        {
            byte[] buffer = headData;
            // put data inside DDSHeader
            GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            DDSHeader = (_DDSHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(_DDSHeader));
            handle.Free();

            imgSize = new ImageSize(DDSHeader.Width, DDSHeader.Height);
            format = subtype();
            switch (format)
            {
                case "DXT1": BPP = 0.5F; break;
                case "DXT5":
                case "ATI2": BPP = 1F; break;
                case "V8U8": BPP = 2F; break;
                default: BPP = (float)DDSHeader.RGBBitCount / 8; break;
            }
        }
Exemple #6
0
 private long GetMipMapDataSize(ImageSize imgsize)
 {
     uint w = imgsize.width;
     uint h = imgsize.height;
     if (compressed)
     {
         if (w < 4)
             w = 4;
         if (h < 4)
             h = 4;
     }
     long totalBytes = (long)((float)(w * h) * BPP);
     w = imgsize.width;
     h = imgsize.height;
     if (w == 1 && h == 1)
         return totalBytes;
     if (w != 1)
         w = imgsize.width / 2;
     if (h != 1)
         h = imgsize.height / 2;
     return totalBytes + GetMipMapDataSize(new ImageSize(w, h));
 }
Exemple #7
0
        public byte[] DumpImage(ImageSize imgSize)
        {
            byte[] imgBuffer = null;

            ImageInfo imgInfo;
            if (imgList.Exists(img => (img.imgSize == imgSize && img.cprSize != -1)))
                imgInfo = imgList.Find(img => img.imgSize == imgSize);
            else
                //throw new FileNotFoundException("Image with resolution " + imgSize + " not found");
                return null;
            switch (imgInfo.storageType)
            {
                case storage.pccSto:
                    imgBuffer = new byte[imgInfo.uncSize];
                    Buffer.BlockCopy(imageData, imgInfo.offset, imgBuffer, 0, imgInfo.uncSize);
                    break;
                case storage.arcCpr:
                case storage.arcUnc:
                    string archivePath = FindFile();
                    if (String.IsNullOrEmpty(archivePath))
                        throw new FileNotFoundException();
                    PCCObject temp = new PCCObject(archivePath);
                    for (int i = 0; i < temp.ExportCount; i++)
                    {
                        if (String.Compare(texName, temp.Exports[i].ObjectName, true) == 0 && (temp.Exports[i].ClassName == "Texture2D"))// || temp.Exports[i].ClassName == "TextureFlipBook"))
                        {
                            Texture2D temptex = new Texture2D(temp, i);
                            /*if (imgSize.width > dims)
                            {
                                dims = (int) imgSize.width;*/
                                byte[] temp1 = temptex.DumpImage(imgSize);
                                if (temp1 != null)
                                    imgBuffer = temp1;
                            //}
                                
                        }
                    }
                    break;
                case storage.pccCpr:
                    using (MemoryStream ms = new MemoryStream(imageData))
                    {
                        SaltLZOHelper lzohelp = new SaltLZOHelper();
                        imgBuffer = lzohelp.DecompressTex(ms, imgInfo.offset, imgInfo.uncSize, imgInfo.cprSize);
                    }
                    break;
                default:
                    throw new FormatException("Unsupported texture storage type");
                    imgBuffer = null;
                    break;
            }
            
            return imgBuffer;
        }
 private long ImageMipMapDataSize(ImageSize imgsize, bool compressed, float BytesPerPixel)
 {
     uint w = imgsize.width;
     uint h = imgsize.height;
     if (compressed)
     {
         if (w < 4)
             w = 4;
         if (h < 4)
             h = 4;
     }
     long totalBytes = (long)((float)(w * h) * BytesPerPixel);
     w = imgsize.width;
     h = imgsize.height;
     if (w == 1 && h == 1)
         return totalBytes;
     if (w != 1)
         w = imgsize.width / 2;
     if (h != 1)
         h = imgsize.height / 2;
     return totalBytes + ImageMipMapDataSize(new ImageSize(w, h), compressed, BytesPerPixel);
 }
        public DDS(string fileName, ImageSize imgSize, string format, byte[] rawData)
            : base(fileName, imgSize, format)
        {
            DDSHeader.magic   = 0x20534444;
            DDSHeader.Size    = 0x7C;
            DDSHeader.Flags   = 0x081007;
            DDSHeader.Caps    = 0x1000;
            DDSHeader.pfFlags = 0x4;
            DDSHeader.pfSize  = 0x20;
            DDSHeader.Width   = imgSize.width;
            DDSHeader.Height  = imgSize.height;
            switch (format)
            {
            case "PF_DXT1":
            case "DXT1":
                DDSHeader.FourCC = (int)FourCC.DXT1;
                BPP = 0.5F;
                break;

            case "PF_DXT5":
            case "DXT5":
                DDSHeader.FourCC = (int)FourCC.DXT5;
                BPP = 1;
                break;

            case "PF_V8U8":
            case "V8U8":
                DDSHeader.Flags       = 0x001007;
                DDSHeader.pfFlags     = 0x80000;
                DDSHeader.RGBBitCount = 0x10;
                DDSHeader.RBitMask    = 0x0000FF;
                DDSHeader.GBitMask    = 0x00FF00;
                BPP = 2;
                break;

            case "ATI2":
            case "PF_NormalMap_HQ":
                DDSHeader.FourCC = (int)FourCC.ATI2;
                BPP = 1;
                break;

            case "PF_A8R8G8B8":
            case "A8R8G8B8":
            case "ARGB":
                BPP = 4;
                DDSHeader.pfFlags     = 0x41;
                DDSHeader.RGBBitCount = 0x20;
                DDSHeader.RBitMask    = 0xFF0000;
                DDSHeader.GBitMask    = 0xFF00;
                DDSHeader.BBitMask    = 0xFF;
                DDSHeader.ABitMask    = -16777216;
                break;

            case "PF_R8G8B8":
            case "R8G8B8":
                BPP = 3;
                DDSHeader.pfFlags     = 0x40;
                DDSHeader.RGBBitCount = 0x18;
                DDSHeader.RBitMask    = 0xFF0000;
                DDSHeader.GBitMask    = 0xFF00;
                DDSHeader.BBitMask    = 0xFF;
                DDSHeader.ABitMask    = 0x0;
                break;

            case "PF_G8":
            case "G8":
            case "G8_L8":
                BPP = 1;
                DDSHeader.pfFlags     = 0x20000;
                DDSHeader.RGBBitCount = 0x8;
                DDSHeader.RBitMask    = 0xFF;
                break;

            default: throw new FormatException("Invalid DDS format");
            }
            imgData = rawData;

            // convert DDSHeader to byte array
            int headSize = Marshal.SizeOf(DDSHeader);

            headData = new byte[headSize];
            IntPtr ptr = Marshal.AllocHGlobal(headSize);

            Marshal.StructureToPtr(DDSHeader, ptr, true);
            Marshal.Copy(ptr, headData, 0, headData.Length);
            Marshal.FreeHGlobal(ptr);
        }
        public TGA(string fileName, ImageSize imgSize, string format, byte[] rawData) // used when extracting raw data from me3 archive
            : base(fileName, imgSize, format)
        {
            TGAHeader.imagetype = 0x2; // RGB Image type
            TGAHeader.bits      = (byte)(rawData.Length / (imgSize.width * imgSize.height) * 8);
            TGAHeader.width     = (short)imgSize.width;
            TGAHeader.height    = (short)imgSize.height;
            //TGAHeader.colourmapbits = 32;
            TGAHeader.descriptor = 0x00; // 0x20 = flips the image vertically
            BPP = TGAHeader.bits / 8;

            switch (format)
            {
            case "PF_G8":
            case "G8":
            {
                imgData = new byte[rawData.Length];
                for (int i = 0; i < imgSize.height; i++)
                {
                    for (int j = 0; j < imgSize.width; j++)
                    {
                        imgData[(imgSize.width * i) + j] = rawData[(imgSize.width * i) + j];
                    }
                }
                TGAHeader.imagetype = 0x3;
            }
            break;

            case "PF_A8R8G8B8":
            case "A8R8G8B8":
            {
                TGAHeader.descriptor |= 0x08;         // 0x08 = 8-bit alpha bits
                imgData = new byte[rawData.Length];
                int intBPP = (int)BPP;

                for (int i = 0; i < imgSize.height; i++)
                {
                    for (int j = 0; j < imgSize.width; j++)
                    {
                        for (int k = 0; k < intBPP; k++)
                        {
                            imgData[(intBPP * imgSize.width * (imgSize.height - 1 - i)) + (j * intBPP) + k] = rawData[(imgSize.width * intBPP * i) + (j * intBPP) + k];
                        }

                        /*imgData[(4 * imgSize.width * (imgSize.height - 1 - i)) + (j * 4) + 1] = rawData[(imgSize.width * 4 * i) + (j * 4) + 1];
                        *  imgData[(4 * imgSize.width * (imgSize.height - 1 - i)) + (j * 4) + 2] = rawData[(imgSize.width * 4 * i) + (j * 4) + 2];
                        *  imgData[(4 * imgSize.width * (imgSize.height - 1 - i)) + (j * 4) + 3] = rawData[(imgSize.width * 4 * i) + (j * 4) + 3];*/
                    }
                }
                verticalFlipped = true;
                flipVertically();
            }
            break;

            default: throw new FormatException("Invalid TGA format");
            }

            // convert TGAHeader to byte array
            int headSize = Marshal.SizeOf(TGAHeader);

            headData = new byte[headSize];
            IntPtr ptr = Marshal.AllocHGlobal(headSize);

            Marshal.StructureToPtr(TGAHeader, ptr, true);
            Marshal.Copy(ptr, headData, 0, headData.Length);
            Marshal.FreeHGlobal(ptr);
        }
 protected ImageFile(string fileName, ImageSize imgSize, string format)
 {
     this.fileName = fileName;
     this.imgSize  = imgSize;
     this.format   = format;
 }
        public ImageMipMapHandler(string imageWithMipMaps, byte[] data)
        {
            imageWithMipMaps = (imageWithMipMaps == "") ? ".dds" : imageWithMipMaps;
            imageList        = new List <ImageFile>();
            string    fileFormat = Path.GetExtension(imageWithMipMaps).ToLowerInvariant();
            string    fileName   = Path.GetFileNameWithoutExtension(imageWithMipMaps);
            ImageFile imageMipMap;
            int       headerSize;

            if (data != null)
            {
                headerSize  = 128;
                imageMipMap = new DDS("", data);
            }
            else
            {
                switch (fileFormat)
                {
                case ".dds":
                    headerSize  = 128;
                    imageMipMap = new DDS(imageWithMipMaps, null);
                    break;

                case ".tga":
                    headerSize  = 18;
                    imageMipMap = new TGA(imageWithMipMaps, null);
                    break;

                default: throw new FormatException("Invalid image format");
                }
            }

            //Console.WriteLine("Image Format: {0}", imageMipMap.format);

            //check if image has mipmaps

            /* using (FileStream imageStream = File.OpenRead(imageWithMipMaps))
             * {
             *  long size = ImageMipMapDataSize(imageMipMap.imgSize, CprFormat(imageMipMap.format), imageMipMap.BPP);
             *  if (imageStream.Length - headerSize != ImageMipMapDataSize(imageMipMap.imgSize, CprFormat(imageMipMap.format), imageMipMap.BPP))
             *  {
             *      //MessageBox.Show("bytes in file: " + (imageStream.Length - headerSize) + ", bytes calulated: " + ImageMipMapDataSize(imageMipMap.imgSize, imageMipMap.format, imageMipMap.BPP) + ", BPP: " + imageMipMap.BPP + ", format: " + imageMipMap.format);
             *      //Console.WriteLine("bytes in file: {0}, bytes calulated: {1}, BPP: {2}", imageStream.Length - headerSize, ImageMipMapDataSize(imageMipMap.imgSize, imageMipMap.format, imageMipMap.BPP), imageMipMap.BPP);
             *      throw new FormatException("The image doesn't have any mipmaps");
             *  }
             * } */



            // KFreon: This can be wrong too much. Single mip images etc.

            /*
             * var size = ImageMipMapDataSize(imageMipMap.imgSize, CprFormat(imageMipMap.format), imageMipMap.BPP);
             * if (imageMipMap.imgData.Length != size)
             *  throw new FormatException("The image doesn't have any mipmaps");*/

            byte[] buffer = null;

            // add the first tga image
            if (fileFormat == ".tga")
            {
                //buffer = new byte[imageMipMap.imgData.Length];
                buffer = imageMipMap.imgData;
                //imageList.Add(imageMipMap);
            }

            // Heff: Use Max to support 1x1 mips for 1:2 ratio images.
            int maxCount   = (int)Math.Max(imageMipMap.imgSize.width, imageMipMap.imgSize.height);
            int count      = 1;
            int imgDataPos = 0;

            while (count <= maxCount)
            {
                ImageFile newImageFile;
                ImageSize newImageSize = imageMipMap.imgSize / count;
                //if (newImageSize.width < 4 || newImageSize.height < 4)
                //    break;
                int imgDataSize = (int)ImageDataSize(newImageSize, imageMipMap.format, imageMipMap.BPP);

                if (fileFormat == ".dds")
                {
                    buffer = new byte[imgDataSize];
                    Buffer.BlockCopy(imageMipMap.imgData, imgDataPos, buffer, 0, imgDataSize);
                    imgDataPos += imgDataSize;

                    if (imageMipMap.format == "R8G8B8") // Automatic conversion to 32-bit
                    {
                        buffer       = ConvertTo32bit(buffer, (int)newImageSize.width, (int)newImageSize.height);
                        newImageFile = new DDS(fileName + "_" + newImageSize + fileFormat, newImageSize, "A8R8G8B8", buffer);
                    }
                    else
                    {
                        newImageFile = new DDS(fileName + "_" + newImageSize + fileFormat, newImageSize, imageMipMap.format, buffer);
                    }
                }
                else if (fileFormat == ".tga")
                {
                    newImageFile = new TGA(fileName + "_" + newImageSize + fileFormat, newImageSize, imageMipMap.format, buffer);
                    if (newImageSize != new ImageSize(1, 1))
                    {
                        buffer = ShrinkImage(buffer, newImageSize, imageMipMap.BPP);
                    }
                }
                else
                {
                    throw new FormatException("Invalid image format");
                }

                imageList.Add(newImageFile);

                count *= 2;
            }
        }
Exemple #13
0
 public byte[] DumpImg(ImageSize imageSize, string ArcPath)
 {
     throw new NotImplementedException();
 }
Exemple #14
0
 public byte[] DumpImg(ImageSize imageSize, string ArcPath)
 {
     return DumpImage(imageSize, ArcPath);
 }
 protected ImageFile(string fileName, ImageSize imgSize, string format)
 {
     this.fileName = fileName;
     this.imgSize = imgSize;
     this.format = format;
 }
        private long ImageDataSize(ImageSize imgsize, string format, float BytesPerPixel)
        {
            uint w = imgsize.width;
            uint h = imgsize.height;
            if (CprFormat(format))
            {
                if (w < 4)
                    w = 4;
                if (h < 4)
                    h = 4;
            }
            return (long)((float)(w * h) * BytesPerPixel);

            /*long totalBytes = (long)((float)(imgsize.width * imgsize.height) * BytesPerPixel);
            switch (format)
            {
                case "DXT1":
                    if (imgsize.width <= 4 && imgsize.height <= 4)
                        return 8;
                    else
                        return totalBytes;
                case "ATI2":
                case "DXT5":
                    if (imgsize.width <= 4 && imgsize.height <= 4)
                        return 16;
                    else
                        return totalBytes;
                case "V8U8":
                    if (imgsize.width <= 4 && imgsize.height <= 4)
                        return 32;
                    else
                        return totalBytes;
                case "G8":
                case "R8G8B8":
                case "A8R8G8B8":
                    return totalBytes;
                default:
                    throw new FormatException("Invalid image format");
            } */
        }
 public static long ImageDataSize(ImageSize imgsize, string format, float BytesPerPixel)
 {
     uint w = imgsize.width;
     uint h = imgsize.height;
     if (CprFormat(format))
     {
         if (w < 4)
             w = 4;
         if (h < 4)
             h = 4;
     }
     return (long)((float)(w * h) * BytesPerPixel);
 }
        private byte[] ShrinkImage(byte[] imageData, ImageSize imgsize, float BytesPerPixel)
        {
            //Console.WriteLine("image length: {0}, image size: {1}, BPP: {2}", imageData.Length, imgsize, BytesPerPixel);
            byte[] final = new byte[imageData.Length / 4];
            int BPP = (int)BytesPerPixel;

            for (int i = 0; i < imgsize.height; i += 2)
            {
                for (int j = 0; j < imgsize.width; j += 2)
                {
                    for (int k = 0; k < BPP; k++)
                    {
                        final[(i * (imgsize.width * BPP) / 4) + j * BPP / 2 + k] = (byte)
                          (((int)imageData[i * (imgsize.width * BPP) + j * BPP + k] +
                            (int)imageData[i * (imgsize.width * BPP) + j * BPP + BPP + k] +
                            (int)imageData[(i + 1) * (imgsize.width * BPP) + j * BPP + k] +
                            (int)imageData[(i + 1) * (imgsize.width * BPP) + j * BPP + BPP + k]) / 4);
                    }
                }
            }

            return final;
        }
        public DDS(string fileName, ImageSize imgSize, string format, byte[] rawData)
            : base(fileName, imgSize, format)
        {
            DDSHeader.magic = 0x20534444;
            DDSHeader.Size = 0x7C;
            DDSHeader.Flags = 0x081007;
            DDSHeader.Caps = 0x1000;
            DDSHeader.pfFlags = 0x4;
            DDSHeader.pfSize = 0x20;
            DDSHeader.Width = imgSize.width;
            DDSHeader.Height = imgSize.height;
            switch (format)
            {
                case "PF_DXT1":
                case "DXT1":
                    DDSHeader.FourCC = (int)FourCC.DXT1;
                    BPP = 0.5F;
                    break;
                case "PF_DXT5":
                case "DXT5":
                    DDSHeader.FourCC = (int)FourCC.DXT5;
                    BPP = 1;
                    break;
                case "PF_V8U8":
                case "V8U8":
                    DDSHeader.Flags = 0x001007;
                    DDSHeader.pfFlags = 0x80000;
                    DDSHeader.RGBBitCount = 0x10;
                    DDSHeader.RBitMask = 0x0000FF;
                    DDSHeader.GBitMask = 0x00FF00;
                    BPP = 2;
                    break;
                case "ATI2":
                case "PF_NormalMap_HQ":
                    DDSHeader.FourCC = (int)FourCC.ATI2;
                    BPP = 1;
                    break;
                case "PF_A8R8G8B8":
                case "A8R8G8B8":
                    BPP = 4;
                    DDSHeader.pfFlags = 0x41;
                    DDSHeader.RGBBitCount = 0x20;
                    DDSHeader.RBitMask = 0xFF0000;
                    DDSHeader.GBitMask = 0xFF00;
                    DDSHeader.BBitMask = 0xFF;
                    DDSHeader.ABitMask = -16777216;
                    break;
                case "PF_R8G8B8":
                case "R8G8B8":
                    BPP = 3;
                    DDSHeader.pfFlags = 0x40;
                    DDSHeader.RGBBitCount = 0x18;
                    DDSHeader.RBitMask = 0xFF0000;
                    DDSHeader.GBitMask = 0xFF00;
                    DDSHeader.BBitMask = 0xFF;
                    DDSHeader.ABitMask = 0x0;
                    break;
                case "PF_G8":
                case "G8":
                    BPP = 1;
                    DDSHeader.pfFlags = 0x20000;
                    DDSHeader.RGBBitCount = 0x8;
                    DDSHeader.RBitMask = 0xFF;
                    break;
                default: throw new FormatException("Invalid DDS format");
            }
            imgData = rawData;

            // convert DDSHeader to byte array
            int headSize = Marshal.SizeOf(DDSHeader);
            headData = new byte[headSize];
            IntPtr ptr = Marshal.AllocHGlobal(headSize);
            Marshal.StructureToPtr(DDSHeader, ptr, true);
            Marshal.Copy(ptr, headData, 0, headData.Length);
            Marshal.FreeHGlobal(ptr);
        }
        /// <summary>
        /// Gets the raw image data, mainly for use with AK's image displayer
        /// </summary>
        /// <param name="strImgSize"></param>
        /// <param name="archiveDir"></param>
        /// <returns></returns>
        public byte[] DumpImg(ImageSize imgSize, string archiveDir)
        {
            byte[] imgBuff;

            ImageInfo imgInfo;
            if (privateimgList.Exists(img => img.imgSize == imgSize))
                imgInfo = privateimgList.Find(img => img.imgSize == imgSize);
            else
                throw new FileNotFoundException("Image with resolution " + imgSize + " not found");

            switch (imgInfo.storageType)
            {
                case storage.pccSto:
                    imgBuff = new byte[imgInfo.uncSize];
                    Buffer.BlockCopy(imageData, imgInfo.offset, imgBuff, 0, imgInfo.uncSize);
                    break;
                case storage.arcCpr:
                case storage.arcUnc:
                    //string archivePath = archiveDir + "\\" + arcName + ".tfc";
                    string archivePath = GetTexArchive(archiveDir);

                    if (!File.Exists(archivePath))
                        throw new FileNotFoundException("Texture archive not found in " + archivePath);

                    using (FileStream archiveStream = File.OpenRead(archivePath))
                    {
                        archiveStream.Seek(imgInfo.offset, SeekOrigin.Begin);
                        if (imgInfo.storageType == storage.arcCpr)
                        {
                            imgBuff = ZBlock.Decompress(archiveStream, imgInfo.cprSize);
                        }
                        else
                        {
                            imgBuff = new byte[imgInfo.uncSize];
                            archiveStream.Read(imgBuff, 0, imgBuff.Length);
                        }
                    }
                    break;
                default:
                    throw new FormatException("Unsupported texture storage type");
            }

            return imgBuff;
        }
Exemple #21
0
        public byte[] DumpImage(ImageSize imgSize, string archiveDir)
        {
            byte[] imgBuff = null;

            ImageInfo imgInfo;
            if (imgList.Exists(img => img.imgSize == imgSize))
                imgInfo = imgList.Find(img => img.imgSize == imgSize);
            else
                throw new FileNotFoundException("Image with resolution " + imgSize + " not found");

            switch (imgInfo.storageType)
            {
                case storage.pccSto:
                    imgBuff = new byte[imgInfo.uncSize];
                    Buffer.BlockCopy(imageData, imgInfo.offset, imgBuff, 0, imgInfo.uncSize);
                    break;
                case storage.arcCpr:
                case storage.arcUnc:
                    string archivePath = FullArcPath;
                    if (String.IsNullOrEmpty(archivePath))
                        archivePath = GetTexArchive(archiveDir);
                    if (!File.Exists(archivePath))
                        throw new FileNotFoundException("Texture archive not found in " + archivePath);

                    using (FileStream archiveStream = File.OpenRead(archivePath))
                    {
                        if (imgInfo.storageType == storage.arcCpr)
                        {
                            SaltLZOHelper lzohelp = new SaltLZOHelper();
                            imgBuff = lzohelp.DecompressTex(archiveStream, imgInfo.offset, imgInfo.uncSize, imgInfo.cprSize);
                        }
                        else
                        {
                            archiveStream.Seek(imgInfo.offset, SeekOrigin.Begin);
                            imgBuff = new byte[imgInfo.uncSize];
                            archiveStream.Read(imgBuff, 0, imgBuff.Length);
                        }
                    }
                    break;
                default:
                    throw new FormatException("Unsupported texture storage type");
            }
            return imgBuff;
        }