11 # include <vcl_msvc_warnings.h> 20 #include <vxl_config.h> 35 void swap_endian( vxl_uint_32& word )
37 auto* bytes = reinterpret_cast<vxl_uint_8*>( &word );
38 vxl_uint_8 t = bytes[0];
50 bool read_uint_32(
vil_stream* vs, vxl_uint_32& word )
52 if ( vs->
read( &word, 4 ) < 4 )
62 bool write_uint_32(
vil_stream* vs, vxl_uint_32 word )
67 return vs->
write( &word, 4 ) == 4;
74 vxl_uint_32 compute_length( vxl_uint_32 w, vxl_uint_32 h, vxl_uint_32 d )
83 const vxl_uint_8 RAS_MAGIC[] = { 0x59, 0xA6, 0x6A, 0x95 };
84 constexpr vxl_uint_32 RT_OLD = 0;
85 constexpr vxl_uint_32 RT_STANDARD = 1;
86 constexpr vxl_uint_32 RT_BYTE_ENCODED = 2;
87 constexpr vxl_uint_32 RT_FORMAT_RGB = 3;
88 constexpr vxl_uint_32 RMT_NONE = 0;
89 constexpr vxl_uint_32 RMT_EQUAL_RGB = 1;
102 vxl_uint_8 buf[4] = { 0, 0, 0, 0 };
104 if ( ! ( buf[0] == RAS_MAGIC[0] && buf[1] == RAS_MAGIC[1] &&
105 buf[2] == RAS_MAGIC[2] && buf[3] == RAS_MAGIC[3] ) )
175 std::cerr << __FILE__ <<
": can't handle " 184 std::cerr << __FILE__ <<
": can't handle " <<
bits_per_component_ <<
" bits per component\n";
191 type_ = RT_FORMAT_RGB;
222 if (! ( buf[0] == RAS_MAGIC[0] && buf[1] == RAS_MAGIC[1] &&
223 buf[2] == RAS_MAGIC[2] && buf[3] == RAS_MAGIC[3] ) )
242 if (
type_ != RT_OLD &&
type_ != RT_STANDARD &&
243 type_ != RT_BYTE_ENCODED &&
type_ != RT_FORMAT_RGB ) {
244 std::cerr << __FILE__ <<
": unknown type " <<
type_ << std::endl;
248 std::cerr << __FILE__ <<
": unknown map type " <<
map_type_ << std::endl;
252 std::cerr << __FILE__ <<
": No colour map according to header, but there is a map!\n";
257 std::cerr << __FILE__ <<
": depth " <<
depth_ <<
" not implemented\n";
262 if (
type_ == RT_OLD ) {
266 std::cerr << __FILE__ <<
": header says image has length zero\n";
270 std::cerr << __FILE__ <<
": length " <<
length_ <<
" does not match wxhxd = " 275 std::cerr << __FILE__ <<
": color map length is not a multiple of 3\n";
357 unsigned j0,
unsigned nj )
const 359 if (
type_ == RT_BYTE_ENCODED )
362 unsigned file_bytes_per_pixel = (
depth_+7)/8;
364 unsigned file_byte_width =
width_ * file_bytes_per_pixel;
365 file_byte_width += ( file_byte_width % 2 );
367 unsigned buff_byte_width =
ni * buff_bytes_per_pixel;
371 auto* ib = reinterpret_cast<vxl_uint_8*>( buf->data() );
377 for (
unsigned j = 0; j <
nj; ++j ) {
378 vs_->
seek( file_byte_start + j * file_byte_width );
379 vs_->
read( ib + j * buff_byte_width, buff_byte_width );
382 vxl_uint_8* pixel = ib + j * buff_byte_width;
383 for (
unsigned i = 0; i <
ni; ++i ) {
384 vxl_uint_8* rp = pixel+2;
385 vxl_uint_8 t = *pixel;
395 assert( file_bytes_per_pixel == 1 && buff_bytes_per_pixel == 3 );
398 std::vector<vxl_uint_8> line(
ni );
399 for (
unsigned j = 0; j <
nj; ++j ) {
400 vs_->
seek( file_byte_start + j * file_byte_width );
401 vs_->
read( &line[0], line.size() );
402 vxl_uint_8* in_p = &line[0];
403 vxl_uint_8* out_p = ib + j * buff_byte_width;
404 for (
unsigned i=0; i <
ni; ++i ) {
405 assert( *in_p < col_len );
407 *(out_p++) =
col_map_[ *in_p + col_len ];
408 *(out_p++) =
col_map_[ *in_p + 2*col_len ];
420 #if (VXL_LITTLE_ENDIAN) 422 for (
unsigned long is = 0; is<
ni*
nj*2; is+=2)
430 auto* sib = reinterpret_cast<vxl_uint_16*>(ib);
446 if ( ! this->
view_fits( section, i0, j0 ) ) {
452 std::cerr <<
"ERROR: " << __FILE__ <<
": data parameters of view don't match\n";
457 std::cerr << __FILE__ <<
": writing to file with a colour map is not implemented\n";
460 if (
type_ == RT_BYTE_ENCODED ) {
461 std::cerr << __FILE__ <<
": writing to a run-length encoded file is not implemented\n";
465 std::cerr << __FILE__ <<
": writing BGR format is not implemented\n";
471 unsigned file_bytes_per_pixel = (
depth_+7)/8;
473 unsigned file_byte_width =
width_ * file_bytes_per_pixel;
474 file_byte_width += ( file_byte_width % 2 );
476 unsigned buff_byte_width = view.
ni() * buff_bytes_per_pixel;
478 assert( file_bytes_per_pixel == buff_bytes_per_pixel );
479 assert( file_byte_width >= buff_byte_width );
485 std::vector<vxl_uint_8> data_buffer;
486 if ( file_byte_width == buff_byte_width+1 ) {
487 data_buffer.resize( file_byte_width );
488 data_buffer[ file_byte_width-1 ] = 0;
490 data_buffer.resize( buff_byte_width );
493 for (
unsigned j = 0; j < section.
nj(); ++j )
500 vxl_uint_8* ptr = &data_buffer[0];
501 for (
unsigned i = i0; i < section.
ni(); ++i ) {
502 for (
unsigned p = 0; p < section.
nplanes(); ++p ) {
503 *ptr = section(i,j,p);
509 vs_->
seek( file_byte_start + j * file_byte_width );
510 vs_->
write( &data_buffer[0], data_buffer.size() );
Stream interface for VIL image loaders.
An abstract base class of smart pointers to actual image data in memory.
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.
virtual vil_streampos tell() const =0
Return file pointer.
bool put_view(const vil_image_view_base &im, unsigned i0, unsigned j0) override
Put the data in this view back into the image source.
bool get_property(char const *tag, void *prop=nullptr) const override
Extra property information.
virtual vil_streampos write(void const *buf, vil_streampos n)=0
Write n bytes from buf. Returns number of bytes written.
Concrete view of image data of type T held in memory.
vil_streampos start_of_data_
~vil_ras_image() override
virtual void seek(vil_streampos position)=0
Goto file pointer.
virtual vil_streampos read(void *buf, vil_streampos n)=0
Read n bytes into buf. Returns number of bytes read.
Exceptions thrown by vil, and a mechanism for turning them off.
char const * file_format() const override
Return a string describing the file format.
unsigned bits_per_component_
unsigned ni() const
Width.
Stream interface for VIL image loaders.
unsigned nj() const
Height.
Generic image implementation for Sun raster files.
Indicates that some reference was made to pixels beyond the bounds of an image.
vil_ras_image(vil_stream *is)
char const * vil_ras_format_tag
void ref()
up/down the reference count.
vil_image_view_base_sptr get_copy_view() const
Create a read/write view of a copy of all the data.
A base class reference-counting view of some image data.
Ref. counted block of data on the heap.
bool read_header()
Read the header of a Sun raster file.
unsigned nplanes() const
Number of planes.
unsigned nplanes() const override
Dimensions: Planes x ni x nj.
unsigned nj() const override
Dimensions: Planes x ni x nj.
void vil_exception_warning(T exception)
Throw an exception indicating a potential problem.
Representation of a generic image source or destination.
vil_pixel_format pixel_format() const override
Pixel Format.
unsigned ni() const override
Dimensions: Planes x ni x nj.