0

refac: TgaFile

This commit is contained in:
2026-02-12 01:35:41 +03:00
parent 9cad290a68
commit bf73b91b60
3 changed files with 88 additions and 60 deletions

View File

@@ -47,52 +47,6 @@ public class qfiles {
/*
========================================================================
TGA files are used for sky planes
========================================================================
*/
public static class tga_t {
// targa header
public int id_length, colormap_type, image_type; // unsigned char
public int colormap_index, colormap_length; // unsigned short
public int colormap_size; // unsigned char
public int x_origin, y_origin, width, height; // unsigned short
public int pixel_size, attributes; // unsigned char
public ByteBuffer data; // (un)compressed data
public tga_t(byte[] dataBytes) {
this(ByteBuffer.wrap(dataBytes));
}
public tga_t(ByteBuffer b) {
// is stored as little endian
b.order(ByteOrder.LITTLE_ENDIAN);
// fill header
id_length = b.get() & 0xFF;
colormap_type = b.get() & 0xFF;
image_type = b.get() & 0xFF;
colormap_index = b.getShort() & 0xFFFF;
colormap_length = b.getShort() & 0xFFFF;
colormap_size = b.get() & 0xFF;
x_origin = b.getShort() & 0xFFFF;
y_origin = b.getShort() & 0xFFFF;
width = b.getShort() & 0xFFFF;
height = b.getShort() & 0xFFFF;
pixel_size = b.get() & 0xFF;
attributes = b.get() & 0xFF;
// fill data
data = b.slice();
}
}
/*
========================================================================
.MD2 triangle model file format
========================================================================

View File

@@ -45,6 +45,7 @@ import org.lwjgl.opengl.ARBMultitexture;
import org.lwjgl.opengl.EXTSharedTexturePalette;
import org.lwjgl.opengl.GL11;
import ru.di9.lwjake2.PcxFile;
import ru.di9.lwjake2.TgaFile;
/**
* Image
@@ -542,7 +543,7 @@ public abstract class Image extends Main {
int row, column;
byte[] raw;
ByteBuffer buf_p;
qfiles.tga_t targa_header;
TgaFile targa_header;
byte[] pic = null;
//
@@ -556,16 +557,16 @@ public abstract class Image extends Main {
return null;
}
targa_header = new qfiles.tga_t(raw);
targa_header = new TgaFile(ByteBuffer.wrap(raw));
if (targa_header.image_type != 2 && targa_header.image_type != 10)
if (targa_header.getImageType() != 2 && targa_header.getImageType() != 10)
Com.Error(Defines.ERR_DROP, "LoadTGA: Only type 2 and 10 targa RGB images supported\n");
if (targa_header.colormap_type != 0 || (targa_header.pixel_size != 32 && targa_header.pixel_size != 24))
if (targa_header.getColormapType() != 0 || (targa_header.getPixelSize() != 32 && targa_header.getPixelSize() != 24))
Com.Error (Defines.ERR_DROP, "LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n");
columns = targa_header.width;
rows = targa_header.height;
columns = targa_header.getWidth();
rows = targa_header.getHeight();
numPixels = columns * rows;
if (dim != null) {
@@ -575,22 +576,22 @@ public abstract class Image extends Main {
pic = new byte[numPixels * 4]; // targa_rgba;
if (targa_header.id_length != 0)
targa_header.data.position(targa_header.id_length); // skip TARGA image comment
if (targa_header.getIdLength() != 0)
targa_header.getData().position(targa_header.getIdLength()); // skip TARGA image comment
buf_p = targa_header.data;
buf_p = targa_header.getData();
byte red,green,blue,alphabyte;
red = green = blue = alphabyte = 0;
int packetHeader, packetSize, j;
if (targa_header.image_type==2) { // Uncompressed, RGB images
if (targa_header.getImageType()==2) { // Uncompressed, RGB images
for(row=rows-1; row>=0; row--) {
pixbuf = row * columns * 4;
for(column=0; column<columns; column++) {
switch (targa_header.pixel_size) {
switch (targa_header.getPixelSize()) {
case 24:
blue = buf_p.get();
@@ -615,7 +616,7 @@ public abstract class Image extends Main {
}
}
}
else if (targa_header.image_type==10) { // Runlength encoded RGB images
else if (targa_header.getImageType()==10) { // Runlength encoded RGB images
for(row=rows-1; row>=0; row--) {
pixbuf = row * columns * 4;
@@ -627,7 +628,7 @@ public abstract class Image extends Main {
packetSize = 1 + (packetHeader & 0x7f);
if ((packetHeader & 0x80) != 0) { // run-length packet
switch (targa_header.pixel_size) {
switch (targa_header.getPixelSize()) {
case 24:
blue = buf_p.get();
green = buf_p.get();
@@ -662,7 +663,7 @@ public abstract class Image extends Main {
}
else { // non run-length packet
for(j=0;j<packetSize;j++) {
switch (targa_header.pixel_size) {
switch (targa_header.getPixelSize()) {
case 24:
blue = buf_p.get();
green = buf_p.get();

View File

@@ -0,0 +1,73 @@
package ru.di9.lwjake2;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class TgaFile {
private final int id_length; // unsigned char
private final int colormap_type; // unsigned char
private final int image_type; // unsigned char
private final int width; // unsigned short
private final int height; // unsigned short
private final int pixel_size; // unsigned char
private final ByteBuffer data; // (un)compressed data
public TgaFile(ByteBuffer buffer) {
// is stored as little endian
buffer.order(ByteOrder.LITTLE_ENDIAN);
// fill header
id_length = buffer.get() & 0xFF;
colormap_type = buffer.get() & 0xFF;
image_type = buffer.get() & 0xFF;
// Skip bytes
buffer.position(buffer.position()
+ 2 // unsigned short 'colormap_index'
+ 2 // unsigned short 'colormap_length'
+ 1 // unsigned char 'colormap_size'
+ 2 // unsigned short 'x_origin'
+ 2 // unsigned short 'y_origin'
);
width = buffer.getShort() & 0xFFFF;
height = buffer.getShort() & 0xFFFF;
pixel_size = buffer.get() & 0xFF;
// Skip byte
// - unsigned char 'attributes'
buffer.get();
// fill data
data = buffer.slice();
}
public int getImageType() {
return image_type;
}
public int getColormapType() {
return colormap_type;
}
public int getPixelSize() {
return pixel_size;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getIdLength() {
return id_length;
}
public ByteBuffer getData() {
return data;
}
}