TLIGHT_PRODUCTS / WS281X
Committer:
mutech
Date:
Fri Nov 04 17:40:25 2016 +0000
Revision:
36:0fe7917a832a
Parent:
35:dffa06d09fdc
Child:
46:2374900f8845
WS2811/WS2812 Library

Who changed what in which revision?

UserRevisionLine numberNew 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 27:bc79f444883b 37 _dummyPixel = 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 27:bc79f444883b 44 _dummyPixel = 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 32:64c391617f6c 64 _maxPixels = (maxPixels < 0) ? 0 : (maxPixels > MAX_PIXELS) ? MAX_PIXELS : maxPixels;
mutech 32:64c391617f6c 65 _pixels = (!_maxPixels) ? NULL : buffer;
mutech 27:bc79f444883b 66
mutech 27:bc79f444883b 67 if (!_pixels && _maxPixels > 0)
mutech 27:bc79f444883b 68 {
mutech 32:64c391617f6c 69 #if USE_MALLOC
mutech 32:64c391617f6c 70 _pixels = static_cast<RGBColor*>(malloc(sizeof(RGBColor)*_maxPixels));
mutech 32:64c391617f6c 71 if (_pixels)
mutech 32:64c391617f6c 72 _owned_buffer = true;
mutech 32:64c391617f6c 73 else
mutech 32:64c391617f6c 74 _maxPixels = 0;
mutech 32:64c391617f6c 75 #else
mutech 27:bc79f444883b 76 _pixels = new RGBColor[_maxPixels];
mutech 32:64c391617f6c 77 _owned_buffer = true;
mutech 32:64c391617f6c 78 #endif
mutech 27:bc79f444883b 79 }
mutech 27:bc79f444883b 80 _numPixels = _maxPixels;
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 27:bc79f444883b 87 _numPixels = (value > _maxPixels) ? _maxPixels : value;
mutech 27:bc79f444883b 88 return _numPixels;
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 35:dffa06d09fdc 94 int numPixels = static_cast<int>(_numPixels);
mutech 35:dffa06d09fdc 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 35:dffa06d09fdc 100 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 35:dffa06d09fdc 111 int numPixels = static_cast<int>(_numPixels);
mutech 35:dffa06d09fdc 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 27:bc79f444883b 130 // 指定色を指定位置のピクセルから指定サイズ分書き込む
mutech 28:b452e097da53 131 void RGBPixels::fillPixels(int index, const RGBColor color, int len)
mutech 27:bc79f444883b 132 {
mutech 36:0fe7917a832a 133 int numPixels = static_cast<int>(_numPixels);
mutech 36:0fe7917a832a 134 if (_pixels && len > 0 && index < numPixels && (index + len) >= 0)
mutech 27:bc79f444883b 135 {
mutech 36:0fe7917a832a 136 if (index < 0)
mutech 36:0fe7917a832a 137 {
mutech 36:0fe7917a832a 138 len += index;
mutech 36:0fe7917a832a 139 index = 0;
mutech 36:0fe7917a832a 140 }
mutech 36:0fe7917a832a 141 if (index + len > numPixels)
mutech 36:0fe7917a832a 142 len = numPixels - index;
mutech 27:bc79f444883b 143 _pixels[index] = color;
mutech 27:bc79f444883b 144 repeat_buffer<RGBColor>(_pixels + index, len, 1);
mutech 27:bc79f444883b 145 }
mutech 27:bc79f444883b 146 }
mutech 27:bc79f444883b 147
mutech 28:b452e097da53 148 void RGBPixels::fillPixels(int index, const HSVColor color, int len)
mutech 27:bc79f444883b 149 {
mutech 29:a362df191524 150 fillPixels(index, RGBColor(color), len);
mutech 27:bc79f444883b 151 }
mutech 27:bc79f444883b 152
mutech 27:bc79f444883b 153 // 先頭から指定サイズ分のブロックをバッファの最後までコピーする
mutech 27:bc79f444883b 154 void RGBPixels::repeatPixels(int block_size)
mutech 27:bc79f444883b 155 {
mutech 27:bc79f444883b 156 if (_pixels && block_size > 0 && block_size < _numPixels)
mutech 27:bc79f444883b 157 {
mutech 27:bc79f444883b 158 repeat_buffer<RGBColor>(_pixels, _numPixels, block_size);
mutech 27:bc79f444883b 159 }
mutech 27:bc79f444883b 160 }
mutech 27:bc79f444883b 161
mutech 27:bc79f444883b 162 void RGBPixels::repeatPixels(RGBColor *source, int size)
mutech 27:bc79f444883b 163 {
mutech 27:bc79f444883b 164 if (_pixels && source && size > 0)
mutech 27:bc79f444883b 165 {
mutech 27:bc79f444883b 166 if (size > _numPixels)
mutech 27:bc79f444883b 167 size = _numPixels;
mutech 27:bc79f444883b 168 memcpy(_pixels, source, size * sizeof(_pixels[0]));
mutech 27:bc79f444883b 169 repeat_buffer<RGBColor>(_pixels, _numPixels, size);
mutech 27:bc79f444883b 170 }
mutech 27:bc79f444883b 171 }
mutech 27:bc79f444883b 172
mutech 27:bc79f444883b 173 void RGBPixels::repeatPixels(HSVColor *source, int size)
mutech 27:bc79f444883b 174 {
mutech 27:bc79f444883b 175 if (_pixels && source && size > 0)
mutech 27:bc79f444883b 176 {
mutech 27:bc79f444883b 177 if (size > _numPixels)
mutech 27:bc79f444883b 178 size = _numPixels;
mutech 27:bc79f444883b 179 for (int i = 0; i < size; ++i)
mutech 27:bc79f444883b 180 _pixels[i] = *source++;
mutech 27:bc79f444883b 181 repeat_buffer<RGBColor>(_pixels, _numPixels, size);
mutech 27:bc79f444883b 182 }
mutech 27:bc79f444883b 183 }
mutech 27:bc79f444883b 184
mutech 27:bc79f444883b 185 RGBPixels& RGBPixels::operator=(const RGBPixels& rhs)
mutech 27:bc79f444883b 186 {
mutech 27:bc79f444883b 187 if (!rhs._pixels || !rhs._maxPixels)
mutech 27:bc79f444883b 188 {
mutech 27:bc79f444883b 189 // 右辺が空の場合何もしない
mutech 27:bc79f444883b 190 return *this;
mutech 27:bc79f444883b 191 }
mutech 27:bc79f444883b 192 if (!_pixels || !_maxPixels)
mutech 27:bc79f444883b 193 {
mutech 27:bc79f444883b 194 // 自分のバッファなしの場合、、新規確保
mutech 27:bc79f444883b 195 setPixelBuffer(nullptr, rhs._maxPixels);
mutech 27:bc79f444883b 196 }
mutech 27:bc79f444883b 197
mutech 27:bc79f444883b 198 if (_pixels && _maxPixels)
mutech 27:bc79f444883b 199 {
mutech 27:bc79f444883b 200 _numPixels = rhs._numPixels;
mutech 27:bc79f444883b 201 if (_numPixels > rhs._maxPixels)
mutech 27:bc79f444883b 202 _numPixels = rhs._maxPixels;
mutech 27:bc79f444883b 203 if (_numPixels > _maxPixels)
mutech 27:bc79f444883b 204 _numPixels = _maxPixels;
mutech 27:bc79f444883b 205 memcpy(_pixels, rhs._pixels, sizeof(_pixels[0]) * _numPixels);
mutech 27:bc79f444883b 206 }
mutech 27:bc79f444883b 207
mutech 27:bc79f444883b 208 return *this;
mutech 27:bc79f444883b 209 }
mutech 27:bc79f444883b 210
mutech 27:bc79f444883b 211 //----------------------------------------------------------------------------
mutech 28:b452e097da53 212 void RGBPixels::makeGradation(int index, RGBColor from, RGBColor to, int len)
mutech 28:b452e097da53 213 {
mutech 28:b452e097da53 214 if (!_pixels || len < 1 || index >= _numPixels || (index + len) < 0)
mutech 28:b452e097da53 215 return;
mutech 28:b452e097da53 216
mutech 28:b452e097da53 217 int end = len;
mutech 28:b452e097da53 218 if (index + end > _numPixels)
mutech 28:b452e097da53 219 end = _numPixels - index;
mutech 28:b452e097da53 220
mutech 28:b452e097da53 221 RGBColor color;
mutech 28:b452e097da53 222 RGBColor *dest = _pixels;
mutech 28:b452e097da53 223 if (index > 0)
mutech 28:b452e097da53 224 dest += index;
mutech 28:b452e097da53 225 for (int i = (index < 0) ? -index : 0; i < end; ++i)
mutech 28:b452e097da53 226 {
mutech 28:b452e097da53 227 int j = len - i;
mutech 28:b452e097da53 228 color.red = ((from.red * j) + (to.red * i)) / len;
mutech 28:b452e097da53 229 color.green = ((from.green * j) + (to.green * i)) / len;
mutech 28:b452e097da53 230 color.blue = ((from.blue * j) + (to.blue * i)) / len;
mutech 28:b452e097da53 231 *dest++ = GammaColor(color);
mutech 28:b452e097da53 232 }
mutech 28:b452e097da53 233 }
mutech 28:b452e097da53 234
mutech 28:b452e097da53 235 void RGBPixels::makeRainbow(int index, HSVColor color, int len, int direction)
mutech 28:b452e097da53 236 {
mutech 28:b452e097da53 237 if (!_pixels || len < 1 || index >= _numPixels || (index + len) < 0)
mutech 28:b452e097da53 238 return;
mutech 28:b452e097da53 239
mutech 28:b452e097da53 240 int end = len;
mutech 28:b452e097da53 241 if (index + end > _numPixels)
mutech 28:b452e097da53 242 end = _numPixels - index;
mutech 28:b452e097da53 243
mutech 28:b452e097da53 244 HSVColor hsv(color);
mutech 28:b452e097da53 245 RGBColor *dest = _pixels;
mutech 28:b452e097da53 246 if (index > 0)
mutech 28:b452e097da53 247 dest += index;
mutech 28:b452e097da53 248 direction = (direction >= 0) ? -3600 : 3600;
mutech 28:b452e097da53 249 for (int i = (index < 0) ? -index : 0; i < end; ++i)
mutech 28:b452e097da53 250 {
mutech 28:b452e097da53 251 hsv.hue = color.hue + direction * i / len;
mutech 28:b452e097da53 252 *dest++ = GammaColor(hsv);
mutech 28:b452e097da53 253 }
mutech 28:b452e097da53 254 }
mutech 28:b452e097da53 255
mutech 28:b452e097da53 256 //----------------------------------------------------------------------------
mutech 27:bc79f444883b 257 //----------------------------------------------------------------------------
mutech 27:bc79f444883b 258 HSVPixels::HSVPixels(HSVColor *buffer, int maxPixels)
mutech 32:64c391617f6c 259 : _owned_buffer(false)
mutech 27:bc79f444883b 260 {
mutech 27:bc79f444883b 261 _dummyPixel = 0;
mutech 27:bc79f444883b 262 setPixelBuffer(buffer, maxPixels);
mutech 27:bc79f444883b 263 }
mutech 27:bc79f444883b 264
mutech 27:bc79f444883b 265 HSVPixels::HSVPixels(int maxPixels)
mutech 32:64c391617f6c 266 : _owned_buffer(false)
mutech 27:bc79f444883b 267 {
mutech 27:bc79f444883b 268 _dummyPixel = 0;
mutech 27:bc79f444883b 269 setPixelBuffer(nullptr, maxPixels);
mutech 27:bc79f444883b 270 }
mutech 27:bc79f444883b 271
mutech 27:bc79f444883b 272 HSVPixels::~HSVPixels()
mutech 27:bc79f444883b 273 {
mutech 32:64c391617f6c 274 setPixelBuffer(0, 0);
mutech 27:bc79f444883b 275 }
mutech 27:bc79f444883b 276
mutech 27:bc79f444883b 277 void HSVPixels::setPixelBuffer(HSVColor *buffer, int maxPixels)
mutech 27:bc79f444883b 278 {
mutech 32:64c391617f6c 279 if (_owned_buffer && _pixels)
mutech 32:64c391617f6c 280 {
mutech 32:64c391617f6c 281 #if USE_MALLOC
mutech 32:64c391617f6c 282 free(_pixels);
mutech 32:64c391617f6c 283 #else
mutech 27:bc79f444883b 284 delete[] _pixels;
mutech 32:64c391617f6c 285 #endif
mutech 32:64c391617f6c 286 }
mutech 32:64c391617f6c 287 _owned_buffer = false;
mutech 32:64c391617f6c 288 _maxPixels = (maxPixels < 0) ? 0 : (maxPixels > MAX_PIXELS) ? MAX_PIXELS : maxPixels;
mutech 32:64c391617f6c 289 _pixels = (!_maxPixels) ? NULL : buffer;
mutech 27:bc79f444883b 290
mutech 27:bc79f444883b 291 if (!_pixels && _maxPixels > 0)
mutech 27:bc79f444883b 292 {
mutech 32:64c391617f6c 293 #if USE_MALLOC
mutech 32:64c391617f6c 294 _pixels = static_cast<HSVColor*>(malloc(sizeof(HSVColor)*_maxPixels));
mutech 32:64c391617f6c 295 if (_pixels)
mutech 32:64c391617f6c 296 _owned_buffer = true;
mutech 32:64c391617f6c 297 else
mutech 32:64c391617f6c 298 _maxPixels = 0;
mutech 32:64c391617f6c 299 #else
mutech 27:bc79f444883b 300 _pixels = new HSVColor[_maxPixels];
mutech 32:64c391617f6c 301 _owned_buffer = true;
mutech 32:64c391617f6c 302 #endif
mutech 27:bc79f444883b 303 }
mutech 27:bc79f444883b 304 _numPixels = _maxPixels;
mutech 27:bc79f444883b 305 clear();
mutech 27:bc79f444883b 306 }
mutech 27:bc79f444883b 307
mutech 27:bc79f444883b 308 int HSVPixels::numPixels(int value)
mutech 27:bc79f444883b 309 {
mutech 27:bc79f444883b 310 if (value >= 0)
mutech 27:bc79f444883b 311 _numPixels = (value > _maxPixels) ? _maxPixels : value;
mutech 27:bc79f444883b 312 return _numPixels;
mutech 27:bc79f444883b 313 }
mutech 27:bc79f444883b 314
mutech 27:bc79f444883b 315 // 指定位置のピクセルへ色配列を指定サイズ分をコピーする
mutech 27:bc79f444883b 316 void HSVPixels::setPixels(int index, HSVColor *color, int len)
mutech 27:bc79f444883b 317 {
mutech 35:dffa06d09fdc 318 int numPixels = static_cast<int>(_numPixels);
mutech 35:dffa06d09fdc 319 if (_pixels && len > 0 && index < numPixels && (index + len) >= 0)
mutech 27:bc79f444883b 320 {
mutech 34:5a141ed5d52a 321 if (index < 0)
mutech 35:dffa06d09fdc 322 {
mutech 35:dffa06d09fdc 323 len += index;
mutech 35:dffa06d09fdc 324 color -= index;
mutech 35:dffa06d09fdc 325 index = 0;
mutech 35:dffa06d09fdc 326 }
mutech 35:dffa06d09fdc 327 if (index + len > numPixels)
mutech 35:dffa06d09fdc 328 len = numPixels - index;
mutech 27:bc79f444883b 329 memcpy(&_pixels[index], color, len * sizeof(_pixels[0]));
mutech 27:bc79f444883b 330 }
mutech 27:bc79f444883b 331 }
mutech 27:bc79f444883b 332
mutech 27:bc79f444883b 333 void HSVPixels::setPixels(int index, RGBColor *color, int len)
mutech 27:bc79f444883b 334 {
mutech 35:dffa06d09fdc 335 int numPixels = static_cast<int>(_numPixels);
mutech 35:dffa06d09fdc 336 if (_pixels && len > 0 && index < numPixels && (index + len) >= 0)
mutech 27:bc79f444883b 337 {
mutech 34:5a141ed5d52a 338 if (index < 0)
mutech 34:5a141ed5d52a 339 {
mutech 35:dffa06d09fdc 340 len += index;
mutech 35:dffa06d09fdc 341 color -= index;
mutech 35:dffa06d09fdc 342 index = 0;
mutech 34:5a141ed5d52a 343 }
mutech 35:dffa06d09fdc 344 if (index + len > numPixels)
mutech 35:dffa06d09fdc 345 len = numPixels - index;
mutech 27:bc79f444883b 346 HSVColor *dest = &_pixels[index];
mutech 27:bc79f444883b 347 do
mutech 27:bc79f444883b 348 {
mutech 27:bc79f444883b 349 *dest++ = *color++;
mutech 27:bc79f444883b 350 } while (--len);
mutech 27:bc79f444883b 351 }
mutech 27:bc79f444883b 352 }
mutech 27:bc79f444883b 353
mutech 27:bc79f444883b 354 // 指定色を指定位置のピクセルから指定サイズ分書き込む
mutech 29:a362df191524 355 void HSVPixels::fillPixels(int index, const HSVColor color, int len)
mutech 27:bc79f444883b 356 {
mutech 36:0fe7917a832a 357 int numPixels = static_cast<int>(_numPixels);
mutech 36:0fe7917a832a 358 if (_pixels && len > 0 && index < numPixels && (index + len) >= 0)
mutech 27:bc79f444883b 359 {
mutech 36:0fe7917a832a 360 if (index < 0)
mutech 36:0fe7917a832a 361 {
mutech 36:0fe7917a832a 362 len += index;
mutech 36:0fe7917a832a 363 index = 0;
mutech 36:0fe7917a832a 364 }
mutech 36:0fe7917a832a 365 if (index + len > numPixels)
mutech 36:0fe7917a832a 366 len = numPixels - index;
mutech 27:bc79f444883b 367 _pixels[index] = color;
mutech 27:bc79f444883b 368 repeat_buffer<HSVColor>(_pixels + index, len, 1);
mutech 27:bc79f444883b 369 }
mutech 27:bc79f444883b 370 }
mutech 27:bc79f444883b 371
mutech 29:a362df191524 372 void HSVPixels::fillPixels(int index, const RGBColor color, int len)
mutech 27:bc79f444883b 373 {
mutech 29:a362df191524 374 fillPixels(index, HSVColor(color), len);
mutech 27:bc79f444883b 375 }
mutech 27:bc79f444883b 376
mutech 27:bc79f444883b 377 // 先頭から指定サイズ分のブロックをバッファの最後までコピーする
mutech 27:bc79f444883b 378 void HSVPixels::repeatPixels(int block_size)
mutech 27:bc79f444883b 379 {
mutech 27:bc79f444883b 380 if (_pixels && block_size > 0 && block_size < _numPixels)
mutech 27:bc79f444883b 381 {
mutech 27:bc79f444883b 382 repeat_buffer<HSVColor>(_pixels, _numPixels, block_size);
mutech 27:bc79f444883b 383 }
mutech 27:bc79f444883b 384 }
mutech 27:bc79f444883b 385
mutech 27:bc79f444883b 386 void HSVPixels::repeatPixels(HSVColor *source, int size)
mutech 27:bc79f444883b 387 {
mutech 27:bc79f444883b 388 if (_pixels && source && size > 0)
mutech 27:bc79f444883b 389 {
mutech 27:bc79f444883b 390 if (size > _numPixels)
mutech 27:bc79f444883b 391 size = _numPixels;
mutech 27:bc79f444883b 392 memcpy(_pixels, source, size * sizeof(_pixels[0]));
mutech 27:bc79f444883b 393 repeat_buffer<HSVColor>(_pixels, _numPixels, size);
mutech 27:bc79f444883b 394 }
mutech 27:bc79f444883b 395 }
mutech 27:bc79f444883b 396
mutech 27:bc79f444883b 397 void HSVPixels::repeatPixels(RGBColor *source, int size)
mutech 27:bc79f444883b 398 {
mutech 27:bc79f444883b 399 if (_pixels && source && size > 0)
mutech 27:bc79f444883b 400 {
mutech 27:bc79f444883b 401 if (size > _numPixels)
mutech 27:bc79f444883b 402 size = _numPixels;
mutech 27:bc79f444883b 403 for (int i = 0; i < size; ++i)
mutech 27:bc79f444883b 404 _pixels[i] = *source++;
mutech 27:bc79f444883b 405 repeat_buffer<HSVColor>(_pixels, _numPixels, size);
mutech 27:bc79f444883b 406 }
mutech 27:bc79f444883b 407 }
mutech 27:bc79f444883b 408
mutech 27:bc79f444883b 409 HSVPixels& HSVPixels::operator=(const HSVPixels& rhs)
mutech 27:bc79f444883b 410 {
mutech 27:bc79f444883b 411 if (!rhs._pixels || !rhs._maxPixels)
mutech 27:bc79f444883b 412 {
mutech 27:bc79f444883b 413 // 右辺が空の場合何もしない
mutech 27:bc79f444883b 414 return *this;
mutech 27:bc79f444883b 415 }
mutech 27:bc79f444883b 416 if (!_pixels || !_maxPixels)
mutech 27:bc79f444883b 417 {
mutech 27:bc79f444883b 418 // 自分のバッファなしの場合、、新規確保
mutech 27:bc79f444883b 419 setPixelBuffer(nullptr, rhs._maxPixels);
mutech 27:bc79f444883b 420 }
mutech 27:bc79f444883b 421
mutech 27:bc79f444883b 422 if (_pixels && _maxPixels)
mutech 27:bc79f444883b 423 {
mutech 27:bc79f444883b 424 _numPixels = rhs._numPixels;
mutech 27:bc79f444883b 425 if (_numPixels > rhs._maxPixels)
mutech 27:bc79f444883b 426 _numPixels = rhs._maxPixels;
mutech 27:bc79f444883b 427 if (_numPixels > _maxPixels)
mutech 27:bc79f444883b 428 _numPixels = _maxPixels;
mutech 27:bc79f444883b 429 memcpy(_pixels, rhs._pixels, sizeof(_pixels[0]) * _numPixels);
mutech 27:bc79f444883b 430 }
mutech 27:bc79f444883b 431
mutech 27:bc79f444883b 432 return *this;
mutech 27:bc79f444883b 433 }
mutech 27:bc79f444883b 434
mutech 27:bc79f444883b 435 //----------------------------------------------------------------------------
mutech 28:b452e097da53 436 void HSVPixels::makeGradation(int index, HSVColor from, HSVColor to, int len)
mutech 28:b452e097da53 437 {
mutech 28:b452e097da53 438 if (!_pixels || len < 1 || index >= _numPixels || (index + len) < 0)
mutech 28:b452e097da53 439 return;
mutech 28:b452e097da53 440
mutech 28:b452e097da53 441 int end = len;
mutech 28:b452e097da53 442 if (index + end > _numPixels)
mutech 28:b452e097da53 443 end = _numPixels - index;
mutech 28:b452e097da53 444
mutech 28:b452e097da53 445 RGBColor rgb_from(from);
mutech 28:b452e097da53 446 RGBColor rgb_to(to);
mutech 28:b452e097da53 447 RGBColor color;
mutech 28:b452e097da53 448 HSVColor *dest = _pixels;
mutech 28:b452e097da53 449 if (index > 0)
mutech 28:b452e097da53 450 dest += index;
mutech 28:b452e097da53 451 for (int i = (index < 0) ? -index : 0; i < end; ++i)
mutech 28:b452e097da53 452 {
mutech 28:b452e097da53 453 int j = len - i;
mutech 28:b452e097da53 454 color.red = ((rgb_from.red * j) + (rgb_to.red * i)) / len;
mutech 28:b452e097da53 455 color.green = ((rgb_from.green * j) + (rgb_to.green * i)) / len;
mutech 28:b452e097da53 456 color.blue = ((rgb_from.blue * j) + (rgb_to.blue * i)) / len;
mutech 28:b452e097da53 457 *dest++ = GammaColor(color);
mutech 28:b452e097da53 458 }
mutech 28:b452e097da53 459 }
mutech 28:b452e097da53 460
mutech 28:b452e097da53 461 void HSVPixels::makeRainbow(int index, HSVColor color, int len, int direction)
mutech 28:b452e097da53 462 {
mutech 28:b452e097da53 463 if (!_pixels || len < 1 || index >= _numPixels || (index + len) < 0)
mutech 28:b452e097da53 464 return;
mutech 28:b452e097da53 465
mutech 28:b452e097da53 466 int end = len;
mutech 28:b452e097da53 467 if (index + end > _numPixels)
mutech 28:b452e097da53 468 end = _numPixels - index;
mutech 28:b452e097da53 469
mutech 28:b452e097da53 470 HSVColor hsv(color);
mutech 28:b452e097da53 471 HSVColor *dest = _pixels;
mutech 28:b452e097da53 472 if (index > 0)
mutech 28:b452e097da53 473 dest += index;
mutech 28:b452e097da53 474 direction = (direction >= 0) ? -3600 : 3600;
mutech 28:b452e097da53 475 for (int i = (index < 0) ? -index : 0; i < end; ++i)
mutech 28:b452e097da53 476 {
mutech 28:b452e097da53 477 hsv.hue = color.hue + direction * i / len;
mutech 29:a362df191524 478 *dest++ = GammaColor(hsv);
mutech 28:b452e097da53 479 }
mutech 28:b452e097da53 480 }
mutech 28:b452e097da53 481
mutech 28:b452e097da53 482 //----------------------------------------------------------------------------