1 // This is core/vgui/vgui_section_buffer.h
2 #ifndef vgui_section_buffer_h_
3 #define vgui_section_buffer_h_
4 //:
5 // \file
6 // \author fsm
7 // \brief  Holds a section of a GL image with given OpenGL buffer format and types.
8 // \verbatim
9 //  Modifications
10 //   J.L. Mundy December 2004
11 //    Added a range mapping function to allow non-byte images to be
12 //    displayed within a specified range. Originally these were just clamped
13 //    to a max value of 255.
14 // \endverbatim
15 // Contains classes:  vgui_section_buffer
16 //
17 #include "vgui_gl.h"
18 class vil1_image;
19 #include <vil/vil_fwd.h>
20 #include "vgui_range_map_params_sptr.h"
21 
22 //: Holds a section of a GL image with given OpenGL buffer format and types.
23 //
24 // A section_buffer is an object which holds a section of a GL image
25 // with given OpenGL buffer format and types. The constructor is
26 // responsible for allocating a suitably sized (and aligned) buffer.
27 //
28 // The apply() method infers the format supplied by the vgui_image or
29 // vgui_image_view and performs the necessary pixel conversion.
30 //
31 // Note that if the format and type are left unspecified, defaults
32 // will be chosen based on the current GL state. Thus, in this case,
33 // the section buffer should not be created until a GL state has been
34 // created.
35 //
36 // The 'format' and 'type' arguments describe the image format in
37 // OpenGL terms. They are those passed to glDrawPixels(), so that
38 // 'format' may be one of
39 //    GL_COLOR_INDEX, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT, GL_RGBA,
40 //    GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_RGB, GL_LUMINANCE,
41 //    GL_LUMINANCE_ALPHA and extensions such as GL_ABGR_EXT
42 // and 'type' may be one of
43 //    GL_UNSIGNED_BYTE, GL_BYTE, GL_BITMAP, GL_UNSIGNED_SHORT,
44 //    GL_SHORT, GL_UNSIGNED_INT, GL_INT, and GL_FLOAT
45 //
46 // Usually 'format'=GL_RGBA, 'type'=GL_UNSIGNED_BYTE works well.
47 //
48 class vgui_section_buffer
49 {
50  public:
51   //: Create a \a w by \a h buffer
52   //
53   // The buffer will be used to hold the GL pixels from (x,y) to
54   // (x+w-1, y+w-1) from the input image. (The input image is given
55   // via the apply() function).
56   //
57   vgui_section_buffer( unsigned x, unsigned y,
58                        unsigned w, unsigned h,
59                        GLenum format_ = GL_NONE,
60                        GLenum type_ = GL_NONE );
61 
62   ~vgui_section_buffer();
63 
set_zoom(float zoomx,float zoomy)64   void set_zoom(float zoomx, float zoomy)
65     {zoomx_ = zoomx; zoomy_ = zoomy;}
66   //: These methods take arguments in original image coordinates and return false on failure.
67   // See .cxx file for more details.
68 
69   //: Draw a section of the image
70   //
71   // The parameters are in the original image coordinates.
72   //
73   // It will return false on failure.
74   //
75   bool draw_as_image( float xlo, float ylo, float xhi, float yhi ) const;
76 
77   //: Draw a the border of a section of the image.
78   //
79   // The parameters are in the original image coordinates.
80   //
81   // It will return false on failure.
82   //
83   bool draw_as_rectangle( float xlo, float ylo, float xhi, float yhi ) const;
84 
85   //: Convenience method to draw the whole image.
86   bool draw_as_image() const;
87 
88   //: Draw a precomputed viewport image section using view rendering.
89   // Supports redrawing only visible section during overlay redraw
90   bool draw_viewport_as_image() const;
91 
92   //: Convenience method to draw the whole image.
93   bool draw_as_rectangle() const;
94 
95   //: Grab a section from the given image.
96   void apply( vil1_image const & ,
97               vgui_range_map_params_sptr const& );
98 
99   //: Grab a section from the given resource.
100   void apply( vil_image_resource_sptr const&,
101               vgui_range_map_params_sptr const& );
102 
width()103   unsigned width () const { return w_; }
height()104   unsigned height() const { return h_; }
105 
106  private:
107   // fsm: I want these to be GLenums. Please don't make them ints.
108   GLenum format_;
109   GLenum type_;
110 
111   // These fields describe where in the image the section comes from,
112   // how big it is and its resolution.
113   unsigned x_, y_;          // starting position in original image.
114   unsigned w_, h_;          // no of columns and rows (in the section).
115   float zoomx_, zoomy_;     // zoom factor when rendering
116   // actual width and height allocated.
117   // The actual buffer was bigger than the requested one in the old
118   // code when images could be rendered as a texture. It's here in
119   // case someone wants to bring that code back. -- Amitha Perera
120   unsigned allocw_, alloch_;
121 
122   //: Pointer to pixel buffer, as given to glDrawPixels() or glTexImage2D().
123   void* buffer_;
124 
125   //: Did the last apply() work?
126   bool buffer_ok_;
127 };
128 
129 #endif // vgui_section_buffer_h_
130