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.
|
2015-02-04 15:59:06 +11:00
|
|
|
*
|
2012-05-16 15:03:49 +10:00
|
|
|
* 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.
|
2015-02-04 15:59:06 +11:00
|
|
|
*
|
2012-05-16 15:03:49 +10:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2015-02-04 15:59:06 +11:00
|
|
|
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
|
2012-05-16 15:03:49 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "image.hpp"
|
|
|
|
|
|
|
|
#include "debug.hpp"
|
|
|
|
#include "except.hpp"
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
|
2015-02-04 15:59:06 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
util::image::buffer<T>::buffer (size_t _w, size_t _h, size_t _s):
|
|
|
|
w (_w),
|
|
|
|
h (_h),
|
|
|
|
s (_s),
|
|
|
|
data (std::make_unique<T[]> (_w * _s))
|
|
|
|
{
|
|
|
|
CHECK_NEQ (w * h, 0);
|
|
|
|
CHECK_GE (s, w);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
util::image::buffer<T>::buffer (size_t _w, size_t _h):
|
|
|
|
buffer<T> (_w, _h, _w)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
util::image::buffer<T>::buffer (size_t _w,
|
|
|
|
size_t _h,
|
|
|
|
size_t _s,
|
|
|
|
std::unique_ptr<T[]> &&_data):
|
|
|
|
w (_w),
|
|
|
|
h (_h),
|
|
|
|
s (_s),
|
|
|
|
data (std::move (_data))
|
|
|
|
{
|
|
|
|
CHECK_NEQ (w * h, 0);
|
|
|
|
CHECK_GE (s, w);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
template <typename U>
|
|
|
|
util::image::buffer<U>
|
|
|
|
util::image::buffer<T>::alloc (void) const
|
|
|
|
{
|
|
|
|
return buffer<U> (w, h, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
void
|
|
|
|
util::image::buffer<T>::fill (const T v)
|
|
|
|
{
|
|
|
|
std::fill (data.get (), data.get () + w * s, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T, typename U>
|
|
|
|
static U
|
|
|
|
rescale (T v)
|
|
|
|
{
|
|
|
|
return v * sizeof (U) / sizeof (T);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
template <typename U>
|
|
|
|
util::image::buffer<U>
|
|
|
|
util::image::buffer<T>::clone (void) const
|
|
|
|
{
|
|
|
|
auto out = alloc<U> ();
|
|
|
|
|
|
|
|
std::transform (data.get (),
|
|
|
|
data.get () + w * s,
|
|
|
|
out.data.get (),
|
|
|
|
[] (auto v) {
|
|
|
|
return rescale<T,U> (v);
|
|
|
|
});
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
util::image::buffer<T>
|
|
|
|
util::image::buffer<T>::downsample (unsigned factor)
|
|
|
|
{
|
2015-02-04 18:39:40 +11:00
|
|
|
CHECK_GE (factor, 1);
|
|
|
|
if (factor == 1)
|
|
|
|
return clone<T> ();
|
2015-02-04 15:59:06 +11:00
|
|
|
|
|
|
|
size_t w_ = w / factor;
|
|
|
|
size_t h_ = h / factor;
|
|
|
|
size_t s_ = w_;
|
|
|
|
|
|
|
|
CHECK_NEQ (w_, 0);
|
|
|
|
CHECK_NEQ (h_, 0);
|
|
|
|
|
|
|
|
buffer out { w_, h_, s_, std::make_unique<T[]> (w_ * h_) };
|
|
|
|
|
|
|
|
// Do a really shitty nearest neighbour average in src-space.
|
|
|
|
for (size_t y = 0; y < out.h; ++y)
|
|
|
|
for (size_t x = 0; x < out.w; ++x) {
|
2015-02-04 18:39:40 +11:00
|
|
|
size_t src00 = (y + 0) * s * factor + (x * factor) + 0;
|
|
|
|
size_t src01 = (y + 0) * s * factor + (x * factor) + 1;
|
|
|
|
size_t src10 = (y + 1) * s * factor + (x * factor) + 0;
|
|
|
|
size_t src11 = (y + 1) * s * factor + (x * factor) + 1;
|
2015-02-04 15:59:06 +11:00
|
|
|
|
2015-02-04 18:39:40 +11:00
|
|
|
out.data[x + y * out.s] = (data[src00] + data[src01] + data[src10] + data[src11]) / 4;
|
2015-02-04 15:59:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2015-02-04 15:59:06 +11:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template struct util::image::buffer<uint8_t>;
|
|
|
|
template util::image::buffer<uint8_t> util::image::buffer<uint8_t>::alloc (void) const;
|
2015-02-04 18:39:08 +11:00
|
|
|
template util::image::buffer<uint8_t> util::image::buffer<uint8_t>::clone (void) const;
|