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