C ++ rle bmp压缩会跳过最后一个字节

时间:2016-12-12 08:59:35

标签: c++ bmp

我正在为bmp文件编写一个压缩文件。 (它是更大项目的一部分)编码功能采用仅包含像素(不带标题)的文件。一切都运行良好,除了它没有压缩几个最后的字节(取决于我猜的文件大小)。 De压缩效果也很好。 在此先感谢您的帮助!

以下是代码:

 #include <string>
#include <fstream>
#include <iostream>

using namespace std;

typedef struct SPixel {
    uint8_t red;    
    uint8_t green;
    uint8_t blue;
}PIXEL;

bool compare(SPixel pixel1, SPixel pixel2) {
    if (pixel1.red == pixel2.red && pixel1.green == pixel2.green && pixel1.blue == pixel2.blue)
        return true;
    else return false;
}

void encode(string &input, string &output){
    unsigned short repetition = 1;
    PIXEL current;
    PIXEL next;

    fstream file;
    fstream compressed;
    file.open(input, ios::in | ios::binary);
    if (!file.is_open()) {
        cout << "cannot open file to encode." << endl;
        getchar();
        exit(1);
    }

    compressed.open(output, ios::out | ios::trunc | ios::binary);
    if (!compressed.is_open()) {
        cout << "cannot open file to save encoded file." << endl;
        getchar();
        exit(1);
    }

    file.read((char*)&current, sizeof(PIXEL));
    file.read((char*)&next, sizeof(PIXEL));

    while (!file.eof())
        if (!compare(current, next)) {
            compressed.write((char*)&repetition, sizeof(repetition));
            compressed.write((char*)&current, sizeof(PIXEL));
            repetition = 0;
        }
        repetition++;
        current = next;
        file.read((char*)&next, sizeof(PIXEL));
    }

    file.close();
    compressed.close();
}

void decode(string &input, string &output) {
    fstream file;
    fstream ready;
    PIXEL current;
    unsigned short repetition = 0;

    file.open(input, ios::in | ios::binary);
    if (!file.is_open()) {
        cout << "cannot open file to decode." << endl;
        getchar();
        exit(1);
    }
    ready.open(output, ios::trunc | ios::out | ios::binary);
    if (!ready.is_open()) {
        cout << "cannot open file to save decoded file." << endl;
        getchar();
        exit(1);
    }

    while (!file.eof()){
        file.read((char*)&repetition, sizeof(repetition));
        file.read((char*)&current, sizeof(PIXEL));
        for (int j = 0; j < repetition; j++)
            ready.write((char*)&current, sizeof(PIXEL));
    }

    file.close();
    ready.close();
}

1 个答案:

答案 0 :(得分:0)

正如Some programmer dude已正确提及,您需要进行以下更正。

encode功能

中插入以下代码
while (!file.eof()) {
    ...
}

if (1 < repetition) {
    compressed.write((char*)&repetition, sizeof(repetition));
    compressed.write((char*)&current, sizeof(PIXEL));
}

file.close();
...