]> git.tdb.fi Git - libs/gl.git/blob - source/pixelstore.cpp
Deal with nontrivial image configurations
[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::set_canvas_size(unsigned w, unsigned h)
31 {
32         row_length = w;
33         image_height = h;
34 }
35
36 void PixelStore::set_origin(unsigned x, unsigned y, unsigned z)
37 {
38         skip_pixels = x;
39         skip_rows = y;
40         skip_images = z;
41 }
42
43 void PixelStore::set_alignment(unsigned a)
44 {
45         alignment = a;
46 }
47
48 void PixelStore::bind() const
49 {
50         if(set_current(this))
51         {
52                 glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length);
53                 glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, image_height);
54                 glPixelStorei(GL_UNPACK_SKIP_PIXELS, skip_pixels);
55                 glPixelStorei(GL_UNPACK_SKIP_ROWS, skip_rows);
56                 glPixelStorei(GL_UNPACK_SKIP_IMAGES, skip_images);
57                 glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
58         }
59 }
60
61 } // namespace GL
62 } // namespace Msp