Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect to the BC6 decoding from Python #1

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 9 additions & 13 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 @@ -744,7 +740,7 @@ decode_bc6_block(rgb32f *col, const UINT8 *src, int sign) {
}
if (sign || info->tr) { /* sign-extend e1,2,3 if signed or deltas */
for (i = 3; i < numep; i += 3) {
bc6_sign_extend(&endpoints[i + 0], info->rb);
bc6_sign_extend(&endpoints[i], info->rb);
bc6_sign_extend(&endpoints[i + 1], info->gb);
bc6_sign_extend(&endpoints[i + 2], info->bb);
}
Expand All @@ -757,7 +753,7 @@ decode_bc6_block(rgb32f *col, const UINT8 *src, int sign) {
}
if (sign) {
for (i = 3; i < numep; i += 3) {
bc6_sign_extend(&endpoints[i + 0], info->rb);
bc6_sign_extend(&endpoints[i], info->rb);
bc6_sign_extend(&endpoints[i + 1], info->gb);
bc6_sign_extend(&endpoints[i + 2], info->bb);
}
Expand Down Expand Up @@ -864,7 +860,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