core/vgui/vgui_section_buffer.h
Go to the documentation of this file.
00001 // This is core/vgui/vgui_section_buffer.h
00002 #ifndef vgui_section_buffer_h_
00003 #define vgui_section_buffer_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \author fsm
00010 // \brief  Holds a section of a GL image with given OpenGL buffer format and types.
00011 // \verbatim
00012 //  Modifications
00013 //   J.L. Mundy December 2004
00014 //    Added a range mapping function to allow non-byte images to be
00015 //    displayed within a specified range. Originally these were just clamped
00016 //    to a max value of 255.
00017 // \endverbatim
00018 // Contains classes:  vgui_section_buffer
00019 //
00020 #include <vgui/vgui_gl.h>
00021 class vil1_image;
00022 #include <vil/vil_fwd.h>
00023 #include <vgui/vgui_range_map_params_sptr.h>
00024 
00025 //: Holds a section of a GL image with given OpenGL buffer format and types.
00026 //
00027 // A section_buffer is an object which holds a section of a GL image
00028 // with given OpenGL buffer format and types. The constructor is
00029 // responsible for allocating a suitably sized (and aligned) buffer.
00030 //
00031 // The apply() method infers the format supplied by the vgui_image or
00032 // vgui_image_view and performs the necessary pixel conversion.
00033 //
00034 // Note that if the format and type are left unspecified, defaults
00035 // will be chosen based on the current GL state. Thus, in this case,
00036 // the section buffer should not be created until a GL state has been
00037 // created.
00038 //
00039 // The 'format' and 'type' arguments describe the image format in
00040 // OpenGL terms. They are those passed to glDrawPixels(), so that
00041 // 'format' may be one of
00042 //    GL_COLOR_INDEX, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT, GL_RGBA,
00043 //    GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_RGB, GL_LUMINANCE,
00044 //    GL_LUMINANCE_ALPHA and extensions such as GL_ABGR_EXT
00045 // and 'type' may be one of
00046 //    GL_UNSIGNED_BYTE, GL_BYTE, GL_BITMAP, GL_UNSIGNED_SHORT,
00047 //    GL_SHORT, GL_UNSIGNED_INT, GL_INT, and GL_FLOAT
00048 //
00049 // Usually 'format'=GL_RGBA, 'type'=GL_UNSIGNED_BYTE works well.
00050 //
00051 class vgui_section_buffer
00052 {
00053  public:
00054   //: Create a \a w by \a h buffer
00055   //
00056   // The buffer will be used to hold the GL pixels from (x,y) to
00057   // (x+w-1, y+w-1) from the input image. (The input image is given
00058   // via the apply() function).
00059   //
00060   vgui_section_buffer( unsigned x, unsigned y,
00061                        unsigned w, unsigned h,
00062                        GLenum format_ = GL_NONE,
00063                        GLenum type_ = GL_NONE );
00064 
00065   ~vgui_section_buffer();
00066 
00067   void set_zoom(float zoomx, float zoomy)
00068     {zoomx_ = zoomx; zoomy_ = zoomy;}
00069   //: These methods take arguments in original image coordinates and return false on failure.
00070   // See .cxx file for more details.
00071 
00072   //: Draw a section of the image
00073   //
00074   // The parameters are in the original image coordinates.
00075   //
00076   // It will return false on failure.
00077   //
00078   bool draw_as_image( float xlo, float ylo, float xhi, float yhi ) const;
00079 
00080   //: Draw a the border of a section of the image.
00081   //
00082   // The parameters are in the original image coordinates.
00083   //
00084   // It will return false on failure.
00085   //
00086   bool draw_as_rectangle( float xlo, float ylo, float xhi, float yhi ) const;
00087 
00088   //: Convenience method to draw the whole image.
00089   bool draw_as_image() const;
00090 
00091   //: Draw a precomputed viewport image section using view rendering.
00092   // Supports redrawing only visible section during overlay redraw
00093   bool draw_viewport_as_image() const;
00094 
00095   //: Convenience method to draw the whole image.
00096   bool draw_as_rectangle() const;
00097 
00098   //: Grab a section from the given image.
00099   void apply( vil1_image const & ,
00100               vgui_range_map_params_sptr const& );
00101 
00102   //: Grab a section from the given resource.
00103   void apply( vil_image_resource_sptr const&,
00104               vgui_range_map_params_sptr const& );
00105 
00106   unsigned width () const { return w_; }
00107   unsigned height() const { return h_; }
00108 
00109  private:
00110   // fsm: I want these to be GLenums as gcc 2.95 will not implicitly
00111   // cast ints to enums. Please don't make them ints.
00112   GLenum format_;
00113   GLenum type_;
00114 
00115   // These fields describe where in the image the section comes from,
00116   // how big it is and its resolution.
00117   unsigned x_, y_;          // starting position in original image.
00118   unsigned w_, h_;          // no of columns and rows (in the section).
00119   float zoomx_, zoomy_;     // zoom factor when rendering
00120   // actual width and height allocated.
00121   // The actual buffer was bigger than the requested one in the old
00122   // code when images could be rendered as a texture. It's here in
00123   // case someone wants to bring that code back. -- Amitha Perera
00124   unsigned allocw_, alloch_;
00125 
00126   //: Pointer to pixel buffer, as given to glDrawPixels() or glTexImage2D().
00127   void* buffer_;
00128 
00129   //: Did the last apply() work?
00130   bool buffer_ok_;
00131 };
00132 
00133 #endif // vgui_section_buffer_h_