{
pixel_write(i,j);
}
}
fclose(fp);
return 0;
}
void pixel_write(int i, int j)
{
static unsigned char color[3];
float t = j + i*0.001f;
memcpy(color, &t, 3);
fwrite(color, 1, 3, fp);
// 其实更简单粗爆的方式是
//fwrite(&t, 1, 3, fp);
}
复制代码
代码运行后会生成一种PPM格式的图像,如下图所示:
图像多少有点分形的感觉。PPM格式的图像不太常见,它是一种非常简单的图像格式。在我写的软件Why数学图像生成工具中可以查看,当然我也将该图像的生成算法写到这个软件中,相关代码如下:
复制代码
#ifndef _PixelFloatConvertInt_H_
#define _PixelFloatConvertInt_H_
// --------------------------------------------------------------------------------------
#include "IPixelEquation.h"
// --------------------------------------------------------------------------------------
class CPixelFloatConvertInt : public IPixelEquation
{
public:
CPixelFloatConvertInt()
{
m_width = 1000;
m_height = 1000;
}
const char* GetName() const
{
return "Float Convert Int";
}
unsigned int CalculatePixel(unsigned int x, unsigned int y)
{
float t = y + x*0.001f;
unsigned int rst = *(unsigned int*)&t;
rst |= 0xff000000;
return rst;
}
};
// --------------------------------------------------------------------------------------
#endif