diff --git a/scripts/image.lua b/scripts/image.lua index bd246f5d5..5d6fa0642 100644 --- a/scripts/image.lua +++ b/scripts/image.lua @@ -4,6 +4,8 @@ -- This file is released under the terms of the MIT license. -- Read LICENSE.txt for more information. +dofile('./test_utils.lua') + local rgba = app.pixelColor.rgba local a = Image(32, 64) @@ -92,3 +94,59 @@ do end end end + +-- Resize image +do + local a = Sprite(3, 2) + local cel = a.cels[1] + assert(cel.bounds == Rectangle(0, 0, 3, 2)) + local img = cel.image + local cols = { rgba(10, 60, 1), rgba(20, 50, 2), rgba(30, 40, 3), + rgba(40, 30, 4), rgba(50, 20, 5), rgba(60, 10, 6) } + array_to_pixels(cols, img) + expect_img(img, cols) + + -- Test resize of a cel with origin=0,0 + + img:resize(img.width*2, img.height*2) + + expect_eq(cel.bounds, Rectangle(0, 0, 6, 4)) + expect_eq(img.width, 6) + expect_eq(img.height, 4) + local cols2 = { cols[1],cols[1], cols[2],cols[2], cols[3],cols[3], + cols[1],cols[1], cols[2],cols[2], cols[3],cols[3], + cols[4],cols[4], cols[5],cols[5], cols[6],cols[6], + cols[4],cols[4], cols[5],cols[5], cols[6],cols[6] } + expect_img(img, cols2) + + -- Undo + function undo() + app.undo() + img = cel.image -- TODO img shouldn't be invalidated, the resize operation should kept the image ID + end + undo() + + -- Test a resize when cel origin > 0,0 + cel.position = Point(2, 1) + expect_eq(cel.bounds, Rectangle(2, 1, 3, 2)) + expect_img(img, cols) + img:resize{ width=6, height=4 } + expect_eq(cel.bounds, Rectangle(2, 1, 6, 4)) -- Position is not modified + expect_eq(img.width, 6) + expect_eq(img.height, 4) + + undo() + img:resize{ size=Size(6, 4), pivot=Point(1, 1) } + expect_eq(cel.bounds, Rectangle(1, 0, 6, 4)) + + undo() + img:resize{ size=Size(6, 4), pivot=Point(3, 2) } + expect_eq(cel.bounds, Rectangle(-1, -1, 6, 4)) + + -- Test resize without cel + + local img2 = Image(img) + expect_img(img2, cols2) + img2:resize(3, 2) + expect_img(img2, cols) +end