From 54f6811058ca8580ad681e943aded6aac873bb49 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 16 May 2012 15:03:49 +1000 Subject: [PATCH] Add simple PGM and PPM image writers --- Makefile.am | 2 ++ image.cpp | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++ image.hpp | 45 +++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 image.cpp create mode 100644 image.hpp diff --git a/Makefile.am b/Makefile.am index b7f00a33..7ffe6409 100644 --- a/Makefile.am +++ b/Makefile.am @@ -40,6 +40,8 @@ UTIL_FILES = \ guid.hpp \ hash.cpp \ hash.hpp \ + image.cpp \ + image.hpp \ io.cpp \ io.hpp \ ip.cpp \ diff --git a/image.cpp b/image.cpp new file mode 100644 index 00000000..fe170ca1 --- /dev/null +++ b/image.cpp @@ -0,0 +1,82 @@ +/* + * 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 . + * + * Copyright 2011 Danny Robson + */ + + +#include "image.hpp" + +#include "debug.hpp" +#include "except.hpp" + +#include + + +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) { + CHECK_HARD (pixels); + CHECK_HARD (width > 0); + CHECK_HARD (stride >= width); + CHECK_HARD (height > 0); + + // Establish an output stream + std::ofstream output (path.native (), std::ios::binary); + 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::max () << "\n"; + + // Write the data rows + for (size_t y = 0; y < height; ++y) { + for (size_t x = 0; x < width; ++x) + output << (size_t)pixels[y * stride + x] << " "; + + output << "\n"; + } +} + +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) + static const char MAGIC[] = "P2"; + write_netpbm (pixels, width, height, stride, path, MAGIC); +} + + +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); +} diff --git a/image.hpp b/image.hpp new file mode 100644 index 00000000..06271d0f --- /dev/null +++ b/image.hpp @@ -0,0 +1,45 @@ +/* + * 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 . + * + * Copyright 2011 Danny Robson + */ + +#ifndef __UTIL_IMAGE_HPP +#define __UTIL_IMAGE_HPP + +#include +#include +#include + +namespace util { + struct pgm { + static void write (const uint8_t *restrict pixels, + size_t width, + size_t height, + size_t stride, + const boost::filesystem::path &path); + }; + + struct ppm { + static void write (const uint8_t *restrict pixels, + size_t width, + size_t height, + size_t stride, + const boost::filesystem::path &path); + }; +} + +#endif