Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Channel.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Cinder Project, All rights reserved.
3  This code is intended for use with the Cinder C++ library: http://libcinder.org
4 
5  Portions Copyright (c) 2010, The Barbarian Group
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
9  the following conditions are met:
10 
11  * Redistributions of source code must retain the above copyright notice, this list of conditions and
12  the following disclaimer.
13  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
14  the following disclaimer in the documentation and/or other materials provided with the distribution.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
17  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
19  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
20  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23  POSSIBILITY OF SUCH DAMAGE.
24 */
25 
26 #pragma once
27 
28 #include "cinder/Cinder.h"
29 #include "cinder/Area.h"
30 
31 namespace cinder {
32 
33 typedef std::shared_ptr<class ImageSource> ImageSourceRef;
34 
36 template<typename T>
37 class ChannelT {
38  protected:
40  struct Obj {
41  Obj( int32_t width, int32_t height );
42  Obj( int32_t aWidth, int32_t aHeight, int32_t aRowBytes, uint8_t aIncrement, bool aOwnsData, T *aData );
43  ~Obj();
44 
45  int32_t mWidth, mHeight, mRowBytes;
46  T *mData;
47  uint8_t mIncrement;
48  bool mOwnsData;
49 
50  void (*mDeallocatorFunc)(void *refcon);
51  void *mDeallocatorRefcon;
52  };
54 
55  public:
57  ChannelT() {}
59  ChannelT( int32_t width, int32_t height );
61  ChannelT( int32_t width, int32_t height, int32_t rowBytes, uint8_t increment, T *data );
63  ChannelT( ImageSourceRef imageSource );
64 
65  operator ImageSourceRef() const;
66 
68  ChannelT clone( bool copyPixels = true ) const;
70  ChannelT clone( const Area &area, bool copyPixels = true ) const;
71 
73  int32_t getWidth() const { return mObj->mWidth; }
75  int32_t getHeight() const { return mObj->mHeight; }
77  Vec2i getSize() const { return Vec2i( mObj->mWidth, mObj->mHeight ); }
79  float getAspectRatio() const { return mObj->mWidth / (float)mObj->mHeight; }
81  Area getBounds() const { return Area( 0, 0, mObj->mWidth, mObj->mHeight ); }
83  int32_t getRowBytes() const { return mObj->mRowBytes; }
85  uint8_t getIncrement() const { return mObj->mIncrement; }
87  bool isPlanar() const { return mObj->mIncrement == 1; }
88 
90  T* getData() { return mObj->mData; }
92  const T* getData() const { return mObj->mData; }
94  T* getData( const Vec2i &offset ) { return reinterpret_cast<T*>( reinterpret_cast<unsigned char*>( mObj->mData + offset.x * mObj->mIncrement ) + offset.y * mObj->mRowBytes ); }
96  const T* getData( const Vec2i &offset ) const { return reinterpret_cast<T*>( reinterpret_cast<unsigned char*>( mObj->mData + offset.x * mObj->mIncrement ) + offset.y * mObj->mRowBytes ); }
98  T* getData( int32_t x, int32_t y ) { return reinterpret_cast<T*>( reinterpret_cast<unsigned char*>( mObj->mData + x * mObj->mIncrement ) + y * mObj->mRowBytes ); }
100  const T* getData( int32_t x, int32_t y ) const { return reinterpret_cast<T*>( reinterpret_cast<unsigned char*>( mObj->mData + x * mObj->mIncrement ) + y * mObj->mRowBytes ); }
101 
103  T getValue ( Vec2i pos ) const { pos.x = constrain<int32_t>( pos.x, 0, mObj->mWidth - 1); pos.y = constrain<int32_t>( pos.y, 0, mObj->mHeight - 1 ); return *getData( pos ); }
105  void setValue( Vec2i pos, T v ) { pos.x = constrain<int32_t>( pos.x, 0, mObj->mWidth - 1); pos.y = constrain<int32_t>( pos.y, 0, mObj->mHeight - 1 ); *getData( pos ) = v; }
106 
108  void copyFrom( const ChannelT<T> &srcChannel, const Area &srcArea, const Vec2i &relativeOffset = Vec2i::zero() );
109 
111  T areaAverage( const Area &area ) const;
112 
114  void setDeallocator( void(*aDeallocatorFunc)( void * ), void *aDeallocatorRefcon );
115 
117  typedef std::shared_ptr<Obj> ChannelT::*unspecified_bool_type;
118  operator unspecified_bool_type() const { return ( mObj.get() == 0 ) ? 0 : &ChannelT::mObj; }
119  void reset() { mObj.reset(); }
121 
123  class Iter {
124  public:
125  Iter( ChannelT<T> &channelT, const Area &area )
126  : mInc( channelT.getIncrement() ), mRowInc( channelT.getRowBytes() )
127  {
128  Area clippedArea( area.getClipBy( channelT.getBounds() ) );
129  mWidth = clippedArea.getWidth();
130  mHeight = clippedArea.getHeight();
131  mLinePtr = reinterpret_cast<uint8_t*>( channelT.getData( clippedArea.getUL() ) );
132  mPtr = reinterpret_cast<T*>( mLinePtr );
133  mStartX = mX = clippedArea.getX1();
134  mStartY = mY = clippedArea.getY1();
135  mEndX = clippedArea.getX2();
136  mEndY = clippedArea.getY2();
137  // in order to be at the right place after an initial call to line(), we need to back up one line
138  mY = clippedArea.getY1() - 1;
139  mLinePtr -= mRowInc;
140  }
141 
143  T& v() const { return *mPtr; }
145  T& v( int32_t xOff, int32_t yOff ) const { return mPtr[xOff * mInc + yOff * mRowInc]; }
147  T& vClamped( int32_t xOff, int32_t yOff ) const
148  { xOff = std::min(std::max(mX + xOff, mStartX),mEndX - 1) - mX; yOff = std::min(std::max( mY + yOff, mStartY ), mEndY - 1) - mY;
149  return *(T*)((uint8_t*)( mPtr + xOff * mInc ) + yOff * mRowInc); }
150 
152  const int32_t x() const { return mX; }
154  const int32_t y() const { return mY; }
156  Vec2i getPos() const { return Vec2i( mX, mY ); }
157 
159  bool pixel() {
160  ++mX;
161  mPtr += mInc;
162  return mX < mEndX;
163  }
164 
166  bool line() {
167  ++mY;
168  mLinePtr += mRowInc;
169  mPtr = reinterpret_cast<T*>( mLinePtr );
170  // in order to be at the right place after an initial call to pixel(), we need to back up one pixel
171  mPtr -= mInc;
172  mX = mStartX - 1;
173  return mY < mEndY;
174  }
175 
177  int32_t getWidth() { return mWidth; }
179  int32_t getHeight() { return mHeight; }
180 
182  uint8_t mInc;
183  uint8_t *mLinePtr;
184  T *mPtr;
185  int32_t mRowInc, mWidth, mHeight;
186  int32_t mX, mY, mStartX, mStartY, mEndX, mEndY;
188  };
189 
191  class ConstIter {
192  public:
193  ConstIter( const ChannelT<T> &channelT, const Area &area )
194  : mInc( channelT.getIncrement() ), mRowInc( channelT.getRowBytes() )
195  {
196  Area clippedArea( area.getClipBy( channelT.getBounds() ) );
197  mWidth = clippedArea.getWidth();
198  mHeight = clippedArea.getHeight();
199  mLinePtr = reinterpret_cast<const uint8_t*>( channelT.getData( clippedArea.getUL() ) );
200  mPtr = reinterpret_cast<const T*>( mLinePtr );
201  mStartX = mX = clippedArea.getX1();
202  mStartY = mY = clippedArea.getY1();
203  mEndX = clippedArea.getX2();
204  mEndY = clippedArea.getY2();
205  // in order to be at the right place after an initial call to line(), we need to back up one line
206  mY = clippedArea.getY1() - 1;
207  mLinePtr -= mRowInc;
208  }
209 
211  const T& v() const { return *mPtr; }
213  const T& v( int32_t xOff, int32_t yOff ) const { return mPtr[xOff * mInc + yOff * mRowInc]; }
215  const T& vClamped( int32_t xOff, int32_t yOff ) const
216  { xOff = std::min(std::max(mX + xOff, mStartX),mEndX - 1) - mX; yOff = std::min(std::max( mY + yOff, mStartY ), mEndY - 1) - mY;
217  return *(T*)((uint8_t*)( mPtr + xOff * mInc ) + yOff * mRowInc); }
218 
220  const int32_t x() const { return mX; }
222  const int32_t y() const { return mY; }
224  Vec2i getPos() const { return Vec2i( mX, mY ); }
225 
227  bool pixel() {
228  ++mX;
229  mPtr += mInc;
230  return mX < mEndX;
231  }
232 
234  bool line() {
235  ++mY;
236  mLinePtr += mRowInc;
237  mPtr = reinterpret_cast<const T*>( mLinePtr );
238  // in order to be at the right place after an initial call to pixel(), we need to back up one pixel
239  mPtr -= mInc;
240  mX = mStartX - 1;
241  return mY < mEndY;
242  }
243 
245  int32_t getWidth() { return mWidth; }
247  int32_t getHeight() { return mHeight; }
248 
250  uint8_t mInc;
251  const uint8_t *mLinePtr;
252  const T *mPtr;
253  int32_t mRowInc, mWidth, mHeight;
254  int32_t mX, mY, mStartX, mStartY, mEndX, mEndY;
256  };
257 
259  Iter getIter() { return Iter( *this, this->getBounds() ); }
261  Iter getIter( const Area &area ) { return Iter( *this, area ); }
263  ConstIter getIter() const { return ConstIter( *this, this->getBounds() ); }
265  ConstIter getIter( const Area &area ) const { return ConstIter( *this, area ); }
266 
267  protected:
268  std::shared_ptr<Obj> mObj;
269 };
270 
271 
280 
281 } // namespace cinder
int32_t getY1() const
Definition: Area.h:69
GLenum GLint GLint y
Definition: GLee.h:987
T * getData(const Vec2i &offset)
Returns a pointer to the data of the Channel's pixel at offset. Result is a uint8_t* for Channel8u an...
Definition: Channel.h:94
bool line()
Increments which row the Iter points to, and returns false when no rows remain in the Channel...
Definition: Channel.h:234
uint8_t getIncrement() const
Returns the amount to increment a T* to increment by a pixel. For a planar channel this is 1...
Definition: Channel.h:85
float getAspectRatio() const
Returns the Channel aspect ratio, which is its width / height.
Definition: Channel.h:79
int int * max
Definition: GLee.h:17208
int32_t getY2() const
Definition: Area.h:73
Definition: Area.h:37
int32_t getX2() const
Definition: Area.h:71
T & v(int32_t xOff, int32_t yOff) const
Returns a reference to the value of the pixel that the Iter currently points to, offset by (xOff...
Definition: Channel.h:145
int32_t getRowBytes() const
Returns the width of a row of the Channel measured in bytes, which is not necessarily getWidth() * ge...
Definition: Channel.h:83
Iter getIter(const Area &area)
Returns an Iter which iterates the Area area.
Definition: Channel.h:261
GLenum GLsizei width
Definition: GLee.h:969
const T & vClamped(int32_t xOff, int32_t yOff) const
Returns a reference to the value of the pixel that the Iter currently points to, offset by (xOff...
Definition: Channel.h:215
Convenience class for iterating the pixels of a Channel. The iteration is const, performing read-only...
Definition: Channel.h:191
ConstIter(const ChannelT< T > &channelT, const Area &area)
Definition: Channel.h:193
static Vec2< int > zero()
Definition: Vector.h:295
const T & v() const
Returns a reference to the value of the pixel that the Iter currently points to.
Definition: Channel.h:211
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: GLee.h:1011
Vec2i getSize() const
Returns the size of the Channel in pixels.
Definition: Channel.h:77
T x
Definition: Vector.h:71
Vec2i getUL() const
Definition: Area.h:75
typedef void(APIENTRYP GLEEPFNGLBLENDCOLORPROC)(GLclampf red
ChannelT< uint16_t > Channel16u
16-bit image channel. Suitable as an intermediate representation and ImageIo but not a first-class ci...
Definition: Channel.h:277
const T * getData(const Vec2i &offset) const
Returns a const pointer to the data of the Channel's pixel at offset. Result is a uint8_t* for Channe...
Definition: Channel.h:96
int32_t getHeight() const
Returns the height of the Channel in pixels.
Definition: Channel.h:75
int32_t getX1() const
Definition: Area.h:67
ChannelT< float > Channel32f
32-bit floating point image channel
Definition: Channel.h:279
#define min(a, b)
Definition: AppImplMsw.cpp:36
const int32_t y() const
Returns the y coordinate of the pixel the Iter currently points to.
Definition: Channel.h:154
ChannelT< uint8_t > Channel
8-bit image channel. Synonym for Channel8u.
Definition: Channel.h:273
bool isPlanar() const
Returns whether the Channel represents a tightly packed array of values. This will be false if the Ch...
Definition: Channel.h:87
int32_t getWidth() const
Definition: Area.h:47
GLenum GLsizei GLsizei height
Definition: GLee.h:1029
int32_t getWidth() const
Returns the width of the Channel in pixels.
Definition: Channel.h:73
T * getData(int32_t x, int32_t y)
Returns a pointer to the data of the Channel's pixel at (x, y). Result is a uint8_t* for Channel8u an...
Definition: Channel.h:98
const T * getData() const
Returns a const pointer to the data of the Channel's first pixel. Result is a uint8_t* for Channel8u ...
Definition: Channel.h:92
T areaAverage(const Area &area) const
Returns an averaged value for the Area defined by area.
Definition: Channel.cpp:225
void copyFrom(const ChannelT< T > &srcChannel, const Area &srcArea, const Vec2i &relativeOffset=Vec2i::zero())
Copies the Area srcArea of the Channel srcChannel to this Channel. The destination Area is srcArea of...
Definition: Channel.cpp:203
Area getClipBy(const Area &clip) const
Definition: Area.cpp:82
GLenum GLint x
Definition: GLee.h:987
bool pixel()
Increments which pixel of the current row the Iter points to, and returns false when no pixels remain...
Definition: Channel.h:227
Vec2i getPos() const
Returns the coordinate of the pixel the Iter currently points to.
Definition: Channel.h:156
GLintptr offset
Definition: GLee.h:2095
const int32_t y() const
Returns the y coordinate of the pixel the Iter currently points to.
Definition: Channel.h:222
A single channel of image data, either a color channel of a Surface or a grayscale image...
Definition: Channel.h:37
Vec2i getPos() const
Returns the coordinate of the pixel the Iter currently points to.
Definition: Channel.h:224
void setDeallocator(void(*aDeallocatorFunc)(void *), void *aDeallocatorRefcon)
Definition: Channel.cpp:175
const GLdouble * v
Definition: GLee.h:1384
Iter(ChannelT< T > &channelT, const Area &area)
Definition: Channel.h:125
void setValue(Vec2i pos, T v)
Convenience method for setting a single value v at pixel pos. For performance-sensitive code consider...
Definition: Channel.h:105
T y
Definition: Vector.h:71
ChannelT clone(bool copyPixels=true) const
Returns a new Channel which is a duplicate. If copyPixels the pixel values are copied, otherwise the clone's pixels remain uninitialized.
Definition: Channel.cpp:182
ConstIter getIter() const
Returns a ConstIter which iterates the entire Channel.
Definition: Channel.h:263
Iter getIter()
Returns an Iter which iterates the entire Channel.
Definition: Channel.h:259
ChannelT()
Constructs an empty Channel, which is the equivalent of NULL and should not be used directly...
Definition: Channel.h:57
T & v() const
Returns a reference to the value of the pixel that the Iter currently points to.
Definition: Channel.h:143
std::shared_ptr< Obj > mObj
Definition: Channel.h:268
T * getData()
Returns a pointer to the data of the Channel's first pixel. Result is a uint8_t* for Channel8u and a ...
Definition: Channel.h:90
const int32_t x() const
Returns the x coordinate of the pixel the Iter currently points to.
Definition: Channel.h:152
GLboolean reset
Definition: GLee.h:1101
Convenience class for iterating the pixels of a Channel.
Definition: Channel.h:123
T getValue(Vec2i pos) const
Convenience method for getting a single value at pixel pos. For performance-sensitive code consider C...
Definition: Channel.h:103
int32_t getHeight()
Returns the height of the Area the Iter iterates.
Definition: Channel.h:247
Area getBounds() const
Returns the bounding Area of the Channel in pixels: [0,0]-(width,height)
Definition: Channel.h:81
std::shared_ptr< class ImageSource > ImageSourceRef
Definition: Channel.h:33
int32_t getHeight()
Returns the height of the Area the Iter iterates.
Definition: Channel.h:179
const T * getData(int32_t x, int32_t y) const
Returns a const pointer to the data of the Channel's pixel at (x, y). Result is a uint8_t* for Channe...
Definition: Channel.h:100
bool pixel()
Increments which pixel of the current row the Iter points to, and returns false when no pixels remain...
Definition: Channel.h:159
ConstIter getIter(const Area &area) const
Returns a ConstIter which iterates the Area area.
Definition: Channel.h:265
T & vClamped(int32_t xOff, int32_t yOff) const
Returns a reference to the value of the pixel that the Iter currently points to, offset by (xOff...
Definition: Channel.h:147
const int32_t x() const
Returns the x coordinate of the pixel the Iter currently points to.
Definition: Channel.h:220
int32_t getWidth()
Returns the width of the Area the Iter iterates.
Definition: Channel.h:177
int32_t getWidth()
Returns the width of the Area the Iter iterates.
Definition: Channel.h:245
ChannelT< uint8_t > Channel8u
8-bit image channel
Definition: Channel.h:275
int32_t getHeight() const
Definition: Area.h:48
bool line()
Increments which row the Iter points to, and returns false when no rows remain in the Channel...
Definition: Channel.h:166
const T & v(int32_t xOff, int32_t yOff) const
Returns a reference to the value of the pixel that the Iter currently points to, offset by (xOff...
Definition: Channel.h:213
Vec2< int > Vec2i
Definition: Vector.h:1313