Use BABL method for 16-to-8 bit conversion (#3429)

This commit is contained in:
Flössie 2016-09-22 21:52:07 +02:00
parent 7635559062
commit 759676788b
6 changed files with 30 additions and 26 deletions

View File

@ -103,7 +103,7 @@ public:
template<>
inline void ImageDatas::convertTo(unsigned short src, unsigned char& dst) const
{
dst = (src + 128) / 257;
dst = uint16ToUint8Rounded(src);
}
template<>
inline void ImageDatas::convertTo(unsigned char src, int& dst) const
@ -118,7 +118,7 @@ inline void ImageDatas::convertTo(unsigned char src, unsigned short& dst) const
template<>
inline void ImageDatas::convertTo(float src, unsigned char& dst) const
{
dst = (static_cast<int>(src) + 128) / 257;
dst = uint16ToUint8Rounded(src);
}
template<>
inline void ImageDatas::convertTo(unsigned char src, float& dst) const

View File

@ -28,9 +28,9 @@ namespace
void getScanline8 (const uint16_t *red, const uint16_t *green, const uint16_t *blue, int width, unsigned char* buffer)
{
for (int i = 0, ix = 0; i < width; i++) {
buffer[ix++] = (red[i] + 128) / 257;
buffer[ix++] = (green[i] + 128) / 257;
buffer[ix++] = (blue[i] + 128) / 257;
buffer[ix++] = rtengine::uint16ToUint8Rounded(red[i]);
buffer[ix++] = rtengine::uint16ToUint8Rounded(green[i]);
buffer[ix++] = rtengine::uint16ToUint8Rounded(blue[i]);
}
}
@ -300,9 +300,9 @@ Image16::to8()
for (int h = 0; h < height; ++h) {
for (int w = 0; w < width; ++w) {
img8->r(h, w) = (r(h, w) + 128) / 257;
img8->g(h, w) = (g(h, w) + 128) / 257;
img8->b(h, w) = (b(h, w) + 128) / 257;
img8->r(h, w) = uint16ToUint8Rounded(r(h, w));
img8->g(h, w) = uint16ToUint8Rounded(g(h, w));
img8->b(h, w) = uint16ToUint8Rounded(b(h, w));
}
}

View File

@ -74,7 +74,7 @@ void Image8::setScanline (int row, unsigned char* buffer, int bps, float *minVal
unsigned short* sbuffer = (unsigned short*) buffer;
for (int i = 0, ix = row * width * 3; i < width * 3; ++i, ++ix) {
data[ix] = (sbuffer[i] + 128) / 257;
data[ix] = uint16ToUint8Rounded(sbuffer[i]);
}
break;

View File

@ -335,9 +335,9 @@ Imagefloat::to8()
for (int h = 0; h < height; ++h) {
for (int w = 0; w < width; ++w) {
img8->r(h, w) = (static_cast<unsigned int>(r(h, w)) + 128) / 257;
img8->g(h, w) = (static_cast<unsigned int>(g(h, w)) + 128) / 257;
img8->b(h, w) = (static_cast<unsigned int>(b(h, w)) + 128) / 257;
img8->r(h, w) = uint16ToUint8Rounded(r(h, w));
img8->g(h, w) = uint16ToUint8Rounded(g(h, w));
img8->b(h, w) = uint16ToUint8Rounded(b(h, w));
}
}

View File

@ -107,9 +107,9 @@ void ImProcFunctions::lab2monitorRgb (LabImage* lab, Image8* image)
/* copy RGB */
//int R1=((int)gamma2curve[(R)])
data[ix++] = (static_cast<int>(Color::gamma2curve[R]) + 128) / 257;
data[ix++] = (static_cast<int>(Color::gamma2curve[G]) + 128) / 257;
data[ix++] = (static_cast<int>(Color::gamma2curve[B]) + 128) / 257;
data[ix++] = uint16ToUint8Rounded(Color::gamma2curve[R]);
data[ix++] = uint16ToUint8Rounded(Color::gamma2curve[G]);
data[ix++] = uint16ToUint8Rounded(Color::gamma2curve[B]);
}
}
}
@ -218,9 +218,9 @@ Image8* ImProcFunctions::lab2rgb (LabImage* lab, int cx, int cy, int cw, int ch,
Color::xyz2rgb(x_, y_, z_, R, G, B, xyz_rgb);
image->data[ix++] = (static_cast<int>(Color::gamma2curve[R]) + 128) / 257;
image->data[ix++] = (static_cast<int>(Color::gamma2curve[G]) + 128) / 257;
image->data[ix++] = (static_cast<int>(Color::gamma2curve[B]) + 128) / 257;
image->data[ix++] = uint16ToUint8Rounded(Color::gamma2curve[R]);
image->data[ix++] = uint16ToUint8Rounded(Color::gamma2curve[G]);
image->data[ix++] = uint16ToUint8Rounded(Color::gamma2curve[B]);
}
}
}

View File

@ -1,14 +1,15 @@
#ifndef _MYMATH_
#define _MYMATH_
#include <cmath>
#include <algorithm>
#pragma once
#include <algorithm>
#include <cmath>
#include <cstdint>
namespace rtengine
{
static const int MAXVAL = 0xffff;
static const float MAXVALF = float(MAXVAL); // float version of MAXVAL
static const double MAXVALD = double(MAXVAL); // double version of MAXVAL
constexpr int MAXVAL = 0xffff;
constexpr float MAXVALF = static_cast<float>(MAXVAL); // float version of MAXVAL
constexpr double MAXVALD = static_cast<double>(MAXVAL); // double version of MAXVAL
template <typename _Tp>
inline _Tp SQR (_Tp x)
@ -107,6 +108,9 @@ inline int float2uint16range(float d) // clips input to [0;65535] and rounds
return d + 0.5f;
}
constexpr std::uint8_t uint16ToUint8Rounded(std::uint16_t i)
{
return ((i + 128) - ((i + 128) >> 8)) >> 8;
}
#endif
}