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@28:b452e097da53, 2016-09-07 (annotated)
- Committer:
- mutech
- Date:
- Wed Sep 07 21:07:17 2016 +0000
- Revision:
- 28:b452e097da53
- Parent:
- 27:bc79f444883b
- Child:
- 29:a362df191524
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 | 27:bc79f444883b | 11 | //---------------------------------------------------------------------------- |
| mutech | 27:bc79f444883b | 12 | // 指定されたバッファの先頭からblock_size分をbuf_sizeが満杯になるまで繰り返しコピーする |
| mutech | 27:bc79f444883b | 13 | template <class T> |
| mutech | 27:bc79f444883b | 14 | static void repeat_buffer(T *buffer, int buf_size, int block_size = 1) |
| mutech | 27:bc79f444883b | 15 | { |
| mutech | 27:bc79f444883b | 16 | if (buffer && block_size > 0 && (uint16_t)block_size < buf_size) |
| mutech | 27:bc79f444883b | 17 | { |
| mutech | 27:bc79f444883b | 18 | T *dest = buffer + block_size; |
| mutech | 27:bc79f444883b | 19 | int left = buf_size - block_size; |
| mutech | 27:bc79f444883b | 20 | while (left > block_size) |
| mutech | 27:bc79f444883b | 21 | { |
| mutech | 27:bc79f444883b | 22 | memcpy(dest, buffer, block_size * sizeof(T)); |
| mutech | 27:bc79f444883b | 23 | dest += block_size; |
| mutech | 27:bc79f444883b | 24 | left -= block_size; |
| mutech | 27:bc79f444883b | 25 | block_size <<= 1; // 次回は2倍のサイズの転送 |
| mutech | 27:bc79f444883b | 26 | } |
| mutech | 27:bc79f444883b | 27 | memcpy(dest, buffer, left * sizeof(T)); |
| mutech | 27:bc79f444883b | 28 | } |
| mutech | 28:b452e097da53 | 29 | } |
| mutech | 27:bc79f444883b | 30 | |
| mutech | 27:bc79f444883b | 31 | //---------------------------------------------------------------------------- |
| mutech | 27:bc79f444883b | 32 | RGBPixels::RGBPixels(RGBColor *buffer, int maxPixels) |
| mutech | 27:bc79f444883b | 33 | : _buf_owner(false) |
| mutech | 27:bc79f444883b | 34 | { |
| mutech | 27:bc79f444883b | 35 | _dummyPixel = 0; |
| mutech | 27:bc79f444883b | 36 | setPixelBuffer(buffer, maxPixels); |
| mutech | 27:bc79f444883b | 37 | } |
| mutech | 27:bc79f444883b | 38 | |
| mutech | 27:bc79f444883b | 39 | RGBPixels::RGBPixels(int maxPixels) |
| mutech | 27:bc79f444883b | 40 | : _buf_owner(false) |
| mutech | 27:bc79f444883b | 41 | { |
| mutech | 27:bc79f444883b | 42 | _dummyPixel = 0; |
| mutech | 27:bc79f444883b | 43 | setPixelBuffer(nullptr, maxPixels); |
| mutech | 27:bc79f444883b | 44 | } |
| mutech | 27:bc79f444883b | 45 | |
| mutech | 27:bc79f444883b | 46 | RGBPixels::~RGBPixels() |
| mutech | 27:bc79f444883b | 47 | { |
| mutech | 27:bc79f444883b | 48 | if (_buf_owner && _pixels) |
| mutech | 27:bc79f444883b | 49 | delete[] _pixels; |
| mutech | 27:bc79f444883b | 50 | } |
| mutech | 27:bc79f444883b | 51 | |
| mutech | 27:bc79f444883b | 52 | void RGBPixels::setPixelBuffer(RGBColor *buffer, int maxPixels) |
| mutech | 27:bc79f444883b | 53 | { |
| mutech | 27:bc79f444883b | 54 | if (_buf_owner && _pixels) |
| mutech | 27:bc79f444883b | 55 | delete[] _pixels; |
| mutech | 27:bc79f444883b | 56 | |
| mutech | 27:bc79f444883b | 57 | _buf_owner = false; |
| mutech | 27:bc79f444883b | 58 | _pixels = buffer; |
| mutech | 27:bc79f444883b | 59 | |
| mutech | 27:bc79f444883b | 60 | _maxPixels = (maxPixels < 0) ? 0 : (maxPixels > MAX_PIXELS) ? MAX_PIXELS : maxPixels; |
| mutech | 27:bc79f444883b | 61 | if (!_pixels && _maxPixels > 0) |
| mutech | 27:bc79f444883b | 62 | { |
| mutech | 27:bc79f444883b | 63 | _pixels = new RGBColor[_maxPixels]; |
| mutech | 28:b452e097da53 | 64 | _buf_owner = true; |
| mutech | 27:bc79f444883b | 65 | } |
| mutech | 27:bc79f444883b | 66 | _numPixels = _maxPixels; |
| mutech | 27:bc79f444883b | 67 | clear(); |
| mutech | 27:bc79f444883b | 68 | } |
| mutech | 27:bc79f444883b | 69 | |
| mutech | 27:bc79f444883b | 70 | int RGBPixels::numPixels(int value) |
| mutech | 27:bc79f444883b | 71 | { |
| mutech | 27:bc79f444883b | 72 | if (value >= 0) |
| mutech | 27:bc79f444883b | 73 | _numPixels = (value > _maxPixels) ? _maxPixels : value; |
| mutech | 27:bc79f444883b | 74 | return _numPixels; |
| mutech | 27:bc79f444883b | 75 | } |
| mutech | 27:bc79f444883b | 76 | |
| mutech | 27:bc79f444883b | 77 | // 指定位置のピクセルへ色配列を指定サイズ分をコピーする |
| mutech | 27:bc79f444883b | 78 | void RGBPixels::setPixels(int index, RGBColor *color, int len) |
| mutech | 27:bc79f444883b | 79 | { |
| mutech | 27:bc79f444883b | 80 | if (_pixels && len > 0 && (uint16_t)index < _numPixels) |
| mutech | 27:bc79f444883b | 81 | { |
| mutech | 27:bc79f444883b | 82 | if (index + len > _numPixels) |
| mutech | 27:bc79f444883b | 83 | len = _numPixels - index; |
| mutech | 27:bc79f444883b | 84 | memcpy(&_pixels[index], color, len * sizeof(_pixels[0])); |
| mutech | 27:bc79f444883b | 85 | } |
| mutech | 27:bc79f444883b | 86 | } |
| mutech | 27:bc79f444883b | 87 | |
| mutech | 27:bc79f444883b | 88 | void RGBPixels::setPixels(int index, HSVColor *color, int len) |
| mutech | 27:bc79f444883b | 89 | { |
| mutech | 27:bc79f444883b | 90 | if (_pixels && len > 0 && (uint16_t)index < _numPixels) |
| mutech | 27:bc79f444883b | 91 | { |
| mutech | 27:bc79f444883b | 92 | if (index + len > _numPixels) |
| mutech | 27:bc79f444883b | 93 | len = _numPixels - index; |
| mutech | 27:bc79f444883b | 94 | RGBColor *dest = &_pixels[index]; |
| mutech | 27:bc79f444883b | 95 | do |
| mutech | 27:bc79f444883b | 96 | { |
| mutech | 27:bc79f444883b | 97 | *dest++ = *color++; |
| mutech | 27:bc79f444883b | 98 | } while (--len); |
| mutech | 27:bc79f444883b | 99 | } |
| mutech | 27:bc79f444883b | 100 | } |
| mutech | 27:bc79f444883b | 101 | |
| mutech | 27:bc79f444883b | 102 | // 指定色を指定位置のピクセルから指定サイズ分書き込む |
| mutech | 28:b452e097da53 | 103 | void RGBPixels::fillPixels(int index, const RGBColor color, int len) |
| mutech | 27:bc79f444883b | 104 | { |
| mutech | 27:bc79f444883b | 105 | if (_pixels && len > 0 && (uint16_t)index < _numPixels) |
| mutech | 27:bc79f444883b | 106 | { |
| mutech | 27:bc79f444883b | 107 | if (index + len > _numPixels) |
| mutech | 27:bc79f444883b | 108 | len = _numPixels - index; |
| mutech | 27:bc79f444883b | 109 | _pixels[index] = color; |
| mutech | 27:bc79f444883b | 110 | repeat_buffer<RGBColor>(_pixels + index, len, 1); |
| mutech | 27:bc79f444883b | 111 | } |
| mutech | 27:bc79f444883b | 112 | } |
| mutech | 27:bc79f444883b | 113 | |
| mutech | 28:b452e097da53 | 114 | void RGBPixels::fillPixels(int index, const HSVColor color, int len) |
| mutech | 27:bc79f444883b | 115 | { |
| mutech | 27:bc79f444883b | 116 | fillPixels(RGBColor(color), index, len); |
| mutech | 27:bc79f444883b | 117 | } |
| mutech | 27:bc79f444883b | 118 | |
| mutech | 27:bc79f444883b | 119 | // 先頭から指定サイズ分のブロックをバッファの最後までコピーする |
| mutech | 27:bc79f444883b | 120 | void RGBPixels::repeatPixels(int block_size) |
| mutech | 27:bc79f444883b | 121 | { |
| mutech | 27:bc79f444883b | 122 | if (_pixels && block_size > 0 && block_size < _numPixels) |
| mutech | 27:bc79f444883b | 123 | { |
| mutech | 27:bc79f444883b | 124 | repeat_buffer<RGBColor>(_pixels, _numPixels, block_size); |
| mutech | 27:bc79f444883b | 125 | } |
| mutech | 27:bc79f444883b | 126 | } |
| mutech | 27:bc79f444883b | 127 | |
| mutech | 27:bc79f444883b | 128 | void RGBPixels::repeatPixels(RGBColor *source, int size) |
| mutech | 27:bc79f444883b | 129 | { |
| mutech | 27:bc79f444883b | 130 | if (_pixels && source && size > 0) |
| mutech | 27:bc79f444883b | 131 | { |
| mutech | 27:bc79f444883b | 132 | if (size > _numPixels) |
| mutech | 27:bc79f444883b | 133 | size = _numPixels; |
| mutech | 27:bc79f444883b | 134 | memcpy(_pixels, source, size * sizeof(_pixels[0])); |
| mutech | 27:bc79f444883b | 135 | repeat_buffer<RGBColor>(_pixels, _numPixels, size); |
| mutech | 27:bc79f444883b | 136 | } |
| mutech | 27:bc79f444883b | 137 | } |
| mutech | 27:bc79f444883b | 138 | |
| mutech | 27:bc79f444883b | 139 | void RGBPixels::repeatPixels(HSVColor *source, int size) |
| mutech | 27:bc79f444883b | 140 | { |
| mutech | 27:bc79f444883b | 141 | if (_pixels && source && size > 0) |
| mutech | 27:bc79f444883b | 142 | { |
| mutech | 27:bc79f444883b | 143 | if (size > _numPixels) |
| mutech | 27:bc79f444883b | 144 | size = _numPixels; |
| mutech | 27:bc79f444883b | 145 | for (int i = 0; i < size; ++i) |
| mutech | 27:bc79f444883b | 146 | _pixels[i] = *source++; |
| mutech | 27:bc79f444883b | 147 | repeat_buffer<RGBColor>(_pixels, _numPixels, size); |
| mutech | 27:bc79f444883b | 148 | } |
| mutech | 27:bc79f444883b | 149 | } |
| mutech | 27:bc79f444883b | 150 | |
| mutech | 27:bc79f444883b | 151 | RGBPixels& RGBPixels::operator=(const RGBPixels& rhs) |
| mutech | 27:bc79f444883b | 152 | { |
| mutech | 27:bc79f444883b | 153 | if (!rhs._pixels || !rhs._maxPixels) |
| mutech | 27:bc79f444883b | 154 | { |
| mutech | 27:bc79f444883b | 155 | // 右辺が空の場合何もしない |
| mutech | 27:bc79f444883b | 156 | return *this; |
| mutech | 27:bc79f444883b | 157 | } |
| mutech | 27:bc79f444883b | 158 | if (!_pixels || !_maxPixels) |
| mutech | 27:bc79f444883b | 159 | { |
| mutech | 27:bc79f444883b | 160 | // 自分のバッファなしの場合、、新規確保 |
| mutech | 27:bc79f444883b | 161 | setPixelBuffer(nullptr, rhs._maxPixels); |
| mutech | 27:bc79f444883b | 162 | } |
| mutech | 27:bc79f444883b | 163 | |
| mutech | 27:bc79f444883b | 164 | if (_pixels && _maxPixels) |
| mutech | 27:bc79f444883b | 165 | { |
| mutech | 27:bc79f444883b | 166 | _numPixels = rhs._numPixels; |
| mutech | 27:bc79f444883b | 167 | if (_numPixels > rhs._maxPixels) |
| mutech | 27:bc79f444883b | 168 | _numPixels = rhs._maxPixels; |
| mutech | 27:bc79f444883b | 169 | if (_numPixels > _maxPixels) |
| mutech | 27:bc79f444883b | 170 | _numPixels = _maxPixels; |
| mutech | 27:bc79f444883b | 171 | memcpy(_pixels, rhs._pixels, sizeof(_pixels[0]) * _numPixels); |
| mutech | 27:bc79f444883b | 172 | } |
| mutech | 27:bc79f444883b | 173 | |
| mutech | 27:bc79f444883b | 174 | return *this; |
| mutech | 27:bc79f444883b | 175 | } |
| mutech | 27:bc79f444883b | 176 | |
| mutech | 27:bc79f444883b | 177 | //---------------------------------------------------------------------------- |
| mutech | 28:b452e097da53 | 178 | void RGBPixels::makeGradation(int index, RGBColor from, RGBColor to, int len) |
| mutech | 28:b452e097da53 | 179 | { |
| mutech | 28:b452e097da53 | 180 | if (!_pixels || len < 1 || index >= _numPixels || (index + len) < 0) |
| mutech | 28:b452e097da53 | 181 | return; |
| mutech | 28:b452e097da53 | 182 | |
| mutech | 28:b452e097da53 | 183 | int end = len; |
| mutech | 28:b452e097da53 | 184 | if (index + end > _numPixels) |
| mutech | 28:b452e097da53 | 185 | end = _numPixels - index; |
| mutech | 28:b452e097da53 | 186 | |
| mutech | 28:b452e097da53 | 187 | RGBColor color; |
| mutech | 28:b452e097da53 | 188 | RGBColor *dest = _pixels; |
| mutech | 28:b452e097da53 | 189 | if (index > 0) |
| mutech | 28:b452e097da53 | 190 | dest += index; |
| mutech | 28:b452e097da53 | 191 | for (int i = (index < 0) ? -index : 0; i < end; ++i) |
| mutech | 28:b452e097da53 | 192 | { |
| mutech | 28:b452e097da53 | 193 | int j = len - i; |
| mutech | 28:b452e097da53 | 194 | color.red = ((from.red * j) + (to.red * i)) / len; |
| mutech | 28:b452e097da53 | 195 | color.green = ((from.green * j) + (to.green * i)) / len; |
| mutech | 28:b452e097da53 | 196 | color.blue = ((from.blue * j) + (to.blue * i)) / len; |
| mutech | 28:b452e097da53 | 197 | *dest++ = GammaColor(color); |
| mutech | 28:b452e097da53 | 198 | } |
| mutech | 28:b452e097da53 | 199 | } |
| mutech | 28:b452e097da53 | 200 | |
| mutech | 28:b452e097da53 | 201 | void RGBPixels::makeRainbow(int index, HSVColor color, int len, int direction) |
| mutech | 28:b452e097da53 | 202 | { |
| mutech | 28:b452e097da53 | 203 | if (!_pixels || len < 1 || index >= _numPixels || (index + len) < 0) |
| mutech | 28:b452e097da53 | 204 | return; |
| mutech | 28:b452e097da53 | 205 | |
| mutech | 28:b452e097da53 | 206 | int end = len; |
| mutech | 28:b452e097da53 | 207 | if (index + end > _numPixels) |
| mutech | 28:b452e097da53 | 208 | end = _numPixels - index; |
| mutech | 28:b452e097da53 | 209 | |
| mutech | 28:b452e097da53 | 210 | HSVColor hsv(color); |
| mutech | 28:b452e097da53 | 211 | RGBColor *dest = _pixels; |
| mutech | 28:b452e097da53 | 212 | if (index > 0) |
| mutech | 28:b452e097da53 | 213 | dest += index; |
| mutech | 28:b452e097da53 | 214 | direction = (direction >= 0) ? -3600 : 3600; |
| mutech | 28:b452e097da53 | 215 | for (int i = (index < 0) ? -index : 0; i < end; ++i) |
| mutech | 28:b452e097da53 | 216 | { |
| mutech | 28:b452e097da53 | 217 | hsv.hue = color.hue + direction * i / len; |
| mutech | 28:b452e097da53 | 218 | *dest++ = GammaColor(hsv); |
| mutech | 28:b452e097da53 | 219 | } |
| mutech | 28:b452e097da53 | 220 | } |
| mutech | 28:b452e097da53 | 221 | |
| mutech | 28:b452e097da53 | 222 | //---------------------------------------------------------------------------- |
| mutech | 27:bc79f444883b | 223 | //---------------------------------------------------------------------------- |
| mutech | 27:bc79f444883b | 224 | HSVPixels::HSVPixels(HSVColor *buffer, int maxPixels) |
| mutech | 27:bc79f444883b | 225 | : _buf_owner(false) |
| mutech | 27:bc79f444883b | 226 | { |
| mutech | 27:bc79f444883b | 227 | _dummyPixel = 0; |
| mutech | 27:bc79f444883b | 228 | setPixelBuffer(buffer, maxPixels); |
| mutech | 27:bc79f444883b | 229 | } |
| mutech | 27:bc79f444883b | 230 | |
| mutech | 27:bc79f444883b | 231 | HSVPixels::HSVPixels(int maxPixels) |
| mutech | 27:bc79f444883b | 232 | : _buf_owner(false) |
| mutech | 27:bc79f444883b | 233 | { |
| mutech | 27:bc79f444883b | 234 | _dummyPixel = 0; |
| mutech | 27:bc79f444883b | 235 | setPixelBuffer(nullptr, maxPixels); |
| mutech | 27:bc79f444883b | 236 | } |
| mutech | 27:bc79f444883b | 237 | |
| mutech | 27:bc79f444883b | 238 | HSVPixels::~HSVPixels() |
| mutech | 27:bc79f444883b | 239 | { |
| mutech | 27:bc79f444883b | 240 | if (_buf_owner && _pixels) |
| mutech | 27:bc79f444883b | 241 | delete[] _pixels; |
| mutech | 27:bc79f444883b | 242 | } |
| mutech | 27:bc79f444883b | 243 | |
| mutech | 27:bc79f444883b | 244 | void HSVPixels::setPixelBuffer(HSVColor *buffer, int maxPixels) |
| mutech | 27:bc79f444883b | 245 | { |
| mutech | 27:bc79f444883b | 246 | if (_buf_owner && _pixels) |
| mutech | 27:bc79f444883b | 247 | delete[] _pixels; |
| mutech | 27:bc79f444883b | 248 | |
| mutech | 27:bc79f444883b | 249 | _buf_owner = false; |
| mutech | 27:bc79f444883b | 250 | _pixels = buffer; |
| mutech | 27:bc79f444883b | 251 | |
| mutech | 27:bc79f444883b | 252 | _maxPixels = (maxPixels < 0) ? 0 : (maxPixels > MAX_PIXELS) ? MAX_PIXELS : maxPixels; |
| mutech | 27:bc79f444883b | 253 | if (!_pixels && _maxPixels > 0) |
| mutech | 27:bc79f444883b | 254 | { |
| mutech | 27:bc79f444883b | 255 | _pixels = new HSVColor[_maxPixels]; |
| mutech | 28:b452e097da53 | 256 | _buf_owner = true; |
| mutech | 27:bc79f444883b | 257 | } |
| mutech | 27:bc79f444883b | 258 | _numPixels = _maxPixels; |
| mutech | 27:bc79f444883b | 259 | clear(); |
| mutech | 27:bc79f444883b | 260 | } |
| mutech | 27:bc79f444883b | 261 | |
| mutech | 27:bc79f444883b | 262 | int HSVPixels::numPixels(int value) |
| mutech | 27:bc79f444883b | 263 | { |
| mutech | 27:bc79f444883b | 264 | if (value >= 0) |
| mutech | 27:bc79f444883b | 265 | _numPixels = (value > _maxPixels) ? _maxPixels : value; |
| mutech | 27:bc79f444883b | 266 | return _numPixels; |
| mutech | 27:bc79f444883b | 267 | } |
| mutech | 27:bc79f444883b | 268 | |
| mutech | 27:bc79f444883b | 269 | // 指定位置のピクセルへ色配列を指定サイズ分をコピーする |
| mutech | 27:bc79f444883b | 270 | void HSVPixels::setPixels(int index, HSVColor *color, int len) |
| mutech | 27:bc79f444883b | 271 | { |
| mutech | 27:bc79f444883b | 272 | if (_pixels && len > 0 && (uint16_t)index < _numPixels) |
| mutech | 27:bc79f444883b | 273 | { |
| mutech | 27:bc79f444883b | 274 | if (index + len > _numPixels) |
| mutech | 27:bc79f444883b | 275 | len = _numPixels - index; |
| mutech | 27:bc79f444883b | 276 | memcpy(&_pixels[index], color, len * sizeof(_pixels[0])); |
| mutech | 27:bc79f444883b | 277 | } |
| mutech | 27:bc79f444883b | 278 | } |
| mutech | 27:bc79f444883b | 279 | |
| mutech | 27:bc79f444883b | 280 | void HSVPixels::setPixels(int index, RGBColor *color, int len) |
| mutech | 27:bc79f444883b | 281 | { |
| mutech | 27:bc79f444883b | 282 | if (_pixels && len > 0 && (uint16_t)index < _numPixels) |
| mutech | 27:bc79f444883b | 283 | { |
| mutech | 27:bc79f444883b | 284 | if (index + len > _numPixels) |
| mutech | 27:bc79f444883b | 285 | len = _numPixels - index; |
| mutech | 27:bc79f444883b | 286 | HSVColor *dest = &_pixels[index]; |
| mutech | 27:bc79f444883b | 287 | do |
| mutech | 27:bc79f444883b | 288 | { |
| mutech | 27:bc79f444883b | 289 | *dest++ = *color++; |
| mutech | 27:bc79f444883b | 290 | } while (--len); |
| mutech | 27:bc79f444883b | 291 | } |
| mutech | 27:bc79f444883b | 292 | } |
| mutech | 27:bc79f444883b | 293 | |
| mutech | 27:bc79f444883b | 294 | // 指定色を指定位置のピクセルから指定サイズ分書き込む |
| mutech | 27:bc79f444883b | 295 | void HSVPixels::fillPixels(const HSVColor color, int index, int len) |
| mutech | 27:bc79f444883b | 296 | { |
| mutech | 27:bc79f444883b | 297 | if (_pixels && len > 0 && (uint16_t)index < _numPixels) |
| mutech | 27:bc79f444883b | 298 | { |
| mutech | 27:bc79f444883b | 299 | if (index + len > _numPixels) |
| mutech | 27:bc79f444883b | 300 | len = _numPixels - index; |
| mutech | 27:bc79f444883b | 301 | _pixels[index] = color; |
| mutech | 27:bc79f444883b | 302 | repeat_buffer<HSVColor>(_pixels + index, len, 1); |
| mutech | 27:bc79f444883b | 303 | } |
| mutech | 27:bc79f444883b | 304 | } |
| mutech | 27:bc79f444883b | 305 | |
| mutech | 27:bc79f444883b | 306 | void HSVPixels::fillPixels(const RGBColor color, int index, int len) |
| mutech | 27:bc79f444883b | 307 | { |
| mutech | 27:bc79f444883b | 308 | fillPixels(HSVColor(color), index, len); |
| mutech | 27:bc79f444883b | 309 | } |
| mutech | 27:bc79f444883b | 310 | |
| mutech | 27:bc79f444883b | 311 | // 先頭から指定サイズ分のブロックをバッファの最後までコピーする |
| mutech | 27:bc79f444883b | 312 | void HSVPixels::repeatPixels(int block_size) |
| mutech | 27:bc79f444883b | 313 | { |
| mutech | 27:bc79f444883b | 314 | if (_pixels && block_size > 0 && block_size < _numPixels) |
| mutech | 27:bc79f444883b | 315 | { |
| mutech | 27:bc79f444883b | 316 | repeat_buffer<HSVColor>(_pixels, _numPixels, block_size); |
| mutech | 27:bc79f444883b | 317 | } |
| mutech | 27:bc79f444883b | 318 | } |
| mutech | 27:bc79f444883b | 319 | |
| mutech | 27:bc79f444883b | 320 | void HSVPixels::repeatPixels(HSVColor *source, int size) |
| mutech | 27:bc79f444883b | 321 | { |
| mutech | 27:bc79f444883b | 322 | if (_pixels && source && size > 0) |
| mutech | 27:bc79f444883b | 323 | { |
| mutech | 27:bc79f444883b | 324 | if (size > _numPixels) |
| mutech | 27:bc79f444883b | 325 | size = _numPixels; |
| mutech | 27:bc79f444883b | 326 | memcpy(_pixels, source, size * sizeof(_pixels[0])); |
| mutech | 27:bc79f444883b | 327 | repeat_buffer<HSVColor>(_pixels, _numPixels, size); |
| mutech | 27:bc79f444883b | 328 | } |
| mutech | 27:bc79f444883b | 329 | } |
| mutech | 27:bc79f444883b | 330 | |
| mutech | 27:bc79f444883b | 331 | void HSVPixels::repeatPixels(RGBColor *source, int size) |
| mutech | 27:bc79f444883b | 332 | { |
| mutech | 27:bc79f444883b | 333 | if (_pixels && source && size > 0) |
| mutech | 27:bc79f444883b | 334 | { |
| mutech | 27:bc79f444883b | 335 | if (size > _numPixels) |
| mutech | 27:bc79f444883b | 336 | size = _numPixels; |
| mutech | 27:bc79f444883b | 337 | for (int i = 0; i < size; ++i) |
| mutech | 27:bc79f444883b | 338 | _pixels[i] = *source++; |
| mutech | 27:bc79f444883b | 339 | repeat_buffer<HSVColor>(_pixels, _numPixels, size); |
| mutech | 27:bc79f444883b | 340 | } |
| mutech | 27:bc79f444883b | 341 | } |
| mutech | 27:bc79f444883b | 342 | |
| mutech | 27:bc79f444883b | 343 | HSVPixels& HSVPixels::operator=(const HSVPixels& rhs) |
| mutech | 27:bc79f444883b | 344 | { |
| mutech | 27:bc79f444883b | 345 | if (!rhs._pixels || !rhs._maxPixels) |
| mutech | 27:bc79f444883b | 346 | { |
| mutech | 27:bc79f444883b | 347 | // 右辺が空の場合何もしない |
| mutech | 27:bc79f444883b | 348 | return *this; |
| mutech | 27:bc79f444883b | 349 | } |
| mutech | 27:bc79f444883b | 350 | if (!_pixels || !_maxPixels) |
| mutech | 27:bc79f444883b | 351 | { |
| mutech | 27:bc79f444883b | 352 | // 自分のバッファなしの場合、、新規確保 |
| mutech | 27:bc79f444883b | 353 | setPixelBuffer(nullptr, rhs._maxPixels); |
| mutech | 27:bc79f444883b | 354 | } |
| mutech | 27:bc79f444883b | 355 | |
| mutech | 27:bc79f444883b | 356 | if (_pixels && _maxPixels) |
| mutech | 27:bc79f444883b | 357 | { |
| mutech | 27:bc79f444883b | 358 | _numPixels = rhs._numPixels; |
| mutech | 27:bc79f444883b | 359 | if (_numPixels > rhs._maxPixels) |
| mutech | 27:bc79f444883b | 360 | _numPixels = rhs._maxPixels; |
| mutech | 27:bc79f444883b | 361 | if (_numPixels > _maxPixels) |
| mutech | 27:bc79f444883b | 362 | _numPixels = _maxPixels; |
| mutech | 27:bc79f444883b | 363 | memcpy(_pixels, rhs._pixels, sizeof(_pixels[0]) * _numPixels); |
| mutech | 27:bc79f444883b | 364 | } |
| mutech | 27:bc79f444883b | 365 | |
| mutech | 27:bc79f444883b | 366 | return *this; |
| mutech | 27:bc79f444883b | 367 | } |
| mutech | 27:bc79f444883b | 368 | |
| mutech | 27:bc79f444883b | 369 | //---------------------------------------------------------------------------- |
| mutech | 28:b452e097da53 | 370 | void HSVPixels::makeGradation(int index, HSVColor from, HSVColor to, int len) |
| mutech | 28:b452e097da53 | 371 | { |
| mutech | 28:b452e097da53 | 372 | if (!_pixels || len < 1 || index >= _numPixels || (index + len) < 0) |
| mutech | 28:b452e097da53 | 373 | return; |
| mutech | 28:b452e097da53 | 374 | |
| mutech | 28:b452e097da53 | 375 | int end = len; |
| mutech | 28:b452e097da53 | 376 | if (index + end > _numPixels) |
| mutech | 28:b452e097da53 | 377 | end = _numPixels - index; |
| mutech | 28:b452e097da53 | 378 | |
| mutech | 28:b452e097da53 | 379 | RGBColor rgb_from(from); |
| mutech | 28:b452e097da53 | 380 | RGBColor rgb_to(to); |
| mutech | 28:b452e097da53 | 381 | RGBColor color; |
| mutech | 28:b452e097da53 | 382 | HSVColor *dest = _pixels; |
| mutech | 28:b452e097da53 | 383 | if (index > 0) |
| mutech | 28:b452e097da53 | 384 | dest += index; |
| mutech | 28:b452e097da53 | 385 | for (int i = (index < 0) ? -index : 0; i < end; ++i) |
| mutech | 28:b452e097da53 | 386 | { |
| mutech | 28:b452e097da53 | 387 | int j = len - i; |
| mutech | 28:b452e097da53 | 388 | color.red = ((rgb_from.red * j) + (rgb_to.red * i)) / len; |
| mutech | 28:b452e097da53 | 389 | color.green = ((rgb_from.green * j) + (rgb_to.green * i)) / len; |
| mutech | 28:b452e097da53 | 390 | color.blue = ((rgb_from.blue * j) + (rgb_to.blue * i)) / len; |
| mutech | 28:b452e097da53 | 391 | *dest++ = GammaColor(color); |
| mutech | 28:b452e097da53 | 392 | } |
| mutech | 28:b452e097da53 | 393 | } |
| mutech | 28:b452e097da53 | 394 | |
| mutech | 28:b452e097da53 | 395 | void HSVPixels::makeRainbow(int index, HSVColor color, int len, int direction) |
| mutech | 28:b452e097da53 | 396 | { |
| mutech | 28:b452e097da53 | 397 | if (!_pixels || len < 1 || index >= _numPixels || (index + len) < 0) |
| mutech | 28:b452e097da53 | 398 | return; |
| mutech | 28:b452e097da53 | 399 | |
| mutech | 28:b452e097da53 | 400 | int end = len; |
| mutech | 28:b452e097da53 | 401 | if (index + end > _numPixels) |
| mutech | 28:b452e097da53 | 402 | end = _numPixels - index; |
| mutech | 28:b452e097da53 | 403 | |
| mutech | 28:b452e097da53 | 404 | HSVColor hsv(color); |
| mutech | 28:b452e097da53 | 405 | HSVColor *dest = _pixels; |
| mutech | 28:b452e097da53 | 406 | if (index > 0) |
| mutech | 28:b452e097da53 | 407 | dest += index; |
| mutech | 28:b452e097da53 | 408 | direction = (direction >= 0) ? -3600 : 3600; |
| mutech | 28:b452e097da53 | 409 | for (int i = (index < 0) ? -index : 0; i < end; ++i) |
| mutech | 28:b452e097da53 | 410 | { |
| mutech | 28:b452e097da53 | 411 | hsv.hue = color.hue + direction * i / len; |
| mutech | 28:b452e097da53 | 412 | *dest++ = GammaColor(hsv); |
| mutech | 28:b452e097da53 | 413 | } |
| mutech | 28:b452e097da53 | 414 | } |
| mutech | 28:b452e097da53 | 415 | |
| mutech | 28:b452e097da53 | 416 | //---------------------------------------------------------------------------- |