Skip to content

Commit

Permalink
Slice chunk + bits operations fix
Browse files Browse the repository at this point in the history
  • Loading branch information
miriti committed Apr 23, 2020
1 parent bcf88bd commit df0c6f1
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 23 deletions.
15 changes: 14 additions & 1 deletion src/ase/Frame.hx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import ase.chunks.ChunkHeader;
class Frame {
public var header:FrameHeader;
public var chunks:Array<Chunk> = [];
public var chunkTypes:Map<Int, Array<Chunk>> = [];

public function new(header:FrameHeader, frameData:Bytes) {
var bytesInput:BytesInput = new BytesInput(frameData);
Expand All @@ -23,7 +24,8 @@ class Frame {
var chunkHeader:ChunkHeader = new ChunkHeader(bytesInput.read(ChunkHeader.BYTE_SIZE));

try {
var chunkBytes:Bytes = bytesInput.read(chunkHeader.size - ChunkHeader.BYTE_SIZE);
var chunkBytes:Bytes = bytesInput.read(chunkHeader.size
- ChunkHeader.BYTE_SIZE);
var chunk:Chunk = Chunk.factory(chunkHeader, chunkBytes);

if (lastChunk != null) {
Expand All @@ -37,6 +39,17 @@ class Frame {
}

chunks.push(chunk);
if (!chunkTypes.exists(chunk.header.type))
chunkTypes[
chunk.header.type
] = [
chunk
];
else
chunkTypes[
chunk.header.type
].push(chunk);

lastChunk = chunk;
} catch (error:String) {
trace('Error: ${error}');
Expand Down
23 changes: 12 additions & 11 deletions src/ase/chunks/ChunkType.hx
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,31 @@ class ChunkType {
public static inline var SLICE:Int = 0x2022;

public static var NAMES:Map<Int, String> = [
OLD_PALETTE_04 => "OLD_PALETTE_04",
OLD_PALETTE_11 => "OLD_PALETTE_11",
LAYER => "LAYER",
CEL => "CEL",
CEL_EXTRA => "CEL_EXTRA",
COLOR_PROFILE => "COLOR_PROFILE",
LAYER => "LAYER",
MASK => "MASK",
PATH => "PATH",
TAGS => "TAGS",
OLD_PALETTE_04 => "OLD_PALETTE_04",
OLD_PALETTE_11 => "OLD_PALETTE_11",
PALETTE => "PALETTE",
USER_DATA => "USER_DATA",
PATH => "PATH",
SLICE => "SLICE",
TAGS => "TAGS",
USER_DATA => "USER_DATA"
];

public static var CLASSES:Map<Int, Class<Chunk>> = [
OLD_PALETTE_04 => OldPaleteChunk,
OLD_PALETTE_11 => OldPaleteChunk,
LAYER => LayerChunk,
CEL => CelChunk,
CEL_EXTRA => CelExtraChunk,
COLOR_PROFILE => ColorProfileChunk,
LAYER => LayerChunk,
MASK => MaskChunk,
OLD_PALETTE_04 => OldPaleteChunk,
OLD_PALETTE_11 => OldPaleteChunk,
PALETTE => PaletteChunk,
SLICE => SliceChunk,
TAGS => TagsChunk,
USER_DATA => UserDataChunk,
MASK => MaskChunk,
TAGS => TagsChunk
];
}
1 change: 0 additions & 1 deletion src/ase/chunks/ColorProfileChunk.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ase.chunks;

import haxe.Int32;
import haxe.io.Bytes;

class ColorProfileChunk extends Chunk {
Expand Down
2 changes: 1 addition & 1 deletion src/ase/chunks/PaletteChunk.hx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PaletteEntry {
blue = bytesInput.readByte();
alpha = bytesInput.readByte();

if (flags == 1) {
if (flags & (1 << 0) != 0) {
name = bytesInput.readString(bytesInput.readUInt16());
}
}
Expand Down
62 changes: 62 additions & 0 deletions src/ase/chunks/SliceChunk.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ase.chunks;

import haxe.Int32;
import haxe.io.Bytes;

class SliceKey {
public var frameNumber:Int;
public var xOrigin:Int32;
public var yOrigin:Int32;
public var width:Int32;
public var height:Int32;
public var xCenter:Int32;
public var yCenter:Int32;
public var centerWidth:Int32;
public var centerHeight:Int32;
public var xPivot:Int32;
public var yPivot:Int32;

public function new() {}
}

class SliceChunk extends Chunk {
public var numSliceKeys:Int32;
public var flags:Int32;

public var reserved:Int32;
public var name:String;

public var sliceKeys:Array<SliceKey> = [];

public function new(header:ChunkHeader, chunkData:Bytes) {
super(header, chunkData);

numSliceKeys = bytesInput.readInt32();
flags = bytesInput.readInt32();
reserved = bytesInput.readInt32();
name = bytesInput.readString(bytesInput.readUInt16());

for (n in 0...numSliceKeys) {
var sliceKey:SliceKey = new SliceKey();
sliceKey.frameNumber = bytesInput.readInt32();
sliceKey.xOrigin = bytesInput.readInt32();
sliceKey.yOrigin = bytesInput.readInt32();
sliceKey.width = bytesInput.readInt32();
sliceKey.height = bytesInput.readInt32();

if (flags & (1 << 0) != 0) {
sliceKey.xCenter = bytesInput.readInt32();
sliceKey.yCenter = bytesInput.readInt32();
sliceKey.centerWidth = bytesInput.readInt32();
sliceKey.centerHeight = bytesInput.readInt32();
}

if (flags & (1 << 1) != 0) {
sliceKey.xPivot = bytesInput.readInt32();
sliceKey.yPivot = bytesInput.readInt32();
}

sliceKeys.push(sliceKey);
}
}
}
8 changes: 3 additions & 5 deletions src/ase/chunks/UserDataChunk.hx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ class UserDataChunk extends Chunk {

flags = bytesInput.readInt32();

hasText = flags == 1 || flags == 3;
hasColor = flags == 2 || flags == 3;
hasText = flags & (1 << 0) != 0;
hasColor = flags & (1 << 1) != 0;

if (hasText) {
text = bytesInput.readString((hasColor ? bytesInput.length
- 4 : bytesInput.length)
- 4);
text = bytesInput.readString(bytesInput.readUInt16());
}

if (hasColor) {
Expand Down
6 changes: 4 additions & 2 deletions test.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
-main TestMain
-lib munit
-lib hamcrest
-cp src
-cp test
-cp src
--resource test_files/128x128_rgba.aseprite@128x128_rgba
--resource test_files/slices.aseprite@slices.aseprite
-neko bin/neko_test.n

## JavaScript NodeJS
Expand All @@ -14,7 +15,8 @@
-lib munit
-lib hamcrest
-lib hxnodejs
-cp src
-cp test
-cp src
--resource test_files/128x128_rgba.aseprite@128x128_rgba
--resource test_files/slices.aseprite@slices.aseprite
-js bin/js_test.js
24 changes: 22 additions & 2 deletions test/AsepriteTest.hx
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
import ase.AseHeader;
import haxe.io.Bytes;
import haxe.Resource;
import ase.Aseprite;
import ase.chunks.ChunkType;
import haxe.Resource;
import haxe.io.Bytes;
import massive.munit.Assert;

class AsepriteTest {
@Test
public function case128x128_rgba() {
// TODO: More/better tests
var aseprite = Aseprite.fromBytes(Resource.getBytes('128x128_rgba'));
Assert.areEqual(AseHeader.ASEPRITE_MAGIC, aseprite.header.magic);
Assert.areEqual(128, aseprite.header.width);
Assert.areEqual(128, aseprite.header.height);
Assert.areEqual(1, aseprite.header.frames);
Assert.areEqual(32, aseprite.header.colorDepth);
}

@Test
public function caseSlices() {
// TODO: More/better tests
var aseprite = Aseprite.fromBytes(Resource.getBytes('slices.aseprite'));
Assert.areEqual(aseprite.frames[
0
].chunkTypes[
ChunkType.SLICE
].length, 4);
Assert.areEqual(aseprite.frames[
0
].chunkTypes[
ChunkType.SLICE
][
0
].userData.text, 'User Data + Blue color');
}
}
Binary file added test_files/slices.aseprite
Binary file not shown.

0 comments on commit df0c6f1

Please sign in to comment.