source: GTP/trunk/Lib/Vis/Preprocessing/src/mixkit/MxRaster.h @ 1097

Revision 1097, 3.3 KB checked in by mattausch, 18 years ago (diff)
Line 
1#ifndef MXRASTER_INCLUDED // -*- C++ -*-
2#define MXRASTER_INCLUDED
3#if !defined(__GNUC__)
4#  pragma once
5#endif
6
7/************************************************************************
8
9  MxRaster
10
11  Copyright (C) 1998 Michael Garland.  See "COPYING.txt" for details.
12 
13  $Id: MxRaster.h,v 1.1 2002/09/24 16:53:54 wimmer Exp $
14
15 ************************************************************************/
16
17#include "MxBlock.h"
18
19class MxRaster : private MxBlock<unsigned char>
20{
21private:
22    uint W, H, nchan;
23
24public:
25
26    MxRaster(uint w, uint h, uint c)
27        : MxBlock<unsigned char>(w*h*c)  { W=w;  H=h;  nchan=c; }
28
29    uint width() const { return W; }
30    uint height() const { return H; }
31    uint channels() const { return nchan; }
32    uint length() const { return MxBlock<unsigned char>::length(); }
33
34#ifdef __GNUC__
35    unsigned char& operator[](uint i)       { return raw(i); }
36    unsigned char  operator[](uint i) const { return raw(i); }
37#endif
38    operator unsigned char *()
39        { return MxBlock<unsigned char>::operator unsigned char*(); }
40    operator const unsigned char *() const
41        { return MxBlock<unsigned char>::operator const unsigned char*(); }
42
43    unsigned char *pixel(uint i, uint j)
44        {
45            AssertBound(i<W && j<H);
46            return ((unsigned char *)*this) + (j*W + i)*nchan;
47        }
48
49    const unsigned char *pixel(uint i, uint j) const
50        {
51            AssertBound(i<W && j<H);
52            return ((const unsigned char *)*this) + (j*W + i)*nchan;
53        }
54
55    unsigned char *operator()(uint i, uint j) { return pixel(i,j); }
56    const unsigned char *operator()(uint i, uint j) const {return pixel(i,j);}
57
58    void reverse(uint start=0, uint end=MXID_NIL);
59    void hflip();
60    void vflip();
61};
62
63////////////////////////////////////////////////////////////////////////
64//
65// File I/O primitives for rasters.
66// The basic format supported is PNM files.  Support for this format
67// is built-in, and thus always available.
68//
69extern ostream& pnm_write_header(ostream&,const MxRaster&, bool is_raw=true);
70extern ostream& pnm_write_data(ostream&,const MxRaster&, bool is_raw=true);
71inline ostream& pnm_write(ostream& out, const MxRaster& r, bool is_raw=true)
72{ pnm_write_header(out,r,is_raw);  return pnm_write_data(out, r, is_raw); }
73
74extern MxRaster *pnm_read(istream&);
75
76// (Optional) support for TIFF files.
77// If libtiff is not available, these procedures will always fail.
78//
79extern bool tiff_write(const char *filename, const MxRaster&);
80extern MxRaster *tiff_read(const char *filename);
81
82////////////////////////////////////////////////////////////////////////
83//
84// OpenGL support
85//
86#ifdef MXGL_INCLUDED
87inline void glTexImage(const MxRaster& tex, GLenum target=GL_TEXTURE_2D)
88{
89    glTexImage2D(target, 0, 3, tex.width(), tex.height(),
90                 0, GL_RGB, GL_UNSIGNED_BYTE, (const unsigned char *)tex);
91}
92
93inline void gluMipmaps(const MxRaster& tex, GLenum target=GL_TEXTURE_2D)
94{
95    gluBuild2DMipmaps(target, 3, tex.width(), tex.height(),
96                      GL_RGB, GL_UNSIGNED_BYTE, (const unsigned char *)tex);
97}
98
99inline void glTexImage(const MxRaster *tex, GLenum target=GL_TEXTURE_2D)
100        { glTexImage(*tex, target); }
101inline void gluMipmaps(const MxRaster *tex, GLenum target=GL_TEXTURE_2D)
102        { gluMipmaps(*tex, target); }
103#endif
104
105// MXRASTER_INCLUDED
106#endif
Note: See TracBrowser for help on using the repository browser.