AGX Dynamics  2.37.3.2
ImageValueInterpreter.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGXIO_IMAGEVALUEINTERPRETER_H
18 #define AGXIO_IMAGEVALUEINTERPRETER_H
19 
20 #include <agxIO/Image.h>
21 #include <agx/Vec4.h>
22 
23 namespace agxIO
24 {
25  typedef float (*GetValueFunction)( float val );
26  typedef float (*ReadValue01Function)( agxIO::Image* image, size_t x, size_t y );
27 
28  inline float linearValueFunction( float val )
29  {
30  return val;
31  }
32 
33  inline float quadraticValueFunction( float val )
34  {
35  return val*val;
36  }
37 
38  inline float cubicValueFunction( float val )
39  {
40  return val*val*val;
41  }
42 
43  inline float readOneOrTwoChannelValue01( agxIO::Image* image, size_t x, size_t y )
44  {
45  size_t pos = (size_t)image->getWidth() * y + x;
46  pos *= (size_t)image->getChannels();
47  return 0.00392156863f * image->getData()[ pos ]; // unsigned char / 255.0.
48  }
49 
50  inline float readThreeChannelMergedValue01( agxIO::Image* image, size_t x, size_t y )
51  {
52  const unsigned char* data = image->getData();
53  size_t pos = (size_t)image->getWidth() * y + x;
54  pos *= 3;
55  return 0.00392156863f * float(( 77 * data[ pos ] + 150 * data[ pos + 1 ] + 29 * data[ pos + 2 ] ) >> 8);
56  }
57 
58  inline float readFourChannelMergedValue01( agxIO::Image* image, size_t x, size_t y )
59  {
60  const unsigned char* data = image->getData();
61  size_t pos = (size_t)image->getWidth() * y + x;
62  pos *= 4;
63  return 0.00392156863f * float(( 77 * data[ pos ] + 150 * data[ pos + 1 ] + 29 * data[ pos + 2 ] ) >> 8);
64  }
65 
66  inline float readSingleChannelValue01( agxIO::Image* image, size_t x, size_t y, size_t channel )
67  {
68  const unsigned char* data = image->getData();
69  size_t pos = (size_t)image->getWidth() * y + x;
70  pos *= (size_t)image->getChannels();
71  return 0.00392156863f * data[ pos + channel ];
72  }
73 
74  inline void writeSingleChannelValue01( unsigned char val, agxIO::Image* image, size_t x, size_t y, size_t channel )
75  {
76  unsigned char* data = image->getData();
77  size_t pos = (size_t)image->getWidth() * y + x;
78  pos *= (size_t)image->getChannels();
79  data[ pos + channel ] = val;
80  }
81 
83  {
84  public:
89 
95  PNGImageValueInterpreter( float minValue, float maxValue );
96 
101  bool read( const std::string& filename );
102 
106  bool valid() const;
107 
112  void setMaxValue( float maxValue );
113 
118  void setMinValue( float minValue );
119 
123  float getMaxValue() const;
124 
128  float getMinValue() const;
129 
133  agxIO::Image* getImage() const;
134 
138  size_t getImageHeight() const;
139 
143  size_t getImageWidth() const;
144 
154  float getValue( size_t x, size_t y, GetValueFunction func = linearValueFunction ) const;
155 
163  float getValueR( size_t x, size_t y, GetValueFunction func = linearValueFunction ) const;
164 
173  float getValueG( size_t x, size_t y, GetValueFunction func = linearValueFunction ) const;
174 
183  float getValueB( size_t x, size_t y, GetValueFunction func = linearValueFunction ) const;
184 
194  float getValueA( size_t x, size_t y, GetValueFunction func = linearValueFunction ) const;
195 
203  agx::Vec3f getValueRGB( size_t x, size_t y, GetValueFunction func = linearValueFunction ) const;
204 
213  agx::Vec4f getValueRGBA( size_t x, size_t y, GetValueFunction func = linearValueFunction ) const;
214 
219  void setValueR( unsigned char val, size_t x, size_t y );
220 
225  void setValueG( unsigned char val, size_t x, size_t y );
226 
231  void setValueB( unsigned char val, size_t x, size_t y );
232 
237  void setValueA( unsigned char val, size_t x, size_t y );
238 
239  protected:
243  virtual ~PNGImageValueInterpreter();
244 
245  protected:
247  float m_minValue;
248  float m_maxValue;
250  };
251 
253 }
254 
255 #endif
bool AGXPHYSICS_EXPORT read(const std::string &filename, agxStream::SerializablePtrVector &readObjects)
Open and read serializable objects from a file on disk with previously stored objects.
float readSingleChannelValue01(agxIO::Image *image, size_t x, size_t y, size_t channel)
The agxIO namespace contains classes for reading, writing and finding files.
Definition: Material.h:41
float(* ReadValue01Function)(agxIO::Image *image, size_t x, size_t y)
agx::ref_ptr< PNGImageValueInterpreter > PNGImageValueInterpreterRef
A class holding 4 dimensional vectors and providing basic arithmetic.
Definition: Vec4Template.h:34
#define AGXPHYSICS_EXPORT
Base class providing referencing counted objects.
Definition: Referenced.h:119
float cubicValueFunction(float val)
unsigned char * getData()
unsigned int getChannels() const
float quadraticValueFunction(float val)
unsigned int getWidth() const
float readThreeChannelMergedValue01(agxIO::Image *image, size_t x, size_t y)
float readFourChannelMergedValue01(agxIO::Image *image, size_t x, size_t y)
float(* GetValueFunction)(float val)
float linearValueFunction(float val)
float readOneOrTwoChannelValue01(agxIO::Image *image, size_t x, size_t y)
Class to represent Images.
Definition: Image.h:32
void writeSingleChannelValue01(unsigned char val, agxIO::Image *image, size_t x, size_t y, size_t channel)