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