1

我编写了一个代码来简单地读/写(复制)一个 *.bmp 文件。但是有问题我的程序一遍又一遍地运行......我的意思是看起来里面有一个while(true)循环或其他东西。这是我的代码:

#include <stdio.h>
#include <stdlib.h>

#pragma pack(push, 1)
typedef struct Pix
{
  unsigned char R;
  unsigned char G;
  unsigned char B;
  unsigned char L;
  int BW;
}Pix;
#pragma pack(pop)

#pragma pack(push, 1)
typedef struct BitMap
{
    short Signature;
    long Reserved1;
    long Reserved2;
    long DataOffSet;

    long Size;
    long Width;
    long Height;
    short Planes;
    short BitsPerPixel;
    long Compression;
    long SizeImage;
    long XPixelsPreMeter;
    long YPixelsPreMeter;
    long ColorsUsed;
    long ColorsImportant;
    struct Pix *pixels
}BitMap;
#pragma pack(pop)

int main(int argc, char **argv)
{

unsigned long int i=0;//to count pixels readed
unsigned long int S=0;//number of pixcels to read

struct BitMap source_info;//to store bitmap info header
struct Pix source_pix;// to store pixcels

FILE *fp;//file pointer for source file
FILE *Dfp;//file ponter for distenation file

if(!(fp=fopen("in.bmp","rb")))//open in binery read mode
{
printf(" can not open file");//prind and exit if file open error
exit(-1);
}


Dfp=fopen("out.bmp","wb");//opne in binery write mode
//read the headers to souirce file
fread(&source_info, (sizeof(long)*3 + sizeof(short)),1,fp);

//calucate the number of pix to read
S=source_info.Width*source_info.Height;
source_info.pixels =  (struct Pix *) malloc(sizeof(struct Pix)*S);

//read pixcels
for(i=1;i<=S;i++)
{
//read pixcel form source file
fread(&source_pix,sizeof(struct Pix),1,fp);
source_info.pixels[i-1] = source_pix;
}

// write header to dest file
fwrite(&source_info,  (sizeof(long)*3 + sizeof(short)),1,Dfp);
// write pixels to dest file
for(i=1;i<=S;i++)
{
    fwrite(&source_info.pixels[i-1],sizeof(struct Pix),1,Dfp);
}

//close all fiels
fclose(fp);
fclose(Dfp);
return 0;
}
4

3 回答 3

3

对于我尝试的示例图像,您读取的像素数 (S) 为 3762821376。这显然太大了。

查看 BMP-specstruct BitMap您使用的是否正确。

编辑 1

更改这些:

fread(&source_info, (sizeof(long)*3 + sizeof(short)),1,fp);
...
fwrite(&source_info,  (sizeof(long)*3 + sizeof(short)),1,Dfp);

fread(&source_info, sizeof(source_info),1,fp);
...
fwrite(&source_info, sizeof(source_info),1,Dfp);

它现在可以很好地复制我的测试 .bmp。

编辑2:

我认为您机器上的颜色切换问题是因为您这样做了source_info.pixels = ...。您应该使用自己的指针Pix* pixels = malloc...并在循环中更改source_info.pixelspixels. 只是不要将你的 malloc 分配到 source_info 结构中,它应该没问题

于 2013-02-22T11:25:21.293 回答
1

您正在访问未初始化的source_info.Widthandsource_info.Height数据,因为当您fread获取文件的标头数据时,您只需在第一个4 fields.

因此 source_info 的其他字段可能包含垃圾数据。(特别widthheight因为它们会影响下一个循环)

确保阅读完整的标题更改

fread(&source_info, (sizeof(long)*3 + sizeof(short)),1,fp);

fread(&source_info, sizeof(BitMap),1,fp);

然后确保出于同样的原因正确转储它,链接

fwrite(&source_info,  (sizeof(long)*3 + sizeof(short)),1,Dfp);

fwrite(&source_info, sizeof(BitMap),1,Dfp);
于 2013-02-22T11:38:05.757 回答
0

您需要跳过复制 Pix 指针,它可以在您想要的单个结构中为我工作。

fread(&source_info, (sizeof(BitMap) - sizeof(struct Pix*)),1,fp);

顺便说一句,如果您将 Pix 定义为

typedef struct Pix
{
  unsigned char R;
  unsigned char G;
  unsigned char B;
}Pix;
于 2013-10-09T10:21:45.677 回答