mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-30 01:52:18 +00:00
refactor(images): hoist the SOF payload parse out of the jpeg loop
This commit is contained in:
parent
a0a87d458b
commit
6c58cb4267
1 changed files with 8 additions and 5 deletions
|
|
@ -241,6 +241,13 @@ def _webp_dimensions(head: bytes) -> ImageDimensions | None:
|
|||
return None
|
||||
|
||||
|
||||
def _sof_dimensions(sof: bytes) -> ImageDimensions | None:
|
||||
if len(sof) < _JPEG_SOF_PAYLOAD_SIZE:
|
||||
return None
|
||||
unpacked: Final[tuple[int, int]] = struct.unpack(">HH", sof[1:5])
|
||||
return ImageDimensions(width=unpacked[1], height=unpacked[0])
|
||||
|
||||
|
||||
def _jpeg_fill_run(stream: IO[bytes], start: int, offset: int) -> int:
|
||||
stream.seek(start + offset)
|
||||
fill: Final = stream.read(_JPEG_FILL_CHUNK)
|
||||
|
|
@ -261,11 +268,7 @@ def _jpeg_sof_dimensions(stream: IO[bytes], start: int) -> ImageDimensions | Non
|
|||
offset += _jpeg_fill_run(stream, start, offset)
|
||||
continue
|
||||
if marker[1] in _JPEG_SOF_MARKERS:
|
||||
sof = stream.read(_JPEG_SOF_PAYLOAD_SIZE)
|
||||
if len(sof) < _JPEG_SOF_PAYLOAD_SIZE:
|
||||
return None
|
||||
unpacked: Final[tuple[int, int]] = struct.unpack(">HH", sof[1:5])
|
||||
return ImageDimensions(width=unpacked[1], height=unpacked[0])
|
||||
return _sof_dimensions(stream.read(_JPEG_SOF_PAYLOAD_SIZE))
|
||||
segment_length = int.from_bytes(marker[2:4], "big")
|
||||
if segment_length < 2:
|
||||
return None
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue