Skip to content

Commit 333c428

Browse files
authored
Store and preserve the ply format in the Ply header (#22)
1 parent 8c328d6 commit 333c428

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

src/io.jl

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#--------------------------------------------------
55
# Header IO
66

7-
@enum Format Format_ascii Format_binary_little Format_binary_big
8-
97
function ply_type(type_name)
108
if type_name == "char" || type_name == "int8"; return Int8
119
elseif type_name == "short" || type_name == "int16"; return Int16
@@ -141,9 +139,10 @@ function write_header_field(stream::IO, comment::PlyComment)
141139
println(stream, prefix, comment.comment)
142140
end
143141

144-
function write_header(ply, stream::IO, ascii)
142+
function write_header(ply, stream::IO, format=ply.format)
145143
println(stream, "ply")
146-
if ascii
144+
_check_native_endian(format)
145+
if format == Format_ascii
147146
println(stream, "format ascii 1.0")
148147
else
149148
endianness = _host_is_little_endian ? "little" : "big"
@@ -309,6 +308,13 @@ function write_binary_values(stream::IO, elen, props::ArrayProperty{T}...) where
309308
end
310309
end
311310

311+
function _check_native_endian(format)
312+
if _host_is_little_endian && format == Format_binary_big
313+
error("Reading big endian ply on little endian host is not implemented")
314+
elseif !_host_is_little_endian && format == Format_binary_little
315+
error("Reading little endian ply on big endian host is not implemented")
316+
end
317+
end
312318

313319
#-------------------------------------------------------------------------------
314320
# High level IO for complete files
@@ -321,13 +327,7 @@ be a file name or an open stream.
321327
"""
322328
function load_ply(io::IO)
323329
elements, format, comments = read_header(io)
324-
if format != Format_ascii
325-
if _host_is_little_endian && format != Format_binary_little
326-
error("Reading big endian ply on little endian host is not implemented")
327-
elseif !_host_is_little_endian && format != Format_binary_big
328-
error("Reading little endian ply on big endian host is not implemented")
329-
end
330-
end
330+
_check_native_endian(format)
331331
for element in elements
332332
for prop in element.properties
333333
resize!(prop, length(element))
@@ -338,11 +338,11 @@ function load_ply(io::IO)
338338
read_ascii_value!(io, prop, i)
339339
end
340340
end
341-
else # format == Format_binary_little
341+
else
342342
read_binary_values!(io, length(element), element.properties...)
343343
end
344344
end
345-
Ply(elements, comments)
345+
Ply(format, elements, comments)
346346
end
347347

348348
function load_ply(file_name::AbstractString)
@@ -359,10 +359,14 @@ Save data from `Ply` data structure into `file` which may be a filename or an
359359
open stream. The file will be native endian binary, unless the keyword
360360
argument `ascii` is set to `true`.
361361
"""
362-
function save_ply(ply, stream::IO; ascii::Bool=false)
363-
write_header(ply, stream, ascii)
362+
function save_ply(ply, stream::IO; ascii=nothing)
363+
format = ascii === nothing ? ply.format :
364+
ascii ? Format_ascii :
365+
_host_is_little_endian ? Format_binary_little :
366+
Format_binary_big
367+
write_header(ply, stream, format)
364368
for element in ply
365-
if ascii
369+
if format == Format_ascii
366370
for i=1:length(element)
367371
for (j,property) in enumerate(element.properties)
368372
if j != 1

src/types.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ end
187187

188188

189189
#--------------------------------------------------
190+
@enum Format Format_ascii Format_binary_little Format_binary_big
191+
190192
"""
191193
Ply()
192194
@@ -196,15 +198,19 @@ contents of the header. Ply elements and comments can be added using
196198
interface, and looked up by indexing with a string.
197199
"""
198200
mutable struct Ply
201+
format::Format
199202
elements::Vector{PlyElement}
200203
comments::Vector{PlyComment}
201204
end
202205

203-
Ply() = Ply(Vector{PlyElement}(), Vector{String}())
206+
Ply(format=Format_ascii) = Ply(format, Vector{PlyElement}(), Vector{String}())
207+
208+
# For compat
209+
Ply(elements::Vector{PlyElement}, comments::Vector{PlyComment}) = Ply(Format_ascii, elements, comments)
204210

205211
function Base.show(io::IO, ply::Ply)
206212
buf = IOBuffer()
207-
write_header(ply, buf, true)
213+
write_header(ply, buf)
208214
headerstr = String(take!(buf))
209215
headerstr = replace(strip(headerstr), "\n"=>"\n ")
210216
print(io, "$Ply with header:\n $headerstr")

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ end
130130
@test newply.comments == [PlyComment("A comment",false,1),
131131
PlyComment("Blah blah",false,1),
132132
PlyComment("x=10",true,1)]
133+
@test (newply.format == PlyIO.Format_ascii) == test_ascii
134+
135+
save_ply(newply, "roundtrip_test_tmp_2.ply")
136+
newply_2 = load_ply("roundtrip_test_tmp_2.ply")
137+
@test (newply_2.format == PlyIO.Format_ascii) == test_ascii
133138
end
134139

135140
@testset "proptype=$proptype" for proptype in [Int8, Int16, Int32, Int64,

0 commit comments

Comments
 (0)