~ubuntu-branches/ubuntu/saucy/caret/saucy-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*LICENSE_START*/
/*
 *  Copyright 1995-2002 Washington University School of Medicine
 *
 *  http://brainmap.wustl.edu
 *
 *  This file is part of CARET.
 *
 *  CARET is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 * 
 *  CARET is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with CARET; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
/*LICENSE_END*/

#include <QTextStream>

#include "ColorFile.h"
#include "GiftiCommon.h"
#include "GiftiLabelTable.h"
#include "StringTable.h"

/**
 * constructor.
 */
GiftiLabelTable::GiftiLabelTable()
{
   clear();
}

/**
 * copy constructor.
 */
GiftiLabelTable::GiftiLabelTable(const GiftiLabelTable& nlt)
{
   copyHelperGiftiLabelTable(nlt);
}

/**
 * destructor.
 */
GiftiLabelTable::~GiftiLabelTable()
{
   clear();
}

/** 
 * assignment operator.
 */
GiftiLabelTable& 
GiftiLabelTable::operator=(const GiftiLabelTable& nlt)
{
   if (this != &nlt) {
      copyHelperGiftiLabelTable(nlt);
   }
   return *this;
}

/**
 * add a label (returns the label's index).
 * If the label is already in the table its index is returned.
 */
int 
GiftiLabelTable::addLabel(const QString& labelName)
{
   const int indx = getLabelIndex(labelName);
   if (indx >= 0) {
      return indx;
   }
   
   labels.push_back(LabelData(labelName));
   return (labels.size() - 1);
}

/**
 * delete a label.
 */
void 
GiftiLabelTable::deleteLabel(const int labelIndex)
{
   labels.erase(labels.begin() + labelIndex);
}
      
/**
 * append a label table to this one.
 */
void 
GiftiLabelTable::append(const GiftiLabelTable& nlt,
                        const std::vector<int>* labelsWithTheseIndicesOnly)
{
   const int numLabels = nlt.getNumberOfLabels();
   if (numLabels > 0) {
      //
      // Determine which labels should be appended
      //
      std::vector<bool> appendThisLabel(numLabels, false);
      if (labelsWithTheseIndicesOnly != NULL) {
         const int num = static_cast<int>(labelsWithTheseIndicesOnly->size());
         for (int i = 0; i < num; i++) {
            const int indx = (*labelsWithTheseIndicesOnly)[i];
            appendThisLabel[indx] = true;
         }
      }
      else {
         std::fill(appendThisLabel.begin(), appendThisLabel.end(), true);
      }
      
      //
      // Append the labels
      //
      for (int i = 0; i < numLabels; i++) {
         if (appendThisLabel[i]) {
            addLabel(nlt.getLabel(i));
         }
      }
   }
}

/**
 * clear out the labels.
 */
void 
GiftiLabelTable::clear()
{
   labels.clear();
}

/**
 * get the index of a label (returns -1 if not found).
 */
int 
GiftiLabelTable::getLabelIndex(const QString& labelName) const
{
   int numLabels = getNumberOfLabels();
   for (int i = 0; i < numLabels; i++) {
      if (labels[i].getLabelName() == labelName) {
         return i;
      }
   }
   return -1;
}

/**
 * get label using its index.
 */
QString 
GiftiLabelTable::getLabel(const int indx) const
{
   if ((indx >= 0) && (indx < static_cast<int>(labels.size()))) {
      return labels[indx].getLabelName();
   }
   return "";
}

/**
 * set the label for a specified index.
 */
void 
GiftiLabelTable::setLabel(const int indx,
                          const QString& labelName)
{
   //
   // Add space if needed
   // 
   if (indx >= getNumberOfLabels()) {
      labels.resize(indx + 1, LabelData(""));
   }
   labels[indx].setLabelName(labelName);
}


/**
 * Get the color components as floats ranging 0.0 to 1.0
 */
void
GiftiLabelTable::getColorFloat(const int indx,
                   float& red,
                   float& green,
                   float& blue,
                   float& alpha) const
{
   if ((indx >= 0) && (indx < static_cast<int>(labels.size()))) {
      labels[indx].getColorFloat(red, green, blue, alpha);
   }
}

/**
 * Set the color components from floats ranging 0.0 to 1.0 .
 */
void
GiftiLabelTable::setColorFloat(const int indx,
                   float red,
                   float green,
                   float blue,
                   float alpha)
{
   //
   // Add space if needed
   //
   if (indx >= getNumberOfLabels()) {
      labels.resize(indx + 1, LabelData(""));
   }
   labels[indx].setColorFloat(red, green, blue, alpha);
}

/**
 * Get teh color components.
 */
void
GiftiLabelTable::getColor(const int indx,
              unsigned char& red,
              unsigned char& green,
              unsigned char& blue,
              unsigned char& alpha) const
{
   if ((indx >= 0) && (indx < static_cast<int>(labels.size()))) {
      labels[indx].getColor(red, green, blue, alpha);
   }
}
/**
 * Set the color components.
 */
void
GiftiLabelTable::setColor(const int indx,
              unsigned char red,
              unsigned char green,
              unsigned char blue,
              unsigned char alpha)
{
   //
   // Add space if needed
   //
   if (indx >= getNumberOfLabels()) {
      labels.resize(indx + 1, LabelData(""));
   }

   float r = ((float)red)   / 255.0;
   float g = ((float)green) / 255.0;
   float b = ((float)blue)  / 255.0;
   float a = ((float)alpha) / 255.0;

   labels[indx].setColorFloat(r, g, b, a);
}

/**
 * get label enabled using its index.
 */
bool 
GiftiLabelTable::getLabelEnabled(const int indx) const
{
   if ((indx >= 0) && (indx < static_cast<int>(labels.size()))) {
      return labels[indx].getLabelEnabled();
   }
   return false;
}

/**
 * set label enabled for a specified index (index must be >= 0).
 */
void 
GiftiLabelTable::setLabelEnabled(const int indx,
                                 const bool b)
{
   if ((indx >= 0) && (indx < static_cast<int>(labels.size()))) {
      labels[indx].setLabelEnabled(b);
   }
}
                           
/**
 * set all labels enabled.
 */
void 
GiftiLabelTable::setAllLabelsEnabled(const bool b)
{
   const int num = getNumberOfLabels();
   for (int i = 0; i < num; i++) {
      setLabelEnabled(i, b);
   }
}

/**
 * set a label's color file index.
 */
void 
GiftiLabelTable::setColorFileIndex(const int indx,
                                   const int colorFileIndex)
{
   if ((indx >= 0) &&
       (indx < getNumberOfLabels())) {
      labels[indx].setColorFileIndex(colorFileIndex);
   }
}

/**
 * get a labels color file index.
 */
int 
GiftiLabelTable::getColorFileIndex(const int indx) const
{
   if ((indx >= 0) &&
       (indx < getNumberOfLabels())) {
      return labels[indx].getColorFileIndex();
   }
   
   return -1;
}
      
/**
 * get all labels.
 */
void 
GiftiLabelTable::getAllLabels(std::vector<QString>& labelsOut) const
{
   const int num = getNumberOfLabels();
   labelsOut.clear();
   for (int i = 0; i < num; i++) {
      labelsOut.push_back(labels[i].getLabelName());
   }
}
                  
/**
 * copy helper used by copy constructor and assignement operators.
 */
void 
GiftiLabelTable::copyHelperGiftiLabelTable(const GiftiLabelTable& nlt)
{
   labels = nlt.labels;
}      

/**
 * Get colors from the labels.
 */
void
GiftiLabelTable::addColorsToColorFile(ColorFile& colorFile)
{
   unsigned char defRed, defGreen, defBlue, defAlpha;
   GiftiLabelTable::getDefaultColor(defRed, defGreen, defBlue, defAlpha);

   const int numLabels = getNumberOfLabels();
   for (int i = 0; i < numLabels; i++) {
      unsigned char r, g, b, a;
      labels[i].getColor(r, g, b, a);
      QString labelName = labels[i].getLabelName();

      bool haveColorInColorFile = colorFile.getColorExists(labelName);
      if (haveColorInColorFile == false) {
         //
         // Add color since it does not exist in color file
         //
         colorFile.addColor(labelName, r, g, b, a);
      }
      else {
         //
         // Color is in color file so override only if
         // label color is not the default color
         //
         if ((r != defRed) ||
             (g != defGreen) ||
             (b != defBlue) ||
             (a != defAlpha)) {
            colorFile.addColor(labelName, r, g, b, a);
         }
      }
   }

}

/**
 * Create a label from each of the colors in a color file.
 */
void
GiftiLabelTable::createLabelsFromColors(const ColorFile& colorFile)
{
   int numColors = colorFile.getNumberOfColors();
   for (int i = 0; i < numColors; i++) {
      const ColorFile::ColorStorage* cs = colorFile.getColor(i);
      QString name = cs->getName();
      unsigned char r, g, b, a;
      cs->getRgba(r, g, b, a);

      this->labels.push_back(LabelData(name, r, g, b, a));
   }
}

/**
 * assign colors to the labels.
 */
void 
GiftiLabelTable::assignColors(const ColorFile& colorFile)
{
   unsigned char r, g, b, a;
   GiftiLabelTable::getDefaultColor(r, g, b, a);
   bool exactMatch = false;
   const int numLabels = getNumberOfLabels();
   for (int i = 0; i < numLabels; i++) {
      QString labelName = labels[i].getLabelName();
      int indx = colorFile.getColorByName(labelName,
                                          exactMatch,
                                          r, g, b, a);
      labels[i].setColorFileIndex(indx);
      labels[i].setColor(r, g, b, a);
   }
   
   //
   // Use alpha of zero for ???
   //
   int unknownIndex = this->addLabel("???");
   if (unknownIndex >= 0) {
      float r, g, b, a;
      this->getColorFloat(unknownIndex, r, g, b, a);
      this->setColorFloat(unknownIndex, r, r, b, 0.0);
   }
}      

/**
 * write metadata (used by other classes so static).
 */
void 
GiftiLabelTable::writeAsXML(QTextStream& stream,
                                const int indentOffset) const
{
   int indent = indentOffset;
   
   if (labels.empty()) {
      GiftiCommon::writeIndentationXML(stream, indent);
      stream << "<" << GiftiCommon::tagLabelTable << "/>" << "\n";
   }
   else {
      GiftiCommon::writeIndentationXML(stream, indent);
      stream << "<" << GiftiCommon::tagLabelTable << ">" << "\n";
      indent++;
      
      const int numLabels = getNumberOfLabels();
      for (int i = 0; i < numLabels; i++) {
         float red, green, blue, alpha;
         labels[i].getColorFloat(red, green, blue, alpha);
         GiftiCommon::writeIndentationXML(stream, indent);
         stream << "<" << GiftiCommon::tagLabel << " "
                << GiftiCommon::attIndex << "=\"" << i << "\" "
                << GiftiCommon::attRed << "=\"" << QString::number(red, 'f', 3) << "\" "
                << GiftiCommon::attGreen << "=\"" << QString::number(green, 'f', 3) << "\" "
                << GiftiCommon::attBlue << "=\"" << QString::number(blue, 'f', 3) << "\" "
                << GiftiCommon::attAlpha << "=\"" << QString::number(alpha, 'f', 3) << "\""
                << ">";
         stream << "<![CDATA["
                << labels[i].getLabelName()
                << "]]>";
         stream << "</" << GiftiCommon::tagLabel << ">" << "\n";
      }
      
      indent--;
      GiftiCommon::writeIndentationXML(stream, indent);
      stream << "</" << GiftiCommon::tagLabelTable << ">" << "\n";
   }
}

/**
 * Write as Caret6 XML.
 */
void
GiftiLabelTable::writeAsXML(XmlGenericWriter& xmlWriter) const
{
   xmlWriter.writeStartElement(GiftiCommon::tagLabelTable);

   const int numLabels = getNumberOfLabels();
   for (int i = 0; i < numLabels; i++) {
      float red, green, blue, alpha;
      labels[i].getColorFloat(red, green, blue, alpha);

      XmlGenericWriterAttributes attributes;
      attributes.addAttribute(GiftiCommon::attIndex, i);
      attributes.addAttribute(GiftiCommon::attRed, red);
      attributes.addAttribute(GiftiCommon::attGreen, green);
      attributes.addAttribute(GiftiCommon::attBlue, blue);
      attributes.addAttribute(GiftiCommon::attAlpha, alpha);
      xmlWriter.writeElementCData(GiftiCommon::tagLabel,
                                  attributes,
                                  labels[i].getLabelName());
   }

   xmlWriter.writeEndElement();

}

/**
 * write the data into a StringTable.
 */
void 
GiftiLabelTable::writeDataIntoStringTable(StringTable& table) const
{
   const int numLabels = getNumberOfLabels();
   if (numLabels <= 0) {
      return;
   }
   table.setNumberOfRowsAndColumns(numLabels, 2, GiftiCommon::tagLabelTable);
   
   table.setTableTitle(GiftiCommon::tagLabelTable);
   table.setColumnTitle(0, "index");
   table.setColumnTitle(1, GiftiCommon::tagLabel);
   
   int rowCount = 0;
   for (int i = 0; i < numLabels; i++) {
      table.setElement(rowCount, 0, i);
      table.setElement(rowCount, 1, labels[i].getLabelName());
      rowCount++;
   }
}

/**
 * read the data from a StringTable.
 */
void 
GiftiLabelTable::readDataFromStringTable(const StringTable& table) throw (FileException)
{
   clear();
   
   int indexCol = -1;
   int labelCol = -1;
   for (int j = 0; j < table.getNumberOfColumns(); j++) {
      if (table.getColumnTitle(j) == "index") {
         indexCol = j;
      }
      else if (table.getColumnTitle(j) == GiftiCommon::tagLabel) {
         labelCol = j;
      }
   }
   
   if ((indexCol < 0) || (labelCol < 0)) {
      throw FileException("GiftiLabelTable: Unable to find index and label column titles.");
   }
   
   for (int i = 0; i < table.getNumberOfRows(); i++) {
      const int indx = table.getElementAsInt(i, indexCol);
      const QString label = table.getElement(i, labelCol);
      setLabel(indx, label);
   }
}