📄 rle1.cpp
字号:
// Created:09-24-98
// By Jeff Connelly
// RLE type 1 encoding/decoding
// Based on ORIGSRC\CODRLE1.C and ORIGSRC\DCODRLE1.C written by
// David Bourgin
// Similar algorithms are used in MacPackBit, Targa, PCX, TIFF and more
#include "stdafx.h"
#define EXPORTING
#include "comprlib.h"
#include "rle1.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void write_array(void* array, int bytes)
{
register int i;
for (i = 0; i < bytes; ++i)
write_byte((char)((char*)array + i));
}
static void write_block(char byte, int time_nb)
{
unsigned char array_to_write[129];
memset(array_to_write, byte, time_nb);
write_array(array_to_write, time_nb);
}
void EXPORT rle1_encode()
{
register unsigned char byte1, byte2, frame_size, array[129];
if (!end_of_data())
{
byte1 = read_byte();
frame_size = 1;
if (!end_of_data())
{
byte2 = read_byte();
frame_size = 2;
do
{
if (byte1 == byte2)
{
while (!end_of_data() && (byte1 == byte2) &&
(frame_size < 129))
{
byte2 = read_byte();
++frame_size;
}
if (byte1 == byte2)
{
write_byte(126 + frame_size);
write_byte(byte1);
if (!end_of_data())
{
byte1 = read_byte();
frame_size = 1;
}
else
frame_size = 0;
} else {
write_byte(125 + frame_size);
write_byte(byte1);
byte1 = byte2;
frame_size = 1;
}
if (!end_of_data())
{
byte2 = read_byte();
frame_size = 2;
}
} else {
*array = byte1;
array[1] = byte2;
while (!end_of_data() && (array[frame_size - 2] !=
array[frame_size - 1]) && (frame_size < 128))
{
array[frame_size] = read_byte();
++frame_size;
}
if (array[frame_size - 2] == array[frame_size - 1])
{
write_byte(frame_size - 3);
write_array(array, frame_size - 2);
byte1 = array[frame_size - 2];
byte2 = byte1;
frame_size = 2;
} else {
write_byte(frame_size - 1);
write_array(array, frame_size);
if (end_of_data())
frame_size = 0;
else
{
byte1 = read_byte();
if (end_of_data())
frame_size = 1;
else
{
byte2 = read_byte();
frame_size = 2;
}
}
}
}
} while ((!end_of_data()) || (frame_size >= 2));
}
if (frame_size == 1)
{
write_byte(0);
write_byte(byte1);
}
}
}
void EXPORT rle1_decode ()
{
unsigned char header;
register unsigned char i;
while (!end_of_data())
{
header = read_byte();
switch(header & 128)
{
case 0: for (i = 0; i < header; i++)
write_byte(read_byte());
break;
case 128: write_block(read_byte(), (header & 127 + 2));
}
}
// Wow! That code was short!
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -