OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimRgbToGreyFilter.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // Copyright (C) 2000 ImageLinks Inc.
3 //
4 // License: LGPL
5 //
6 // See LICENSE.txt file in the top level directory for more details.
7 //
8 // Author: Garrett Potts
9 //
10 //*************************************************************************
11 // $Id: ossimRgbToGreyFilter.cpp 19223 2011-03-24 13:38:21Z dburken $
12 
16 #include <ossim/base/ossimCommon.h>
18 
20 
22  :ossimImageSourceFilter(owner),
23  theBlankTile(NULL),
24  theTile(NULL),
25  theC1(1.0/3.0),
26  theC2(1.0/3.0),
27  theC3(1.0/3.0)
28 {
29 }
30 
32  double c1,
33  double c2,
34  double c3)
35  : ossimImageSourceFilter(NULL, inputSource),
36  theBlankTile(NULL),
37  theTile(NULL),
38  theC1(c1),
39  theC2(c2),
40  theC3(c3)
41 {
42 }
43 
45  ossimImageSource* inputSource,
46  double c1,
47  double c2,
48  double c3)
49  : ossimImageSourceFilter(owner, inputSource),
50  theBlankTile(NULL),
51  theTile(NULL),
52  theC1(c1),
53  theC2(c2),
54  theC3(c3)
55 {
56 }
58 {
59 }
60 
62  ossim_uint32 resLevel)
63 {
65  {
66  return NULL; // This filter requires an input.
67  }
68 
70  resLevel);
71  if(!isSourceEnabled() || !inputTile.valid())
72  {
73  return inputTile;
74  }
75 
76  if(!theTile)
77  {
78  allocate(); // First time through...
79  }
80 
81  // if (!theTile) // throw exeption...
82 
83  if( inputTile->getDataObjectStatus() == OSSIM_NULL ||
84  inputTile->getDataObjectStatus() == OSSIM_EMPTY )
85  {
87  return theBlankTile;
88  }
89 
90  // Set the origin, resize if needed of the output tile.
91  theTile->setImageRectangle(tileRect);
92 
93  // Filter the tile.
94  runUcharTransformation(inputTile);
95 
96  // Always validate to set the status.
97  theTile->validate();
98 
99  return theTile;
100 }
101 
102 
104 {
105  // Base class will recapture "theInputConnection".
107 }
108 
110 {
112  {
113  theTile = NULL;
114  theBlankTile = NULL;
115 
116  if(isSourceEnabled())
117  {
118  theBlankTile = new ossimU8ImageData(this,
119  1,
122 
123 
125  theTile->initialize();
126  }
127  }
128 }
129 
131 {
132  if(isSourceEnabled())
133  {
134  return 1;
135  }
136 
138 }
139 
141  const char* prefix)const
142 {
144 
145  kwl.add(prefix,
146  "c1",
147  theC1,
148  true);
149  kwl.add(prefix,
150  "c2",
151  theC2,
152  true);
153  kwl.add(prefix,
154  "c3",
155  theC2,
156  true);
157 
158  return true;
159 }
160 
162  const char* prefix)
163 {
165 
166  const char* lookup = kwl.find(prefix, "c1");
167  if(lookup)
168  {
169  theC1 = ossimString(lookup).toDouble();
170  }
171  lookup = kwl.find(prefix, "c2");
172  if(lookup)
173  {
174  theC2 = ossimString(lookup).toDouble();
175  }
176  lookup = kwl.find(prefix, "c3");
177  if(lookup)
178  {
179  theC3 = ossimString(lookup).toDouble();
180  }
181  return true;
182 }
183 
185 {
186  unsigned char** bandSrc = new unsigned char*[tile->getNumberOfBands()];
187  unsigned char* bandDest;
188 
189  if(tile->getNumberOfBands() == 1)
190  {
191  bandSrc[0] = static_cast<unsigned char*>(tile->getBuf(0));
192  bandSrc[1] = static_cast<unsigned char*>(tile->getBuf(0));
193  bandSrc[2] = static_cast<unsigned char*>(tile->getBuf(0));
194  }
195  else if(tile->getNumberOfBands() == 2)
196  {
197  bandSrc[0] = static_cast<unsigned char*>(tile->getBuf(0));
198  bandSrc[1] = static_cast<unsigned char*>(tile->getBuf(1));
199  bandSrc[2] = static_cast<unsigned char*>(tile->getBuf(1));
200  }
201  else if(tile->getNumberOfBands() >= 3)
202  {
203  bandSrc[0] = static_cast<unsigned char*>(tile->getBuf(0));
204  bandSrc[1] = static_cast<unsigned char*>(tile->getBuf(1));
205  bandSrc[2] = static_cast<unsigned char*>(tile->getBuf(2));
206  }
207  bandDest = static_cast<unsigned char*>(theTile->getBuf());
208 
209  ossim_int32 offset;
210 
211  ossim_int32 upperBound = tile->getWidth()*tile->getHeight();
212  for(offset = 0; offset < upperBound; ++offset)
213  {
214  ossim_int32 value;
215 
216  value = ossim::round<int>(theC1*(bandSrc[0][offset]) +
217  theC2*(bandSrc[1][offset]) +
218  theC3*(bandSrc[2][offset]));
219 
220  value = value<255?value:255;
221  value = value>0?value:0;
222 
223  bandDest[offset] = value;
224  }
225 
226  delete [] bandSrc;
227 }
228 
230 {
231  return ossimString("grey");
232 }
233 
235 {
236  return ossimString("rgb to grey scale filter");
237 }
238 
240 {
241  if(isSourceEnabled())
242  {
243  return OSSIM_UCHAR;
244  }
245 
247 }
virtual ossim_uint32 getWidth() const
virtual bool isSourceEnabled() const
Definition: ossimSource.cpp:79
ossimRgbToGreyFilter(ossimObject *owner=NULL)
virtual ossim_uint32 getNumberOfBands() const
ossimRefPtr< ossimImageData > theBlankTile
virtual void setImageRectangle(const ossimIrect &rect)
Represents serializable keyword/value map.
virtual ossim_uint32 getNumberOfOutputBands() const
Returns the number of bands in a tile returned from this TileSource.
bool valid() const
Definition: ossimRefPtr.h:75
const char * find(const char *key) const
virtual ossim_uint32 getTileHeight() const
Returns the default processing tile height.
ossimRefPtr< ossimImageData > theTile
void runUcharTransformation(ossimRefPtr< ossimImageData > &tile)
virtual ossimDataObjectStatus getDataObjectStatus() const
virtual ossim_uint32 getHeight() const
virtual void initialize()
Initialize the data buffer.
ossim_uint32 getNumberOfOutputBands() const
Returns the number of bands in a tile returned from this TileSource.
virtual ossim_uint32 getTileWidth() const
Returns the default processing tile width.
virtual ossimObject * dup() const
void add(const char *prefix, const ossimKeywordlist &kwl, bool overwrite=true)
virtual ossimDataObjectStatus validate() const
ossimImageSource * theInputConnection
unsigned int ossim_uint32
virtual ossimScalarType getOutputScalarType() const
This will be used to query the output pixel type of the tile source.
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
double toDouble() const
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
Method to the load (recreate) the state of an object from a keyword list.
void allocate()
Called on first getTile, will initialize all data needed.
ossimScalarType
virtual ossimScalarType getOutputScalarType() const
This will be used to query the output pixel type of the tile source.
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Method to save the state of an object to a keyword list.
virtual ossimString getLongName() const
virtual const void * getBuf() const
virtual ossimRefPtr< ossimImageData > getTile(const ossimIrect &tileRect, ossim_uint32 resLevel=0)
#define RTTI_DEF1(cls, name, b1)
Definition: ossimRtti.h:485
virtual ossimString getShortName() const
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Method to save the state of an object to a keyword list.
8 bit unsigned iteger
int ossim_int32
virtual ossimRefPtr< ossimImageData > getTile(const ossimIpt &origin, ossim_uint32 resLevel=0)