From dd5fc1bd5a4ec847ca737fec27ae65f68c9b4f87 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 12 Aug 2022 23:14:09 +1000 Subject: [PATCH] Minor changes --- src/PIL/VtfImagePlugin.py | 14 +++++++------- src/encode.c | 2 +- src/libImaging/BcnEncode.c | 20 +++++++++++++------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/PIL/VtfImagePlugin.py b/src/PIL/VtfImagePlugin.py index d6e9aae970b..5b5345baf57 100644 --- a/src/PIL/VtfImagePlugin.py +++ b/src/PIL/VtfImagePlugin.py @@ -163,9 +163,9 @@ def _get_texture_size(pixel_format: VtfPF, width, height): return width * height elif pixel_format in LA_FORMATS: return width * height * 2 - elif pixel_format in (VtfPF.RGB888,): + elif pixel_format == VtfPF.RGB888: return width * height * 3 - elif pixel_format in (VtfPF.RGBA8888,): + elif pixel_format == VtfPF.RGBA8888: return width * height * 4 raise VTFException(f"Unsupported VTF pixel format: {pixel_format}") @@ -205,7 +205,7 @@ def _open(self): 0, ) self.fp.seek(header.header_size) - elif (7, 2) <= version < (7, 3): + elif version < (7, 3): header = VTFHeader( *struct.unpack(HEADER_V72, self.fp.read(struct.calcsize(HEADER_V72))), 0, @@ -214,7 +214,7 @@ def _open(self): 0, ) self.fp.seek(header.header_size) - elif (7, 3) <= version < (7, 5): + elif sversion < (7, 5): header = VTFHeader( *struct.unpack(HEADER_V73, self.fp.read(struct.calcsize(HEADER_V73))) ) @@ -270,9 +270,9 @@ def _save(im, fp, filename): im: Image.Image if im.mode not in ("RGB", "RGBA"): raise OSError(f"cannot write mode {im.mode} as VTF") - arguments = im.encoderinfo - pixel_format = VtfPF(arguments.get("pixel_format", VtfPF.RGBA8888)) - version = arguments.get("version", (7, 4)) + encoderinfo = im.encoderinfo + pixel_format = VtfPF(encoderinfo.get("pixel_format", VtfPF.RGBA8888)) + version = encoderinfo.get("version", (7, 4)) flags = CompiledVtfFlags(0) if "A" in im.mode: if pixel_format == VtfPF.DXT1_ONEBITALPHA: diff --git a/src/encode.c b/src/encode.c index 3a7f142f846..4d252351112 100644 --- a/src/encode.c +++ b/src/encode.c @@ -376,7 +376,7 @@ get_packer(ImagingEncoderObject *encoder, const char *mode, const char *rawmode) } /* -------------------------------------------------------------------- */ -/* BNC */ +/* BCN */ /* -------------------------------------------------------------------- */ PyObject * diff --git a/src/libImaging/BcnEncode.c b/src/libImaging/BcnEncode.c index 4f11597bdc5..c1840ab1b43 100644 --- a/src/libImaging/BcnEncode.c +++ b/src/libImaging/BcnEncode.c @@ -77,9 +77,11 @@ selection_sort(Color arr[], UINT32 n) { for (i = 0; i < n - 1; i++) { min_idx = i; - for (j = i + 1; j < n; j++) - if (arr[j].frequency < arr[min_idx].frequency) + for (j = i + 1; j < n; j++) { + if (arr[j].frequency < arr[min_idx].frequency) { min_idx = j; + } + } SWAP(Color, arr[min_idx], arr[i]); } } @@ -103,8 +105,9 @@ pick_2_major_colors( if (color_count == 1) { *color1 = colors[color_count - 1].value; - } else + } else { *color1 = colors[color_count - 2].value; + } } static UINT8 @@ -131,8 +134,9 @@ encode_bc1(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) { bc1_color *blocks = (bc1_color *)buf; UINT8 no_alpha = 0; INT32 block_index; - if (strchr(im->mode, 'A') == NULL) + if (strchr(im->mode, 'A') == NULL) { no_alpha = 1; + } UINT32 block_count = (im->xsize * im->ysize) / 16; if (block_count * sizeof(bc1_color) > bytes) { state->errcode = IMAGING_CODEC_MEMORY; @@ -185,8 +189,9 @@ encode_bc1(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) { UINT16 c0 = 0, c1 = 0; pick_2_major_colors(unique_colors, color_frequency, unique_count, &c0, &c1); - if (c0 < c1 && no_alpha) + if (c0 < c1 && no_alpha) { SWAP(UINT16, c0, c1); + } UINT16 palette[4] = {c0, c1, 0, 0}; if (no_alpha) { @@ -203,10 +208,11 @@ encode_bc1(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) { UINT32 color_id; for (color_id = 0; color_id < 16; ++color_id) { UINT8 bc_color_id; - if (opaque[color_id] || no_alpha) + if (opaque[color_id] || no_alpha) { bc_color_id = get_closest_color_index(palette, all_colors[color_id]); - else + } else { bc_color_id = 3; + } SET_BITS(block->lut, color_id * 2, 2, bc_color_id); } }