1 //
2 // This file is part of the SDTS++ toolkit, written by the U.S.
3 // Geological Survey. It is experimental software, written to support
4 // USGS research and cartographic data production.
5 //
6 // SDTS++ is public domain software. It may be freely copied,
7 // distributed, and modified. The USGS welcomes user feedback, but makes
8 // no committment to any level of support for this code. See the SDTS
9 // web site at http://mcmcweb.er.usgs.gov/sdts for more information,
10 // including points of contact.
11 //
12 // $Id: sb_Clrx.cpp,v 1.4 2002/11/24 22:07:42 mcoletti Exp $
13 //
14
15 #include "builder/sb_Clrx.h"
16
17
18
19 #include <iostream>
20 #include <strstream>
21
22 #include <limits.h>
23 #include <float.h>
24
25 #ifndef INCLUDED_SB_UTILS_H
26 #include "builder/sb_Utils.h"
27 #endif
28
29 #ifndef INCLUDED_SB_FOREIGNID_H
30 #include "builder/sb_ForeignID.h"
31 #endif
32
33 #ifndef INCLUDED_SC_RECORD_H
34 #include "container/sc_Record.h"
35 #endif
36
37 #ifndef INCLUDED_SC_FIELD_H
38 #include "container/sc_Field.h"
39 #endif
40
41 #ifndef INCLUDED_SC_SUBFIELD_H
42 #include "container/sc_Subfield.h"
43 #endif
44
45 #ifndef INCLUDED_SIO_8211CONVERTER_H
46 #include "io/sio_8211Converter.h"
47 #endif
48
49
50
51 using namespace std;
52
53
54 static const char* _ident = "$Id: sb_Clrx.cpp,v 1.4 2002/11/24 22:07:42 mcoletti Exp $";
55
56 // Strings and integers are initialized with these values; they are used
57 // to indicate whether a given module value has been assigned a value or not.
58
59 // (XXX I arbitrarily chose 0x4 as the sentinal value. I hate ad hoc crap.)
60
61 static const string UNVALUED_STRING(1, static_cast<string::value_type>(0x4) );
62
63 static const long UNVALUED_LONG = INT_MIN;
64
65 static const double UNVALUED_DOUBLE = DBL_MAX;
66
67 static const sb_ForeignID UNVALUED_FOREIGN_ID( UNVALUED_STRING,
68 -1 );
69
70 struct sb_Clrx_Imp
71 {
72 double _RedComponent;
73 double _GreenComponent;
74 double _BlueComponent;
75 double _BlackComponent;
76
77
sb_Clrx_Impsb_Clrx_Imp78 sb_Clrx_Imp()
79 :
80 _RedComponent( UNVALUED_DOUBLE ),
81 _GreenComponent( UNVALUED_DOUBLE ),
82 _BlueComponent( UNVALUED_DOUBLE ),
83 _BlackComponent( UNVALUED_DOUBLE )
84 {}
85
86 };
87
88
sb_Clrx()89 sb_Clrx::sb_Clrx()
90 : _imp( new sb_Clrx_Imp() )
91 {
92 setMnemonic("CLRX");
93 setID( 1 );
94
95
96 // insert static initializers
97
98 } // Clrx ctor
99
100
~sb_Clrx()101 sb_Clrx::~sb_Clrx()
102 {
103 delete _imp;
104 } // Clrx dtor
105
106
107
108
109 static sio_8211Converter_I converter_I; // XXX should define these in
110 static sio_8211Converter_A converter_A; // XXX sio_8211Converter.h
111 static sio_8211Converter_R converter_R;
112 static sio_8211Converter_C converter_C;
113
114 static sio_8211Schema _schema; // module specific schema
115
116 static
117 void
_build_schema(sio_8211Schema & schema)118 _build_schema( sio_8211Schema& schema )
119 {
120 schema.clear(); // make sure we are starting with clean schema
121
122 schema.push_back( sio_8211FieldFormat() );
123
124 sio_8211FieldFormat& field_format = schema.back();
125
126 field_format.setDataStructCode( sio_8211FieldFormat::vector );
127 field_format.setDataTypeCode( sio_8211FieldFormat::mixed_data_type );
128 field_format.setName( "Color Index" );
129 field_format.setTag( "CLRX" );
130
131
132 field_format.push_back( sio_8211SubfieldFormat() );
133
134 field_format.back().setLabel( "MODN" );
135 field_format.back().setType( sio_8211SubfieldFormat::A );
136 field_format.back().setFormat( sio_8211SubfieldFormat::variable );
137 field_format.back().setConverter( &converter_A );
138
139 field_format.push_back( sio_8211SubfieldFormat() );
140
141 field_format.back().setLabel( "RCID" );
142 field_format.back().setType( sio_8211SubfieldFormat::I );
143 field_format.back().setFormat( sio_8211SubfieldFormat::variable );
144 field_format.back().setConverter( &converter_I );
145
146 field_format.push_back( sio_8211SubfieldFormat() );
147
148 field_format.back().setLabel( "RED" );
149 field_format.back().setType( sio_8211SubfieldFormat::R );
150 field_format.back().setFormat( sio_8211SubfieldFormat::variable );
151 field_format.back().setConverter( &converter_R );
152
153
154 field_format.push_back( sio_8211SubfieldFormat() );
155
156 field_format.back().setLabel( "GREN" );
157 field_format.back().setType( sio_8211SubfieldFormat::R );
158 field_format.back().setFormat( sio_8211SubfieldFormat::variable );
159 field_format.back().setConverter( &converter_R );
160
161
162 field_format.push_back( sio_8211SubfieldFormat() );
163
164 field_format.back().setLabel( "BLUE" );
165 field_format.back().setType( sio_8211SubfieldFormat::R );
166 field_format.back().setFormat( sio_8211SubfieldFormat::variable );
167 field_format.back().setConverter( &converter_R );
168
169
170 field_format.push_back( sio_8211SubfieldFormat() );
171
172 field_format.back().setLabel( "BLCK" );
173 field_format.back().setType( sio_8211SubfieldFormat::R );
174 field_format.back().setFormat( sio_8211SubfieldFormat::variable );
175 field_format.back().setConverter( &converter_R );
176
177
178 } // _build_schema
179
180
181
182
183
184 void
buildSpecificSchema_()185 sb_Clrx::buildSpecificSchema_( )
186 {
187 if ( _schema.empty() )
188 {
189 _build_schema( _schema );
190 }
191 } // sb_Clrx::buildSpecificSchema_( )
192
193
194 sio_8211Schema &
schema_()195 sb_Clrx::schema_()
196 {
197 _build_schema( _schema );
198
199 return _schema;
200 } // sio_8211Schema& schema_()
201
202
203
204 static
205 bool
_ingest_record(sb_Clrx & clrx,sb_Clrx_Imp & clrx_imp,sc_Record const & record)206 _ingest_record( sb_Clrx& clrx, sb_Clrx_Imp &clrx_imp, sc_Record const& record )
207 {
208
209 // Make sure we have a record from an
210 // External Spatial Reference module.
211
212 sc_FieldCntr::const_iterator curfield;
213
214 if ( ! sb_Utils::getFieldByMnem( record,"CLRX",curfield) )
215 {
216 #ifdef SDTSXX_DEBUG
217 cerr << "sb_Clrx::sb_Clrx(sc_Record const&): "
218 << "Not an color index record.";
219 cerr << endl;
220 #endif
221 return false;
222 }
223
224
225 // We have a primary field from a module. Start// picking it apart.
226
227 sc_SubfieldCntr::const_iterator cursubfield;
228
229 string tmp_str;
230 long tmp_int;
231
232
233 // MODN
234 if (sb_Utils::getSubfieldByMnem(*curfield,"MODN",cursubfield))
235 {
236 cursubfield->getA( tmp_str );
237 clrx.setMnemonic( tmp_str );
238 }
239
240
241 // RCID
242 if (sb_Utils::getSubfieldByMnem(*curfield,"RCID",cursubfield))
243 {
244 cursubfield->getI( tmp_int );
245 clrx.setID( tmp_int );
246 }
247
248
249 // RED
250 if (sb_Utils::getSubfieldByMnem(*curfield,"RED",cursubfield))
251 {
252 cursubfield->getR( clrx_imp._RedComponent);
253 }
254 else
255 {
256 return false;
257 }
258
259
260 // GREN
261 if (sb_Utils::getSubfieldByMnem(*curfield,"GREN",cursubfield))
262 {
263 cursubfield->getR( clrx_imp._GreenComponent);
264 }
265 else
266 {
267 return false;
268 }
269
270
271 // BLUE
272 if (sb_Utils::getSubfieldByMnem(*curfield,"BLUE",cursubfield))
273 {
274 cursubfield->getR( clrx_imp._BlueComponent);
275 }
276 else
277 {
278 return false;
279 }
280
281
282 // BLCK
283 if (sb_Utils::getSubfieldByMnem(*curfield,"BLCK",cursubfield))
284 {
285 cursubfield->getR( clrx_imp._BlackComponent);
286 }
287 else
288 {
289 return false;
290 }
291
292
293 return true;
294
295
296 } // _ingest_record
297
298
299
300
301 bool
getRedComponent(double & val) const302 sb_Clrx::getRedComponent( double& val ) const
303 {
304 if ( _imp->_RedComponent == UNVALUED_DOUBLE )
305 return false;
306
307 val = _imp->_RedComponent;
308
309 return true;
310 } // sb_Clrx::getRedComponent
311
312
313 bool
getGreenComponent(double & val) const314 sb_Clrx::getGreenComponent( double& val ) const
315 {
316 if ( _imp->_GreenComponent == UNVALUED_DOUBLE )
317 return false;
318
319 val = _imp->_GreenComponent;
320
321 return true;
322 } // sb_Clrx::getGreenComponent
323
324
325 bool
getBlueComponent(double & val) const326 sb_Clrx::getBlueComponent( double& val ) const
327 {
328 if ( _imp->_BlueComponent == UNVALUED_DOUBLE )
329 return false;
330
331 val = _imp->_BlueComponent;
332
333 return true;
334 } // sb_Clrx::getBlueComponent
335
336
337 bool
getBlackComponent(double & val) const338 sb_Clrx::getBlackComponent( double& val ) const
339 {
340 if ( _imp->_BlackComponent == UNVALUED_DOUBLE )
341 return false;
342
343 val = _imp->_BlackComponent;
344
345 return true;
346 } // sb_Clrx::getBlackComponent
347
348
349 bool
getSchema(sio_8211Schema & schema) const350 sb_Clrx::getSchema( sio_8211Schema& schema ) const
351 {
352 // If the schema hasn't been
353 // initialized, please do so.
354
355 if ( _schema.empty() )
356 {
357 _build_schema( _schema );
358 }
359
360 if ( _schema.empty() ) // oops ... something screwed up
361 {
362 return false;
363 }
364
365 schema = _schema;
366
367 return true;
368
369 } // sb_Clrx::getSchema
370
371
372
373
374 bool
getRecord(sc_Record & record) const375 sb_Clrx::getRecord( sc_Record & record ) const
376 {
377 record.clear(); // start with a clean slate
378
379 // first field, which contains module name and record number
380
381 record.push_back( sc_Field() );
382
383 record.back().setMnemonic( "CLRX" );
384
385 record.back().setName( "Color Index" );
386
387 string tmp_str;
388 double tmp_double;
389 long tmp_long;
390
391 getMnemonic( tmp_str );
392 sb_Utils::add_subfield( record.back(), "MODN", tmp_str );
393 sb_Utils::add_subfield( record.back(), "RCID", getID() );
394
395 if ( getRedComponent( tmp_double ) )
396 {
397 sb_Utils::add_subfield( record.back(),"RED", tmp_double );
398 }
399 else
400 {
401 sb_Utils::add_empty_subfield( record.back(), "RED", sc_Subfield::is_R );
402 }
403
404
405 if ( getGreenComponent( tmp_double ) )
406 {
407 sb_Utils::add_subfield( record.back(),"GREN", tmp_double );
408 }
409 else
410 {
411 sb_Utils::add_empty_subfield( record.back(), "GREN", sc_Subfield::is_R );
412 }
413
414
415 if ( getBlueComponent( tmp_double ) )
416 {
417 sb_Utils::add_subfield( record.back(),"BLUE", tmp_double );
418 }
419 else
420 {
421 sb_Utils::add_empty_subfield( record.back(), "BLUE", sc_Subfield::is_R );
422 }
423
424
425 if ( getBlackComponent( tmp_double ) )
426 {
427 sb_Utils::add_subfield( record.back(),"BLCK", tmp_double );
428 }
429 else
430 {
431 sb_Utils::add_empty_subfield( record.back(), "BLCK", sc_Subfield::is_R );
432 }
433
434
435 return true;
436
437
438 } // Clrx::getRecord
439
440
441
442
443 bool
setRedComponent(double val)444 sb_Clrx::setRedComponent( double val )
445 {
446 _imp->_RedComponent = val;
447
448 return true;
449 } // sb_Clrx::setRedComponent
450
451
452 bool
setGreenComponent(double val)453 sb_Clrx::setGreenComponent( double val )
454 {
455 _imp->_GreenComponent = val;
456
457 return true;
458 } // sb_Clrx::setGreenComponent
459
460
461 bool
setBlueComponent(double val)462 sb_Clrx::setBlueComponent( double val )
463 {
464 _imp->_BlueComponent = val;
465
466 return true;
467 } // sb_Clrx::setBlueComponent
468
469
470 bool
setBlackComponent(double val)471 sb_Clrx::setBlackComponent( double val )
472 {
473 _imp->_BlackComponent = val;
474
475 return true;
476 } // sb_Clrx::setBlackComponent
477
478
479 bool
setRecord(sc_Record const & record)480 sb_Clrx::setRecord( sc_Record const& record )
481 {
482 return _ingest_record( *this, *_imp, record );
483 } // sb_Clrx::setRecord
484
485
486
487
488 void
unDefineRedComponent()489 sb_Clrx::unDefineRedComponent( )
490 {
491 _imp->_RedComponent = UNVALUED_DOUBLE;
492 } // sb_Clrx::unDefineRedComponent
493
494
495 void
unDefineGreenComponent()496 sb_Clrx::unDefineGreenComponent( )
497 {
498 _imp->_GreenComponent = UNVALUED_DOUBLE;
499 } // sb_Clrx::unDefineGreenComponent
500
501
502 void
unDefineBlueComponent()503 sb_Clrx::unDefineBlueComponent( )
504 {
505 _imp->_BlueComponent = UNVALUED_DOUBLE;
506 } // sb_Clrx::unDefineBlueComponent
507
508
509 void
unDefineBlackComponent()510 sb_Clrx::unDefineBlackComponent( )
511 {
512 _imp->_BlackComponent = UNVALUED_DOUBLE;
513 } // sb_Clrx::unDefineBlackComponent
514
515
516