1 // -*- mode: c++; c-set-style: "stroustrup"; tab-width: 4; -*-
2 //
3 // CImage.c
4 //
5 // Copyright (C) 2004 Koji Nakamaru
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software Foundation,
19 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 //
21 
22 #ifndef _CImage_h_
23 #define _CImage_h_
24 
25 template <typename T, int C>
26 class CImage {
27 public:
28 	CImage();
29 	virtual ~CImage();
30 	bool initialize(int w, int h);
31 	void finalize();
32 	void clear();
33 	T *pixel(int x, int y);
34 	int w();
35 	int h();
36 	void lock();
37 	void unlock();
38 	bool isChanged();
39 	void setChanged(bool p);
40 
41 protected:
42 	int _w;
43 	int _h;
44 	T *_pixels;
45 	pthread_mutex_t _mutex;
46 	bool _is_mutex_initialized;
47 	bool _is_changed;
48 };
49 
50 // inline functions
51 
52 template <class T, int C>
CImage()53 CImage<T, C>::CImage()
54 : _w(0),
55   _h(0),
56   _pixels(NULL),
57   _is_mutex_initialized(false)
58 {
59 	memset(&_mutex, 0, sizeof(_mutex));
60 }
61 
62 template <class T, int C>
~CImage()63 CImage<T, C>::~CImage()
64 {
65 	finalize();
66 }
67 
68 template <class T, int C>
initialize(int w,int h)69 bool CImage<T, C>::initialize(
70 	int w,
71 	int h)
72 {
73 	if (w <= 0 || h <= 0) {
74 		return false;
75 	}
76 	finalize();
77 	if ((_pixels = new T[w * h * C]) == NULL) {
78 		return false;
79 	}
80 	_w = w;
81 	_h = h;
82 	pthread_mutex_init(&_mutex, NULL);
83 	_is_mutex_initialized = true;
84 	return true;
85 }
86 
87 template <class T, int C>
finalize()88 void CImage<T, C>::finalize()
89 {
90 	if (_is_mutex_initialized) {
91 		pthread_mutex_destroy(&_mutex);
92 		memset(&_mutex, 0, sizeof(_mutex));
93 		_is_mutex_initialized = false;
94 	}
95 	forgetArray(&_pixels);
96 }
97 
98 template <class T, int C>
clear()99 inline void CImage<T, C>::clear()
100 {
101 	memset(_pixels, 0, sizeof(T) * _w * _h * C);
102 }
103 
104 template <class T, int C>
pixel(int x,int y)105 inline T *CImage<T, C>::pixel(
106 	int x,
107 	int y)
108 {
109 	return &_pixels[(y * _w + x) * C];
110 }
111 
112 template <class T, int C>
w()113 inline int CImage<T, C>::w()
114 {
115 	return _w;
116 }
117 
118 template <class T, int C>
h()119 inline int CImage<T, C>::h()
120 {
121 	return _h;
122 }
123 
124 template <class T, int C>
lock()125 inline void CImage<T, C>::lock()
126 {
127 	pthread_mutex_lock(&_mutex);
128 }
129 
130 template <class T, int C>
unlock()131 inline void CImage<T, C>::unlock()
132 {
133 	pthread_mutex_unlock(&_mutex);
134 }
135 
136 template <class T, int C>
isChanged()137 inline bool CImage<T, C>::isChanged()
138 {
139 	return _is_changed;
140 }
141 
142 template <class T, int C>
setChanged(bool p)143 inline void CImage<T, C>::setChanged(bool p)
144 {
145 	_is_changed = p;
146 }
147 
148 #endif
149