Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
PixelBuffer.cpp@46:2374900f8845, 2016-12-20 (annotated)
- Committer:
- mutech
- Date:
- Tue Dec 20 03:22:01 2016 +0000
- Revision:
- 46:2374900f8845
- Parent:
- 36:0fe7917a832a
WS2811/WS2812 support Library;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mutech | 27:bc79f444883b | 1 | /* PixelBuffer.cpp |
| mutech | 27:bc79f444883b | 2 | * mbed Microcontroller Library |
| mutech | 27:bc79f444883b | 3 | * Copyright (c) 2016 muetch, t.kuroki |
| mutech | 27:bc79f444883b | 4 | * Allrights reserved. |
| mutech | 27:bc79f444883b | 5 | * |
| mutech | 27:bc79f444883b | 6 | * Rev 0.97 2016-09-07 |
| mutech | 28:b452e097da53 | 7 | * Rev 0.98 2016-09-08 |
| mutech | 27:bc79f444883b | 8 | */ |
| mutech | 27:bc79f444883b | 9 | #include "PixelBuffer.h" |
| mutech | 27:bc79f444883b | 10 | |
| mutech | 32:64c391617f6c | 11 | #define USE_MALLOC 1 // 0:new, 1:malloc |
| mutech | 32:64c391617f6c | 12 | |
| mutech | 27:bc79f444883b | 13 | //---------------------------------------------------------------------------- |
| mutech | 27:bc79f444883b | 14 | // 指定されたバッファの先頭からblock_size分をbuf_sizeが満杯になるまで繰り返しコピーする |
| mutech | 27:bc79f444883b | 15 | template <class T> |
| mutech | 27:bc79f444883b | 16 | static void repeat_buffer(T *buffer, int buf_size, int block_size = 1) |
| mutech | 27:bc79f444883b | 17 | { |
| mutech | 27:bc79f444883b | 18 | if (buffer && block_size > 0 && (uint16_t)block_size < buf_size) |
| mutech | 27:bc79f444883b | 19 | { |
| mutech | 27:bc79f444883b | 20 | T *dest = buffer + block_size; |
| mutech | 27:bc79f444883b | 21 | int left = buf_size - block_size; |
| mutech | 27:bc79f444883b | 22 | while (left > block_size) |
| mutech | 27:bc79f444883b | 23 | { |
| mutech | 27:bc79f444883b | 24 | memcpy(dest, buffer, block_size * sizeof(T)); |
| mutech | 27:bc79f444883b | 25 | dest += block_size; |
| mutech | 27:bc79f444883b | 26 | left -= block_size; |
| mutech | 27:bc79f444883b | 27 | block_size <<= 1; // 次回は2倍のサイズの転送 |
| mutech | 27:bc79f444883b | 28 | } |
| mutech | 27:bc79f444883b | 29 | memcpy(dest, buffer, left * sizeof(T)); |
| mutech | 27:bc79f444883b | 30 | } |
| mutech | 28:b452e097da53 | 31 | } |
| mutech | 27:bc79f444883b | 32 | |
| mutech | 27:bc79f444883b | 33 | //---------------------------------------------------------------------------- |
| mutech | 27:bc79f444883b | 34 | RGBPixels::RGBPixels(RGBColor *buffer, int maxPixels) |
| mutech | 32:64c391617f6c | 35 | : _owned_buffer(false) |
| mutech | 27:bc79f444883b | 36 | { |
| mutech | 46:2374900f8845 | 37 | _dummy_pixel = 0; |
| mutech | 27:bc79f444883b | 38 | setPixelBuffer(buffer, maxPixels); |
| mutech | 27:bc79f444883b | 39 | } |
| mutech | 27:bc79f444883b | 40 | |
| mutech | 27:bc79f444883b | 41 | RGBPixels::RGBPixels(int maxPixels) |
| mutech | 32:64c391617f6c | 42 | : _owned_buffer(false) |
| mutech | 27:bc79f444883b | 43 | { |
| mutech | 46:2374900f8845 | 44 | _dummy_pixel = 0; |
| mutech | 27:bc79f444883b | 45 | setPixelBuffer(nullptr, maxPixels); |
| mutech | 27:bc79f444883b | 46 | } |
| mutech | 27:bc79f444883b | 47 | |
| mutech | 27:bc79f444883b | 48 | RGBPixels::~RGBPixels() |
| mutech | 27:bc79f444883b | 49 | { |
| mutech | 32:64c391617f6c | 50 | setPixelBuffer(0, 0); |
| mutech | 27:bc79f444883b | 51 | } |
| mutech | 27:bc79f444883b | 52 | |
| mutech | 27:bc79f444883b | 53 | void RGBPixels::setPixelBuffer(RGBColor *buffer, int maxPixels) |
| mutech | 27:bc79f444883b | 54 | { |
| mutech | 32:64c391617f6c | 55 | if (_owned_buffer && _pixels) |
| mutech | 32:64c391617f6c | 56 | { |
| mutech | 32:64c391617f6c | 57 | #if USE_MALLOC |
| mutech | 32:64c391617f6c | 58 | free(_pixels); |
| mutech | 32:64c391617f6c | 59 | #else |
| mutech | 27:bc79f444883b | 60 | delete[] _pixels; |
| mutech | 32:64c391617f6c | 61 | #endif |
| mutech | 32:64c391617f6c | 62 | } |
| mutech | 32:64c391617f6c | 63 | _owned_buffer = false; |
| mutech | 46:2374900f8845 | 64 | _max_pixels = (maxPixels < 0) ? 0 : (maxPixels > MAX_PIXELS) ? MAX_PIXELS : maxPixels; |
| mutech | 46:2374900f8845 | 65 | _pixels = (!_max_pixels) ? NULL : buffer; |
| mutech | 27:bc79f444883b | 66 | |
| mutech | 46:2374900f8845 | 67 | if (!_pixels && _max_pixels > 0) |
| mutech | 27:bc79f444883b | 68 | { |
| mutech | 32:64c391617f6c | 69 | #if USE_MALLOC |
| mutech | 46:2374900f8845 | 70 | _pixels = static_cast<RGBColor*>(malloc(sizeof(RGBColor)*_max_pixels)); |
| mutech | 32:64c391617f6c | 71 | if (_pixels) |
| mutech | 32:64c391617f6c | 72 | _owned_buffer = true; |
| mutech | 32:64c391617f6c | 73 | else |
| mutech | 46:2374900f8845 | 74 | _max_pixels = 0; |
| mutech | 32:64c391617f6c | 75 | #else |
| mutech | 46:2374900f8845 | 76 | _pixels = new RGBColor[_max_pixels]; |
| mutech | 32:64c391617f6c | 77 | _owned_buffer = true; |
| mutech | 32:64c391617f6c | 78 | #endif |
| mutech | 27:bc79f444883b | 79 | } |
| mutech | 46:2374900f8845 | 80 | _num_pixels = _max_pixels; |
| mutech | 27:bc79f444883b | 81 | clear(); |
| mutech | 27:bc79f444883b | 82 | } |
| mutech | 27:bc79f444883b | 83 | |
| mutech | 27:bc79f444883b | 84 | int RGBPixels::numPixels(int value) |
| mutech | 27:bc79f444883b | 85 | { |
| mutech | 27:bc79f444883b | 86 | if (value >= 0) |
| mutech | 46:2374900f8845 | 87 | _num_pixels = (value > _max_pixels) ? _max_pixels : value; |
| mutech | 46:2374900f8845 | 88 | return _num_pixels; |
| mutech | 27:bc79f444883b | 89 | } |
| mutech | 27:bc79f444883b | 90 | |
| mutech | 27:bc79f444883b | 91 | // 指定位置のピクセルへ色配列を指定サイズ分をコピーする |
| mutech | 27:bc79f444883b | 92 | void RGBPixels::setPixels(int index, RGBColor *color, int len) |
| mutech | 27:bc79f444883b | 93 | { |
| mutech | 46:2374900f8845 | 94 | int numPixels = static_cast<int>(_num_pixels); |
| mutech | 46:2374900f8845 | 95 | if (_pixels && len > 0 && index < numPixels && (index + len) > 0) |
| mutech | 27:bc79f444883b | 96 | { |
| mutech | 34:5a141ed5d52a | 97 | if (index < 0) |
| mutech | 34:5a141ed5d52a | 98 | { |
| mutech | 35:dffa06d09fdc | 99 | len += index; |
| mutech | 46:2374900f8845 | 100 | color -= index; // <- color += -index; |
| mutech | 35:dffa06d09fdc | 101 | index = 0; |
| mutech | 34:5a141ed5d52a | 102 | } |
| mutech | 35:dffa06d09fdc | 103 | if (index + len > numPixels) |
| mutech | 35:dffa06d09fdc | 104 | len = numPixels - index; |
| mutech | 27:bc79f444883b | 105 | memcpy(&_pixels[index], color, len * sizeof(_pixels[0])); |
| mutech | 27:bc79f444883b | 106 | } |
| mutech | 27:bc79f444883b | 107 | } |
| mutech | 27:bc79f444883b | 108 | |
| mutech | 27:bc79f444883b | 109 | void RGBPixels::setPixels(int index, HSVColor *color, int len) |
| mutech | 27:bc79f444883b | 110 | { |
| mutech | 46:2374900f8845 | 111 | int numPixels = static_cast<int>(_num_pixels); |
| mutech | 46:2374900f8845 | 112 | if (_pixels && len > 0 && index < numPixels && (index + len) > 0) |
| mutech | 27:bc79f444883b | 113 | { |
| mutech | 34:5a141ed5d52a | 114 | if (index < 0) |
| mutech | 34:5a141ed5d52a | 115 | { |
| mutech | 35:dffa06d09fdc | 116 | len += index; |
| mutech | 35:dffa06d09fdc | 117 | color -= index; |
| mutech | 35:dffa06d09fdc | 118 | index = 0; |
| mutech | 34:5a141ed5d52a | 119 | } |
| mutech | 35:dffa06d09fdc | 120 | if (index + len > numPixels) |
| mutech | 35:dffa06d09fdc | 121 | len = numPixels - index; |
| mutech | 27:bc79f444883b | 122 | RGBColor *dest = &_pixels[index]; |
| mutech | 27:bc79f444883b | 123 | do |
| mutech | 27:bc79f444883b | 124 | { |
| mutech | 27:bc79f444883b | 125 | *dest++ = *color++; |
| mutech | 27:bc79f444883b | 126 | } while (--len); |
| mutech | 27:bc79f444883b | 127 | } |
| mutech | 27:bc79f444883b | 128 | } |
| mutech | 27:bc79f444883b | 129 | |
| mutech | 46:2374900f8845 | 130 | void RGBPixels::setGammaPixels(int index, RGBColor *color, int len) |
| mutech | 46:2374900f8845 | 131 | { |
| mutech | 46:2374900f8845 | 132 | int numPixels = static_cast<int>(_num_pixels); |
| mutech | 46:2374900f8845 | 133 | if (_pixels && len > 0 && index < numPixels && (index + len) > 0) |
| mutech | 46:2374900f8845 | 134 | { |
| mutech | 46:2374900f8845 | 135 | if (index < 0) |
| mutech | 46:2374900f8845 | 136 | { |
| mutech | 46:2374900f8845 | 137 | len += index; |
| mutech | 46:2374900f8845 | 138 | color -= index; |
| mutech | 46:2374900f8845 | 139 | index = 0; |
| mutech | 46:2374900f8845 | 140 | } |
| mutech | 46:2374900f8845 | 141 | if (index + len > numPixels) |
| mutech | 46:2374900f8845 | 142 | len = numPixels - index; |
| mutech | 46:2374900f8845 | 143 | uint8_t *dst = reinterpret_cast<uint8_t *>(&_pixels[index]); |
| mutech | 46:2374900f8845 | 144 | uint8_t *src = reinterpret_cast<uint8_t *>(color); |
| mutech | 46:2374900f8845 | 145 | const uint8_t *gammatab = GetGammaTable(); |
| mutech | 46:2374900f8845 | 146 | len *= 3; |
| mutech | 46:2374900f8845 | 147 | do |
| mutech | 46:2374900f8845 | 148 | { |
| mutech | 46:2374900f8845 | 149 | *dst++ = gammatab[*src++]; |
| mutech | 46:2374900f8845 | 150 | } while (--len); |
| mutech | 46:2374900f8845 | 151 | } |
| mutech | 46:2374900f8845 | 152 | } |
| mutech | 46:2374900f8845 | 153 | |
| mutech | 46:2374900f8845 | 154 | void RGBPixels::setGammaPixels(int index, HSVColor *color, int len) |
| mutech | 46:2374900f8845 | 155 | { |
| mutech | 46:2374900f8845 | 156 | int numPixels = static_cast<int>(_num_pixels); |
| mutech | 46:2374900f8845 | 157 | if (_pixels && len > 0 && index < numPixels && (index + len) > 0) |
| mutech | 46:2374900f8845 | 158 | { |
| mutech | 46:2374900f8845 | 159 | if (index < 0) |
| mutech | 46:2374900f8845 | 160 | { |
| mutech | 46:2374900f8845 | 161 | len += index; |
| mutech | 46:2374900f8845 | 162 | color -= index; |
| mutech | 46:2374900f8845 | 163 | index = 0; |
| mutech | 46:2374900f8845 | 164 | } |
| mutech | 46:2374900f8845 | 165 | if (index + len > numPixels) |
| mutech | 46:2374900f8845 | 166 | len = numPixels - index; |
| mutech | 46:2374900f8845 | 167 | uint8_t *dst = reinterpret_cast<uint8_t *>(&_pixels[index]); |
| mutech | 46:2374900f8845 | 168 | const uint8_t *gammatab = GetGammaTable(); |
| mutech | 46:2374900f8845 | 169 | do |
| mutech | 46:2374900f8845 | 170 | { |
| mutech | 46:2374900f8845 | 171 | RGBColor col = *color++; |
| mutech | 46:2374900f8845 | 172 | *dst++ = gammatab[col.r]; |
| mutech | 46:2374900f8845 | 173 | *dst++ = gammatab[col.g]; |
| mutech | 46:2374900f8845 | 174 | *dst++ = gammatab[col.b]; |
| mutech | 46:2374900f8845 | 175 | } while (--len); |
| mutech | 46:2374900f8845 | 176 | } |
| mutech | 46:2374900f8845 | 177 | } |
| mutech | 46:2374900f8845 | 178 | |
| mutech | 27:bc79f444883b | 179 | // 指定色を指定位置のピクセルから指定サイズ分書き込む |
| mutech | 28:b452e097da53 | 180 | void RGBPixels::fillPixels(int index, const RGBColor color, int len) |
| mutech | 27:bc79f444883b | 181 | { |
| mutech | 46:2374900f8845 | 182 | int numPixels = static_cast<int>(_num_pixels); |
| mutech | 46:2374900f8845 | 183 | if (_pixels && len > 0 && index < numPixels && (index + len) > 0) |
| mutech | 27:bc79f444883b | 184 | { |
| mutech | 36:0fe7917a832a | 185 | if (index < 0) |
| mutech | 36:0fe7917a832a | 186 | { |
| mutech | 36:0fe7917a832a | 187 | len += index; |
| mutech | 36:0fe7917a832a | 188 | index = 0; |
| mutech | 36:0fe7917a832a | 189 | } |
| mutech | 36:0fe7917a832a | 190 | if (index + len > numPixels) |
| mutech | 36:0fe7917a832a | 191 | len = numPixels - index; |
| mutech | 27:bc79f444883b | 192 | _pixels[index] = color; |
| mutech | 27:bc79f444883b | 193 | repeat_buffer<RGBColor>(_pixels + index, len, 1); |
| mutech | 27:bc79f444883b | 194 | } |
| mutech | 27:bc79f444883b | 195 | } |
| mutech | 27:bc79f444883b | 196 | |
| mutech | 28:b452e097da53 | 197 | void RGBPixels::fillPixels(int index, const HSVColor color, int len) |
| mutech | 27:bc79f444883b | 198 | { |
| mutech | 29:a362df191524 | 199 | fillPixels(index, RGBColor(color), len); |
| mutech | 27:bc79f444883b | 200 | } |
| mutech | 27:bc79f444883b | 201 | |
| mutech | 27:bc79f444883b | 202 | // 先頭から指定サイズ分のブロックをバッファの最後までコピーする |
| mutech | 27:bc79f444883b | 203 | void RGBPixels::repeatPixels(int block_size) |
| mutech | 27:bc79f444883b | 204 | { |
| mutech | 46:2374900f8845 | 205 | if (_pixels && block_size > 0 && block_size < _num_pixels) |
| mutech | 27:bc79f444883b | 206 | { |
| mutech | 46:2374900f8845 | 207 | repeat_buffer<RGBColor>(_pixels, _num_pixels, block_size); |
| mutech | 27:bc79f444883b | 208 | } |
| mutech | 27:bc79f444883b | 209 | } |
| mutech | 27:bc79f444883b | 210 | |
| mutech | 27:bc79f444883b | 211 | void RGBPixels::repeatPixels(RGBColor *source, int size) |
| mutech | 27:bc79f444883b | 212 | { |
| mutech | 27:bc79f444883b | 213 | if (_pixels && source && size > 0) |
| mutech | 27:bc79f444883b | 214 | { |
| mutech | 46:2374900f8845 | 215 | if (size > _num_pixels) |
| mutech | 46:2374900f8845 | 216 | size = _num_pixels; |
| mutech | 27:bc79f444883b | 217 | memcpy(_pixels, source, size * sizeof(_pixels[0])); |
| mutech | 46:2374900f8845 | 218 | repeat_buffer<RGBColor>(_pixels, _num_pixels, size); |
| mutech | 27:bc79f444883b | 219 | } |
| mutech | 27:bc79f444883b | 220 | } |
| mutech | 27:bc79f444883b | 221 | |
| mutech | 27:bc79f444883b | 222 | void RGBPixels::repeatPixels(HSVColor *source, int size) |
| mutech | 27:bc79f444883b | 223 | { |
| mutech | 27:bc79f444883b | 224 | if (_pixels && source && size > 0) |
| mutech | 27:bc79f444883b | 225 | { |
| mutech | 46:2374900f8845 | 226 | if (size > _num_pixels) |
| mutech | 46:2374900f8845 | 227 | size = _num_pixels; |
| mutech | 27:bc79f444883b | 228 | for (int i = 0; i < size; ++i) |
| mutech | 27:bc79f444883b | 229 | _pixels[i] = *source++; |
| mutech | 46:2374900f8845 | 230 | repeat_buffer<RGBColor>(_pixels, _num_pixels, size); |
| mutech | 27:bc79f444883b | 231 | } |
| mutech | 27:bc79f444883b | 232 | } |
| mutech | 27:bc79f444883b | 233 | |
| mutech | 27:bc79f444883b | 234 | RGBPixels& RGBPixels::operator=(const RGBPixels& rhs) |
| mutech | 27:bc79f444883b | 235 | { |
| mutech | 46:2374900f8845 | 236 | if (!rhs._pixels || !rhs._max_pixels) |
| mutech | 27:bc79f444883b | 237 | { |
| mutech | 27:bc79f444883b | 238 | // 右辺が空の場合何もしない |
| mutech | 27:bc79f444883b | 239 | return *this; |
| mutech | 27:bc79f444883b | 240 | } |
| mutech | 46:2374900f8845 | 241 | if (!_pixels || !_max_pixels) |
| mutech | 27:bc79f444883b | 242 | { |
| mutech | 27:bc79f444883b | 243 | // 自分のバッファなしの場合、、新規確保 |
| mutech | 46:2374900f8845 | 244 | setPixelBuffer(nullptr, rhs._max_pixels); |
| mutech | 27:bc79f444883b | 245 | } |
| mutech | 27:bc79f444883b | 246 | |
| mutech | 46:2374900f8845 | 247 | if (_pixels && _max_pixels) |
| mutech | 27:bc79f444883b | 248 | { |
| mutech | 46:2374900f8845 | 249 | _num_pixels = rhs._num_pixels; |
| mutech | 46:2374900f8845 | 250 | if (_num_pixels > rhs._max_pixels) |
| mutech | 46:2374900f8845 | 251 | _num_pixels = rhs._max_pixels; |
| mutech | 46:2374900f8845 | 252 | if (_num_pixels > _max_pixels) |
| mutech | 46:2374900f8845 | 253 | _num_pixels = _max_pixels; |
| mutech | 46:2374900f8845 | 254 | memcpy(_pixels, rhs._pixels, sizeof(_pixels[0]) * _num_pixels); |
| mutech | 27:bc79f444883b | 255 | } |
| mutech | 27:bc79f444883b | 256 | |
| mutech | 27:bc79f444883b | 257 | return *this; |
| mutech | 27:bc79f444883b | 258 | } |
| mutech | 27:bc79f444883b | 259 | |
| mutech | 27:bc79f444883b | 260 | //---------------------------------------------------------------------------- |
| mutech | 28:b452e097da53 | 261 | void RGBPixels::makeGradation(int index, RGBColor from, RGBColor to, int len) |
| mutech | 28:b452e097da53 | 262 | { |
| mutech | 46:2374900f8845 | 263 | if (!_pixels || len < 1 || index >= _num_pixels || (index + len) <= 0) |
| mutech | 28:b452e097da53 | 264 | return; |
| mutech | 28:b452e097da53 | 265 | |
| mutech | 28:b452e097da53 | 266 | int end = len; |
| mutech | 46:2374900f8845 | 267 | if (index + end > _num_pixels) |
| mutech | 46:2374900f8845 | 268 | end = _num_pixels - index; |
| mutech | 28:b452e097da53 | 269 | |
| mutech | 28:b452e097da53 | 270 | RGBColor color; |
| mutech | 28:b452e097da53 | 271 | RGBColor *dest = _pixels; |
| mutech | 28:b452e097da53 | 272 | if (index > 0) |
| mutech | 28:b452e097da53 | 273 | dest += index; |
| mutech | 28:b452e097da53 | 274 | for (int i = (index < 0) ? -index : 0; i < end; ++i) |
| mutech | 28:b452e097da53 | 275 | { |
| mutech | 28:b452e097da53 | 276 | int j = len - i; |
| mutech | 28:b452e097da53 | 277 | color.red = ((from.red * j) + (to.red * i)) / len; |
| mutech | 28:b452e097da53 | 278 | color.green = ((from.green * j) + (to.green * i)) / len; |
| mutech | 28:b452e097da53 | 279 | color.blue = ((from.blue * j) + (to.blue * i)) / len; |
| mutech | 28:b452e097da53 | 280 | *dest++ = GammaColor(color); |
| mutech | 28:b452e097da53 | 281 | } |
| mutech | 28:b452e097da53 | 282 | } |
| mutech | 28:b452e097da53 | 283 | |
| mutech | 28:b452e097da53 | 284 | void RGBPixels::makeRainbow(int index, HSVColor color, int len, int direction) |
| mutech | 28:b452e097da53 | 285 | { |
| mutech | 46:2374900f8845 | 286 | if (!_pixels || len < 1 || index >= _num_pixels || (index + len) <= 0) |
| mutech | 28:b452e097da53 | 287 | return; |
| mutech | 28:b452e097da53 | 288 | |
| mutech | 28:b452e097da53 | 289 | int end = len; |
| mutech | 46:2374900f8845 | 290 | if (index + end > _num_pixels) |
| mutech | 46:2374900f8845 | 291 | end = _num_pixels - index; |
| mutech | 28:b452e097da53 | 292 | |
| mutech | 28:b452e097da53 | 293 | HSVColor hsv(color); |
| mutech | 28:b452e097da53 | 294 | RGBColor *dest = _pixels; |
| mutech | 28:b452e097da53 | 295 | if (index > 0) |
| mutech | 28:b452e097da53 | 296 | dest += index; |
| mutech | 28:b452e097da53 | 297 | direction = (direction >= 0) ? -3600 : 3600; |
| mutech | 28:b452e097da53 | 298 | for (int i = (index < 0) ? -index : 0; i < end; ++i) |
| mutech | 28:b452e097da53 | 299 | { |
| mutech | 28:b452e097da53 | 300 | hsv.hue = color.hue + direction * i / len; |
| mutech | 28:b452e097da53 | 301 | *dest++ = GammaColor(hsv); |
| mutech | 28:b452e097da53 | 302 | } |
| mutech | 28:b452e097da53 | 303 | } |
| mutech | 28:b452e097da53 | 304 | |
| mutech | 28:b452e097da53 | 305 | //---------------------------------------------------------------------------- |
| mutech | 27:bc79f444883b | 306 | //---------------------------------------------------------------------------- |
| mutech | 27:bc79f444883b | 307 | HSVPixels::HSVPixels(HSVColor *buffer, int maxPixels) |
| mutech | 32:64c391617f6c | 308 | : _owned_buffer(false) |
| mutech | 27:bc79f444883b | 309 | { |
| mutech | 46:2374900f8845 | 310 | _dummy_pixel = 0; |
| mutech | 27:bc79f444883b | 311 | setPixelBuffer(buffer, maxPixels); |
| mutech | 27:bc79f444883b | 312 | } |
| mutech | 27:bc79f444883b | 313 | |
| mutech | 27:bc79f444883b | 314 | HSVPixels::HSVPixels(int maxPixels) |
| mutech | 32:64c391617f6c | 315 | : _owned_buffer(false) |
| mutech | 27:bc79f444883b | 316 | { |
| mutech | 46:2374900f8845 | 317 | _dummy_pixel = 0; |
| mutech | 27:bc79f444883b | 318 | setPixelBuffer(nullptr, maxPixels); |
| mutech | 27:bc79f444883b | 319 | } |
| mutech | 27:bc79f444883b | 320 | |
| mutech | 27:bc79f444883b | 321 | HSVPixels::~HSVPixels() |
| mutech | 27:bc79f444883b | 322 | { |
| mutech | 32:64c391617f6c | 323 | setPixelBuffer(0, 0); |
| mutech | 27:bc79f444883b | 324 | } |
| mutech | 27:bc79f444883b | 325 | |
| mutech | 27:bc79f444883b | 326 | void HSVPixels::setPixelBuffer(HSVColor *buffer, int maxPixels) |
| mutech | 27:bc79f444883b | 327 | { |
| mutech | 32:64c391617f6c | 328 | if (_owned_buffer && _pixels) |
| mutech | 32:64c391617f6c | 329 | { |
| mutech | 32:64c391617f6c | 330 | #if USE_MALLOC |
| mutech | 32:64c391617f6c | 331 | free(_pixels); |
| mutech | 32:64c391617f6c | 332 | #else |
| mutech | 27:bc79f444883b | 333 | delete[] _pixels; |
| mutech | 32:64c391617f6c | 334 | #endif |
| mutech | 32:64c391617f6c | 335 | } |
| mutech | 32:64c391617f6c | 336 | _owned_buffer = false; |
| mutech | 46:2374900f8845 | 337 | _max_pixels = (maxPixels < 0) ? 0 : (maxPixels > MAX_PIXELS) ? MAX_PIXELS : maxPixels; |
| mutech | 46:2374900f8845 | 338 | _pixels = (!_max_pixels) ? NULL : buffer; |
| mutech | 27:bc79f444883b | 339 | |
| mutech | 46:2374900f8845 | 340 | if (!_pixels && _max_pixels > 0) |
| mutech | 27:bc79f444883b | 341 | { |
| mutech | 32:64c391617f6c | 342 | #if USE_MALLOC |
| mutech | 46:2374900f8845 | 343 | _pixels = static_cast<HSVColor*>(malloc(sizeof(HSVColor)*_max_pixels)); |
| mutech | 32:64c391617f6c | 344 | if (_pixels) |
| mutech | 32:64c391617f6c | 345 | _owned_buffer = true; |
| mutech | 32:64c391617f6c | 346 | else |
| mutech | 46:2374900f8845 | 347 | _max_pixels = 0; |
| mutech | 32:64c391617f6c | 348 | #else |
| mutech | 46:2374900f8845 | 349 | _pixels = new HSVColor[_max_pixels]; |
| mutech | 32:64c391617f6c | 350 | _owned_buffer = true; |
| mutech | 32:64c391617f6c | 351 | #endif |
| mutech | 27:bc79f444883b | 352 | } |
| mutech | 46:2374900f8845 | 353 | _num_pixels = _max_pixels; |
| mutech | 27:bc79f444883b | 354 | clear(); |
| mutech | 27:bc79f444883b | 355 | } |
| mutech | 27:bc79f444883b | 356 | |
| mutech | 27:bc79f444883b | 357 | int HSVPixels::numPixels(int value) |
| mutech | 27:bc79f444883b | 358 | { |
| mutech | 27:bc79f444883b | 359 | if (value >= 0) |
| mutech | 46:2374900f8845 | 360 | _num_pixels = (value > _max_pixels) ? _max_pixels : value; |
| mutech | 46:2374900f8845 | 361 | return _num_pixels; |
| mutech | 27:bc79f444883b | 362 | } |
| mutech | 27:bc79f444883b | 363 | |
| mutech | 27:bc79f444883b | 364 | // 指定位置のピクセルへ色配列を指定サイズ分をコピーする |
| mutech | 27:bc79f444883b | 365 | void HSVPixels::setPixels(int index, HSVColor *color, int len) |
| mutech | 27:bc79f444883b | 366 | { |
| mutech | 46:2374900f8845 | 367 | int numPixels = static_cast<int>(_num_pixels); |
| mutech | 46:2374900f8845 | 368 | if (_pixels && len > 0 && index < numPixels && (index + len) > 0) |
| mutech | 27:bc79f444883b | 369 | { |
| mutech | 34:5a141ed5d52a | 370 | if (index < 0) |
| mutech | 35:dffa06d09fdc | 371 | { |
| mutech | 35:dffa06d09fdc | 372 | len += index; |
| mutech | 35:dffa06d09fdc | 373 | color -= index; |
| mutech | 35:dffa06d09fdc | 374 | index = 0; |
| mutech | 35:dffa06d09fdc | 375 | } |
| mutech | 35:dffa06d09fdc | 376 | if (index + len > numPixels) |
| mutech | 35:dffa06d09fdc | 377 | len = numPixels - index; |
| mutech | 27:bc79f444883b | 378 | memcpy(&_pixels[index], color, len * sizeof(_pixels[0])); |
| mutech | 27:bc79f444883b | 379 | } |
| mutech | 27:bc79f444883b | 380 | } |
| mutech | 27:bc79f444883b | 381 | |
| mutech | 27:bc79f444883b | 382 | void HSVPixels::setPixels(int index, RGBColor *color, int len) |
| mutech | 27:bc79f444883b | 383 | { |
| mutech | 46:2374900f8845 | 384 | int numPixels = static_cast<int>(_num_pixels); |
| mutech | 46:2374900f8845 | 385 | if (_pixels && len > 0 && index < numPixels && (index + len) > 0) |
| mutech | 27:bc79f444883b | 386 | { |
| mutech | 34:5a141ed5d52a | 387 | if (index < 0) |
| mutech | 34:5a141ed5d52a | 388 | { |
| mutech | 35:dffa06d09fdc | 389 | len += index; |
| mutech | 35:dffa06d09fdc | 390 | color -= index; |
| mutech | 35:dffa06d09fdc | 391 | index = 0; |
| mutech | 34:5a141ed5d52a | 392 | } |
| mutech | 35:dffa06d09fdc | 393 | if (index + len > numPixels) |
| mutech | 35:dffa06d09fdc | 394 | len = numPixels - index; |
| mutech | 27:bc79f444883b | 395 | HSVColor *dest = &_pixels[index]; |
| mutech | 27:bc79f444883b | 396 | do |
| mutech | 27:bc79f444883b | 397 | { |
| mutech | 27:bc79f444883b | 398 | *dest++ = *color++; |
| mutech | 27:bc79f444883b | 399 | } while (--len); |
| mutech | 27:bc79f444883b | 400 | } |
| mutech | 27:bc79f444883b | 401 | } |
| mutech | 27:bc79f444883b | 402 | |
| mutech | 27:bc79f444883b | 403 | // 指定色を指定位置のピクセルから指定サイズ分書き込む |
| mutech | 29:a362df191524 | 404 | void HSVPixels::fillPixels(int index, const HSVColor color, int len) |
| mutech | 27:bc79f444883b | 405 | { |
| mutech | 46:2374900f8845 | 406 | int numPixels = static_cast<int>(_num_pixels); |
| mutech | 46:2374900f8845 | 407 | if (_pixels && len > 0 && index < numPixels && (index + len) > 0) |
| mutech | 27:bc79f444883b | 408 | { |
| mutech | 36:0fe7917a832a | 409 | if (index < 0) |
| mutech | 36:0fe7917a832a | 410 | { |
| mutech | 36:0fe7917a832a | 411 | len += index; |
| mutech | 36:0fe7917a832a | 412 | index = 0; |
| mutech | 36:0fe7917a832a | 413 | } |
| mutech | 36:0fe7917a832a | 414 | if (index + len > numPixels) |
| mutech | 36:0fe7917a832a | 415 | len = numPixels - index; |
| mutech | 27:bc79f444883b | 416 | _pixels[index] = color; |
| mutech | 27:bc79f444883b | 417 | repeat_buffer<HSVColor>(_pixels + index, len, 1); |
| mutech | 27:bc79f444883b | 418 | } |
| mutech | 27:bc79f444883b | 419 | } |
| mutech | 27:bc79f444883b | 420 | |
| mutech | 29:a362df191524 | 421 | void HSVPixels::fillPixels(int index, const RGBColor color, int len) |
| mutech | 27:bc79f444883b | 422 | { |
| mutech | 29:a362df191524 | 423 | fillPixels(index, HSVColor(color), len); |
| mutech | 27:bc79f444883b | 424 | } |
| mutech | 27:bc79f444883b | 425 | |
| mutech | 27:bc79f444883b | 426 | // 先頭から指定サイズ分のブロックをバッファの最後までコピーする |
| mutech | 27:bc79f444883b | 427 | void HSVPixels::repeatPixels(int block_size) |
| mutech | 27:bc79f444883b | 428 | { |
| mutech | 46:2374900f8845 | 429 | if (_pixels && block_size > 0 && block_size < _num_pixels) |
| mutech | 27:bc79f444883b | 430 | { |
| mutech | 46:2374900f8845 | 431 | repeat_buffer<HSVColor>(_pixels, _num_pixels, block_size); |
| mutech | 27:bc79f444883b | 432 | } |
| mutech | 27:bc79f444883b | 433 | } |
| mutech | 27:bc79f444883b | 434 | |
| mutech | 27:bc79f444883b | 435 | void HSVPixels::repeatPixels(HSVColor *source, int size) |
| mutech | 27:bc79f444883b | 436 | { |
| mutech | 27:bc79f444883b | 437 | if (_pixels && source && size > 0) |
| mutech | 27:bc79f444883b | 438 | { |
| mutech | 46:2374900f8845 | 439 | if (size > _num_pixels) |
| mutech | 46:2374900f8845 | 440 | size = _num_pixels; |
| mutech | 27:bc79f444883b | 441 | memcpy(_pixels, source, size * sizeof(_pixels[0])); |
| mutech | 46:2374900f8845 | 442 | repeat_buffer<HSVColor>(_pixels, _num_pixels, size); |
| mutech | 27:bc79f444883b | 443 | } |
| mutech | 27:bc79f444883b | 444 | } |
| mutech | 27:bc79f444883b | 445 | |
| mutech | 27:bc79f444883b | 446 | void HSVPixels::repeatPixels(RGBColor *source, int size) |
| mutech | 27:bc79f444883b | 447 | { |
| mutech | 27:bc79f444883b | 448 | if (_pixels && source && size > 0) |
| mutech | 27:bc79f444883b | 449 | { |
| mutech | 46:2374900f8845 | 450 | if (size > _num_pixels) |
| mutech | 46:2374900f8845 | 451 | size = _num_pixels; |
| mutech | 27:bc79f444883b | 452 | for (int i = 0; i < size; ++i) |
| mutech | 27:bc79f444883b | 453 | _pixels[i] = *source++; |
| mutech | 46:2374900f8845 | 454 | repeat_buffer<HSVColor>(_pixels, _num_pixels, size); |
| mutech | 27:bc79f444883b | 455 | } |
| mutech | 27:bc79f444883b | 456 | } |
| mutech | 27:bc79f444883b | 457 | |
| mutech | 27:bc79f444883b | 458 | HSVPixels& HSVPixels::operator=(const HSVPixels& rhs) |
| mutech | 27:bc79f444883b | 459 | { |
| mutech | 46:2374900f8845 | 460 | if (!rhs._pixels || !rhs._max_pixels) |
| mutech | 27:bc79f444883b | 461 | { |
| mutech | 27:bc79f444883b | 462 | // 右辺が空の場合何もしない |
| mutech | 27:bc79f444883b | 463 | return *this; |
| mutech | 27:bc79f444883b | 464 | } |
| mutech | 46:2374900f8845 | 465 | if (!_pixels || !_max_pixels) |
| mutech | 27:bc79f444883b | 466 | { |
| mutech | 27:bc79f444883b | 467 | // 自分のバッファなしの場合、、新規確保 |
| mutech | 46:2374900f8845 | 468 | setPixelBuffer(nullptr, rhs._max_pixels); |
| mutech | 27:bc79f444883b | 469 | } |
| mutech | 27:bc79f444883b | 470 | |
| mutech | 46:2374900f8845 | 471 | if (_pixels && _max_pixels) |
| mutech | 27:bc79f444883b | 472 | { |
| mutech | 46:2374900f8845 | 473 | _num_pixels = rhs._num_pixels; |
| mutech | 46:2374900f8845 | 474 | if (_num_pixels > rhs._max_pixels) |
| mutech | 46:2374900f8845 | 475 | _num_pixels = rhs._max_pixels; |
| mutech | 46:2374900f8845 | 476 | if (_num_pixels > _max_pixels) |
| mutech | 46:2374900f8845 | 477 | _num_pixels = _max_pixels; |
| mutech | 46:2374900f8845 | 478 | memcpy(_pixels, rhs._pixels, sizeof(_pixels[0]) * _num_pixels); |
| mutech | 27:bc79f444883b | 479 | } |
| mutech | 27:bc79f444883b | 480 | |
| mutech | 27:bc79f444883b | 481 | return *this; |
| mutech | 27:bc79f444883b | 482 | } |
| mutech | 27:bc79f444883b | 483 | |
| mutech | 27:bc79f444883b | 484 | //---------------------------------------------------------------------------- |
| mutech | 28:b452e097da53 | 485 | void HSVPixels::makeGradation(int index, HSVColor from, HSVColor to, int len) |
| mutech | 28:b452e097da53 | 486 | { |
| mutech | 46:2374900f8845 | 487 | if (!_pixels || len < 1 || index >= _num_pixels || (index + len) <= 0) |
| mutech | 28:b452e097da53 | 488 | return; |
| mutech | 28:b452e097da53 | 489 | |
| mutech | 28:b452e097da53 | 490 | int end = len; |
| mutech | 46:2374900f8845 | 491 | if (index + end > _num_pixels) |
| mutech | 46:2374900f8845 | 492 | end = _num_pixels - index; |
| mutech | 28:b452e097da53 | 493 | |
| mutech | 28:b452e097da53 | 494 | RGBColor rgb_from(from); |
| mutech | 28:b452e097da53 | 495 | RGBColor rgb_to(to); |
| mutech | 28:b452e097da53 | 496 | RGBColor color; |
| mutech | 28:b452e097da53 | 497 | HSVColor *dest = _pixels; |
| mutech | 28:b452e097da53 | 498 | if (index > 0) |
| mutech | 28:b452e097da53 | 499 | dest += index; |
| mutech | 28:b452e097da53 | 500 | for (int i = (index < 0) ? -index : 0; i < end; ++i) |
| mutech | 28:b452e097da53 | 501 | { |
| mutech | 28:b452e097da53 | 502 | int j = len - i; |
| mutech | 28:b452e097da53 | 503 | color.red = ((rgb_from.red * j) + (rgb_to.red * i)) / len; |
| mutech | 28:b452e097da53 | 504 | color.green = ((rgb_from.green * j) + (rgb_to.green * i)) / len; |
| mutech | 28:b452e097da53 | 505 | color.blue = ((rgb_from.blue * j) + (rgb_to.blue * i)) / len; |
| mutech | 28:b452e097da53 | 506 | *dest++ = GammaColor(color); |
| mutech | 28:b452e097da53 | 507 | } |
| mutech | 28:b452e097da53 | 508 | } |
| mutech | 28:b452e097da53 | 509 | |
| mutech | 28:b452e097da53 | 510 | void HSVPixels::makeRainbow(int index, HSVColor color, int len, int direction) |
| mutech | 28:b452e097da53 | 511 | { |
| mutech | 46:2374900f8845 | 512 | if (!_pixels || len < 1 || index >= _num_pixels || (index + len) <= 0) |
| mutech | 28:b452e097da53 | 513 | return; |
| mutech | 28:b452e097da53 | 514 | |
| mutech | 28:b452e097da53 | 515 | int end = len; |
| mutech | 46:2374900f8845 | 516 | if (index + end > _num_pixels) |
| mutech | 46:2374900f8845 | 517 | end = _num_pixels - index; |
| mutech | 28:b452e097da53 | 518 | |
| mutech | 28:b452e097da53 | 519 | HSVColor hsv(color); |
| mutech | 28:b452e097da53 | 520 | HSVColor *dest = _pixels; |
| mutech | 28:b452e097da53 | 521 | if (index > 0) |
| mutech | 28:b452e097da53 | 522 | dest += index; |
| mutech | 28:b452e097da53 | 523 | direction = (direction >= 0) ? -3600 : 3600; |
| mutech | 28:b452e097da53 | 524 | for (int i = (index < 0) ? -index : 0; i < end; ++i) |
| mutech | 28:b452e097da53 | 525 | { |
| mutech | 28:b452e097da53 | 526 | hsv.hue = color.hue + direction * i / len; |
| mutech | 29:a362df191524 | 527 | *dest++ = GammaColor(hsv); |
| mutech | 28:b452e097da53 | 528 | } |
| mutech | 28:b452e097da53 | 529 | } |
| mutech | 28:b452e097da53 | 530 | |
| mutech | 28:b452e097da53 | 531 | //---------------------------------------------------------------------------- |