public override string Convert(IIndexer m, ConversionFlags conversionFlags) { StringBuilder builder = new StringBuilder(); builder.Append(Convert(m.Modifiers, conversionFlags)); if (ShowMemberModifiers(conversionFlags) && m.IsStatic) { AppendPangoHtmlTag(builder, "static", "i", conversionFlags); } if (UseFullyQualifiedMemberNames(conversionFlags)) { AppendPangoHtmlTag(builder, m.FullyQualifiedName, "b", conversionFlags); } else { AppendPangoHtmlTag(builder, m.Name, "b", conversionFlags); } builder.Append(" ["); if (m.Parameters.Count > 0) { if (IncludeHTMLMarkup(conversionFlags)) { builder.Append("<br>"); } builder.Append(Convert(m.Parameters[0], conversionFlags)); for (int i = 1; i < m.Parameters.Count; ++i) { builder.Append(", "); if (IncludeHTMLMarkup(conversionFlags)) { builder.Append("<br>"); } builder.Append(Convert(m.Parameters[i], conversionFlags)); } if (IncludeHTMLMarkup(conversionFlags)) { builder.Append("<br>"); } } builder.Append(']'); if (m.ReturnType != null) { builder.Append(" : "); builder.Append(Convert(m.ReturnType, conversionFlags)); builder.Append(' '); } if (IncludeBodies(conversionFlags)) { builder.Append(";"); } return(builder.ToString()); }
public override string Convert(IEvent e, ConversionFlags conversionFlags) { StringBuilder builder = new StringBuilder(); builder.Append(Convert(e.Modifiers, conversionFlags)); if (ShowMemberModifiers(conversionFlags)) { builder.Append(GetModifier(e, conversionFlags)); } if (UseFullyQualifiedMemberNames(conversionFlags)) { AppendPangoHtmlTag(builder, e.FullyQualifiedName, "b", conversionFlags); } else { AppendPangoHtmlTag(builder, e.Name, "b", conversionFlags); } if (e.ReturnType != null) { builder.Append(" : "); builder.Append(Convert(e.ReturnType, conversionFlags)); builder.Append(' '); } if (IncludeBodies(conversionFlags)) { builder.Append(";"); } return(builder.ToString()); }
public void GenericTypeWithNested(ConversionFlags flags, string expectedOutput) { var typeDef = GetDefinition(typeof(List <> .Enumerator)); ambience.ConversionFlags = flags; Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(typeDef)); }
static string GetName(IMember member, ConversionFlags flags) { IAmbience ambience = AmbienceService.GetCurrentAmbience(); ambience.ConversionFlags = flags | ConversionFlags.ShowParameterNames | ConversionFlags.ShowTypeParameterList; return(ambience.Convert(member)); }
public override string Convert(IParameter param, ConversionFlags conversionFlags) { StringBuilder builder = new StringBuilder(); if (param.IsRef) { AppendPangoHtmlTag(builder, "ref ", "i", conversionFlags); } else if (param.IsOut) { AppendPangoHtmlTag(builder, "out ", "i", conversionFlags); } else if (param.IsParams) { AppendPangoHtmlTag(builder, "params ", "i", conversionFlags); } if (ShowParameterNames(conversionFlags)) { builder.Append(' '); builder.Append(param.Name); } builder.Append(" : "); builder.Append(Convert(param.ReturnType, conversionFlags)); return(builder.ToString()); }
public void ReadonlyRefStructTest(ConversionFlags flags, string expectedOutput) { var typeDef = GetDefinition(typeof(ReadonlyRefStruct)); ambience.ConversionFlags = flags; Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(typeDef)); }
public void GenericInterface(ConversionFlags flags, string expectedOutput) { var typeDef = GetDefinition(typeof(IEnumerable <>)); ambience.ConversionFlags = flags; Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(typeDef)); }
public void GenericType(ConversionFlags flags, string expectedOutput) { var typeDef = GetDefinition(typeof(Dictionary <,>)); ambience.ConversionFlags = flags; Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(typeDef)); }
// Note that many common DDS reader/writers (including D3DX) swap the // the RED/BLUE masks for 10:10:10:2 formats. We assume // below that the 'backwards' header mask is being used since it is most // likely written by D3DX. The more robust solution is to use the 'DX10' // header extension and specify the Format.R10G10B10A2_UNorm format directly // We do not support the following legacy Direct3D 9 formats: // BumpDuDv D3DFMT_V8U8, D3DFMT_Q8W8V8U8, D3DFMT_V16U16, D3DFMT_A2W10V10U10 // BumpLuminance D3DFMT_L6V5U5, D3DFMT_X8L8V8U8 // FourCC "UYVY" D3DFMT_UYVY // FourCC "YUY2" D3DFMT_YUY2 // FourCC 117 D3DFMT_CxV8U8 // ZBuffer D3DFMT_D16_LOCKABLE // FourCC 82 D3DFMT_D32F_LOCKABLE public static PixelFormats GetDXGIFormat(ref DDS.PixelFormat pixelFormat, out ConversionFlags conversionFlags, DDSFlags flags = DDSFlags.None) { conversionFlags = ConversionFlags.None; int index = 0; for (index = 0; index < LegacyMaps.Length; ++index) { var entry = LegacyMaps[index]; if ((pixelFormat.Flags & entry.PixelFormat.Flags) != 0) { if ((entry.PixelFormat.Flags & DDS.PixelFormatFlags.FourCC) != 0) { if (pixelFormat.FourCC == entry.PixelFormat.FourCC) { break; } } else if ((entry.PixelFormat.Flags & DDS.PixelFormatFlags.Pal8) != 0) { if (pixelFormat.RGBBitCount == entry.PixelFormat.RGBBitCount) { break; } } else if (pixelFormat.RGBBitCount == entry.PixelFormat.RGBBitCount) { // RGB, RGBA, ALPHA, LUMINANCE if (pixelFormat.RBitMask == entry.PixelFormat.RBitMask && pixelFormat.GBitMask == entry.PixelFormat.GBitMask && pixelFormat.BBitMask == entry.PixelFormat.BBitMask && pixelFormat.ABitMask == entry.PixelFormat.ABitMask) { break; } } } } if (index >= LegacyMaps.Length) { return(PixelFormats.UNKNOWN); } conversionFlags = LegacyMaps[index].ConversionFlags; var format = LegacyMaps[index].Format; if ((conversionFlags & ConversionFlags.Expand) != 0 && (flags & DDSFlags.NoLegacyExpansion) != 0) { return(PixelFormats.UNKNOWN); } if ((format == PixelFormats.R10G10B10A2_UNORM) && (flags & DDSFlags.NoR10B10G10A2Fixup) != 0) { conversionFlags ^= ConversionFlags.Swizzle; } return(format); }
public void SimpleType(ConversionFlags flags, string expectedOutput) { var typeDef = GetDefinition(typeof(object)); ambience.ConversionFlags = flags; Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(typeDef)); }
public void FuncDelegate(ConversionFlags flags, string expectedOutput) { var func = GetDefinition(typeof(Func <,>)); ambience.ConversionFlags = flags; Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(func)); }
/// <summary> /// Gets a value on the entity. /// </summary> /// <param name="entity"></param> /// <param name="fields"></param> /// <returns></returns> public object[] GetValues(object entity, EntityField[] fields, ConversionFlags flags, FetchFlags fetchFlags) { if (entity == null) { throw new ArgumentNullException("entity"); } if (fields == null) { throw new ArgumentNullException("fields"); } if (fields.Length == 0) { return new object[] {} } ; // check... if (EntityType == null) { throw new ArgumentNullException("EntityType"); } this.EntityType.AssertIsOfType(entity); // create... object[] results = new object[fields.Length]; for (int index = 0; index < fields.Length; index++) { results[index] = this.GetValue(entity, fields[index], flags, fetchFlags); } // return... return(results); }
public override string Convert(ModifierEnum modifier, ConversionFlags conversionFlags) { if (ShowAccessibility(conversionFlags)) { if (ModifierIsSet(modifier, ModifierEnum.Public)) { return("public "); } else if (ModifierIsSet(modifier, ModifierEnum.Private)) { return("private "); } else if (ModifierIsSet(modifier, ModifierEnum.ProtectedAndInternal)) { return("protected internal "); } else if (ModifierIsSet(modifier, ModifierEnum.ProtectedOrInternal)) { return("internal protected "); } else if (ModifierIsSet(modifier, ModifierEnum.Internal)) { return("internal "); } else if (ModifierIsSet(modifier, ModifierEnum.Protected)) { return("protected "); } } return(string.Empty); }
public void SealedClassTest(ConversionFlags flags, string expectedOutput) { var typeDef = GetDefinition(typeof(SealedClass)); ambience.ConversionFlags = flags; Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(typeDef)); }
public void Indexer(ConversionFlags flags, string expectedOutput) { var prop = compilation.FindType(typeof(CSharpAmbienceTests.Program)).GetProperties(p => p.IsIndexer).Single(); ambience.ConversionFlags = flags; Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(prop)); }
public void ConstructorTests(ConversionFlags flags, string expectedOutput) { var prop = compilation.FindType(typeof(CSharpAmbienceTests.Program)).GetConstructors().Single(); ambience.ConversionFlags = flags; Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(prop)); }
public void SimpleField(ConversionFlags flags, string expectedOutput) { var field = GetDefinition(typeof(CSharpAmbienceTests.Program)).GetFields(f => f.Name == "test").Single(); ambience.ConversionFlags = flags; Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(field)); }
public void SimpleConstField(ConversionFlags flags, string expectedOutput) { var field = compilation.FindType(typeof(CSharpAmbienceTests.Program)).GetFields(f => f.Name == "TEST2").Single(); ambience.ConversionFlags = flags; Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(field)); }
public void DestructorTests(ConversionFlags flags, string expectedOutput) { var dtor = compilation.FindType(typeof(CSharpAmbienceTests.Program)) .GetMembers(m => m.SymbolKind == SymbolKind.Destructor, GetMemberOptions.IgnoreInheritedMembers).Single(); ambience.ConversionFlags = flags; Assert.AreEqual(expectedOutput, ambience.ConvertSymbol(dtor)); }
public override string Convert(LocalVariable localVariable, ConversionFlags conversionFlags) { StringBuilder builder = new StringBuilder(); builder.Append(localVariable.Name); builder.Append(" : "); builder.Append(Convert(localVariable.ReturnType, conversionFlags)); return(builder.ToString()); }
internal static void ConvertIncremental(World conversionWorld, ConversionFlags flags, ref IncrementalConversionBatch batch) { using (var conversion = new Conversion(conversionWorld)) { conversion.MappingSystem.BeginIncrementalConversionPreparation(flags, ref batch); conversionWorld.GetExistingSystem <ConversionSetupGroup>().Update(); conversion.MappingSystem.FinishIncrementalConversionPreparation(); FinishConvertIncremental(conversionWorld, conversion); } }
private static Header EncodeTgaHeader(Image image, ref ConversionFlags convFlags) { Header header = new Header(); if (image.Width > 0xFFFF || image.Height > 0xFFFF) { throw new NotSupportedException("The specified image exceeds the maximum pixel size of a TGA file."); } header.Width = (ushort)image.Width; header.Height = (ushort)image.Height; switch (image.Format) { case DataFormat.R8G8B8A8_UNORM: case DataFormat.R8G8B8A8_UNORM_SRGB: header.ImageType = ImageType.TrueColor; header.BitsPerPixel = 32; header.Descriptor = DescriptorFlags.InvertY | (DescriptorFlags)8; // 8 bit alpha convFlags |= ConversionFlags.Swizzle; break; case DataFormat.B8G8R8A8_UNORM: case DataFormat.B8G8R8A8_UNORM_SRGB: header.ImageType = ImageType.TrueColor; header.BitsPerPixel = 32; header.Descriptor = DescriptorFlags.InvertY | (DescriptorFlags)8; // 8 bit alpha break; case DataFormat.B8G8R8X8_UNORM: case DataFormat.B8G8R8X8_UNORM_SRGB: header.ImageType = ImageType.TrueColor; header.BitsPerPixel = 24; header.Descriptor = DescriptorFlags.InvertY; convFlags |= ConversionFlags.Format888; break; case DataFormat.R8_UNORM: case DataFormat.A8_UNORM: header.ImageType = ImageType.BlackAndWhite; header.BitsPerPixel = 8; header.Descriptor = DescriptorFlags.InvertY; break; case DataFormat.B5G5R5A1_UNORM: header.ImageType = ImageType.TrueColor; header.BitsPerPixel = 16; header.Descriptor = DescriptorFlags.InvertY | (DescriptorFlags)1; // 1 bit alpha break; } return(header); }
public ParamTypeConversion( Parameter unoParameter, string foreignType, Func <string, FatConversion> fromUno, Func <string, FatConversion> toUno, ConversionFlags conversionFlags) { UnoParameter = unoParameter; ForeignType = foreignType; FromUno = fromUno; ToUno = toUno; ConversionFlags = conversionFlags; }
// pango has some problems with // <i>static </i>bool <b>Equals</b> (<i></i>object a, <i></i>object b) // it will make "object a" italics. so rather tan appending a markup // tag if there might be a modifier, we only do it if there is. void AppendPangoHtmlTag(StringBuilder sb, string str, string tag, ConversionFlags conversionFlags) { if (IncludeHTMLMarkup(conversionFlags) | IncludePangoMarkup(conversionFlags)) { sb.Append('<').Append(tag).Append('>'); } sb.Append(str); if (IncludeHTMLMarkup(conversionFlags) | IncludePangoMarkup(conversionFlags)) { sb.Append("</").Append(tag).Append('>'); } }
/// <summary> /// Gets key values for an entity. /// </summary> /// <param name="entity"></param> /// <param name="flags"></param> /// <returns></returns> public object[] GetKeyValues(object entity, ConversionFlags flags) { if (entity == null) { throw new ArgumentNullException("entity"); } if (EntityType == null) { throw new ArgumentNullException("EntityType"); } this.EntityType.AssertIsOfType(entity); // return... return(this.GetValues(entity, this.EntityType.GetKeyFields(), flags)); }
/// <summary> /// Gets the value in the given field. /// </summary> /// <returns></returns> protected object GetFieldValue(string fieldName, Type returnType, ConversionFlags conversionFlags, OnNotFound onNotFound) { if (fieldName == null) { throw new ArgumentNullException("fieldName"); } if (fieldName.Length == 0) { throw ExceptionHelper.CreateZeroLengthArgumentException("fieldName"); } // get it... int index = this.GetFieldIndex(fieldName, onNotFound); return(this.GetFieldValue(index, returnType, conversionFlags)); }
public override string Convert(IField field, ConversionFlags conversionFlags) { StringBuilder builder = new StringBuilder(); builder.Append(Convert(field.Modifiers, conversionFlags)); if (ShowMemberModifiers(conversionFlags)) { if (field.IsStatic && field.IsLiteral) { AppendPangoHtmlTag(builder, "const ", "i", conversionFlags); } else if (field.IsStatic) { AppendPangoHtmlTag(builder, "static ", "i", conversionFlags); } if (!field.IsReadonly) { AppendPangoHtmlTag(builder, "mutable ", "i", conversionFlags); } } if (UseFullyQualifiedMemberNames(conversionFlags)) { AppendPangoHtmlTag(builder, field.FullyQualifiedName, "b", conversionFlags); } else { AppendPangoHtmlTag(builder, field.Name, "b", conversionFlags); } if (field.ReturnType != null) { builder.Append(" : "); builder.Append(Convert(field.ReturnType, conversionFlags)); builder.Append(' '); } if (IncludeBodies(conversionFlags)) { builder.Append(";"); } return(builder.ToString()); }
public TypeConversion( DataType unoType, string foreignType, Func <string, string> fromUno, Func <string, string> toUno, Func <string, string> box, Func <string, string> unbox, ConversionFlags conversionFlags) { UnoType = unoType; ForeignType = foreignType; FromUno = fromUno; ToUno = toUno; Box = box; Unbox = unbox; ConversionFlags = conversionFlags; }
/// <summary> /// Gets the value at the given index. /// </summary> /// <param name="index"></param> /// <param name="returnType"></param> /// <returns></returns> private object GetFieldValue(int index, Type returnType, ConversionFlags conversionFlags) { object value = null; if (index != -1) { value = this.Values[index]; } // return... if (returnType == null) { return(value); } else { return(ConversionHelper.ChangeType(value, returnType, Cultures.System, conversionFlags)); } }
public override string Convert(ModifierEnum modifier, ConversionFlags conversionFlags) { if (ShowAccessibility(conversionFlags)) { if (ModifierIsSet(modifier, ModifierEnum.Public)) { return "public "; } else if (ModifierIsSet(modifier, ModifierEnum.Private)) { return "private "; } else if (ModifierIsSet(modifier, ModifierEnum.ProtectedAndInternal)) { return "protected internal "; } else if (ModifierIsSet(modifier, ModifierEnum.ProtectedOrInternal)) { return "internal protected "; } else if (ModifierIsSet(modifier, ModifierEnum.Internal)) { return "internal "; } else if (ModifierIsSet(modifier, ModifierEnum.Protected)) { return "protected "; } } return string.Empty; }
/// <summary> /// Gets the value in the given field. /// </summary> /// <param name="field"></param> /// <returns></returns> protected object GetFieldValue(EntityField field, Type returnType, ConversionFlags conversionFlags, OnNotFound onNotFound) { if (field == null) { throw new ArgumentNullException("field"); } // get it... int index = this.GetFieldIndex(field, onNotFound); object value = this.GetFieldValue(index, returnType, conversionFlags); // mbr - 31-08-2006 - added change in here for numeric types to round values... if (field.NeedsRounding) { if (value == null) { throw new InvalidOperationException("value is null."); } // convert... if (value is decimal) { value = Math.Round((decimal)value, field.Scale); } else if (value is double) { value = Math.Round((double)value, field.Scale); } else if (value is float) { value = Math.Round((float)value, field.Scale); } else { throw new NotSupportedException(string.Format("Cannot handle '{0}'.", value.GetType())); } } // return... return(value); }
public override string Convert(IClass c, ConversionFlags conversionFlags) { StringBuilder builder = new StringBuilder(); builder.Append(Convert(c.Modifiers, conversionFlags)); if (ShowClassModifiers(conversionFlags)) { if (c.IsSealed) { switch (c.ClassType) { case ClassType.Delegate: case ClassType.Struct: case ClassType.Enum: break; default: AppendPangoHtmlTag (builder, "sealed ", "i", conversionFlags); break; } } else if (c.IsAbstract && c.ClassType != ClassType.Interface) { AppendPangoHtmlTag (builder, "abstract ", "i", conversionFlags); } } if (ShowClassModifiers(conversionFlags)) { switch (c.ClassType) { case ClassType.Delegate: builder.Append("delegate"); // Only display the return type when modifiers are to be // shown - this fixes the vay delegates are shown in the // popup window if (c.Methods.Count > 0) { foreach(IMethod m in c.Methods) { if (m.Name != "Invoke") continue; builder.Append (' '); builder.Append (Convert(m.ReturnType)); } } break; case ClassType.Class: builder.Append("class"); break; case ClassType.Struct: builder.Append("struct"); break; case ClassType.Interface: builder.Append("interface"); break; case ClassType.Enum: builder.Append("enum"); break; } builder.Append(' '); } if (UseFullyQualifiedMemberNames(conversionFlags)) { AppendPangoHtmlTag (builder, c.FullyQualifiedName, "b", conversionFlags); } else { AppendPangoHtmlTag (builder, c.Name, "b", conversionFlags); } // Display generic parameters only if told so if (ShowGenericParameters(conversionFlags) && c.GenericParameters != null && c.GenericParameters.Count > 0) { bool includeMarkup = IncludeHTMLMarkup(conversionFlags) || IncludePangoMarkup(conversionFlags); builder.Append ("["); // Since we know that there is at least one generic parameter in // the list, we can add it outside the loop - so, we don't have // to check whether we may append a comma or not builder.Append (c.GenericParameters[0].Name); // Now continue with the others, if there are any for (int i = 1; i < c.GenericParameters.Count; i++) { builder.Append (", "); builder.Append (c.GenericParameters[i].Name); } builder.Append ("]"); } if (c.ClassType == ClassType.Delegate) { foreach(IMethod m in c.Methods) { if (m.Name != "Invoke") continue; builder.Append(" ("); if (IncludeHTMLMarkup(conversionFlags)) builder.Append("<br>"); for (int i = 0; i < m.Parameters.Count; ++i) { // if (IncludeHTMLMarkup(conversionFlags)) builder.Append(" "); builder.Append(Convert(m.Parameters[i], conversionFlags)); if (i + 1 < m.Parameters.Count) builder.Append(", "); if (IncludeHTMLMarkup(conversionFlags)) builder.Append("<br>"); } builder.Append(") : "); builder.Append(Convert(m.ReturnType, conversionFlags)); builder.Append(' '); } } else if (ShowInheritanceList(conversionFlags) && c.ClassType != ClassType.Enum) { if (c.BaseTypes.Count > 0) { builder.Append (" : "); builder.Append (Convert(c.BaseTypes[0])); for (int i = 1; i < c.BaseTypes.Count; ++i) { builder.Append (", "); builder.Append (Convert(c.BaseTypes[i])); } } } if (IncludeBodies(conversionFlags)) { builder.Append("\n{"); } return builder.ToString(); }
public override string ConvertEnd(IClass c, ConversionFlags conversionFlags) { return "}"; }
public override string Convert(IField field, ConversionFlags conversionFlags) { StringBuilder builder = new StringBuilder(); builder.Append(Convert(field.Modifiers, conversionFlags)); if (ShowMemberModifiers(conversionFlags)) { if (field.IsStatic && field.IsLiteral) AppendPangoHtmlTag (builder, "const ", "i", conversionFlags); else if (field.IsStatic) AppendPangoHtmlTag (builder, "static ", "i", conversionFlags); if (!field.IsReadonly) { AppendPangoHtmlTag (builder, "mutable ", "i", conversionFlags); } } if (UseFullyQualifiedMemberNames(conversionFlags)) AppendPangoHtmlTag (builder, field.FullyQualifiedName, "b", conversionFlags); else AppendPangoHtmlTag (builder, field.Name, "b", conversionFlags); if (field.ReturnType != null) { builder.Append(" : "); builder.Append(Convert(field.ReturnType, conversionFlags)); builder.Append(' '); } if (IncludeBodies(conversionFlags)) builder.Append(";"); return builder.ToString(); }
static string GetName(IMember member, ConversionFlags flags) { IAmbience ambience = AmbienceService.GetCurrentAmbience(); ambience.ConversionFlags = flags | ConversionFlags.ShowParameterNames | ConversionFlags.ShowTypeParameterList; return ambience.Convert(member); }
/// <summary> /// Decodes the TGA file header. /// </summary> /// <param name="stream">The stream.</param> /// <param name="offset">The offset in the stream at which the data starts.</param> /// <param name="convFlags">The conversion flags.</param> /// <exception cref="ArgumentNullException"> /// <paramref name="stream"/> is <see langword="null"/>. /// </exception> /// <exception cref="InvalidDataException"> /// Invalid data. /// </exception> /// <exception cref="NotSupportedException"> /// The specified format is not supported. /// </exception> private static TextureDescription DecodeTGAHeader(Stream stream, out int offset, out ConversionFlags convFlags) { if (stream == null) throw new ArgumentNullException("stream"); TextureDescription description = new TextureDescription { Dimension = TextureDimension.Texture2D, Format = DataFormat.R8G8B8A8_UNORM, }; offset = 0; convFlags = ConversionFlags.None; long size = stream.Length - stream.Position; int sizeOfTGAHeader = Marshal.SizeOf(typeof(Header)); if (size < sizeOfTGAHeader) throw new InvalidDataException("The TGA file is corrupt."); var header = stream.ReadStruct<Header>(); if (header.ColorMapType != 0 || header.ColorMapLength != 0) throw new NotSupportedException("TGA files with color maps are not supported."); if ((header.Descriptor & (DescriptorFlags.Interleaved2Way | DescriptorFlags.Interleaved4Way)) != 0) throw new NotSupportedException("TGA files with interleaved images are not supported."); if (header.Width == 0 || header.Height == 0) throw new NotSupportedException("The TGA file is corrupt. Width and height are invalid."); switch (header.ImageType) { case ImageType.TrueColor: case ImageType.TrueColorRLE: switch (header.BitsPerPixel) { case 16: description.Format = DataFormat.B5G5R5A1_UNORM; break; case 24: description.Format = DataFormat.R8G8B8A8_UNORM; convFlags |= ConversionFlags.Expand; // We could use DXGI_FORMAT_B8G8R8X8_UNORM, but we prefer DXGI 1.0 formats break; case 32: description.Format = DataFormat.R8G8B8A8_UNORM; // We could use DXGI.Format.B8G8R8A8_UNORM, but we prefer DXGI 1.0 formats break; } if (header.ImageType == ImageType.TrueColorRLE) convFlags |= ConversionFlags.RLE; break; case ImageType.BlackAndWhite: case ImageType.BlackAndWhiteRLE: switch (header.BitsPerPixel) { case 8: description.Format = DataFormat.R8_UNORM; break; default: throw new NotSupportedException("The black-and-white format used by the TGA file is not supported. Only 8-bit black-and-white images are supported."); } if (header.ImageType == ImageType.BlackAndWhiteRLE) convFlags |= ConversionFlags.RLE; break; case ImageType.NoImage: case ImageType.ColorMapped: case ImageType.ColorMappedRLE: throw new NotSupportedException("The image format used by the TGA file is not supported."); default: throw new InvalidDataException("Unknown image format used by the TGA file."); } description.Width = header.Width; description.Height = header.Height; description.Depth = 1; description.MipLevels = 1; description.ArraySize = 1; if ((header.Descriptor & DescriptorFlags.InvertX) != 0) convFlags |= ConversionFlags.InvertX; if ((header.Descriptor & DescriptorFlags.InvertY) != 0) convFlags |= ConversionFlags.InvertY; offset = sizeOfTGAHeader; if (header.IDLength != 0) offset += header.IDLength; return description; }
/// <summary> /// Uncompresses the pixel data from a TGA file into the target image. /// </summary> /// <param name="reader">The input reader.</param> /// <param name="image">The image.</param> /// <param name="format">The texture format.</param> /// <param name="convFlags">The conversion flags.</param> /// <exception cref="ArgumentNullException"> /// <paramref name="reader"/> or <paramref name="image"/> is <see langword="null"/>. /// </exception> private static unsafe void UncompressPixels(BinaryReader reader, Image image, DataFormat format, ConversionFlags convFlags) { if (reader == null) throw new ArgumentNullException("reader"); if (image == null) throw new ArgumentNullException("image"); bool invertX = (convFlags & ConversionFlags.InvertX) != 0; bool invertY = (convFlags & ConversionFlags.InvertY) != 0; bool expand = (convFlags & ConversionFlags.Expand) != 0; byte[] pixelData = image.Data; fixed (byte* imagePtr = pixelData) { switch (format) { case DataFormat.R8_UNORM: // 8-bit { for (int y = 0; y < image.Height; y++) { int xOffset = invertX ? image.Width - 1 : 0; int yOffset = invertY ? y : image.Height - y - 1; byte* ptr = imagePtr + image.RowPitch * yOffset + xOffset; for (int x = 0; x < image.Width; ) { byte b = reader.ReadByte(); int count = (b & 0x7F) + 1; if ((b & 0x80) != 0) { // Repeat b = reader.ReadByte(); for (int j = 0; j < count; j++) { *ptr = b; if (invertX) ptr--; else ptr++; } } else { // Literal for (int j = 0; j < count; j++) { *ptr = reader.ReadByte(); if (invertX) ptr--; else ptr++; } } x += count; } } } break; case DataFormat.B5G5R5A1_UNORM: // 16-bit { bool nonZeroAlpha = false; for (int y = 0; y < image.Height; y++) { int xOffset = invertX ? image.Width - 1 : 0; int yOffset = invertY ? y : image.Height - y - 1; ushort* ptr = (ushort*)(imagePtr + image.RowPitch * yOffset) + xOffset; for (int x = 0; x < image.Width; ) { byte b = reader.ReadByte(); int count = (b & 0x7F) + 1; if ((b & 0x80) != 0) { // Repeat ushort color = ReadBgra5551(reader); if (GetAlpha(color) != 0) nonZeroAlpha = true; for (int j = count; j > 0; j--) { *ptr = color; if (invertX) ptr--; else ptr++; } } else { // Literal for (int j = count; j > 0; j--) { ushort color = ReadBgra5551(reader); if (GetAlpha(color) != 0) nonZeroAlpha = true; *ptr = color; if (invertX) ptr--; else ptr++; } } x += count; } } // If there are no non-zero alpha channel entries, we'll assume alpha is not used and force it to opaque. if (!nonZeroAlpha) { SetAlphaToOpaque(image); } } break; case DataFormat.R8G8B8A8_UNORM: // 24/32-bit { bool nonZeroAlpha = false; for (int y = 0; y < image.Height; y++) { int xOffset = invertX ? image.Width - 1 : 0; int yOffset = invertY ? y : image.Height - y - 1; uint* ptr = (uint*)(imagePtr + image.RowPitch * yOffset) + xOffset; for (int x = 0; x < image.Width; ) { byte b = reader.ReadByte(); int count = (b & 0x7F) + 1; if ((b & 0x80) != 0) { // Repeat uint color = ReadColor(reader, expand); if (GetAlpha(color) != 0) nonZeroAlpha = true; for (int j = count; j > 0; j--) { *ptr = color; if (invertX) ptr--; else ptr++; } } else { // Literal for (int j = count; j > 0; j--) { uint color = ReadColor(reader, expand); if (GetAlpha(color) != 0) nonZeroAlpha = true; *ptr = color; if (invertX) ptr--; else ptr++; } } x += count; } } // If there are no non-zero alpha channel entries, we'll assume alpha is not used and force it to opaque. if (!nonZeroAlpha) { SetAlphaToOpaque(image); } } break; } } }
public override string ConvertEnd(IMethod m, ConversionFlags conversionFlags) { return "}"; }
public override string Convert(IReturnType returnType, ConversionFlags conversionFlags) { if (returnType == null) { return String.Empty; } StringBuilder builder = new StringBuilder(); //bool linkSet = false; //if (UseLinkArrayList(conversionFlags)) { //SharpAssemblyReturnType ret = returnType as SharpAssemblyReturnType; //if (ret != null) { // if (ret.UnderlyingClass != null) { // builder.Append("<a href='as://" + linkArrayList.Add(ret.UnderlyingClass) + "'>"); // linkSet = true; // } //} //} if (typeConversionTable[returnType.FullyQualifiedName] != null) { builder.Append(typeConversionTable[returnType.FullyQualifiedName].ToString()); } else { if (UseFullyQualifiedMemberNames(conversionFlags)) { builder.Append (returnType.FullyQualifiedName); } else { builder.Append (returnType.Name); } } // Display generic parameters only if told so if (ShowGenericParameters(conversionFlags) && returnType.GenericArguments != null && returnType.GenericArguments.Count > 0) { bool includeMarkup = IncludeHTMLMarkup(conversionFlags) || IncludePangoMarkup(conversionFlags); builder.Append ("["); // Since we know that there is at least one generic argument in // the list, we can add it outside the loop - so, we don't have // to check whether we may append a comma or not builder.Append (Convert(returnType.GenericArguments[0], conversionFlags)); // Now continue with the others, if there are any for (int i = 1; i < returnType.GenericArguments.Count; i++) { builder.Append (", "); builder.Append ( Convert(returnType.GenericArguments[i], conversionFlags)); } builder.Append ("]"); } // if (linkSet) { // builder.Append("</a>"); // } if (returnType.ArrayCount > 0) return "array[" + builder.ToString() + "]"; else return builder.ToString(); }
public override string Convert(IParameter param, ConversionFlags conversionFlags) { StringBuilder builder = new StringBuilder(); if (param.IsRef) AppendPangoHtmlTag (builder, "ref ", "i", conversionFlags); else if (param.IsOut) AppendPangoHtmlTag (builder, "out ", "i", conversionFlags); else if (param.IsParams) AppendPangoHtmlTag (builder, "params ", "i", conversionFlags); if (ShowParameterNames(conversionFlags)) { builder.Append(' '); builder.Append(param.Name); } builder.Append(" : "); builder.Append(Convert(param.ReturnType, conversionFlags)); return builder.ToString(); }
public override string Convert(LocalVariable localVariable, ConversionFlags conversionFlags) { StringBuilder builder = new StringBuilder(); builder.Append(localVariable.Name); builder.Append(" : "); builder.Append(Convert(localVariable.ReturnType, conversionFlags)); return builder.ToString(); }
// pango has some problems with // <i>static </i>bool <b>Equals</b> (<i></i>object a, <i></i>object b) // it will make "object a" italics. so rather tan appending a markup // tag if there might be a modifier, we only do it if there is. void AppendPangoHtmlTag (StringBuilder sb, string str, string tag, ConversionFlags conversionFlags) { if (IncludeHTMLMarkup(conversionFlags) | IncludePangoMarkup(conversionFlags)) sb.Append ('<').Append (tag).Append ('>'); sb.Append (str); if (IncludeHTMLMarkup(conversionFlags) | IncludePangoMarkup(conversionFlags)) sb.Append ("</").Append (tag).Append ('>'); }
// Note that many common DDS reader/writers (including D3DX) swap the // the RED/BLUE masks for 10:10:10:2 formats. We assumme // below that the 'backwards' header mask is being used since it is most // likely written by D3DX. The more robust solution is to use the 'DX10' // header extension and specify the Format.R10G10B10A2_UNorm format directly // We do not support the following legacy Direct3D 9 formats: // BumpDuDv D3DFMT_V8U8, D3DFMT_Q8W8V8U8, D3DFMT_V16U16, D3DFMT_A2W10V10U10 // BumpLuminance D3DFMT_L6V5U5, D3DFMT_X8L8V8U8 // FourCC "UYVY" D3DFMT_UYVY // FourCC "YUY2" D3DFMT_YUY2 // FourCC 117 D3DFMT_CxV8U8 // ZBuffer D3DFMT_D16_LOCKABLE // FourCC 82 D3DFMT_D32F_LOCKABLE private static Format GetDXGIFormat(ref DDS.PixelFormat pixelFormat, DDSFlags flags, out ConversionFlags conversionFlags) { conversionFlags = ConversionFlags.None; int index = 0; for (index = 0; index < LegacyMaps.Length; ++index) { var entry = LegacyMaps[index]; if ((pixelFormat.Flags & entry.PixelFormat.Flags) != 0) { if ((entry.PixelFormat.Flags & DDS.PixelFormatFlags.FourCC) != 0) { if (pixelFormat.FourCC == entry.PixelFormat.FourCC) break; } else if ((entry.PixelFormat.Flags & DDS.PixelFormatFlags.Pal8) != 0) { if (pixelFormat.RGBBitCount == entry.PixelFormat.RGBBitCount) break; } else if (pixelFormat.RGBBitCount == entry.PixelFormat.RGBBitCount) { // RGB, RGBA, ALPHA, LUMINANCE if (pixelFormat.RBitMask == entry.PixelFormat.RBitMask && pixelFormat.GBitMask == entry.PixelFormat.GBitMask && pixelFormat.BBitMask == entry.PixelFormat.BBitMask && pixelFormat.ABitMask == entry.PixelFormat.ABitMask) break; } } } if (index >= LegacyMaps.Length) return Format.Unknown; conversionFlags = LegacyMaps[index].ConversionFlags; var format = LegacyMaps[index].Format; if ((conversionFlags & ConversionFlags.Expand) != 0 && (flags & DDSFlags.NoLegacyExpansion) != 0) return Format.Unknown; if ((format == Format.R10G10B10A2_UNorm) && (flags & DDSFlags.NoR10B10G10A2Fixup) != 0) { conversionFlags ^= ConversionFlags.Swizzle; } return format; }
/// <summary> /// Converts or copies image data from pPixels into scratch image data /// </summary> /// <param name="pDDS"></param> /// <param name="offset"></param> /// <param name="size"></param> /// <param name="metadata"></param> /// <param name="cpFlags"></param> /// <param name="convFlags"></param> /// <param name="pal8"></param> /// <param name="handle"></param> /// <returns></returns> private static unsafe Image CreateImageFromDDS(IntPtr pDDS, int offset, int size, ImageDescription metadata, Image.PitchFlags cpFlags, ConversionFlags convFlags, uint* pal8, GCHandle? handle) { if ((convFlags & ConversionFlags.Expand) != 0) { if ((convFlags & ConversionFlags.Format888) != 0) cpFlags |= Image.PitchFlags.Bpp24; else if ((convFlags & (ConversionFlags.Format565 | ConversionFlags.Format5551 | ConversionFlags.Format4444 | ConversionFlags.Format8332 | ConversionFlags.FormatA8P8)) != 0) cpFlags |= Image.PitchFlags.Bpp16; else if ((convFlags & (ConversionFlags.Format44 | ConversionFlags.Format332 | ConversionFlags.Pal8)) != 0) cpFlags |= Image.PitchFlags.Bpp8; } // If source image == dest image and no swizzle/alpha is required, we can return it as-is var isCopyNeeded = (convFlags & (ConversionFlags.Expand | ConversionFlags.CopyMemory)) != 0 || ((cpFlags & Image.PitchFlags.LegacyDword) != 0); var image = new Image(metadata, pDDS, offset, handle, !isCopyNeeded, cpFlags); // Ensure there is enough data to copy from. Debug.Assert(size >= image.TotalSizeInBytes); if (!isCopyNeeded && (convFlags & (ConversionFlags.Swizzle | ConversionFlags.NoAlpha)) == 0) return image; var imageDst = isCopyNeeded ? new Image(metadata, IntPtr.Zero, 0, null, false) : image; var images = image.PixelBuffer; var imagesDst = imageDst.PixelBuffer; ScanlineFlags tflags = (convFlags & ConversionFlags.NoAlpha) != 0 ? ScanlineFlags.SetAlpha : ScanlineFlags.None; if ((convFlags & ConversionFlags.Swizzle) != 0) tflags |= ScanlineFlags.Legacy; int index = 0; int checkSize = size; for (int arrayIndex = 0; arrayIndex < metadata.ArraySize; arrayIndex++) { int d = metadata.Depth; // Else we need to go through each mips/depth slice to convert all scanlines. for (int level = 0; level < metadata.MipLevels; ++level) { for (int slice = 0; slice < d; ++slice, ++index) { IntPtr pSrc = images[index].DataPointer; IntPtr pDest = imagesDst[index].DataPointer; checkSize -= images[index].BufferStride; if (checkSize < 0) throw new InvalidOperationException("Unexpected end of buffer"); if (metadata.Format.IsCompressed()) { Utilities.CopyMemory(pDest, pSrc, Math.Min(images[index].BufferStride, imagesDst[index].BufferStride)); } else { int spitch = images[index].RowStride; int dpitch = imagesDst[index].RowStride; for (int h = 0; h < images[index].Height; ++h) { if ((convFlags & ConversionFlags.Expand) != 0) { #if DIRECTX11_1 if ((convFlags & (ConversionFlags.Format565 | ConversionFlags.Format5551 | ConversionFlags.Format4444)) != 0) #else if ((convFlags & (ConversionFlags.Format565 | ConversionFlags.Format5551)) != 0) #endif { ExpandScanline(pDest, dpitch, pSrc, spitch, (convFlags & ConversionFlags.Format565) != 0 ? PixelFormat.B5G6R5_UNorm : PixelFormat.B5G5R5A1_UNorm, tflags); } else { var lformat = FindLegacyFormat(convFlags); LegacyExpandScanline(pDest, dpitch, metadata.Format, pSrc, spitch, lformat, pal8, tflags); } } else if ((convFlags & ConversionFlags.Swizzle) != 0) { SwizzleScanline(pDest, dpitch, pSrc, spitch, metadata.Format, tflags); } else { if (pSrc != pDest) CopyScanline(pDest, dpitch, pSrc, spitch, metadata.Format, tflags); } pSrc = (IntPtr) ((byte*) pSrc + spitch); pDest = (IntPtr) ((byte*) pDest + dpitch); } } } if (d > 1) d >>= 1; } } // Return the imageDst or the original image if (isCopyNeeded) { image.Dispose(); image = imageDst; } return image; }
public override string Convert(IMethod m, ConversionFlags conversionFlags) { StringBuilder builder = new StringBuilder(); builder.Append(Convert(m.Modifiers, conversionFlags)); if (ShowMemberModifiers(conversionFlags)) { builder.Append(GetModifier(m, conversionFlags)); } if (m.IsConstructor) { AppendPangoHtmlTag (builder, "this", "b", conversionFlags); } else { if (UseFullyQualifiedMemberNames(conversionFlags)) AppendPangoHtmlTag (builder, m.FullyQualifiedName, "b", conversionFlags); else AppendPangoHtmlTag (builder, m.Name, "b", conversionFlags); } // Display generic parameters only if told so if (ShowGenericParameters(conversionFlags) && m.GenericParameters != null && m.GenericParameters.Count > 0) { bool includeMarkup = IncludeHTMLMarkup(conversionFlags) || IncludePangoMarkup(conversionFlags); builder.Append ("["); // Since we know that there is at least one generic parameter in // the list, we can add it outside the loop - so, we don't have // to check whether we may append a comma or not builder.Append (m.GenericParameters[0].Name); // Now continue with the others, if there are any for (int i = 1; i < m.GenericParameters.Count; i++) { builder.Append (", "); builder.Append (m.GenericParameters[i].Name); } builder.Append ("]"); } builder.Append(" ("); if (m.Parameters.Count > 0) { if (IncludeHTMLMarkup(conversionFlags)) builder.Append ("<br>"); builder.Append (Convert (m.Parameters[0], conversionFlags)); for (int i = 1; i < m.Parameters.Count; ++i) { builder.Append (", "); if (IncludeHTMLMarkup(conversionFlags)) builder.Append("<br>"); builder.Append(Convert(m.Parameters[i], conversionFlags)); } if (IncludeHTMLMarkup(conversionFlags)) builder.Append("<br>"); } builder.Append(')'); if (m.ReturnType != null) { builder.Append(" : "); builder.Append(Convert(m.ReturnType, conversionFlags)); builder.Append(' '); } if (IncludeBodies(conversionFlags)) { if (m.DeclaringType != null) { if (m.DeclaringType.ClassType == ClassType.Interface) { builder.Append(";"); } else { builder.Append(" {"); } } else { builder.Append(" {"); } } return builder.ToString(); }
private static TEXP_LEGACY_FORMAT FindLegacyFormat(ConversionFlags flags) { var lformat = TEXP_LEGACY_FORMAT.UNKNOWN; if ((flags & ConversionFlags.Pal8) != 0) { lformat = (flags & ConversionFlags.FormatA8P8) != 0 ? TEXP_LEGACY_FORMAT.A8P8 : TEXP_LEGACY_FORMAT.P8; } else if ((flags & ConversionFlags.Format888) != 0) lformat = TEXP_LEGACY_FORMAT.R8G8B8; else if ((flags & ConversionFlags.Format332) != 0) lformat = TEXP_LEGACY_FORMAT.R3G3B2; else if ((flags & ConversionFlags.Format8332) != 0) lformat = TEXP_LEGACY_FORMAT.A8R3G3B2; else if ((flags & ConversionFlags.Format44) != 0) lformat = TEXP_LEGACY_FORMAT.A4L4; #if DIRECTX11_1 else if ((flags & ConversionFlags.Format4444) != 0) lformat = TEXP_LEGACY_FORMAT.B4G4R4A4; #endif return lformat; }
public override string Convert(IIndexer m, ConversionFlags conversionFlags) { StringBuilder builder = new StringBuilder(); builder.Append(Convert(m.Modifiers, conversionFlags)); if (ShowMemberModifiers(conversionFlags) && m.IsStatic) AppendPangoHtmlTag (builder, "static", "i", conversionFlags); if (UseFullyQualifiedMemberNames (conversionFlags)) AppendPangoHtmlTag (builder, m.FullyQualifiedName, "b", conversionFlags); else AppendPangoHtmlTag (builder, m.Name, "b", conversionFlags); builder.Append(" ["); if (m.Parameters.Count > 0) { if (IncludeHTMLMarkup(conversionFlags)) builder.Append ("<br>"); builder.Append (Convert (m.Parameters[0], conversionFlags)); for (int i = 1; i < m.Parameters.Count; ++i) { builder.Append (", "); if (IncludeHTMLMarkup(conversionFlags)) builder.Append("<br>"); builder.Append(Convert(m.Parameters[i], conversionFlags)); } if (IncludeHTMLMarkup(conversionFlags)) builder.Append("<br>"); } builder.Append(']'); if (m.ReturnType != null) { builder.Append(" : "); builder.Append(Convert(m.ReturnType, conversionFlags)); builder.Append(' '); } if (IncludeBodies(conversionFlags)) builder.Append(";"); return builder.ToString(); }
private static Header EncodeTgaHeader(Image image, ref ConversionFlags convFlags) { Header header = new Header(); if (image.Width > 0xFFFF || image.Height > 0xFFFF) throw new NotSupportedException("The specified image exceeds the maximum pixel size of a TGA file."); header.Width = (ushort)image.Width; header.Height = (ushort)image.Height; switch (image.Format) { case DataFormat.R8G8B8A8_UNORM: case DataFormat.R8G8B8A8_UNORM_SRGB: header.ImageType = ImageType.TrueColor; header.BitsPerPixel = 32; header.Descriptor = DescriptorFlags.InvertY | (DescriptorFlags)8; // 8 bit alpha convFlags |= ConversionFlags.Swizzle; break; case DataFormat.B8G8R8A8_UNORM: case DataFormat.B8G8R8A8_UNORM_SRGB: header.ImageType = ImageType.TrueColor; header.BitsPerPixel = 32; header.Descriptor = DescriptorFlags.InvertY | (DescriptorFlags)8; // 8 bit alpha break; case DataFormat.B8G8R8X8_UNORM: case DataFormat.B8G8R8X8_UNORM_SRGB: header.ImageType = ImageType.TrueColor; header.BitsPerPixel = 24; header.Descriptor = DescriptorFlags.InvertY; convFlags |= ConversionFlags.Format888; break; case DataFormat.R8_UNORM: case DataFormat.A8_UNORM: header.ImageType = ImageType.BlackAndWhite; header.BitsPerPixel = 8; header.Descriptor = DescriptorFlags.InvertY; break; case DataFormat.B5G5R5A1_UNORM: header.ImageType = ImageType.TrueColor; header.BitsPerPixel = 16; header.Descriptor = DescriptorFlags.InvertY | (DescriptorFlags)1; // 1 bit alpha break; } return header; }
public override string Convert(IEvent e, ConversionFlags conversionFlags) { StringBuilder builder = new StringBuilder(); builder.Append(Convert(e.Modifiers, conversionFlags)); if (ShowMemberModifiers(conversionFlags)) { builder.Append(GetModifier(e, conversionFlags)); } if (UseFullyQualifiedMemberNames(conversionFlags)) AppendPangoHtmlTag (builder, e.FullyQualifiedName, "b", conversionFlags); else AppendPangoHtmlTag (builder, e.Name, "b", conversionFlags); if (e.ReturnType != null) { builder.Append(" : "); builder.Append(Convert(e.ReturnType, conversionFlags)); builder.Append(' '); } if (IncludeBodies(conversionFlags)) builder.Append(";"); return builder.ToString(); }
private static TextureDescription DecodeDDSHeader(BinaryReader reader, DdsFlags flags, out ConversionFlags convFlags) { Debug.Assert(Marshal.SizeOf(typeof(Header)) == 124, "DDS Header size mismatch"); Debug.Assert(Marshal.SizeOf(typeof(HeaderDXT10)) == 20, "DDS DX10 Extended Header size mismatch"); var description = new TextureDescription(); convFlags = ConversionFlags.None; long size = reader.BaseStream.Length - reader.BaseStream.Position; int sizeOfTGAHeader = Marshal.SizeOf(typeof(Header)); if (size < sizeOfTGAHeader) throw new InvalidDataException("The DDS file is corrupt."); // DDS files always start with the same magic number ("DDS "). if (reader.ReadUInt32() != MagicHeader) throw new InvalidDataException("The file does not appear to be a DDS file."); var header = reader.BaseStream.ReadStruct<Header>(); // Verify header to validate DDS file if (header.Size != Marshal.SizeOf(typeof(Header)) || header.PixelFormat.Size != Marshal.SizeOf(typeof(PixelFormat))) throw new InvalidDataException("Incorrect sizes in DDS file."); description.MipLevels = header.MipMapCount; if (description.MipLevels == 0) description.MipLevels = 1; // Check for DX10 extension if ((header.PixelFormat.Flags & PixelFormatFlags.FourCC) != 0 && (new FourCC('D', 'X', '1', '0') == header.PixelFormat.FourCC)) { // Buffer must be big enough for both headers and magic value if (reader.BaseStream.Length < (Marshal.SizeOf(typeof(Header)) + Marshal.SizeOf(typeof(HeaderDXT10)) + sizeof(uint))) throw new InvalidDataException("The DDS files is truncated."); var headerDX10 = reader.BaseStream.ReadStruct<HeaderDXT10>(); convFlags |= ConversionFlags.DX10; description.ArraySize = headerDX10.ArraySize; if (description.ArraySize == 0) throw new InvalidDataException("Invalid array size specified in DDS file."); description.Format = headerDX10.DXGIFormat; if (!TextureHelper.IsValidDds(description.Format) || TextureHelper.IsPalettized(description.Format)) throw new InvalidDataException("Invalid format specified in DDS file."); switch (headerDX10.ResourceDimension) { case ResourceDimension.Texture1D: // D3DX writes 1D textures with a fixed Height of 1 if ((header.Flags & HeaderFlags.Height) != 0 && header.Height != 1) throw new InvalidDataException("Invalid height for 1D texture specified in DDS file."); description.Dimension = TextureDimension.Texture1D; description.Width = header.Width; description.Height = 1; description.Depth = 1; break; case ResourceDimension.Texture2D: if ((headerDX10.MiscFlags & ResourceOptionFlags.TextureCube) != 0) { description.Dimension = TextureDimension.TextureCube; description.ArraySize *= 6; } else { description.Dimension = TextureDimension.Texture2D; } description.Width = header.Width; description.Height = header.Height; description.Depth = 1; break; case ResourceDimension.Texture3D: if ((header.Flags & HeaderFlags.Volume) == 0) throw new InvalidDataException("Volume flag for 3D texture is missing in DDS file."); if (description.ArraySize > 1) throw new InvalidDataException("Invalid array size for 3D texture specified in DDS file."); description.Dimension = TextureDimension.Texture3D; description.Width = header.Width; description.Height = header.Height; description.Depth = header.Depth; break; default: throw new InvalidDataException(string.Format(CultureInfo.InvariantCulture, "Invalid texture dimension specified in DDS file.")); } } else { description.ArraySize = 1; if ((header.Flags & HeaderFlags.Volume) != 0) { description.Dimension = TextureDimension.Texture3D; description.Width = header.Width; description.Height = header.Height; description.Depth = header.Depth; } else { if ((header.CubemapFlags & CubemapFlags.CubeMap) != 0) { // We require all six faces to be defined if ((header.CubemapFlags & CubemapFlags.AllFaces) != CubemapFlags.AllFaces) throw new InvalidDataException("Cube map faces missing in DDS file. All six faces required."); description.Dimension = TextureDimension.TextureCube; description.ArraySize = 6; } else { description.Dimension = TextureDimension.Texture2D; } description.Width = header.Width; description.Height = header.Height; description.Depth = 1; // Note there's no way for a legacy Direct3D 9 DDS to express a '1D' texture } description.Format = GetDXGIFormat(ref header.PixelFormat, flags, out convFlags); if (description.Format == DataFormat.Unknown) throw new NotSupportedException("The texture format used in the DDS file is not supported."); // Premultiplied formats are considered deprecated. //if ((convFlags & ConversionFlags.PremultipliedAlpha) != 0) // Description.AlphaMode = AlphaMode.Premultiplied; // Special flag for handling LUMINANCE legacy formats if ((flags & DdsFlags.ExpandLuminance) != 0) { switch (description.Format) { case DataFormat.R8_UNORM: description.Format = DataFormat.R8G8B8A8_UNORM; convFlags |= ConversionFlags.FormatL8 | ConversionFlags.Expand; break; case DataFormat.R8G8_UNORM: description.Format = DataFormat.R8G8B8A8_UNORM; convFlags |= ConversionFlags.FormatA8L8 | ConversionFlags.Expand; break; case DataFormat.R16_UNORM: description.Format = DataFormat.R16G16B16A16_UNORM; convFlags |= ConversionFlags.FormatL16 | ConversionFlags.Expand; break; } } } // Special flag for handling BGR DXGI 1.1 formats if ((flags & DdsFlags.ForceRgb) != 0) { switch (description.Format) { case DataFormat.B8G8R8A8_UNORM: description.Format = DataFormat.R8G8B8A8_UNORM; convFlags |= ConversionFlags.Swizzle; break; case DataFormat.B8G8R8X8_UNORM: description.Format = DataFormat.R8G8B8A8_UNORM; convFlags |= ConversionFlags.Swizzle | ConversionFlags.NoAlpha; break; case DataFormat.B8G8R8A8_TYPELESS: description.Format = DataFormat.R8G8B8A8_TYPELESS; convFlags |= ConversionFlags.Swizzle; break; case DataFormat.B8G8R8A8_UNORM_SRGB: description.Format = DataFormat.R8G8B8A8_UNORM_SRGB; convFlags |= ConversionFlags.Swizzle; break; case DataFormat.B8G8R8X8_TYPELESS: description.Format = DataFormat.R8G8B8A8_TYPELESS; convFlags |= ConversionFlags.Swizzle | ConversionFlags.NoAlpha; break; case DataFormat.B8G8R8X8_UNORM_SRGB: description.Format = DataFormat.R8G8B8A8_UNORM_SRGB; convFlags |= ConversionFlags.Swizzle | ConversionFlags.NoAlpha; break; } } // Special flag for handling 16bpp formats if ((flags & DdsFlags.No16Bpp) != 0) { switch (description.Format) { case DataFormat.B5G6R5_UNORM: case DataFormat.B5G5R5A1_UNORM: case DataFormat.B4G4R4A4_UNORM: description.Format = DataFormat.R8G8B8A8_UNORM; convFlags |= ConversionFlags.Expand; if (description.Format == DataFormat.B5G6R5_UNORM) convFlags |= ConversionFlags.NoAlpha; break; } } return description; }
public override string Convert(IProperty property, ConversionFlags conversionFlags) { StringBuilder builder = new StringBuilder(); builder.Append(Convert(property.Modifiers, conversionFlags)); if (ShowMemberModifiers(conversionFlags)) { builder.Append(GetModifier(property, conversionFlags)); } if (UseFullyQualifiedMemberNames(conversionFlags)) AppendPangoHtmlTag (builder, property.FullyQualifiedName, "b", conversionFlags); else AppendPangoHtmlTag (builder, property.Name, "b", conversionFlags); if (property.Parameters.Count > 0) { builder.Append(" ("); if (IncludeHTMLMarkup(conversionFlags)) builder.Append("<br>"); builder.Append (Convert (property.Parameters[0], conversionFlags)); for (int i = 0; i < property.Parameters.Count; ++i) { if (IncludeHTMLMarkup(conversionFlags)) builder.Append("<br>"); builder.Append(", "); builder.Append(Convert(property.Parameters[i], conversionFlags)); } if (IncludeHTMLMarkup(conversionFlags)) builder.Append("<br>"); builder.Append(')'); } if (property.ReturnType != null) { builder.Append(" : "); builder.Append(Convert(property.ReturnType, conversionFlags)); builder.Append(' '); } builder.Append(" { "); if (property.CanGet) { builder.Append("get; "); } if (property.CanSet) { builder.Append("set; "); } builder.Append(" } "); return builder.ToString(); }
string GetModifier(IDecoration decoration, ConversionFlags conversionFlags) { string mod; if (decoration.IsStatic) mod = "static "; else if (decoration.IsFinal) mod = "final "; else if (decoration.IsVirtual) mod = "virtual "; else if (decoration.IsOverride) mod = "override "; else if (decoration.IsNew) mod = "new "; else return ""; if (IncludeHTMLMarkup(conversionFlags) | IncludePangoMarkup(conversionFlags)) return "<i>" + mod + "</i>"; else return mod; }
private static Texture CopyImage(BinaryReader reader, TextureDescription description, ComputePitchFlags cpFlags, ConversionFlags convFlags, uint[] pal8) { if (reader == null) throw new ArgumentNullException("reader"); if ((convFlags & ConversionFlags.Expand) != 0) { if ((convFlags & ConversionFlags.Format888) != 0) cpFlags |= ComputePitchFlags.Bpp24; else if ((convFlags & (ConversionFlags.Format565 | ConversionFlags.Format5551 | ConversionFlags.Format4444 | ConversionFlags.Format8332 | ConversionFlags.FormatA8P8 | ConversionFlags.FormatL16 | ConversionFlags.FormatA8L8)) != 0) cpFlags |= ComputePitchFlags.Bpp16; else if ((convFlags & (ConversionFlags.Format44 | ConversionFlags.Format332 | ConversionFlags.Pal8 | ConversionFlags.FormatL8)) != 0) cpFlags |= ComputePitchFlags.Bpp8; } var texture = new Texture(description); description = texture.Description; // MipLevel may have been set. ScanlineFlags tflags = (convFlags & ConversionFlags.NoAlpha) != 0 ? ScanlineFlags.SetAlpha : 0; if ((convFlags & ConversionFlags.Swizzle) != 0) tflags |= ScanlineFlags.Legacy; switch (description.Dimension) { case TextureDimension.Texture1D: case TextureDimension.Texture2D: case TextureDimension.TextureCube: { int index = 0; for (int item = 0; item < description.ArraySize; ++item) { int width = description.Width; int height = description.Height; for (int level = 0; level < description.MipLevels; ++level, ++index) { int sRowPitch, sSlicePitch; TextureHelper.ComputePitch(description.Format, width, height, out sRowPitch, out sSlicePitch, cpFlags); var image = texture.Images[index]; if (TextureHelper.IsBCn(description.Format) || TextureHelper.IsPlanar(description.Format)) { reader.Read(image.Data, 0, image.Data.Length); } else { using (var stream = new MemoryStream(image.Data)) using (var writer = new BinaryWriter(stream)) { for (int h = 0; h < height; ++h) { if ((convFlags & ConversionFlags.Expand) != 0) { if ((convFlags & (ConversionFlags.Format565 | ConversionFlags.Format5551 | ConversionFlags.Format4444)) != 0) { if (!TextureHelper.ExpandScanline(reader, sRowPitch, (convFlags & ConversionFlags.Format565) != 0 ? DataFormat.B5G6R5_UNORM : DataFormat.B5G5R5A1_UNORM, writer, image.RowPitch, DataFormat.R8G8B8A8_UNORM, tflags)) throw new InvalidDataException("Unable to expand format."); } else { LegacyFormat lformat = FindLegacyFormat(convFlags); if (!LegacyExpandScanline(reader, sRowPitch, lformat, writer, image.RowPitch, description.Format, pal8, tflags)) throw new InvalidDataException("Unable to expand legacy format."); } } else if ((convFlags & ConversionFlags.Swizzle) != 0) { TextureHelper.SwizzleScanline(reader, sRowPitch, writer, image.RowPitch, description.Format, tflags); } else { TextureHelper.CopyScanline(reader, sRowPitch, writer, image.RowPitch, description.Format, tflags); } } } } if (width > 1) width >>= 1; if (height > 1) height >>= 1; } } } break; case TextureDimension.Texture3D: { int index = 0; int width = description.Width; int height = description.Height; int depth = description.Depth; for (int level = 0; level < description.MipLevels; ++level) { int sRowPitch, sSlicePitch; TextureHelper.ComputePitch(description.Format, width, height, out sRowPitch, out sSlicePitch, cpFlags); for (int slice = 0; slice < depth; ++slice, ++index) { // We use the same memory organization that Direct3D 11 needs for D3D11_SUBRESOURCE_DATA // with all slices of a given miplevel being continuous in memory var image = texture.Images[index]; if (TextureHelper.IsBCn(description.Format)) { reader.Read(image.Data, 0, image.Data.Length); } else if (TextureHelper.IsPlanar(description.Format)) { // Direct3D does not support any planar formats for Texture3D throw new NotSupportedException("Planar texture formats are not support for volume textures."); } else { using (var stream = new MemoryStream(image.Data)) using (var writer = new BinaryWriter(stream)) { for (int h = 0; h < height; ++h) { if ((convFlags & ConversionFlags.Expand) != 0) { if ((convFlags & (ConversionFlags.Format565 | ConversionFlags.Format5551 | ConversionFlags.Format4444)) != 0) { if (!TextureHelper.ExpandScanline(reader, sRowPitch, (convFlags & ConversionFlags.Format565) != 0 ? DataFormat.B5G6R5_UNORM : DataFormat.B5G5R5A1_UNORM, writer, image.RowPitch, DataFormat.R8G8B8A8_UNORM, tflags)) throw new InvalidDataException("Unable to expand format."); } else { LegacyFormat lformat = FindLegacyFormat(convFlags); if (!LegacyExpandScanline(reader, sRowPitch, lformat, writer, image.RowPitch, description.Format, pal8, tflags)) throw new InvalidDataException("Unable to expand legacy format."); } } else if ((convFlags & ConversionFlags.Swizzle) != 0) { TextureHelper.SwizzleScanline(reader, sRowPitch, writer, image.RowPitch, description.Format, tflags); } else { TextureHelper.CopyScanline(reader, sRowPitch, writer, image.RowPitch, description.Format, tflags); } } } } } if (width > 1) width >>= 1; if (height > 1) height >>= 1; if (depth > 1) depth >>= 1; } } break; default: throw new NotSupportedException("The specified texture dimension is not supported."); } return texture; }
/// <summary> /// Decodes DDS header including optional DX10 extended header /// </summary> /// <param name="headerPtr">Pointer to the DDS header.</param> /// <param name="size">Size of the DDS content.</param> /// <param name="flags">Flags used for decoding the DDS header.</param> /// <param name="description">Output texture description.</param> /// <param name="convFlags">Output conversion flags.</param> /// <exception cref="ArgumentException">If the argument headerPtr is null</exception> /// <exception cref="InvalidOperationException">If the DDS header contains invalid datas.</exception> /// <returns>True if the decoding is successfull, false if this is not a DDS header.</returns> private static unsafe bool DecodeDDSHeader(IntPtr headerPtr, int size, DDSFlags flags, out ImageDescription description, out ConversionFlags convFlags) { description = new ImageDescription(); convFlags = ConversionFlags.None; if (headerPtr == IntPtr.Zero) throw new ArgumentException("Pointer to DDS header cannot be null", "headerPtr"); if (size < (Utilities.SizeOf<DDS.Header>() + sizeof(uint))) return false; // DDS files always start with the same magic number ("DDS ") if (*(uint*)(headerPtr) != DDS.MagicHeader) return false; var header = *(DDS.Header*)((byte*)headerPtr + sizeof(int)); // Verify header to validate DDS file if (header.Size != Utilities.SizeOf<DDS.Header>() || header.PixelFormat.Size != Utilities.SizeOf<DDS.PixelFormat>()) return false; // Setup MipLevels description.MipLevels = header.MipMapCount; if (description.MipLevels == 0) description.MipLevels = 1; // Check for DX10 extension if ((header.PixelFormat.Flags & DDS.PixelFormatFlags.FourCC) != 0 && (new FourCC('D', 'X', '1', '0') == header.PixelFormat.FourCC)) { // Buffer must be big enough for both headers and magic value if (size < (Utilities.SizeOf<DDS.Header>() + sizeof(uint) + Utilities.SizeOf<DDS.HeaderDXT10>())) return false; var headerDX10 = *(DDS.HeaderDXT10*)((byte*)headerPtr + sizeof(int) + Utilities.SizeOf<DDS.Header>()); convFlags |= ConversionFlags.DX10; description.ArraySize = headerDX10.ArraySize; if (description.ArraySize == 0) throw new InvalidOperationException("Unexpected ArraySize == 0 from DDS HeaderDX10 "); description.Format = headerDX10.DXGIFormat; if (!FormatHelper.IsValid(description.Format)) throw new InvalidOperationException("Invalid Format from DDS HeaderDX10 "); switch (headerDX10.ResourceDimension) { case ResourceDimension.Texture1D: // D3DX writes 1D textures with a fixed Height of 1 if ((header.Flags & DDS.HeaderFlags.Height) != 0 && header.Height != 1) throw new InvalidOperationException("Unexpected Height != 1 from DDS HeaderDX10 "); description.Width = header.Width; description.Height = 1; description.Depth = 1; description.Dimension = TextureDimension.Texture1D; break; case ResourceDimension.Texture2D: if ((headerDX10.MiscFlags & ResourceOptionFlags.TextureCube) != 0) { description.ArraySize *= 6; description.Dimension = TextureDimension.TextureCube; } else { description.Dimension = TextureDimension.Texture2D; } description.Width = header.Width; description.Height = header.Height; description.Depth = 1; break; case ResourceDimension.Texture3D: if ((header.Flags & DDS.HeaderFlags.Volume) == 0) throw new InvalidOperationException("Texture3D missing HeaderFlags.Volume from DDS HeaderDX10"); if (description.ArraySize > 1) throw new InvalidOperationException("Unexpected ArraySize > 1 for Texture3D from DDS HeaderDX10"); description.Width = header.Width; description.Height = header.Height; description.Depth = header.Depth; description.Dimension = TextureDimension.Texture3D; break; default: throw new InvalidOperationException(string.Format("Unexpected dimension [{0}] from DDS HeaderDX10", headerDX10.ResourceDimension)); } } else { description.ArraySize = 1; if ((header.Flags & DDS.HeaderFlags.Volume) != 0) { description.Width = header.Width; description.Height = header.Height; description.Depth = header.Depth; description.Dimension = TextureDimension.Texture3D; } else { if ((header.CubemapFlags & DDS.CubemapFlags.CubeMap) != 0) { // We require all six faces to be defined if ((header.CubemapFlags & DDS.CubemapFlags.AllFaces) != DDS.CubemapFlags.AllFaces) throw new InvalidOperationException("Unexpected CubeMap, expecting all faces from DDS Header"); description.ArraySize = 6; description.Dimension = TextureDimension.TextureCube; } else { description.Dimension = TextureDimension.Texture2D; } description.Width = header.Width; description.Height = header.Height; description.Depth = 1; // Note there's no way for a legacy Direct3D 9 DDS to express a '1D' texture } description.Format = GetDXGIFormat(ref header.PixelFormat, flags, out convFlags); if (description.Format == Format.Unknown) throw new InvalidOperationException("Unsupported PixelFormat from DDS Header"); } // Special flag for handling BGR DXGI 1.1 formats if ((flags & DDSFlags.ForceRgb) != 0) { switch ((Format)description.Format) { case Format.B8G8R8A8_UNorm: description.Format = Format.R8G8B8A8_UNorm; convFlags |= ConversionFlags.Swizzle; break; case Format.B8G8R8X8_UNorm: description.Format = Format.R8G8B8A8_UNorm; convFlags |= ConversionFlags.Swizzle | ConversionFlags.NoAlpha; break; case Format.B8G8R8A8_Typeless: description.Format = Format.R8G8B8A8_Typeless; convFlags |= ConversionFlags.Swizzle; break; case Format.B8G8R8A8_UNorm_SRgb: description.Format = Format.R8G8B8A8_UNorm_SRgb; convFlags |= ConversionFlags.Swizzle; break; case Format.B8G8R8X8_Typeless: description.Format = Format.R8G8B8A8_Typeless; convFlags |= ConversionFlags.Swizzle | ConversionFlags.NoAlpha; break; case Format.B8G8R8X8_UNorm_SRgb: description.Format = Format.R8G8B8A8_UNorm_SRgb; convFlags |= ConversionFlags.Swizzle | ConversionFlags.NoAlpha; break; } } // Pass DDSFlags copy memory to the conversion flags if ((flags & DDSFlags.CopyMemory) != 0) convFlags |= ConversionFlags.CopyMemory; // Special flag for handling 16bpp formats if ((flags & DDSFlags.No16Bpp) != 0) { switch ((Format)description.Format) { case Format.B5G6R5_UNorm: case Format.B5G5R5A1_UNorm: case Format.B4G4R4A4_UNorm: description.Format = Format.R8G8B8A8_UNorm; convFlags |= ConversionFlags.Expand; if (description.Format == Format.B5G6R5_UNorm) convFlags |= ConversionFlags.NoAlpha; break; } } return true; }
//------------------------------------------------------------------------------------- // Decodes TGA header //------------------------------------------------------------------------------------- public static bool DecodeTGAHeader(IntPtr pSource, int size, out ImageDescription metadata, out int offset, out ConversionFlags convFlags) { metadata = new ImageDescription(); offset = 0; convFlags = ConversionFlags.None; int sizeOfTGAHeader = Marshal.SizeOf(typeof(Header)); if (size < sizeOfTGAHeader) return false; var header = (Header) Marshal.PtrToStructure(pSource, typeof (Header)); if (header.ColorMapType != 0 || header.ColorMapLength != 0) { throw new NotSupportedException(); } if ((header.Descriptor & (DescriptorFlags.Interleaved2Way | DescriptorFlags.Interleaved4Way)) != 0) { throw new NotSupportedException(); } if (header.Width == 0 || header.Height == 0) { throw new NotSupportedException("TGA Invalid Width or Height (=0)"); } switch (header.ImageType) { case ImageType.TrueColor: case ImageType.TrueColorRLE: switch (header.BitsPerPixel) { case 16: metadata.Format = DXGI.Format.B5G5R5A1_UNorm; break; case 24: metadata.Format = DXGI.Format.R8G8B8A8_UNorm; convFlags |= ConversionFlags.Expand; break; case 32: metadata.Format = DXGI.Format.R8G8B8A8_UNorm; // We could use DXGI.Format.B8G8R8A8_UNORM, but we prefer DXGI 1.0 formats break; } if (header.ImageType == ImageType.TrueColorRLE) { convFlags |= ConversionFlags.RLE; } break; case ImageType.BlackAndWhite: case ImageType.BlackAndWhiteRLE: switch (header.BitsPerPixel) { case 8: metadata.Format = DXGI.Format.R8_UNorm; break; default: throw new NotSupportedException(); } if (header.ImageType == ImageType.BlackAndWhiteRLE) { convFlags |= ConversionFlags.RLE; } break; case ImageType.NoImage: case ImageType.ColorMapped: case ImageType.ColorMappedRLE: //throw new NotSupportedException(); return false; default: throw new NotSupportedException("TGA Invalid Data"); } metadata.Width = header.Width; metadata.Height = header.Height; metadata.Depth = metadata.ArraySize = metadata.MipLevels = 1; metadata.Dimension = TextureDimension.Texture2D; if ((header.Descriptor & DescriptorFlags.InvertX) != 0) convFlags |= ConversionFlags.InvertX; if ((header.Descriptor & DescriptorFlags.InvertY) != 0) convFlags |= ConversionFlags.InvertY; offset = sizeOfTGAHeader; if (header.IDLength != 0) { offset += header.IDLength; } return true; }
/// <summary> /// Initializes a new instance of the <see cref="LegacyMap" /> struct. /// </summary> /// <param name="format">The format.</param> /// <param name="conversionFlags">The conversion flags.</param> /// <param name="pixelFormat">The pixel format.</param> public LegacyMap(Format format, ConversionFlags conversionFlags, DDS.PixelFormat pixelFormat) { Format = format; ConversionFlags = conversionFlags; PixelFormat = pixelFormat; }
private static LegacyFormat FindLegacyFormat(ConversionFlags flags) { var lformat = LegacyFormat.Unknown; if ((flags & ConversionFlags.Pal8) != 0) lformat = (flags & ConversionFlags.FormatA8P8) != 0 ? LegacyFormat.A8P8 : LegacyFormat.P8; else if ((flags & ConversionFlags.Format888) != 0) lformat = LegacyFormat.R8G8B8; else if ((flags & ConversionFlags.Format332) != 0) lformat = LegacyFormat.R3G3B2; else if ((flags & ConversionFlags.Format8332) != 0) lformat = LegacyFormat.A8R3G3B2; else if ((flags & ConversionFlags.Format44) != 0) lformat = LegacyFormat.A4L4; else if ((flags & ConversionFlags.Format4444) != 0) lformat = LegacyFormat.B4G4R4A4; else if ((flags & ConversionFlags.FormatL8) != 0) lformat = LegacyFormat.L8; else if ((flags & ConversionFlags.FormatL16) != 0) lformat = LegacyFormat.L16; else if ((flags & ConversionFlags.FormatA8L8) != 0) lformat = LegacyFormat.A8L8; return lformat; }