00001 /* 00002 * pgmppm.h 00003 * 00004 * Copyright (C) 2008 Thomas A. Vaughan 00005 * All rights reserved. 00006 * 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions are met: 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * * Neither the name of the <organization> nor the 00016 * names of its contributors may be used to endorse or promote products 00017 * derived from this software without specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY 00020 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00022 * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY 00023 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00026 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 * 00030 * 00031 */ 00032 00033 #ifndef WAVEPACKET_PGMPPM_H__ 00034 #define WAVEPACKET_PGMPPM_H__ 00035 00036 // includes -------------------------------------------------------------------- 00037 #include "common/common.h" 00038 00039 #include <iostream> 00040 00041 00042 namespace pgmppm { 00043 00044 00045 /// \ingroup media 00046 /*@{*/ 00047 00048 //////////////////////////////////////////////////////////////////////////////// 00049 /// 00050 /// \defgroup pgmppm PGM/PPM Library 00051 /// 00052 /// My own library to read/write pgm/ppm files. 00053 /// See internet searches for libnetpbm and the PGM/PPM file formats. 00054 /// 00055 /// The authority: http://netpbm.sourceforge.net/doc/pgm.html 00056 /// 00057 /// WHY did I write my own? Partly because it is simple, and partly because I 00058 /// wanted to base it on std::iostreams so I could break any dependency on files. 00059 /// For instance, gzipped files, or in-memory data, etc. 00060 //////////////////////////////////////////////////////////////////////////////// 00061 00062 //////////////////////////////////////////////////////////////////////////////// 00063 /// 00064 /// \ingroup pgmppm 00065 /// \defgroup pgmppm_write File Writing 00066 /// 00067 /// To write a file, the caller specifies the dimensions and a callback. 00068 /// The file-writing routine will call back to retrieve per-pixel info. 00069 /// 00070 /// These are designed to be safe and agnostic to image storage, not fast. 00071 /// 00072 //////////////////////////////////////////////////////////////////////////////// 00073 /*@{*/ 00074 00075 typedef int (*pgm_pixel_fn)(IN void * context, IN int x, IN int y); 00076 00077 /// write a pgm file to an output stream 00078 void writePgm(IO std::ostream& out, 00079 IN int width, 00080 IN int height, 00081 IN int max_gray, // 0 < m_g < 65536 00082 IN pgm_pixel_fn fn, 00083 IN void * context); 00084 00085 00086 struct color_t { 00087 int red; 00088 int green; 00089 int blue; 00090 }; 00091 00092 typedef color_t (*ppm_pixel_fn)(IN void * context, IN int x, IN int y); 00093 00094 void writePpm(IO std::ostream& out, 00095 IN int width, 00096 IN int height, 00097 IN int max_color, // 0 < max_color < 65536 00098 IN ppm_pixel_fn fn, 00099 IN void * context); 00100 00101 /*@}*/ 00102 00103 00104 //////////////////////////////////////////////////////////////////////////////// 00105 /// 00106 /// \ingroup pgmppm 00107 /// \defgroup pgmppm_read File Reading 00108 /// 00109 /// Caller provides the stream to be read, and two callbacks. The first 00110 /// callback (notify_size) lets the caller know the size of the image. The 00111 /// second callback (write_pixel) is called per pixel in the image. 00112 /// 00113 /// Again, designed for reusability and safety, not speed. 00114 /// 00115 //////////////////////////////////////////////////////////////////////////////// 00116 /*@{*/ 00117 00118 00119 // return true if okay, false if read should be cancelled 00120 typedef bool (*notify_size_fn)(IN void * context, 00121 IN int width, 00122 IN int length, 00123 IN int max_val); 00124 00125 typedef void (*pgm_write_pixel_fn)(IN void * context, 00126 IN int x, IN int y, IN int height); 00127 00128 bool readPgm(IO std::istream& in, 00129 IN notify_size_fn notify_size, 00130 IN pgm_write_pixel_fn write_pixel, 00131 IN void * context); 00132 00133 00134 00135 }; // pgmppm namespace 00136 00137 #endif // WAVEPACKET_PGMPPM_H__ 00138