Пример #1
0
TEST_F(SerializeTest, NonEmptyArrayTest) {
    vector<Json> a;
    a.push_back(Json::number(1.0));
    a.push_back(Json::number(2.0));
    a.push_back(Json::number(3.0));
    EXPECT_THAT(Json::array(a), SerializesTo(format("[{0},{1},{2}]", 1.0, 2.0, 3.0)));
}
Пример #2
0
TEST_F(SerializeTest, NonEmptyObjectTest) {
    StringMap<Json> o;
    o.insert(make_pair("one", Json::number(1.0)));
    o.insert(make_pair("two", Json::number(2.0)));
    o.insert(make_pair("three", Json::number(3.0)));
    EXPECT_THAT(Json::object(o), SerializesTo(format(
                    "{{"
                    "\"one\":{0},"
                    "\"three\":{1},"
                    "\"two\":{2}"
                    "}}",
                    1.0, 3.0, 2.0)));
}
Пример #3
0
void PixMap::read_direct_image(ReadSource in, unique_ptr<RasterImage>& image) const {
    if (pixel_type != RGB_DIRECT) {
        throw Exception("image is not direct");
    }
    if (pack_type != 4) {
        throw Exception(format("unsupported pack_type {0}", pack_type));
    }
    image.reset(new RasterImage(bounds));
    if (row_bytes == 0) {
        return;
    }
    size_t bytes_read = 0;
    for (int y = 0; y < bounds.height(); ++y) {
        Bytes bytes;
        if (row_bytes <= 250) {
            bytes.resize(read<uint8_t>(in));
            bytes_read += 1;
        } else {
            bytes.resize(read<uint16_t>(in));
            bytes_read += 2;
        }
        in.shift(bytes.data(), bytes.size());
        bytes_read += bytes.size();

        BytesSlice remainder(bytes);
        Bytes components;
        while (!remainder.empty()) {
            uint8_t header = read<uint8_t>(remainder);
            if (header >= 0x80) {
                components.push(0x101 - header, read<uint8_t>(remainder));
            } else {
                size_t size = header + 1;
                components.push(size, '\0');
                uint8_t* data = components.data() + components.size() - size;
                remainder.shift(data, size);
            }
        }
        const int16_t w = bounds.width();
        const BytesSlice red = components.slice((cmp_count - 3) * w, w);
        const BytesSlice green = components.slice((cmp_count - 2) * w, w);
        const BytesSlice blue = components.slice((cmp_count - 1) * w);
        for (int x = 0; x < w; ++x) {
            image->set(x, y, AlphaColor(red.at(x), green.at(x), blue.at(x)));
        }
    }
    if ((bytes_read % 2) != 0) {
        in.shift(1);
    }
}
Пример #4
0
void BitsSlice::shift(uint8_t* data, size_t size) {
    if (size == 0) {
        return;
    } else if (size + _bit_index > 8) {
        throw Exception(format("unhandled case ({0} + {1})", size, _bit_index));
    }

    uint8_t byte = _bytes.at(0);
    *data = bits(byte, _bit_index, _bit_index + size);

    _bit_index += size;
    if (_bit_index == 8) {
        _bytes.shift(1);
        _bit_index = 0;
    }
}
Пример #5
0
ScenarioList::ScenarioList() {
    linked_ptr<Entry> factory_scenario(new Entry);
    factory_scenario->identifier.assign("com.biggerplanet.ares");
    factory_scenario->title.assign("Ares");
    factory_scenario->download_url.assign("http://www.arescentral.com");
    factory_scenario->author.assign("Bigger Planet");
    factory_scenario->author_url.assign("http://www.biggerplanet.com");
    u32_to_version(0x01010100, factory_scenario->version);
    _scenarios.push_back(factory_scenario);

    ScopedGlob g;
    const String home(utf8::decode(getenv("HOME")));
    const StringSlice scenarios("Library/Application Support/Antares/Scenarios");
    const StringSlice info("scenario-info/128.nlAG");
    String str(format("{0}/{1}/*/{2}", home, scenarios, info));
    CString c_str(str);
    glob(c_str.data(), 0, NULL, &g.data);

    size_t prefix_len = home.size() + scenarios.size() + 2;
    size_t suffix_len = info.size() + 1;
    for (int i = 0; i < g.data.gl_matchc; ++i) {
        const String path(utf8::decode(g.data.gl_pathv[i]));
        StringSlice identifier = path.slice(prefix_len, path.size() - prefix_len - suffix_len);
        if (identifier == factory_scenario->identifier) {
            continue;
        }

        MappedFile file(path);
        BytesSlice data(file.data());
        scenarioInfoType info;
        read(data, info);
        linked_ptr<Entry> entry(new Entry);
        entry->identifier.assign(identifier);
        entry->title.assign(info.titleString);
        entry->download_url.assign(info.downloadURLString);
        entry->author.assign(info.authorNameString);
        entry->author_url.assign(info.authorURLString);
        u32_to_version(info.version, entry->version);
        _scenarios.push_back(entry);
    }
}
Пример #6
0
void read_from(ReadSource in, PixMap& out) {
    read(in, out.row_bytes);
    out.row_bytes &= 0x3fff;
    read(in, out.bounds);
    read(in, out.pm_version);
    read(in, out.pack_type);
    read(in, out.pack_size);
    read(in, out.h_res);
    read(in, out.v_res);
    read(in, out.pixel_type);
    read(in, out.pixel_size);
    read(in, out.cmp_count);
    read(in, out.cmp_size);
    read(in, out.plane_bytes);
    read(in, out.pm_table);
    read(in, out.pm_reserved);

    if (out.plane_bytes != 0) {
        throw Exception("PixMap::plane_bytes must be 0");
    }
    if (out.pm_reserved != 0) {
        throw Exception("PixMap::pm_reserved must be 0");
    }

    switch (out.pixel_type) {
        case INDEXED: {
            switch (out.pixel_size) {
              case 1:
              case 2:
              case 4:
              case 8:
                break;

              default:
                throw Exception(format("indexed pixels may not have size {0}", out.pixel_size));
            }
            if ((out.pack_type != 0) || (out.pack_size != 0)) {
                throw Exception("indexed pixels may not be packed");
            }
            if ((out.cmp_count != 1) || (out.cmp_size != out.pixel_size)) {
                throw Exception("indexed pixels must have one component");
            }
            break;
        }

        case RGB_DIRECT: {
            switch (out.pixel_size) {
                case 16: {
                    throw Exception(format("unsupported pixel_size {0}", out.pixel_size));
                    break;
                }

                case 32: {
                    if (out.cmp_size != 8) {
                        throw Exception("32-bit direct pixels must have cmp_size 8");
                    }
                    break;
                }

                default: {
                    throw Exception(format("direct pixels may not have size {0}", out.pixel_size));
                }
            }
            if ((out.cmp_count != 3) && (out.cmp_count != 4)) {
                throw Exception("direct pixels must have three or four components");
            }
            break;
        }

        default: {
            throw Exception(format("illegal PixMap pixel_type {0}", out.pixel_type));
        }
    }
}