小编Lit*_*ony的帖子

为什么在使用rand()时会得到这种特殊的颜色模式?

我试图创建一个图像文件,如下所示:

uint8_t raw_r[pixel_width][pixel_height];
uint8_t raw_g[pixel_width][pixel_height];
uint8_t raw_b[pixel_width][pixel_height];
uint8_t blue(uint32_t x, uint32_t y)
{
    return (rand()%2)? (x+y)%rand() : ((x*y%1024)%rand())%2 ? (x-y)%rand() : rand();
}
uint8_t green(uint32_t x, uint32_t y)
{
    return (rand()%2)? (x-y)%rand() : ((x*y%1024)%rand())%2 ? (x+y)%rand() : rand();
}
uint8_t red(uint32_t x, uint32_t y)
{
    return (rand()%2)? (y-x)%rand() : ((x*y%1024)%rand())%2 ? (x+y)%rand() : rand();
}

for (y=0; y<pixel_height; ++y)
{
    for (x=0; x<pixel_width; ++x)
    {
        raw_b[x][y]=blue(x, y);
        raw_g[x][y]=green(x, y);
        raw_r[x][y]=red(x, y);
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望随机得到一些东西(白噪声).但是,输出很有意思:

你知道原因吗?


编辑

现在,很明显它与之无关rand().

也试试这段代码: …

c random image

169
推荐指数
2
解决办法
7457
查看次数

printf() 格式化十六进制 uint32_t 且不丢失零。(在 c89 中)

想要打印 uint32 十六进制。尝试过“PRIx32”,对我来说看起来很糟糕:

967407c0 23 4481d55f ffffff52 de3cd140
2fc aa7363cf fffff40d 563270c0 2b86
Run Code Online (Sandbox Code Playgroud)

所以...尝试了这个。肮脏但有效:

 /*******************************************************************************
 Print a digit in hex.
 *******************************************************************************/
 void mpal_digit_t_print_hex(
     mpal_digit_t * input)
 {
     uint8_t p_1, p_2, p_3, p_4, p_5, p_6, p_7, p_8;

     p_1 = (uint8_t)(((*input) << 0) >> 28);
     p_2 = (uint8_t)(((*input) << 4) >> 28);
     p_3 = (uint8_t)(((*input) << 8) >> 28);
     p_4 = (uint8_t)(((*input) << 12) >> 28);
     p_5 = (uint8_t)(((*input) << 16) >> 28);
     p_6 = (uint8_t)(((*input) << 20) >> 28);
     p_7 = …
Run Code Online (Sandbox Code Playgroud)

c format printf hex

3
推荐指数
1
解决办法
5793
查看次数

标签 统计

c ×2

format ×1

hex ×1

image ×1

printf ×1

random ×1