21 # include <vcl_msvc_warnings.h> 40 info.nBits =
sizeof(vxl_uint_32)*8;
41 info.bSigned = std::numeric_limits<vxl_uint_32>::is_signed;
46 info.nBits =
sizeof(vxl_int_32)*8;
47 info.bSigned = std::numeric_limits<vxl_int_32>::is_signed;
52 info.nBits =
sizeof(vxl_uint_16)*8;
53 info.bSigned = std::numeric_limits<vxl_uint_16>::is_signed;
58 info.nBits =
sizeof(vxl_int_16)*8;
59 info.bSigned = std::numeric_limits<vxl_int_16>::is_signed;
64 info.nBits =
sizeof(vxl_byte)*8;
65 info.bSigned = std::numeric_limits<vxl_byte>::is_signed;
70 info.nBits =
sizeof(vxl_sbyte)*8;
71 info.bSigned = std::numeric_limits<vxl_sbyte>::is_signed;
76 info.nBits =
sizeof(float)*8;
77 info.bSigned = std::numeric_limits<float>::is_signed;
82 info.nBits =
sizeof(double)*8;
83 info.bSigned = std::numeric_limits<double>::is_signed;
90 case VIL_PIXEL_FORMAT_UINT_64:
91 case VIL_PIXEL_FORMAT_INT_64:
95 info.nBits = 0; info.bSigned =
false; info.szDesc = 0;
104 static char const j2k_string[] =
"j2k";
132 return static_cast<vil_image_resource*>(j2k_img);
139 case VIL_PIXEL_FORMAT_UINT_64:
return NCSCT_UINT64;
140 case VIL_PIXEL_FORMAT_INT_64:
return NCSCT_INT64;
154 assert(0);
return NCSCT_UINT8;
163 case NCSCT_UINT64:
return VIL_PIXEL_FORMAT_UINT_64;
164 case NCSCT_INT64:
return VIL_PIXEL_FORMAT_INT_64;
184 mFileResource( new CNCSFile() ),
186 mMaxLocalDimension( 5000 ),
187 mMaxRemoteDimension( 640 ),
188 mRemoteFile( false ),
193 if (
mFileResource->Open( (
char*)fileOrUrl.c_str(),
false, false ) != NCS_SUCCESS ) {
197 if ( fileOrUrl.substr( 0, 7 ) ==
"ecwp://" ||
198 fileOrUrl.substr( 0, 7 ) ==
"ECWP://" )
206 mFileResource( new CNCSFile() ),
208 mMaxLocalDimension( 5000 ),
209 mMaxRemoteDimension( 640 ),
210 mRemoteFile( false ),
217 if ( (static_cast<CNCSJP2FileView*>(
mFileResource))->Open(
mStr ) != NCS_SUCCESS ) {
225 unsigned compression_ratio)
227 mFileResource( new CNCSFile()),
229 mMaxLocalDimension( 5000 ),
230 mMaxRemoteDimension( 640 ),
231 mRemoteFile( false ),
232 mFinfo(new NCSFileViewFileInfoEx()),
237 for (
unsigned i = 0; i<
nplanes; ++i)
248 NCSFileViewFileInfoEx finfo=*
mFinfo;
254 finfo.nCompressionRate = compression_ratio;
255 finfo.eCellSizeUnits = ECW_CELL_UNITS_METERS;
256 finfo.fCellIncrementX = 1.0;
257 finfo.fCellIncrementY = 1.0;
258 finfo.fOriginX = 0.0;
259 finfo.fOriginY = 0.0;
260 finfo.szDatum =
"RAW";
261 finfo.szProjection =
"RAW";
262 finfo.fCWRotationDegrees = 0.0;
264 finfo.eColorSpace = NCSCS_GREYSCALE;
266 finfo.eColorSpace = NCSCS_sRGB;
272 if (Error != NCS_SUCCESS) {
278 if (Error != NCS_SUCCESS) {
283 CNCSJP2FileView* fview = static_cast<CNCSJP2FileView*>(
mFileResource);
284 Error = fview->Open(static_cast<CNCSJPCIOStream*>(
mStr));
285 if (Error != NCS_SUCCESS) {
337 unsigned num_samples,
341 double j_factor)
const 347 (
unsigned int)(((
double)num_samples)/i_factor),
348 (
unsigned int)(((
double)numLines)/j_factor));
353 unsigned num_samples,
356 unsigned int output_width,
357 unsigned int output_height)
const 360 !( ( sample0 + num_samples - 1 ) <
ni() &&
361 ( line0 + numLines - 1 ) <
nj() ) )
369 INT32* bandMap = (INT32*) std::malloc(
sizeof(UINT32) * nBands );
370 for (
int i = 0 ; i < nBands ; i++ ) { bandMap[i] = i; }
376 if ( output_width > maxDim || output_height > maxDim ) {
377 unsigned int biggestDim = (std::max)( output_width, output_height );
378 double zoomFactor = ((double)maxDim) / ((double)biggestDim);
379 output_width = (
unsigned int) ( ((
double)output_width) * zoomFactor );
380 output_height = (
unsigned int) ( ((
double)output_height) * zoomFactor );
387 NCSError setViewError =
mFileResource->SetView( nBands, bandMap, output_width, output_height,
388 (INT32)sample0, (INT32)line0, (INT32)(sample0+num_samples-1), (INT32)(line0+numLines-1) );
389 if ( setViewError != NCS_SUCCESS ) {
395 double bitsPerSample =
mFileResource->GetFileInfo()->pBands[0].nBits;
396 unsigned int bytesPerSample = (
unsigned int) std::ceil( bitsPerSample / 8.0 );
397 unsigned int singleBandLineSizeBytes = output_width * bytesPerSample;
398 unsigned int allBandLineSizeBytes = singleBandLineSizeBytes * nBands;
399 unsigned int dataPtrSizeBytes = allBandLineSizeBytes * output_height;
402 void** linePtrPtr = (
void**)std::malloc( nBands *
sizeof(
int* ) );
404 for (
unsigned int currLine = 0 ; currLine < output_height ; currLine++ ) {
405 for (
int currBand = 0 ; currBand < nBands ; currBand++ ) {
406 linePtrPtr[currBand] = (
void*) ( ((
char*)data_ptr->data()) + currLine * allBandLineSizeBytes + currBand * singleBandLineSizeBytes );
409 if ( readStatus != NCSECW_READ_OK ) {
426 #define macro( F, T ) \ 428 view = new vil_image_view< T > ( data_ptr, reinterpret_cast<T*>(data_ptr->data()), \ 429 output_width, output_height, nBands, 1, output_width*nBands, output_width); \ 433 macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 );
434 macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 );
442 std::cerr <<
"Pixel format not supported by ERMapper SDK\n";
451 unsigned num_samples,
453 unsigned numLines )
const 474 unsigned i0,
unsigned ni,
475 unsigned j0,
unsigned nj,
476 double i_factor,
double j_factor )
489 unsigned i0,
unsigned ni,
490 unsigned j0,
unsigned nj,
491 unsigned int output_width,
492 unsigned int output_height )
502 unsigned ni,
unsigned nplanes,
unsigned istep,
503 unsigned planestep,
unsigned bytes_per_pixel,
504 CNCSFile* f_resource, NCSEcwCellType t)
506 T* cdata = reinterpret_cast<T*>(chunk->data());
507 T** line_ptr =
new T*[nplanes];
508 for (
unsigned p = 0; p<nplanes; ++p)
509 line_ptr[p] =
new T[ni*bytes_per_pixel];
511 for (
unsigned p = 0; p<nplanes; ++p) {
512 T* wline = line_ptr[p];
513 for (
unsigned i = 0; i<ni; ++i) {
514 *(wline+i) = *(cdata+ i*istep + p*planestep);
518 void ** outbuf = reinterpret_cast<void**>(line_ptr);
519 CNCSError writeError = f_resource->WriteLineBIL(t, nplanes, outbuf);
520 if ( writeError != NCS_SUCCESS ) good =
false;
522 for (
unsigned p = 0; p<nplanes; ++p)
523 delete [] line_ptr[p];
539 unsigned ni = this->
ni(),
nj = this->
nj(), nplanes = this->
nplanes();
547 unsigned bytes_per_pixel = 0;
555 #define macro( F, T )\ 557 bytes_per_pixel = sizeof(T); \ 558 const vil_image_view<T>& view = static_cast<const vil_image_view<T>&>(im); \ 559 chunk = view.memory_chunk(); \ 560 if (!write_line_BIL<T>(chunk, ni, nplanes, view.istep(), view.planestep(),\ 561 bytes_per_pixel, mFileResource, t)) \ 574 std::cerr <<
"Pixel format not supported by ERMapper SDK\n";
596 for (
unsigned j = 0; j<
nj; ++j) {
597 view = mem_res->get_copy_view(0,
ni, j, 1);
598 if (!this->
put_line(*view))
return false;
607 unsigned ni_view = im.
ni(), nj_view = im.
nj(), nplanes_view = im.
nplanes();
608 return i0+1 < ni_view
610 && ni_view <= this->
ni()
611 && nj_view <= this->
nj()
612 && nplanes_view <= this->
nplanes();
622 const char* out_filename,
623 unsigned compression_ratio,
624 unsigned num_lines_block,
630 unsigned ni = in_res->ni(),
nj = in_res->nj(),
nplanes = in_res->nplanes();
633 if (!vs)
return false;
638 if (!res)
return false;
642 unsigned n_blocks =
nj/num_lines_block;
644 for (
unsigned b = 0; b<n_blocks; b++, jb += num_lines_block)
648 if (!block_view)
return false;
655 for (
unsigned j = 0; j<num_lines_block; ++j) {
657 block_res->get_copy_view(0,
ni, j, 1);
658 if (!j2k_img->
put_line(*line_view))
return false;
665 unsigned remaining_lines =
nj-jb;
666 if (remaining_lines) {
668 in_res->get_view(0,
ni, jb, remaining_lines);
672 for (
unsigned j = 0; j<remaining_lines; ++j) {
673 view = residual_res->get_copy_view(0,
ni, j, 1);
674 if (!j2k_img->
put_line(*view))
return false;
680 if (verbose) std::cout <<
'\n';
685 const char* out_filename,
686 unsigned compression_ratio,
687 unsigned num_lines_block,
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_image_resource_sptr vil_load_image_resource_raw(vil_stream *, bool verbose=true)
Load from a stream.
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.
NCSFileBandInfo bandInfo(const vil_pixel_format &vilType)
Class capable of reading JPEG2000 Part I files and ECW (ER Mapper's proprietary format) image files.
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.
vil_j2k: Written by Rob Radtke (rob@) and Harry Voorhees (hlv@) of Stellar Science Ltd.
vil_stream * vil_open(char const *what, char const *how)
make a vil_stream from a filename, an URL, etc.
void unsetMaxImageDimension(bool remote=false)
Call this if you don't want get_copy_view() to do size checking.
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...
read an image from a file
NCSEcwCellType convertType(const vil_pixel_format &vilType)
unsigned ni() const
Width.
Stream interface for VIL image loaders.
unsigned nj() const
Height.
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.
Abstract representation of an image source or image destination.
void ref()
up/down the reference count.
virtual CNCSError Open(vil_stream *stream, bool bWrite=false)
Pass me the stream you want me to wrap.
A base class reference-counting view of some image data.
Ref. counted block of data on the heap.
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...
unsigned nplanes() const
Number of planes.
make a vil_stream from a filename, an URL, etc.
vil_image_resource_sptr vil_new_image_resource_of_view(vil_image_view_base const &view)
Make a new image resource that is a wrapper on an existing view's data.
Wrapper class that allows you to effectively "convert" a vil_stream to a CNCSJPCIOStream.
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.
T * ptr() const
These methods all return the raw/dumb pointer.
NCSFileViewFileInfoEx * mFinfo
the file information block.