Changeset View
Changeset View
Standalone View
Standalone View
extern/draco/dracoenc/src/draco/io/ply_reader.cc
| Context not available. | |||||
| #include <array> | #include <array> | ||||
| #include <regex> | #include <regex> | ||||
| #include "draco/core/status.h" | |||||
| #include "draco/io/parser_utils.h" | #include "draco/io/parser_utils.h" | ||||
| #include "draco/io/ply_property_writer.h" | #include "draco/io/ply_property_writer.h" | ||||
| Context not available. | |||||
| PlyReader::PlyReader() : format_(kLittleEndian) {} | PlyReader::PlyReader() : format_(kLittleEndian) {} | ||||
| bool PlyReader::Read(DecoderBuffer *buffer) { | Status PlyReader::Read(DecoderBuffer *buffer) { | ||||
| error_message_.clear(); | |||||
| std::string value; | std::string value; | ||||
| // The first line needs to by "ply". | // The first line needs to by "ply". | ||||
| if (!parser::ParseString(buffer, &value) || value != "ply") { | if (!parser::ParseString(buffer, &value) || value != "ply") { | ||||
| error_message_ = "Not a valid ply file."; | return Status(Status::INVALID_PARAMETER, "Not a valid ply file"); | ||||
| return false; | |||||
| } | } | ||||
| parser::SkipLine(buffer); | parser::SkipLine(buffer); | ||||
| Context not available. | |||||
| format = words[1]; | format = words[1]; | ||||
| version = words[2]; | version = words[2]; | ||||
| } else { | } else { | ||||
| error_message_ = "Missing or wrong format line."; | return Status(Status::INVALID_PARAMETER, "Missing or wrong format line"); | ||||
| return false; | |||||
| } | } | ||||
| if (version != "1.0") { | if (version != "1.0") { | ||||
| error_message_ = "Unsupported PLY version."; | return Status(Status::UNSUPPORTED_VERSION, "Unsupported PLY version"); | ||||
| return false; // Wrong version. | |||||
| } | } | ||||
| if (format == "binary_big_endian") { | if (format == "binary_big_endian") { | ||||
| error_message_ = | return Status(Status::UNSUPPORTED_VERSION, | ||||
| "Unsupported format. Currently we support only ascii and" | "Unsupported format. Currently we support only ascii and" | ||||
| " binary_little_endian format."; | " binary_little_endian format."); | ||||
| return false; | |||||
| } | } | ||||
| if (format == "ascii") { | if (format == "ascii") { | ||||
| format_ = kAscii; | format_ = kAscii; | ||||
| } else { | } else { | ||||
| format_ = kLittleEndian; | format_ = kLittleEndian; | ||||
| } | } | ||||
| if (!ParseHeader(buffer)) | DRACO_RETURN_IF_ERROR(ParseHeader(buffer)); | ||||
| return false; | if (!ParsePropertiesData(buffer)) { | ||||
| if (!ParsePropertiesData(buffer)) | return Status(Status::INVALID_PARAMETER, "Couldn't parse properties"); | ||||
| return false; | } | ||||
| return true; | return OkStatus(); | ||||
| } | } | ||||
| bool PlyReader::ParseHeader(DecoderBuffer *buffer) { | Status PlyReader::ParseHeader(DecoderBuffer *buffer) { | ||||
| while (error_message_.length() == 0 && !ParseEndHeader(buffer)) { | while (true) { | ||||
| DRACO_ASSIGN_OR_RETURN(bool end, ParseEndHeader(buffer)); | |||||
| if (end) | |||||
| break; | |||||
| if (ParseElement(buffer)) | if (ParseElement(buffer)) | ||||
| continue; | continue; | ||||
| if (ParseProperty(buffer)) | DRACO_ASSIGN_OR_RETURN(bool property_parsed, ParseProperty(buffer)); | ||||
| if (property_parsed) | |||||
| continue; | continue; | ||||
| parser::SkipLine(buffer); | parser::SkipLine(buffer); | ||||
| } | } | ||||
| if (error_message_.length() > 0) { | return OkStatus(); | ||||
| printf("ERROR %s\n", error_message_.c_str()); | |||||
| return false; | |||||
| } | |||||
| return true; | |||||
| } | } | ||||
| bool PlyReader::ParseEndHeader(DecoderBuffer *buffer) { | StatusOr<bool> PlyReader::ParseEndHeader(DecoderBuffer *buffer) { | ||||
| parser::SkipWhitespace(buffer); | parser::SkipWhitespace(buffer); | ||||
| std::array<char, 10> c; | std::array<char, 10> c; | ||||
| if (!buffer->Peek(&c)) { | if (!buffer->Peek(&c)) { | ||||
| error_message_ = "End of file reached before the end_header."; | return Status(Status::INVALID_PARAMETER, | ||||
| return false; | "End of file reached before the end_header"); | ||||
| } | } | ||||
| if (std::memcmp(&c[0], "end_header", 10) != 0) | if (std::memcmp(&c[0], "end_header", 10) != 0) | ||||
| return false; | return false; | ||||
| Context not available. | |||||
| return true; | return true; | ||||
| } | } | ||||
| bool PlyReader::ParseProperty(DecoderBuffer *buffer) { | StatusOr<bool> PlyReader::ParseProperty(DecoderBuffer *buffer) { | ||||
| if (elements_.empty()) | if (elements_.empty()) | ||||
| return false; // Ignore properties if there is no active element. | return false; // Ignore properties if there is no active element. | ||||
| DecoderBuffer line_buffer(*buffer); | DecoderBuffer line_buffer(*buffer); | ||||
| Context not available. | |||||
| } | } | ||||
| const DataType data_type = GetDataTypeFromString(data_type_str); | const DataType data_type = GetDataTypeFromString(data_type_str); | ||||
| if (data_type == DT_INVALID) { | if (data_type == DT_INVALID) { | ||||
| error_message_ = "Wrong property data type."; | return Status(Status::INVALID_PARAMETER, "Wrong property data type"); | ||||
| return true; // Parsed. | |||||
| } | } | ||||
| DataType list_type = DT_INVALID; | DataType list_type = DT_INVALID; | ||||
| if (property_list_search) { | if (property_list_search) { | ||||
| list_type = GetDataTypeFromString(list_type_str); | list_type = GetDataTypeFromString(list_type_str); | ||||
| if (list_type == DT_INVALID) { | if (list_type == DT_INVALID) { | ||||
| error_message_ = "Wrong property list type."; | return Status(Status::INVALID_PARAMETER, "Wrong property list type"); | ||||
| return true; // Parsed. | |||||
| } | } | ||||
| } | } | ||||
| elements_.back().AddProperty( | elements_.back().AddProperty( | ||||
| Context not available. | |||||