Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Aug 12, 2022
1 parent 25e1f4c commit dd5fc1b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
14 changes: 7 additions & 7 deletions src/PIL/VtfImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down Expand Up @@ -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,
Expand All @@ -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)))
)
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ get_packer(ImagingEncoderObject *encoder, const char *mode, const char *rawmode)
}

/* -------------------------------------------------------------------- */
/* BNC */
/* BCN */
/* -------------------------------------------------------------------- */

PyObject *
Expand Down
20 changes: 13 additions & 7 deletions src/libImaging/BcnEncode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
}
Expand Down

0 comments on commit dd5fc1b

Please sign in to comment.