From 08473e50e90059e75d6e7bce7e12be8bf6dd028b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Wed, 11 Sep 2013 18:14:22 +0100 Subject: [PATCH] image: Move helpers to respective modules. --- image/image.hpp | 39 +++++++++++---------------------------- image/image_bmp.cpp | 2 ++ image/image_png.cpp | 11 +++++++++++ image/image_pnm.cpp | 19 +++++++++++++++++-- image/image_raw.cpp | 19 ++++++++++++++++++- 5 files changed, 59 insertions(+), 31 deletions(-) diff --git a/image/image.hpp b/image/image.hpp index 7e13dd6..6b32f27 100644 --- a/image/image.hpp +++ b/image/image.hpp @@ -31,7 +31,7 @@ #define _IMAGE_HPP_ -#include +#include namespace image { @@ -81,43 +81,26 @@ public: return flipped ? -(signed)(width*channels) : width*channels; } - bool writeBMP(const char *filename) const; + bool + writeBMP(const char *filename) const; - void writePNM(std::ostream &os, const char *comment = NULL) const; + void + writePNM(std::ostream &os, const char *comment = NULL) const; - inline bool writePNM(const char *filename, const char *comment = NULL) const { - std::ofstream os(filename, std::ofstream::binary); - if (!os) { - return false; - } - writePNM(os, comment); - return true; - } + bool + writePNM(const char *filename, const char *comment = NULL) const; bool writePNG(std::ostream &os) const; - inline bool - writePNG(const char *filename) const { - std::ofstream os(filename, std::ofstream::binary); - if (!os) { - return false; - } - return writePNG(os); - } + bool + writePNG(const char *filename) const; void writeRAW(std::ostream &os) const; - inline bool - writeRAW(const char *filename) const { - std::ofstream os(filename, std::ofstream::binary); - if (!os) { - return false; - } - writeRAW(os); - return true; - } + bool + writeRAW(const char *filename) const; }; diff --git a/image/image_bmp.cpp b/image/image_bmp.cpp index e0c6428..1961f9d 100644 --- a/image/image_bmp.cpp +++ b/image/image_bmp.cpp @@ -28,6 +28,8 @@ #include #include +#include + #include "image.hpp" diff --git a/image/image_png.cpp b/image/image_png.cpp index 0a413e1..357a78e 100644 --- a/image/image_png.cpp +++ b/image/image_png.cpp @@ -123,6 +123,17 @@ no_png: } +bool +Image::writePNG(const char *filename) const +{ + std::ofstream os(filename, std::ofstream::binary); + if (!os) { + return false; + } + return writePNG(os); +} + + Image * readPNG(const char *filename) { diff --git a/image/image_pnm.cpp b/image/image_pnm.cpp index f9cd05d..9e9d0e1 100644 --- a/image/image_pnm.cpp +++ b/image/image_pnm.cpp @@ -30,6 +30,8 @@ #include #include +#include + #include "image.hpp" @@ -40,8 +42,8 @@ namespace image { * http://netpbm.sourceforge.net/doc/ppm.html */ void -Image::writePNM(std::ostream &os, const char *comment) const { - assert(channels == 1 || channels >= 3); +Image::writePNM(std::ostream &os, const char *comment) const +{ os << (channels == 1 ? "P5" : "P6") << "\n"; if (comment) { @@ -109,6 +111,19 @@ Image::writePNM(std::ostream &os, const char *comment) const { } } + +bool +Image::writePNM(const char *filename, const char *comment) const +{ + std::ofstream os(filename, std::ofstream::binary); + if (!os) { + return false; + } + writePNM(os, comment); + return true; +} + + const char * readPNMHeader(const char *buffer, size_t bufferSize, unsigned *channels, unsigned *width, unsigned *height) { diff --git a/image/image_raw.cpp b/image/image_raw.cpp index dd45d10..93946be 100644 --- a/image/image_raw.cpp +++ b/image/image_raw.cpp @@ -30,13 +30,17 @@ #include #include +#include + #include "image.hpp" namespace image { + void -Image::writeRAW(std::ostream &os) const { +Image::writeRAW(std::ostream &os) const +{ const unsigned char *row; for (row = start(); row != end(); row += stride()) { @@ -44,4 +48,17 @@ Image::writeRAW(std::ostream &os) const { } } + +bool +Image::writeRAW(const char *filename) const +{ + std::ofstream os(filename, std::ofstream::binary); + if (!os) { + return false; + } + writeRAW(os); + return true; +} + + } /* namespace image */ -- 2.43.0