问题描述
我是 C 编码的新手。 我想在以下条件下解码 RLE 编码的二进制文件(复古、稀有图片格式):
字符,后跟字符数
运行计数对计数 >=4 有效
0 被编码为 00 00
1 被编码为 00 01
10(十六进制?)是 LF
EOF 编码为 00 03
底部字节(EOF后为0A)
有一个 2 字节的计数:,48> ! 128,&255,例子
0 1 2 3 4 5 编码为 0 0 1 2 3 4 5 编码 0 为 0,0
5 6 7 8 9 10 编码为 5 6 7 8 9 0 1 编码 10 为 0,1
5...(200 次) 编码为 5 0 128 200 长重复计数 200
7...(515 次) 编码为 7 0 130 3 16_203 的长重复计数
可变头长度(512/1024 字节) 不同的按压,包括不同的 RLE 按压(水平、垂直) 可变颜色图(可变长度)和无, 立体图片,CAD图片,分形, geosat 图片等,但现在我只想解码无头数据。
我在这里和其他地方搜索了很多 RLE,并尝试了几天,但主要是关于字符串的线程,而没有关于字符 0 等选定条件的线程。尤其是检查 3 字节。
我有 2 种旧语言的文档和代码,包括汇编程序。我在这里找到了一个类似的 C++ 代码,但我无法正确翻译它。 我设法做了一些没有编译错误的c程序,但结果是错误的。
最好的代码如下。它读取多个字节但缺少一些“00”字节。字节 1+3 是正确的。字节 2 为 0,缺少字节 4。
输出到文件是完全错误的,只有 3 个字节。甚至第一个字节也是错误的。 (80)
我很困惑用 2 个变量读取 3 个字节。字节翻转?
有人可以帮忙吗?谢谢!
while (!feof(fp1))
{ //originally also have to check if 1st char=10 (LF) and overrides that
if(fread(buf,1,2,fp1)!=2) //Read two bytes: original code was number of characters + characters
// but I have characters + number of ch,and more conditions
continue; // was break;
ch = buf[0]; //was num ch instead of ch,also swapped below at buf 1
printf("%x\n",ch); //later added this to output to screen,reads more than output but bytes like 0 missing
if(ch==0) {
if(ch==1) {
ch=10; // code for LF
continue; // is this correct? continue or return (0) or -1? same below
}else{
if(ch==0) {
ch=0; // 00 00 encoded as 0
continue;
}else{
if(ch==3); //EOF,after that bottom byte 0A follows
return (0);
}
} //else beep? return -1,break,but I rely on proper image not having 00 04...
}
}
num = buf[1];
char * chbuf = (char *)malloc(num);
//main code from_WRITE IMAGE,although read
if(num==1) { // no action v----------v-----------v-------
} else if (num<4) { // run counts only when count =>4
for (int i =0;i < num;i++)
chbuf[i] = ch;
fwrite(&chbuf,num,fp2); //num ?,1? or 0 was fwrite(chbuf
}else if (num<=127); {
for (int i =0;i < num;i++)
chbuf[i] = num; ////newest added is this right?
if (num==10) {
fwrite(&chbuf,fp2); //num--; //num ?,or is it ch or char or i?
}else {
if ((ch&255)==10) { // was num&,I tried all,num and ch,always wrong
num++;
fwrite(&chbuf,ch,fp2); // was num--; at end
num++; // is num++ right? but neverless always just 3 bytes
fwrite(&chbuf,((unsigned) num>>8|0x80),fp2); // was chbuf 1,also checked unsigned char,80,else
num++;
fwrite(&chbuf,fp2); // was 1
}
}
}
free(chbuf);
新代码 3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//3rd code this time with internal string,code 2 worked (read 2 bytes from file,print to screen)
int i,j; //output should be 88 times 05,4 times 06
int main()
{
char string[] = "0500580604"; //hex test input to not have external file
//for Now no check for conditions like EOF,00 at begin
int number1 = atoi(string [0]); //convert string to byte was [1]
int number2 = atoi(string [1]);
int number3 = atoi(string [2]);
if (number2 == 0){ //read byte 2 and check if it's a count,Byte 1 is a char
if (number2 >=4){ //check byte 2 if valid count or char
}else {printf("%x\n",number1,number2); //byte 2 is a char
} return (0);
if (number3<127){ //check byte 3 if its a long run
for (int i =0;i < number2;i++) // print byte2 times the char
printf("%x\n",number1);
}else {
for (int i =0;i <number3;i++) //print byte3 times the char
printf("%x\n",number1);
return (0);
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)