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
|
/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "gdcmSequenceOfFragments.h"
#include "gdcmImplicitDataElement.h"
#include "gdcmByteValue.h"
namespace gdcm
{
void SequenceOfFragments::Clear()
{
Table.SetByteValue( "", 0 );
Fragments.clear();
}
SequenceOfFragments::SizeType SequenceOfFragments::GetNumberOfFragments() const
{
// Do not count the last fragment
//assert( SequenceLengthField.IsUndefined() );
return Fragments.size();
}
void SequenceOfFragments::AddFragment(Fragment const &item)
{
Fragments.push_back(item);
}
VL SequenceOfFragments::ComputeLength() const
{
VL length = 0;
// First the table
length += Table.GetLength();
// Then all the fragments
FragmentVector::const_iterator it = Fragments.begin();
for(;it != Fragments.end(); ++it)
{
const VL fraglen = it->ComputeLength();
assert( fraglen % 2 == 0 );
length += fraglen;
}
assert( SequenceLengthField.IsUndefined() );
length += 8; // seq end delimitor (tag + vl)
return length;
}
unsigned long SequenceOfFragments::ComputeByteLength() const
{
unsigned long r = 0;
FragmentVector::const_iterator it = Fragments.begin();
for(;it != Fragments.end(); ++it)
{
assert( !it->GetVL().IsUndefined() );
r += it->GetVL();
}
return r;
}
bool SequenceOfFragments::GetFragBuffer(unsigned int fragNb, char *buffer, unsigned long &length) const
{
FragmentVector::const_iterator it = Fragments.begin();
{
const Fragment &frag = *(it+fragNb);
const ByteValue &bv = dynamic_cast<const ByteValue&>(frag.GetValue());
const VL len = frag.GetVL();
bv.GetBuffer(buffer, len);
length = len;
}
return true;
}
const Fragment& SequenceOfFragments::GetFragment(SizeType num) const
{
assert( num < Fragments.size() );
FragmentVector::const_iterator it = Fragments.begin();
const Fragment &frag = *(it+num);
return frag;
}
bool SequenceOfFragments::GetBuffer(char *buffer, unsigned long length) const
{
FragmentVector::const_iterator it = Fragments.begin();
unsigned long total = 0;
for(;it != Fragments.end(); ++it)
{
const Fragment &frag = *it;
const ByteValue &bv = dynamic_cast<const ByteValue&>(frag.GetValue());
const VL len = frag.GetVL();
bv.GetBuffer(buffer, len);
buffer += len;
total += len;
}
if( total != length )
{
//std::cerr << " DEBUG: " << total << " " << length << std::endl;
assert(0);
return false;
}
return true;
}
bool SequenceOfFragments::WriteBuffer(std::ostream &os) const
{
FragmentVector::const_iterator it = Fragments.begin();
unsigned long total = 0;
for(;it != Fragments.end(); ++it)
{
const Fragment &frag = *it;
const ByteValue *bv = frag.GetByteValue();
assert( bv );
const VL len = frag.GetVL();
bv->WriteBuffer(os);
total += len;
}
//if( total != length )
// {
// //std::cerr << " DEBUG: " << total << " " << length << std::endl;
// assert(0);
// return false;
// }
return true;
}
bool SequenceOfFragments::FillFragmentWithJPEG( Fragment & frag, std::istream & is )
{
std::vector<unsigned char> jfif;
unsigned char byte;
// begin /simple/ JPEG parser:
while( is.read( (char*)&byte, 1 ) )
{
jfif.push_back( byte );
if( byte == 0xd9 && jfif[ jfif.size() - 2 ] == 0xff ) break;
}
const uint32_t len = static_cast<uint32_t>(jfif.size());
frag.SetByteValue( (char*)&jfif[0], len );
return true;
}
} // end namespace gdcm
|