怎么用C语言读取gif图片内容文件

怎么用C语言读取gif图片内容文件,第1张

分类: 电脑/网络 >>程序设计 >>其他编程语言

问题描述:

怎么用C语御卖言读取gif图片内容文件 假如文件中有6个gif图片,只读取第一个图片读完后写入一个空文件中

解析:

用二进制文件方法打开。

(1)读gif_id,输出gif_id, 6 bytes

(2)读logical_screen_descriptor(结构),并输出

(3)如果 ScrHd.flag_GC_table == 1,

则有全局色表,贺拆备则读全局色表,并输出

(4)循环,

检查gif块的类型,按块的类型读块,并输出

若:

case: 0x2c -- 它是,Image Descriptor(结构),读块并输出

判断有无色表,有则读块并输禅毁出,

下面 1 byte 表示 lZW min Code Size,读并输出

子块 (include size, bytes*size data) 读块并输出

子块 (include size, bytes*size data) 读块并输出

可能有很多子块,读块并输出

直到 0x00 读块并输出,

break

若case: 0x21 0xf9 -- 控制块,读到 0x00, 并输出

break

若case: 0x21 0xFe -- 注解块,读到 0x00,并输出

break

若case: 0x21 0x01 -- 普通文本块,读到 0x00,并输出

break

若case: 0x21 0xff -- 应用程序块,读到 0x00,并输出

break

若case: 0x3B -- END 第一个图片完毕的标识,并输出

break

default: 打印“文件错误”break

(5)关闭文件。

#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" <<endlreturn}

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 = 0i for (int j = 0j if (iw - 3 || j>h - 3)

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

else

{

float temp = 0

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

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

{

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" <<endlreturn}

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 <stdlib.h>

#include <windows.h>

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

{

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

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

}

#include <stdio.h>

#include <stdlib.h>

#include <windows.h>

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

{

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

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

}

扩展资料:

c语言读取TXT文件:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_LINE 1024

int main()

{

char buf[MAX_LINE]  /*缓冲区*/

FILE *fp            /*文件指针*/

int len             /*行字符个数*/

if((fp = fopen("test.txt","r")) == NULL)

{

perror("fail to read")

exit (1)

}

while(fgets(buf,MAX_LINE,fp) != NULL)

{

len = strlen(buf)

buf[len-1] = '\0'  /*去掉换行符*/

printf("%s %d \n",buf,len - 1)

}

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12382688.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存