将位图图像写入文件

时间:2014-12-15 01:17:04

标签: c bitmap bit-manipulation

我正在尝试从一个文件中读取位图图像并将其写入另一个文件,以检查内容是否已传输。但是,当我运行我的代码时,图像不会被创建到输出文件中。相反,每当我点击新文件时,它都会告诉我该文件无法打开。这是我的头文件的样子:

//////////////////////////////////////////////////////////////////////
// RGB.h  header file for bitmap BMP definitions
// adapted from <WinGDI.h> 
///To get DIB header struct.   winGDI.h is huge overkill for the needs here


#ifndef RGBH  //don't doubly include this stuff, set a flag when first including
#define RGBH
/* structures for defining DIBs //from WinGDI.h  */
typedef unsigned short WORD;
typedef unsigned int DWORD;
typedef unsigned long int LONG;
typedef unsigned char BYTE;

typedef struct tagBITMAPFILEHEADER {
        WORD    bfType;
        DWORD   bfSize;
        WORD    bfReserved1;
        WORD    bfReserved2;
        DWORD   bfOffBits;
} BITMAPFILEHEADER, *PBITMAPFILEHEADER;
typedef PBITMAPFILEHEADER pbfh;

typedef struct tagBITMAPINFOHEADER {
        DWORD   biSize;                 /* used to get to color table */
        DWORD   biWidth;
        DWORD   biHeight;
        WORD    biPlanes;
        WORD    biBitCount;
} BITMAPINFOHEADER, *PBITMAPINFOHEADER;
typedef BITMAPINFOHEADER pbih;

typedef struct tagRGBTRIPLE {  //from WinGDI.h
        BYTE    b; //rgbtBlue;
        BYTE    g; //rgbtGreen;
        BYTE    r; //rgbtRed;
} pix, *ppix;

#define BYTES_PER_PIX sizeof(pix)

const pix RED = {0,0,255};
const pix GREEN = {0,255,0};
const pix BLUE = {255,0,0};

#endif
////////////////////// End of RGB.h header file ////////////////////////////////

这是我写的代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "RGB.h" //header file for Bitmap  

BITMAPFILEHEADER *hp; //var pointer to the header file for bitmap   
BITMAPINFOHEADER *p; //var pointer to the info file for bitmap 

#define SIZE 100
int main(void)
{
    FILE *bp, *op; //var for two file streams 
    char pix[SIZE]; //var for char name of input file
    char out[SIZE]; //var for char name of output file 
    int n; //var to hold different values through out the program 
    unsigned char *bitmapImage; //var to store image data 


    printf("Enter bitmap filename: "); //prompts user to enter filename
    scanf("%s", pix);  //collects filename from user 

    //open filename in read binary mode
    if((bp = fopen(pix, "rb")) == NULL)                                                                                                                                                                                                                                                                                                                                                                                         if ((bp = fopen(pix, "rb")) == NULL)
    {
        printf("Can't open file %s", bp);  //prints if file path wasn't valid 
        getchar();
        exit (1);
    }

    //malloc memory for file header 
    hp = (tagBITMAPFILEHEADER*)malloc(sizeof(tagBITMAPFILEHEADER));

    //reads the bitmap file header
    n = fread(pix, 1,SIZE, bp);

    //read bitmap info header
    fread(bp, sizeof(tagBITMAPINFOHEADER), 1, bp);

    //allocate enough memory for the bitmap image data
    p = (tagBITMAPINFOHEADER*) (pix+14); 
    bitmapImage = (unsigned char*) malloc(p->biSize);

    //verify memory allocation 
    if (!bitmapImage)
    {
        free(bitmapImage);
        fclose(bp);
            puts("Memory allocation not allowed");
            return NULL;
    }

    puts("Input File Statistics: \n");
    printf("File size: %ld bytes",  p->biSize); //prints out total file size
    printf("\nWidth x Height = %ld x %ld",  p->biWidth,  p->biHeight); //prints out dimensions of image 
    printf("\nBits/pixel = %u\n", p->biBitCount);


    printf("\nEnter an output filename for bitmap: "); //prompts user for output file name 
    scanf("%s", out); //collects pathway from user 

    //open output file 
    if((op = fopen(out, "wb")) == NULL)
    {
        puts("Invalid file");
        fclose(op);
        exit (2);
    }




    fclose(op);


    return 0;

}

任何建议都将不胜感激!谢谢! O. Helm

1 个答案:

答案 0 :(得分:1)

有时候这个解决方案太容易被人看到了。

你是否使用相同的&#34;计数器&#34;在两个for循环中?我认为这不会奏效。

另外,在你的fread和fwrite中,你可能在同一个地方读书和写作。您需要更新您正在阅读的位置的指针。

你的第一个fread的评论说&#34;读取位图文件标题&#34;。我不确定它是否正确这样做。

检查你的第二个恐惧。这可能有一个致命的问题。您确定要阅读进入 bp吗?如果您为变量使用更有意义的名称,它可能会对您有所帮助。

你需要写BITMAPFILEHEADER和BITMAPINFOHEADER out 吗?检查以确保您正确执行此操作。

由于这是一个课堂作业,显然最好不给出精确的帮助。我希望这有助于取得进展。如果有什么比我希望你能通过密切观察找到它们。