Skip to content

Commit

Permalink
Implement indexed mode (#1136)
Browse files Browse the repository at this point in the history
* Create a custom PixeloramaImage class, initial support for indexed mode

* Convert opened projects and images to indexed mode

* Use shaders for RGB to Indexed conversion and vice versa

* Add `is_indexed` variable in PixeloramaImage

* Basic undo/redo support for indexed mode when drawing

* Make image effects respect indexed mode

* Move code from image effects to ShaderImageEffect instead

* Bucket tool works with indexed mode

* Move and selection tools works with indexed mode

* Brushes respect indexed mode

* Add color_mode variable and some helper methods in Project

Replace hard-coded cases of Image.FORMAT_RGBA8 with `Project.get_image_format()` just in case we want to add more formats in the future

* Add a helper new_empty_image() method to Project

* Set new images to indexed if the project is indexed

* Change color modes from the Image menu

* Fix open image to replace cel

* Load/save indices in pxo files

* Merging layers works with indexed mode

* Layer effects respect indexed mode

* Add an `other_image` parameter to `PixeloramaImage.add_data_to_dictionary()`

* Scale image works with indexed mode

* Resizing works with indexed mode

* Fix non-shader rotation not working with indexed mode

* Minor refactor of PixeloramaImage's set_pixelv_custom()

* Make the text tool work with indexed mode

* Remove print from PixeloramaImage

* Rename "PixeloramaImage" to "ImageExtended"

* Add docstrings in ImageExtended

* Set color mode from the create new image dialog

* Update Translations.pot

* Show the color mode in the project properties dialog
  • Loading branch information
OverloadedOrama authored Nov 20, 2024
1 parent 74d95c2 commit 2d28136
Show file tree
Hide file tree
Showing 39 changed files with 750 additions and 268 deletions.
12 changes: 12 additions & 0 deletions Translations/Translations.pot
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,18 @@ msgstr ""
msgid "Percentage"
msgstr ""

#. Found in the create new image dialog. Allows users to change the color mode of the new project, such as RGBA or indexed mode.
msgid "Color mode:"
msgstr ""

#. Found in the image menu. A submenu that allows users to change the color mode of the project, such as RGBA or indexed mode.
msgid "Color Mode"
msgstr ""

#. Found in the image menu, under the "Color Mode" submenu. Refers to the indexed color mode. See this wikipedia page for more information: https://en.wikipedia.org/wiki/Indexed_color
msgid "Indexed"
msgstr ""

#. Found in the image menu. Sets the size of the project to be the same as the size of the active selection.
msgid "Crop to Selection"
msgstr ""
Expand Down
125 changes: 74 additions & 51 deletions src/Autoload/DrawingAlgos.gd
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func get_ellipse_points_filled(pos: Vector2i, size: Vector2i, thickness := 1) ->

func scale_3x(sprite: Image, tol := 0.196078) -> Image:
var scaled := Image.create(
sprite.get_width() * 3, sprite.get_height() * 3, false, Image.FORMAT_RGBA8
sprite.get_width() * 3, sprite.get_height() * 3, sprite.has_mipmaps(), sprite.get_format()
)
var width_minus_one := sprite.get_width() - 1
var height_minus_one := sprite.get_height() - 1
Expand Down Expand Up @@ -509,6 +509,8 @@ func similar_colors(c1: Color, c2: Color, tol := 0.392157) -> bool:
func center(indices: Array) -> void:
var project := Global.current_project
Global.canvas.selection.transform_content_confirm()
var redo_data := {}
var undo_data := {}
project.undos += 1
project.undo_redo.create_action("Center Frames")
for frame in indices:
Expand All @@ -528,46 +530,68 @@ func center(indices: Array) -> void:
for cel in project.frames[frame].cels:
if not cel is PixelCel:
continue
var sprite := Image.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8)
sprite.blend_rect(cel.image, used_rect, offset)
Global.undo_redo_compress_images({cel.image: sprite.data}, {cel.image: cel.image.data})
var cel_image := (cel as PixelCel).get_image()
var tmp_centered := project.new_empty_image()
tmp_centered.blend_rect(cel.image, used_rect, offset)
var centered := ImageExtended.new()
centered.copy_from_custom(tmp_centered, cel_image.is_indexed)
centered.add_data_to_dictionary(redo_data, cel_image)
cel_image.add_data_to_dictionary(undo_data)
Global.undo_redo_compress_images(redo_data, undo_data)
project.undo_redo.add_undo_method(Global.undo_or_redo.bind(true))
project.undo_redo.add_do_method(Global.undo_or_redo.bind(false))
project.undo_redo.commit_action()


func scale_image(width: int, height: int, interpolation: int) -> void:
func scale_project(width: int, height: int, interpolation: int) -> void:
var redo_data := {}
var undo_data := {}
for f in Global.current_project.frames:
for i in range(f.cels.size() - 1, -1, -1):
var cel := f.cels[i]
if not cel is PixelCel:
continue
var sprite := Image.new()
sprite.copy_from(cel.get_image())
if interpolation == Interpolation.SCALE3X:
var times := Vector2i(
ceili(width / (3.0 * sprite.get_width())),
ceili(height / (3.0 * sprite.get_height()))
)
for _j in range(maxi(times.x, times.y)):
sprite.copy_from(scale_3x(sprite))
sprite.resize(width, height, Image.INTERPOLATE_NEAREST)
elif interpolation == Interpolation.CLEANEDGE:
var gen := ShaderImageEffect.new()
gen.generate_image(sprite, clean_edge_shader, {}, Vector2i(width, height))
elif interpolation == Interpolation.OMNISCALE and omniscale_shader:
var gen := ShaderImageEffect.new()
gen.generate_image(sprite, omniscale_shader, {}, Vector2i(width, height))
else:
sprite.resize(width, height, interpolation)
redo_data[cel.image] = sprite.data
undo_data[cel.image] = cel.image.data
var cel_image := (cel as PixelCel).get_image()
var sprite := _resize_image(cel_image, width, height, interpolation) as ImageExtended
sprite.add_data_to_dictionary(redo_data, cel_image)
cel_image.add_data_to_dictionary(undo_data)

general_do_and_undo_scale(width, height, redo_data, undo_data)


func _resize_image(
image: Image, width: int, height: int, interpolation: Image.Interpolation
) -> Image:
var new_image: Image
if image is ImageExtended:
new_image = ImageExtended.new()
new_image.is_indexed = image.is_indexed
new_image.copy_from(image)
new_image.select_palette("", false)
else:
new_image = Image.new()
new_image.copy_from(image)
if interpolation == Interpolation.SCALE3X:
var times := Vector2i(
ceili(width / (3.0 * new_image.get_width())),
ceili(height / (3.0 * new_image.get_height()))
)
for _j in range(maxi(times.x, times.y)):
new_image.copy_from(scale_3x(new_image))
new_image.resize(width, height, Image.INTERPOLATE_NEAREST)
elif interpolation == Interpolation.CLEANEDGE:
var gen := ShaderImageEffect.new()
gen.generate_image(new_image, clean_edge_shader, {}, Vector2i(width, height), false)
elif interpolation == Interpolation.OMNISCALE and omniscale_shader:
var gen := ShaderImageEffect.new()
gen.generate_image(new_image, omniscale_shader, {}, Vector2i(width, height), false)
else:
new_image.resize(width, height, interpolation)
if new_image is ImageExtended:
new_image.on_size_changed()
return new_image


## Sets the size of the project to be the same as the size of the active selection.
func crop_to_selection() -> void:
if not Global.current_project.has_selection:
Expand All @@ -577,13 +601,13 @@ func crop_to_selection() -> void:
Global.canvas.selection.transform_content_confirm()
var rect: Rect2i = Global.canvas.selection.big_bounding_rectangle
# Loop through all the cels to crop them
for f in Global.current_project.frames:
for cel in f.cels:
if not cel is PixelCel:
continue
var sprite := cel.get_image().get_region(rect)
redo_data[cel.image] = sprite.data
undo_data[cel.image] = cel.image.data
for cel in Global.current_project.get_all_pixel_cels():
var cel_image := cel.get_image()
var tmp_cropped := cel_image.get_region(rect)
var cropped := ImageExtended.new()
cropped.copy_from_custom(tmp_cropped, cel_image.is_indexed)
cropped.add_data_to_dictionary(redo_data, cel_image)
cel_image.add_data_to_dictionary(undo_data)

general_do_and_undo_scale(rect.size.x, rect.size.y, redo_data, undo_data)

Expand Down Expand Up @@ -615,32 +639,31 @@ func crop_to_content() -> void:
var redo_data := {}
var undo_data := {}
# Loop through all the cels to trim them
for f in Global.current_project.frames:
for cel in f.cels:
if not cel is PixelCel:
continue
var sprite := cel.get_image().get_region(used_rect)
redo_data[cel.image] = sprite.data
undo_data[cel.image] = cel.image.data
for cel in Global.current_project.get_all_pixel_cels():
var cel_image := cel.get_image()
var tmp_cropped := cel_image.get_region(used_rect)
var cropped := ImageExtended.new()
cropped.copy_from_custom(tmp_cropped, cel_image.is_indexed)
cropped.add_data_to_dictionary(redo_data, cel_image)
cel_image.add_data_to_dictionary(undo_data)

general_do_and_undo_scale(width, height, redo_data, undo_data)


func resize_canvas(width: int, height: int, offset_x: int, offset_y: int) -> void:
var redo_data := {}
var undo_data := {}
for f in Global.current_project.frames:
for cel in f.cels:
if not cel is PixelCel:
continue
var sprite := Image.create(width, height, false, Image.FORMAT_RGBA8)
sprite.blend_rect(
cel.get_image(),
Rect2i(Vector2i.ZERO, Global.current_project.size),
Vector2i(offset_x, offset_y)
)
redo_data[cel.image] = sprite.data
undo_data[cel.image] = cel.image.data
for cel in Global.current_project.get_all_pixel_cels():
var cel_image := cel.get_image()
var resized := ImageExtended.create_custom(
width, height, cel_image.has_mipmaps(), cel_image.get_format(), cel_image.is_indexed
)
resized.blend_rect(
cel_image, Rect2i(Vector2i.ZERO, cel_image.get_size()), Vector2i(offset_x, offset_y)
)
resized.convert_rgb_to_indexed()
resized.add_data_to_dictionary(redo_data, cel_image)
cel_image.add_data_to_dictionary(undo_data)

general_do_and_undo_scale(width, height, redo_data, undo_data)

Expand Down
8 changes: 4 additions & 4 deletions src/Autoload/Export.gd
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func cache_blended_frames(project := Global.current_project) -> void:
blended_frames.clear()
var frames := _calculate_frames(project)
for frame in frames:
var image := Image.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8)
var image := project.new_empty_image()
_blend_layers(image, frame)
blended_frames[frame] = image

Expand Down Expand Up @@ -208,7 +208,7 @@ func process_spritesheet(project := Global.current_project) -> void:
spritesheet_columns = temp
var width := project.size.x * spritesheet_columns
var height := project.size.y * spritesheet_rows
var whole_image := Image.create(width, height, false, Image.FORMAT_RGBA8)
var whole_image := Image.create(width, height, false, project.get_image_format())
var origin := Vector2i.ZERO
var hh := 0
var vv := 0
Expand Down Expand Up @@ -287,10 +287,10 @@ func process_animation(project := Global.current_project) -> void:
ProcessedImage.new(image, project.frames.find(frame), duration)
)
else:
var image := Image.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8)
var image := project.new_empty_image()
image.copy_from(blended_frames[frame])
if erase_unselected_area and project.has_selection:
var crop := Image.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8)
var crop := project.new_empty_image()
var selection_image = project.selection_map.return_cropped_copy(project.size)
crop.blit_rect_mask(
image, selection_image, Rect2i(Vector2i.ZERO, image.get_size()), Vector2i.ZERO
Expand Down
14 changes: 12 additions & 2 deletions src/Autoload/Global.gd
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ enum WindowMenu { WINDOW_OPACITY, PANELS, LAYOUTS, MOVABLE_PANELS, ZEN_MODE, FUL
## Enumeration of items present in the Image Menu.
enum ImageMenu {
PROJECT_PROPERTIES,
COLOR_MODE,
RESIZE_CANVAS,
SCALE_IMAGE,
CROP_TO_SELECTION,
Expand Down Expand Up @@ -1113,8 +1114,17 @@ func undo_redo_compress_images(
func undo_redo_draw_op(
image: Image, new_size: Vector2i, compressed_image_data: PackedByteArray, buffer_size: int
) -> void:
var decompressed := compressed_image_data.decompress(buffer_size)
image.set_data(new_size.x, new_size.y, image.has_mipmaps(), image.get_format(), decompressed)
if image is ImageExtended and image.is_indexed:
# If using indexed mode,
# just convert the indices to RGB instead of setting the image data directly.
if image.get_size() != new_size:
image.crop(new_size.x, new_size.y)
image.convert_indexed_to_rgb()
else:
var decompressed := compressed_image_data.decompress(buffer_size)
image.set_data(
new_size.x, new_size.y, image.has_mipmaps(), image.get_format(), decompressed
)


## This method is used to write project setting overrides to the override.cfg file, located
Expand Down
Loading

0 comments on commit 2d28136

Please sign in to comment.