Files
LihonT-Store/src/lihonstore/app/Cut.java
cheb0chik e8277bb7e8 LihonT Store 0.1
A catalog app for the LihonT mod: avatar frames and chat bubbles made by members. Browse, add with the plus button, author profiles,
publishing with a check before anything is shown. Signs in through the TikTok that carries the mod, which hands over only the public
account data to an app signed with the same key.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-05 15:40:01 +03:00

61 lines
2.5 KiB
Java

package lihonstore.app;
/**
* The pixel maths that turns a picture into a frame, on plain ARGB arrays (so it is tested without a phone). The same as the mod's:
* a frame is a square picture drawn OVERLAY times as wide as the avatar, and the avatar fills the middle 1/OVERLAY of it.
*/
final class Cut {
static final int SIDE = 256;
static final float OVERLAY = 1.3f;
private Cut() {}
/** True when most of the middle disc is see-through: the picture is a frame already and is kept as it is. */
static boolean seeThroughMiddle(int[] pixels) {
float middle = SIDE / 2f;
float reach = SIDE * 0.3f;
int total = 0;
int solid = 0;
for (int y = 0; y < SIDE; y++) {
for (int x = 0; x < SIDE; x++) {
float dx = x + 0.5f - middle;
float dy = y + 0.5f - middle;
if (dx * dx + dy * dy > reach * reach) continue;
total++;
if ((pixels[y * SIDE + x] >>> 24) > 128) solid++;
}
}
return total > 0 && solid < total / 2;
}
/** Keeps only the ring between the hole (1/OVERLAY of the width, a little less so the ring overlaps the avatar) and the circle. */
static void ring(int[] pixels) {
float middle = SIDE / 2f;
float inner = SIDE / OVERLAY / 2f * 0.96f;
float outer = SIDE / 2f - 1f;
for (int y = 0; y < SIDE; y++) {
for (int x = 0; x < SIDE; x++) {
float dx = x + 0.5f - middle;
float dy = y + 0.5f - middle;
float distance = (float) Math.sqrt(dx * dx + dy * dy);
float keep = Math.min(clamp(distance - inner + 1f), clamp(outer - distance + 1f));
int color = pixels[y * SIDE + x];
int alpha = Math.round(((color >>> 24) & 0xFF) * keep);
pixels[y * SIDE + x] = (alpha << 24) | (color & 0x00FFFFFF);
}
}
}
/** The size a picture is scaled to so that its longer side is at most `limit` (never enlarged), as {width, height}. */
static int[] fit(int width, int height, int limit) {
int longer = Math.max(width, height);
if (longer <= limit) return new int[] {width, height};
float scale = limit / (float) longer;
return new int[] {Math.max(3, Math.round(width * scale)), Math.max(3, Math.round(height * scale))};
}
private static float clamp(float value) {
return value < 0f ? 0f : (value > 1f ? 1f : value);
}
}