File: mvdTextStream.h

package info (click to toggle)
otb 8.1.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,030,436 kB
  • sloc: xml: 231,007; cpp: 224,490; ansic: 4,592; sh: 1,790; python: 1,131; perl: 92; makefile: 72
file content (835 lines) | stat: -rw-r--r-- 23,014 bytes parent folder | download
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
/*
 * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef mvdStream_h
#define mvdStream_h

//
// Configuration include.
//// Included at first position before any other ones.
#include "ConfigureMonteverdi.h"

#include "OTBMonteverdiCoreExport.h"

/*****************************************************************************/
/* INCLUDE SECTION                                                           */

//
// Qt includes (sorted by alphabetic order)
//// Must be included before system/custom includes.
#include <QtCore>

//
// System includes (sorted by alphabetic order)

//
// ITK includes (sorted by alphabetic order)
#include "itkHistogram.h"
#include "itkSize.h"

//
// OTB includes (sorted by alphabetic order)

//
// Monteverdi includes (sorted by alphabetic order)
#include "mvdAlgorithm.h"
#include "mvdSystemError.h"
#include "mvdTypes.h"


/*****************************************************************************/
/* PRE-DECLARATION SECTION                                                   */

//
// External classes pre-declaration.
namespace
{
}

namespace mvd
{

/**
 * \brief TODO.
 */
enum StreamTag
{
  STREAM_TAG_SIZE = 0,
  STREAM_TAG_VECTOR,
  STREAM_TAG_VARIABLE_LENGTH_VECTOR,
  //
  STREAM_TAG_COUNT
};

/**
 * \brief TODO.
 */
extern const char* STREAM_TAG_NAMES[STREAM_TAG_COUNT];

//
// Internal classes pre-declaration.
} // end of namespace 'mvd'.


/*****************************************************************************/
/* FUNCTIONS DECLARATION.                                                    */


/*****************************************************************************/

namespace mvd
{

/**
 * \brief Check status Qt stream and throw SystemError exception if
 * status is not Ok.
 */
inline void CheckStreamStatus(QTextStream& stream);

/**
 * \brief Write a text tag followed by a white space to the stream.
 *
 * \param stream Stream to write into.
 * \param name Tag name to write.
 * \param enqueueWS Enqueue a whitespace following the tag name.
 *
 * \return stream instance.
 */
inline QTextStream& WriteStreamTag(QTextStream& stream, const QString& name, bool enqueueWS = true);

/**
 * \brief Read a text tag followed by a whitespace from the stream and
 * check it against an expected value.
 *
 * \param stream Stream to read from.
 * \param tag Text tag which has been read.
 * \param expected The expected tag name. If non-empty, value read
 * from stream is checked against expected value; an
 * std::runtime_exception is thrown if both values doest not match.
 * \param skipWS Skip whitespace following tag name.
 *
 * \return stream instance.
 */
inline QTextStream& ReadStreamTag(QTextStream& stream, QString& tag, const QString& expected = QString(), bool skipWS = true);

/*****************************************************************************/

/**
 * \brief Write an StreamTag followed by a whitespace separator into a
 * QTextStream.
 *
 * \param stream QTextStream into which to write data.
 * \param tag StreamTag data to write.
 *
 * \return stream instance.
 */
inline QTextStream& operator<<(QTextStream& stream, StreamTag tag);

/**
 * \brief Read a StreamTag followed by a whitespace from a
 * QTextStream.
 *
 * \param stream QTextStream from which to read data.
 * \param tag StreamTag to data read.
 *
 * \return stream instance.
 */
inline QTextStream& operator>>(QTextStream& stream, StreamTag& tag);

/*****************************************************************************/

/**
 * \brief Write an std::vector< T, std::allocator< T > > into a QTextStream.
 *
 * \param stream QTextStream into which to write data.
 * \param vector std::vector< T, std::allocator< T > > data to write.
 *
 * \return stream instance.
 */
template <typename T>
inline QTextStream& operator<<(QTextStream& stream, const std::vector<T, std::allocator<T>>& vector);

/**
 * \brief
 */
template <>
inline QTextStream& operator<<<double>(QTextStream& stream, const std::vector<double>& vector);

/**
 * \brief
 */
template <>
inline QTextStream& operator<<<float>(QTextStream& stream, const std::vector<float>& vector);

/**
 * \brief Read an std::vector< T, Alloc > from a QTextStream.
 *
 * \param stream QTextStream from which to read data.
 * \param vector std::vector< T, Alloc > container to read data into.
 *
 * \return stream instance.
 */
template <typename T, typename Alloc>
inline QTextStream& operator>>(QTextStream& stream, std::vector<T, Alloc>& vector);

/*****************************************************************************/

/**
 * \brief Write an itk::Array< T > into a QTextStream.
 *
 * \param stream QTextStream into which to write data.
 * \param array itk::Array< T > data to write.
 *
 * \return stream instance.
 */
template <typename T>
inline QTextStream& operator<<(QTextStream& stream, const itk::Array<T>& array);

/**
 * \brief Read an itk::Array< T > from a QTextStream.
 *
 * \param stream QTextStream from which to read data.
 * \param array itk::Array< T > container to read data into.
 *
 * \return stream instance.
 */
template <typename T>
inline QTextStream& operator>>(QTextStream& stream, itk::Array<T>& array);

/*****************************************************************************/

/**
 * \brief Write an itk::VariableLengthVector< T > into a QTextStream.
 *
 * \param stream QTextStream into which to write data.
 * \param vector itk::VariableLengthVector< T > data to write.
 *
 * \return stream instance.
 */
template <typename T>
inline QTextStream& operator<<(QTextStream& stream, const itk::VariableLengthVector<T>& vector);

/**
 */
template <>
inline QTextStream& operator<<(QTextStream& stream, const itk::VariableLengthVector<float>& vector);

/**
 */
template <>
inline QTextStream& operator<<(QTextStream& stream, const itk::VariableLengthVector<double>& vector);

/**
 * \brief Read an itk::VariableLengthVector< T > from a QTextStream.
 *
 * \param stream QTextStream from which to read data.
 * \param vector itk::VariableLengthVector< T > container to read data into.
 *
 * \return stream instance.
 */
template <typename T>
inline QTextStream& operator>>(QTextStream& stream, itk::VariableLengthVector<T>& vector);

/*****************************************************************************/

/**
 * \brief Write an itk::statistics::Histogram< T, FC > into a QTextStream.
 *
 * \param stream QTextStream into which to write data.
 * \param histogram itk::statistics::Histogram< T, FC > data to write.
 *
 * \return stream instance.
 */
template <typename T, typename FC>
inline QTextStream& operator<<(QTextStream& stream, const itk::Statistics::Histogram<T, FC>& histogram);

/**
 * \brief Read an itk::statistics::Histogram< T, FC > from a QTextStream.
 *
 * \param stream QTextStream from which to read data.
 * \param histogram itk::statistics::Histogram< T, FC > container to
 * read data into.
 *
 * \return stream instance.
 */
template <typename T, typename FC>
inline QTextStream& operator>>(QTextStream& stream, itk::Statistics::Histogram<T, FC>& histogram);

} // end namespace 'mvd'.


/*****************************************************************************/
/* INLINE SECTION                                                            */

namespace mvd
{

/*******************************************************************************/
inline void CheckStreamStatus(QTextStream& stream)
{
  if (stream.status() != QTextStream::Ok)
    throw SystemError();
}

/*******************************************************************************/
inline QTextStream& WriteStreamTag(QTextStream& stream, const QString& name, bool queueWhiteSpace)
{
  stream << name;

  if (queueWhiteSpace)
    stream << " ";

  CheckStreamStatus(stream);

  return stream;
}

/*******************************************************************************/
inline QTextStream& ReadStreamTag(QTextStream& stream, QString& name, const QString& expected, bool skipWhiteSpace)
{
  stream >> name;

  if (skipWhiteSpace)
    stream >> ws;

  CheckStreamStatus(stream);

  if (!expected.isEmpty() && expected.compare(name, Qt::CaseInsensitive) != 0)
  {
    throw std::runtime_error(
        ToStdString(QCoreApplication::translate("mvd::TextStream", "Read tag '%1' does not match expected tag '%2'.").arg(name).arg(expected)));
  }

  return stream;
}

/*******************************************************************************/
inline QTextStream& operator<<(QTextStream& stream, StreamTag tag)
{
  WriteStreamTag(stream, STREAM_TAG_NAMES[tag]);

  return stream;
}

/*******************************************************************************/
inline QTextStream& operator>>(QTextStream& stream, StreamTag& tag)
{
  QString name;

  ReadStreamTag(stream, name);

  bool found = false;

  for (int i = 0; i < STREAM_TAG_COUNT && !found; ++i)
  {
    if (name.compare(STREAM_TAG_NAMES[i], Qt::CaseInsensitive) == 0)
    {
      found = true;
      tag   = static_cast<StreamTag>(i);
    }
  }

  if (!found)
  {
    throw std::runtime_error(ToStdString(QCoreApplication::translate("mvd::TextStream", "Invalid tag name '%1'.").arg(name)));
  }

  return stream;
}

/*******************************************************************************/
template <typename T>
inline QTextStream& operator<<(QTextStream& stream, const std::vector<T, std::allocator<T>>& vector)
{
  typedef std::vector<T, std::allocator<T>> Vector;

  stream << STREAM_TAG_VECTOR << vector.size();

  for (typename Vector::const_iterator it(vector.begin()); it != vector.end(); ++it)
  {
    stream << " " << *it;
  }

  return stream;
}

/*******************************************************************************/
template <>
inline QTextStream& operator<<<float>(QTextStream& stream, const std::vector<float>& vector)
{
  typedef std::vector<float, std::allocator<float>> Vector;

  stream << STREAM_TAG_VECTOR << vector.size();

  for (Vector::const_iterator it(vector.begin()); it != vector.end(); ++it)
  {
    stream << " " << ToQString(*it);
  }

  return stream;
}

/*******************************************************************************/
template <>
inline QTextStream& operator<<<double>(QTextStream& stream, const std::vector<double>& vector)
{
  typedef std::vector<double, std::allocator<double>> Vector;

  stream << STREAM_TAG_VECTOR << vector.size();

  for (Vector::const_iterator it(vector.begin()); it != vector.end(); ++it)
  {
    stream << " " << ToQString(*it);
  }

  return stream;
}

/*******************************************************************************/
template <typename T, typename Alloc>
inline QTextStream& operator>>(QTextStream& stream, std::vector<T, Alloc>& vector)
{
  typedef std::vector<T, Alloc> Vector;

  QString type;
  ReadStreamTag(stream, type, STREAM_TAG_NAMES[STREAM_TAG_VECTOR]);

  typename Vector::size_type size = 0;

  stream >> size;

  vector.resize(size);

  for (typename Vector::iterator it(vector.begin()); it != vector.end(); ++it)
  {
    stream >> ws >> *it;
  }

  return stream;
}

/*****************************************************************************/
template <typename T>
inline QTextStream& operator<<(QTextStream& stream, const itk::Array<T>& array)
{

  stream << STREAM_TAG_SIZE << array.GetSize();
  CheckStreamStatus(stream);

  for (CountType i = 0; i < array.GetSize(); ++i)
  {
    stream << " " << array[i];
    CheckStreamStatus(stream);
  }

  return stream;
}

/*******************************************************************************/
template <typename T>
inline QTextStream& operator>>(QTextStream& stream, itk::Array<T>& array)
{

  CountType dimension = 0;

  QString type;
  ReadStreamTag(stream, type, STREAM_TAG_NAMES[STREAM_TAG_SIZE]);

  stream >> dimension;

  if (dimension != array.GetSize())
  {
    throw std::length_error(ToStdString(QCoreApplication::translate("mvd::TextStream",
                                                                    "Array dimension (%1) does not match "
                                                                    "expected dimension (%2).")
                                            .arg(dimension)
                                            .arg(array.GetSize())));
  }

  for (CountType i = 0; i < array.GetSize(); ++i)
  {
    stream >> ws >> array[i];
    CheckStreamStatus(stream);
  }

  return stream;
}

/*******************************************************************************/
template <typename T>
inline QTextStream& operator<<(QTextStream& stream, const itk::VariableLengthVector<T>& vector)
{
  typedef typename itk::VariableLengthVector<T> Vector;

  // Write number of elements.
  stream << STREAM_TAG_VARIABLE_LENGTH_VECTOR << vector.Size();
  CheckStreamStatus(stream);

  // Write each element prefixed by a delimitting whitespace.
  for (typename Vector::ElementIdentifier i = 0; i < vector.Size(); ++i)
  {
    stream << " " << vector[i];
    CheckStreamStatus(stream);
  }

  return stream;
}

/*******************************************************************************/
template <>
inline QTextStream& operator<<(QTextStream& stream, const itk::VariableLengthVector<double>& vector)
{
  typedef itk::VariableLengthVector<double> Vector;

  // Write number of elements.
  stream << STREAM_TAG_VARIABLE_LENGTH_VECTOR << vector.Size();
  CheckStreamStatus(stream);

  // Write each element prefixed by a delimitting whitespace.
  for (Vector::ElementIdentifier i = 0; i < vector.Size(); ++i)
  {
    stream << " " << ToQString(vector[i]);
    CheckStreamStatus(stream);
  }

  return stream;
}

/*******************************************************************************/
template <>
inline QTextStream& operator<<(QTextStream& stream, const itk::VariableLengthVector<float>& vector)
{
  typedef itk::VariableLengthVector<float> Vector;

  // Write number of elements.
  stream << STREAM_TAG_VARIABLE_LENGTH_VECTOR << vector.Size();
  CheckStreamStatus(stream);

  // Write each element prefixed by a delimitting whitespace.
  for (Vector::ElementIdentifier i = 0; i < vector.Size(); ++i)
  {
    stream << " " << ToQString(vector[i]);
    CheckStreamStatus(stream);
  }

  return stream;
}

/*******************************************************************************/
template <typename T>
inline QTextStream& operator>>(QTextStream& stream, itk::VariableLengthVector<T>& vector)
{
  typedef typename itk::VariableLengthVector<T> Vector;

  QString type;
  ReadStreamTag(stream, type, STREAM_TAG_NAMES[STREAM_TAG_VARIABLE_LENGTH_VECTOR]);

  // Read number of elements.
  typename Vector::ElementIdentifier size = 0;
  stream >> size;
  CheckStreamStatus(stream);

  // Allocates vector elements.
  vector.SetSize(size);

  // Read each element prefixed by a delimitting whitespace.
  for (typename Vector::ElementIdentifier i = 0; i < vector.Size(); ++i)
  {
    stream >> ws >> vector[i];
    CheckStreamStatus(stream);
  }

  return stream;
}

/*******************************************************************************/
template <typename T, typename FC>
inline QTextStream& operator<<(QTextStream& stream, const itk::Statistics::Histogram<T, FC>& histogram)
{
  typedef typename itk::Statistics::Histogram<T, FC> Histogram;

  WriteStreamTag(stream, "HISTOGRAM", false);
  stream << endl;

  WriteStreamTag(stream, "MEASUREMENT");
  stream << histogram.GetSize() << endl;

#if 1
  WriteStreamTag(stream, "MINS");
  stream << histogram.GetMins() << endl;

  WriteStreamTag(stream, "MAXS");
  stream << histogram.GetMaxs() << endl;
#endif


#if 0
  typename Histogram::SizeType size( histogram.GetSize() );

  for( unsigned int dim=0; dim<N; ++dim )
    {
    for( unsigned long bin=0; bin<size[ dim ]; ++bin )
      {
      stream
	<< dim << " "
	<< bin << " "
	<< histogram.GetMins()[ dim ][ bin ] << " "
	<< histogram.GetMaxs()[ dim ][ bin ] << " "
	<< histogram.GetFrequency( bin, dim ) << endl;

      CheckStreamStatus( stream );
      }
    }

#else
  CountType size = histogram.Size();

  WriteStreamTag(stream, "SAMPLES");
  stream << size << endl;
  CheckStreamStatus(stream);

  CountType count = 0;

  for (typename Histogram::ConstIterator it(histogram.Begin()); it != histogram.End(); ++it, ++count)
  {
    stream << it.GetInstanceIdentifier() << " " << ToQString(it.GetFrequency()) << endl;

    CheckStreamStatus(stream);
  }

  WriteStreamTag(stream, "COUNT");
  stream << count << endl;
  CheckStreamStatus(stream);

  if (count != size)
  {
    throw std::runtime_error(ToStdString(QCoreApplication::translate("mvd::TextStream",
                                                                     "Histogram sample count (%1) does not match "
                                                                     "written sample count (%2).")
                                             .arg(size)
                                             .arg(count)));
  }
#endif

  return stream;
}

/*******************************************************************************/
template <typename T, typename FC>
inline QTextStream& operator>>(QTextStream& stream, itk::Statistics::Histogram<T, FC>& histogram)
{
  typedef itk::Statistics::Histogram<T, FC> Histogram;

  QString      string;
  unsigned int N = histogram.GetMeasurementVectorSize();


  //
  // Histogram section.
  ReadStreamTag(stream, string, "HISTOGRAM");

  //
  // Measurement-size section.
  ReadStreamTag(stream, string, "MEASUREMENT");
  typename Histogram::SizeType size;
  size.SetSize(N);

  stream >> size;

  // Dimension of size is checked in operator >> ( QTextStream&,
  // itk::Size< T > ).

  histogram.Initialize(size);

#if 1
  //
  // Bin mins.
  ReadStreamTag(stream, string, "MINS");
  typename Histogram::BinMinContainerType mins;
  stream >> mins;

  //
  // Bin maxes.
  ReadStreamTag(stream, string, "MAXS");
  typename Histogram::BinMaxContainerType maxs;
  stream >> maxs;

  if (mins.size() != N)
  {
    throw std::runtime_error(ToStdString(QCoreApplication::translate("mvd::TextStream",
                                                                     "Histogram mins dimension (%1) does not match "
                                                                     "expected dimension (%2).")
                                             .arg(mins.size())
                                             .arg(N)));
  }

  if (maxs.size() != N)
  {
    throw std::runtime_error(ToStdString(QCoreApplication::translate("mvd::TextStream",
                                                                     "Histogram maxs dimension (%1) does not match "
                                                                     "expected dimension (%2).")
                                             .arg(maxs.size())
                                             .arg(N)));
  }

  //
  // Initialize histogram mins & maxs within single pass.
  for (typename Histogram::BinMaxContainerType::size_type dim = 0; dim < maxs.size(); ++dim)
  {
    for (typename Histogram::BinMaxContainerType::value_type::size_type bin = 0; bin < maxs[dim].size(); ++bin)
    {
      histogram.SetBinMin(dim, bin, mins[dim][bin]);
      histogram.SetBinMax(dim, bin, maxs[dim][bin]);
    }
  }
#endif

#if 0
  //
  // Bins.
  for( unsigned int dim=0; dim<N; ++dim )
    {
    for( unsigned long bin=0; bin<size[ dim ]; ++bin )
      {
      // Dimension.
      unsigned int d = 0;

      stream >> d >> ws;
      CheckStreamStatus( stream );
      if( d!=dim )
	{
	throw std::runtime_error(
	  ToStdString(
	    QCoreApplication::translate(
	      "mvd::TextStream",
	      "Histogram dimension (%1) does not match "
	      "expected dimension (%2)."
	    )
	    .arg( d )
	    .arg( dim )
	  )
	);
	}

      // Bin.
      unsigned long b = 0;

      stream >> b >> ws;
      CheckStreamStatus( stream );
      if( b!=bin )
	{
	throw std::runtime_error(
	  ToStdString(
	    QCoreApplication::translate(
	      "mvd::TextStream",
	      "Histogram bin count (%1) does not match "
	      "expected bin count (%2)."
	    )
	    .arg( b )
	    .arg( bin )
	  )
	);
	}

      // Min.
      typename Histogram::BinMinContainerType::value_type::value_type min;

      stream >> min >> ws;
      CheckStreamStatus( stream );

      histogram.SetBinMin( dim, bin, min );

      // Max.
      typename Histogram::BinMaxContainerType::value_type::value_type max;

      stream >> max >> ws;
      CheckStreamStatus( stream );

      histogram.SetBinMax( dim, bin, max );

      // Frequency.
      typename Histogram::AbsoluteFrequencyType freq( 0 );

      stream >> freq;
      CheckStreamStatus( stream );

      typename Histogram::MeasurementType measurement(
	histogram.GetMeasurement( b, d )
      );

      typename Histogram::IndexType index( histogram.GetIndex( measurement ) );

      histogram.SetFrequency( index, freq );
      }
    }

#else
  CountType samples = 0;

  ReadStreamTag(stream, string, "SAMPLES");
  stream >> samples;
  CheckStreamStatus(stream);

  CountType space = histogram.Size();

  if (samples != space)
  {
    throw std::runtime_error(ToStdString(QCoreApplication::translate("mvd::TextStream",
                                                                     "Histogram sample count (%1) does not match "
                                                                     "expected sample count (%2).")
                                             .arg(samples)
                                             .arg(space)));
  }

  CountType i = 0;
  for (; i < samples; ++i)
  {
    typename Histogram::InstanceIdentifier    id(0);
    typename Histogram::AbsoluteFrequencyType freq(0);

    stream >> id >> ws >> freq;
    CheckStreamStatus(stream);

    histogram.SetFrequency(id, freq);
  }

  CountType count = 0;

  ReadStreamTag(stream, string, "COUNT");
  stream >> count;
  CheckStreamStatus(stream);

  if (count != i)
  {
    throw std::runtime_error(ToStdString(QCoreApplication::translate("mvd::TextStream",
                                                                     "Read sample count (histogram) (%1) does not match "
                                                                     "written sample count (%2).")
                                             .arg(i)
                                             .arg(count)));
  }

#endif

  return stream;
}

} // end namespace 'mvd'

#endif // mvdStream_h