core/vil/file_formats/vil_bmp.h
Go to the documentation of this file.
00001 // This is core/vil/file_formats/vil_bmp.h
00002 #ifndef vil_bmp_file_format_h_
00003 #define vil_bmp_file_format_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \author Don Hamilton, Peter Tu
00010 // \date 17 Feb 2000
00011 //
00012 //\verbatim
00013 //  Modifications
00014 // 27 May 2000 fsm Numerous endianness and structure-packing bugs fixed.
00015 //  3 October 2001 Peter Vanroose - Implemented get_property("top_row_first")
00016 // 13 November 2011 Gehua Yang - Allow read and write 32bpp image. Default to have the first plane as alpha channel
00017 //                               See a good reference at http://en.wikipedia.org/wiki/BMP_file_format 
00018 //\endverbatim
00019 
00020 class vil_stream;
00021 
00022 //=============================================================================
00023 
00024 // Due to padding, you cannot expect to read/write the header
00025 // structures as raw sequences of bytes and still get a valid
00026 // BMP header. The compiler will probably place shorts on 4-byte
00027 // boundaries, which means it will place two bytes of padding
00028 // afterwards (little-endian) or before (bigendian).
00029 //
00030 // Use the read() and write() methods instead.
00031 
00032 //--------------------------------------------------------------------------------
00033 
00034 #include <vil/vil_file_format.h>
00035 #include <vil/vil_image_resource.h>
00036 #include <vil/vil_stream.h>
00037 #include "vil_bmp_file_header.h"
00038 #include "vil_bmp_core_header.h"
00039 #include "vil_bmp_info_header.h"
00040 class vil_image_view_base;
00041 
00042 
00043 //: Loader for BMP files
00044 class vil_bmp_file_format : public vil_file_format
00045 {
00046  public:
00047   virtual char const* tag() const;
00048   virtual vil_image_resource_sptr make_input_image(vil_stream* vs);
00049   virtual vil_image_resource_sptr make_output_image(vil_stream* vs,
00050                                                     unsigned nx,
00051                                                     unsigned ny,
00052                                                     unsigned nplanes,
00053                                                     vil_pixel_format format);
00054 };
00055 
00056 //: Generic image implementation for BMP files
00057 class vil_bmp_image : public vil_image_resource
00058 {
00059  public:
00060   vil_bmp_image(vil_stream* is, unsigned ni,
00061                 unsigned nj, unsigned nplanes, vil_pixel_format format);
00062   vil_bmp_image(vil_stream* is);
00063   ~vil_bmp_image();
00064 
00065   //: Dimensions:  planes x width x height x components
00066   virtual unsigned nplanes() const {
00067     return (core_hdr.bitsperpixel<24)?1:core_hdr.bitsperpixel/8; }  // FIXME
00068   virtual unsigned ni() const { return core_hdr.width; }
00069   virtual unsigned nj() const { return core_hdr.height; }
00070 
00071   virtual enum vil_pixel_format pixel_format() const {return VIL_PIXEL_FORMAT_BYTE; }
00072 
00073   //: Create a read/write view of a copy of this data.
00074   // \return 0 if unable to get view of correct size.
00075   virtual vil_image_view_base_sptr get_copy_view(unsigned i0, unsigned ni,
00076                                                  unsigned j0, unsigned nj) const;
00077 
00078   //: Put the data in this view back into the image source.
00079   virtual bool put_view(const vil_image_view_base& im, unsigned i0, unsigned j0);
00080 
00081   char const* file_format() const;
00082   bool get_property(char const *tag, void *prop = 0) const;
00083  private:
00084   vil_stream* is_;
00085 
00086   bool read_header();
00087   bool write_header();
00088 
00089   friend class vil_bmp_file_format;
00090 
00091   vil_bmp_file_header file_hdr;
00092   vil_bmp_core_header core_hdr;
00093   vil_bmp_info_header info_hdr;
00094   vil_streampos bit_map_start; // position in file of bitmap raw data.
00095 #if 0
00096   uchar **freds_colormap;
00097 
00098   xBITMAPINFOHEADER header;
00099   xBITMAPFILEHEADER fbmp;
00100   int pixsize;
00101   int** local_color_map_;
00102 #endif // 0
00103 };
00104 
00105 
00106 #endif // vil_bmp_file_format_h_