void message::get(uint8_t& unsigned_integer, size_t const& part) const { assert(sizeof(uint8_t) == size(part)); uint8_t const* byte = static_cast<uint8_t const*>(raw_data(part)); unsigned_integer = *byte; }
void message::get(int64_t& integer, size_t const& part) const { assert(sizeof(int64_t) == size(part)); uint64_t const* network_order = static_cast<uint64_t const*>(raw_data(part)); integer = static_cast<int64_t>(htonll(*network_order)); }
void message::get(uint64_t& unsigned_integer, size_t const& part) const { assert(sizeof(uint64_t) == size(part)); uint64_t const* network_order = static_cast<uint64_t const*>(raw_data(part)); unsigned_integer = ntohll(*network_order); }
void message::get(int8_t& integer, size_t const& part) const { assert(sizeof(int8_t) == size(part)); int8_t const* byte = static_cast<int8_t const*>(raw_data(part)); integer = *byte; }
void message::get(bool& boolean, size_t const& part) const { assert(sizeof(uint8_t) == size(part)); uint8_t const* byte = static_cast<uint8_t const*>(raw_data(part)); boolean = (*byte != 0); }
String FileUtilities::ReadCompleteTextFile(const String &sFilename) { File oFile; oFile.Open(sFilename, File::OTReadOnly); // Read file shared_ptr<ByteBuffer> pBuffer = oFile.ReadFile(); if (!pBuffer || pBuffer->GetSize() == 0) { // Could not read from this file. return ""; } FileEncoding file_encoding = ANSI; bool is_utf8 = false; // check if utf8 bom exists const unsigned char *unsigned_char_buffer = (const unsigned char*) pBuffer->GetCharBuffer(); if (pBuffer->GetSize() >= 3 && *unsigned_char_buffer == 0xef && *(unsigned_char_buffer + 1) == 0xbb && *(unsigned_char_buffer + 2) == 0xbf) file_encoding = UTF8; else if (pBuffer->GetSize() >= 2 && *unsigned_char_buffer == 0xff && *(unsigned_char_buffer + 1) == 0xfe) file_encoding = UTF16; switch (file_encoding) { case ANSI: { AnsiString sRetVal((const char*) unsigned_char_buffer, pBuffer->GetSize()); return sRetVal; } case UTF8: { AnsiString raw_data((const char*) unsigned_char_buffer, pBuffer->GetSize()); String utf8_data; Unicode::MultiByteToWide(raw_data, utf8_data); return utf8_data; } case UTF16: { int iChars = pBuffer->GetSize() / sizeof(TCHAR); String sRetVal((const wchar_t*) pBuffer->GetCharBuffer() +1, iChars -1); return sRetVal; } default: throw new std::logic_error(Formatter::FormatAsAnsi("Unsupported encoding type: {0}", file_encoding)); } }
void message::get(float& floating_point, size_t const& part) const { assert(sizeof(uint32_t) == size(part)); uint32_t const* network_order = static_cast<uint32_t const*>(raw_data(part)); uint32_t host_order = ntohl(*network_order); float* temp = reinterpret_cast<float*>(&host_order); floating_point = *temp; }
void message::get(double& double_precision, size_t const& part) const { assert(sizeof(uint64_t) == size(part)); uint64_t const* network_order = static_cast<uint64_t const*>(raw_data(part)); uint64_t host_order = ntohll(*network_order); double* temp = reinterpret_cast<double*>(&host_order); double_precision = *temp; }
int test_file(const std::string & test_data_filepath, const std::string & label_prefix) { int fails = 0; std::vector<pstrudel::TreeShape> trees; pstrudel::treeio::read_from_filepath(trees, test_data_filepath, "nexus"); unsigned long tree_idx = 0; for (auto & tree : trees) { std::string label = label_prefix + ":" + std::to_string(tree_idx + 1); pstrudel::LineageThroughTimeProfileCalculator ltt_calc(tree); auto profiles = ltt_calc.build_lineage_splitting_time_profile(); auto lst_profile = profiles.first; auto scaled_lst_profile = profiles.second; std::ostringstream o; TREE_WRITER.write(o, tree); o << "\n"; assert(lst_profile.data_size() == scaled_lst_profile.data_size()); auto rds = lst_profile.data_size(); for (unsigned long idx = 0; idx < rds; ++idx) { o << std::fixed << std::setprecision(22) << lst_profile.raw_data(idx); o << "\t" << std::fixed << std::setprecision(22) << scaled_lst_profile.raw_data(idx); o << "\n"; } colugo::Subprocess ps({"python", CHECK_SCRIPT, "-f", "newick", "-l", label}); try { auto result = ps.communicate(o.str()); if (ps.returncode() != 0) { std::cerr << "(test '" << label << "' returned error: " << ps.returncode() << ")\n"; // TREE_WRITER.write(std::cerr, tree); // std::cerr << std::endl; std::cerr << result.first; std::cerr << result.second; fails += 1; // } else { // std::cerr << result.second; } } catch (const colugo::SubprocessTimeOutException & e) { std::cerr << "(test '" << label << "' timed out)\n"; exit(1); } ++tree_idx; } return fails; }
bool neam::cr::storage::_write_to_file(const std::string &name, char *memory, size_t size) { if (!mapped_file) mapped_file = new std::map<std::string, raw_data>; mapped_file->erase(name); mapped_file->emplace(name, (raw_data(size, reinterpret_cast<int8_t *>(memory), neam::force_duplicate))); _sync(); return true; }
/** * \brief Decodes a chunk of IT data into PCM data for the current music. * \param destination_buffer the destination buffer to write * \param nb_samples number of samples to write */ void Music::decode_it(ALuint destination_buffer, ALsizei nb_samples) { // decode the IT data std::vector<ALushort> raw_data(nb_samples); it_decoder->decode(&raw_data[0], nb_samples); // put this decoded data into the buffer alBufferData(destination_buffer, AL_FORMAT_STEREO16, &raw_data[0], nb_samples, 44100); int error = alGetError(); Debug::check_assertion(error == AL_NO_ERROR, StringConcat() << "Failed to fill the audio buffer with decoded IT data for music file '" << file_name << ": error " << error); }
void DebuggerIPCServer::handleGetRawPacket(int id) { DebuggerPacket *pk = data_manager->getPacket(id); if (pk) { auto dbg_info = pk->getDebugInfo(); if (dbg_info) { auto raw_data = dbg_info->raw_data(); RawPacketValueMessage message(raw_data); send(&message); } } sendRequestFailed(); }
/** * \brief Decodes a chunk of OGG data into PCM data for the current music. * \param destination_buffer The destination buffer to write. * \param nb_samples Number of samples to write. */ void Music::decode_ogg(ALuint destination_buffer, ALsizei nb_samples) { // read the encoded music properties vorbis_info* info = ov_info(&ogg_file, -1); ALsizei sample_rate = ALsizei(info->rate); ALenum al_format = AL_NONE; if (info->channels == 1) { al_format = AL_FORMAT_MONO16; } else if (info->channels == 2) { al_format = AL_FORMAT_STEREO16; } // decode the OGG data std::vector<ALshort> raw_data(nb_samples * info->channels); int bitstream; long bytes_read; long total_bytes_read = 0; long remaining_bytes = nb_samples * info->channels * sizeof(ALshort); do { bytes_read = ov_read(&ogg_file, ((char*) raw_data.data()) + total_bytes_read, int(remaining_bytes), 0, 2, 1, &bitstream); if (bytes_read < 0) { if (bytes_read != OV_HOLE) { // OV_HOLE is normal when the music loops std::ostringstream oss; oss << "Error while decoding ogg chunk: " << bytes_read; Debug::error(oss.str()); return; } } else { total_bytes_read += bytes_read; remaining_bytes -= bytes_read; } } while (remaining_bytes > 0 && bytes_read > 0); // Put this decoded data into the buffer. alBufferData(destination_buffer, al_format, raw_data.data(), ALsizei(total_bytes_read), sample_rate); int error = alGetError(); if (error != AL_NO_ERROR) { std::ostringstream oss; oss << "Failed to fill the audio buffer with decoded OGG data for music file '" << file_name << "': error " << error; Debug::error(oss.str()); } }
std::unique_ptr<Image> loadBitmapImage(const char* filepath) { FILE* fp = fopen(filepath, "r"); if (!fp) { printf("error loading file %s at loadBitmapImage()\n", filepath); return std::unique_ptr<Image>(); } unsigned char info[54]; if (fread(info, sizeof(unsigned char), 54, fp) < 54) { printf("error reading file %s at loadBitmapImage()\n", filepath); fclose(fp); return std::unique_ptr<Image>(); } const uint32_t img_width = *(uint32_t*)(&info[18]); const uint32_t img_height = *(uint32_t*)(&info[22]); auto img_ptr = std::make_unique<Image>(img_width, img_height, 3); size_t row_padded = (img_width*3 + 3)&(~3); std::vector<unsigned char> raw_data(row_padded, 0); for (size_t h = 0; h < img_height; h++) { if (fread(raw_data.data(), sizeof(unsigned char), row_padded, fp) < row_padded) { printf("error reading file %s at loadBitmapImage()\n", filepath); fclose(fp); return std::unique_ptr<Image>(); } for (size_t w = 0; w < img_width; w++) { (img_ptr->getValues(0))[(img_height - 1 - h) * img_width + w] = raw_data[3*w + 2] / 255.0; (img_ptr->getValues(1))[(img_height - 1 - h) * img_width + w] = raw_data[3*w + 1] / 255.0; (img_ptr->getValues(2))[(img_height - 1 - h) * img_width + w] = raw_data[3*w] / 255.0; } } fclose(fp); return img_ptr; }
/** * \brief Decodes a chunk of SPC data into PCM data for the current music. * \param destination_buffer the destination buffer to write * \param nb_samples number of samples to write */ void Music::decode_spc(ALuint destination_buffer, ALsizei nb_samples) { // decode the SPC data std::vector<ALushort> raw_data(nb_samples); spc_decoder->decode((int16_t*) raw_data.data(), nb_samples); // put this decoded data into the buffer alBufferData(destination_buffer, AL_FORMAT_STEREO16, raw_data.data(), nb_samples * 2, 32000); int error = alGetError(); if (error != AL_NO_ERROR) { std::ostringstream oss; oss << "Failed to fill the audio buffer with decoded SPC data for music file '" << file_name << ": error " << error; Debug::die(oss.str()); } }
// Calculate entropy over the bytes in a file. double GetByteEntropy(const std::string& filename) { // Open the file with the pointer at the end (ate). std::ifstream in_f(filename, std::ifstream::in | std::ifstream::binary | std::ifstream::ate); if (!in_f) { throw BadFilenameException; } // Determine file size. size_t fsize = in_f.tellg(); std::vector<char> raw_data(fsize); // Read in file contents. in_f.seekg(0, std::ios::beg); in_f.read(raw_data.data(), fsize); in_f.close(); // Convert to a histogram. std::array<int, 256> byte_counters; std::fill(byte_counters.begin(), byte_counters.end(), 0); std::for_each(raw_data.begin(), raw_data.end(), [&byte_counters](char x) { byte_counters.at(x)++; }); int total_bytes = std::accumulate(byte_counters.begin(), byte_counters.end(), 0); double h = 0.0; // Entropy // Calculate the entropy with this lambda. auto calc_entropy_f = [&](int x) { if (x > 0) { double p_i = static_cast<double>(x) / total_bytes; h -= p_i * (log(p_i) / log(2.0)); } }; // Do calculation. std::for_each(byte_counters.begin(), byte_counters.end(), calc_entropy_f); return h; }
/** * \brief Decodes a chunk of IT data into PCM data for the current music. * \param destination_buffer the destination buffer to write * \param nb_samples number of samples to write */ void Music::decode_it(ALuint destination_buffer, ALsizei nb_samples) { // Decode the IT data. std::vector<ALushort> raw_data(nb_samples); int bytes_read = it_decoder->decode(raw_data.data(), nb_samples); if (bytes_read > 0) { // Put this decoded data into the buffer. alBufferData(destination_buffer, AL_FORMAT_STEREO16, raw_data.data(), nb_samples, 44100); int error = alGetError(); if (error != AL_NO_ERROR) { std::ostringstream oss; oss << "Failed to fill the audio buffer with decoded IT data for music file '" << file_name << ": error " << error; Debug::die(oss.str()); } } }
bool cProject::load_infoFile() { QFile infoFile(m_infoFile); if(!infoFile.open(QIODevice::ReadOnly)) return false; QFileInfo finfo(infoFile); m_rootDir = finfo.absoluteDir(); QString raw_data(infoFile.readAll()); QStringList data_lines(raw_data.split("\n")); if(data_lines.count() < 3) return false; QStringList field_id(data_lines[0].split(":")); QStringList field_name(data_lines[1].split(":")); QStringList field_maintainer(data_lines[2].split(":")); if((field_id.count() != 2) || (field_name.count()!= 2) || (field_maintainer.count() != 2) ) return false; if(field_id[0]!="PROJECT-ID") return false; m_id = field_id[1]; m_name = field_name[1]; m_maintainer = field_maintainer[1]; return true; }
String GetWinRegString(const char *value, const char *path, HKEY base_key) { HKEY key = 0; if(RegOpenKeyEx(base_key, path, 0, KEY_READ, &key) != ERROR_SUCCESS) return String::GetVoid(); dword type, data; if(RegQueryValueEx(key, value, 0, &type, NULL, &data) != ERROR_SUCCESS) { RegCloseKey(key); return String::GetVoid(); } StringBuffer raw_data(data); if(RegQueryValueEx(key, value, 0, 0, (byte *)~raw_data, &data) != ERROR_SUCCESS) { RegCloseKey(key); return String::GetVoid(); } if(data > 0 && (type == REG_SZ || type == REG_EXPAND_SZ)) data--; raw_data.SetLength(data); RegCloseKey(key); return raw_data; }
void* msg_data = zmq_msg_data(&msg); memcpy(msg_data, part, size); } // Stream reader style void message::reset_read_cursor() { _read_cursor = 0; } message& message::operator>>(int8_t& integer) { assert(sizeof(int8_t) == size(_read_cursor)); int8_t* byte = static_cast<int8_t*>(raw_data(_read_cursor++)); integer = *byte; return *this; } message& message::operator>>(int16_t& integer) { assert(sizeof(int16_t) == size(_read_cursor)); uint16_t* network_order = static_cast<uint16_t*>(raw_data(_read_cursor++)); integer = static_cast<int16_t>(ntohs(*network_order)); return *this; }
std::vector<t> get(size_t elements) { std::vector<t> tmp; raw_data(tmp, elements); return tmp; }
std::string message::get(size_t const& part /* = 0 */) const { return std::string(static_cast<char const*>(raw_data(part)), size(part)); }
BaseType at(const position& pos) const { BaseType val; void* datap=raw_data(pos); memcpy(&val,datap,sizeof(BaseType)); return val; }
void readData<std::string>(std::istream& stream, std::string& string, size_t datasize) { std::vector<char> raw_data(datasize); readData(stream, raw_data, datasize); string = std::string(raw_data.data(), datasize); }
static void attr_create_pointiness(Scene *scene, Mesh *mesh, BL::Mesh &b_mesh, bool subdivision) { if (!mesh->need_attribute(scene, ATTR_STD_POINTINESS)) { return; } const int num_verts = b_mesh.vertices.length(); if (num_verts == 0) { return; } /* STEP 1: Find out duplicated vertices and point duplicates to a single * original vertex. */ vector<int> sorted_vert_indeices(num_verts); for (int vert_index = 0; vert_index < num_verts; ++vert_index) { sorted_vert_indeices[vert_index] = vert_index; } VertexAverageComparator compare(mesh->verts); sort(sorted_vert_indeices.begin(), sorted_vert_indeices.end(), compare); /* This array stores index of the original vertex for the given vertex * index. */ vector<int> vert_orig_index(num_verts); for (int sorted_vert_index = 0; sorted_vert_index < num_verts; ++sorted_vert_index) { const int vert_index = sorted_vert_indeices[sorted_vert_index]; const float3 &vert_co = mesh->verts[vert_index]; bool found = false; for (int other_sorted_vert_index = sorted_vert_index + 1; other_sorted_vert_index < num_verts; ++other_sorted_vert_index) { const int other_vert_index = sorted_vert_indeices[other_sorted_vert_index]; const float3 &other_vert_co = mesh->verts[other_vert_index]; /* We are too far away now, we wouldn't have duplicate. */ if ((other_vert_co.x + other_vert_co.y + other_vert_co.z) - (vert_co.x + vert_co.y + vert_co.z) > 3 * FLT_EPSILON) { break; } /* Found duplicate. */ if (len_squared(other_vert_co - vert_co) < FLT_EPSILON) { found = true; vert_orig_index[vert_index] = other_vert_index; break; } } if (!found) { vert_orig_index[vert_index] = vert_index; } } /* Make sure we always points to the very first orig vertex. */ for (int vert_index = 0; vert_index < num_verts; ++vert_index) { int orig_index = vert_orig_index[vert_index]; while (orig_index != vert_orig_index[orig_index]) { orig_index = vert_orig_index[orig_index]; } vert_orig_index[vert_index] = orig_index; } sorted_vert_indeices.free_memory(); /* STEP 2: Calculate vertex normals taking into account their possible * duplicates which gets "welded" together. */ vector<float3> vert_normal(num_verts, make_float3(0.0f, 0.0f, 0.0f)); /* First we accumulate all vertex normals in the original index. */ for (int vert_index = 0; vert_index < num_verts; ++vert_index) { const float3 normal = get_float3(b_mesh.vertices[vert_index].normal()); const int orig_index = vert_orig_index[vert_index]; vert_normal[orig_index] += normal; } /* Then we normalize the accumulated result and flush it to all duplicates * as well. */ for (int vert_index = 0; vert_index < num_verts; ++vert_index) { const int orig_index = vert_orig_index[vert_index]; vert_normal[vert_index] = normalize(vert_normal[orig_index]); } /* STEP 3: Calculate pointiness using single ring neighborhood. */ vector<int> counter(num_verts, 0); vector<float> raw_data(num_verts, 0.0f); vector<float3> edge_accum(num_verts, make_float3(0.0f, 0.0f, 0.0f)); BL::Mesh::edges_iterator e; EdgeMap visited_edges; int edge_index = 0; memset(&counter[0], 0, sizeof(int) * counter.size()); for (b_mesh.edges.begin(e); e != b_mesh.edges.end(); ++e, ++edge_index) { const int v0 = vert_orig_index[b_mesh.edges[edge_index].vertices()[0]], v1 = vert_orig_index[b_mesh.edges[edge_index].vertices()[1]]; if (visited_edges.exists(v0, v1)) { continue; } visited_edges.insert(v0, v1); float3 co0 = get_float3(b_mesh.vertices[v0].co()), co1 = get_float3(b_mesh.vertices[v1].co()); float3 edge = normalize(co1 - co0); edge_accum[v0] += edge; edge_accum[v1] += -edge; ++counter[v0]; ++counter[v1]; } for (int vert_index = 0; vert_index < num_verts; ++vert_index) { const int orig_index = vert_orig_index[vert_index]; if (orig_index != vert_index) { /* Skip duplicates, they'll be overwritten later on. */ continue; } if (counter[vert_index] > 0) { const float3 normal = vert_normal[vert_index]; const float angle = safe_acosf(dot(normal, edge_accum[vert_index] / counter[vert_index])); raw_data[vert_index] = angle * M_1_PI_F; } else { raw_data[vert_index] = 0.0f; } } /* STEP 3: Blur vertices to approximate 2 ring neighborhood. */ AttributeSet &attributes = (subdivision) ? mesh->subd_attributes : mesh->attributes; Attribute *attr = attributes.add(ATTR_STD_POINTINESS); float *data = attr->data_float(); memcpy(data, &raw_data[0], sizeof(float) * raw_data.size()); memset(&counter[0], 0, sizeof(int) * counter.size()); edge_index = 0; visited_edges.clear(); for (b_mesh.edges.begin(e); e != b_mesh.edges.end(); ++e, ++edge_index) { const int v0 = vert_orig_index[b_mesh.edges[edge_index].vertices()[0]], v1 = vert_orig_index[b_mesh.edges[edge_index].vertices()[1]]; if (visited_edges.exists(v0, v1)) { continue; } visited_edges.insert(v0, v1); data[v0] += raw_data[v1]; data[v1] += raw_data[v0]; ++counter[v0]; ++counter[v1]; } for (int vert_index = 0; vert_index < num_verts; ++vert_index) { data[vert_index] /= counter[vert_index] + 1; } /* STEP 4: Copy attribute to the duplicated vertices. */ for (int vert_index = 0; vert_index < num_verts; ++vert_index) { const int orig_index = vert_orig_index[vert_index]; data[vert_index] = data[orig_index]; } }
void get(Type** value, size_t const part) const { *value = static_cast<Type*>(raw_data(part)); }