Skip to content

Commit

Permalink
handle passing palette to shader better - prevent white background wh…
Browse files Browse the repository at this point in the history
…en palette size < 256

change some prints to push_warning
  • Loading branch information
mrgudenheim committed Nov 20, 2024
1 parent da60af3 commit c307c6b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
28 changes: 18 additions & 10 deletions src/Extensions/FFTorama/Main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,7 @@ func set_background_color(color):
if !is_instance_valid(api):
return

#push_warning("Setting background color: " + str(color))
assembled_frame_viewport.sprite_background.texture = ImageTexture.create_from_image(create_blank_frame(color))
assembled_animation_viewport.sprite_background.texture = ImageTexture.create_from_image(create_blank_frame(color))

Expand Down Expand Up @@ -1293,16 +1294,23 @@ func _on_new_palette_selected() -> void:


func _on_palette_changed() -> void:
if Palettes.current_palette.colors.size() == 256:
var palette_image:Image = Image.create_empty(16, 16, false, Image.FORMAT_RGBAF) # RGBA8 would perform a sRGB to linear conversion which would change the color
for y in palette_image.get_height():
for x in palette_image.get_width():
#print_debug(str(Vector2i(x, y)) + " - " + str(x + (y * palette_image.get_width())) + " - " + str(Palettes.current_palette.colors[x + (y * palette_image.get_width())].color))
var shader_palette_color:Color = Palettes.current_palette.colors[x + (y * palette_image.get_width())].color
palette_image.set_pixel(x, y, shader_palette_color)
palette_texture = ImageTexture.create_from_image(palette_image)
assembled_frame_node.material.set_shader_parameter("palette", palette_texture)
export_texture.material.set_shader_parameter("palette", palette_texture)
var palette_image:Image = Image.create_empty(Palettes.current_palette.width, Palettes.current_palette.height, false, Image.FORMAT_RGBAF) # RGBA8 would perform a sRGB to linear conversion which would change the color
palette_image.fill(Color.MAGENTA)
for y in palette_image.get_height():
for x in palette_image.get_width():
#print_debug(str(Vector2i(x, y)) + " - " + str(x + (y * palette_image.get_width())) + " - " + str(Palettes.current_palette.colors[x + (y * palette_image.get_width())].color))
# handle when there are less colors than Palettes.current_palette.width * Palettes.current_palette.height
if (x + (y * palette_image.get_width())) >= Palettes.current_palette.colors.size():
push_warning("Trying to get color " + str(x + (y * palette_image.get_width())) + " out of " + str(Palettes.current_palette.colors.size()))
continue

var shader_palette_color:Color = Palettes.current_palette.colors[x + (y * palette_image.get_width())].color
palette_image.set_pixel(x, y, shader_palette_color)

palette_texture = ImageTexture.create_from_image(palette_image)
assembled_frame_node.material.set_shader_parameter("palette", palette_texture)
export_texture.material.set_shader_parameter("palette", palette_texture)


class CelSelector:
var cel_api
Expand Down
16 changes: 8 additions & 8 deletions src/Extensions/FFTorama/bmp.gd
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const bit_depth = {

func _init(bmp_file:PackedByteArray = []):
if bmp_file.size() == 0:
print_debug("file is empty")
push_warning("file is empty")
return

pixel_data_start = bmp_file.decode_u32(0x000A)
Expand All @@ -38,7 +38,7 @@ func _init(bmp_file:PackedByteArray = []):
# store palette_colors
color_palette.resize(num_colors)
if bits_per_pixel > 8:
print_debug("Bit depth > 8, no palette to extract") # a compressed 16bpp format can use a palette, but is not covered by this utility
push_warning("Bit depth > 8, no palette to extract") # a compressed 16bpp format can use a palette, but is not covered by this utility
else:
for i in num_colors:
var color:Color = Color.BLACK
Expand All @@ -51,7 +51,7 @@ func _init(bmp_file:PackedByteArray = []):

# store color_indices
if bits_per_pixel > 8:
print_debug("Bit depth > 8, colors are not indexed") # a compressed 16bpp format can use indexed colors, but is not covered by this utility
push_warning("Bit depth > 8, colors are not indexed") # a compressed 16bpp format can use indexed colors, but is not covered by this utility
else:
color_indices.resize(num_pixels)
for i in num_pixels:
Expand Down Expand Up @@ -94,7 +94,7 @@ func _init(bmp_file:PackedByteArray = []):
color.r8 = word & bmp_file.decode_u8(pixel_data_start + pixel_offset + 2) # red
pixel_colors[i] = color
else:
print_debug("Bit depth != 1, 4, 8, 16, or 24")
push_warning("Bit depth != 1, 4, 8, 16, or 24")


func set_color_indexed_data(image:Image, palette:Array[Color]):
Expand All @@ -109,7 +109,7 @@ func set_color_indexed_data(image:Image, palette:Array[Color]):
num_colors = palette.size()
color_palette = palette.duplicate()

#print_debug(color_palette)
#push_warning(color_palette)
var color_palette_lookup := {}
for i in num_colors:
if not color_palette_lookup.has(str(color_palette[i])): # only get lowest index lookup
Expand Down Expand Up @@ -161,13 +161,13 @@ func set_colors_by_indices() -> void:
else:
pixel_colors[i] = color_palette[color_indices[i]]
else:
print_debug("Bit depth > 8, colors are not indexed")
push_warning("Bit depth > 8, colors are not indexed")


static func create_paletted_bmp(image:Image, palette:Array[Color], bits_per_pixel = 8) -> PackedByteArray:
var bmp_file:PackedByteArray = []
if not (bits_per_pixel == 1 or bits_per_pixel == 4 or bits_per_pixel == 8 or bits_per_pixel == 16 or bits_per_pixel == 24):
print_debug("not valid bits_per_pixel: " + str(bits_per_pixel))
push_warning("not valid bits_per_pixel: " + str(bits_per_pixel))
return bmp_file

#image.convert(Image.FORMAT_RGBAF)
Expand Down Expand Up @@ -231,7 +231,7 @@ static func create_paletted_bmp(image:Image, palette:Array[Color], bits_per_pixe
if color_index.has(color_string):
index = color_index[color_string]
else:
print_debug("color at " + str(Vector2i(x,y)) + " not in palette: " + color_string + " - " + str(color * 255))
push_warning("color at " + str(Vector2i(x,y)) + " not in palette: " + color_string + " - " + str(color * 255))
index = 0

if bits_per_pixel <= 8:
Expand Down

0 comments on commit c307c6b

Please sign in to comment.