-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTImage.hpp
155 lines (116 loc) · 4.13 KB
/
TImage.hpp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
Copyright 2015 Esri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
A local copy of the license and additional notices are located with the
source distribution at:
http://github.com/Esri/lerc/
Contributors: Thomas Maurer
*/
#ifndef TIMAGE_HPP
#define TIMAGE_HPP
// ---- includes ------------------------------------------------------------ ;
#include <cstring>
#include <stdlib.h>
#include "Image.h"
NAMESPACE_LERC_START
class CntZ
{
public:
float cnt, z;
bool operator == (const CntZ& cz) const { return cnt == cz.cnt && z == cz.z; }
bool operator != (const CntZ& cz) const { return cnt != cz.cnt || z != cz.z; }
void operator += (const CntZ& cz) { cnt += cz.cnt; z += cz.z; }
};
template< class Element >
class TImage : public Image
{
public:
TImage() : data_(0) {}
TImage(const TImage& tImg) : data_(0) { type_ = tImg.type_; *this = tImg; }
virtual ~TImage() { clear(); }
/// assignment
virtual TImage& operator=(const TImage& tImg);
bool resize(int width, int height);
virtual void clear();
/// get data
Element getPixel(int row, int col) const { return data_[row * width_ + col]; }
const Element& operator() (int row, int col) const { return data_[row * width_ + col]; }
const Element* getData() const { return data_; }
/// set data
void setPixel(int row, int col, Element element) { data_[row * width_ + col] = element; }
Element& operator() (int row, int col) { return data_[row * width_ + col]; }
Element* getData() { return data_; }
/// compare
bool operator == (const Image& img) const;
bool operator != (const Image& img) const { return !operator==(img); };
protected:
Element* data_;
};
// -------------------------------------------------------------------------- ;
template< class Element >
bool TImage< Element >::resize(int width, int height)
{
if (width <= 0 || height <= 0)
return false;
if (width == width_ && height == height_ && data_)
return true;
free(data_);
width_ = 0;
height_ = 0;
data_ = (Element*)malloc(width * height * sizeof(Element));
if (!data_)
return false;
width_ = width;
height_ = height;
return true;
}
// -------------------------------------------------------------------------- ;
template< class Element >
void TImage< Element >::clear()
{
free(data_);
data_ = 0;
width_ = 0;
height_ = 0;
}
// -------------------------------------------------------------------------- ;
template< class Element >
TImage< Element >& TImage< Element >::operator = (const TImage& tImg)
{
// allow copying image to itself
if (this == &tImg) return *this;
// only for images of the same type!
// conversions are implemented in the derived classes
if (!resize(tImg.getWidth(), tImg.getHeight()))
return *this; // return empty image if resize fails
if (data_ && tImg.data_)
{
memcpy(data_, tImg.data_, getSize() * sizeof(Element));
Image::operator=(tImg);
}
return *this;
}
// -------------------------------------------------------------------------- ;
template< class Element >
bool TImage< Element >::operator == (const Image& img) const
{
if (!Image::operator == (img)) return false;
const Element* ptr0 = getData();
const Element* ptr1 = ((const TImage&)img).getData();
int cnt = getSize();
while (cnt--)
if (*ptr0++ != *ptr1++)
return false;
return true;
}
// -------------------------------------------------------------------------- ;
NAMESPACE_LERC_END
#endif