2012-05-16 15:03:49 +10:00
|
|
|
/*
|
|
|
|
* This file is part of libgim.
|
|
|
|
*
|
|
|
|
* libgim 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.
|
|
|
|
*
|
|
|
|
* libgim 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2014-06-29 22:50:47 +10:00
|
|
|
* Copyright 2011-2014 Danny Robson <danny@nerdcruft.net>
|
2012-05-16 15:03:49 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "image.hpp"
|
|
|
|
|
|
|
|
#include "debug.hpp"
|
|
|
|
#include "except.hpp"
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
|
2014-06-29 22:50:47 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-05-16 15:03:49 +10:00
|
|
|
static void
|
|
|
|
write_netpbm (const uint8_t *restrict pixels,
|
|
|
|
size_t width,
|
|
|
|
size_t height,
|
|
|
|
size_t stride,
|
|
|
|
const boost::filesystem::path &path,
|
|
|
|
const char* MAGIC) {
|
2015-01-28 14:49:34 +11:00
|
|
|
CHECK (pixels);
|
|
|
|
CHECK_GT (width, 0);
|
|
|
|
CHECK_GE (stride, width);
|
|
|
|
CHECK_GT (height, 0);
|
2012-05-16 15:03:49 +10:00
|
|
|
|
|
|
|
// Establish an output stream
|
2012-05-17 14:16:40 +10:00
|
|
|
std::ofstream output (path.string ());
|
2012-05-16 15:03:49 +10:00
|
|
|
if (!output.good ())
|
|
|
|
throw util::output_error ("Unable to open output file");
|
|
|
|
|
|
|
|
// Write the PPM header.
|
|
|
|
output << MAGIC << "\n"
|
|
|
|
<< width << "\n"
|
|
|
|
<< height << "\n"
|
|
|
|
<< (size_t)std::numeric_limits<uint8_t>::max () << "\n";
|
|
|
|
|
|
|
|
// Write the data rows
|
|
|
|
for (size_t y = 0; y < height; ++y) {
|
|
|
|
for (size_t x = 0; x < width; ++x)
|
2014-07-02 15:45:06 +10:00
|
|
|
output << pixels[y * stride + x];
|
2012-05-16 15:03:49 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-29 22:50:47 +10:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2012-05-16 15:03:49 +10:00
|
|
|
void
|
|
|
|
util::pgm::write (const uint8_t *restrict pixels,
|
|
|
|
size_t width,
|
|
|
|
size_t height,
|
|
|
|
size_t stride,
|
|
|
|
const boost::filesystem::path &path) {
|
|
|
|
// TODO: We should switch between P2 (ascii) and P5 (binary)
|
2012-05-24 17:07:01 +10:00
|
|
|
static const char MAGIC[] = "P5";
|
2012-05-16 15:03:49 +10:00
|
|
|
write_netpbm (pixels, width, height, stride, path, MAGIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-29 22:50:47 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-05-16 15:03:49 +10:00
|
|
|
void
|
|
|
|
util::ppm::write (const uint8_t *restrict pixels,
|
|
|
|
size_t width,
|
|
|
|
size_t height,
|
|
|
|
size_t stride,
|
|
|
|
const boost::filesystem::path &path) {
|
|
|
|
// TODO: We should switch between P3 (ascii) and P6 (binary)
|
|
|
|
static const char MAGIC[] = "P6";
|
|
|
|
write_netpbm (pixels, width, height, stride, path, MAGIC);
|
|
|
|
}
|