vil_j2k_image.h
Go to the documentation of this file.
1 //:
2 // \file
3 // vil_j2k: Written by Rob Radtke (rob@) and Harry Voorhees (hlv@) of
4 // Stellar Science Ltd. Co. (stellarscience.com) for
5 // Air Force Research Laboratory, 2005.
6 // Write capability added by J. Mundy, April 2009
7 // Do not remove the following notice
8 // Modifications approved for public Release, distribution unlimited
9 // DISTAR Case 14074
10 //
11 #ifndef VIL_J2K_H
12 #define VIL_J2K_H
13 
14 #include <vil/vil_image_resource.h>
15 #include <vil/vil_file_format.h>
16 #include <NCSECWClient.h>
17 class CNCSFile;
18 class vil_stream;
19 class CNCSJPCVilIOStream;
21 {
22  public:
24  virtual char const *tag() const;
26  virtual
28  unsigned ni,
29  unsigned nj,
30  unsigned nplanes,
31  enum vil_pixel_format format);
32  //: This compression ratio is set as a target for the ermapper compression algorithm. The default is lossless compression (ratio == 1).
33  void set_compression_ratio(unsigned ratio)
34  {compression_ratio_ = ratio;}
35  private:
37 };
38 
39 //:
40 // Class capable of reading JPEG2000 Part I files and ECW (ER Mapper's proprietary format)
41 // image files. They can either be local or hosted on an Image Web Server. Either way, you
42 // just pass in the file path or url (eg. "ecwp://www.earthetc.com/images/australia/Sydney.ecw")
43 // to the ctor. The call get_copy_view() to get the image data that you want. The class efficiently
44 // handles reading large images (Terrabytes) -- and can read image portions without loading the whole file
45 // into memory.
46 //
47 // Because the source image can be really big, it is possible (through the get_copy_view() API) to
48 // ask for more data than you can handle in memory -- or more than you download for the remote case.
49 // That is why the setMaxImageDimension() guard is in place. You can set a max image dimension
50 // for both remote and local files (different values for each). Then if you call get_copy_view()
51 // asking for an image that is too big, get_copy_view() will scale down the image to the max dimension
52 // you specified. It does this silently -- perhaps we could/should change this?
53 //
54 // Note that, in order to use this class, you need to use cmake to configure VXL to link against
55 // Er Mapper's freely available ECW JPEG 2000 SDK (http://ermapper.com/downloads/sdks.aspx#16).
56 //
57 // Writing implemented by J. Mundy April 2009
58 // The SDK appears not to support float or double pixel types only integral
59 // forms; both signed or unsigned are supported. Another caveat is that the
60 // actual compression rate is only loosely related to the targeted compression
61 // rate specified upon creating the resource
62 //
64 {
65  public:
66  //:
67  // \param filelOrUrl: can either be a local file (eg. /home/beavis/file1.jp2) or
68  // it can be a url to a file hosted on an Image Web Server (eg.
69  // ecwp://www.earthetc.com/images/australia/Sydney.ecw or
70  // ecwp://www.earthetc.com/images/usa/1metercalif.ecw
71  vil_j2k_image( const std::string& fileOrUrl );
72  //:
73  // Read a jpeg 2000 image from a stream containing either a raw j2k codestream
74  // or a jp2 file stream. is' current position needs to be pointing at the beginning of
75  // one of these two things. In other words, the beginning of the stream must contain one
76  // of the two signatures:
77  // - Hex( FF 4F ) -- (codestream)
78  // - Hex( 00 00 00 0C 6A 50 20 20 0D 0A 87 0A ) -- jp2 file
79  //
80  // Note that some references state that jp2 files start with ( 00 00 00 0C 6A 50 1A 1A 0D 0A 87 0A )
81  // [note the 1A 1A difference]. I believe this was changed between the last draft and the final version
82  // of ISO/IEC 155444-1 (JPEG standard). I have never seen a real jp2 file with this old signature (including
83  // the official jpeg conformance test files)
84  //
85  // also note: Don't use is while I'm trying to use it... it'll screw us both up.
87  vil_j2k_image( vil_stream* vs, unsigned ni, unsigned nj,
88  unsigned nplanes, enum vil_pixel_format format,
89  unsigned compression_ratio = 1);
91  //: Dimensions: planes x width x height x components
92  virtual unsigned nplanes() const;
93  virtual unsigned ni() const;
94  virtual unsigned nj() const;
95  virtual enum vil_pixel_format pixel_format() const;
96  //: returns j2k
97  char const* file_format() const;
98  virtual bool get_property(char const* /* tag */, void* /* property_value */ = 0) const
99  { return false; }
100 
101  //: Create a read/write view of a copy of this data.
102  // \return 0 if unable to get view of correct size.
103  virtual vil_image_view_base_sptr get_copy_view(unsigned i0, unsigned ni,
104  unsigned j0, unsigned nj) const;
105 
106  virtual vil_image_view_base_sptr get_copy_view_decimated(unsigned i0, unsigned ni,
107  unsigned j0, unsigned nj,
108  double i_factor, double j_factor) const;
110  get_copy_view_decimated_by_size(unsigned i0, unsigned ni,
111  unsigned j0, unsigned nj,
112  unsigned int output_width,
113  unsigned int output_height) const;
114 
115 
117  { return get_copy_view( 0, ni(), 0, nj() ); }
118 
119  //:
120  // Call this after construction to see if you can get valid data from me.
121  // If this returns false, then this image is of no use to you
122  bool is_valid() const { return mFileResource != 0; }
123 
124  //:
125  // When calling get_copy_view(), the function will scale down the output image_view
126  // so that neither dimension (x or y) is greater than widthOrHeight. This feature
127  // is here to protect you in the case that your code asks for an excessively large
128  // image that will crash your program. You can turn this checking off with \sa unsetMaxImageDimension()
129  // By default, this value is set to 5000.
130  void setMaxImageDimension( unsigned int widthOrHeight, bool remote = false );
131  //:
132  // Call this if you don't want get_copy_view() to do size checking.
133  // Be warned that jpeg 2000 codestreams can be really big, so you could
134  // cause a program crash.
135  void unsetMaxImageDimension( bool remote = false );
136 
137  //:
138  // Static function that can be used to decode a JPEG2000 codestream
139  // or file (jp2 file). The stream must start at vs' current position.
141  unsigned i0, unsigned ni,
142  unsigned j0, unsigned nj,
143  double i_factor, double j_factor );
146  unsigned i0, unsigned ni,
147  unsigned j0, unsigned nj,
148  unsigned int output_width,
149  unsigned int output_height );
150 
151 
152  //:
153  // Encode an entire image by loading the input resource from stream
154  // and compressing the input line by line by extracting an image view
155  // of a block of lines at a time, thus works for arbitrarily large images.
156  // The num_lines_block parameter is the number of image rows in the
157  // block which is read into memory from the resource
158  static bool s_encode_jpeg2000(vil_stream* vs,
159  const char* out_filename,
160  unsigned compression_ratio = 1,
161  unsigned num_lines_block = 1024,
162  bool verbose = false);
163 
164  //: encode an entire image by loading the input resource from file
165  // Uses the stream-based method of the same name
166  static bool s_encode_jpeg2000(const char* in_filename,
167  const char* out_filename,
168  unsigned compression_ratio = 1,
169  unsigned num_lines_block = 1024,
170  bool verbose = false);
171 
172  //: JPEG2K compress the data from the full image view and insert in resource
173  // The compression ratio is determined when the resource is created by
174  // the file_format class.
175  virtual bool put_view(const vil_image_view_base& im);
176 
177  //: JPEG2K compress the data from an image view and insert in resource
178  // This method cannot be implemented because the J2K SDK does not support
179  // compressing arbitrary views. Only inserting strictly successive lines
180  // is allowed.
181  virtual bool put_view(const vil_image_view_base& im, unsigned i0, unsigned j0){return false;}
182 
183  //: JPEG2K compress by inserting an image row (line) at a time.
184  // When the full image has been inserted, the resource is closed
185  // and is no longer valid. The lines must be inserted in strict row order.
186  bool put_line(const vil_image_view_base& im);
187 
188  //: Check that a view will fit into the data at the given offset.
189  virtual bool view_fits(const vil_image_view_base& im, unsigned i0, unsigned j0);
190  protected:
191  //: The ermapper file
192  CNCSFile* mFileResource;
193  //: The ermapper stream
195  //:
196  // \sa setMaxImageDimension and \sa unsetMaxImageDimension
197  //
198  // if this equals std::numeric_limits<unsigned int>::max(), then this feature is turned off
199  // Of course I'm ignored if mRemoteFile is true
200  unsigned int mMaxLocalDimension;
201  //:
202  // Same as \sa mMaxLocalDimension but applies to remote files.
203  // This is typically a smaller number because of the speed concerns of downloading
204  // a very largeimage
205  unsigned int mMaxRemoteDimension;
206  //:file is remote
208  //: band information array
209  NCSFileBandInfo* mBandinfo;
210  //: the file information block
211  NCSFileViewFileInfoEx* mFinfo;
212  //: the current line being written for compression
213  unsigned line_index_;
214 };
215 
216 #endif // VIL_J2K_H
virtual vil_image_resource_sptr make_input_image(vil_stream *vs)
Attempt to make a generic_image which will read from vil_stream vs.
virtual bool view_fits(const vil_image_view_base &im, unsigned i0, unsigned j0)
Check that a view will fit into the data at the given offset.
An abstract base class of smart pointers to actual image data in memory.
vil_pixel_format
Describes the type of the concrete data.
static vil_image_view_base_sptr s_decode_jpeg_2000_by_size(vil_stream *vs, unsigned i0, unsigned ni, unsigned j0, unsigned nj, unsigned int output_width, unsigned int output_height)
vil_image_view_base_sptr get_copy_view() const
unsigned int mMaxLocalDimension
vil_j2k_image(const std::string &fileOrUrl)
virtual unsigned nj() const
Dimensions: Planes x ni x nj.
Class capable of reading JPEG2000 Part I files and ECW (ER Mapper's proprietary format) image files.
Definition: vil_j2k_image.h:63
bool is_valid() const
Call this after construction to see if you can get valid data from me.
CNCSJPCVilIOStream * mStr
The ermapper stream.
unsigned int mMaxRemoteDimension
Same as.
virtual unsigned nplanes() const
Dimensions: planes x width x height x components.
Base class for image formats.
void unsetMaxImageDimension(bool remote=false)
Call this if you don't want get_copy_view() to do size checking.
virtual bool put_view(const vil_image_view_base &im, unsigned i0, unsigned j0)
JPEG2K compress the data from an image view and insert in resource.
virtual unsigned ni() const
Dimensions: Planes x ni x nj.
static bool s_encode_jpeg2000(vil_stream *vs, const char *out_filename, unsigned compression_ratio=1, unsigned num_lines_block=1024, bool verbose=false)
Encode an entire image by loading the input resource from stream and compressing the input line by li...
virtual char const * tag() const
Return a character string which uniquely identifies this format.
Stream interface for VIL image loaders.
Definition: vil_stream.h:21
CNCSFile * mFileResource
The ermapper file.
virtual bool put_view(const vil_image_view_base &im)
JPEG2K compress the data from the full image view and insert in resource.
void set_compression_ratio(unsigned ratio)
This compression ratio is set as a target for the ermapper compression algorithm. The default is loss...
Definition: vil_j2k_image.h:33
Abstract representation of an image source or image destination.
virtual enum vil_pixel_format pixel_format() const
Pixel Format.
virtual vil_image_view_base_sptr get_copy_view_decimated(unsigned i0, unsigned ni, unsigned j0, unsigned nj, double i_factor, double j_factor) const
bool put_line(const vil_image_view_base &im)
JPEG2K compress by inserting an image row (line) at a time.
NCSFileBandInfo * mBandinfo
band information array.
void setMaxImageDimension(unsigned int widthOrHeight, bool remote=false)
When calling get_copy_view(), the function will scale down the output image_view so that neither dime...
virtual vil_image_resource_sptr make_output_image(vil_stream *vs, unsigned ni, unsigned nj, unsigned nplanes, enum vil_pixel_format format)
Make a "generic_image" on which put_section may be applied.
Representation of a generic image source or destination.
Wrapper class that allows you to effectively "convert" a vil_stream to a CNCSJPCIOStream.
Base class for image formats.
char const * file_format() const
returns j2k.
virtual vil_image_view_base_sptr get_copy_view_decimated_by_size(unsigned i0, unsigned ni, unsigned j0, unsigned nj, unsigned int output_width, unsigned int output_height) const
static vil_image_view_base_sptr s_decode_jpeg_2000(vil_stream *vs, unsigned i0, unsigned ni, unsigned j0, unsigned nj, double i_factor, double j_factor)
Static function that can be used to decode a JPEG2000 codestream or file (jp2 file).
bool mRemoteFile
file is remote.
unsigned line_index_
the current line being written for compression.
NCSFileViewFileInfoEx * mFinfo
the file information block.
unsigned compression_ratio_
Definition: vil_j2k_image.h:36
virtual bool get_property(char const *, void *=0) const
Extra property information.
Definition: vil_j2k_image.h:98