10 # include <vcl_msvc_warnings.h> 18 #define where (std::cerr << __FILE__ " : " << __LINE__ << " : ") 85 is_(vs), bit_map_start(-1L)
120 if (local_color_map_)
122 delete [] local_color_map_[0];
123 delete [] local_color_map_[1];
124 delete [] local_color_map_[2];
125 delete local_color_map_;
130 delete [] freds_colormap[0];
131 delete [] freds_colormap[1];
132 delete [] freds_colormap[2];
133 delete [] freds_colormap[3];
134 delete [] freds_colormap;
164 where <<
"File is not a valid BMP file\n";
199 where <<
"cannot cope with compression at the moment\n";
223 typedef unsigned char uchar;
224 freds_colormap =
new uchar *[4];
225 freds_colormap[0] =
new uchar[256];
226 freds_colormap[1] =
new uchar[256];
227 freds_colormap[2] =
new uchar[256];
228 freds_colormap[3] =
new uchar[256];
230 for (
int i=0; i<256; ++i)
233 freds_colormap[0][i] = bif[0];
234 freds_colormap[1][i] = bif[1];
235 freds_colormap[2][i] = bif[2];
236 freds_colormap[3][i] = bif[3];
251 if (header.biClrUsed != 0)
252 ccount = header.biClrUsed;
253 else if (header.biBitCount != 24)
254 ccount = 1 << header.biBitCount;
262 if (header.biSize ==
sizeof(xBITMAPCOREHEADER))
263 cmap_size = ccount*3;
265 cmap_size = ccount*4;
267 std::vector<uchar> cmap(cmap_size, 0);
268 if (
is_->
read(&cmap[0], 1024L) != 1024L)
270 std::cerr <<
"Error reading image palette\n";
276 int ncolors = ccount;
279 int **color_map =
new int*[3];
280 for (
int i=0; i<3; ++i)
282 color_map[i] =
new int[ncolors];
283 for (
int j=0; j<ncolors; j++)
284 color_map[i][j] = (
int) cmap[2-i+4*j];
288 local_color_map_=color_map;
306 std::cerr <<
"Writing BMP header\n" 307 <<
ni() <<
'x' <<
nj() <<
'@' 314 rowlen += (3-(rowlen+3)%4);
315 int data_size =
nj() * rowlen;
343 auto* map =
new vxl_byte[n*4];
345 for (
unsigned int i=0; i<n; ++i, ptr+=4)
347 for (
unsigned int j=0; j<3; ++j)
349 auto c = (
unsigned char)i;
363 unsigned x0,
unsigned nx,
unsigned y0,
unsigned ny)
const 365 if (x0+nx >
ni() || y0+ny >
nj())
376 unsigned const have_bytes_per_raster = ((bytes_per_pixel *
core_hdr.
width + 3)/4)*4;
379 unsigned long want_bytes_per_raster = nx*bytes_per_pixel;
386 std::ptrdiff_t top_left_y0_in_mem = 0;
387 std::ptrdiff_t ystep = want_bytes_per_raster;
388 unsigned int rows_to_skip = y0;
395 rows_to_skip =
nj() - (y0+ny);
396 top_left_y0_in_mem = (ny-1)*want_bytes_per_raster;
412 if (nx ==
ni() && want_bytes_per_raster == have_bytes_per_raster)
415 bytes_read =
is_->
read(reinterpret_cast<vxl_byte *>(buf->data()), want_bytes_per_raster *ny);
419 for (
unsigned i=0; i<ny; ++i)
422 bytes_read +=
is_->
read(reinterpret_cast<vxl_byte *>(buf->data()) + want_bytes_per_raster*i, want_bytes_per_raster);
433 std::ptrdiff_t plane_step = 1;
434 std::ptrdiff_t top_left_plane0_in_mem = 0;
439 top_left_plane0_in_mem = 0;
450 top_left_plane0_in_mem = 2;
456 assert( (want_bytes_per_raster & 3) == 0 );
457 auto* data = reinterpret_cast<vxl_byte *>(buf->data());
458 vxl_byte*
const data_end = data+(want_bytes_per_raster*ny);
459 for(; data!=data_end; data+=4)
464 std::swap(data[0], data[2]);
469 top_left_plane0_in_mem = 0;
475 reinterpret_cast<vxl_byte *>(buf->data())+top_left_plane0_in_mem+top_left_y0_in_mem,
477 np, ystep, plane_step);
482 unsigned x0,
unsigned y0)
497 unsigned const bypp =
nplanes();
498 unsigned const rowlen =
ni() * bypp;
499 unsigned const padlen = (3-(rowlen+3)%4);
500 vxl_byte padding[3]={0, 0, 0};
504 if ((view2.planestep() == -1||
nplanes()==1)&&
505 view2.istep()==(int)view2.nplanes())
507 for (
unsigned y=0; y<view2.nj(); ++y)
510 is_->
write(&view2(0,y,view2.nplanes()-1), rowlen);
511 if (padlen !=0)
is_->
write(padding, padlen);
517 auto* buf =
new vxl_byte[rowlen+padlen];
518 for (
unsigned i=rowlen; i<rowlen+padlen; ++i) buf[i]=0;
519 for (
unsigned j=0; j<view2.nj(); ++j)
523 for (
unsigned i=0; i<view2.ni(); ++i)
525 *(b++) = view2(i, j, 2);
526 *(b++) = view2(i, j, 1);
527 *(b++) = view2(i, j, 0);
537 auto* buf =
new vxl_byte[rowlen+padlen];
538 for (
unsigned i=rowlen; i<rowlen+padlen; ++i) buf[i]=0;
539 for (
unsigned j=0; j<view2.nj(); ++j)
543 for (
unsigned i=0; i<view2.ni(); ++i)
545 *(b++) = view2(i, j, 2);
546 *(b++) = view2(i, j, 1);
547 *(b++) = view2(i, j, 0);
548 *(b++) = view2(i, j, 3);
unsigned nj() const override
Dimensions: Planes x ni x nj.
vil_bmp_image(vil_stream *is, unsigned ni, unsigned nj, unsigned nplanes, vil_pixel_format format)
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.
char const * file_format() const override
Return a string describing the file format.
virtual vil_streampos tell() const =0
Return file pointer.
Indicates that some operation is not supported.
unsigned nplanes() const override
Dimensions: planes x width x height x components.
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.
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.
Indicates that an image load or save operation failed.
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.
#define vil_property_quantisation_depth
The quantisation depth of pixel components.
Stream interface for VIL image loaders.
virtual enum vil_pixel_format pixel_format() const =0
Return a description of the concrete data pixel type.
Indicates that some reference was made to pixels beyond the bounds of an image.
vil_streampos bit_map_start
Generic image implementation for BMP files.
Indicates unexpected problems image file's data.
~vil_bmp_image() override
bool get_property(char const *tag, void *prop=nullptr) const override
Extra property information.
enum vil_pixel_format pixel_format() const override
Pixel Format.
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.
There is no class or function called vil_property.
char const * vil_bmp_format_tag
Ref. counted block of data on the heap.
unsigned ni() const override
Dimensions: Planes x ni x nj.
void vil_exception_warning(T exception)
Throw an exception indicating a potential problem.
void vil_exception_error(T exception)
Throw an exception indicating a definite problem.
vil_bmp_info_header info_hdr
vil_bmp_file_header file_hdr
vil_bmp_core_header core_hdr