Beispiel #1
0
void ArrayReferenceTest::voidConversion() {
    int a[] = {3, 4, 7, 12, 0, -15};

    /** @todo C++14: test that all the operations are really constexpr (C++11 doesn't allow void conversions IMHO) */

    /* void reference to compile-time array */
    Containers::ArrayReference<const void> b = a;
    CORRADE_VERIFY(b == a);
    CORRADE_COMPARE(b.size(), 6*sizeof(int));

    /* void reference to runtime array */
    Containers::ArrayReference<const void> c = {a, 6};
    CORRADE_VERIFY(c == a);
    CORRADE_COMPARE(c.size(), 6*sizeof(int));

    /* void reference to Array */
    Containers::Array<int> d(6);
    Containers::ArrayReference<const void> e = d;
    CORRADE_VERIFY(e == d);
    CORRADE_COMPARE(e.size(), d.size()*sizeof(int));

    /* void reference to ArrayReference */
    Containers::ArrayReference<int> f = a;
    Containers::ArrayReference<const void> g = f;
    CORRADE_VERIFY(g == f);
    CORRADE_COMPARE(g.size(), f.size()*sizeof(int));
}
Beispiel #2
0
bool Directory::write(const std::string& filename, const Containers::ArrayReference<const void> data) {
    std::ofstream file(filename, std::ofstream::binary);
    if(!file) return false;

    file.write(reinterpret_cast<const char*>(data.data()), data.size());
    return true;
}
Beispiel #3
0
 void doOpenData(Containers::ArrayReference<const unsigned char> data) override {
     opened = (data.size() == 1 && data[0] == 0xa5);
 }
Beispiel #4
0
void WavImporter::doOpenData(Containers::ArrayReference<const unsigned char> data) {
    /* Check file size */
    if(data.size() < sizeof(WavHeader)) {
        Error() << "Audio::WavImporter::openData(): the file is too short:" << data.size() << "bytes";
        return;
    }

    /* Get header contents and fix endianness */
    WavHeader header(*reinterpret_cast<const WavHeader*>(data.begin()));
    Utility::Endianness::littleEndianInPlace(header.chunkSize,
        header.subChunk1Size, header.audioFormat, header.numChannels,
        header.sampleRate, header.byteRate, header.blockAlign,
        header.bitsPerSample, header.subChunk2Size);

    /* Check file signature */
    if(std::strncmp(header.chunkId, "RIFF", 4) != 0 ||
       std::strncmp(header.format, "WAVE", 4) != 0 ||
       std::strncmp(header.subChunk1Id, "fmt ", 4) != 0 ||
       std::strncmp(header.subChunk2Id, "data", 4) != 0) {
        Error() << "Audio::WavImporter::openData(): the file signature is invalid";
        return;
    }

    /* Check file size */
    if(header.chunkSize + 8 != data.size()) {
        Error() << "Audio::WavImporter::openData(): the file has improper size, expected"
                << header.chunkSize + 8 << "but got" << data.size();
        return;
    }

    /* Check PCM format */
    if(header.audioFormat != 1) {
        Error() << "Audio::WavImporter::openData(): unsupported audio format" << header.audioFormat;
        return;
    }

    /* Verify more things */
    if(header.subChunk1Size != 16 ||
       header.subChunk2Size + 44 != data.size() ||
       header.blockAlign != header.numChannels*header.bitsPerSample/8 ||
       header.byteRate != header.sampleRate*header.blockAlign) {
        Error() << "Audio::WavImporter::openData(): the file is corrupted";
        return;
    }

    /* Decide about format */
    if(header.numChannels == 1 && header.bitsPerSample == 8)
        _format = Buffer::Format::Mono8;
    else if(header.numChannels == 1 && header.bitsPerSample == 16)
        _format = Buffer::Format::Mono16;
    else if(header.numChannels == 2 && header.bitsPerSample == 8)
        _format = Buffer::Format::Stereo8;
    else if(header.numChannels == 2 && header.bitsPerSample == 16)
        _format = Buffer::Format::Stereo16;
    else {
        Error() << "Audio::WavImporter::openData(): unsupported channel count"
                << header.numChannels << "with" << header.bitsPerSample
                << "bits per sample";
        return;
    }

    /* Save frequency */
    _frequency = header.sampleRate;

    /** @todo Convert the data from little endian too */
    CORRADE_INTERNAL_ASSERT(!Utility::Endianness::isBigEndian());

    /* Copy the data */
    _data = Containers::Array<unsigned char>(header.subChunk2Size);
    std::copy(data.begin()+sizeof(WavHeader), data.end(), _data.begin());
    return;
}
Beispiel #5
0
std::string Resource::get(const std::string& filename) const {
    Containers::ArrayReference<const unsigned char> data = getRaw(filename);
    return data ? std::string(reinterpret_cast<const char*>(data.begin()), data.size()) : std::string();
}
Beispiel #6
0
void PngImporter::doOpenData(const Containers::ArrayReference<const unsigned char> data) {
    _in = new std::istringstream(std::string(reinterpret_cast<const char*>(data.begin()), data.size()));
}
void JpegImporter::doOpenData(const Containers::ArrayReference<const unsigned char> data) {
    _in = Containers::Array<unsigned char>(data.size());
    std::copy(data.begin(), data.end(), _in.begin());
}
void StbImageImporter::doOpenData(const Containers::ArrayReference<const char> data) {
    _in = Containers::Array<unsigned char>{data.size()};
    std::copy(data.begin(), data.end(), _in.data());
}