openspades/Sources/Core/PngWriter.cpp

92 lines
2.5 KiB
C++
Raw Normal View History

2014-03-16 16:41:11 +01:00
/*
Copyright (c) 2014 Marco Schlumpp <marco.schlumpp@gmail.com>
2014-03-16 16:41:11 +01:00
This file is part of OpenSpades.
2014-03-16 16:41:11 +01:00
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2014-03-16 16:41:11 +01:00
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2014-03-16 16:41:11 +01:00
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
2014-03-16 16:41:11 +01:00
*/
2014-03-24 23:50:07 +09:00
#include <cstdint>
#include <cstring>
2014-03-16 16:41:11 +01:00
#include "Bitmap.h"
#include "IBitmapCodec.h"
2014-03-16 16:41:11 +01:00
#include "IStream.h"
#include "pnglite.h"
namespace {
unsigned WriteCallback(void *input, std::size_t size, std::size_t numel, void *user_ptr) {
auto stream = static_cast<spades::IStream *>(user_ptr);
2014-03-16 16:41:11 +01:00
stream->Write(input, size * numel);
return static_cast<unsigned>(size * numel);
2014-03-16 16:41:11 +01:00
}
}
namespace spades {
class PngWriter : public IBitmapCodec {
public:
PngWriter() : IBitmapCodec() { png_init(nullptr, nullptr); }
2014-03-16 16:41:11 +01:00
bool CanLoad() override { return false; }
2014-03-16 16:41:11 +01:00
bool CanSave() override { return true; }
2014-03-16 16:41:11 +01:00
bool CheckExtension(const std::string &filename) override {
2014-03-16 16:41:11 +01:00
return EndsWith(filename, ".png");
}
std::string GetName() override {
2014-03-16 16:41:11 +01:00
static std::string name("libpng exporter");
return name;
}
Bitmap *Load(IStream *str) override {
2014-03-16 16:41:11 +01:00
SPADES_MARK_FUNCTION();
SPUnreachable();
2014-03-16 16:41:11 +01:00
}
void Save(IStream *stream, Bitmap *bmp) override {
2014-03-16 16:41:11 +01:00
SPADES_MARK_FUNCTION();
int err;
png_t png_s;
if ((err = png_open_write(&png_s, &WriteCallback, stream))) {
2014-03-16 16:41:11 +01:00
SPRaise("Error while png_open_write: %s", png_error_string(err));
}
// Create flipped buffer
std::vector<std::uint8_t> buf(bmp->GetWidth() * bmp->GetHeight() * 4);
{
std::uint32_t *pixels = bmp->GetPixels();
auto width = bmp->GetWidth();
auto rowLengthBytes = width * sizeof(std::uint8_t) * 4;
for (long y = bmp->GetHeight() - 1; y >= 0; --y) {
std::memcpy(&buf[y * rowLengthBytes], pixels, rowLengthBytes);
2014-03-16 16:41:11 +01:00
pixels += width;
}
}
if ((err = png_set_data(&png_s, bmp->GetWidth(), bmp->GetHeight(), 8,
PNG_TRUECOLOR_ALPHA, buf.data()))) {
2014-03-16 16:41:11 +01:00
SPRaise("Error while png_set_data: %s", png_error_string(err));
}
}
};
static PngWriter sharedCodec;
}