Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexError: list index out of range #1

Open
shura11 opened this issue Nov 12, 2020 · 3 comments · May be fixed by #3
Open

IndexError: list index out of range #1

shura11 opened this issue Nov 12, 2020 · 3 comments · May be fixed by #3

Comments

@shura11
Copy link

shura11 commented Nov 12, 2020

Hello,

I've tried using your code with your original picture and it worked, but with other picture such as the one below, I get the following error :

Traceback (most recent call last):
File "...", line 363, in
img.decode()
File "...", line 350, in decode
len_chunk = self.StartOfScan(data, len_chunk)
File "...", line 290, in StartOfScan
st, 0, self.quant[self.quantMapping[0]], oldlumdccoeff
File "...", line 253, in BuildMatrix
code = self.huffman_tables[0 + idx].GetCode(st)
File "...", line 201, in GetCode
res = self.Find(st)
File "...", line 196, in Find
r = r[st.GetBit()]
IndexError: list index out of range

I've looked into it and it seems that when the error occurs, r is a list of size 1, st.GetBit() is 0, but the code still fails. So I've tried to force Find to return r[0] when r is of size 1, but then another error occurs

Traceback (most recent call last):
File "...", line 363, in
img.decode()
File "...", line 350, in decode
len_chunk = self.StartOfScan(data, len_chunk)
File "...", line 290, in StartOfScan
st, 0, self.quant[self.quantMapping[0]], oldlumdccoeff
File "...", line 260, in BuildMatrix
code = self.huffman_tables[16 + idx].GetCode(st)
File "...", line 201, in GetCode
res = self.Find(st)
File "...", line 196, in Find
r = r[st.GetBit()]
File "...", line 218, in GetBit
b = self.data[self.pos >> 3]
IndexError: list index out of range

Any idea why this occurs ? Thanks in advance !

cat

@Chrisa142857
Copy link

Hi, 😀

I have got the same situation. I've tried three other pictures, and all of them shown the wrong result before hitting the error.

For example, I added a pause into decoding after each DrawMatrix
image
and then it is suspended with the same error

Traceback (most recent call last):
  File "JPEG.py", line 371, in <module>
    img.decode()
  File "JPEG.py", line 353, in decode
    len_chunk = self.StartOfScan(data, len_chunk)
  File "JPEG.py", line 284, in StartOfScan
    st, 0, self.quant[self.quantMapping[0]], oldlumdccoeff
  File "JPEG.py", line 247, in BuildMatrix
    code = self.huffman_tables[0 + idx].GetCode(st)
  File "JPEG.py", line 192, in GetCode
    res = self.Find(st)
  File "JPEG.py", line 187, in Find
    r = r[st.GetBit()]
IndexError: list index out of range

while the input is
mountains

@xiaonengmiao
Copy link

Same issue! Did u guys solve this at the end?

@peter50216 peter50216 linked a pull request Jun 6, 2023 that will close this issue
Repository owner deleted a comment from liMike1998 Feb 23, 2024
Repository owner deleted a comment from liuzhebaba Feb 23, 2024
@DRCRecoveryData
Copy link

Hello,

I've tried using your code with your original picture and it worked, but with other picture such as the one below, I get the following error :

Traceback (most recent call last):

File "...", line 363, in

img.decode()

File "...", line 350, in decode

len_chunk = self.StartOfScan(data, len_chunk)

File "...", line 290, in StartOfScan

st, 0, self.quant[self.quantMapping[0]], oldlumdccoeff

File "...", line 253, in BuildMatrix

code = self.huffman_tables[0 + idx].GetCode(st)

File "...", line 201, in GetCode

res = self.Find(st)

File "...", line 196, in Find

r = r[st.GetBit()]

IndexError: list index out of range

I've looked into it and it seems that when the error occurs, r is a list of size 1, st.GetBit() is 0, but the code still fails. So I've tried to force Find to return r[0] when r is of size 1, but then another error occurs

Traceback (most recent call last):

File "...", line 363, in

img.decode()

File "...", line 350, in decode

len_chunk = self.StartOfScan(data, len_chunk)

File "...", line 290, in StartOfScan

st, 0, self.quant[self.quantMapping[0]], oldlumdccoeff

File "...", line 260, in BuildMatrix

code = self.huffman_tables[16 + idx].GetCode(st)

File "...", line 201, in GetCode

res = self.Find(st)

File "...", line 196, in Find

r = r[st.GetBit()]

File "...", line 218, in GetBit

b = self.data[self.pos >> 3]

IndexError: list index out of range

Any idea why this occurs ? Thanks in advance !

cat

try this:

class HuffmanTable:
    """
    A Huffman Table class
    """

    def __init__(self):
        self.root = []
        self.elements = []

    def BitsFromLengths(self, root, element, pos):
        if isinstance(root, list):
            if pos == 0:
                if len(root) < 2:
                    root.append(element)
                    return True
                return False
            for i in [0, 1]:
                if len(root) == i:
                    root.append([])
                if self.BitsFromLengths(root[i], element, pos - 1) == True:
                    return True
        return False

    def GetHuffmanBits(self, lengths, elements):
        self.elements = elements
        ii = 0
        for i in range(len(lengths)):
            for j in range(lengths[i]):
                self.BitsFromLengths(self.root, elements[ii], i)
                ii += 1

    def Find(self, st):
        r = self.root
        while isinstance(r, list):
            bit = st.GetBit()
            if bit is None:
                return None  # Handle the case when st.GetBit() returns None
            if bit >= len(r):
                return None  # Handle the case when the index is out of range
            r = r[bit]
        return r

    def GetCode(self, st):
        while True:
            res = self.Find(st)
            if res is None:
                return 0
            elif res != -1:
                return res

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants