inline std::string key_qvalue_pairs(array_view<kv_pair> pairs) { std::string buffer; // Ensure string is large enough for our use, to avoid useless internal resize size_t maj = std::accumulate(pairs.begin(), pairs.end(), size_t{0}, [](size_t acc, kv_pair p){return acc + p.key.size()+p.value.size()+8;}); // reserve some space for 8 quoted chars inside value // if there is more string is on it's own and will spend slightly more time buffer.reserve(maj+8); return key_qvalue_pairs(buffer, pairs); }
explicit mdarray(const array_view<ATYPE> &view): _imap(map_utils<map_type>::create(view.template shape<shape_t>())), _data(container_utils<storage_type>::create(view.size())) { std::copy(view.begin(),view.end(),_data.begin()); }
bitmap xtk::pcx_decode (const array_view<std::uint8_t>& data) { static_assert (sizeof (pcx_header) == 128, "Header length incorrect"); const auto& header = *(const pcx_header*)data.data(); auto width = header.xmax - header.xmin + 1; auto height = header.ymax - header.ymin + 1; __xtk_assert (std::invalid_argument, header.vendor_id == 0x0A && header.version >= 5); xtk::Debug::log( "load_pcx: \n" "\tversion = %d\n" "\tbits_per_pixel = %d\n" "\tcolor_planes = %d\n" "\twidth = %u\n" "\theight = %u\n" , header.version, header.bits_per_pixel, header.color_planes, width, height ); __xtk_assert(std::invalid_argument, header.bits_per_pixel == 8 && header.color_planes == 0); auto buffer = std::make_unique<bitmap::value_type []> (width*height); auto stream = array_view<const std::uint8_t> {data.begin() + sizeof (header), data.end()}; auto next_pixel = 0u; auto next_sbyte = stream.begin (); auto palette = xtk::array_view<glm::tvec3<std::uint8_t>> { (const glm::tvec3<std::uint8_t> *)data.end () - 256, (const glm::tvec3<std::uint8_t> *)data.end () }; while (next_sbyte < stream.end ()) { auto rle = *next_sbyte; ++next_sbyte; if (rle >= 0xc0) { rle &= 0x3f; auto pixel = bitmap::value_type (palette [*next_sbyte], 255); ++next_sbyte; for (auto j = 0; j < rle; ++j) { buffer [next_pixel] = pixel; ++next_pixel; __xtk_assert (std::overflow_error, next_pixel <= width*height); } } else { buffer [next_pixel] = bitmap::value_type (palette [rle], 255); ++next_pixel; __xtk_assert (std::overflow_error, next_pixel <= width*height); } if (next_pixel >= width*height) { break; } } __xtk_assert (std::invalid_argument, next_pixel == width*height); return bitmap (std::move (buffer), width, height); }