| 8 | namespace CesiumImage { |
| 9 | |
| 10 | void ImageAsset::changeNumberOfChannels( |
| 11 | int32_t newChannels, |
| 12 | std::byte defaultValue) { |
| 13 | if (newChannels == this->channels) { |
| 14 | // Nothing to do. |
| 15 | return; |
| 16 | } else if (newChannels < this->channels) { |
| 17 | // We're using fewer channels than previous, so we can perform the |
| 18 | // conversion in-place. |
| 19 | size_t writePos = 0; |
| 20 | for (size_t i = 0; i < this->pixelData.size(); |
| 21 | i += (size_t)this->channels) { |
| 22 | for (size_t j = 0; j < (size_t)newChannels; j++) { |
| 23 | this->pixelData[writePos] = this->pixelData[i + j]; |
| 24 | ++writePos; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | this->pixelData.resize(writePos); |
| 29 | } else { |
| 30 | // We're using more channels than previous, so we need to make a new buffer. |
| 31 | std::vector<std::byte> newPixelData( |
| 32 | (size_t)(newChannels * this->width * this->height)); |
| 33 | size_t writePos = 0; |
| 34 | for (size_t i = 0; i < this->pixelData.size(); |
| 35 | i += (size_t)this->channels) { |
| 36 | for (size_t j = 0; j < (size_t)this->channels; j++) { |
| 37 | newPixelData[writePos] = this->pixelData[i + j]; |
| 38 | ++writePos; |
| 39 | } |
| 40 | |
| 41 | for (size_t j = 0; j < (size_t)(newChannels - this->channels); j++) { |
| 42 | newPixelData[writePos] = defaultValue; |
| 43 | ++writePos; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | this->pixelData = std::move(newPixelData); |
| 48 | } |
| 49 | |
| 50 | this->channels = newChannels; |
| 51 | } |
| 52 | } // namespace CesiumImage |