/// <summary> /// Converts the specified texture to another format. /// </summary> /// <param name="format">The desired texture format.</param> /// <returns> /// The texture using <paramref name="format" />. Does nothing (returns <c>this</c>) if texture /// already has the desired format. /// </returns> /// <exception cref="NotSupportedException"> /// Texture conversion to the specified format is not supported. /// </exception> public Texture ConvertTo(DataFormat format) { var srcFormat = Description.Format; var dstFormat = format; if (srcFormat == dstFormat) return this; // ----- Direct conversion: // srcFormat -> dstFormat if (TextureHelper.CanConvert(srcFormat, dstFormat)) { var description = Description; description.Format = dstFormat; var texture = new Texture(description); #if SINGLE_THREADED for (int i = 0; i < Images.Count; i++) #else Parallel.For(0, Images.Count, i => #endif { TextureHelper.Convert(Images[i], texture.Images[i]); } #if !SINGLE_THREADED ); #endif return texture; } // ----- Conversion using intermediate formats: // srcFormat -> R32G32B32A32_FLOAT -> dstFormat if (TextureHelper.CanConvert(srcFormat, DataFormat.R32G32B32A32_FLOAT) && TextureHelper.CanConvert(DataFormat.R32G32B32A32_FLOAT, dstFormat)) { var texture = ConvertTo(DataFormat.R32G32B32A32_FLOAT); return texture.ConvertTo(dstFormat); } // srcFormat -> R8G8B8A8_UNORM -> dstFormat if (TextureHelper.CanConvert(srcFormat, DataFormat.R8G8B8A8_UNORM) && TextureHelper.CanConvert(DataFormat.R8G8B8A8_UNORM, dstFormat)) { var texture = ConvertTo(DataFormat.R8G8B8A8_UNORM); return texture.ConvertTo(dstFormat); } throw new NotSupportedException(string.Format("Texture format conversion from {0} to {1} is not supported.", srcFormat, dstFormat)); }
/// <summary> /// Determines whether conversion to the specified texture format is supported. /// </summary> /// <param name="format">The desired texture format.</param> /// <returns> /// <see langword="true"/> if the conversion from the current format to <paramref name="format"/> /// is supported; otherwise, <see langword="false"/>. /// </returns> public bool CanConvertTo(DataFormat format) { var srcFormat = Description.Format; var dstFormat = format; // srcFormat -> dstFormat if (TextureHelper.CanConvert(srcFormat, dstFormat)) return true; // srcFormat -> R32G32B32A32_FLOAT -> dstFormat if (TextureHelper.CanConvert(srcFormat, DataFormat.R32G32B32A32_FLOAT) && TextureHelper.CanConvert(DataFormat.R32G32B32A32_FLOAT, dstFormat)) return true; // srcFormat -> R8G8B8A8_UNORM -> dstFormat if (TextureHelper.CanConvert(srcFormat, DataFormat.R8G8B8A8_UNORM) && TextureHelper.CanConvert(DataFormat.R8G8B8A8_UNORM, dstFormat)) return true; return false; }