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