TLIGHT_PRODUCTS / WS281X
Committer:
mutech
Date:
Thu Oct 13 23:51:20 2016 +0000
Revision:
32:64c391617f6c
Parent:
29:a362df191524
Child:
34:5a141ed5d52a
WS281X

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