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