Skip to content
Snippets Groups Projects
Commit 58f9d475 authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

More scaling cleanup... no undefined behaviour

Remove undefined behaviour (right-shifting a negative integer) from
the scaler... replace with divide-by-2.  Any modern compiler should
be able to optimize that.

On the enabling the compiler front, const-ify more variables and
use types that allow removing some casts.

I have a bit more hope this will fix the macOS issue than the previous
efforts.
parent 80b9fccc
No related branches found
No related tags found
No related merge requests found
Pipeline #7047 failed
...@@ -757,39 +757,34 @@ pointy_scale3(const uint32_t* src, uint32_t* dest, const int width, const int he ...@@ -757,39 +757,34 @@ pointy_scale3(const uint32_t* src, uint32_t* dest, const int width, const int he
} }
struct YCoCg_data { struct YCoCg_data {
unsigned Y; uint32_t Y; // 0 to 255
signed Co; int32_t Co; // -255 to 255
signed Cg; int32_t Cg; // -255 to 255
}; };
static inline void static inline void
RGB_to_YCoCg(const uint32_t RGB, struct YCoCg_data * const YCoCg) RGB_to_YCoCg(const uint32_t RGB, struct YCoCg_data * const YCoCg)
{ {
signed R, G, B, tmp; const uint8_t R = (RGB >> 16) & 0xFF;
const uint8_t G = (RGB >> 8) & 0xFF;
const uint8_t B = (RGB) & 0xFF;
R = (RGB >> 16) & 0xFF; YCoCg->Co = R - B; // -255 to 255
G = (RGB >> 8) & 0xFF; const uint8_t tmp = B + (YCoCg->Co / 2);
B = (RGB) & 0xFF;
YCoCg->Co = R - B;
tmp = B + (YCoCg->Co >> 1);
YCoCg->Cg = G - tmp; YCoCg->Cg = G - tmp;
YCoCg->Y = tmp + (YCoCg->Cg >> 1); YCoCg->Y = tmp + (YCoCg->Cg / 2);
} }
static inline uint32_t static inline uint32_t
YCoCg_to_RGB(struct YCoCg_data const * const YCoCg) YCoCg_to_RGB(struct YCoCg_data const * const YCoCg)
{ {
signed Ri, Gi, Bi, tmp; const int16_t tmp = YCoCg->Y - (YCoCg->Cg / 2);
unsigned R, G, B; const int16_t Gi = YCoCg->Cg + tmp;
const int16_t Bi = tmp - (YCoCg->Co / 2);
tmp = YCoCg->Y - (YCoCg->Cg >> 1); const int16_t Ri = Bi + YCoCg->Co;
Gi = YCoCg->Cg + tmp; const uint32_t R = (Ri < 0) ? 0 : ((Ri > 255) ? 255 : Ri);
Bi = tmp - (YCoCg->Co >> 1); const uint32_t G = (Gi < 0) ? 0 : ((Gi > 255) ? 255 : Gi);
Ri = Bi + YCoCg->Co; const uint32_t B = (Bi < 0) ? 0 : ((Bi > 255) ? 255 : Bi);
R = ((Ri < 0) ? 0 : ((Ri > 255) ? 255 : Ri));
G = ((Gi < 0) ? 0 : ((Gi > 255) ? 255 : Gi));
B = ((Bi < 0) ? 0 : ((Bi > 255) ? 255 : Bi));
return (R << 16) | (G << 8) | B; return (R << 16) | (G << 8) | B;
} }
...@@ -805,9 +800,9 @@ blend_YCoCg(const uint32_t c1, const uint32_t c2, const uint16_t weight) ...@@ -805,9 +800,9 @@ blend_YCoCg(const uint32_t c1, const uint32_t c2, const uint16_t weight)
RGB_to_YCoCg(c1, &ycc1); RGB_to_YCoCg(c1, &ycc1);
RGB_to_YCoCg(c2, &ycc2); RGB_to_YCoCg(c2, &ycc2);
ycc3.Y = ((uint32_t)ycc1.Y * iw + (uint32_t)ycc2.Y * weight) / 65535; ycc3.Y = (ycc1.Y * iw + ycc2.Y * weight) / 65535;
ycc3.Co = ((int32_t)ycc1.Co * iw + (int32_t)ycc2.Co * weight) / 65535; ycc3.Co = (ycc1.Co * iw + ycc2.Co * weight) / 65535;
ycc3.Cg = ((int32_t)ycc1.Cg * iw + (int32_t)ycc2.Cg * weight) / 65535; ycc3.Cg = (ycc1.Cg * iw + ycc2.Cg * weight) / 65535;
return YCoCg_to_RGB(&ycc3); return YCoCg_to_RGB(&ycc3);
} }
......
...@@ -40,9 +40,9 @@ ...@@ -40,9 +40,9 @@
#endif #endif
struct YCoCg_data { struct YCoCg_data {
unsigned Y; uint32_t Y;
signed Co; int32_t Co;
signed Cg; int32_t Cg;
}; };
static inline void static inline void
...@@ -55,9 +55,9 @@ RGB_to_YCoCg(const uint32_t RGB, struct YCoCg_data *YCoCg) ...@@ -55,9 +55,9 @@ RGB_to_YCoCg(const uint32_t RGB, struct YCoCg_data *YCoCg)
B = (RGB) & 0xFF; B = (RGB) & 0xFF;
YCoCg->Co = R - B; YCoCg->Co = R - B;
tmp = B + (YCoCg->Co >> 1); tmp = B + (YCoCg->Co / 2);
YCoCg->Cg = G - tmp; YCoCg->Cg = G - tmp;
YCoCg->Y = tmp + (YCoCg->Cg >> 1); YCoCg->Y = tmp + (YCoCg->Cg / 2);
} }
static inline uint32_t pixel_diff(const uint32_t x, const uint32_t y) static inline uint32_t pixel_diff(const uint32_t x, const uint32_t y)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment