Skip to content

Commit

Permalink
style: Fix new read-whole-file (FURB101) errors
Browse files Browse the repository at this point in the history
  • Loading branch information
echoix committed Jan 3, 2025
1 parent a59faab commit eea7646
Showing 1 changed file with 52 additions and 53 deletions.
105 changes: 52 additions & 53 deletions python/grass/imaging/images2swf.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@

import os
import zlib
from pathlib import Path

try:
import numpy as np
Expand Down Expand Up @@ -921,62 +922,60 @@ def readSwf(filename, asNumpy=True):
images = []

# Open file and read all
with open(filename, "rb") as fp:
bb = fp.read()

# Check opening tag
tmp = bb[0:3].decode("ascii", "ignore")
if tmp.upper() == "FWS":
pass # ok
elif tmp.upper() == "CWS":
# Decompress movie
bb = bb[:8] + zlib.decompress(bb[8:])
bb = Path(filename).read_bytes()
# Check opening tag
tmp = bb[0:3].decode("ascii", "ignore")
if tmp.upper() == "FWS":
pass # ok
elif tmp.upper() == "CWS":
# Decompress movie
bb = bb[:8] + zlib.decompress(bb[8:])
else:
raise OSError("Not a valid SWF file: " + str(filename))

# Set filepointer at first tag (skipping framesize RECT and two uin16's
i = 8
nbits = bitsToInt(bb[i : i + 1], 5) # skip FrameSize
nbits = 5 + nbits * 4
Lrect = nbits / 8.0
if Lrect % 1:
Lrect += 1
Lrect = int(Lrect)
i += Lrect + 4

# Iterate over the tags
counter = 0
while True:
counter += 1

# Get tag header
head = bb[i : i + 6]
if not head:
break # Done (we missed end tag)

# Determine type and length
T, L1, L2 = getTypeAndLen(head)
if not L2:
print("Invalid tag length, could not proceed")
break
# print(T, L2)

# Read image if we can
if T in {20, 36}:
im = _readPixels(bb, i + 6, T, L1)
if im is not None:
images.append(im)
elif T in {6, 21, 35, 90}:
print("Ignoring JPEG image: cannot read JPEG.")
else:
raise OSError("Not a valid SWF file: " + str(filename))

# Set filepointer at first tag (skipping framesize RECT and two uin16's
i = 8
nbits = bitsToInt(bb[i : i + 1], 5) # skip FrameSize
nbits = 5 + nbits * 4
Lrect = nbits / 8.0
if Lrect % 1:
Lrect += 1
Lrect = int(Lrect)
i += Lrect + 4

# Iterate over the tags
counter = 0
while True:
counter += 1

# Get tag header
head = bb[i : i + 6]
if not head:
break # Done (we missed end tag)

# Determine type and length
T, L1, L2 = getTypeAndLen(head)
if not L2:
print("Invalid tag length, could not proceed")
break
# print(T, L2)

# Read image if we can
if T in [20, 36]:
im = _readPixels(bb, i + 6, T, L1)
if im is not None:
images.append(im)
elif T in [6, 21, 35, 90]:
print("Ignoring JPEG image: cannot read JPEG.")
else:
pass # Not an image tag
pass # Not an image tag

# Detect end tag
if T == 0:
break
# Detect end tag
if T == 0:
break

# Next tag!
i += L2
# Next tag!
i += L2
# Convert to normal PIL images if needed
if not asNumpy:
images2 = images
Expand Down

0 comments on commit eea7646

Please sign in to comment.