public static System.Drawing.Color GetAttributeAsColor(XElement elem, string attrName) { string colorString = elem.Attribute(attrName).Value; System.Drawing.Color color = TmxHelper.ColorFromHtml(colorString); return(color); }
public static TmxImage FromXml(XElement elemImage) { TmxImage tmxImage = new TmxImage(); tmxImage.AbsolutePath = TmxHelper.GetAttributeAsFullPath(elemImage, "source"); // Get default image size in case we are not opening the file { int width = TmxHelper.GetAttributeAsInt(elemImage, "width", 0); int height = TmxHelper.GetAttributeAsInt(elemImage, "height", 0); tmxImage.Size = new System.Drawing.Size(width, height); } // Do not open the image in Tiled2UnityLite (due to difficulty with GDI+ in some mono installs) #if !TILED_2_UNITY_LITE if (!Tiled2Unity.Settings.IsAutoExporting) { try { tmxImage.ImageBitmap = TmxHelper.FromFileBitmap32bpp(tmxImage.AbsolutePath); } catch (FileNotFoundException fnf) { string msg = String.Format("Image file not found: {0}", tmxImage.AbsolutePath); throw new TmxException(msg, fnf); // Testing for when image files are missing. Just make up an image. //int width = TmxHelper.GetAttributeAsInt(elemImage, "width"); //int height = TmxHelper.GetAttributeAsInt(elemImage, "height"); //tmxImage.ImageBitmap = new TmxHelper.CreateBitmap32bpp(width, height); //using (Graphics g = Graphics.FromImage(tmxImage.ImageBitmap)) //{ // int color32 = tmxImage.AbsolutePath.GetHashCode(); // Color color = Color.FromArgb(color32); // color = Color.FromArgb(255, color); // using (Brush brush = new SolidBrush(color)) // { // g.FillRectangle(brush, new Rectangle(Point.Empty, tmxImage.ImageBitmap.Size)); // } //} } tmxImage.Size = new System.Drawing.Size(tmxImage.ImageBitmap.Width, tmxImage.ImageBitmap.Height); } #endif // Some images use a transparency color key instead of alpha (blerg) tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", ""); #if !TILED_2_UNITY_LITE if (!String.IsNullOrEmpty(tmxImage.TransparentColor) && tmxImage.ImageBitmap != null) { System.Drawing.Color transColor = TmxHelper.ColorFromHtml(tmxImage.TransparentColor); tmxImage.ImageBitmap.MakeTransparent(transColor); } #endif return(tmxImage); }
public static TmxImage FromXml(XElement elemImage, string prefix, string postfix) { TmxImage tmxImage = new TmxImage(); tmxImage.AbsolutePath = TmxHelper.GetAttributeAsFullPath(elemImage, "source"); tmxImage.ImageName = String.Format("{0}{1}{2}", prefix, Path.GetFileNameWithoutExtension(tmxImage.AbsolutePath), postfix); // Get default image size in case we are not opening the file { int width = TmxHelper.GetAttributeAsInt(elemImage, "width", 0); int height = TmxHelper.GetAttributeAsInt(elemImage, "height", 0); tmxImage.Size = new System.Drawing.Size(width, height); } bool canMakeTransparentPixels = true; // Do not open the image in Tiled2UnityLite (no SkiaSharp in Tiled2UnityLite) #if !TILED_2_UNITY_LITE if (!Tiled2Unity.Settings.IsAutoExporting) { try { if (!tmxImage.Size.IsEmpty) { // We know our size and can decode the image into our prefered format var info = new SKImageInfo(); info.ColorType = SKColorType.Rgba8888; info.AlphaType = SKAlphaType.Unpremul; info.Width = tmxImage.Size.Width; info.Height = tmxImage.Size.Height; tmxImage.ImageBitmap = SKBitmap.Decode(tmxImage.AbsolutePath, info); } else { // Open the image without any helpful information // This stops us from being able to make pixels transparent tmxImage.ImageBitmap = SKBitmap.Decode(tmxImage.AbsolutePath); canMakeTransparentPixels = false; } } catch (FileNotFoundException fnf) { string msg = String.Format("Image file not found: {0}", tmxImage.AbsolutePath); throw new TmxException(msg, fnf); } catch (Exception e) { // Disable previewing. Some users are reporting problems. Perhaps due to older versions of windows. Logger.WriteError("Error creating image with Skia Library. Exception: {0}", e.Message); Tiled2Unity.Settings.DisablePreviewing(); } tmxImage.Size = new System.Drawing.Size(tmxImage.ImageBitmap.Width, tmxImage.ImageBitmap.Height); } #endif // Some images use a transparency color key instead of alpha (blerg) tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", ""); #if !TILED_2_UNITY_LITE if (!String.IsNullOrEmpty(tmxImage.TransparentColor) && tmxImage.ImageBitmap != null) { if (canMakeTransparentPixels) { Logger.WriteLine("Removing alpha from transparent pixels."); System.Drawing.Color systemTransColor = TmxHelper.ColorFromHtml(tmxImage.TransparentColor); // Set the transparent pixels if using color-keying SKColor transColor = new SKColor((uint)systemTransColor.ToArgb()).WithAlpha(0); for (int x = 0; x < tmxImage.ImageBitmap.Width; ++x) { for (int y = 0; y < tmxImage.ImageBitmap.Height; ++y) { SKColor pixel = tmxImage.ImageBitmap.GetPixel(x, y); if (pixel.Red == transColor.Red && pixel.Green == transColor.Green && pixel.Blue == transColor.Blue) { tmxImage.ImageBitmap.SetPixel(x, y, transColor); } } } } else { Logger.WriteWarning("Cannot make transparent pixels for viewing purposes. Save tileset with newer verion of Tiled."); } } #endif return(tmxImage); }
public static TmxImage FromXml(XElement elemImage, string prefix, string postfix) { TmxImage tmxImage = new TmxImage(); tmxImage.AbsolutePath = TmxHelper.GetAttributeAsFullPath(elemImage, "source"); tmxImage.ImageName = $"{prefix}{Path.GetFileNameWithoutExtension(tmxImage.AbsolutePath)}{postfix}"; int attributeAsInt = TmxHelper.GetAttributeAsInt(elemImage, "width", 0); int attributeAsInt2 = TmxHelper.GetAttributeAsInt(elemImage, "height", 0); tmxImage.Size = new Size(attributeAsInt, attributeAsInt2); bool flag = true; if (!Settings.IsAutoExporting) { try { if (!tmxImage.Size.IsEmpty) { UnityEngine.Texture2D texture2 = new UnityEngine.Texture2D(tmxImage.Size.Width, tmxImage.Size.Height); //bitmapInfo.AlphaType = SKAlphaType.Unpremul; //bitmapInfo.Width = tmxImage.Size.Width; //bitmapInfo.Height = tmxImage.Size.Height; var v = File.ReadAllBytes(tmxImage.AbsolutePath); // ImageMagick.MagickImage img = new ImageMagick.MagickImage(v); texture2.LoadImage(v); //texture2.LoadRawTextureData(v); tmxImage.ImageBitmap = texture2; //using (FileStream stream = File.Open(tmxImage.AbsolutePath, FileMode.Open)) //{ // tmxImage.ImageBitmap = SKBitmap.Decode(stream, bitmapInfo); //} } else { UnityEngine.Texture2D texture2 = new UnityEngine.Texture2D(tmxImage.Size.Width, tmxImage.Size.Height); var v = File.ReadAllBytes(tmxImage.AbsolutePath); texture2.LoadImage(v); tmxImage.ImageBitmap = texture2; flag = false; } TmxImage tmxImage2 = tmxImage; tmxImage2.Size = new Size(tmxImage2.ImageBitmap.width, tmxImage.ImageBitmap.height); } catch (FileNotFoundException inner) { throw new TmxException($"Image file not found: {tmxImage.AbsolutePath}", inner); } catch (Exception ex) { Logger.WriteError("Skia Library exception: {0}\n\tStack:\n{1}", ex.Message, ex.StackTrace); Settings.DisablePreviewing(); } } tmxImage.TransparentColor = TmxHelper.GetAttributeAsString(elemImage, "trans", ""); if (!string.IsNullOrEmpty(tmxImage.TransparentColor) && tmxImage.ImageBitmap != null) { if (flag) { Logger.WriteInfo("Removing alpha from transparent pixels."); UnityEngine.Color color = TmxHelper.ColorFromHtml(tmxImage.TransparentColor); color.a = 0; for (int i = 0; i < tmxImage.ImageBitmap.width; i++) { for (int j = 0; j < tmxImage.ImageBitmap.height; j++) { UnityEngine.Color pixel = tmxImage.ImageBitmap.GetPixel(i, j); if (pixel.r == color.r && pixel.g == color.g && pixel.b == color.b) { tmxImage.ImageBitmap.SetPixel(i, j, color); } } } } else { Logger.WriteWarning("Cannot make transparent pixels for viewing purposes. Save tileset with newer verion of Tiled."); } } return(tmxImage); }