A
Army1987
Under Linux (gcc) this works OK, but under Windows (lcc-win32)
p.Blue always is zero.
Within the function itself, result.Blue is correct (either 0, 255,
or the floor of the expression assigned to it modulo 256, according
to the sign of n). But when returned to main(), it magically turns
to zero. (The code below is entirely copied and pasted.)
What the deuce is happening? Any ideas? sizeof(struct Pixel) is 3,
if that can matter to anything (but so it is with gcc).
#define DEBUG
#include <stdio.h>
#include <limits.h>
#include <math.h>
struct Pixel {
unsigned char Red;
unsigned char Green;
unsigned char Blue;
} colormap(int n, int max);
#ifdef DEBUG
int main(void)
{
int n, max;
while (scanf("%d%d",&n, &max) > 1) {
struct Pixel p = colormap(n, max);
printf("%d %d %d\n", (int)p.Red, (int)p.Green,
(int)p.Blue);
if (!p.Blue)
puts("p.Blue is *really* zero.");
}
return 0;
}
#else
/*real program*/
#endif
struct Pixel colormap(int n, int max)
{
struct Pixel result;
if (n < 1) { /* black if negative, white if zero */
result.Red = n ? 0 : UCHAR_MAX;
result.Green = n ? 0 : UCHAR_MAX;
result.Blue = n ? 0 : UCHAR_MAX;
} else {
const double pi = 4 * atan(1);
double val = UCHAR_MAX*(1 - log(n)/log(max))
+ log(n)/log(max);
double hue = 2 * pi * log(n) / log(2.0);
result.Red = val * (1 + cos(hue))/2;
result.Green = val * (1 + cos(hue - 2*pi/3))/2;
#ifdef DEBUG
printf("%g\t", val * (1 + cos(hue - 4*pi/3))/2);
#endif
result.Blue = val * (1 + cos(hue - 4*pi/3))/2;
}
#ifdef DEBUG
printf("result.Blue is %d\n", (int)result.Blue);
#endif
return result;
}
Behaviour with gcc:
army1987@army1987-laptop:~$ ./a.out
-1 255
result.Blue is 0
0 0 0
p.Blue is *really* zero.
0 255
result.Blue is 255
255 255 255
1 255
63.75 result.Blue is 63
255 63 63
42 255
35.4608 result.Blue is 35
9 80 35
23 32767
144.707 result.Blue is 144
0 121 144
ok thanks
army1987@army1987-laptop:~$
Behaviour with lcc-win32:
C:\lcc\projects\lcc>test
-1 255
result.Blue is 0
0 0 0
p.Blue is *really* zero.
0 255
result.Blue is 255
255 255 0
p.Blue is *really* zero.
1 255
63.75 result.Blue is 63
255 63 0
p.Blue is *really* zero.
42 255
35.4608 result.Blue is 35
9 80 0
p.Blue is *really* zero.
23 32767
144.707 result.Blue is 144
0 121 0
p.Blue is *really* zero.
go f*** yourself
C:\lcc\projects\lcc>
p.Blue always is zero.
Within the function itself, result.Blue is correct (either 0, 255,
or the floor of the expression assigned to it modulo 256, according
to the sign of n). But when returned to main(), it magically turns
to zero. (The code below is entirely copied and pasted.)
What the deuce is happening? Any ideas? sizeof(struct Pixel) is 3,
if that can matter to anything (but so it is with gcc).
#define DEBUG
#include <stdio.h>
#include <limits.h>
#include <math.h>
struct Pixel {
unsigned char Red;
unsigned char Green;
unsigned char Blue;
} colormap(int n, int max);
#ifdef DEBUG
int main(void)
{
int n, max;
while (scanf("%d%d",&n, &max) > 1) {
struct Pixel p = colormap(n, max);
printf("%d %d %d\n", (int)p.Red, (int)p.Green,
(int)p.Blue);
if (!p.Blue)
puts("p.Blue is *really* zero.");
}
return 0;
}
#else
/*real program*/
#endif
struct Pixel colormap(int n, int max)
{
struct Pixel result;
if (n < 1) { /* black if negative, white if zero */
result.Red = n ? 0 : UCHAR_MAX;
result.Green = n ? 0 : UCHAR_MAX;
result.Blue = n ? 0 : UCHAR_MAX;
} else {
const double pi = 4 * atan(1);
double val = UCHAR_MAX*(1 - log(n)/log(max))
+ log(n)/log(max);
double hue = 2 * pi * log(n) / log(2.0);
result.Red = val * (1 + cos(hue))/2;
result.Green = val * (1 + cos(hue - 2*pi/3))/2;
#ifdef DEBUG
printf("%g\t", val * (1 + cos(hue - 4*pi/3))/2);
#endif
result.Blue = val * (1 + cos(hue - 4*pi/3))/2;
}
#ifdef DEBUG
printf("result.Blue is %d\n", (int)result.Blue);
#endif
return result;
}
Behaviour with gcc:
army1987@army1987-laptop:~$ ./a.out
-1 255
result.Blue is 0
0 0 0
p.Blue is *really* zero.
0 255
result.Blue is 255
255 255 255
1 255
63.75 result.Blue is 63
255 63 63
42 255
35.4608 result.Blue is 35
9 80 35
23 32767
144.707 result.Blue is 144
0 121 144
ok thanks
army1987@army1987-laptop:~$
Behaviour with lcc-win32:
C:\lcc\projects\lcc>test
-1 255
result.Blue is 0
0 0 0
p.Blue is *really* zero.
0 255
result.Blue is 255
255 255 0
p.Blue is *really* zero.
1 255
63.75 result.Blue is 63
255 63 0
p.Blue is *really* zero.
42 255
35.4608 result.Blue is 35
9 80 0
p.Blue is *really* zero.
23 32767
144.707 result.Blue is 144
0 121 0
p.Blue is *really* zero.
go f*** yourself
C:\lcc\projects\lcc>