Пример #1
0
void Gosu::saveImageFile(const Gosu::Bitmap& bitmap, Gosu::Writer writer,
    const std::wstring& formatHint)
{
    int ok;
    
    if (isExtension(formatHint.c_str(), L"bmp"))
    {
        ok = stbi_write_bmp_to_func(stbiWriteToWriter, &writer,
                                    bitmap.width(), bitmap.height(), 4, bitmap.data());
    }
    else if (isExtension(formatHint.c_str(), L"tga"))
    {
        stbi_write_tga_with_rle = 0;
        ok = stbi_write_tga_to_func(stbiWriteToWriter, &writer,
                                    bitmap.width(), bitmap.height(), 4, bitmap.data());
    }
    else
    {
        ok = stbi_write_png_to_func(stbiWriteToWriter, &writer,
                                    bitmap.width(), bitmap.height(), 4, bitmap.data(), 0);
    }
    
    if (ok == 0)
        throw std::runtime_error("Could not save image data to memory (format hint = '" +
                                 Gosu::wstringToUTF8(formatHint) + "'");
}
Пример #2
0
void Gosu::saveImageFile(const Gosu::Bitmap& bitmap, Gosu::Writer writer,
    const std::wstring& formatHint)
{
    unsigned char* rgba =
        const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(bitmap.data()));
    
    // TODO: Use the new *_to_func functions in stb_imagewrite.h instead:
    // https://github.com/nothings/stb/blob/master/stb_image_write.h#L39
    int length;
    unsigned char* png =
        stbi_write_png_to_mem(rgba, 0, bitmap.width(), bitmap.height(), 4, &length);
    
    if (png == 0)
        throw std::runtime_error("Could not save image data to memory");
    
    try
    {
        writer.write(png, length);
    }
    catch (...)
    {
        STBIW_FREE(png);
        throw;
    }
    
    STBIW_FREE(png);
}
Пример #3
0
void Gosu::saveImageFile(const Gosu::Bitmap& bitmap, Gosu::Writer writer,
    const std::wstring& formatHint)
{
    unsigned char* rgba =
        const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(bitmap.data()));
    
    int length;
    unsigned char* png =
        stbi_write_png_to_mem(rgba, 0, bitmap.width(), bitmap.height(), 4, &length);
    
    if (png == 0)
        throw std::runtime_error("Could not save image data to memory");
    
    try
    {
        writer.write(png, length);
    }
    catch (...)
    {
        STBIW_FREE(png);
        throw;
    }
    
    STBIW_FREE(png);
}
Пример #4
0
void Gosu::saveImageFile(const Gosu::Bitmap& bitmap, const std::wstring& filename)
{
    int ok;
    std::string utf8 = Gosu::wstringToUTF8(filename);
    
    if (isExtension(filename.c_str(), L"bmp"))
    {
        ok = stbi_write_bmp(utf8.c_str(), bitmap.width(), bitmap.height(), 4, bitmap.data());
    }
    else if (isExtension(filename.c_str(), L"tga"))
    {
        ok = stbi_write_tga(utf8.c_str(), bitmap.width(), bitmap.height(), 4, bitmap.data());
    }
    else
    {
        ok = stbi_write_png(utf8.c_str(), bitmap.width(), bitmap.height(), 4, bitmap.data(), 0);
    }
    
    if (ok == 0)
        throw std::runtime_error("Could not save image data to file: " + utf8);
}
Пример #5
0
Gosu::Bitmap Gosu::Texture::toBitmap(unsigned x, unsigned y, unsigned width, unsigned height) const
{
#ifdef GOSU_IS_IPHONE
    throw std::logic_error("Texture::toBitmap not supported on iOS");
#else
    Gosu::Bitmap fullTexture;
    fullTexture.resize(size(), size());
    glBindTexture(GL_TEXTURE_2D, name);
    glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, fullTexture.data());
    Gosu::Bitmap bitmap;
    bitmap.resize(width, height);
    bitmap.insert(fullTexture, -int(x), -int(y));
    
    return bitmap;
#endif
}
Пример #6
0
void Gosu::loadImageFile(Gosu::Bitmap& bitmap, Reader input)
{
    stbi_io_callbacks callbacks;
    callbacks.read = readCallback;
    callbacks.skip = skipCallback;
    callbacks.eof = eofCallback;
    int x, y, n;
    stbi_uc* bytes = stbi_load_from_callbacks(&callbacks, &input, &x, &y, &n, STBI_rgb_alpha);

    if (bytes == 0) {
        // TODO - stbi_failure_reason is not thread safe. Everything here should be wrapped in a mutex.
        throw std::runtime_error("Cannot load image: " + std::string(stbi_failure_reason()));
    }

    bitmap.resize(x, y);
    std::memcpy(bitmap.data(), bytes, x * y * sizeof(Gosu::Color));

    stbi_image_free(bytes);
}