I have a YCCK image on my input and I use libjpeg-turbo to first convert it to CMYK and then I manually convert CMYK to BGRA. I am doing this because I have read somewhere that direct conversion from YCCK to BGRA is not supported by libjpeg-turbo.
This is my current conversion algorithm:
auto const rowStride{ jpegDecompressWrapper.cinfo().output_width * jpegDecompressWrapper.cinfo().output_components };
JSAMPARRAY buffer{ ( *( cinfo ).mem->alloc_sarray )( reinterpret_cast< j_common_ptr >( &( jpegDecompressWrapper.cinfo() ) ), JPOOL_IMAGE, rowStride, 1 ) };
while ( cinfo.output_scanline < cinfo.output_height )
{
jpeg_read_scanlines( &cinfo, buffer, 1 );
for ( std::size_t i{ 0U }; i < rowStride / 4; ++i )
{
auto const c{ static_cast< std::uint8_t >( buffer[ 0 ][ i * 4U ] ) };
auto const m{ static_cast< std::uint8_t >( buffer[ 0 ][ i * 4U + 1U ] ) };
auto const y{ static_cast< std::uint8_t >( buffer[ 0 ][ i * 4U + 2U ] ) };
auto const k{ static_cast< std::uint8_t >( buffer[ 0 ][ i * 4U + 3U ] ) };
// Convert CMYK to RGB
auto const r{ static_cast< std::uint8_t >( ( ( 255 - c ) * ( 255 - k ) ) / 255 ) };
auto const g{ static_cast< std::uint8_t >( ( ( 255 - m ) * ( 255 - k ) ) / 255 ) };
auto const b{ static_cast< std::uint8_t >( ( ( 255 - y ) * ( 255 - k ) ) / 255 ) };
// Assign RGB values to the same pixel
pOutput[ i * 4U ] = b;
pOutput[ i * 4U + 1U ] = g;
pOutput[ i * 4U + 2U ] = r;
pOutput[ i * 4U + 3U ] = 255;
}
pOutput += rowStride;
}
It's pretty poor because I get too dark image on the output.
Does anyone know how can I improve my algorithm or if it's possible to directly convert YCCK to BGRA with libjpeg-turbo?