NativeImage .java

com.mojang.blaze3d.platform.NativeImage (class): members: getLuminanceOrAlpha, resizeSubRectTo, makePixelArray, WriteCallback, getPixelBytes, OPEN_OPTIONS.

com.mojang.blaze3d.platformclass654 linesclient19.2 KB

Implements

API Surface (51)

String toString()
NativeImage read(final InputStream inputStream)
NativeImage read(final byte[] bytes)
NativeImage read(final ByteBuffer bytes)
void close()
boolean isClosed()
int getWidth()
int getHeight()
NativeImage.Format format()
int getPixel(final int x, final int y)
void setPixelABGR(final int x, final int y, final int pixel)
void setPixel(final int x, final int y, final int pixel)
NativeImage mappedCopy(final IntUnaryOperator function)
int[] getPixelsABGR()
int[] getPixels()
byte getLuminanceOrAlpha(final int x, final int y)
int[] makePixelArray()
void writeToFile(final File file)
boolean copyFromFont(final FT_Face face, final int index)
void writeToFile(final Path file)
void copyFrom(final NativeImage from)
void fillRect(final int xs, final int ys, final int width, final int height, final int pixel)
void copyRect(final int startX, final int startY, final int offsetX, final int offsetY, final int sizeX, final int sizeY, final boolean swapX, final boolean swapY)
void copyRect(final NativeImage target, final int sourceX, final int sourceY, final int targetX, final int targetY, final int sizeX, final int sizeY, final boolean swapX, final boolean swapY)
void resizeSubRectTo(final int sourceX, final int sourceY, final int sizeX, final int sizeY, final NativeImage to)
void untrack()
long getPointer()
ByteBuffer getPixelBytes()
Transparency computeTransparency(final int x0, final int y0, final int x1, final int y1)
Transparency computeTransparency()
int components()
boolean hasRed()
boolean hasGreen()
boolean hasBlue()
boolean hasLuminance()
boolean hasAlpha()
int redOffset()
int greenOffset()
int blueOffset()
int luminanceOffset()
int alphaOffset()
boolean hasLuminanceOrRed()
boolean hasLuminanceOrGreen()
boolean hasLuminanceOrBlue()
boolean hasLuminanceOrAlpha()
int luminanceOrRedOffset()
int luminanceOrGreenOffset()
int luminanceOrBlueOffset()
int luminanceOrAlphaOffset()
void invoke(final long context, final long data, final int size)
void throwIfException()

Fields & Constants (17)

LOGGER
MEMORY_POOL
OPEN_OPTIONS
format
width
height
useStbFree
pixels
size
swapX
target
sourceX
sourceY
targetX
targetY
sizeX
sizeY

String literals (19)

"Unable to allocate texture of size "
" channels)"
"Invalid texture size: "
"NativeImage["
"Invalid buffer"
"Could not load image: "
"Image is not allocated."
"can only call makePixelArray for RGBA images."
"Can only write fonts into 1-component images."
"Loading glyph"
"Glyph not initialized"
"Rendered glyph was not 8-bit grayscale"
"Glyph has no bitmap"
"Could not write image to the PNG file \""
"Image formats don't match."
"Image of size "
" is too large to compute translucency"
"Cannot compute translucency out of bounds: ["
" image"

Code Graph: Dependencies (7)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

NativeImage.java654 lines · Minecraft 26.3 · verbatim
1
package com.mojang.blaze3d.platform;
2
3
import com.mojang.jtracy.MemoryPool;
4
import com.mojang.jtracy.TracyClient;
5
import com.mojang.logging.LogUtils;
6
import java.io.File;
7
import java.io.IOException;
8
import java.io.InputStream;
9
import java.nio.ByteBuffer;
10
import java.nio.IntBuffer;
11
import java.nio.channels.WritableByteChannel;
12
import java.nio.file.Files;
13
import java.nio.file.Path;
14
import java.nio.file.StandardOpenOption;
15
import java.util.EnumSet;
16
import java.util.Locale;
17
import java.util.Objects;
18
import java.util.Set;
19
import java.util.function.IntUnaryOperator;
20
import net.fabricmc.api.EnvType;
21
import net.fabricmc.api.Environment;
22
import net.minecraft.client.gui.font.providers.FreeTypeUtil;
23
import net.minecraft.util.ARGB;
24
import net.minecraft.util.PngInfo;
25
import org.apache.commons.io.IOUtils;
26
import org.jspecify.annotations.Nullable;
27
import org.lwjgl.stb.STBIWriteCallback;
28
import org.lwjgl.stb.STBImage;
29
import org.lwjgl.stb.STBImageResize;
30
import org.lwjgl.stb.STBImageWrite;
31
import org.lwjgl.system.MemoryStack;
32
import org.lwjgl.system.MemoryUtil;
33
import org.lwjgl.util.freetype.FT_Bitmap;
34
import org.lwjgl.util.freetype.FT_Face;
35
import org.lwjgl.util.freetype.FT_GlyphSlot;
36
import org.lwjgl.util.freetype.FreeType;
37
import org.slf4j.Logger;
38
39
@Environment(EnvType.CLIENT)
40
public final class NativeImage implements AutoCloseable {
41
	private static final Logger LOGGER = LogUtils.getLogger();
42
	private static final MemoryPool MEMORY_POOL = TracyClient.createMemoryPool("NativeImage");
43
	private static final Set<StandardOpenOption> OPEN_OPTIONS = EnumSet.of(
44
		StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING
45
	);
46
	private final NativeImage.Format format;
47
	private final int width;
48
	private final int height;
49
	private final boolean useStbFree;
50
	private long pixels;
51
	private final long size;
52
53
	public NativeImage(final int width, final int height, final boolean zero) {
54
		this(NativeImage.Format.RGBA, width, height, zero);
55
	}
56
57
	public NativeImage(final NativeImage.Format format, final int width, final int height, final boolean zero) {
58
		if (width > 0 && height > 0) {
59
			this.format = format;
60
			this.width = width;
61
			this.height = height;
62
			this.size = (long)width * height * format.components();
63
			this.useStbFree = false;
64
			if (zero) {
65
				this.pixels = MemoryUtil.nmemCalloc(1L, this.size);
66
			} else {
67
				this.pixels = MemoryUtil.nmemAlloc(this.size);
68
			}
69
70
			MEMORY_POOL.malloc(this.pixels, (int)this.size);
71
			if (this.pixels == 0L) {
72
				throw new IllegalStateException("Unable to allocate texture of size " + width + "x" + height + " (" + format.components() + " channels)");
73
			}
74
		} else {
75
			throw new IllegalArgumentException("Invalid texture size: " + width + "x" + height);
76
		}
77
	}
78
79
	public NativeImage(final NativeImage.Format format, final int width, final int height, final boolean useStbFree, final long pixels) {
80
		if (width > 0 && height > 0) {
81
			this.format = format;
82
			this.width = width;
83
			this.height = height;
84
			this.useStbFree = useStbFree;
85
			this.pixels = pixels;
86
			this.size = (long)width * height * format.components();
87
		} else {
88
			throw new IllegalArgumentException("Invalid texture size: " + width + "x" + height);
89
		}
90
	}
91
92
	@Override
93
	public String toString() {
94
		return "NativeImage[" + this.format + " " + this.width + "x" + this.height + "@" + this.pixels + (this.useStbFree ? "S" : "N") + "]";
95
	}
96
97
	private boolean isOutsideBounds(final int x, final int y) {
98
		return x < 0 || x >= this.width || y < 0 || y >= this.height;
99
	}
100
101
	public static NativeImage read(final InputStream inputStream) throws IOException {
102
		ByteBuffer file = null;
103
104
		try {
105
			file = TextureUtil.readResource(inputStream);
106
			return read(file);
107
		} finally {
108
			MemoryUtil.memFree(file);
109
			IOUtils.closeQuietly(inputStream);
110
		}
111
	}
112
113
	public static NativeImage read(final byte[] bytes) throws IOException {
114
		MemoryStack memoryStack = MemoryStack.stackGet();
115
		int bytesAvailable = memoryStack.getPointer();
116
		if (bytesAvailable < bytes.length) {
117
			ByteBuffer buffer = MemoryUtil.memAlloc(bytes.length);
118
119
			try {
120
				return putAndRead(buffer, bytes);
121
			} finally {
122
				MemoryUtil.memFree(buffer);
123
			}
124
		} else {
125
			try (MemoryStack stack = MemoryStack.stackPush()) {
126
				ByteBuffer buffer = stack.malloc(bytes.length);
127
				return putAndRead(buffer, bytes);
128
			}
129
		}
130
	}
131
132
	private static NativeImage putAndRead(final ByteBuffer nativeBuffer, final byte[] bytes) throws IOException {
133
		nativeBuffer.put(bytes);
134
		nativeBuffer.rewind();
135
		return read(nativeBuffer);
136
	}
137
138
	public static NativeImage read(final ByteBuffer bytes) throws IOException {
139
		if (MemoryUtil.memAddress(bytes) == 0L) {
140
			throw new IllegalArgumentException("Invalid buffer");
141
		}
142
143
		PngInfo.validateHeader(bytes);
144
145
		try (MemoryStack stack = MemoryStack.stackPush()) {
146
			IntBuffer w = stack.mallocInt(1);
147
			IntBuffer h = stack.mallocInt(1);
148
			IntBuffer comp = stack.mallocInt(1);
149
			ByteBuffer pixels = STBImage.stbi_load_from_memory(bytes, w, h, comp, NativeImage.Format.RGBA.components);
150
			if (pixels == null) {
151
				throw new IOException("Could not load image: " + STBImage.stbi_failure_reason());
152
			}
153
154
			long address = MemoryUtil.memAddress(pixels);
155
			MEMORY_POOL.malloc(address, pixels.limit());
156
			return new NativeImage(NativeImage.Format.RGBA, w.get(0), h.get(0), true, address);
157
		}
158
	}
159
160
	private void checkAllocated() {
161
		if (this.pixels == 0L) {
162
			throw new IllegalStateException("Image is not allocated.");
163
		}
164
	}
165
166
	@Override
167
	public void close() {
168
		if (this.pixels != 0L) {
169
			if (this.useStbFree) {
170
				STBImage.nstbi_image_free(this.pixels);
171
			} else {
172
				MemoryUtil.nmemFree(this.pixels);
173
			}
174
175
			MEMORY_POOL.free(this.pixels);
176
		}
177
178
		this.pixels = 0L;
179
	}
180
181
	public boolean isClosed() {
182
		return this.pixels == 0L;
183
	}
184
185
	public int getWidth() {
186
		return this.width;
187
	}
188
189
	public int getHeight() {
190
		return this.height;
191
	}
192
193
	public NativeImage.Format format() {
194
		return this.format;
195
	}
196
197
	private int getPixelABGR(final int x, final int y) {
198
		if (this.format != NativeImage.Format.RGBA) {
199
			throw new IllegalArgumentException(String.format(Locale.ROOT, "getPixelRGBA only works on RGBA images; have %s", this.format));
200
		}
201
202
		if (this.isOutsideBounds(x, y)) {
203
			throw new IllegalArgumentException(String.format(Locale.ROOT, "(%s, %s) outside of image bounds (%s, %s)", x, y, this.width, this.height));
204
		}
205
206
		this.checkAllocated();
207
		long offset = (x + (long)y * this.width) * 4L;
208
		return MemoryUtil.memGetInt(this.pixels + offset);
209
	}
210
211
	public int getPixel(final int x, final int y) {
212
		return ARGB.fromABGR(this.getPixelABGR(x, y));
213
	}
214
215
	public void setPixelABGR(final int x, final int y, final int pixel) {
216
		if (this.format != NativeImage.Format.RGBA) {
217
			throw new IllegalArgumentException(String.format(Locale.ROOT, "setPixelRGBA only works on RGBA images; have %s", this.format));
218
		}
219
220
		if (this.isOutsideBounds(x, y)) {
221
			throw new IllegalArgumentException(String.format(Locale.ROOT, "(%s, %s) outside of image bounds (%s, %s)", x, y, this.width, this.height));
222
		}
223
224
		this.checkAllocated();
225
		long offset = (x + (long)y * this.width) * 4L;
226
		MemoryUtil.memPutInt(this.pixels + offset, pixel);
227
	}
228
229
	public void setPixel(final int x, final int y, final int pixel) {
230
		this.setPixelABGR(x, y, ARGB.toABGR(pixel));
231
	}
232
233
	public NativeImage mappedCopy(final IntUnaryOperator function) {
234
		if (this.format != NativeImage.Format.RGBA) {
235
			throw new IllegalArgumentException(String.format(Locale.ROOT, "function application only works on RGBA images; have %s", this.format));
236
		}
237
238
		this.checkAllocated();
239
		NativeImage result = new NativeImage(this.width, this.height, false);
240
		int pixelCount = this.width * this.height;
241
		IntBuffer sourceBuffer = MemoryUtil.memIntBuffer(this.pixels, pixelCount);
242
		IntBuffer targetBuffer = MemoryUtil.memIntBuffer(result.pixels, pixelCount);
243
244
		for (int i = 0; i < pixelCount; i++) {
245
			int pixel = ARGB.fromABGR(sourceBuffer.get(i));
246
			int modified = function.applyAsInt(pixel);
247
			targetBuffer.put(i, ARGB.toABGR(modified));
248
		}
249
250
		return result;
251
	}
252
253
	public int[] getPixelsABGR() {
254
		if (this.format != NativeImage.Format.RGBA) {
255
			throw new IllegalArgumentException(String.format(Locale.ROOT, "getPixels only works on RGBA images; have %s", this.format));
256
		}
257
258
		this.checkAllocated();
259
		int[] result = new int[this.width * this.height];
260
		MemoryUtil.memIntBuffer(this.pixels, this.width * this.height).get(result);
261
		return result;
262
	}
263
264
	public int[] getPixels() {
265
		int[] result = this.getPixelsABGR();
266
267
		for (int i = 0; i < result.length; i++) {
268
			result[i] = ARGB.fromABGR(result[i]);
269
		}
270
271
		return result;
272
	}
273
274
	public byte getLuminanceOrAlpha(final int x, final int y) {
275
		if (!this.format.hasLuminanceOrAlpha()) {
276
			throw new IllegalArgumentException(String.format(Locale.ROOT, "no luminance or alpha in %s", this.format));
277
		}
278
279
		if (this.isOutsideBounds(x, y)) {
280
			throw new IllegalArgumentException(String.format(Locale.ROOT, "(%s, %s) outside of image bounds (%s, %s)", x, y, this.width, this.height));
281
		}
282
283
		int offset = (x + y * this.width) * this.format.components() + this.format.luminanceOrAlphaOffset() / 8;
284
		return MemoryUtil.memGetByte(this.pixels + offset);
285
	}
286
287
	@Deprecated
288
	public int[] makePixelArray() {
289
		if (this.format != NativeImage.Format.RGBA) {
290
			throw new UnsupportedOperationException("can only call makePixelArray for RGBA images.");
291
		}
292
293
		this.checkAllocated();
294
		int[] pixels = new int[this.getWidth() * this.getHeight()];
295
296
		for (int y = 0; y < this.getHeight(); y++) {
297
			for (int x = 0; x < this.getWidth(); x++) {
298
				pixels[x + y * this.getWidth()] = this.getPixel(x, y);
299
			}
300
		}
301
302
		return pixels;
303
	}
304
305
	public void writeToFile(final File file) throws IOException {
306
		this.writeToFile(file.toPath());
307
	}
308
309
	public boolean copyFromFont(final FT_Face face, final int index) {
310
		if (this.format.components() != 1) {
311
			throw new IllegalArgumentException("Can only write fonts into 1-component images.");
312
		} else if (FreeTypeUtil.checkError(FreeType.FT_Load_Glyph(face, index, 4), "Loading glyph")) {
313
			return false;
314
		} else {
315
			FT_GlyphSlot glyph = Objects.requireNonNull(face.glyph(), "Glyph not initialized");
316
			FT_Bitmap bitmap = glyph.bitmap();
317
			if (bitmap.pixel_mode() != 2) {
318
				throw new IllegalStateException("Rendered glyph was not 8-bit grayscale");
319
			} else if (bitmap.width() == this.getWidth() && bitmap.rows() == this.getHeight()) {
320
				int bufferSize = bitmap.width() * bitmap.rows();
321
				ByteBuffer buffer = Objects.requireNonNull(bitmap.buffer(bufferSize), "Glyph has no bitmap");
322
				MemoryUtil.memCopy(MemoryUtil.memAddress(buffer), this.pixels, bufferSize);
323
				return true;
324
			} else {
325
				throw new IllegalArgumentException(
326
					String.format(
327
						Locale.ROOT, "Glyph bitmap of size %sx%s does not match image of size: %sx%s", bitmap.width(), bitmap.rows(), this.getWidth(), this.getHeight()
328
					)
329
				);
330
			}
331
		}
332
	}
333
334
	public void writeToFile(final Path file) throws IOException {
335
		this.checkAllocated();
336
337
		try (WritableByteChannel out = Files.newByteChannel(file, OPEN_OPTIONS)) {
338
			if (!this.writeToChannel(out)) {
339
				throw new IOException("Could not write image to the PNG file \"" + file.toAbsolutePath() + "\": " + STBImage.stbi_failure_reason());
340
			}
341
		}
342
	}
343
344
	private boolean writeToChannel(final WritableByteChannel output) throws IOException {
345
		NativeImage.WriteCallback writer = new NativeImage.WriteCallback(output);
346
347
		try {
348
			int height = Math.min(this.getHeight(), Integer.MAX_VALUE / this.getWidth() / this.format.components());
349
			if (height < this.getHeight()) {
350
				LOGGER.warn("Dropping image height from {} to {} to fit the size into 32-bit signed int", this.getHeight(), height);
351
			}
352
353
			if (STBImageWrite.nstbi_write_png_to_func(writer.address(), 0L, this.getWidth(), height, this.format.components(), this.pixels, 0) == 0) {
354
				return false;
355
			}
356
357
			writer.throwIfException();
358
			return true;
359
		} finally {
360
			writer.free();
361
		}
362
	}
363
364
	public void copyFrom(final NativeImage from) {
365
		if (from.format() != this.format) {
366
			throw new UnsupportedOperationException("Image formats don't match.");
367
		}
368
369
		int components = this.format.components();
370
		this.checkAllocated();
371
		from.checkAllocated();
372
		if (this.width == from.width) {
373
			MemoryUtil.memCopy(from.pixels, this.pixels, Math.min(this.size, from.size));
374
		} else {
375
			int minWidth = Math.min(this.getWidth(), from.getWidth());
376
			int minHeight = Math.min(this.getHeight(), from.getHeight());
377
378
			for (int y = 0; y < minHeight; y++) {
379
				int fromOffset = y * from.getWidth() * components;
380
				int toOffset = y * this.getWidth() * components;
381
				MemoryUtil.memCopy(from.pixels + fromOffset, this.pixels + toOffset, minWidth);
382
			}
383
		}
384
	}
385
386
	public void fillRect(final int xs, final int ys, final int width, final int height, final int pixel) {
387
		for (int y = ys; y < ys + height; y++) {
388
			for (int x = xs; x < xs + width; x++) {
389
				this.setPixel(x, y, pixel);
390
			}
391
		}
392
	}
393
394
	public void copyRect(
395
		final int startX, final int startY, final int offsetX, final int offsetY, final int sizeX, final int sizeY, final boolean swapX, final boolean swapY
396
	) {
397
		this.copyRect(this, startX, startY, startX + offsetX, startY + offsetY, sizeX, sizeY, swapX, swapY);
398
	}
399
400
	public void copyRect(
401
		final NativeImage target,
402
		final int sourceX,
403
		final int sourceY,
404
		final int targetX,
405
		final int targetY,
406
		final int sizeX,
407
		final int sizeY,
408
		final boolean swapX,
409
		final boolean swapY
410
	) {
411
		for (int y = 0; y < sizeY; y++) {
412
			for (int x = 0; x < sizeX; x++) {
413
				int dx = swapX ? sizeX - 1 - x : x;
414
				int dy = swapY ? sizeY - 1 - y : y;
415
				int source = this.getPixelABGR(sourceX + x, sourceY + y);
416
				target.setPixelABGR(targetX + dx, targetY + dy, source);
417
			}
418
		}
419
	}
420
421
	public void resizeSubRectTo(final int sourceX, final int sourceY, final int sizeX, final int sizeY, final NativeImage to) {
422
		this.checkAllocated();
423
		if (to.format() != this.format) {
424
			throw new UnsupportedOperationException("resizeSubRectTo only works for images of the same format.");
425
		}
426
427
		int components = this.format.components();
428
		STBImageResize.nstbir_resize_uint8_linear(
429
			this.pixels + (sourceX + sourceY * this.getWidth()) * components,
430
			sizeX,
431
			sizeY,
432
			this.getWidth() * components,
433
			to.pixels,
434
			to.getWidth(),
435
			to.getHeight(),
436
			0,
437
			components
438
		);
439
	}
440
441
	public void untrack() {
442
		DebugMemoryUntracker.untrack(this.pixels);
443
	}
444
445
	public long getPointer() {
446
		return this.pixels;
447
	}
448
449
	public ByteBuffer getPixelBytes() {
450
		return MemoryUtil.memByteBuffer(this.pixels, Math.toIntExact(this.size));
451
	}
452
453
	public Transparency computeTransparency(final int x0, final int y0, final int x1, final int y1) {
454
		this.checkAllocated();
455
		if (this.format != NativeImage.Format.RGBA) {
456
			return Transparency.NONE;
457
		}
458
459
		if (x0 >= 0 && y0 >= 0 && x1 <= this.width && y1 <= this.height) {
460
			if ((long)this.width * this.height * 4L > 2147483647L) {
461
				throw new IllegalArgumentException("Image of size " + this.width + "x" + this.height + " is too large to compute translucency");
462
			}
463
464
			boolean hasTransparentPixel = false;
465
			boolean hasTranslucentPixel = false;
466
			IntBuffer buffer = MemoryUtil.memIntBuffer(this.pixels, this.width * this.height);
467
468
			for (int y = y0; y < y1; y++) {
469
				for (int x = x0; x < x1; x++) {
470
					int alpha = ARGB.alpha(buffer.get(x + y * this.width));
471
					if (alpha == 0) {
472
						hasTransparentPixel = true;
473
					} else if (alpha != 255) {
474
						hasTranslucentPixel = true;
475
					}
476
				}
477
			}
478
479
			return Transparency.of(hasTransparentPixel, hasTranslucentPixel);
480
		} else {
481
			throw new IllegalArgumentException(
482
				"Cannot compute translucency out of bounds: [" + x0 + ", " + y0 + ", " + x1 + ", " + y1 + "] in " + this.width + "x" + this.height + " image"
483
			);
484
		}
485
	}
486
487
	public Transparency computeTransparency() {
488
		return this.computeTransparency(0, 0, this.width, this.height);
489
	}
490
491
	@Environment(EnvType.CLIENT)
492
	public enum Format {
493
		RGBA(4, true, true, true, false, true, 0, 8, 16, 255, 24),
494
		RGB(3, true, true, true, false, false, 0, 8, 16, 255, 255),
495
		LUMINANCE_ALPHA(2, false, false, false, true, true, 255, 255, 255, 0, 8),
496
		LUMINANCE(1, false, false, false, true, false, 0, 0, 0, 0, 255);
497
498
		private final int components;
499
		private final boolean hasRed;
500
		private final boolean hasGreen;
501
		private final boolean hasBlue;
502
		private final boolean hasLuminance;
503
		private final boolean hasAlpha;
504
		private final int redOffset;
505
		private final int greenOffset;
506
		private final int blueOffset;
507
		private final int luminanceOffset;
508
		private final int alphaOffset;
509
510
		Format(
511
			final int components,
512
			final boolean hasRed,
513
			final boolean hasGreen,
514
			final boolean hasBlue,
515
			final boolean hasLuminance,
516
			final boolean hasAlpha,
517
			final int redOffset,
518
			final int greenOffset,
519
			final int blueOffset,
520
			final int luminanceOffset,
521
			final int alphaOffset
522
		) {
523
			this.components = components;
524
			this.hasRed = hasRed;
525
			this.hasGreen = hasGreen;
526
			this.hasBlue = hasBlue;
527
			this.hasLuminance = hasLuminance;
528
			this.hasAlpha = hasAlpha;
529
			this.redOffset = redOffset;
530
			this.greenOffset = greenOffset;
531
			this.blueOffset = blueOffset;
532
			this.luminanceOffset = luminanceOffset;
533
			this.alphaOffset = alphaOffset;
534
		}
535
536
		public int components() {
537
			return this.components;
538
		}
539
540
		public boolean hasRed() {
541
			return this.hasRed;
542
		}
543
544
		public boolean hasGreen() {
545
			return this.hasGreen;
546
		}
547
548
		public boolean hasBlue() {
549
			return this.hasBlue;
550
		}
551
552
		public boolean hasLuminance() {
553
			return this.hasLuminance;
554
		}
555
556
		public boolean hasAlpha() {
557
			return this.hasAlpha;
558
		}
559
560
		public int redOffset() {
561
			return this.redOffset;
562
		}
563
564
		public int greenOffset() {
565
			return this.greenOffset;
566
		}
567
568
		public int blueOffset() {
569
			return this.blueOffset;
570
		}
571
572
		public int luminanceOffset() {
573
			return this.luminanceOffset;
574
		}
575
576
		public int alphaOffset() {
577
			return this.alphaOffset;
578
		}
579
580
		public boolean hasLuminanceOrRed() {
581
			return this.hasLuminance || this.hasRed;
582
		}
583
584
		public boolean hasLuminanceOrGreen() {
585
			return this.hasLuminance || this.hasGreen;
586
		}
587
588
		public boolean hasLuminanceOrBlue() {
589
			return this.hasLuminance || this.hasBlue;
590
		}
591
592
		public boolean hasLuminanceOrAlpha() {
593
			return this.hasLuminance || this.hasAlpha;
594
		}
595
596
		public int luminanceOrRedOffset() {
597
			return this.hasLuminance ? this.luminanceOffset : this.redOffset;
598
		}
599
600
		public int luminanceOrGreenOffset() {
601
			return this.hasLuminance ? this.luminanceOffset : this.greenOffset;
602
		}
603
604
		public int luminanceOrBlueOffset() {
605
			return this.hasLuminance ? this.luminanceOffset : this.blueOffset;
606
		}
607
608
		public int luminanceOrAlphaOffset() {
609
			return this.hasLuminance ? this.luminanceOffset : this.alphaOffset;
610
		}
611
612
		private static NativeImage.Format getStbFormat(final int i) {
613
			switch (i) {
614
				case 1:
615
					return LUMINANCE;
616
				case 2:
617
					return LUMINANCE_ALPHA;
618
				case 3:
619
					return RGB;
620
				case 4:
621
				default:
622
					return RGBA;
623
			}
624
		}
625
	}
626
627
	@Environment(EnvType.CLIENT)
628
	private static class WriteCallback extends STBIWriteCallback {
629
		private final WritableByteChannel output;
630
		private @Nullable IOException exception;
631
632
		private WriteCallback(final WritableByteChannel output) {
633
			this.output = output;
634
		}
635
636
		@Override
637
		public void invoke(final long context, final long data, final int size) {
638
			ByteBuffer dataBuf = getData(data, size);
639
640
			try {
641
				this.output.write(dataBuf);
642
			} catch (IOException e) {
643
				this.exception = e;
644
			}
645
		}
646
647
		public void throwIfException() throws IOException {
648
			if (this.exception != null) {
649
				throw this.exception;
650
			}
651
		}
652
	}
653
}
654

Decompiled Minecraft 26.3 source of NativeImage (com/mojang/blaze3d/platform/NativeImage.java) with Mojang official mappings, stored verbatim on the site.