summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2024-09-14 11:08:20 +0000
committerrodri <rgl@antares-labs.eu>2024-09-14 11:08:20 +0000
commit4e3927d643a352bb65665cfbb5547355aa1aaa82 (patch)
tree3aaab03cadd804f81c66c278095b4d1781e56ded
parenteeda874d7c96703668d8ea9da2a4d22299ae4e5f (diff)
downloadlibgraphics-4e3927d643a352bb65665cfbb5547355aa1aaa82.tar.gz
libgraphics-4e3927d643a352bb65665cfbb5547355aa1aaa82.tar.bz2
libgraphics-4e3927d643a352bb65665cfbb5547355aa1aaa82.zip
fb: do a better greyscale to color raster conversion.
-rw-r--r--fb.c27
1 files changed, 23 insertions, 4 deletions
diff --git a/fb.c b/fb.c
index 353c8a1..e17b227 100644
--- a/fb.c
+++ b/fb.c
@@ -81,16 +81,35 @@ static void
rasterconvF2C(Raster *dst, Raster *src)
{
ulong *c, len;
- float *f;
+ float *f, min, max;
uchar b;
- c = dst->data;
+ /* first run: get the domain */
f = (float*)src->data;
len = Dx(dst->r)*Dy(dst->r);
+ for(min = 0, max = 0; len--; f++){
+ if(isInf(*f, -1)) /* -∞ is the DNotacolor of the z-buffer */
+ continue;
+ min = min(*f, min);
+ max = max(*f, max);
+ }
+ /* center it at zero: [min, max] → [0, max-min]*/
+ max -= min;
+ if(max == 0)
+ max = 1;
+ /* second run: average the values */
+ c = dst->data;
+ f = (float*)src->data;
+ len = Dx(dst->r)*Dy(dst->r);
while(len--){
- b = fclamp(*f++, 0, 1)*0xFF;
- *c++ = 0xFF<<24 | b<<16 | b<<8 | b;
+ if(isInf(*f, -1)){ /* -∞ is the DNotacolor of the z-buffer */
+ *c++ = 0x00;
+ f++;
+ continue;
+ }
+ b = (*f++ - min)/max * 0xFF;
+ *c++ = (b * 0x00010101)<<8 | 0xFF;
}
}