我对算法ByteRun有一点问题(非常类似于RLE)。好吧,当我使解压缩smth出错并且我的输出与输入不同时。原始cookie以.bmp格式(24像素)保存。输出应该是我们的格式,例如。 “.xd”。
我看到(目前为止)只有4个选项,它取决于我们在宽度上有多少像素。
我们有421
对于422我们有
我们有423
我们有424
和原文:
Tbh我认为我考虑填充但是idk是什么让我的可怜的饼干破了。 :(请帮我保存饼干。:D
#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
struct colour // structure of every pixel BGR
{
unsigned char b;
unsigned char g;
unsigned char r;
};
unsigned int reading_and_writing_the_header(FILE *file_in, FILE *file_out, unsigned char header[])
{
unsigned int width = 0;
unsigned int height = 0;
unsigned int bite_for_pixel = 0;
unsigned int amount_of_pixels = 0;
unsigned int padding = 0;
unsigned int row = 0;
fread(header, 1, 54, file_in); // reading the header // 1=sizeof(unsigned int) = sizof(uni8_t)
fwrite(header, 1, 54, file_out); // wrting header
width = *(unsigned int*)&header[18];
height = *(unsigned int*)&header[22];
bite_for_pixel = *(unsigned int*)&header[28];
padding = width % 4; // counting padding
row = width * 3 + padding; // bites in a row
amount_of_pixels = height * row; // amount of bites after header
return amount_of_pixels;
}
void compression_ByteRun(FILE *file_in,FILE *file_out, unsigned int aop)
{
colour *q = new colour[aop];
fread(q, 1, aop, file_in);
int k = 0;
colour x, y;
int counter1 = 0;
int counter2 = 0;
char *the_same_colours = new char[4];
char *diffrent_colours = new char[128 * 3 + 1];
while (k < (aop / 3))
{
x = q[k];
y = q[k + 1];
if (x.r == y.r && x.b == y.b && x.g == y.g) //if the same R and B and G
{
if (counter1 == 126) // not sure about what exactly should be here
{
the_same_colours[0] = (counter1)*(-1);
fwrite(the_same_colours, 1, 4, file_out);
counter1 = 0;
}
if (counter2 != 0)
{
diffrent_colours[0] = counter2 / 3 - 1;
fwrite(diffrent_colours, 1, counter2 + 1, file_out);
counter2 = 0;
}
counter1++;
k++;
continue;
}
else // if we have diffrent colours side by side
{
if (counter1 != 0)
{
the_same_colours[0] = (counter1)*(-1);
the_same_colours[1] = x.b;
the_same_colours[2] = x.g;
the_same_colours[3] = x.r;
fwrite(the_same_colours, 1, 4, file_out);
counter1 = 0;
k++;
}
else
{
diffrent_colours[counter2 + 1] = x.b;
diffrent_colours[counter2 + 2] = x.g;
diffrent_colours[counter2 + 3] = x.r;
counter2 = counter2+ 3;
if (counter2 / 3 == 127) // same here, not sure about what exactly should be here
{
diffrent_colours[0] = counter2 / 3 - 1;
fwrite(diffrent_colours, 1, counter2 + 1, file_out);
counter2 = 0;
}
k++;
}
}
}
}
void decompression_ByteRun(FILE *file_in,FILE *file_out)
{
char x; // variavle where we keep how many colours we need 2 write
char y, z, o; // variables for comparing values
bool game_over; // variable for checking if the file isn't over
game_over = fread(&x, 1, 1, file_in);
while (game_over == true)
{
if (x >= 0) // if we had diffrent colours side by dide
{
for (int i = 1; i <= x * 3 + 3; i++)
{
fread(&y, 1, 1, file_in);
fwrite(&y, 1, 1, file_out);
}
game_over = fread(&x, 1, 1, file_in);
continue;
}
else // if we have the same colours
{
fread(&z, 1, 1, file_in);
fread(&o, 1, 1, file_in);
fread(&y, 1, 1, file_in);
for (int i = 1; i <= (-(x)+1); i++)
{
fwrite(&z, 1, 1, file_out);
fwrite(&o, 1, 1, file_out);
fwrite(&y, 1, 1, file_out);
}
game_over = fread(&x, 1, 1, file_in);
}
}
}
int main()
{
FILE *file_in;
FILE *file_out;
int number = 0;
char* name = new char;
char* name_out = new char;
unsigned char header[54];
unsigned int amount_of_pixels = 0;
cout << "Write input file: " << endl;
cin >> name;
cout << endl;
cout << "Write output file:" << endl;
cin >> name_out;
cout << endl;
cout << "What u wanna do (1- compression or 2- decompression ";
cin >> number;
file_in = fopen(name, "rb");
if (file_in == NULL)
{
cout << "Error we couldn't open that file.\n Contact our IT group 4 more help" << endl;
system("PAUSE");
exit(0);
}
file_out = fopen(name_out, "wb");
if (file_out == NULL)
{
cout << "Error we couldn't open that file.\n Contact our IT group 4 more help" << endl;
system("PAUSE");
exit(0);
}
switch (number)
{
case 1:
cout << "Compression Byterun" << endl;
amount_of_pixels = reading_and_writing_the_header(file_in, file_out, header);
compression_ByteRun(file_in, file_out, amount_of_pixels);
break;
case 2:
cout << "Decompression Byterun" << endl;
amount_of_pixels = reading_and_writing_the_header(file_in, file_out, header);
decompression_ByteRun(file_in, file_out);
break;
}
system("PAUSE");
return 0;
}