示例#1
0
        /// <summary>
        /// Generate DDSMetadata from a Redengine CBitmapTexture
        /// </summary>
        /// <param name="xbm"></param>
        /// <returns></returns>
        private static DDSMetadata GetDDSMetadata(CBitmapTexture xbm)
        {
            int residentMipIndex = xbm.ResidentMipIndex?.val ?? 0;

            var mipcount = xbm.Mipdata.elements.Count - residentMipIndex;

            var width  = xbm.Mipdata.elements[residentMipIndex].Width.val;
            var height = xbm.Mipdata.elements[residentMipIndex].Height.val;

            var compression = xbm.Compression.Value;

            var ddsformat = ImageUtility.GetEFormatFromCompression(compression);

            // TODO: TEST THIS
            if (ddsformat == EFormat.R8G8B8A8_UNORM)
            {
                var format = xbm.Format.Value;
                switch (format)
                {
                case Enums.ETextureRawFormat.TRF_Grayscale:       // only this is ever used
                    break;

                case Enums.ETextureRawFormat.TRF_TrueColor:       // this is set if format is NULL
                case Enums.ETextureRawFormat.TRF_HDR:
                case Enums.ETextureRawFormat.TRF_AlphaGrayscale:
                case Enums.ETextureRawFormat.TRF_HDRGrayscale:
                default:
                    ddsformat = EFormat.R8G8B8A8_UNORM;
                    //throw new Exception("Invalid texture format type! [" + format + "]");
                    break;
                }
            }

            return(new DDSMetadata(width, height, (uint)mipcount, ddsformat));
        }
示例#2
0
        //public static DdsImage Xbm2Dds(CBitmapTexture xbm, byte[] rawimage)
        //{
        //    if (xbm == null || rawimage == null)
        //        return null;
        //    return new DdsImage(Xbm2DdsBytes(xbm, rawimage));
        //}



        public static byte[] Xbm2DdsBytes(CBitmapTexture xbm)
        {
            if (xbm == null)
            {
                return(null);
            }

            int residentMipIndex = xbm.GetVariableByName("ResidentMipIndex") == null ? 0 : (int)((CUInt8)xbm.GetVariableByName("ResidentMipIndex")).val;

            byte[] bytesource;
            // handle cooked xbms
            if (xbm.GetVariableByName("SourceData") == null)
            {
                bytesource = xbm.Residentmip.Bytes;
            }
            // handle imported xbms
            else
            {
                bytesource = xbm.Mips.elements[residentMipIndex].Bytes;
            }

            using (var ms = new MemoryStream())
                using (var bw = new BinaryWriter(ms))
                {
                    DDSUtils.GenerateAndWriteHeader(bw.BaseStream, Xbm2Ddsheader(xbm));

                    bw.Write(bytesource);

                    ms.Flush();

                    return(ms.ToArray());
                }
        }
示例#3
0
        public static Bitmap Xbm2Bmp(CBitmapTexture xbm)
        {
            if (xbm == null)
            {
                return(null);
            }

            int residentMipIndex = xbm.GetVariableByName("residentMipIndex") == null ? 0 : (int)((CUInt8)xbm.GetVariableByName("residentMipIndex")).val;

            byte[] bytesource;
            // handle cooked xbms
            if (xbm.GetVariableByName("sourceData") == null && xbm.Residentmip != null)
            {
                bytesource = xbm.Residentmip.Bytes;
            }
            // handle imported xbms
            else if (xbm.Mips != null)
            {
                bytesource = xbm.Mips.elements[residentMipIndex].Bytes;
            }
            else
            {
                return(null);
            }

            using (var ms = new MemoryStream(Xbm2DdsBytes(xbm, bytesource)))
            {
                return(FromStream(ms));
            }
        }
        public RDTInkTextureAtlasViewModel(inkTextureAtlas atlas, CBitmapTexture xbm, RedDocumentViewModel file) : base(xbm, file)
        {
            _atlas = atlas;
            Header = "Part Mapping";
            Width  = xbm.Width;
            Height = xbm.Height;

            Bitmap sourceBitmap;

            using (var outStream = new MemoryStream())
            {
                BitmapEncoder enc = new TiffBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create((BitmapSource)Image));
                enc.Save(outStream);
                sourceBitmap = new Bitmap(outStream);
            }

            var destBitmap = new Bitmap((int)Math.Round(Width), (int)Math.Round(Height), System.Drawing.Imaging.PixelFormat.Format64bppArgb);

            using (var gfx = Graphics.FromImage(destBitmap))
            {
                // this is doesn't account for premultipied alpha, so the opacity mask is still needed
                var matrix = new ColorMatrix(new float[][]
                {
                    new float[] { 1, 0, 0, 0, 0 },
                    new float[] { 0, 1, 0, 0, 0 },
                    new float[] { 0, 0, 1, 0, 0 },
                    new float[] { 0, 0, 0, 1, 0 },
                    new float[] { 0, 0, 0, 0, 0 },
                });
                //matrix.Matrix03 = 1F;
                //matrix.Matrix13 = TintColor.Alpha / 3F;
                //matrix.Matrix23 = TintColor.Alpha / 3F;
                //matrix.Matrix40 = TintColor.R / 255F;
                //matrix.Matrix41 = TintColor.G / 255F;
                //matrix.Matrix42 = TintColor.B / 255F;

                var attributes = new ImageAttributes();

                attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

                gfx.DrawImage(sourceBitmap, new Rectangle(0, 0, (int)Width, (int)Height), 0, 0, Width, Height, GraphicsUnit.Pixel, attributes);
            }

            sourceBitmap.Dispose();

            Image = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                destBitmap.GetHbitmap(),
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());

            foreach (var part in atlas.Slots[0].Parts)
            {
                OverlayItems.Add(new InkTextureAtlasMapperViewModel(part, xbm, atlas.Slots[0].Texture.DepotPath.ToString(), file.RelativePath, (BitmapSource)Image));
            }
        }
示例#5
0
        /// <summary>
        /// Create a System.Drawing.Bitmap from a Redengine CBitmapTexture with Wkit DDS Utility
        /// </summary>
        /// <param name="xbm"></param>
        /// <returns></returns>
        public static Bitmap Xbm2Bmp(CBitmapTexture xbm)
        {
            if (xbm == null)
            {
                return(null);
            }

            using var ms = new MemoryStream(Xbm2DdsBytes(xbm));
            return(FromStream(ms));
        }
示例#6
0
 public void SetImage(CR2WExportWrapper chunk)
 {
     try
     {
         CBitmapTexture xbm = chunk.data as CBitmapTexture;
         ImagePreviewControl.Image = ImageUtility.Xbm2Bmp(xbm) ?? SystemIcons.Warning.ToBitmap();
         //this.Text = Path.GetFileName(path);
     }
     catch (Exception)
     {
     }
 }
示例#7
0
        /// <summary>
        /// Create a byte array from a Redengine CBitmapTexture with Wkit DDS Utility
        /// </summary>
        /// <param name="xbm"></param>
        /// <returns></returns>
        public static byte[] Xbm2DdsBytes(CBitmapTexture xbm)
        {
            if (xbm == null)
            {
                return(null);
            }

            using var ms = new MemoryStream();
            using var bw = new BinaryWriter(ms);
            DDSUtils.GenerateAndWriteHeader(bw.BaseStream, GetDDSMetadata(xbm));

            bw.Write(xbm.GetBytes());

            ms.Flush();

            return(ms.ToArray());
        }
示例#8
0
        public static byte[] Xbm2DdsBytes(CBitmapTexture xbm, byte[] bytesource)
        {
            if (xbm == null)
            {
                return(null);
            }

            using (var ms = new MemoryStream())
                using (var bw = new BinaryWriter(ms))
                {
                    DDSUtils.GenerateAndWriteHeader(bw.BaseStream, Xbm2Ddsheader(xbm));

                    bw.Write(bytesource);

                    ms.Flush();

                    return(ms.ToArray());
                }
        }
示例#9
0
        private static DDSMetadata Xbm2Ddsheader(CBitmapTexture xbm)
        {
            try
            {
                int residentMipIndex = xbm.GetVariableByName("ResidentMipIndex") == null ? 0 : (int)((CUInt8)xbm.GetVariableByName("ResidentMipIndex")).val;

                int mipcount;
                // handle cooked xbms
                if (xbm.GetVariableByName("SourceData") == null)
                {
                    mipcount = xbm.Mipdata.elements.Count - residentMipIndex;
                }
                // handle imported xbms
                else
                {
                    mipcount = 0;
                }

                uint width  = xbm.Mipdata.elements[residentMipIndex].Width.val;
                uint height = xbm.Mipdata.elements[residentMipIndex].Height.val;

                var ecompression = (CName)xbm.GetVariableByName("compression");
                ETextureCompression compression = (ETextureCompression)Enum.Parse(typeof(ETextureCompression), ecompression.Value);
                var eformat = (CName)xbm.GetVariableByName("format");
                ETextureRawFormat format = ETextureRawFormat.TRF_TrueColor;
                if (eformat != null)
                {
                    format = (ETextureRawFormat)Enum.Parse(typeof(ETextureRawFormat), eformat.Value);
                }



                var ddsformat = ETextureFormat.TEXFMT_R8G8B8A8;
                switch (compression)
                {
                case ETextureCompression.TCM_DXTNoAlpha:
                    ddsformat = ETextureFormat.TEXFMT_BC1;
                    break;

                case ETextureCompression.TCM_DXTAlpha:
                    ddsformat = ETextureFormat.TEXFMT_BC3;
                    break;

                case ETextureCompression.TCM_Normals:
                    ddsformat = ETextureFormat.TEXFMT_BC1;
                    break;

                case ETextureCompression.TCM_NormalsHigh:
                    ddsformat = ETextureFormat.TEXFMT_BC3;
                    break;

                case ETextureCompression.TCM_NormalsGloss:
                    ddsformat = ETextureFormat.TEXFMT_BC3;
                    break;

                case ETextureCompression.TCM_QualityR:
                    ddsformat = ETextureFormat.TEXFMT_BC4;
                    break;

                case ETextureCompression.TCM_QualityRG:
                    ddsformat = ETextureFormat.TEXFMT_BC5;
                    break;

                case ETextureCompression.TCM_QualityColor:
                    ddsformat = ETextureFormat.TEXFMT_BC3;
                    break;

                case ETextureCompression.TCM_DXTAlphaLinear:
                case ETextureCompression.TCM_RGBE:
                case ETextureCompression.TCM_None:
                    switch (format)
                    {
                    case ETextureRawFormat.TRF_TrueColor:
                        ddsformat = ETextureFormat.TEXFMT_R8G8B8A8;
                        break;

                    case ETextureRawFormat.TRF_Grayscale:
                        break;

                    case ETextureRawFormat.TRF_HDR:
                    case ETextureRawFormat.TRF_AlphaGrayscale:
                    case ETextureRawFormat.TRF_HDRGrayscale:
                    default:
                        throw new Exception("Invalid compression type! [" + compression + "]");
                    }
                    break;

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

                return(new DDSMetadata(width, height, (uint)mipcount, ddsformat));
            }
            catch (Exception e)
            {
                //string message = e.Message;
                //string caption = "Error!";
                //MessageBoxButtons buttons = MessageBoxButtons.OK;
                //MessageBox.Show(message, caption, buttons);
                throw e;
            }
        }