/// <summary> /// Calculates the margins of a sprite, essentially trimming away empty space (alpha = 0) /// </summary> /// <param name="sprite">Sprite to calculate margins for</param> private static void CalculateSpriteMargins(UnpackedSprite sprite) { var pixels = sprite.pixels; int left = sprite.width; int top = sprite.height; int right = 0; int bottom = 0; int i = 0; for (int y = 0; y < sprite.height; y++) { bool pixelPresent = false; for (int x = 0; x < sprite.width; x++) { if (pixels[i].a != 0) { if (x < left) { left = x; } if (x > right) { right = x; } pixelPresent = true; } i++; } if (pixelPresent) { if (y < top) { top = y; } if (y > bottom) { bottom = y; } } } // If we didn't find any pixels then flag the sprite as empty if (left == sprite.width) { sprite.isEmpty = true; sprite.size = 0; } else { sprite.pixelRect = new Rect2i(left, top, right - left + 1, bottom - top + 1); sprite.size = sprite.pixelRect.width * sprite.pixelRect.height; } }
private static int CompareSpriteSizeBiggestFirst(UnpackedSprite sprite1, UnpackedSprite sprite2) { int diff = sprite2.size - sprite1.size; // Break ties by sequence number. This helps ensures the same sort order on all platforms if (diff == 0) { return(sprite1.seq - sprite2.seq); } return(diff); }
/// <summary> /// Get pixels from an encoded image buffer. This uses Unity APIs and must be ran on main thread /// </summary> /// <param name="sprite">Sprite to decode</param> /// <param name="workingTexture">Temporary working texture to use</param> /// <returns>True if successful</returns> private static bool GetPixelsFromImageBuffer(UnpackedSprite sprite, Texture2D workingTexture) { #if !RETROBLIT_STANDALONE if (sprite.imageBytes == null) { return(false); } workingTexture.LoadImage(sprite.imageBytes, false); sprite.pixels = workingTexture.GetPixels32(); sprite.width = workingTexture.width; sprite.height = workingTexture.height; sprite.size = sprite.width * sprite.height; sprite.pixelRect = new Rect2i(0, 0, sprite.width, sprite.height); #endif return(true); }