From 2a259c5ea3417a0d9ddd60228478b7c59be55e8e Mon Sep 17 00:00:00 2001 From: schodet Date: Sun, 13 Mar 2005 19:43:49 +0000 Subject: Ajout png_writer. --- 2004/i/nono/src/image/image_writer.h | 38 +++++++++++++++++ 2004/i/nono/src/image/png_writer.cc | 80 ++++++++++++++++++++++++++++++++++++ 2004/i/nono/src/image/png_writer.h | 41 ++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 2004/i/nono/src/image/image_writer.h create mode 100644 2004/i/nono/src/image/png_writer.cc create mode 100644 2004/i/nono/src/image/png_writer.h (limited to '2004') diff --git a/2004/i/nono/src/image/image_writer.h b/2004/i/nono/src/image/image_writer.h new file mode 100644 index 0000000..46e1ec8 --- /dev/null +++ b/2004/i/nono/src/image/image_writer.h @@ -0,0 +1,38 @@ +#ifndef image_writer_h +#define image_writer_h +// image_writer.h +// nono - programme du robot 2004. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// Robot APB Team/Efrei 2004. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program 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 2 of the License, or +// (at your option) any later version. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} + +/// Classe de base d'un chargeur d'image. +class ImageWriter +{ + public: + /// Destructeur. + virtual ~ImageWriter (void) { } + /// Écrit une image, retourne le nombre d'octets écrits. + virtual int write (void *buf, int size, int width, int height) const = 0; +}; + +#endif // image_writer_h diff --git a/2004/i/nono/src/image/png_writer.cc b/2004/i/nono/src/image/png_writer.cc new file mode 100644 index 0000000..347eca3 --- /dev/null +++ b/2004/i/nono/src/image/png_writer.cc @@ -0,0 +1,80 @@ +// png_writer.cc +// nono - programme du robot 2004. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// Robot APB Team/Efrei 2004. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program 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 2 of the License, or +// (at your option) any later version. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "png_writer.h" +#include "utils/errno_exception.h" + +#include +#include + +/// Constructeur. +PngWriter::PngWriter (const std::string &filename) + : filename_ (filename) +{ +} + +/// Écrit une image, retourne le nombre d'octets écrits. +int +PngWriter::write (void *buf, int size, int width, int height) const +{ + int y; + unsigned char *p; + FILE *fd; + png_infop info_ptr; + png_structp png_ptr; + int depth = 3; + // Ouvre le fichier. + fd = fopen(filename_.c_str (), "w+"); + if (!fd) + throw std::runtime_error (filename_); + // Crée le structure PNG. + png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0); + if (!png_ptr) + return 0; + info_ptr = png_create_info_struct (png_ptr); + if (!info_ptr) + { + png_destroy_write_struct (&png_ptr, 0); + return 0; + } + // Initialise. + png_init_io (png_ptr, fd); + png_set_IHDR (png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + png_set_bgr (png_ptr); + png_write_info (png_ptr, info_ptr); + // Écrit l'image. + p = reinterpret_cast (buf); + for (y = 0; y < height; y++) { + png_write_row (png_ptr, p); + p += width * depth; + } + // Fin. + png_write_end (png_ptr, info_ptr); + png_destroy_write_struct (&png_ptr, &info_ptr); + fclose (fd); + return size; +} + diff --git a/2004/i/nono/src/image/png_writer.h b/2004/i/nono/src/image/png_writer.h new file mode 100644 index 0000000..ac0dc19 --- /dev/null +++ b/2004/i/nono/src/image/png_writer.h @@ -0,0 +1,41 @@ +#ifndef png_writer_h +#define png_writer_h +// png_writer.h +// nono - programme du robot 2004. {{{ +// +// Copyright (C) 2004 Nicolas Schodet +// +// Robot APB Team/Efrei 2004. +// Web: http://assos.efrei.fr/robot/ +// Email: robot AT efrei DOT fr +// +// This program 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 2 of the License, or +// (at your option) any later version. +// +// This program 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 this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +// }}} +#include "image_writer.h" + +#include + +class PngWriter : public ImageWriter +{ + std::string filename_; + public: + /// Constructeur. + PngWriter (const std::string &filename); + /// Écrit une image, retourne le nombre d'octets écrits. + int write (void *buf, int size, int width, int height) const; +}; + +#endif // png_writer_h -- cgit v1.2.3