UltraScan III
us_colorgradIO.cpp
Go to the documentation of this file.
1 
3 #include "us_colorgradIO.h"
4 #include "us_defines.h"
5 
6 int US_ColorGradIO::read_color_gradient( QString xmlfilename,
7  QList< QColor >& gcolors )
8 {
9  int rc = XFS_OK;
10  gcolors.clear(); // clear out the list
11  QList< int > cparams; // color file attrib values list
12 
13  // get the integer quadruples for all the color steps
14  rc = read_color_step_params( xmlfilename, cparams );
15 
16  if ( rc == XFS_OK )
17  {
18  int nvals = cparams.size();
19 
20  int sred; // start RGB of step
21  int sgrn;
22  int sblu;
23  int ered; // end RGB of step
24  int egrn;
25  int eblu;
26  int npts;
27 
28  for ( int jj = 0; jj < nvals; jj+= 4 )
29  { // read each quadruple as obtained from the XML
30  ered = cparams.at( jj );
31  egrn = cparams.at( jj+1 );
32  eblu = cparams.at( jj+2 );
33  npts = cparams.at( jj+3 );
34 
35  if ( npts > 0 )
36  { // a step entry: expand all color points
37  qreal dpts = (qreal)npts;
38  // get delta values between color points in the step
39  qreal dred = (qreal)( ered - sred ) / dpts;
40  qreal dgrn = (qreal)( egrn - sgrn ) / dpts;
41  qreal dblu = (qreal)( eblu - sblu ) / dpts;
42  // start with the previous step's end
43  qreal ored = (qreal)sred;
44  qreal ogrn = (qreal)sgrn;
45  qreal oblu = (qreal)sblu;
46 
47  for ( int ii = 0; ii < npts; ii++ )
48  { // output colors from interpolated RGB
49  ored += dred; // bump color values
50  ogrn += dgrn;
51  oblu += dblu;
52  int ired = qRound( ored ); // integer equivalent
53  int igrn = qRound( ogrn );
54  int iblu = qRound( oblu );
55  // add to list
56  gcolors.append( QColor( ired, igrn, iblu ) );
57  }
58  }
59  else
60  { // first element (npts=0): start color
61  gcolors.append( QColor( ered, egrn, eblu ) );
62  }
63 
64  // set start values for next step
65  sred = ered;
66  sgrn = egrn;
67  sblu = eblu;
68  }
69  }
70  return rc; // return code
71 }
72 
73 int US_ColorGradIO::read_color_steps( QString xmlfilename,
74  QList< QColor >& scolors, QList< double >& svalues )
75 {
76  int rc = XFS_OK;
77 
78  scolors.clear(); // clear out the lists
79  svalues.clear();
80  QList< int > cparams;
81 
82  rc = read_color_step_params( xmlfilename, cparams );
83 
84  if ( rc == XFS_OK )
85  {
86  int ncolor = 0;
87  int nvals = cparams.size();
88 
89  // loop to output step colors and count total colors
90  for ( int jj = 0; jj < nvals; jj += 4 )
91  { // read each quadruple as obtained from the XML
92  int red = cparams.at( jj );
93  int grn = cparams.at( jj+1 );
94  int blu = cparams.at( jj+2 );
95  int npt = cparams.at( jj+3 );
96 
97  // add a step color to the list
98  scolors.append( QColor( red, grn, blu ) );
99 
100  if ( npt > 0 )
101  { // a step entry: save the accumulated colors count
102  ncolor += npt;
103  }
104  }
105 
106  // now loop to replace values with fractions in the 0.0-1.0 range
107  int kcolor = 0;
108  double rcolor = (double)( ncolor );
109  double rvalue;
110 
111  for ( int jj = 3; jj < nvals; jj += 4 )
112  {
113  kcolor += cparams.at( jj ); // bump running colors count
114  rvalue = (double)kcolor / rcolor; // output count/range ratio
115  svalues.append( rvalue );
116  }
117  }
118  return rc; // return code
119 }
120 
121 int US_ColorGradIO::read_color_step_params( QString xmlfilename,
122  QList< int >& cparams )
123 {
124  int rc = XFS_OK;
125  cparams.clear(); // clear out the list
126 
127  QFile filei( xmlfilename );
128 
129  if ( filei.open( QIODevice::ReadOnly ) )
130  { // able to open file
131  QXmlStreamReader xmli( &filei );
132  bool is_uscs = false;
133 
134  while( ! xmli.atEnd() )
135  { // read each element of the XML
136  xmli.readNext();
137 
138  if ( xmli.isComment() )
139  { // comment line: verify that we have color steps type
140  QString comm = xmli.text().toString();
141 
142  if ( comm.contains( "UltraScanColorSteps" ) )
143  { // mark that we have the right type
144  is_uscs = true;
145  }
146  }
147 
148  if ( xmli.isStartElement() && xmli.name() == "color" )
149  { // start of "color" entry
150  // get entries for a color step
151  QXmlStreamAttributes ats = xmli.attributes();
152  int red = ats.value( "red" ).toString().toInt();
153  int grn = ats.value( "green" ).toString().toInt();
154  int blu = ats.value( "blue" ).toString().toInt();
155  int npts = ats.value( "points" ).toString().toInt();
156 
157  cparams.append( red ); // add RGB and number_points entries
158  cparams.append( grn );
159  cparams.append( blu );
160  cparams.append( npts );
161  }
162  }
163 
164  filei.close(); // close file
165  rc = is_uscs ? rc : XFS_ERCST; // verify file color step
166  rc = xmli.hasError() ? XFS_ERXML : rc; // verify file xml
167  }
168  else
169  { // error: unable to open file
170  rc = XFS_EROPN;
171  }
172  return rc; // return code
173 }
174