44# --------------------------------------------------
55# Header IO
66
7- @enum Format Format_ascii Format_binary_little Format_binary_big
8-
97function 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)
142140end
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
310309end
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"""
322328function 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)
346346end
347347
348348function load_ply (file_name:: AbstractString )
@@ -359,10 +359,14 @@ Save data from `Ply` data structure into `file` which may be a filename or an
359359open stream. The file will be native endian binary, unless the keyword
360360argument `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
0 commit comments