Add raw encoders/decoders for 2 channel RG format

This commit is contained in:
REDxEYE 2022-08-14 16:21:46 +03:00
parent 53e1831617
commit 6b781a623b
4 changed files with 36 additions and 15 deletions

View File

@ -74,14 +74,14 @@ def test_get_texture_size(
("Tests/images/vtf_i8.png", "Tests/images/vtf_i8.vtf", "L", 0.0), ("Tests/images/vtf_i8.png", "Tests/images/vtf_i8.vtf", "L", 0.0),
("Tests/images/vtf_a8.png", "Tests/images/vtf_a8.vtf", "L", 0.0), ("Tests/images/vtf_a8.png", "Tests/images/vtf_a8.vtf", "L", 0.0),
("Tests/images/vtf_ia88.png", "Tests/images/vtf_ia88.vtf", "LA", 0.0), ("Tests/images/vtf_ia88.png", "Tests/images/vtf_ia88.vtf", "LA", 0.0),
# ( (
# "Tests/images/vtf_RG.png", "Tests/images/vtf_uv88.png",
# "Tests/images/vtf_RG.vtf", "RGB", "Tests/images/vtf_uv88.vtf", "RGB",
# 0.0), 0.0),
("Tests/images/vtf_rgb888.png", "Tests/images/vtf_rgb888.vtf", "RGB", 0.0), ("Tests/images/vtf_rgb888.png", "Tests/images/vtf_rgb888.vtf", "RGB", 0.0),
("Tests/images/vtf_bgr888.png", "Tests/images/vtf_bgr888.vtf", "RGB", 0.0), ("Tests/images/vtf_bgr888.png", "Tests/images/vtf_bgr888.vtf", "RGB", 0.0),
("Tests/images/vtf_dxt1.png", "Tests/images/vtf_dxt1.vtf", "RGBA", 3.0), ("Tests/images/vtf_dxt1.png", "Tests/images/vtf_dxt1.vtf", "RGBA", 3.0),
("Tests/images/vtf_rgba8888.png", "Tests/images/vtf_rgba8888.vtf", "RGBA", 0.1), ("Tests/images/vtf_rgba8888.png", "Tests/images/vtf_rgba8888.vtf", "RGBA", 0),
], ],
) )
def test_vtf_loading( def test_vtf_loading(

View File

@ -230,14 +230,7 @@ def _write_image(fp: BufferedIOBase, im: Image.Image, pixel_format: VtfPF):
im = im.convert("LA") im = im.convert("LA")
elif pixel_format == VtfPF.UV88: elif pixel_format == VtfPF.UV88:
encoder = "raw" encoder = "raw"
if im.mode == "RGB" or im.mode == "RGBA": encoder_args = ("RG", 0, 0)
r, g, *_ = im.split()
im = Image.merge("LA", (r, g))
elif im.mode == "LA":
pass
else:
raise VTFException(f"Cannot encode {im.mode} as {pixel_format}")
encoder_args = ("LA", 0, 0)
else: else:
raise VTFException(f"Unsupported pixel format: {pixel_format!r}") raise VTFException(f"Unsupported pixel format: {pixel_format!r}")
@ -327,7 +320,7 @@ class VtfImageFile(ImageFile.ImageFile):
elif pixel_format in (VtfPF.BGRA8888,): elif pixel_format in (VtfPF.BGRA8888,):
tile = ("raw", (0, 0) + self.size, data_start, ("BGRA", 0, 1)) tile = ("raw", (0, 0) + self.size, data_start, ("BGRA", 0, 1))
elif pixel_format in (VtfPF.UV88,): elif pixel_format in (VtfPF.UV88,):
tile = ("raw", (0, 0) + self.size, data_start, ("LA", 0, 1)) tile = ("raw", (0, 0) + self.size, data_start, ("RG", 0, 1))
elif pixel_format in L_FORMATS: elif pixel_format in L_FORMATS:
tile = ("raw", (0, 0) + self.size, data_start, ("L", 0, 1)) tile = ("raw", (0, 0) + self.size, data_start, ("L", 0, 1))
elif pixel_format in LA_FORMATS: elif pixel_format in LA_FORMATS:

View File

@ -242,6 +242,17 @@ packLA(UINT8 *out, const UINT8 *in, int pixels) {
in += 4; in += 4;
} }
} }
static void
packRG(UINT8 *out, const UINT8 *in, int pixels) {
int i;
/* LA, pixel interleaved */
for (i = 0; i < pixels; i++) {
out[0] = in[R];
out[1] = in[G];
out += 2;
in += 4;
}
}
static void static void
packLAL(UINT8 *out, const UINT8 *in, int pixels) { packLAL(UINT8 *out, const UINT8 *in, int pixels) {
@ -491,7 +502,7 @@ copy3(UINT8 *out, const UINT8 *in, int pixels) {
static void static void
copy4(UINT8 *out, const UINT8 *in, int pixels) { copy4(UINT8 *out, const UINT8 *in, int pixels) {
/* RGBA, CMYK quadruples */ /* RGBA, CMYK quadruples */
memcpy(out, in, 4 * pixels); memcpy(out, in, pixels * 4);
} }
static void static void
@ -580,6 +591,7 @@ static struct {
{"RGB", "BGRX", 32, ImagingPackBGRX}, {"RGB", "BGRX", 32, ImagingPackBGRX},
{"RGB", "XBGR", 32, ImagingPackXBGR}, {"RGB", "XBGR", 32, ImagingPackXBGR},
{"RGB", "RGB;L", 24, packRGBL}, {"RGB", "RGB;L", 24, packRGBL},
{"RGB", "RG", 16, packRG},
{"RGB", "R", 8, band0}, {"RGB", "R", 8, band0},
{"RGB", "G", 8, band1}, {"RGB", "G", 8, band1},
{"RGB", "B", 8, band2}, {"RGB", "B", 8, band2},
@ -592,6 +604,7 @@ static struct {
{"RGBA", "BGRA", 32, ImagingPackBGRA}, {"RGBA", "BGRA", 32, ImagingPackBGRA},
{"RGBA", "ABGR", 32, ImagingPackABGR}, {"RGBA", "ABGR", 32, ImagingPackABGR},
{"RGBA", "BGRa", 32, ImagingPackBGRa}, {"RGBA", "BGRa", 32, ImagingPackBGRa},
{"RGBA", "RG", 16, packRG},
{"RGBA", "R", 8, band0}, {"RGBA", "R", 8, band0},
{"RGBA", "G", 8, band1}, {"RGBA", "G", 8, band1},
{"RGBA", "B", 8, band2}, {"RGBA", "B", 8, band2},

View File

@ -424,6 +424,18 @@ unpackLA(UINT8 *_out, const UINT8 *in, int pixels) {
} }
} }
static void
unpackRG(UINT8 *_out, const UINT8 *in, int pixels) {
int i;
/* LA, pixel interleaved */
for (i = 0; i < pixels; i++) {
_out[R] = in[0];
_out[G] = in[1];
in += 2;
_out += 4;
}
}
static void static void
unpackLAL(UINT8 *_out, const UINT8 *in, int pixels) { unpackLAL(UINT8 *_out, const UINT8 *in, int pixels) {
int i; int i;
@ -1547,6 +1559,9 @@ static struct {
{"PA", "PA", 16, unpackLA}, {"PA", "PA", 16, unpackLA},
{"PA", "PA;L", 16, unpackLAL}, {"PA", "PA;L", 16, unpackLAL},
/*2 channel to RGB/RGBA*/
{"RGB", "RG", 16, unpackRG},
{"RGBA", "RG", 16, unpackRG},
/* true colour */ /* true colour */
{"RGB", "RGB", 24, ImagingUnpackRGB}, {"RGB", "RGB", 24, ImagingUnpackRGB},
{"RGB", "RGB;L", 24, unpackRGBL}, {"RGB", "RGB;L", 24, unpackRGBL},