forked from ajclinto/memview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLImage.h
110 lines (94 loc) · 2.61 KB
/
GLImage.h
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
/*
This file is part of memview, a real-time memory trace visualization
application.
Copyright (C) 2013 Andrew Clinton
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA.
The GNU General Public License is contained in the file COPYING.
*/
#ifndef GLIMAGE_H
#define GLIMAGE_H
#include <stdlib.h>
#include <string.h>
template <typename T>
class GLImage {
public:
GLImage()
: myData(0)
, myWidth(0)
, myHeight(0)
, myOwnData(false) {}
~GLImage()
{
if (myOwnData)
delete [] myData;
}
void resize(int width, int height)
{
if (myWidth != width ||
myHeight != height)
{
if (myOwnData)
{
delete [] myData;
myData = 0;
}
myWidth = width;
myHeight = height;
if (myWidth && myHeight)
{
myData = new T[myWidth*myHeight];
myOwnData = true;
}
}
}
// Interface to allow external ownership
void setSize(int width, int height)
{
myWidth = width;
myHeight = height;
myOwnData = false;
}
void setData(T *data)
{
myData = data;
myOwnData = false;
}
int width() const { return myWidth; }
int height() const { return myHeight; }
size_t bytes() const { return myWidth*myHeight*sizeof(T); }
const T *data() const { return myData; }
void fill(T val)
{
for (int i = 0; i < myWidth*myHeight; i++)
myData[i] = val;
}
void zero()
{
memset(myData, 0, myWidth*myHeight*sizeof(T));
}
void setPixel(int x, int y, T val)
{
myData[(myHeight-y-1)*myWidth+x] = val;
}
T *getScanline(int y)
{
return &myData[(myHeight-y-1)*myWidth];
}
private:
T *myData;
int myWidth;
int myHeight;
bool myOwnData;
};
#endif