]> git.tdb.fi Git - libs/gl.git/blob - source/pixelstore.cpp
Run a tighter ship with extensions
[libs/gl.git] / source / pixelstore.cpp
1 #include <algorithm>
2 #include <msp/gl/extensions/ext_texture3d.h>
3 #include "gl.h"
4 #include "pixelformat.h"
5 #include "pixelstore.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11
12 PixelStore::PixelStore():
13         row_length(0),
14         image_height(0),
15         skip_pixels(0),
16         skip_rows(0),
17         skip_images(0),
18         alignment(4)
19 { }
20
21 PixelStore PixelStore::from_image(const Graphics::Image &img)
22 {
23         PixelStore pstore;
24         unsigned stride = img.get_stride();
25         unsigned ncomp = get_component_count(pixelformat_from_graphics(img.get_format()));
26         pstore.set_canvas_size(img.get_stride()/ncomp, 0);
27         pstore.set_alignment(min(stride&~(stride-1), 8U));
28         return pstore;
29 }
30
31 void PixelStore::update_parameter(int mask) const
32 {
33         if(cur_obj!=this)
34                 return;
35
36         if(mask&SIZE)
37         {
38                 glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length);
39                 if(EXT_texture3D)
40                         glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, image_height);
41         }
42         if(mask&ORIGIN)
43         {
44                 glPixelStorei(GL_UNPACK_SKIP_PIXELS, skip_pixels);
45                 glPixelStorei(GL_UNPACK_SKIP_ROWS, skip_rows);
46                 if(EXT_texture3D)
47                         glPixelStorei(GL_UNPACK_SKIP_IMAGES, skip_images);
48         }
49         if(mask&ALIGNMENT)
50                 glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
51 }
52
53 void PixelStore::set_canvas_size(unsigned w, unsigned h)
54 {
55         row_length = w;
56         image_height = h;
57         update_parameter(SIZE);
58 }
59
60 void PixelStore::set_origin(unsigned x, unsigned y, unsigned z)
61 {
62         if(z>0)
63                 static Require _req3d(EXT_texture3D);
64         skip_pixels = x;
65         skip_rows = y;
66         skip_images = z;
67         update_parameter(ORIGIN);
68 }
69
70 void PixelStore::set_alignment(unsigned a)
71 {
72         alignment = a;
73         update_parameter(ALIGNMENT);
74 }
75
76 void PixelStore::bind() const
77 {
78         if(set_current(this))
79                 update_parameter(-1);
80 }
81
82 } // namespace GL
83 } // namespace Msp