-
Notifications
You must be signed in to change notification settings - Fork 3
/
_floyd.cpp
126 lines (101 loc) · 2.54 KB
/
_floyd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#define pixel(x,y) image[ w * (y) + (x) ]
#define pixel(x,y,c) c = (int) image_ ## c[w*y+x] ; if (c<0) c=0; if (c>255) c=255; c = c >> 6;
void floydChannel( double *image, int w, int h )
{
int x,y;
double newPixel,oldPixel;
double qerror;
proc_names_printf("%s:%s:%d\n",__FILE__,__FUNCTION__,__LINE__);
for( y=0; y<h;y++)
{
for (x = 0; x<w;x++)
{
oldPixel = pixel(x,y);
newPixel = round( oldPixel / 64.0f ) * 64.0f;
pixel( x , y ) = newPixel;
qerror = oldPixel - newPixel;
if (x<w-1) pixel( x+1, y ) = pixel( x+1 , y ) + qerror * 7.0 / 16;
if (y<h-1)
{
pixel( x, y +1 ) = pixel( x, y + 1 ) + qerror * 5.0 / 16;
if (x>0) pixel( x-1 , y +1 ) = pixel( x-1 , y + 1 ) + qerror * 3.0 / 16;
if (x<w-1) pixel( x+1 , y +1 ) = pixel( x+1 , y + 1 ) + qerror * 1.0 / 16;
}
}
}
}
unsigned findbestColor(
struct retroScreen *screen,
unsigned char r,
unsigned char g,
unsigned char b )
{
int found = 0;
if (screen)
{
struct retroRGB *rgb = screen -> orgPalette;
int dr,dg,db;
int n;
int d;
int bestd = 256 * 3;
for (n=0;n<256;n++)
{
dr = rgb -> r - r;
dg = rgb -> g - g;
db = rgb -> b - b;
d = abs(dr+dg+db);
if (d<bestd)
{
bestd = d;
found = n;
}
}
}
return found;
}
void floyd(struct RastPort *rp, int w, int h, struct retroScreen *screen)
{
int x,y;
int off;
uint32_t argb;
double *image_r = (double*) malloc( sizeof(double) * w* h );
double *image_g = (double*) malloc( sizeof(double) * w* h );
double *image_b = (double*) malloc( sizeof(double) * w* h );
int r,g,b;
int v;
proc_names_printf("%s:%s:%d\n",__FILE__,__FUNCTION__,__LINE__);
if ( (image_r) && (image_g) && (image_b) )
{
proc_names_printf("%s:%s:%d\n",__FILE__,__FUNCTION__,__LINE__);
for (y=0;y<h;y++)
{
for (x=0;x<w;x++)
{
argb = ReadPixelColor(rp,x,y);
off = y*w+x;
image_r[off] = (double) ((argb & 0xFF0000) >> 16);
image_g[off] = (double) ((argb & 0xFF00) >> 8);
image_b[off] = (double) (argb & 0xFF) ;
}
}
proc_names_printf("%s:%s:%d\n",__FILE__,__FUNCTION__,__LINE__);
floydChannel( image_r, w, h );
floydChannel( image_g, w, h );
floydChannel( image_b, w, h );
proc_names_printf("%s:%s:%d\n",__FILE__,__FUNCTION__,__LINE__);
for (y=0;y<h;y++)
{
for (x=0;x<w;x++)
{
pixel(x,y,r);
pixel(x,y,g);
pixel(x,y,b);
retroPixel( screen, x, y, (r<<4) | (g<<2) | b);
}
}
}
proc_names_printf("%s:%s:%d\n",__FILE__,__FUNCTION__,__LINE__);
if (image_r) free(image_r);
if (image_g) free(image_g);
if (image_b) free(image_b);
}