Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

pnm_image.H

Go to the documentation of this file.
00001 /*
00002  * Copyright 1998, Brown University, Providence, RI.
00003  * 
00004  *                         All Rights Reserved
00005  * 
00006  * Permission to use, copy, modify, and distribute this software and its
00007  * documentation for any purpose other than its incorporation into a
00008  * commercial product is hereby granted without fee, provided that the
00009  * above copyright notice appear in all copies and that both that
00010  * copyright notice and this permission notice appear in supporting
00011  * documentation, and that the name of Brown University not be used in
00012  * advertising or publicity pertaining to distribution of the software
00013  * without specific, written prior permission.
00014  * 
00015  * BROWN UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
00016  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
00017  * PARTICULAR PURPOSE.  IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE FOR
00018  * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00019  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00020  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00021  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00022  */
00023 /**********************************************************************
00024  * image.H
00025  **********************************************************************/
00026 #ifndef IMAGE_H_HAS_BEEN_INCLUDED
00027 #define IMAGE_H_HAS_BEEN_INCLUDED
00028 
00029 #define OUTSIDE_GLUE_CORE
00030 
00031 #include "std/config.H"
00032 #include <math.h>
00033 #include "std/list.H"
00034 #include "std/strings.H"
00035 #include "mlib/point2i.H"
00036 
00037 //
00038 //  getPower2Size - returns smallest power of two that is >= size
00039 //
00040 inline unsigned int
00041 getPower2Size(int n)
00042 {
00043    return (1 << ((n>0) ? (int)ceil(log((double) n)/log(2.0)) : 0));
00044 }
00045 
00046 typedef unsigned int  uint;
00047 typedef unsigned char uchar;
00048 
00049 typedef const class Image cImage;
00050 /**********************************************************************
00051  * Image:
00052  *
00053  *    Contains an image stored bottom-up (the first row in the image is
00054  *    the row that appears at the bottom of the image) that can be read or
00055  *    written in pnm or png format
00056  *    
00057  **********************************************************************/
00058 //class DllImpExp Image {
00059 class Image {
00060  protected:
00061    uint         _width;         // width
00062    uint         _height;        // height
00063    uint         _bpp;           // bytes per pixel
00064    uchar*       _data;          // pixel data
00065    bool         _no_delete;
00066 
00067    enum { PNG_BYTES_TO_CHECK = 8 };
00068    FILE* open_png(char* file);
00069 
00070    str_ptr      _file;
00071 
00072  public:
00073    Image() : _width(0), _height(0), _bpp(1), _data(0), _no_delete(0) {}
00074    Image(Cstr_ptr& file) :
00075       _width(0),
00076       _height(0),
00077       _bpp(1),
00078       _data(0),
00079       _no_delete(0)
00080      {
00081       if (file)
00082          load_file(file);
00083    }
00084    Image(cImage &img) { *this = img; }
00085    Image(uint w, uint h, uint bpp, uchar* data, bool nd=1) {
00086       set(w,h,bpp,data,nd);
00087    }
00088    Image(uint w, uint h, uint bpp) :
00089       _width(0), _height(0), _bpp(1), _data(0), _no_delete(0) {
00090       resize(w,h,bpp);
00091    }
00092 
00093    void operator=(const Image &img) {
00094       if (img._file) {
00095          _data = 0; clear();
00096          load_file(img._file);
00097       } else {
00098          set(img._width, img._height, img._bpp, img.copy(), true);
00099       }
00100    }
00101 
00102    void set(int w, int h, uint bpp, uchar* data, bool nd=1) {
00103       if (bpp < 1 || bpp > 4) {
00104          err_msg("Image::Image: %d bytes/pixel not supported", bpp);
00105          clear();
00106       } else {
00107          clear();
00108          _width = w;
00109          _height = h;
00110          _bpp = bpp;
00111          _data = data;
00112          _no_delete = nd;
00113       }
00114    }
00115 
00116    void clear() {
00117       if (!_no_delete)
00118          delete [] _data;
00119       _width = _height = 0;
00120       _bpp = 1;
00121       _data  = 0;
00122       _no_delete = 0;
00123       _file = str_ptr::null_str();
00124    }
00125 
00126    int resize(uint w, uint h, uint b) {
00127       // if current size is different from
00128       // desired size:
00129       //   let go current data (if any)
00130       //   set new width, height and bpp
00131       //   allocate new data
00132       //   but don't initialize it
00133       if (_width != w || _height != h || _bpp != b) {
00134          _width = w;
00135          _height = h;
00136          _bpp = b;
00137          if (!_no_delete)
00138             delete [] _data;
00139          _data = 0;
00140          _no_delete = 0;
00141          if ((_data = new uchar [ size() ]) == 0) {
00142             err_ret("Image::resize: can't allocate data");
00143             return 0;
00144          }
00145       }
00146       return 1;
00147    }
00148 
00149    int copy_tile(const Image& tile, uint i, uint j);
00150 
00151    virtual ~Image() { clear(); }
00152 
00153    // accessors:
00154    int       width()    const { return _width; }
00155    int       height()   const { return _height; }
00156    uint      bpp()      const { return _bpp; }
00157    int       size()     const { return _width*_height*_bpp; }
00158    int       row_size() const { return _width*_bpp; }
00159    point2i   dims()     const { return point2i(_width,_height); }
00160    uchar*    data()     const { return _data; }
00161    uchar*    row(int k) const { return _data + k*row_size(); }
00162    Cstr_ptr &file()     const { return _file;}
00163    uchar*    copy()     const;
00164 
00165    bool empty() const { return !(_width && _height && _bpp && _data); }
00166 
00167    void expand_power2();
00168    int  resize_rows_mult_4();
00169    int  load_file(Cstr_ptr &file);
00170 
00171    int  read_png(Cstr_ptr &file);
00172    int  write_png(char* file) const;
00173 
00174    int  read_pnm (Cstr_ptr &file);
00175    int  read_pnm (istream &in);
00176    int  read_pgm (istream& in, bool ascii);
00177    int  read_ppm (istream& in, bool ascii);
00178    int  write_pnm(char* file,  bool ascii=false) const;
00179    int  write_pnm(ostream &os, bool ascii) const;
00180 
00181    static int write_png(int w, int h, uint bpp, uchar* data, Cstr_ptr& file) {
00182       Image img(w,h,bpp,data,1);
00183       return img.write_png(**file);
00184    }
00185    friend ostream & operator<<(ostream &os, cImage &img);
00186    friend istream & operator>>(istream &is,  Image &img);
00187 };
00188 
00189 
00190 #endif  // IMAGE_H_HAS_BEEN_INCLUDED
00191 /* end of file image.H */

Generated on Mon Sep 15 16:27:56 2003 for inspace by doxygen1.2.18