-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPixmap.cpp
executable file
·111 lines (92 loc) · 1.81 KB
/
Pixmap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Pixmap.cpp
*
* Pixmap class for general use in handling RGBA colors
* Sample solution for CP SC 405/605
*
* Created by Donald House on 9/8/08.
* Copyright 2008 Clemson University. All rights reserved.
*
*/
#include <cstdlib>
#include "Pixmap.h"
#ifdef __APPLE__
# include <GLUT/glut.h>
#else
# include <GL/glut.h>
#endif
using namespace std;
//
// Allow color channels in an RGBA pixel to be alternatively addressed by
// index [0] for red through [3] for alpha
//
unsigned char &RGBAPixel::operator[](int i){
if(i == 0)
return r;
else if(i == 1)
return g;
else if(i == 2)
return b;
else
return a;
}
//
// Address columns of a pixmap to be across a row
//
RGBAPixel &Pixrow::operator[](int c){
return px[c];
}
//
// Internal method to deallocate the memory used by a pixmap
//
void Pixmap::deallocate(){
if(rows == NULL)
return;
delete rows[0].px;
delete []rows;
rows = NULL;
}
//
// Default Pixmap constructor. Create pixmap of r x c dimensions
// (0 x 0) by default
//
Pixmap::Pixmap(int r, int c){
rows = NULL;
nrows = ncols = 0;
SetSize(r, c);
}
//
// Destroy a pixmap by cleaning up its space allocation
//
Pixmap::~Pixmap(){
deallocate();
}
//
// Changing a pixmap's size. This causes any old memory to be deallocated,
// and new space allocated
//
void Pixmap::SetSize(int r, int c){
RGBAPixel *pixels;
if(nrows != r || ncols != c)
deallocate();
nrows = r;
ncols = c;
if(nrows == 0 && ncols == 0)
return;
rows = new Pixrow[nrows];
pixels = new RGBAPixel[nrows * ncols];
for(int row = 0; row < nrows; row++)
rows[row].px = pixels + row * ncols;
}
//
// Address rows of a pixmap
//
Pixrow &Pixmap::operator[](int r){
return rows[r];
}
//
//
//
void Pixmap::Draw(){
glDrawPixels(NCols(), NRows(), GL_RGBA, GL_UNSIGNED_BYTE, Pixels());
}