package com.mojang.blaze3d.platform; import com.mojang.blaze3d.DontObfuscate; import com.mojang.blaze3d.buffers.GpuBuffer; import com.mojang.blaze3d.systems.CommandEncoder; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.textures.GpuTexture; import com.mojang.logging.LogUtils; import it.unimi.dsi.fastutil.ints.IntArrayFIFOQueue; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.nio.channels.SeekableByteChannel; import java.nio.file.Path; import java.util.Arrays; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.IntUnaryOperator; import net.minecraft.util.ARGB; import org.lwjgl.system.MemoryUtil; import org.slf4j.Logger; @DontObfuscate public class TextureUtil { private static final Logger LOGGER = LogUtils.getLogger(); public static final int MIN_MIPMAP_LEVEL = 0; private static final int DEFAULT_IMAGE_BUFFER_SIZE = 8192; private static final int[][] DIRECTIONS = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; public static ByteBuffer readResource(InputStream inputStream) throws IOException { ReadableByteChannel readableByteChannel = Channels.newChannel(inputStream); return readableByteChannel instanceof SeekableByteChannel seekableByteChannel ? readResource(readableByteChannel, (int)seekableByteChannel.size() + 1) : readResource(readableByteChannel, 8192); } private static ByteBuffer readResource(ReadableByteChannel readableByteChannel, int i) throws IOException { ByteBuffer byteBuffer = MemoryUtil.memAlloc(i); try { while (readableByteChannel.read(byteBuffer) != -1) { if (!byteBuffer.hasRemaining()) { byteBuffer = MemoryUtil.memRealloc(byteBuffer, byteBuffer.capacity() * 2); } } byteBuffer.flip(); return byteBuffer; } catch (IOException iOException) { MemoryUtil.memFree(byteBuffer); throw iOException; } } public static void writeAsPNG(Path path, String string, GpuTexture gpuTexture, int i, IntUnaryOperator intUnaryOperator) { RenderSystem.assertOnRenderThread(); int j = 0; for (int k = 0; k <= i; k++) { j += gpuTexture.getFormat().pixelSize() * gpuTexture.getWidth(k) * gpuTexture.getHeight(k); } GpuBuffer gpuBuffer = RenderSystem.getDevice().createBuffer(() -> "Texture output buffer", 9, j); CommandEncoder commandEncoder = RenderSystem.getDevice().createCommandEncoder(); Runnable runnable = () -> { try (GpuBuffer.MappedView mappedView = commandEncoder.mapBuffer(gpuBuffer, true, false)) { int o = 0; for (int p = 0; p <= i; p++) { int q = gpuTexture.getWidth(p); int r = gpuTexture.getHeight(p); try (NativeImage nativeImage = new NativeImage(q, r, false)) { for (int s = 0; s < r; s++) { for (int t = 0; t < q; t++) { int u = mappedView.data().getInt(o + (t + s * q) * gpuTexture.getFormat().pixelSize()); nativeImage.setPixelABGR(t, s, intUnaryOperator.applyAsInt(u)); } } Path path2 = path.resolve(string + "_" + p + ".png"); nativeImage.writeToFile(path2); LOGGER.debug("Exported png to: {}", path2.toAbsolutePath()); } catch (IOException iOException) { LOGGER.debug("Unable to write: ", iOException); } o += gpuTexture.getFormat().pixelSize() * q * r; } } gpuBuffer.close(); }; AtomicInteger atomicInteger = new AtomicInteger(); int l = 0; for (int m = 0; m <= i; m++) { commandEncoder.copyTextureToBuffer(gpuTexture, gpuBuffer, l, () -> { if (atomicInteger.getAndIncrement() == i) { runnable.run(); } }, m); l += gpuTexture.getFormat().pixelSize() * gpuTexture.getWidth(m) * gpuTexture.getHeight(m); } } public static Path getDebugTexturePath(Path path) { return path.resolve("screenshots").resolve("debug"); } public static Path getDebugTexturePath() { return getDebugTexturePath(Path.of(".")); } public static void solidify(NativeImage nativeImage) { int i = nativeImage.getWidth(); int j = nativeImage.getHeight(); int[] k = new int[i * j]; int[] l = new int[i * j]; Arrays.fill(l, Integer.MAX_VALUE); IntArrayFIFOQueue intArrayFIFOQueue = new IntArrayFIFOQueue(); for (int m = 0; m < i; m++) { for (int n = 0; n < j; n++) { int o = nativeImage.getPixel(m, n); if (ARGB.alpha(o) != 0) { int p = pack(m, n, i); l[p] = 0; k[p] = o; intArrayFIFOQueue.enqueue(p); } } } while (!intArrayFIFOQueue.isEmpty()) { int u = intArrayFIFOQueue.dequeueInt(); int w = x(u, i); int y = y(u, i); for (int[] q : DIRECTIONS) { int r = w + q[0]; int s = y + q[1]; int t = pack(r, s, i); if (r >= 0 && s >= 0 && r < i && s < j && l[t] > l[u] + 1) { l[t] = l[u] + 1; k[t] = k[u]; intArrayFIFOQueue.enqueue(t); } } } for (int v = 0; v < i; v++) { for (int x = 0; x < j; x++) { int z = nativeImage.getPixel(v, x); if (ARGB.alpha(z) == 0) { nativeImage.setPixel(v, x, ARGB.color(0, k[pack(v, x, i)])); } else { nativeImage.setPixel(v, x, z); } } } } private static int pack(int i, int j, int k) { return i + j * k; } private static int x(int i, int j) { return i % j; } private static int y(int i, int j) { return i / j; } }