RLE算法应用于bmp文件

时间:2014-04-13 16:39:26

标签: c bitmap

我在尝试将游程长度算法应用于.bmp图片时遇到了问题。当我正在进行编码时,一切都运行良好,但在解码时有一个问题,我无法弄明白,即我无法读取重复像素的数量,因此我的输出是一个空白图像。当我使用调试器时,重复像素的数量不能从二进制文件中正确读取(在位图文件上应用RLE的结果)。这是我的代码

#include<stdlib.h>
#include <stdint.h>

//structure defiens bitmap header
struct BITMAPFILEHEADER{
   uint8_t type[2];//type of file (bit map)
   uint32_t size;//size of file
   uint16_t reserved1;//
   uint16_t reserved2;//
   uint32_t offsetbits;//off set bits
} __attribute__ ((packed));

struct BITMAPINFOHEADER{
   uint32_t size;//bitmap size
  // uint16_t w2;
   uint32_t width;//width of bitmap
  //uint16_t h2;
   uint32_t height;//hight of bitmap

   uint16_t planes;
   uint16_t bitcount;
   uint32_t compression;// compression ratio (zero for no compression)
   uint32_t sizeimage;//size of image
   long xpelspermeter;
   long ypelspermeter;
   uint32_t colorsused;
   uint32_t colorsimportant;
} __attribute__ ((packed));


 //const char* INPUT_FILE = "/home/bogdan/bee.bmp";
const char* INPUT_FILE = "/home/bogdan/Linux.bmp";
const char* ENCODED_FILE = "/home/bogdan/encoded.bin";
const char* DECODED_FILE = "/home/bogdan/decoded.bmp";

typedef struct SINGLE_PIXEL{
    uint8_t green;//Green level 0-255
    uint8_t red;  //Red level 0-255
} PIXEL;

int comparePixels(PIXEL, PIXEL);
void encode();
void decode(char*);

int main()
{
    encode();
    decode(ENCODED_FILE);
    return 0;
}

void encode() {
    uint32_t i=0;//to count pixels read
    uint32_t pixno=0;//number of pixels to read

    struct BITMAPFILEHEADER source_head;//to store file header
    struct BITMAPINFOHEADER source_info;//to store bitmap info header
    PIXEL pixel;// the current pixel

    FILE *in;// bitmap imput pointer file
    FILE *out;//output file pointer

    if(!(in=fopen(INPUT_FILE,"rb")))//open in binary read mode
    {
    printf("\ncan not open file");//error at opening file
    exit(-1);
    }


out=fopen(ENCODED_FILE,"wb");//opne in binary write mode
//read the headers to source file
fread(&source_head,sizeof(struct BITMAPFILEHEADER),1,in);
fread(&source_info,sizeof(struct BITMAPINFOHEADER),1,in);

//write the headers to the output file
fwrite(&source_head,sizeof(struct BITMAPFILEHEADER),1,out);
fwrite(&source_info,sizeof(struct BITMAPINFOHEADER),1,out);

//cumpute the number of pixels to read
pixno=source_info.width*source_info.height;

// init list of pixels
PIXEL pixArr[pixno];
printf("total pixels: %d", pixno);

//printf("w:%f h:%u pn:%lu", (source_head.size/1024.0/1024), source_info.height, pixno);
uint32_t sum = 0;
//read, modify and write pixels
for(i=0;i<pixno;++i)
{
    //read pixel form source file
    fread(&pixel,sizeof(PIXEL),1,in);
    pixArr[i] = pixel;
}
for (i = 0; i < pixno; i++) {
   // printf ("i = %d\tred = %d green = %d blue = %d\n",i, pixArr[i].red, pixArr[i].green, pixArr[i].blue);
    int runlength = 1;
    while ((i + 1) < pixno) {
        if (comparePixels(pixArr[i], pixArr[i+1]) == 0){
        // printf ("i = %d\t red = %d green = %d blue = %d\n",i, pixArr[i].red, pixArr[i].green, pixArr[i].blue);
            runlength++;
            i++;
        } else {
            break;
        }
    }

    //fprintf(out, "%d", runlength);
    fwrite(&runlength, sizeof(runlength), 1, out);
    fwrite(&pixel,sizeof(PIXEL),1,out);
    sum += runlength;
    runlength = 0;
}
    //write the modification to the output file
    //close all fiels
    fclose(in);
    fclose(out);
    printf("sum = %d",sum);
}

void decode(char * filePath) {
    uint32_t i=0;//to count pixels read
    uint32_t j=0;
    uint32_t totalPixels=0;//number of pixels to read
    uint32_t pixelRepetition = 1;
    struct BITMAPFILEHEADER source_head;//to store file header
    struct BITMAPINFOHEADER source_info;//to store bitmap info header
    PIXEL pixel;// the current pixel

    FILE *in;// bitmap encoded pointer file
    FILE *out;//decoded bitmap file pointer
    if (!(in = fopen(filePath, "rb"))) {
        printf("\ncan not open file");
        exit(-1);
    }
    out = fopen(DECODED_FILE, "wb");
    //read the headers to source file
   fread(&source_head,sizeof(struct BITMAPFILEHEADER),1,in);
   fread(&source_info,sizeof(struct BITMAPINFOHEADER),1,in);

   //write the headers to the output file
   fwrite(&source_head,sizeof(struct BITMAPFILEHEADER),1,out);
   fwrite(&source_info,sizeof(struct BITMAPINFOHEADER),1,out);

   totalPixels=source_info.width*source_info.height;

   while(i < totalPixels) {

        fread(&pixelRepetition, sizeof(pixelRepetition), 1, out);
        fread(&pixel,sizeof(PIXEL),1,in);

       for (j = 0; j < pixelRepetition; j++) {
            fwrite(&pixel,sizeof(PIXEL),1,out);
       }
       i += pixelRepetition;
    }

    fclose(in);
    fclose(out);
}

int comparePixels(PIXEL px1, PIXEL px2) {
    if (px1.red == px2.red && px1.green == px2.green && px1.blue == px2.blue) {
      return 0;
    } else {
        return -1;
    }
}

我的实现工作如下:我读取了bmp文件的标题并将其直接放到另一个文件中。之后,我比较像素,看看它们是否具有相同的RGB值。如果这是真的(根据comparePixels函数)我将连续相同像素和一个像素(重复的像素)的数量放入文件中。这是编码阶段。在解码时,我正在读取图像的标题然后我试图读取重复次数(默认值为1,表示没有重复像素)和重复的像素。我真的很感激任何类型的帮助。谢谢。

1 个答案:

答案 0 :(得分:2)

您只是在 中重复写

fwrite(&runlength, sizeof(runlength), 1, out);
fwrite(&pixel,sizeof(PIXEL),1,out);

您需要写出当前像素:

fwrite(pixArr[i], sizeof(PIXEL),1,out);