From 153c24677885ac02ab931faadba10dae3572f0c6 Mon Sep 17 00:00:00 2001 From: miRoox Date: Sun, 16 Oct 2022 10:49:08 +0800 Subject: [PATCH] fix Bitmap example Julia arrays are column major --- README.md | 12 ++++++------ docs/src/index.md | 12 ++++++------ src/macro.jl | 12 ++++++------ test/runtests.jl | 22 +++++++++++----------- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 6aeac96..874e7f7 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/docs/src/index.md b/docs/src/index.md index 5626bbb..6b0223b 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -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 diff --git a/src/macro.jl b/src/macro.jl index 3ad5844..0a08840 100644 --- a/src/macro.jl +++ b/src/macro.jl @@ -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 @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index 10ccecf..4cf7a2c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 @@ -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] @@ -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 ) @@ -465,7 +465,7 @@ 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) @@ -473,14 +473,14 @@ end @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