package com.dairymoose.modernlife.util; import com.dairymoose.modernlife.core.ModernLifeCommon; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; /* loaded from: outputsrg.jar:com/dairymoose/modernlife/util/CanvasData.class */ public class CanvasData { private static final Logger LOGGER = LogManager.getLogger(); public static final int DEFAULT_TEXTURE_SIZE = 64; private ByteBuffer RGBs = null; private int hashCode = 0; private int TEXTURE_WIDTH = 64; private int TEXTURE_HEIGHT = 64; private int MAX_SIZE_BYTES = (this.TEXTURE_WIDTH * this.TEXTURE_HEIGHT) * 3; private boolean disabled = false; public void disable() { this.disabled = true; } public void reenable() { this.disabled = false; } public void setTextureSize(int w, int h) { this.TEXTURE_WIDTH = w; this.TEXTURE_HEIGHT = h; this.MAX_SIZE_BYTES = this.TEXTURE_WIDTH * this.TEXTURE_HEIGHT * 3; } public int textureWidth() { return this.TEXTURE_WIDTH; } public int textureHeight() { return this.TEXTURE_HEIGHT; } public boolean copyDataFrom(CanvasData other) { if (other.RGBs != null && other.RGBs.limit() <= this.MAX_SIZE_BYTES) { this.RGBs = ByteBuffer.allocate(this.MAX_SIZE_BYTES); other.RGBs.position(0); this.RGBs.put(other.RGBs); recomputeHashCode(); return true; } return false; } public static int calculateHashCode(byte[] data) { int h = 1; for (byte b : data) { h = (31 * h) + b; } return h; } public void recomputeHashCode() { if (this.RGBs == null) { this.hashCode = -1; } else { this.hashCode = calculateHashCode(this.RGBs.array()); } } public int hashCode() { return this.hashCode; } public void initImage() { if (this.RGBs == null && !this.disabled) { ModernLifeCommon.LOGGER.debug("initImage'd RGB WAS NULL"); this.RGBs = ByteBuffer.allocate(this.MAX_SIZE_BYTES); for (int x = 0; x < this.TEXTURE_WIDTH; x++) { for (int y = 0; y < this.TEXTURE_HEIGHT; y++) { setRgbPixel(x, y, 16777215); } } recomputeHashCode(); } } public boolean isValid() { if (this.RGBs == null || this.RGBs.limit() == 0 || textureWidth() <= 0 || textureHeight() <= 0 || this.disabled) { return false; } return true; } public byte[] toNbt() { return this.RGBs.array(); } public byte[] toCompressedNbt() { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ZipOutputStream zs = new ZipOutputStream(bos); byte[] nbt = toNbt(); try { ZipEntry ze = new ZipEntry("z"); ze.setTime(0L); zs.putNextEntry(ze); } catch (IOException e1) { e1.printStackTrace(); } try { zs.write(nbt); zs.closeEntry(); zs.finish(); } catch (IOException e) { e.printStackTrace(); } return bos.toByteArray(); } public void fromNbt(byte[] nbt) { if (nbt == null || nbt.length <= 0 || nbt.length > this.MAX_SIZE_BYTES) { return; } this.RGBs = ByteBuffer.allocate(this.MAX_SIZE_BYTES); this.RGBs.put(nbt); recomputeHashCode(); } public void fromCompressedNbt(byte[] nbt) { if (nbt == null || nbt.length == 0) { return; } ByteArrayInputStream bis = new ByteArrayInputStream(nbt); ZipInputStream zs = new ZipInputStream(bis); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { zs.getNextEntry(); while (true) { try { byte[] buffer = new byte[1024]; int readResult = zs.read(buffer); if (readResult == -1) { break; } else { baos.write(buffer, 0, readResult); } } catch (IOException e) { ModernLifeCommon.LOGGER.error("error opening compressed nbt"); this.RGBs = null; recomputeHashCode(); return; } } zs.closeEntry(); zs.close(); if (baos.size() <= 0) { this.RGBs = null; recomputeHashCode(); } else { byte[] output = baos.toByteArray(); fromNbt(output); } } catch (IOException e2) { this.RGBs = null; recomputeHashCode(); } } public int getAbgrOutputPixel(int x, int y) { if (this.RGBs == null || this.disabled) { return 0; } int newX = (this.TEXTURE_WIDTH - x) - 1; int newY = (this.TEXTURE_HEIGHT - y) - 1; int logicalIndex = (newY * this.TEXTURE_WIDTH) + newX; int byteIndex = logicalIndex * 3; this.RGBs.position(byteIndex); int r = this.RGBs.get() + 128; int g = this.RGBs.get() + 128; int b = this.RGBs.get() + 128; return r | (g << 8) | (b << 16) | (-16777216); } public void setRgbPixel(int x, int y, int rgb) { if (this.RGBs == null || this.disabled) { return; } int logicalIndex = (y * this.TEXTURE_WIDTH) + x; int byteIndex = logicalIndex * 3; this.RGBs.position(byteIndex); int r = (rgb & 16711680) >> 16; int g = (rgb & 65280) >> 8; int b = rgb & 255; this.RGBs.put((byte) (r - 128)); this.RGBs.put((byte) (g - 128)); this.RGBs.put((byte) (b - 128)); } public int getRgbPixel(int x, int y) { if (this.RGBs == null || this.disabled) { return 0; } int logicalIndex = (y * this.TEXTURE_WIDTH) + x; int byteIndex = logicalIndex * 3; this.RGBs.position(byteIndex); int r = this.RGBs.get() + 128; int g = this.RGBs.get() + 128; int b = this.RGBs.get() + 128; return (r << 16) | (g << 8) | b; } public int convertToRgbOutput(int rgb) { int r = (rgb & 16711680) >> 16; int g = (rgb & 65280) >> 8; int b = rgb & 255; return r | (g << 8) | (b << 16) | (-16777216); } public static int getRValue(int rgb) { int r = (rgb & 16711680) >> 16; return r; } public static int getGValue(int rgb) { int g = (rgb & 65280) >> 8; return g; } public static int getBValue(int rgb) { int b = rgb & 255; return b; } public static int toRgb(int r, int g, int b) { return (r << 16) | (g << 8) | b; } public int applyBlend(int newColor, int backgroundColor, double blendFactor) { int r = getRValue(newColor); int g = getGValue(newColor); int b = getBValue(newColor); int bgR = getRValue(backgroundColor); int bgG = getGValue(backgroundColor); int bgB = getBValue(backgroundColor); int finalColor = ((((int) (r * blendFactor)) + ((int) (bgR * (1.0d - blendFactor)))) << 16) | ((((int) (g * blendFactor)) + ((int) (bgG * (1.0d - blendFactor)))) << 8) | (((int) (b * blendFactor)) + ((int) (bgB * (1.0d - blendFactor)))); return finalColor; } public int getRgbOutputPixel(int x, int y) { if (this.RGBs == null || this.disabled) { return 0; } int logicalIndex = (y * this.TEXTURE_WIDTH) + x; int byteIndex = logicalIndex * 3; this.RGBs.position(byteIndex); int r = this.RGBs.get() + 128; int g = this.RGBs.get() + 128; int b = this.RGBs.get() + 128; return r | (g << 8) | (b << 16) | (-16777216); } }