c语言图片渐入代码_如何用c语言读取图片

这篇博客介绍了如何使用C语言实现图片的渐入效果。通过读取原始图片,运用特定的矩阵运算对像素进行处理,然后保存为新的图片。代码中包含读取、处理和保存图片的函数,以及一个简单的示例应用。
摘要由CSDN通过智能技术生成

展开全部

#include

using namespace std;

#define Twoto1(i,j,w) i*w+j

void createimage(unsigned char *&img, int w, int h)

{img = new unsigned char[w*h];}

void delateimage(unsigned char*img)

{delete []img;}

void readimage(unsigned char*img, int w, int h, char *fname)

{

FILE *fp;

fopen_s(&fp,fname, "rb");

if (fp == NULL){ cout << "error" << endl; return; }

size_t result;

result=fread(img , sizeof(unsigned char), w*h, fp);

if (result != w*h)

{

cout << "Reading error" << endl;

return;

}

else

cout << "Reading Ok!" << endl;

fclose(fp);

}

void mobanjuanji(unsigned char image, unsigned char*image1, int w, int h, float moban[5][5])

{

for (int i = 0; i for (int j = 0; j if (iw - 3 || j>h - 3)

image1[Twoto1(i,j,w)] = 0;

else

{

float temp = 0;

for (int m = 0; m<5; m++)

for (int n = 0; n<5; n++)

{

temp += (image[Twoto1(i-2+m,j-2+n,w)] moban[m][n]);

}

if (temp>255) image1[Twoto1(i, j, w)] = 255;

else if (temp<0) image1[Twoto1(i, j, w)] = 0;

else image1[Twoto1(i, j, w)] = temp;

}

}

void saveimage(unsigned char *img, int w, int h, char *fname)

{

FILE *fp;

fopen_s(&fp, fname, "wb");

if (fp == NULL) { cout << "error" << endl; return; }

size_t result;

result = fwrite(img, sizeof(unsigned char), w*h, fp);

if (result != w*h)

{

cout << "Write error" << endl;

return;

}

else

cout << "Write Ok!" << endl;

fclose(fp);

}

void main()

{

unsigned char *img;

unsigned char *img1;

float moban[5][5] = { {0,0,0,0,0},{0, -1, 0, 1, 0 }, { 0, -2, 0, 2, 0 }, { 0, -1, 0, 1, 0 }, { 0,0,0,0,0 } };

//float moban[5][5] = { 0 };

int w = 512, h = 512;

createimage(img, w, h);

createimage(img1, w, h);

readimage(img, w, h, "E:\ss.raw");

mobanjuanji(img, img1,w, h, moban);

saveimage(img, w, h, "E:\ss_1.raw");

saveimage(img1, w, h, "E:\ss_2.raw");

delateimage(img);

delateimage(img1);

}

扩展资料

C语言实现一个图片的读出和写入

#include

#include

int file_size(char* filename)//获取文件名为filename的文件大小。

{

FILE *fp = fopen(filename, "rb");//打开62616964757a686964616fe4b893e5b19e31333431376533文件。

int size;

if(fp == NULL) // 打开文件失败

return -1;

fseek(fp, 0, SEEK_END);//定位文件指针到文件尾。

size=ftell(fp);//获取文件指针偏移量,即文件大小。

fclose(fp);//关闭文件。

return size;

}

int main ()

{

int size=0;

size=file_size("qw");

printf("%d\n",size);

FILE * pFile,*qw;

char *buffer=(char*)malloc(sizeof(char)*size);

qw   =fopen("qw","r");

pFile = fopen ( "qwe" , "wb" );

printf("%d==\n",pFile);

printf("%d\n",size);

fread(buffer,1,size,qw);

fwrite (buffer , sizeof(byte), size , pFile );

fclose (pFile);

rename("qwe","Groot.jpg");

return 0;

}

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值