Skip to content

Instantly share code, notes, and snippets.

@Journeyman1337
Created February 2, 2022 20:25
Show Gist options
  • Save Journeyman1337/96086250a2ae9db36226aea53e5adb8a to your computer and use it in GitHub Desktop.
Save Journeyman1337/96086250a2ae9db36226aea53e5adb8a to your computer and use it in GitHub Desktop.
lambda template usage for image between color type conversion with multiple possible bit depths
/*
Copyright (c) 2022 Daniel Valcour
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
void fs::Image::Convert(const Image::ColorType color_type)
{
if (this->IsEmpty())
{
throw fs::runtime_error("Can not convert image color type it is empty. convert_color_type: {}", magic_enum::enum_name(color_type));
}
if (color_type == Image::ColorType::None)
{
throw fs::runtime_error("Can not convert image to none color type. image_color_type: {}", magic_enum::enum_name(this->color_type));
}
std::size_t pixel_count = this->width * this->height * this->pages;
std::size_t new_size = pixel_count * static_cast<std::size_t>(color_type);
this->Reserve(new_size);
auto convert = [&]<typename T>() {
T* data_ptr = reinterpret_cast<T*>(this->data.get());
switch (this->color_type)
{ //loop through all pixels to convert. If input type has less bytes per pixel than output, the whole loop must be reversed. the channels per pixel must be set in reverse in this situation too unless the in color type has one channel per pixel
case ColorType::G:
switch (color_type)
{
case ColorType::G: // G -> G
break;
case ColorType::GA: // G -> GA
for (std::size_t i = pixel_count; i-- != 0;)
{
std::size_t old_i = i;
std::size_t new_i = i * 2;
data_ptr[new_i++] = data_ptr[old_i];
data_ptr[new_i] = 255;
}
break;
case ColorType::RGB: // G -> RGB
for (std::size_t i = pixel_count; i-- != 0;)
{
std::size_t old_i = i;
std::size_t new_i = i * 3;
data_ptr[new_i++] = data_ptr[old_i]; // G -> R
data_ptr[new_i++] = data_ptr[old_i]; // G -> G
data_ptr[new_i] = data_ptr[old_i]; // G -> B
}
break;
case ColorType::RGBA: // G -> RGBA
for (std::size_t i = pixel_count; i-- != 0;)
{
std::size_t old_i = i;
std::size_t new_i = i * 4;
data_ptr[new_i++] = data_ptr[old_i]; // G -> R
data_ptr[new_i++] = data_ptr[old_i]; // G -> G
data_ptr[new_i++] = data_ptr[old_i]; // G -> B
data_ptr[new_i] = 255; // fill A
}
break;
}
break;
case ColorType::GA:
switch (color_type)
{
case ColorType::G: // GA -> G
for (std::size_t i = 0; i < pixel_count; i++)
{
std::size_t old_i = i * 2;
std::size_t new_i = i;
data_ptr[new_i] = data_ptr[old_i]; // G -> G
}
break;
case ColorType::GA: // GA -> GA
break;
case ColorType::RGB: // GA -> RGB
for (std::size_t i = pixel_count; i-- != 0;)
{
std::size_t old_i = i * 2; // alpha is not needed so start on G channel
std::size_t new_i = (i * 3) + 2;
data_ptr[new_i--] = data_ptr[old_i]; // G -> B
data_ptr[new_i--] = data_ptr[old_i]; // G -> G
data_ptr[new_i] = data_ptr[old_i]; // G -> R
}
break;
case ColorType::RGBA: // GA -> RGBA
for (std::size_t i = pixel_count; i-- != 0;)
{
std::size_t old_i = (i * 2) + 1;
std::size_t new_i = (i * 4) + 3;
data_ptr[new_i--] = data_ptr[old_i--]; // A -> A
data_ptr[new_i--] = data_ptr[old_i]; // B -> B
data_ptr[new_i--] = data_ptr[old_i]; // G -> G
data_ptr[new_i--] = data_ptr[old_i]; // R -> R
}
break;
}
break;
case ColorType::RGB:
switch (color_type)
{
case ColorType::G: // RGB -> G
for (std::size_t i = 0; i < pixel_count; i++)
{
std::size_t old_i = i * 3;
std::size_t new_i = i;
std::size_t old_r = data_ptr[old_i++];
std::size_t old_g = data_ptr[old_i++];
std::size_t old_b = data_ptr[old_i];
uint8_t gray = colorToGrayscale(old_r, old_g, old_b);
data_ptr[new_i] = gray;
}
break;
case ColorType::GA: // RGB -> GA
for (std::size_t i = 0; i < pixel_count; i++)
{
std::size_t old_i = i * 3;
std::size_t new_i = i * 2;
std::size_t old_r = data_ptr[old_i++];
std::size_t old_g = data_ptr[old_i++];
std::size_t old_b = data_ptr[old_i];
uint8_t gray = colorToGrayscale(old_r, old_g, old_b);
data_ptr[new_i++] = gray;
data_ptr[new_i] = 255;
}
break;
case ColorType::RGB: // RGB -> RGB
break;
case ColorType::RGBA: // RGB -> RGBA
for (std::size_t i = pixel_count; i-- != 0;)
{
std::size_t old_i = (i * 3) + 2;
std::size_t new_i = (i * 4) + 3; // these assignments are reversed to prevent errors on the second and third pixels
data_ptr[new_i--] = 255; // A fill
data_ptr[new_i--] = data_ptr[old_i--]; // B -> B
data_ptr[new_i--] = data_ptr[old_i--]; // G -> G
data_ptr[new_i] = data_ptr[old_i]; // R -> R
}
break;
}
break;
case ColorType::RGBA:
switch (color_type)
{
case ColorType::G: // RGBA -> G
for (std::size_t i = 0; i < pixel_count; i++)
{
std::size_t old_i = i * 4;
std::size_t new_i = i;
std::size_t old_r = data_ptr[old_i++];
std::size_t old_g = data_ptr[old_i++];
std::size_t old_b = data_ptr[old_i];
uint8_t gray = colorToGrayscale(old_r, old_g, old_b);
data_ptr[new_i] = gray;
}
break;
case ColorType::GA: // RGBA -> GA
for (std::size_t i = 0; i < pixel_count; i++)
{
std::size_t old_i = i * 4;
std::size_t new_i = i * 2;
std::size_t old_r = data_ptr[old_i++];
std::size_t old_g = data_ptr[old_i++];
std::size_t old_b = data_ptr[old_i++];
uint8_t gray = colorToGrayscale(old_r, old_g, old_b);
data_ptr[new_i++] = gray;
data_ptr[new_i] = data_ptr[old_i];
}
break;
case ColorType::RGB: // RGBA -> RGB
for (std::size_t i = 0; i < pixel_count; i++)
{
std::size_t old_i = i * 4;
std::size_t new_i = i * 3;
data_ptr[new_i++] = data_ptr[old_i++]; // R -> R
data_ptr[new_i++] = data_ptr[old_i++]; // G -> G
data_ptr[new_i] = data_ptr[old_i]; // B -> B
}
break;
case ColorType::RGBA: // RGBA -> RGBA
break;
}
break;
}
};
switch (this->bit_depth)
{
case fs::Image::BitDepth::Octuple:
convert.template operator()<std::uint8_t>();
break;
case fs::Image::BitDepth::Sexdecuple:
convert.template operator()<std::uint16_t>();
break;
case fs::Image::BitDepth::Normalized:
convert.template operator()<float>();
break;
default:
break;
}
this->color_type = color_type;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment