Skip to content

Commit

Permalink
fix Bitmap example
Browse files Browse the repository at this point in the history
Julia arrays are column major
  • Loading branch information
miRoox committed Oct 16, 2022
1 parent 2a4cc22 commit 153c246
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ and pixel which is a 2-dimensional byte array with the specified width and heigh
::Const(b"BMP")
width::UInt16le
height::UInt16le
pixel::SizedArray(UInt8, this.width, this.height)
pixel::SizedArray(UInt8, this.height, this.width) # Julia arrays are column major
end
```

```julia
julia> deserialize(Bitmap, b"BMP\x03\x00\x02\x00\x01\x02\x03\x04\x05\x06")
Bitmap(0x0003, 0x0002, UInt8[0x01 0x04; 0x02 0x05; 0x03 0x06])
julia> deserialize(Bitmap, b"BMP\x02\x00\x03\x00\x01\x02\x03\x04\x05\x06")
Bitmap(0x0002, 0x0003, UInt8[0x01 0x04; 0x02 0x05; 0x03 0x06])
```

```julia
julia> serialize(Bitmap(2, 3, UInt8[1 2 3; 7 8 9]))
julia> serialize(Bitmap(3, 2, UInt8[1 2 3; 7 8 9]))
13-element Vector{UInt8}:
0x42
0x4d
0x50
0x02
0x00
0x03
0x00
0x02
0x00
0x01
0x07
0x02
Expand Down
12 changes: 6 additions & 6 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ julia> @construct struct Bitmap
::Const(b"BMP")
width::UInt16le
height::UInt16le
pixel::SizedArray(UInt8, this.width, this.height)
pixel::SizedArray(UInt8, this.height, this.width) # Julia arrays are column major
end
julia> deserialize(Bitmap, b"BMP\x03\x00\x02\x00\x01\x02\x03\x04\x05\x06")
Bitmap(0x0003, 0x0002, UInt8[0x01 0x04; 0x02 0x05; 0x03 0x06])
julia> deserialize(Bitmap, b"BMP\x02\x00\x03\x00\x01\x02\x03\x04\x05\x06")
Bitmap(0x0002, 0x0003, UInt8[0x01 0x04; 0x02 0x05; 0x03 0x06])
julia> serialize(Bitmap(2, 3, UInt8[1 2 3; 7 8 9]))
julia> serialize(Bitmap(3, 2, UInt8[1 2 3; 7 8 9]))
13-element Vector{UInt8}:
0x42
0x4d
0x50
0x02
0x00
0x03
0x00
0x02
0x00
0x01
0x07
0x02
Expand Down
12 changes: 6 additions & 6 deletions src/macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ julia> @construct struct Bitmap
::Const(b"BMP")
width::UInt16le
height::UInt16le
pixel::SizedArray(UInt8, this.width, this.height)
pixel::SizedArray(UInt8, this.height, this.width)
end
julia> deserialize(Bitmap, b"BMP\\x03\\x00\\x02\\x00\\x01\\x02\\x03\\x04\\x05\\x06")
Bitmap(0x0003, 0x0002, UInt8[0x01 0x04; 0x02 0x05; 0x03 0x06])
Bitmap(0x0003, 0x0002, UInt8[0x01 0x03 0x05; 0x02 0x04 0x06])
julia> serialize(Bitmap(2, 3, UInt8[1 2 3; 7 8 9]))
julia> serialize(Bitmap(2, 3, UInt8[1 2; 4 6; 8 9]))
13-element Vector{UInt8}:
0x42
0x4d
Expand All @@ -40,10 +40,10 @@ julia> serialize(Bitmap(2, 3, UInt8[1 2 3; 7 8 9]))
0x03
0x00
0x01
0x07
0x02
0x04
0x08
0x03
0x02
0x06
0x09
julia> estimatesize(Bitmap)
Expand Down
22 changes: 11 additions & 11 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ end
width::UInt32
height::UInt32
::Padded(8)
pixel::SizedArray(UInt8, this.width, this.height)
pixel::SizedArray(UInt8, this.height, this.width)
end
end
withname = quote
Expand All @@ -388,7 +388,7 @@ end
width::UInt32
height::UInt32
::Padded(8)
pixel::SizedArray(UInt8, this.width, this.height)
pixel::SizedArray(UInt8, this.height, this.width)
end
end
@testset "expand pass" for ex in [structonly, withname]
Expand Down Expand Up @@ -437,7 +437,7 @@ end
width::UInt32
height::UInt32
::Padded(8)
pixel::SizedArray(UInt8, (this.width, this.height))
pixel::SizedArray(UInt8, (this.height, this.width))
end
end
)
Expand Down Expand Up @@ -465,22 +465,22 @@ end
::Padded(1)
width::UInt16le
height::UInt16le
pixel::SizedArray(UInt8, this.width, this.height)
pixel::SizedArray(UInt8, this.height, this.width)
end
@test Bitmap1 <: AbstractImage
@test fieldnames(Bitmap1) == (:signature, :width, :height, :pixel)
@test fieldtype(Bitmap1, :signature) == Vector{UInt8}
@test fieldtype(Bitmap1, :width) == UInt16
@test fieldtype(Bitmap1, :pixel) == Matrix{UInt8}
@test estimatesize(Bitmap1) == UnboundedSize(sizeof(b"BMP") + 1 + 2 * sizeof(UInt16))
@test serialize(Bitmap1(b"BMP", 2, 3, UInt8[1 2 3; 7 8 9])) == b"BMP\x00\x02\x00\x03\x00\x01\x07\x02\x08\x03\x09"
@test let res = deserialize(Bitmap1, b"BMP\xfe\x02\x00\x03\x00\x01\x07\x02\x08\x03\x09")
res.signature == b"BMP" && res.width == 2 && res.height == 3 && res.pixel == UInt8[1 2 3; 7 8 9]
@test serialize(Bitmap1(b"BMP", 3, 2, UInt8[1 2 3; 7 8 9])) == b"BMP\x00\x03\x00\x02\x00\x01\x07\x02\x08\x03\x09"
@test let res = deserialize(Bitmap1, b"BMP\xfe\x03\x00\x02\x00\x01\x07\x02\x08\x03\x09")
res.signature == b"BMP" && res.width == 3 && res.height == 2 && res.pixel == UInt8[1 2 3; 7 8 9]
end
@test_throws DimensionMismatch serialize(Bitmap1(b"BMP", 2, 3, UInt8[1 2; 7 8]))
@test_throws ValidationError serialize(Bitmap1(b"PMB", 2, 3, UInt8[1 2 3; 7 8 9]))
@test_throws EOFError deserialize(Bitmap1, b"BMP\xfe\x02\x00\x03\x00\x01\x07\x02\x08\x03")
@test_throws ValidationError deserialize(Bitmap1, b"PMB\xfe\x02\x00\x03\x00\x01\x07\x02\x08\x03\x09")
@test_throws DimensionMismatch serialize(Bitmap1(b"BMP", 3, 2, UInt8[1 2; 7 8]))
@test_throws ValidationError serialize(Bitmap1(b"PMB", 3, 2, UInt8[1 2 3; 7 8 9]))
@test_throws EOFError deserialize(Bitmap1, b"BMP\xfe\x03\x00\x02\x00\x01\x07\x02\x08\x03")
@test_throws ValidationError deserialize(Bitmap1, b"PMB\xfe\x03\x00\x02\x00\x01\x07\x02\x08\x03\x09")
end
end
end

0 comments on commit 153c246

Please sign in to comment.