| 1 | /* |
| 2 | Copyright 2015 - 2026 Esri |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | |
| 16 | A local copy of the license and additional notices are located with the |
| 17 | source distribution at: |
| 18 | |
| 19 | http://github.com/Esri/lerc/ |
| 20 | |
| 21 | Contributors: Thomas Maurer |
| 22 | */ |
| 23 | |
| 24 | #ifndef TIMAGE_HPP |
| 25 | #define TIMAGE_HPP |
| 26 | |
| 27 | // ---- includes ------------------------------------------------------------ ; |
| 28 | |
| 29 | #include <cstring> |
| 30 | #include <stdlib.h> |
| 31 | #include "Image.h" |
| 32 | |
| 33 | NAMESPACE_LERC_START |
| 34 | |
| 35 | class CntZ |
| 36 | { |
| 37 | public: |
| 38 | float cnt, z; |
| 39 | bool operator == (const CntZ& cz) const { return cnt == cz.cnt && z == cz.z; } |
| 40 | bool operator != (const CntZ& cz) const { return cnt != cz.cnt || z != cz.z; } |
| 41 | void operator += (const CntZ& cz) { cnt += cz.cnt; z += cz.z; } |
| 42 | }; |
| 43 | |
| 44 | template< class Element > |
| 45 | class TImage : public Image |
| 46 | { |
| 47 | public: |
| 48 | TImage() : data_(0) {} |
| 49 | TImage(const TImage& tImg) : data_(0) { type_ = tImg.type_; *this = tImg; } |
| 50 | virtual ~TImage() { clear(); } |
| 51 | |
| 52 | /// assignment |
| 53 | virtual TImage& operator=(const TImage& tImg); |
| 54 | |
| 55 | bool resize(int width, int height); |
| 56 | virtual void clear(); |
| 57 | |
| 58 | /// get data |
| 59 | Element getPixel(int row, int col) const { return data_[row * width_ + col]; } |
| 60 | const Element& operator() (int row, int col) const { return data_[row * width_ + col]; } |
| 61 | const Element* getData() const { return data_; } |
| 62 | |
| 63 | /// set data |
| 64 | void setPixel(int row, int col, Element element) { data_[row * width_ + col] = element; } |
| 65 | Element& operator() (int row, int col) { return data_[row * width_ + col]; } |
| 66 | Element* getData() { return data_; } |
| 67 | |
| 68 | /// compare |
| 69 | bool operator == (const Image& img) const; |
| 70 | bool operator != (const Image& img) const { return !operator==(img); }; |
| 71 | |
| 72 | protected: |
| 73 | Element* data_; |
| 74 | }; |
| 75 | |
| 76 | // -------------------------------------------------------------------------- ; |
| 77 | |
| 78 | template< class Element > |
| 79 | bool TImage< Element >::resize(int width, int height) |
| 80 | { |
| 81 | if (width <= 0 || height <= 0) |
| 82 | return false; |
| 83 | |
| 84 | if (width == width_ && height == height_ && data_) |
| 85 | return true; |
| 86 | |
| 87 | free(data_); |
| 88 | width_ = 0; |
| 89 | height_ = 0; |
| 90 | |
| 91 | data_ = (Element*)malloc(sizeof(Element) * width * height); |
| 92 | if (!data_) |
| 93 | return false; |
| 94 | |
| 95 | width_ = width; |
| 96 | height_ = height; |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | // -------------------------------------------------------------------------- ; |
| 102 | |
| 103 | template< class Element > |
| 104 | void TImage< Element >::clear() |
| 105 | { |
| 106 | free(data_); |
| 107 | data_ = 0; |
| 108 | width_ = 0; |
| 109 | height_ = 0; |
| 110 | } |
| 111 | |
| 112 | // -------------------------------------------------------------------------- ; |
| 113 | |
| 114 | template< class Element > |
| 115 | TImage< Element >& TImage< Element >::operator = (const TImage& tImg) |
| 116 | { |
| 117 | // allow copying image to itself |
| 118 | if (this == &tImg) return *this; |
| 119 | |
| 120 | // only for images of the same type! |
| 121 | // conversions are implemented in the derived classes |
| 122 | |
| 123 | if (!resize(tImg.getWidth(), tImg.getHeight())) |
| 124 | return *this; // return empty image if resize fails |
| 125 | |
| 126 | if (data_ && tImg.data_) |
| 127 | { |
| 128 | memcpy(data_, tImg.data_, getSize() * sizeof(Element)); |
| 129 | Image::operator=(tImg); |
| 130 | } |
| 131 | |
| 132 | return *this; |
| 133 | } |
| 134 | |
| 135 | // -------------------------------------------------------------------------- ; |
| 136 | |
| 137 | template< class Element > |
| 138 | bool TImage< Element >::operator == (const Image& img) const |
| 139 | { |
| 140 | if (!Image::operator == (img)) return false; |
| 141 | |
| 142 | const Element* ptr0 = getData(); |
| 143 | const Element* ptr1 = ((const TImage&)img).getData(); |
| 144 | int cnt = getSize(); |
| 145 | while (cnt--) |
| 146 | if (*ptr0++ != *ptr1++) |
| 147 | return false; |
| 148 | |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | // -------------------------------------------------------------------------- ; |
| 153 | |
| 154 | NAMESPACE_LERC_END |
| 155 | #endif |