Skip to content

Commit

Permalink
Save compressed images in undo/redo memory when scaling and centering…
Browse files Browse the repository at this point in the history
… frames
  • Loading branch information
OverloadedOrama committed Nov 14, 2023
1 parent 5d7da07 commit 9279a8e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
25 changes: 10 additions & 15 deletions src/Autoload/DrawingAlgos.gd
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,11 @@ func scale_image(width: int, height: int, interpolation: int) -> void:

for f in Global.current_project.frames:
for i in range(f.cels.size() - 1, -1, -1):
if not f.cels[i] is PixelCel:
var cel := f.cels[i]
if not cel is PixelCel:
continue
var sprite := Image.new()
sprite.copy_from(f.cels[i].get_image())
sprite.copy_from(cel.get_image())
if interpolation == Interpolation.SCALE3X:
var times := Vector2i(
ceili(width / (3.0 * sprite.get_width())),
Expand All @@ -497,10 +498,7 @@ func scale_image(width: int, height: int, interpolation: int) -> void:
gen.generate_image(sprite, omniscale_shader, {}, Vector2i(width, height))
else:
sprite.resize(width, height, interpolation)
Global.current_project.undo_redo.add_do_property(f.cels[i].image, "data", sprite.data)
Global.current_project.undo_redo.add_undo_property(
f.cels[i].image, "data", f.cels[i].image.data
)
Global.undo_redo_compress_images({cel.image: sprite.data}, {cel.image: cel.image.data})

general_undo_scale()

Expand Down Expand Up @@ -529,8 +527,7 @@ func center(indices: Array) -> void:
continue
var sprite := Image.create(project.size.x, project.size.y, false, Image.FORMAT_RGBA8)
sprite.blend_rect(cel.image, used_rect, offset)
project.undo_redo.add_do_property(cel.image, "data", sprite.data)
project.undo_redo.add_undo_property(cel.image, "data", cel.image.data)
Global.undo_redo_compress_images({cel.image: sprite.data}, {cel.image: cel.image.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()
Expand Down Expand Up @@ -565,26 +562,24 @@ func crop_image() -> void:
if not cel is PixelCel:
continue
var sprite := cel.get_image().get_region(used_rect)
Global.current_project.undo_redo.add_do_property(cel.image, "data", sprite.data)
Global.current_project.undo_redo.add_undo_property(cel.image, "data", cel.image.data)
Global.undo_redo_compress_images({cel.image: sprite.data}, {cel.image: cel.image.data})

general_undo_scale()


func resize_canvas(width: int, height: int, offset_x: int, offset_y: int) -> void:
general_do_scale(width, height)
for f in Global.current_project.frames:
for c in f.cels:
if not c is PixelCel:
for cel in f.cels:
if not cel is PixelCel:
continue
var sprite := Image.create(width, height, false, Image.FORMAT_RGBA8)
sprite.blend_rect(
c.get_image(),
cel.get_image(),
Rect2i(Vector2i.ZERO, Global.current_project.size),
Vector2i(offset_x, offset_y)
)
Global.current_project.undo_redo.add_do_property(c.image, "data", sprite.data)
Global.current_project.undo_redo.add_undo_property(c.image, "data", c.image.data)
Global.undo_redo_compress_images({cel.image: sprite.data}, {cel.image: cel.image.data})

general_undo_scale()

Expand Down
29 changes: 15 additions & 14 deletions src/Autoload/Global.gd
Original file line number Diff line number Diff line change
Expand Up @@ -863,31 +863,32 @@ func undo_redo_compress_images(
for image in redo_data:
if not image is Image:
continue
var buffer_size: int = redo_data[image]["data"].size()
var compressed_data: PackedByteArray = redo_data[image]["data"].compress()
project.undo_redo.add_do_method(undo_redo_draw_op.bind(image, compressed_data, buffer_size))
var new_image: Dictionary = redo_data[image]
var new_size := Vector2i(new_image["width"], new_image["height"])
var buffer_size: int = new_image["data"].size()
var compressed_data: PackedByteArray = new_image["data"].compress()
project.undo_redo.add_do_method(
undo_redo_draw_op.bind(image, new_size, compressed_data, buffer_size)
)
for image in undo_data:
if not image is Image:
continue
var buffer_size: int = undo_data[image]["data"].size()
var compressed_data: PackedByteArray = undo_data[image]["data"].compress()
var new_image: Dictionary = undo_data[image]
var new_size := Vector2i(new_image["width"], new_image["height"])
var buffer_size: int = new_image["data"].size()
var compressed_data: PackedByteArray = new_image["data"].compress()
project.undo_redo.add_undo_method(
undo_redo_draw_op.bind(image, compressed_data, buffer_size)
undo_redo_draw_op.bind(image, new_size, compressed_data, buffer_size)
)


## Decompresses the [param compressed_image_data] with [param buffer_size] to the [param image]
## This is an optimization method used while performing undo/redo drawing operations.
func undo_redo_draw_op(
image: Image, compressed_image_data: PackedByteArray, buffer_size: int
image: Image, new_size: Vector2i, compressed_image_data: PackedByteArray, buffer_size: int
) -> void:
image.set_data(
image.get_width(),
image.get_height(),
image.has_mipmaps(),
image.get_format(),
compressed_image_data.decompress(buffer_size)
)
var decompressed := compressed_image_data.decompress(buffer_size)
image.set_data(new_size.x, new_size.y, image.has_mipmaps(), image.get_format(), decompressed)


## Used by the Move tool for undo/redo, moves all of the [Image]s in [param images]
Expand Down

0 comments on commit 9279a8e

Please sign in to comment.