如何使用C语言使用fopen()读取图像 [英] How can read image with fopen() using C language

查看:185
本文介绍了如何使用C语言使用fopen()读取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用fopen()函数读取c中的图像后,我尝试转换到表格时,某些像素(caracter)无法转移并且只能错误地转移

例如某些像素当我用记事本打开图像=XGll ??®Ís(VGuëåëïëëïïëïïë)

在标签中变为ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

哪里有问题?



我尝试了什么:



char ** tab;

nb = 1;

longth =(图像大小)按fseek函数计算



pfile =image.pgm;



tab =(char **)malloc((nb)* sizeof(char *));



fp = fopen(pfile,r);



for(i = 0; i< nb; i ++)>

{

tab [i] =(char *)malloc((40)* sizeof(char));

}



ff = fopen(tab [i],w);



for(j = 0; j< longth; j ++)>

{

c = fgetc(fp);



fputc((char)c,ff);

}

fclose(ff);

fclose( fp)

when i tried to read an image in c with fopen () function after i try to transfert to table , some pixel (caracter) can't transfert and only trasfert incorrectly
example some pixel in image when i opened with notepad= "XGll®Ís(VGuë åëïëëëïåëïìë")
in tab becomes "ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ"
where are the probleme?

What I have tried:

char** tab;
nb=1;
longth=(size of the image) calculate by fseek function

pfile="image.pgm";

tab=(char**)malloc((nb)*sizeof(char*));

fp=fopen(pfile,"r");

for(i=0;i<nb;i++)>
{
tab[i]=(char*)malloc((40)*sizeof(char));
}

ff=fopen(tab[i],"w");

for(j=0;j<longth;j++)>
{
c=fgetc(fp);

fputc((char)c,ff);
}
fclose(ff);
fclose(fp)

推荐答案

对于初学者,请停止使用 char ,并使用 unsigned char - 您可能会丢失最高位数据。

其次,停止使用 fgetc fputc - 它们用于基于字符的数据,而图像则不是。相反,使用 fread fwrite

C库函数fread() [ ^ ]

C库函数fwrite() [ ^ ]

两者都用于二进制数据。

我也用块而不是单个字符/字节值来读取和写入。



但是......你确实意识到你没有;当你用它打开文件时,你的标签数组中有任何文件名吗?你确实需要每次使用时都检查fopen的返回值...
For starters, stop using char, and use unsigned char - you could be losing data in the top bit.
Second, stop using fgetc and fputc - they are for character based data, and images aren't that. Instead, use fread and fwrite:
C library function fread()[^]
C library function fwrite()[^]
Both of which are intended for binary data.
I'd also read and write it in "chunks" rather than individual character / byte values.

But... you do realize you don;t have any file names in your tab array when you use it to open files? You really, really need to check the return values from fopen every time you use it...


你的程序真的很奇怪,你花了一半的程序分配内存而不是你没有使用。

学习C / C ++基础知识的几个链接:

C编程语言 - 维基百科,免费的百科全书 [ ^ ]

https://hassanolity.files.wordpress.com/2013/11/ the_c_programming_language_2.pdf [ ^ ]

http://www.ime。 usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf [<啊ref =http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdftarget =_ blanktitle =New Window> ^ ]
your program is really weird, you spend half of the program allocating memory than you don't use.
A few links to learn C/C++ basics:
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]


您正在使用包含二进制数据的文件。为避免处理二进制数据时出现问题,您应使用 unsigned char 而不是 char 缓冲区。



出于同样的原因, fegtc() fputc()使用 int 作为返回值和参数。那你为什么要把值写成?

You are working with a file containing binary data. To avoid problems when handling binary data you should use unsigned char rather than char buffers.

For the same reason the fegtc() and fputc() use int as return value and parameter. So why you cast the value to be written?
fputc((char)c,ff);



c 大于127(字节的MSB设置)MSB将被您的转换清除写入不同的值(值128 / 0x80将变为零,129 / 0x81将变为1,依此类推)。只需传递 c 而不进行强制转换, fputc()函数将写入正确的值。如果你想对依赖于符号的值(比如移位)做一些事情,请声明 c 作为无符号类型。



使用Windows时,您还必须以二进制模式打开文件(rbresp。wb)。



您发布的代码不完整,它是不清楚你想要实现什么。因此无法提供更多帮助。


When c is greater than 127 (the MSB of the byte is set) the MSB will be cleared by your casting writing a different value (the value 128 / 0x80 will become zero, 129 / 0x81 will become 1 and so on). Just pass c without casting and the fputc() function will write the correct value. If you want to do something with the value that is sign dependent (like shifting) declare c as an unsigned type.

When using Windows you must also open the file in binary mode ("rb" resp. "wb").

Your posted code is incomplete and it is not clear what you want to achieve. So it is impossible to provide more help.


这篇关于如何使用C语言使用fopen()读取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆