【问题标题】:Trying to copy data from bmp file to a text file试图将数据从 bmp 文件复制到文本文件
【发布时间】:2015-02-04 15:10:22
【问题描述】:

我试图将像素数据从 bmp 文件复制到 c 中的文本文件(白色为 0,每隔一种颜色为 1),但由于某种原因,文本文件出现三倍(三个区域为 1)和在错误的方向,我不知道为什么。无论如何,这是我的 scipt:

#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
#pragma pack(1)
typedef struct {
unsigned char type1;                 /* Magic identifier            */
unsigned char type2;
unsigned int size;                       /* File size in bytes          */
unsigned int reserved;
unsigned long offset;                     /* Offset to image data, bytes */
} Header;
#pragma pack(2)
typedef struct {
unsigned int size;               /* Header size in bytes      */
int width,height;                /* Width and height of image */
unsigned short int planes;       /* Number of colour planes   */
unsigned short int bits;         /* Bits per pixel            */
unsigned int compression;        /* Compression type          */
unsigned int imagesize;          /* Image size in bytes       */
unsigned int xresolution,yresolution;     /* Pixels per meter          */
unsigned int ncolours;           /* Number of colours         */
unsigned int importantcolours;   /* Important colours         */
} Infoheader;

typedef struct
{   unsigned char Red, Green, Blue,Reserved;
} Pixel;


void main()
{
int n;
FILE *fin;
Header *fileheader;
Infoheader *information;
Pixel *ppixel,**pImage;
fin=fopen("TEST_web.bmp","rb");
    if (fin==NULL)
    {
    printf("Error reading image\n");
    exit(1);
    }
int i,j;
fileheader=(Header *)malloc(sizeof(Header));
information=(Infoheader *)malloc(sizeof(Infoheader));
ppixel=(Pixel *)malloc(sizeof(Pixel));
fread(fileheader,sizeof(Header),1,fin);
    if ((*fileheader).type1!='B'&&(*fileheader).type2!='M')
    {
    printf("Not a bmp file\n");
    exit(1);
    }
n=fread(information, sizeof(Infoheader),1,fin);
    if(n!=1)
    {
    printf("Error reading image information\n");
    exit(1);
    }
pImage = (Pixel **)malloc(sizeof(Pixel *) * information->height);
    for(i = 0; i < information->height; i++)
    {   pImage[i] = (Pixel *)malloc(sizeof(Pixel) * information->width);
    }
fseek(fin,fileheader->offset,SEEK_SET);
    for(i = 0; i < information->width; i++)

    {   for(j = 0; j < information->height; j++)

    {   fread(&(*ppixel).Red, sizeof(unsigned char), 1, fin);

        fread(&(*ppixel).Green, sizeof(unsigned char), 1, fin);

        fread(&(*ppixel).Blue, sizeof(unsigned char), 1, fin);


        pImage[i][j] = *ppixel;

    }

}
char data[information->width][information->height];
        for(i = 0; i <information->width; i++)

        {   for(j= 0; j<information->height; j++)

            {   
                if((pImage[i][j]).Red==255&&(pImage[i][j]).Green==255&& Image[i][j]).Blue==255)
                data[i][j]='0';
                else
                data[i][j]='1';
            }
        }
FILE *fout;
fout=fopen("text.txt","wt");

for(i = 0; i < information->width; i++)

{   for(j = 0; j < information->width ; j++)

    {             fputc(data[i][j],fout);



}
fputs("\n",fout);
}

free(fileheader);
free(information);
free(ppixel);
for(i = 0; i < information->height; i++)
    free(pImage[i]);
fclose(fin);

}

Im working for now with this picture

【问题讨论】:

  • 好的,您在调试代码时发现了什么?
  • 另外请正确缩进您的代码。像这样读起来没意思。
  • 错误的方向部分似乎是for(i = information-&gt;width-1; i &gt;=0; i--)中故意的
  • 还要注意,在 BMP 中,扫描线用 0 填充,这样每条扫描线的大小都可以被 4 整除
  • 请缩进您的代码。另外,通过像gdb 这样的调试器编译和运行它。这会有所帮助。

标签: c copy bmp


【解决方案1】:

而不是这个:

for(i = 0; i < information->width; i++)    
{
    for(j = 0; j < information->height; j++)   

使用这样的东西:

for(y = 0; y < information->height; y++)    
{
    for(x = 0; x < information->width; x++)   

(当然,在扫描位图时也是如此。)

【讨论】:

  • 感谢您的帮助,对不起,乱七八糟的,我总是有点乱,我试图让它变得更好一点。关于使用数字火星编译器的填充即时消息,它没有得到 0,但它被填充(标题为 14 字节,信息标题为 40 字节)。关于循环,我这样做的唯一原因是颠倒的,因为我读到 bmp 文件的字节的读取是颠倒的,无论如何,如果我将它从 0 更改为最后,我仍然会得到它的颠倒和 3 次.
  • 是的,位图是颠倒的。 (通常或总是,我不记得了。)但我根本不是指沿 Y 轴计数的方向。我指的是你的 Y 环必须是外环,你的 X 环必须是内环,而不是相反。
  • 至于三次,我不知道,如果你修复循环的嵌套,问题可能会消失,也许不会,但我不能真正花太多精力阅读你的代码,因为它是格式很糟糕。
猜你喜欢
  • 1970-01-01
  • 2015-02-01
  • 2018-09-04
  • 1970-01-01
  • 2011-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多