KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
i_pixel.h
Go to the documentation of this file.
1 
6 /*
7  * $Id: i_pixel.h 17393 2014-08-23 20:19:14Z predoehl $
8  */
9 
10 #ifndef KJB_CPP_PIXEL_H
11 #define KJB_CPP_PIXEL_H
12 
13 #include <l/l_sys_std.h>
14 #include <l/l_debug.h>
15 #include <l_cpp/l_util.h>
16 #include <i/i_float.h>
17 
18 #include <algorithm> /* for min and max */
19 #include <sstream>
20 #include <iomanip>
21 
22 namespace kjb
23 {
24 
30 std::string pixel_as_hex_triplet_string( const kjb_c::Pixel* );
31 
57 struct PixelRGBA : public kjb_c::Pixel
58 {
64  : kjb_c::Pixel()
65  {
66  // hit KJB(UNTESTED_CODE());
67  }
68 
75  PixelRGBA(const kjb_c::Pixel& p)
76  {
77  // hit KJB(UNTESTED_CODE());
78  r = p.r;
79  g = p.g;
80  b = p.b;
81  extra.alpha = p.extra.alpha;
82  }
83 
93  PixelRGBA( float rr, float gg, float bb, float aa = 255.0f )
94  {
95  // hit KJB(UNTESTED_CODE());
96  r = rr;
97  g = gg;
98  b = bb;
99  extra.alpha = aa;
100  }
101 
104  static PixelRGBA create_gray( float f )
105  {
106  // hit KJB(UNTESTED_CODE());
107  return PixelRGBA( f, f, f );
108  }
109 
110 
118  static
119  void ow_clamp_channel( float* chan )
120  {
121  // hit KJB(UNTESTED_CODE());
122  *chan = std::min( 255.0f, std::max( 0.0f, *chan ) );
123  }
124 
128  static
129  void ow_clamp_channel( float* chan, float minval, float maxval )
130  {
131  KJB(UNTESTED_CODE());
132  *chan = std::min( maxval, std::max( minval, *chan ) );
133  }
134 
144  {
145  // hit KJB(UNTESTED_CODE());
146  ow_clamp_channel( & this -> r );
147  ow_clamp_channel( & this -> g );
148  ow_clamp_channel( & this -> b );
149  ow_clamp_channel( & this -> extra.alpha );
150  return *this;
151  }
152 
157  PixelRGBA& ow_clamp( float minval, float maxval )
158  {
159  KJB(UNTESTED_CODE());
160  ow_clamp_channel( & this -> r, minval, maxval );
161  ow_clamp_channel( & this -> g, minval, maxval );
162  ow_clamp_channel( & this -> b, minval, maxval );
163  ow_clamp_channel( & this -> extra.alpha, minval, maxval );
164  return *this;
165  }
166 
173  PixelRGBA clamp() const
174  {
175  // hit KJB(UNTESTED_CODE());
176  PixelRGBA clamped( *this );
177  clamped.ow_clamp();
178  return clamped;
179  }
180 
184  PixelRGBA clamp( float minval, float maxval ) const
185  {
186  KJB(UNTESTED_CODE());
187  PixelRGBA clamped( *this );
188  clamped.ow_clamp( minval, maxval );
189  return clamped;
190  }
191 
197  {
198  KJB(UNTESTED_CODE());
199  r = floorf( r );
200  g = floorf( g );
201  b = floorf( b );
202  extra.alpha = floorf( extra.alpha );
203  return *this;
204  }
205 
209  PixelRGBA floor() const
210  {
211  KJB(UNTESTED_CODE());
212  PixelRGBA floored( *this );
213  floored.ow_floor();
214  return floored;
215  }
216 
222  PixelRGBA& operator+=( const kjb_c::Pixel& p )
223  {
224  // hit KJB(UNTESTED_CODE());
225  r += p.r;
226  g += p.g;
227  b += p.b;
228  extra.alpha += p.extra.alpha;
229  return *this;
230  }
231 
237  PixelRGBA operator+( const kjb_c::Pixel& p ) const
238  {
239  // hit KJB(UNTESTED_CODE());
240  PixelRGBA sum( *this );
241  sum += p;
242  return sum;
243  }
244 
253  {
254  // hit KJB(UNTESTED_CODE());
255  r *= p.r;
256  g *= p.g;
257  b *= p.b;
258  extra.alpha *= p.extra.alpha;
259  return *this;
260  }
261 
268  {
269  // hit KJB(UNTESTED_CODE());
270  r *= k;
271  g *= k;
272  b *= k;
273  extra.alpha *= k;
274  return *this;
275  }
276 
282  bool operator==( const kjb_c::Pixel& p ) const
283  {
284  KJB(UNTESTED_CODE());
285  return r==p.r && g==p.g && b==p.b && extra.alpha==p.extra.alpha;
286  }
287 
293  bool operator!=( const kjb_c::Pixel& p ) const
294  {
295  KJB(UNTESTED_CODE());
296  return ! operator==( p );
297  }
298 
300  std::string as_hex_triplet() const
301  {
302  return pixel_as_hex_triplet_string( this );
303  }
304 };
305 
306 
314 inline
316 {
317  // hit KJB(UNTESTED_CODE());
318  PixelRGBA product = p;
319  product *= q;
320  return product;
321 }
322 
328 inline
329 PixelRGBA operator*( const PixelRGBA& p, double k )
330 {
331  // hit KJB(UNTESTED_CODE());
332  PixelRGBA scaled = p;
333  scaled *= k;
334  return scaled;
335 }
336 
342 inline
343 PixelRGBA operator*( double k, const PixelRGBA& p )
344 {
345  // hit KJB(UNTESTED_CODE());
346  return p*k;
347 }
348 
349 
353 inline
354 kjb_c::Pixel abs(const kjb_c::Pixel& p)
355 {
356  kjb_c::Pixel q;
357  q.r = fabsf(p.r);
358  q.g = fabsf(p.g);
359  q.b = fabsf(p.b);
360 
361  return q;
362 }
363 
365 inline
366 std::string pixel_as_hex_triplet_string( const kjb_c::Pixel* p )
367 {
368  kjb::PixelRGBA q( *p );
369  q.ow_clamp();
370  std::ostringstream trip;
371  trip << "#" << std::hex << std::setfill('0')
372  << std::setw(2) << int(q.r)
373  << std::setw(2) << int(q.g)
374  << std::setw(2) << int(q.b);
375  return trip.str();
376 }
377 
379 inline
380 std::string pixel_as_hex_triplet_string( const kjb_c::Pixel& p )
381 {
382  return pixel_as_hex_triplet_string( &p );
383 }
384 
386 
387 } //namespace kjb
388 
389 #endif
PixelRGBA(float rr, float gg, float bb, float aa=255.0f)
This builds a valid pixel with given values for color channels.
Definition: i_pixel.h:93
PixelRGBA & operator*=(float k)
Overwrite a pixel by scaling its value (r, g, b, and alpha too)
Definition: i_pixel.h:267
Int_matrix::Value_type max(const Int_matrix &mat)
Return the maximum value in this matrix.
Definition: l_int_matrix.h:1397
PixelRGBA floor() const
Return new pixel with integer channel values.
Definition: i_pixel.h:209
PixelRGBA operator+(const kjb_c::Pixel &p) const
This will add corresponding channels, creating a new pixel.
Definition: i_pixel.h:237
#define KJB(x)
Definition: l_util.h:9
for k
Definition: APPgetLargeConnectedEdges.m:61
r
Definition: APPgetLargeConnectedEdges.m:127
PixelRGBA & operator+=(const kjb_c::Pixel &p)
Adds another pixel (channel-wise) to this pixel, overwriting.
Definition: i_pixel.h:222
bool operator!=(const kjb_c::Pixel &p) const
Test for ANY pixel inequality in any channel.
Definition: i_pixel.h:293
PixelRGBA()
Default ctor, which leaves all fields uninitialized.
Definition: i_pixel.h:63
std::string as_hex_triplet() const
express RGB values in an HTML-style string
Definition: i_pixel.h:300
kjb_c::Pixel abs(const kjb_c::Pixel &p)
Take the channel-wise absolute value of a kjb_c::Pixel.
Definition: i_pixel.h:354
PixelRGBA & ow_clamp(float minval, float maxval)
Clamp all channels to a specified range, overwriting.
Definition: i_pixel.h:157
PixelRGBA clamp() const
Return a new pixel with all channels clamped to range 0..255.
Definition: i_pixel.h:173
PixelRGBA(const kjb_c::Pixel &p)
This builds a valid pixel from another.
Definition: i_pixel.h:75
Wrapped version of the C struct Pixel, with Alpha (opacity).
Definition: i_pixel.h:57
static void ow_clamp_channel(float *chan)
Clamp one channel of the Pixel to the range 0..255 inclusive.
Definition: i_pixel.h:119
sum(zmx.*zmy) sum(zmy.^2)]
PixelRGBA & operator*=(const PixelRGBA &p)
Overwrite a pixel by multiplying it by p.
Definition: i_pixel.h:252
Int_matrix::Value_type min(const Int_matrix &mat)
Return the minimum value in this matrix.
Definition: l_int_matrix.h:1385
PixelRGBA & ow_clamp()
Clamp all channels of the pixel to the range 0..255 inclusive.
Definition: i_pixel.h:143
PixelRGBA clamp(float minval, float maxval) const
Return a new pixel with all channels clamped; non-overwriting.
Definition: i_pixel.h:184
std::string pixel_as_hex_triplet_string(const kjb_c::Pixel *)
express color as HTML-style hex triplet, e.g., "#FFCC00," of pointer
Definition: i_pixel.h:366
static PixelRGBA create_gray(float f)
A "named constructor" to build a single opaque grayscale pixel.
Definition: i_pixel.h:104
static void ow_clamp_channel(float *chan, float minval, float maxval)
Clamp one channel to a specified range.
Definition: i_pixel.h:129
Gsl_Vector operator*(double scalar, const Gsl_Vector &vector)
multiply scalar and vector, scalar written on the left side
Definition: gsl_vector.h:661
PixelRGBA & ow_floor()
Truncate pixel channel values to integers; overwriting.
Definition: i_pixel.h:196
bool operator==(const kjb_c::Pixel &p) const
Test for EXACT pixel equality in all channels.
Definition: i_pixel.h:282