~josejuan-sanchez/esajpip/debian

« back to all changes in this revision

Viewing changes to src/jpeg2000/image_index.h

  • Committer: José Juan Sánchez Hernández
  • Date: 2013-04-02 18:14:26 UTC
  • Revision ID: josejuan.sanchez@gmail.com-20130402181426-07xn3djblburck53
Version for Debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef _JPEG2000_INDEX_NODE_H_
 
2
#define _JPEG2000_INDEX_NODE_H_
 
3
 
 
4
 
 
5
//#define SHOW_TRACES
 
6
#include "trace.h"
 
7
 
 
8
#include <list>
 
9
#include <vector>
 
10
#include "base.h"
 
11
#include "range.h"
 
12
#include "image_info.h"
 
13
#include "packet_index.h"
 
14
#include "ipc/rdwr_lock.h"
 
15
 
 
16
 
 
17
namespace jpeg2000
 
18
{
 
19
 
 
20
  using namespace std;
 
21
  using namespace ipc;
 
22
 
 
23
 
 
24
  /**
 
25
   * Contains the indexing information of a JPEG2000 image file that
 
26
   * is managed by the index manager. This class can be printed.
 
27
   *
 
28
   * Maintains a read/write lock for controlling the multi-thread
 
29
   * access to the indexing information. For instance, by default
 
30
   * all the threads usually want to read the information. The
 
31
   * packet index built on demand, so only when a thread wants to
 
32
   * create a new level of the packet index, it needs to write.
 
33
   *
 
34
   * @see IndexManager
 
35
   */
 
36
  class ImageIndex
 
37
  {
 
38
  private:
 
39
    friend class IndexManager;
 
40
 
 
41
    /**
 
42
     * Read/write lock.
 
43
     */
 
44
    RdWrLock::Ptr rdwr_lock;
 
45
 
 
46
    vector<int> last_plt;
 
47
    vector<int> last_packet;
 
48
    vector<uint64_t> last_offset_PLT;
 
49
    vector<uint64_t> last_offset_packet;
 
50
 
 
51
    string path_name;                           ///< Image file name
 
52
    Metadata meta_data;                         ///< Image Metadata
 
53
    int num_references;                         ///< Number of references
 
54
    vector<int> max_resolution;         ///< Maximum resolution number
 
55
 
 
56
    vector<PacketIndex> packet_indexes;         ///< Code-stream packet index
 
57
    vector<CodestreamIndex> codestreams;        ///< Image code-streams
 
58
 
 
59
    CodingParameters::Ptr coding_parameters;                    ///< Image coding parameters
 
60
    vector<list<ImageIndex>::iterator> hyper_links;     ///< Image hyperlinks
 
61
 
 
62
 
 
63
    /**
 
64
     * Gets the packet lengths from a PLT marker.
 
65
     * @param file File where to read the data from.
 
66
     * @param ind_codestream Codestream index.
 
67
     * @param length_packet It is returned the length of the packet.
 
68
     * @return <code>true</code> if successful.
 
69
     */
 
70
    bool GetPLTLength(const File& file, int ind_codestream, uint64_t *length_packet);
 
71
 
 
72
    /**
 
73
     * Gets the packet offsets.
 
74
     * @param file File where to read the data from.
 
75
     * @param ind_codestream Codestream index.
 
76
     * @param length_packet Packet length.
 
77
     * @return <code>true</code> if successful.
 
78
     */
 
79
    void GetOffsetPacket(const File& file, int ind_codestream, uint64_t length_packet);
 
80
 
 
81
    /**
 
82
     * Builds the required index for the required resolution levels.
 
83
     * @param ind_codestream Codestream index.
 
84
     * @param max_index Maximum resolution level.
 
85
     * @return <code>true</code> if successful
 
86
     */
 
87
    bool BuildIndex(int ind_codestream, int max_index);
 
88
 
 
89
    /**
 
90
     * Initializes the object.
 
91
     * @param path_name Path name of the image.
 
92
     * @param image_info Indexing image information.
 
93
     * @return <code>true</code> if successful
 
94
     */
 
95
    bool Init(const string& path_name, const ImageInfo& image_info);
 
96
 
 
97
    /**
 
98
     * Initializes the object.
 
99
     * @param path_name Path name of the image.
 
100
     * @param coding_parameters Coding parameters.
 
101
     * @param image_info Indexing image information.
 
102
     * @param index Image index.
 
103
     * @return <code>true</code> if successful
 
104
     */
 
105
    bool Init(const string& path_name, CodingParameters::Ptr coding_parameters,
 
106
                const ImageInfo& image_info, int index);
 
107
 
 
108
    /**
 
109
     * Empty constructor. Only the index manager can
 
110
     * use this constructor.
 
111
     */
 
112
    ImageIndex() { }
 
113
 
 
114
  public:
 
115
    /**
 
116
     * Pointer of an object of this class.
 
117
     */
 
118
    typedef list<ImageIndex>::iterator Ptr;
 
119
 
 
120
 
 
121
    /**
 
122
     * Copy constructor.
 
123
     */
 
124
    ImageIndex(const ImageIndex& image_index)
 
125
    {
 
126
      *this = image_index;
 
127
    }
 
128
 
 
129
    /**
 
130
     * Returns the number of codestreams.
 
131
     */
 
132
    int GetNumCodestreams() const
 
133
    {
 
134
      if(codestreams.size() > 0) return codestreams.size();
 
135
      else return hyper_links.size();
 
136
    }
 
137
 
 
138
    /**
 
139
     * Returns the number of meta-data blocks.
 
140
     */
 
141
    int GetNumMetadatas() const
 
142
    {
 
143
      return meta_data.meta_data.size();
 
144
    }
 
145
 
 
146
    /**
 
147
     * Gets the lock for reading, for a specific range of
 
148
     * codestreams.
 
149
     * @return <code>true</code> if successful
 
150
     */
 
151
    bool ReadLock(const Range& range = Range(0, 0));
 
152
 
 
153
    /**
 
154
     * Releases the lock for reading, for a specific range of
 
155
     * codestreams.
 
156
     * @return <code>true</code> if successful
 
157
     */
 
158
    bool ReadUnlock(const Range& range = Range(0, 0));
 
159
 
 
160
    /**
 
161
     * Returns the path name of the image.
 
162
     */
 
163
    string GetPathName() const
 
164
    {
 
165
      return path_name;
 
166
    }
 
167
 
 
168
    /**
 
169
     * Returns the path name of a given codestream, if it is
 
170
     * a hyperlinked codestream.
 
171
     * @param num_codestream Codestream number.
 
172
     */
 
173
    string GetPathName(int num_codestream) const
 
174
    {
 
175
      if(codestreams.size() > 0) return path_name;
 
176
      else return hyper_links[num_codestream]->path_name;
 
177
    }
 
178
 
 
179
    /**
 
180
     * Returns the file segment the main header of a given
 
181
     * codestream.
 
182
     * @param num_codestream Codestream number
 
183
     */
 
184
    FileSegment GetMainHeader(int num_codestream) const
 
185
    {
 
186
      if(codestreams.size() > 0) return codestreams[num_codestream].header;
 
187
      else return hyper_links[num_codestream]->codestreams.back().header;
 
188
    }
 
189
 
 
190
    /**
 
191
     * Returns the file segment of a meta-data block.
 
192
     * @param num_metadata Meta-data number.
 
193
     */
 
194
    FileSegment GetMetadata(int num_metadata) const
 
195
    {
 
196
      return meta_data.meta_data[num_metadata];
 
197
    }
 
198
 
 
199
    /**
 
200
     * Returns the information of a place-holder.
 
201
     * @param num_placeholder Place-holder number.
 
202
     */
 
203
    PlaceHolder GetPlaceHolder(int num_placeholder) const
 
204
    {
 
205
      return meta_data.place_holders[num_placeholder];
 
206
    }
 
207
 
 
208
    /**
 
209
     * Returns the file segment of a packet.
 
210
     * @param num_codestream Codestream number.
 
211
     * @param packet Packet information.
 
212
     * @param offset If it is not <code>NULL</code> receives the
 
213
     * offset of the packet.
 
214
     */
 
215
    FileSegment GetPacket(int num_codestream, const Packet& packet, int *offset = NULL);
 
216
 
 
217
    /**
 
218
     * Returns a pointer to the coding parameters.
 
219
     */
 
220
    CodingParameters::Ptr GetCodingParameters() const
 
221
    {
 
222
      return coding_parameters;
 
223
    }
 
224
 
 
225
    /**
 
226
     * Returns <code>true</code> if the image contains
 
227
     * hyperlinks.
 
228
     */
 
229
    bool IsHyperLinked(int num_codestream) const
 
230
    {
 
231
      return (num_codestream < (int)hyper_links.size());
 
232
    }
 
233
 
 
234
    /**
 
235
     * Returns a pointer to a hyperlink.
 
236
     * @param num_codestream Number of the hyperlink (codestream).
 
237
     */
 
238
    Ptr GetHyperLink(int num_codestream) const
 
239
    {
 
240
      return hyper_links[num_codestream];
 
241
    }
 
242
 
 
243
    /**
 
244
     * Returns the number of hyperlinks.
 
245
     */
 
246
    int GetNumHyperLinks() const
 
247
    {
 
248
      return (int)hyper_links.size();
 
249
    }
 
250
 
 
251
    operator CodingParameters::Ptr() const
 
252
    {
 
253
      return coding_parameters;
 
254
    }
 
255
 
 
256
    ImageIndex& operator=(const ImageIndex& image_index)
 
257
    {
 
258
      rdwr_lock = image_index.rdwr_lock;
 
259
 
 
260
      meta_data = image_index.meta_data;
 
261
      path_name = image_index.path_name;
 
262
      num_references = image_index.num_references;
 
263
      coding_parameters = image_index.coding_parameters;
 
264
 
 
265
      base::copy(max_resolution, image_index.max_resolution);
 
266
      base::copy(last_plt, image_index.last_plt);
 
267
      base::copy(last_packet, image_index.last_packet);
 
268
      base::copy(codestreams, image_index.codestreams);
 
269
      base::copy(hyper_links, image_index.hyper_links);
 
270
      base::copy(packet_indexes, image_index.packet_indexes);
 
271
      base::copy(last_offset_PLT, image_index.last_offset_PLT);
 
272
      base::copy(last_offset_packet, image_index.last_offset_packet);
 
273
 
 
274
      return *this;
 
275
    }
 
276
 
 
277
    friend ostream& operator <<(ostream &out, const ImageIndex &info_node)
 
278
    {
 
279
      out << "Image file name: " << info_node.path_name << endl
 
280
          << *(info_node.coding_parameters)
 
281
          << "Coding parameters ref: " << info_node.coding_parameters.use_count() << endl
 
282
          << "Max resolution: ";
 
283
      for (vector<int>::const_iterator i = info_node.max_resolution.begin(); i != info_node.max_resolution.end(); i++)
 
284
              out << *i << "  ";
 
285
 
 
286
      out << endl;
 
287
 
 
288
      for (vector<CodestreamIndex>::const_iterator i = info_node.codestreams.begin(); i != info_node.codestreams.end(); i++)
 
289
        out << "Codestream index: " << endl << "----------------- " << endl << *i << endl << endl;
 
290
 
 
291
      out << "Packet indexes: " << endl << "--------------- " << endl;
 
292
 
 
293
      for (vector<PacketIndex>::const_iterator i = info_node.packet_indexes.begin(); i != info_node.packet_indexes.end(); i++)
 
294
        for (int j = 0; j < i->Size(); j++)
 
295
          out << j << " - " << (*i)[j] << endl;
 
296
 
 
297
      out << endl << "Num. Hyperlinks: " << info_node.hyper_links.size() << endl;
 
298
 
 
299
      for (vector<list<ImageIndex>::iterator>::const_iterator i = info_node.hyper_links.begin(); i != info_node.hyper_links.end(); i++)
 
300
        out << "Hyperlinks: " << endl << "----------- " << endl << **i << endl << "----------- " << endl;
 
301
 
 
302
      out << endl << "Meta-data: ";
 
303
      out << endl << info_node.meta_data;
 
304
 
 
305
      out << endl << "Num. References: " << info_node.num_references << endl;
 
306
 
 
307
      return out;
 
308
    }
 
309
 
 
310
    virtual ~ImageIndex()
 
311
    {
 
312
      TRACE("Destroying the image index of '" << path_name << "'");
 
313
    }
 
314
  };
 
315
 
 
316
}
 
317
 
 
318
#endif /* _JPEG2000_INDEX_NODE_H_ */
 
319