This commit is contained in:
Zeecka
2025-12-28 10:51:07 +01:00
parent 1b0c6e6d4b
commit ac1427ddac
2 changed files with 23 additions and 5 deletions
+11
View File
@@ -146,6 +146,17 @@ def analyze_image_resize(input_img: Path, output_dir: Path) -> None:
logs.append(f"Target CRC found: 0x{png.crc:08x}") logs.append(f"Target CRC found: 0x{png.crc:08x}")
if png.crc == png.compute_crc():
logs.append(
f"PNG is already valid with dimensions {png.width}x{png.height} "
f"and crc 0x{png.crc:08x}."
)
output_data: dict[str, Any] = {
"image_resize": {"status": "ok", "output": logs}
}
update_data(output_dir, output_data)
return
saved_img_urls = [] saved_img_urls = []
db_matches = lookup_crc(png) db_matches = lookup_crc(png)
+12 -5
View File
@@ -180,17 +180,24 @@ class PNG:
self.height = size[1] self.height = size[1]
self.bit_depth = bit_depth self.bit_depth = bit_depth
self.color_type = color_type self.color_type = color_type
self.crc = ( self.interlace = interlace
crc self.crc = crc or self.compute_crc()
or zlib.crc32(
def compute_crc(self) -> int:
"""Compute the CRC checksum for the PNG IHDR chunk.
Returns:
int: The computed CRC checksum.
"""
return (
zlib.crc32(
b"IHDR" b"IHDR"
+ self.width.to_bytes(4, "big") + self.width.to_bytes(4, "big")
+ self.height.to_bytes(4, "big") + self.height.to_bytes(4, "big")
+ bytes((bit_depth, color_type, 0, 0, interlace)) + bytes((self.bit_depth, self.color_type, 0, 0, self.interlace))
) )
& 0xFFFFFFFF & 0xFFFFFFFF
) )
self.interlace = interlace
@classmethod @classmethod
def from_packed(cls, packed: int, crc: int | None = None) -> Self: def from_packed(cls, packed: int, crc: int | None = None) -> Self: