CompressTex() public method

Takes regular image data and returns it in a compressed form ready for archiving
public CompressTex ( byte data ) : byte[]
data byte
return byte[]
コード例 #1
0
ファイル: ME2Texture2D.cs プロジェクト: Dybuk/ME3Explorer
        public void replaceImage(string strImgSize, ImageFile im, string archiveDir)
        {
            ImageSize imgSize = ImageSize.stringToSize(strImgSize);
            if (!privateimgList.Exists(img => img.imgSize == imgSize))
                throw new FileNotFoundException("Image with resolution " + imgSize + " isn't found");

            int imageIdx = privateimgList.FindIndex(img => img.imgSize == imgSize);
            ImageInfo imgInfo = privateimgList[imageIdx];

            ImageFile imgFile = im;

            if (imgFile.imgSize.height != imgInfo.imgSize.height || imgFile.imgSize.width != imgInfo.imgSize.width)
                throw new FormatException("Incorrect input texture dimensions. Expected: " + imgInfo.imgSize.ToString());

            if (!Methods.CheckTextureFormat(texFormat, imgFile.format))
                throw new FormatException("Different image format, original is " + texFormat + ", new is " + imgFile.subtype());

            byte[] imgBuffer;

            // if the image is empty then recover the archive compression from the image list
            if (imgInfo.storageType == storage.empty)
            {
                imgInfo.storageType = privateimgList.Find(img => img.storageType != storage.empty && img.storageType != storage.pccSto).storageType;
                imgInfo.uncSize = imgFile.resize().Length;
                imgInfo.cprSize = imgFile.resize().Length;
            }

            // overwrite previous choices for specific cases
            if (properties.ContainsKey("NeverStream") && properties["NeverStream"].Value.IntValue == 1)
                imgInfo.storageType = storage.pccSto;

            switch (imgInfo.storageType)
            {
                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);

                    if (getFileFormat() == ".tga")
                        imgBuffer = imgFile.resize(); // shrink image to essential data
                    else
                        imgBuffer = imgFile.imgData;

                    if (imgBuffer.Length != imgInfo.uncSize)
                        throw new FormatException("image sizes do not match, original is " + imgInfo.uncSize + ", new is " + imgBuffer.Length);

                    if (arcName.Length <= CustCache.Length || arcName.Substring(0, CustCache.Length) != CustCache) // Check whether existing texture is in a custom cache
                    {
                        ChooseNewCache(archiveDir, imgBuffer.Length);
                        archivePath = FullArcPath;
                    }
                    else
                    {
                        FileInfo arc = new FileInfo(archivePath);
                        if (arc.Length + imgBuffer.Length >= 0x80000000)
                        {
                            ChooseNewCache(archiveDir, imgBuffer.Length);
                            archivePath = FullArcPath;
                        }
                    }

                    using (FileStream archiveStream = new FileStream(archivePath, FileMode.Append, FileAccess.Write))
                    {
                        int newOffset = (int)archiveStream.Position;
                        //archiveStream.Position = imgInfo.offset;
                        if (imgInfo.storageType == storage.arcCpr)
                        {
                            byte[] tempBuff;
                            SaltLZOHelper lzohelper = new SaltLZOHelper();
                            tempBuff = lzohelper.CompressTex(imgBuffer);
                            imgBuffer = new byte[tempBuff.Length];
                            Buffer.BlockCopy(tempBuff, 0, imgBuffer, 0, tempBuff.Length);
                            imgInfo.cprSize = imgBuffer.Length;
                        }
                        //else
                        archiveStream.Write(imgBuffer, 0, imgBuffer.Length);

                        imgInfo.offset = newOffset;
                    }
                    break;
                case storage.pccSto:
                    //imgBuffer = imgFile.imgData; // copy image data as-is
                    imgBuffer = imgFile.resize();
                    using (MemoryStream dataStream = new MemoryStream())
                    {
                        dataStream.WriteBytes(imageData);
                        if (imgBuffer.Length <= imgInfo.uncSize && imgInfo.offset > 0)
                            dataStream.Seek(imgInfo.offset, SeekOrigin.Begin);
                        else
                            imgInfo.offset = (int)dataStream.Position;
                        dataStream.WriteBytes(imgBuffer);
                        imgInfo.cprSize = imgBuffer.Length;
                        imgInfo.uncSize = imgBuffer.Length;
                        imageData = dataStream.ToArray();
                    }
                    break;
            }

            privateimgList[imageIdx] = imgInfo;
        }
コード例 #2
0
ファイル: ME2Texture2D.cs プロジェクト: Dybuk/ME3Explorer
        public void replaceImage2(string strImgSize, ImageFile im, string archiveDir)
        {
            ImageSize imgSize = ImageSize.stringToSize(strImgSize);
            if (!privateimgList.Exists(img => img.imgSize == imgSize))
                throw new FileNotFoundException("Image with resolution " + imgSize + " isn't found");

            int imageIdx = privateimgList.FindIndex(img => img.imgSize == imgSize);
            ImageInfo imgInfo = privateimgList[imageIdx];

            // check if replacing image is supported
            ImageFile imgFile = im;

            if (!Methods.CheckTextureFormat(texFormat, imgFile.format))
                throw new FormatException("Different image format, original is " + texFormat + ", new is " + imgFile.subtype());

            byte[] imgBuffer;

            // if the image is empty then recover the archive compression from the image list
            if (imgInfo.storageType == storage.empty)
            {
                imgInfo.storageType = privateimgList.Find(img => img.storageType != storage.empty && img.storageType != storage.pccSto).storageType;
                imgInfo.uncSize = imgFile.resize().Length;
                imgInfo.cprSize = imgFile.resize().Length;
            }

            switch (imgInfo.storageType)
            {
                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);

                    imgBuffer = imgFile.imgData;

                    if (imgBuffer.Length != imgInfo.uncSize)
                        throw new FormatException("image sizes do not match, original is " + imgInfo.uncSize + ", new is " + imgBuffer.Length);

                    if (arcName.Length <= CustCache.Length || arcName.Substring(0, CustCache.Length) != CustCache) // Check whether existing texture is in a custom cache
                    {
                        ChooseNewCache(archiveDir, imgBuffer.Length);
                        archivePath = FullArcPath;
                    }
                    else
                    {
                        FileInfo arc = new FileInfo(archivePath);
                        if (arc.Length + imgBuffer.Length >= 0x80000000)
                        {
                            ChooseNewCache(archiveDir, imgBuffer.Length);
                            archivePath = FullArcPath;
                        }
                    }

                    using (FileStream archiveStream = new FileStream(archivePath, FileMode.Append, FileAccess.Write))
                    {
                        int newOffset = (int)archiveStream.Position;
                        if (imgInfo.storageType == storage.arcCpr)
                        {
                            byte[] tempBuff;
                            SaltLZOHelper lzohelper = new SaltLZOHelper();
                            tempBuff = lzohelper.CompressTex(imgBuffer);
                            imgBuffer = new byte[tempBuff.Length];
                            Buffer.BlockCopy(tempBuff, 0, imgBuffer, 0, tempBuff.Length);
                            imgInfo.cprSize = imgBuffer.Length;
                        }
                        archiveStream.Write(imgBuffer, 0, imgBuffer.Length);

                        imgInfo.offset = newOffset;
                    }
                    break;
                case storage.pccSto:
                    imgBuffer = imgFile.resize();
                    using (MemoryStream dataStream = new MemoryStream())
                    {
                        dataStream.WriteBytes(imageData);
                        if (imgBuffer.Length <= imgInfo.uncSize && imgInfo.offset > 0)
                            dataStream.Seek(imgInfo.offset, SeekOrigin.Begin);
                        else
                            imgInfo.offset = (int)dataStream.Position;
                        dataStream.WriteBytes(imgBuffer);
                        imgInfo.cprSize = imgBuffer.Length;
                        imgInfo.uncSize = imgBuffer.Length;
                        imageData = dataStream.ToArray();
                    }
                    break;
            }

            privateimgList[imageIdx] = imgInfo;
        }
コード例 #3
0
ファイル: ME1Texture2D.cs プロジェクト: Dybuk/ME3Explorer
        public void addMissingImage(String strImgSize, string fileToReplace)
        {
            if (privateimgList.Count == 1)
                throw new Exception("The imglist must contain more than 1 texture");

            ImageSize imgSize = ImageSize.stringToSize(strImgSize);
            if (privateimgList.Exists(img => img.imgSize.width == imgSize.width && img.imgSize.height == imgSize.height))
                throw new Exception("The img already exists in the list");
            ImageInfo tempImg = privateimgList.Last();

            if (!File.Exists(fileToReplace))
                throw new FileNotFoundException("Required file was not found");

            ImageFile dds = new DDS(fileToReplace, null);

            if (dds.imgSize.width != imgSize.width || dds.imgSize.height != imgSize.height)
                throw new FormatException("Input texture is not required size");

            if (dds.format == "R8G8B8")
            {
                byte[] buff = ImageMipMapHandler.ConvertTo32bit(dds.imgData, (int)dds.imgSize.width, (int)dds.imgSize.height);
                dds = new DDS(null, dds.imgSize, "A8R8G8B8", buff);
            }

            if (texFormat == "PF_NormalMap_HQ")
            {
                if (dds.format != "ATI2")
                    throw new FormatException("Input texture is the wrong format");
            }
            else if (String.Compare(texFormat, "PF_" + dds.format, true) != 0 && String.Compare(texFormat, dds.format, true) != 0)
                throw new FormatException("Input texture is the wrong format");

            ImageInfo newImg = new ImageInfo();

            if (tempImg.storageType == storage.empty || tempImg.storageType == storage.arcCpr || tempImg.storageType == storage.arcUnc)
                throw new FormatException("Existing textures cannot be empty or externally stored");
            newImg.storageType = tempImg.storageType;
            newImg.imgSize = imgSize;

            using (MemoryStream ms = new MemoryStream())
            {
                ms.WriteBytes(imageData);
                newImg.offset = (int)ms.Position;
                switch (newImg.storageType)
                {
                    case storage.pccSto:
                        ms.WriteBytes(dds.resize());
                        //newImg.cprSize = dds.imgData.Length;
                        //newImg.uncSize = dds.imgData.Length;
                        newImg.cprSize = dds.resize().Length;
                        newImg.uncSize = dds.resize().Length;
                        break;
                    case storage.pccCpr:
                        SaltLZOHelper lzohelper = new SaltLZOHelper();
                        //byte[] buff = lzohelper.CompressTex(dds.imgData);
                        byte[] buff = lzohelper.CompressTex(dds.resize());
                        ms.WriteBytes(buff);
                        //newImg.uncSize = dds.imgData.Length;
                        newImg.uncSize = dds.resize().Length;
                        newImg.cprSize = buff.Length;
                        break;
                }
                imageData = ms.ToArray();
            }

            int i = 0;
            for (; i < privateimgList.Count; i++)
            {
                if (privateimgList[i].imgSize.width > imgSize.width)
                    continue;

                privateimgList.Insert(i, newImg);
                return;
            }
            privateimgList.Insert(i, newImg);

            if (ImageMipMapHandler.CprFormat(dds.format) && (newImg.imgSize.width < 4 || newImg.imgSize.height < 4))
            {
                newImg = privateimgList[i];
                if (newImg.imgSize.width < 4 && newImg.imgSize.height > 4)
                {
                    newImg.imgSize = new ImageSize(4, newImg.imgSize.height);
                }
                else if (newImg.imgSize.width > 4 && newImg.imgSize.height < 4)
                {
                    newImg.imgSize = new ImageSize(newImg.imgSize.width, 4);
                }
                else if (newImg.imgSize.width < 4 && newImg.imgSize.height < 4)
                {
                    newImg.imgSize = new ImageSize(4, 4);
                }
                else
                    throw new Exception("safety catch");
                privateimgList[i] = newImg;
            }
            //throw new Exception("Newimg wasn't inserted in list!");
        }
コード例 #4
0
ファイル: ME1Texture2D.cs プロジェクト: Dybuk/ME3Explorer
        public void ReplaceImage(ImageFile ddsfile)
        {
            ImageSize imgSize = ddsfile.imgSize;

            int imageIdx = privateimgList.FindIndex(img => img.imgSize == imgSize);
            ImageInfo imgInfo = privateimgList[imageIdx];

            if (imgInfo.storageType == storage.empty && imgInfo.imgSize.width > privateimgList.First(img => img.storageType != storage.empty).imgSize.width)
                imgInfo.storageType = privateimgList.First(img => img.storageType != storage.empty).storageType;
            else if (imgInfo.storageType == storage.empty)
                imgInfo.storageType = privateimgList.Last(img => img.storageType != storage.empty).storageType;
            if (imgInfo.storageType == storage.arcCpr || imgInfo.storageType == storage.arcUnc)
                throw new FormatException("Replacement of externally stored textures is not allowed");
            else if (imgInfo.storageType == storage.empty)
                throw new FormatException("Cannot replace images with empty image lists");

            byte[] imgBuff = ddsfile.resize();

            using (MemoryStream ms = new MemoryStream())
            {
                ms.WriteBytes(imageData);
                imgInfo.uncSize = imgBuff.Length;
                if ((long)imgInfo.uncSize != ImageFile.ImageDataSize(ddsfile.imgSize, ddsfile.format, ddsfile.BPP))
                    throw new FormatException("Input texture not correct length!");

                if (imgInfo.storageType == storage.pccCpr)
                {
                    SaltLZOHelper lzo = new SaltLZOHelper();
                    imgBuff = lzo.CompressTex(imgBuff);
                }
                if (imgBuff.Length <= imgInfo.cprSize && imgInfo.offset > 0)
                    ms.Seek(imgInfo.offset, SeekOrigin.Begin);
                else
                    imgInfo.offset = (int)ms.Position;
                imgInfo.cprSize = imgBuff.Length;
                ms.WriteBytes(imgBuff);
                imageData = ms.ToArray();
            }
            privateimgList[imageIdx] = imgInfo;
        }
コード例 #5
0
ファイル: ME1Texture2D.cs プロジェクト: Dybuk/ME3Explorer
        public void replaceImage(string strImgSize, ImageFile im, bool enforceSize, string archiveDir = null)
        {
            ImageSize imgSize = ImageSize.stringToSize(strImgSize);
            if (!privateimgList.Exists(img => img.imgSize == imgSize))
                throw new FileNotFoundException("Image with resolution " + imgSize + " isn't found");

            int imageIdx = privateimgList.FindIndex(img => img.imgSize == imgSize);
            ImageInfo imgInfo = privateimgList[imageIdx];

            // check if replacing image is supported
            ImageFile imgFile = im;

            // Heff: Made this check optional to allow for replacing with larger images.
            if (enforceSize && (imgFile.imgSize.height != imgInfo.imgSize.height || imgFile.imgSize.width != imgInfo.imgSize.width))
                throw new FormatException("Incorrect input texture dimensions. Expected: " + imgInfo.imgSize.ToString());

            // check if images have same format type
            if (!Methods.CheckTextureFormat(texFormat, imgFile.format))
                throw new FormatException("Different image format, original is " + texFormat + ", new is " + imgFile.subtype());

            byte[] imgBuffer;

            // if the image is empty then recover the archive compression from the image list
            if (imgInfo.storageType == storage.empty)
            {
                imgInfo.storageType = privateimgList.Find(img => img.storageType != storage.empty && img.storageType != storage.pccSto).storageType;
                imgInfo.uncSize = imgFile.resize().Length;
                imgInfo.cprSize = imgFile.resize().Length;
            }

            switch (imgInfo.storageType)
            {
                case storage.arcCpr:
                case storage.arcUnc:
                    throw new NotImplementedException("Texture replacement not supported in external packages yet");
                case storage.pccSto:
                    imgBuffer = imgFile.resize();
                    using (MemoryStream dataStream = new MemoryStream())
                    {
                        dataStream.WriteBytes(imageData);
                        if (imgBuffer.Length <= imgInfo.uncSize && imgInfo.offset > 0)
                            dataStream.Seek(imgInfo.offset, SeekOrigin.Begin);
                        else
                            imgInfo.offset = (int)dataStream.Position;
                        dataStream.WriteBytes(imgBuffer);
                        imgInfo.cprSize = imgBuffer.Length;
                        imgInfo.uncSize = imgBuffer.Length;
                        imageData = dataStream.ToArray();
                    }
                    break;
                case storage.pccCpr:
                    using (MemoryStream dataStream = new MemoryStream())
                    {
                        dataStream.WriteBytes(imageData);
                        SaltLZOHelper lzohelper = new SaltLZOHelper();
                        imgBuffer = lzohelper.CompressTex(imgFile.resize());
                        if (imgBuffer.Length <= imgInfo.cprSize && imgInfo.offset > 0)
                            dataStream.Seek(imgInfo.offset, SeekOrigin.Begin);
                        else
                            imgInfo.offset = (int)dataStream.Position;
                        dataStream.WriteBytes(imgBuffer);
                        imgInfo.cprSize = imgBuffer.Length;
                        imgInfo.uncSize = imgFile.resize().Length;
                        imageData = dataStream.ToArray();
                    }
                    break;
            }

            privateimgList[imageIdx] = imgInfo;
        }
コード例 #6
0
ファイル: ME1Texture2D.cs プロジェクト: Dybuk/ME3Explorer
        public void HardReplaceImage(ImageFile ddsfile)
        {
            ImageSize imgSize = ddsfile.imgSize;

            if (ddsfile.format == "R8G8B8")
            {
                byte[] buff = ImageMipMapHandler.ConvertTo32bit(ddsfile.imgData, (int)ddsfile.imgSize.width, (int)ddsfile.imgSize.height);
                ddsfile = new DDS(null, ddsfile.imgSize, "A8R8G8B8", buff);
            }

            ImageInfo newImg = new ImageInfo();
            newImg.storageType = privateimgList[0].storageType;
            if (newImg.storageType == storage.empty || newImg.storageType == storage.arcCpr || newImg.storageType == storage.arcUnc)
                throw new FormatException("Original texture cannot be empty or externally stored");

            newImg.offset = 0;
            newImg.imgSize = imgSize;
            imageData = ddsfile.resize();
            newImg.uncSize = imageData.Length;
            if ((long)newImg.uncSize != ImageFile.ImageDataSize(imgSize, ddsfile.format, ddsfile.BPP))
                throw new FormatException("Input texture not correct length!");

            switch (newImg.storageType)
            {
                case storage.pccSto:
                    newImg.cprSize = imageData.Length;
                    break;
                case storage.pccCpr:
                    SaltLZOHelper lzohelper = new SaltLZOHelper();
                    imageData = lzohelper.CompressTex(imageData);
                    newImg.cprSize = imageData.Length;
                    break;
            }

            privateimgList.RemoveAt(0);
            privateimgList.Add(newImg);

            // Fix up properties
            properties["SizeX"].Value.IntValue = (int)imgSize.width;
            properties["SizeY"].Value.IntValue = (int)imgSize.height;
        }
コード例 #7
0
ファイル: ME1Texture2D.cs プロジェクト: Dybuk/ME3Explorer
        public void HardReplaceImage(string strImgSize, string fileToReplace)
        {
            if (!File.Exists(fileToReplace))
                throw new FileNotFoundException("Required file was not found");

            ImageFile dds = new DDS(fileToReplace, null);

            if (strImgSize == null)
                strImgSize = dds.imgSize.ToString();

            if (privateimgList.Count != 1)
                throw new Exception("Cannot use this function for a multi-level texture");

            ImageSize imgSize = ImageSize.stringToSize(strImgSize);
            // Check for correct dimensions/ size
            if (imgSize.width == privateimgList[0].imgSize.width)
            {
                if (imgSize.height != privateimgList[0].imgSize.height)
                    throw new FormatException("The input size is not the correct dimensions (Error: 0)");
            }
            else if (imgSize.width < privateimgList[0].imgSize.width)
            {
                if (privateimgList[0].imgSize.width / imgSize.width != privateimgList[0].imgSize.height / imgSize.height || (privateimgList[0].imgSize.width / imgSize.width) % 2 != 0)
                    throw new FormatException("The input size is not the correct size (Error: 1)");
            }
            else
            {
                if (imgSize.width / privateimgList[0].imgSize.width != imgSize.height / privateimgList[0].imgSize.height || (imgSize.width / privateimgList[0].imgSize.width) % 2 != 0)
                    throw new FormatException("The input size is not the correct size (Error: 2)");
            }

            if (dds.format == "R8G8B8")
            {
                byte[] buff = ImageMipMapHandler.ConvertTo32bit(dds.imgData, (int)dds.imgSize.width, (int)dds.imgSize.height);
                dds = new DDS(null, dds.imgSize, "A8R8G8B8", buff);
            }

            if (Class == class2 || Class == class3)
                ChangeFormat(dds.format);

            if (texFormat == "PF_NormalMap_HQ")
            {
                if (dds.format != "ATI2")
                    throw new FormatException("Input texture is the wrong format");
            }
            else if (String.Compare(texFormat, "PF_" + dds.format, true) != 0 && String.Compare(texFormat, dds.format, true) != 0)
                throw new FormatException("Input texture is the wrong format");

            ImageInfo newImg = new ImageInfo();
            newImg.storageType = privateimgList[0].storageType;
            if (newImg.storageType == storage.empty || newImg.storageType == storage.arcCpr || newImg.storageType == storage.arcUnc)
                throw new FormatException("Original texture cannot be empty or externally stored");

            newImg.offset = 0;
            newImg.imgSize = imgSize;

            switch (newImg.storageType)
            {
                case storage.pccSto:
                    imageData = dds.resize();
                    newImg.cprSize = imageData.Length;
                    newImg.uncSize = imageData.Length;
                    break;
                case storage.pccCpr:
                    SaltLZOHelper lzohelper = new SaltLZOHelper();
                    imageData = lzohelper.CompressTex(dds.resize());
                    newImg.cprSize = imageData.Length;
                    newImg.uncSize = dds.resize().Length;
                    break;
            }

            privateimgList.RemoveAt(0);
            privateimgList.Add(newImg);

            // Fix up properties
            properties["SizeX"].Value.IntValue = (int)imgSize.width;
            properties["SizeY"].Value.IntValue = (int)imgSize.height;
        }