Skip to content

Commit

Permalink
UF16 is not FP16
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Jun 4, 2022
1 parent b37f2d1 commit 3b51e1c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
5 changes: 5 additions & 0 deletions src/PIL/DdsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
DXGI_FORMAT_BC5_TYPELESS = 82
DXGI_FORMAT_BC5_UNORM = 83
DXGI_FORMAT_BC5_SNORM = 84
DXGI_FORMAT_BC6H_UF16 = 95
DXGI_FORMAT_BC7_TYPELESS = 97
DXGI_FORMAT_BC7_UNORM = 98
DXGI_FORMAT_BC7_UNORM_SRGB = 99
Expand Down Expand Up @@ -173,6 +174,10 @@ def _open(self):
self.pixel_format = "BC5S"
n = 5
self.mode = "RGB"
elif dxgi_format == DXGI_FORMAT_BC6H_UF16:
self.pixel_format = "BC6"
n = 6
self.mode = "RGB"
elif dxgi_format in (DXGI_FORMAT_BC7_TYPELESS, DXGI_FORMAT_BC7_UNORM):
self.pixel_format = "BC7"
n = 7
Expand Down
5 changes: 1 addition & 4 deletions src/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,8 @@ PyImaging_BcnDecoderNew(PyObject *self, PyObject *args) {
actual = "L";
break;
case 5: /* BC5: 2-channel 8-bit via 2 BC3 alpha blocks */
actual = "RGB";
break;
case 6: /* BC6: 3-channel 16-bit float */
/* TODO: support 4-channel floating point images */
actual = "RGBAF";
actual = "RGB";
break;
default:
PyErr_SetString(PyExc_ValueError, "block compression type unknown");
Expand Down
18 changes: 7 additions & 11 deletions src/libImaging/BcnDecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ typedef struct {
UINT8 l;
} lum;

typedef struct {
FLOAT32 r, g, b;
} rgb32f;

typedef struct {
UINT16 c0, c1;
UINT32 lut;
Expand Down Expand Up @@ -667,22 +663,22 @@ half_to_float(UINT16 h) {
return o.f;
}

static float
static UINT8
bc6_finalize(int v, int sign) {
if (sign) {
if (v < 0) {
v = ((-v) * 31) / 32;
return half_to_float((UINT16)(0x8000 | v));
return (UINT8)half_to_float((UINT16)(0x8000 | v));
} else {
return half_to_float((UINT16)((v * 31) / 32));
return (UINT8)half_to_float((UINT16)((v * 31) / 32));
}
} else {
return half_to_float((UINT16)((v * 31) / 64));
return v >> 8;
}
}

static void
bc6_lerp(rgb32f *col, int *e0, int *e1, int s, int sign) {
bc6_lerp(rgba *col, int *e0, int *e1, int s, int sign) {
int r, g, b;
int t = 64 - s;
r = (e0[0] * t + e1[0] * s) >> 6;
Expand All @@ -694,7 +690,7 @@ bc6_lerp(rgb32f *col, int *e0, int *e1, int s, int sign) {
}

static void
decode_bc6_block(rgb32f *col, const UINT8 *src, int sign) {
decode_bc6_block(rgba *col, const UINT8 *src, int sign) {
UINT16 endpoints[12]; /* storage for r0, g0, b0, r1, ... */
int ueps[12];
int i, i0, ib2, di, dw, mask, numep, s;
Expand Down Expand Up @@ -862,7 +858,7 @@ decode_bcn(
break;
case 6:
while (bytes >= 16) {
rgb32f col[16];
rgba col[16];
decode_bc6_block(col, ptr, (state->state >> 4) & 1);
put_block(im, state, (const char *)col, sizeof(col[0]), C);
ptr += 16;
Expand Down

0 comments on commit 3b51e1c

Please sign in to comment.