ESA JPIP server  0.1
packet_index.h
Go to the documentation of this file.
1 #ifndef _JPEG2000_PACKET_INDEX_H_
2 #define _JPEG2000_PACKET_INDEX_H_
3 
4 
5 #include "data/vint_vector.h"
6 #include "data/file_segment.h"
7 
8 
9 namespace jpeg2000
10 {
11  using namespace data;
12 
13 
23  {
24  private:
29 
34  vector<FileSegment> aux;
35 
36  public:
37  enum {
41  MINIMUM_OFFSET = 64
42  };
43 
48  {
49  }
50 
55  PacketIndex(uint64_t max_offset)
56  {
57  assert(max_offset > 0);
58 
59  int num_bytes = 0;
60 
61  while(max_offset > 0) {
62  max_offset >>= 8;
63  num_bytes++;
64  }
65  offsets.set_num_bytes(num_bytes);
66  }
67 
71  PacketIndex(const PacketIndex& index)
72  {
73  *this = index;
74  }
75 
79  const PacketIndex& operator=(const PacketIndex& index)
80  {
81  offsets = index.offsets;
82  aux.clear();
83  for(vector<FileSegment>::const_iterator i = index.aux.begin(); i != index.aux.end(); i++)
84  aux.push_back(*i);
85 
86  return *this;
87  }
88 
94  PacketIndex& Add(const FileSegment& segment)
95  {
96  assert(segment.offset >= MINIMUM_OFFSET);
97 
98  int last = aux.size() - 1;
99 
100  if(last < 0) {
101  aux.push_back(segment);
102  offsets.push_back(0);
103 
104  } else {
105  if(aux[last].IsContiguousTo(segment)) {
106  offsets.back() = aux[last].offset;
107  offsets.push_back(last);
108  aux[last] = segment;
109 
110  } else {
111  assert(last < (MINIMUM_OFFSET - 1));
112 
113  offsets.push_back(last + 1);
114  aux.push_back(segment);
115  }
116  }
117 
118  return *this;
119  }
120 
124  int Size() const
125  {
126  return offsets.size();
127  }
128 
132  void Clear()
133  {
134  offsets.clear();
135  aux.clear();
136  }
137 
143  FileSegment operator[](int i) const
144  {
145  uint64_t off_i = offsets[i];
146 
147  if(off_i < MINIMUM_OFFSET) return aux[off_i];
148  else {
149  uint64_t off_i1 = offsets[i + 1];
150 
151  if(off_i1 < MINIMUM_OFFSET)
152  off_i1 = aux[off_i1].offset;
153 
154  return FileSegment(off_i, off_i1 - off_i);
155  }
156  }
157 
158  virtual ~PacketIndex()
159  {
160  }
161  };
162 
163 }
164 
165 
166 #endif /* _JPEG2000_PACKET_INDEX_H_ */
void clear()
Clears the content.
Definition: vint_vector.h:130
FileSegment operator[](int i) const
Operator used for accessing the items.
Definition: packet_index.h:143
This class has been implemented with the same philosophy that the class STL vector, but specifically designed to store integers with a length in bytes that can be not multiple from 2 (e.g.
Definition: vint_vector.h:24
Class used for indexing the packets of a codestream image.
Definition: packet_index.h:22
Identifies a data segment of a file.
Definition: file_segment.h:20
uint64_t offset
Offset of the data segment.
Definition: file_segment.h:23
vector< FileSegment > aux
Vector of file segments to handle the different sets of packets that are not contiguous.
Definition: packet_index.h:34
vint_vector offsets
Vector of packet offsets.
Definition: packet_index.h:28
const PacketIndex & operator=(const PacketIndex &index)
Copy assignment.
Definition: packet_index.h:79
PacketIndex()
Empty constructor.
Definition: packet_index.h:47
PacketIndex(uint64_t max_offset)
Initializes the object.
Definition: packet_index.h:55
virtual ~PacketIndex()
Definition: packet_index.h:158
int Size() const
Returns the number of elements of the vector.
Definition: packet_index.h:124
void Clear()
Clears the content.
Definition: packet_index.h:132
PacketIndex(const PacketIndex &index)
Copy constructor.
Definition: packet_index.h:71
PacketIndex & Add(const FileSegment &segment)
Adds a new packet segment to the index.
Definition: packet_index.h:94