Attempt at fixing gif loading.

This commit is contained in:
Relintai 2022-12-10 16:15:38 +01:00
parent 5cb1a3ce2f
commit a7579e1598
2 changed files with 37 additions and 4 deletions

View File

@ -103,10 +103,14 @@ func read(source_file):
img.lock() img.lock()
for y in range(0, img_height): for y in range(0, img_height):
for x in range(0, img_width): for x in range(0, img_width):
if p >= decompressed.size():
break
var c = decompressed[p] var c = decompressed[p]
if transparency == 0 or c != frame_transparent_color: if transparency == 0 or c != frame_transparent_color:
img.set_pixel(img_left + x, img_top + y, local_lut[c]) img.set_pixel(img_left + x, img_top + y, local_lut[c])
p = p + 1 p = p + 1
img.unlock() img.unlock()
var frame = ImageTexture.new() var frame = ImageTexture.new()
frame.create_from_image(img, 0) frame.create_from_image(img, 0)

View File

@ -22,6 +22,12 @@ class CodeEntry:
result += str(element) + ", " result += str(element) + ", "
return result.substr(0, result.length() - 2) return result.substr(0, result.length() - 2)
func duplicate():
var sq : PoolByteArray = PoolByteArray()
sq.append_array(sequence)
var ce : CodeEntry = CodeEntry.new(sq)
return ce
class CodeTable: class CodeTable:
var entries: Dictionary = {} var entries: Dictionary = {}
@ -170,6 +176,9 @@ func decompress_lzw(code_stream_data: PoolByteArray, min_code_size: int, colors:
var prevcode: int = code var prevcode: int = code
# <LOOP POINT> # <LOOP POINT>
while true: while true:
if binary_code_stream.chunk_stream.size() - binary_code_stream.byte_index <= current_code_size:
break
# let CODE be the next code in the code stream # let CODE be the next code in the code stream
code = binary_code_stream.read_bits(current_code_size) code = binary_code_stream.read_bits(current_code_size)
# Detect Clear Code. When detected reset everything and get next code. # Detect Clear Code. When detected reset everything and get next code.
@ -177,6 +186,7 @@ func decompress_lzw(code_stream_data: PoolByteArray, min_code_size: int, colors:
code_table = initialize_color_code_table(colors) code_table = initialize_color_code_table(colors)
current_code_size = min_code_size + 1 current_code_size = min_code_size + 1
code = binary_code_stream.read_bits(current_code_size) code = binary_code_stream.read_bits(current_code_size)
prevcode = code
elif code == clear_code_index + 1: # Stop when detected EOI Code. elif code == clear_code_index + 1: # Stop when detected EOI Code.
break break
# is CODE in the code table? # is CODE in the code table?
@ -188,18 +198,37 @@ func decompress_lzw(code_stream_data: PoolByteArray, min_code_size: int, colors:
var k: CodeEntry = CodeEntry.new([code_entry.sequence[0]]) var k: CodeEntry = CodeEntry.new([code_entry.sequence[0]])
# warning-ignore:return_value_discarded # warning-ignore:return_value_discarded
# add {PREVCODE} + k to the code table # add {PREVCODE} + k to the code table
code_table.add(code_table.get(prevcode).add(k)) var prev_code_entry: CodeEntry = code_table.get(prevcode)
if prev_code_entry:
prev_code_entry = prev_code_entry.duplicate()
code_table.add(prev_code_entry.add(k))
else:
code_table.add(k)
# set PREVCODE = CODE # set PREVCODE = CODE
prevcode = code prevcode = code
else: # if NO else: # if NO
# let k be the first index of {PREVCODE} # let k be the first index of {PREVCODE}
var prevcode_entry: CodeEntry = code_table.get(prevcode) var prevcode_entry: CodeEntry = code_table.get(prevcode)
var k: CodeEntry = CodeEntry.new([prevcode_entry.sequence[0]])
var k: CodeEntry
if prevcode_entry:
k = CodeEntry.new([prevcode_entry.sequence[0]])
else:
k = CodeEntry.new([ code ])
# output {PREVCODE} + k to index stream # output {PREVCODE} + k to index stream
if prevcode_entry:
index_stream.append_array(prevcode_entry.add(k).sequence) index_stream.append_array(prevcode_entry.add(k).sequence)
else:
index_stream.append_array(k.sequence)
# add {PREVCODE} + k to code table # add {PREVCODE} + k to code table
# warning-ignore:return_value_discarded # warning-ignore:return_value_discarded
if prevcode_entry:
code_table.add(prevcode_entry.add(k)) code_table.add(prevcode_entry.add(k))
else:
code_table.add(k)
# set PREVCODE = CODE # set PREVCODE = CODE
prevcode = code prevcode = code