public override Object Clone(bool CopyMemory) { var atlas = new TexAtlas(Layout, (TexImage)base.Clone(CopyMemory)); atlas.Layout = new TexLayout(); foreach (var entry in Layout.TexList) { atlas.Layout.TexList.Add(entry.Key, entry.Value); } return atlas; }
/// <summary> /// Creates an atlas with the given TexImage. /// </summary> /// <param name="textureList">The texture list.</param> /// <param name="forceSquaredAtlas">boolean to decide wheter the atlas will be squared (default is false).</param> /// <returns>An instance of <see cref="TexAtlas"/>.</returns> /// <exception cref="TextureToolsException">No available library could create the atlas.</exception> public TexAtlas CreateAtlas(List<TexImage> textureList, bool forceSquaredAtlas = false) { TexAtlas atlas = new TexAtlas(); AtlasCreationRequest request = new AtlasCreationRequest(textureList, forceSquaredAtlas); ITexLibrary library = FindLibrary(atlas, request); if(library == null) { Log.Error("No available library could create the atlas."); throw new TextureToolsException("No available library could create the atlas."); } atlas.Format = textureList[0].Format; foreach (TexImage texture in textureList) { if (texture.Format != atlas.Format) { Log.Error("The textures in the list must all habe the same format."); throw new TextureToolsException("The textures in the list must all habe the same format."); } texture.Update(); } ExecuteRequest(atlas, request); return atlas; }
/// <summary> /// Checks the conformity of an atlas an its corresponding layout. /// </summary> /// <param name="atlas">The atlas.</param> /// <param name="layout">The layout.</param> /// <exception cref="TextureToolsException">The layout doesn't match the given atlas file.</exception> private void CheckConformity(TexAtlas atlas, TexAtlas.TexLayout layout) { int rightestPoint = 0; int lowestPoint = 0; foreach (var entry in layout.TexList) { if (entry.Value.UOffset + entry.Value.Width > rightestPoint) rightestPoint = entry.Value.UOffset + entry.Value.Width; if (entry.Value.VOffset + entry.Value.Height > lowestPoint) lowestPoint = entry.Value.VOffset + entry.Value.Height; } if (rightestPoint > atlas.Width || lowestPoint > atlas.Height) { Log.Error("The layout doesn't match the given atlas file."); throw new TextureToolsException("The layout doesn't match the given atlas file."); } }
/// <summary> /// Loads the Atlas corresponding to the specified texture file and layout file. /// </summary> /// <param name="layoutFile">The layout.</param> /// <param name="file">The file.</param> /// <returns>An instance of <see cref="TexAtlas"/>.</returns> /// <exception cref="TextureToolsException"> /// The file doesn't exist. Please check the file path. /// or /// The layout doesn't match the given atlas file. /// </exception> public TexAtlas LoadAtlas(string file, string layoutFile = "") { if (!File.Exists(file)) { Log.Error("The file " + file + " doesn't exist. Please check the file path."); throw new TextureToolsException("The file " + file + " doesn't exist. Please check the file path."); } if (!layoutFile.Equals("") && !File.Exists(layoutFile)) { Log.Error("The file " + layoutFile + " doesn't exist. Please check the file path."); throw new TextureToolsException("The file " + layoutFile + " doesn't exist. Please check the file path."); } else { layoutFile = Path.ChangeExtension(file, TexAtlas.TexLayout.Extension); if (!File.Exists(layoutFile)) { Log.Error("Please check that the layout file is in the same directory as the atlas, with the same name and " + TexAtlas.TexLayout.Extension + " as extension."); throw new TextureToolsException("Please check that the layout file is in the same directory as the atlas, with the same name and ." + TexAtlas.TexLayout.Extension + " as extension."); } } var layout = TexAtlas.TexLayout.Import(layoutFile); var atlas = new TexAtlas(layout, Load(new LoadingRequest(file, false))); CheckConformity(atlas, layout); return atlas; }
/// <summary> /// Loads the Atlas corresponding to the specified layout and file. /// </summary> /// <param name="layout">The layout.</param> /// <param name="file">The file.</param> /// <returns>An instance of <see cref="TexAtlas"/>.</returns> /// <exception cref="TextureToolsException"> /// The file doesn't exist. Please check the file path. /// or /// The layout doesn't match the given atlas file. /// </exception> public TexAtlas LoadAtlas(TexAtlas.TexLayout layout, string file) { if (!File.Exists(file)) { Log.Error("The file " + file + " doesn't exist. Please check the file path."); throw new TextureToolsException("The file " + file + " doesn't exist. Please check the file path."); } var atlas = new TexAtlas(layout, Load(new LoadingRequest(file, false))); CheckConformity(atlas, layout); return atlas; }
/// <summary> /// Updates a specific texture in the atlas with the given TexImage. /// </summary> /// <param name="atlas">The atlas.</param> /// <param name="texture">The texture.</param> /// <param name="name">The name of the texture to update (takes the TexImage's name if none given).</param> /// <exception cref="TextureToolsException"> /// You must either set the Name attribute of the TexImage, or you must give the name of the texture to update in the atlas. /// or /// The new texture can't be a texture array. /// </exception> public void Update(TexAtlas atlas, TexImage texture, string name = "") { texture.Update(); if (texture.Name.Equals("") && name.Equals("")) { Log.Error("You must either set the Name attribute of the TexImage, or you must give the name of the texture to update in the atlas."); throw new TextureToolsException("You must either set the Name attribute of the TexImage, or you must give the name of the texture to update in the atlas."); } if (texture.ArraySize > 1) { Log.Error("The new texture can't be a texture array."); throw new TextureToolsException("The new texture can't be a texture array."); } CheckConformity(atlas, texture); name = name.Equals("") ? texture.Name : name; ExecuteRequest(atlas, new AtlasUpdateRequest(texture, name)); }
/// <summary> /// Extracts every TexImage included in the atlas. /// </summary> /// <param name="atlas">The atlas.</param> /// <param name="minimumMipmapSize">The minimum size of the smallest mipmap.</param> /// <returns>The list of TexImage corresponding to each texture composing the atlas.</returns> /// <exception cref="TextureToolsException">The minimup Mipmap size can't be negative. Put 0 or 1 for a complete Mipmap chain.</exception> public List<TexImage> ExtractAll(TexAtlas atlas, int minimumMipmapSize = 1) { if (minimumMipmapSize < 0) { Log.Error("The minimup Mipmap size can't be negative. Put 0 or 1 for a complete Mipmap chain."); throw new TextureToolsException("The minimup Mipmap size can't be negative. Put 0 or 1 for a complete Mipmap chain."); } var request = new AtlasExtractionRequest(minimumMipmapSize); if (atlas.Format.IsCompressed()) { Log.Warning("You can't extract a texture from a compressed atlas. The atlas will be decompressed first.."); Decompress(atlas, atlas.Format.IsSRgb()); } ExecuteRequest(atlas, request); return request.Textures; }
/// <summary> /// Extracts the TexImage corresponding to the specified name in the given atlas. /// </summary> /// <param name="atlas">The atlas.</param> /// <param name="name">The texture name.</param> /// <param name="minimumMipmapSize">The minimum size of the smallest mipmap.</param> /// <returns>The TexImage texture corresponding to the specified name</returns> /// <exception cref="TextureToolsException"> /// You must enter a texture name to extract. /// or /// The minimup Mipmap size can't be negative. Put 0 or 1 for a complete Mipmap chain. /// </exception> public TexImage Extract(TexAtlas atlas, string name, int minimumMipmapSize = 1) { if (name == null || name.Equals("")) { Log.Error("You must enter a texture name to extract."); throw new TextureToolsException("You must enter a texture name to extract."); } if (minimumMipmapSize < 0) { Log.Error("The minimup Mipmap size can't be negative. Put 0 or 1 for a complete Mipmap chain."); throw new TextureToolsException("The minimup Mipmap size can't be negative. Put 0 or 1 for a complete Mipmap chain."); } var request = new AtlasExtractionRequest(name, minimumMipmapSize); if (atlas.Format.IsCompressed()) { Decompress(atlas, atlas.Format.IsSRgb()); } ExecuteRequest(atlas, request); return request.Texture; }