Round when converting to 8 bits (#3429)

This commit is contained in:
Flössie
2016-09-21 21:54:46 +02:00
parent 309b823f93
commit 1e7b8035c4
5 changed files with 21 additions and 21 deletions

View File

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

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) 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++) { for (int i = 0, ix = 0; i < width; i++) {
buffer[ix++] = red[i] / 257; buffer[ix++] = (red[i] + 128) / 257;
buffer[ix++] = green[i] / 257; buffer[ix++] = (green[i] + 128) / 257;
buffer[ix++] = blue[i] / 257; buffer[ix++] = (blue[i] + 128) / 257;
} }
} }
@@ -300,9 +300,9 @@ Image16::to8()
for (int h = 0; h < height; ++h) { for (int h = 0; h < height; ++h) {
for (int w = 0; w < width; ++w) { for (int w = 0; w < width; ++w) {
img8->r(h, w) = r(h, w) / 257; img8->r(h, w) = (r(h, w) + 128) / 257;
img8->g(h, w) = g(h, w) / 257; img8->g(h, w) = (g(h, w) + 128) / 257;
img8->b(h, w) = b(h, w) / 257; img8->b(h, w) = (b(h, w) + 128) / 257;
} }
} }

View File

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

View File

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

View File

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