代码: 全选
#include "string.h"
#include "stdio.h"
#include "png.h"
#include "math.h"
int write_png(char *filename)
{
FILE *fp;
png_structp png_ptr;
png_infop info_ptr;
int ERROR = 1;
fp = fopen(filename, "wb");
if (fp == NULL) {
printf("Can't open file : %s\n", filename);
return ERROR;
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
printf("ERROR : png_create_write_struct \n");
fclose(fp);
return ERROR;
}
/* Allocate/initialize the image information data. REQUIRED */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
printf("ERROR : png_create_info_struct \n");
fclose(fp);
png_destroy_write_struct(&png_ptr, png_infopp_NULL);
return (ERROR);
}
/* Set error handling. REQUIRED if you aren't supplying your own
* error handling functions in the png_create_write_struct() call.
*/
if (setjmp(png_jmpbuf(png_ptr)))
{
/* If we get here, we had a problem writing the file */
printf("ERROR when reading the file \n");
fclose(fp);
png_destroy_write_struct(&png_ptr, &info_ptr);
return (ERROR);
}
/* set up the output control if you are using standard C streams */
png_init_io(png_ptr, fp);
const int width = 800;
const int height = 600;
png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_GRAY,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
png_uint_32 k;
png_byte image[height][width];
png_bytep row_pointers[height];
//main functions
int i;
float x=0,y=0,r,u=0;
srand(time(0));
for(i=0;i<=500000;i++)
{
r=rand()%100;
if (r < 1)
{
x = 0;
y = .16 * y;
}
if (r>=1 && r<86)
{
u = .85 * x + .04 * y;
y = -.04*x + .85 * y + 1.6;
x = u;
}
if (r>=86 && r<97)
{
u = .2 * x - .26 * y;
y = .23 * x + .22 * y + 1.6;
x = u;
}
if (r>=97)
{
u = -.15 * x + .28 * y;
y = .26 * x + .24 * y +.44;
x = u;
}
int m,n;
m=45*x+230;
n=480-45*y;
/* if(m<0)
m=fabs(m);
if(n<0)
n=fabs(n);
*/ image[m][n]=240;
// printf("image[%d][%d]=255\n",m,n);
/* int m,n;
m=x;
n=y;
memset(image[n][m],255,sizeof(png_byte));
*/
/*
int m,n;
n=y;
for(n=0;n
{
if(x==m)
{image[m][n],255,sizeof(png_byte);}
}
*/
/* for(m=0;m
{
for(n=0;n
{
if(width==x&&height==y)
// image[n][m]=char(255);
memset(image[n][m],255,sizeof(png_byte));
}
}
*/
//setpixel(x,y);
// image[45*x+230][480-45*y]=char(255);
}
//*
// fade out
for (k = 0; k < height; k++) {
// memset(image[k], k / 2, width);
// memset(image[k],240, width);
row_pointers[k] = image[k];
}
/*/
// sinusoid
memset(image, 255, sizeof(image));
for (k = 0; k < height; k++) {
double r = double(k) / height * 6 * 3.14159265358;
int y = sin(r) * (width / 2 - 1);
image[k][y + width / 2] = 0;
row_pointers[k] = image[k];
}
//*/
png_write_image(png_ptr, row_pointers);
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
/* close the file */
fclose(fp);
return 0;
}
int main()
{
write_png("testpnggray.png");
}