2023-07-11 16:33:45 +00:00
|
|
|
-- Copyright (C) 2022-2023 Igara Studio S.A.
|
2022-06-13 22:17:05 +00:00
|
|
|
--
|
|
|
|
-- This file is released under the terms of the MIT license.
|
|
|
|
-- Read LICENSE.txt for more information.
|
|
|
|
|
2023-07-11 16:33:45 +00:00
|
|
|
function fix_images(testImg, scale, fileExt, c, cm, c1)
|
|
|
|
-- GIF file is loaded as indexed, so we have to convert from indexed
|
|
|
|
-- to the ColorMode
|
|
|
|
if c.colorMode ~= cm then
|
|
|
|
assert(fileExt == "gif" or fileExt == "bmp")
|
|
|
|
|
|
|
|
if cm == ColorMode.RGB then
|
|
|
|
app.sprite = c
|
|
|
|
app.command.ChangePixelFormat{ format="rgb" }
|
|
|
|
elseif cm == ColorMode.GRAYSCALE then
|
|
|
|
app.sprite = c
|
|
|
|
app.command.ChangePixelFormat{ format="grayscale" }
|
|
|
|
else
|
|
|
|
assert(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-15 13:20:13 +00:00
|
|
|
-- With file formats that don't support alpha channel, we
|
|
|
|
-- compare totally transparent pixels (alpha=0) with black.
|
2022-08-24 22:09:18 +00:00
|
|
|
if fileExt == "tga" and cm == ColorMode.GRAYSCALE then
|
2022-06-15 13:20:13 +00:00
|
|
|
local pixel
|
|
|
|
if cm == ColorMode.RGB then
|
|
|
|
pixel = c1.rgbaPixel
|
|
|
|
elseif cm == ColorMode.GRAYSCALE then
|
|
|
|
pixel = c1.grayPixel
|
|
|
|
else
|
|
|
|
pixel = 0 -- Do nothing in indexed
|
|
|
|
end
|
|
|
|
for p in testImg:pixels() do
|
|
|
|
if p() == 0 then p(pixel) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
testImg:resize(testImg.width*scale, testImg.height*scale)
|
|
|
|
end
|
|
|
|
|
2023-07-11 16:33:45 +00:00
|
|
|
function compatible_modes(fileExt, cm)
|
|
|
|
return
|
|
|
|
-- TODO support saving any color mode to FLI files on the fly
|
|
|
|
(fileExt ~= "fli" or cm == ColorMode.INDEXED) and
|
|
|
|
-- TODO Review grayscale support in bmp files
|
|
|
|
(fileExt ~= "bmp" or cm ~= ColorMode.GRAYSCALE) and
|
|
|
|
-- TODO Review grayscale/indexed support in webp files
|
|
|
|
(fileExt ~= "webp" or cm == ColorMode.RGB)
|
|
|
|
end
|
|
|
|
|
2022-06-15 13:20:13 +00:00
|
|
|
for _,cm in ipairs{ ColorMode.RGB,
|
|
|
|
ColorMode.GRAYSCALE,
|
|
|
|
ColorMode.INDEXED } do
|
|
|
|
local spr = Sprite(32, 32, cm)
|
2022-06-15 00:51:13 +00:00
|
|
|
spr.palettes[1]:resize(3)
|
|
|
|
spr.palettes[1]:setColor(0, Color(0, 0, 0, 0))
|
|
|
|
spr.palettes[1]:setColor(1, Color(0, 0, 0, 255))
|
|
|
|
spr.palettes[1]:setColor(2, Color(255, 0, 0, 255))
|
|
|
|
|
2022-06-15 13:20:13 +00:00
|
|
|
local c1, c2
|
|
|
|
if cm == ColorMode.RGB then
|
|
|
|
c1 = Color(0, 0, 0)
|
|
|
|
c2 = Color(255, 0, 0)
|
|
|
|
elseif cm == ColorMode.GRAYSCALE then
|
|
|
|
c1 = Color{ gray=0 }
|
|
|
|
c2 = Color{ gray=255 }
|
|
|
|
elseif cm == ColorMode.INDEXED then
|
|
|
|
c1 = 1
|
|
|
|
c2 = 2
|
|
|
|
end
|
|
|
|
|
|
|
|
app.useTool{ color=c1, brush=Brush(1),
|
|
|
|
tool="filled_ellipse", points={{0,0},{31,31}} }
|
|
|
|
app.useTool{ color=c2, brush=Brush(1),
|
|
|
|
tool="filled_ellipse", points={{4,4},{27,27}} }
|
2022-06-14 13:58:14 +00:00
|
|
|
spr.filename = "_test_a.aseprite"
|
2022-06-15 13:20:13 +00:00
|
|
|
|
2022-06-13 22:17:05 +00:00
|
|
|
app.command.SaveFile()
|
2022-06-14 13:58:14 +00:00
|
|
|
assert(spr.filename == "_test_a.aseprite")
|
2022-06-13 22:17:05 +00:00
|
|
|
|
|
|
|
app.command.SaveFileAs{ filename="_test_b.png" }
|
|
|
|
assert(spr.filename == "_test_b.png")
|
|
|
|
|
|
|
|
app.command.SaveFileCopyAs{ filename="_test_c.png" }
|
|
|
|
assert(spr.filename == "_test_b.png")
|
2022-06-14 13:58:14 +00:00
|
|
|
|
2022-06-15 00:51:13 +00:00
|
|
|
-- Scale
|
2023-07-11 16:33:45 +00:00
|
|
|
for _,fn in ipairs{ "_test_c_scaled.bmp",
|
2022-06-15 13:20:13 +00:00
|
|
|
"_test_c_scaled.fli",
|
2023-07-11 16:33:45 +00:00
|
|
|
"_test_c_scaled.gif",
|
|
|
|
"_test_c_scaled.png",
|
2022-06-15 13:20:13 +00:00
|
|
|
"_test_c_scaled.tga",
|
2023-07-11 16:33:45 +00:00
|
|
|
"_test_c_scaled.webp" } do
|
2022-06-15 13:20:13 +00:00
|
|
|
local fileExt = app.fs.fileExtension(fn)
|
|
|
|
|
2023-07-11 16:33:45 +00:00
|
|
|
if compatible_modes(fileExt, cm) then
|
|
|
|
for _,scale in ipairs({ 0.25, 0.5, 1, 2, 3, 4 }) do
|
2022-06-15 13:20:13 +00:00
|
|
|
print(fn, scale, cm)
|
|
|
|
|
2023-07-11 16:33:45 +00:00
|
|
|
app.sprite = spr
|
2022-06-15 13:20:13 +00:00
|
|
|
app.command.SaveFileCopyAs{ filename=fn, scale=scale }
|
|
|
|
local c = app.open(fn)
|
|
|
|
assert(c.width == spr.width*scale)
|
|
|
|
assert(c.height == spr.height*scale)
|
|
|
|
|
|
|
|
local testImg = Image(spr.cels[1].image)
|
2023-07-11 16:33:45 +00:00
|
|
|
fix_images(testImg, scale, fileExt, c, cm, c1)
|
2022-06-15 13:20:13 +00:00
|
|
|
if not c.cels[1].image:isEqual(testImg) then
|
|
|
|
c.cels[1].image:saveAs("_testA.png")
|
|
|
|
testImg:saveAs("_testB.png")
|
|
|
|
end
|
|
|
|
assert(c.cels[1].image.colorMode == testImg.colorMode)
|
|
|
|
assert(c.cels[1].image:isEqual(testImg))
|
|
|
|
end
|
2022-06-15 00:51:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Scale + Slices
|
2022-06-14 13:58:14 +00:00
|
|
|
local slice = spr:newSlice(Rectangle(1, 2, 8, 15))
|
|
|
|
slice.name = "small_slice"
|
2023-07-11 16:33:45 +00:00
|
|
|
for _,fn in ipairs({ "_test_c_small_slice.bmp",
|
|
|
|
"_test_c_small_slice.fli",
|
|
|
|
"_test_c_small_slice.gif",
|
|
|
|
"_test_c_small_slice.png",
|
2022-06-15 13:20:13 +00:00
|
|
|
"_test_c_small_slice.tga",
|
2023-07-11 16:33:45 +00:00
|
|
|
"_test_c_small_slice.webp" }) do
|
2022-06-15 13:20:13 +00:00
|
|
|
local fileExt = app.fs.fileExtension(fn)
|
|
|
|
|
2023-07-11 16:33:45 +00:00
|
|
|
if compatible_modes(fileExt, cm) then
|
|
|
|
for _,scale in ipairs({ 0.25, 0.5, 1, 2, 3, 4 }) do
|
2022-06-15 13:20:13 +00:00
|
|
|
print(fn, scale, cm)
|
|
|
|
|
2023-07-11 16:33:45 +00:00
|
|
|
app.sprite = spr
|
2022-06-15 13:20:13 +00:00
|
|
|
app.command.SaveFileCopyAs{ filename=fn, slice="small_slice", scale=scale }
|
|
|
|
local c = app.open(fn)
|
2024-06-03 14:29:15 +00:00
|
|
|
assert(c.width == math.floor(slice.bounds.width*scale))
|
|
|
|
assert(c.height == math.floor(slice.bounds.height*scale))
|
2022-06-15 13:20:13 +00:00
|
|
|
|
|
|
|
local testImg = Image(spr.cels[1].image, spr.slices[1].bounds)
|
2023-07-11 16:33:45 +00:00
|
|
|
fix_images(testImg, scale, fileExt, c, cm, c1)
|
2022-06-15 13:20:13 +00:00
|
|
|
if not c.cels[1].image:isEqual(testImg) then
|
|
|
|
c.cels[1].image:saveAs("_testA.png")
|
|
|
|
testImg:saveAs("_testB.png")
|
|
|
|
end
|
|
|
|
assert(c.cels[1].image:isEqual(testImg))
|
|
|
|
end
|
2022-06-15 00:51:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-13 22:17:05 +00:00
|
|
|
end
|