GIFTranslator: Replace magic constants
Introduce GIFPrivate.h to define these magic constants Also, make sure that we don't go over LZ_MAX_CODE bits
This commit is contained in:
@@ -26,6 +26,8 @@
|
|||||||
#include <TranslatorFormats.h>
|
#include <TranslatorFormats.h>
|
||||||
#include <InterfaceDefs.h>
|
#include <InterfaceDefs.h>
|
||||||
|
|
||||||
|
#include "GIFPrivate.h"
|
||||||
|
|
||||||
|
|
||||||
extern bool debug;
|
extern bool debug;
|
||||||
|
|
||||||
@@ -57,8 +59,8 @@ GIFLoad::GIFLoad(BPositionIO* input, BPositionIO* output)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (c != 0x3b) {
|
while (c != TERMINATOR_INTRODUCER) {
|
||||||
if (c == 0x2c) {
|
if (c == DESCRIPTOR_INTRODUCER) {
|
||||||
if ((!ReadGIFImageHeader()) || (!ReadGIFImageData())) {
|
if ((!ReadGIFImageHeader()) || (!ReadGIFImageData())) {
|
||||||
if (debug) {
|
if (debug) {
|
||||||
syslog(LOG_ERR, "GIFLoad::GIFLoad() - "
|
syslog(LOG_ERR, "GIFLoad::GIFLoad() - "
|
||||||
@@ -75,23 +77,23 @@ GIFLoad::GIFLoad(BPositionIO* input, BPositionIO* output)
|
|||||||
free(fScanLine);
|
free(fScanLine);
|
||||||
fScanLine = NULL;
|
fScanLine = NULL;
|
||||||
return;
|
return;
|
||||||
} else if (c == 0x21) {
|
} else if (c == EXTENSION_INTRODUCER) {
|
||||||
unsigned char d;
|
unsigned char d;
|
||||||
if (fInput->Read(&d, 1) < 1) {
|
if (fInput->Read(&d, 1) < 1) {
|
||||||
fatalerror = true;
|
fatalerror = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (d == 0xff) {
|
if (d == LOOP_BLOCK_LABEL) {
|
||||||
if (!ReadGIFLoopBlock()) {
|
if (!ReadGIFLoopBlock()) {
|
||||||
fatalerror = true;
|
fatalerror = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else if (d == 0xf9) {
|
} else if (d == GRAPHIC_CONTROL_LABEL) {
|
||||||
if (!ReadGIFControlBlock()) {
|
if (!ReadGIFControlBlock()) {
|
||||||
fatalerror = true;
|
fatalerror = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else if (d == 0xfe) {
|
} else if (d == COMMENT_EXTENSION_LABEL) {
|
||||||
if (!ReadGIFCommentBlock()) {
|
if (!ReadGIFCommentBlock()) {
|
||||||
fatalerror = true;
|
fatalerror = true;
|
||||||
return;
|
return;
|
||||||
@@ -102,7 +104,7 @@ GIFLoad::GIFLoad(BPositionIO* input, BPositionIO* output)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (c != 0x00) {
|
} else if (c != BLOCK_TERMINATOR) {
|
||||||
if (!ReadGIFUnknownBlock(c)) {
|
if (!ReadGIFUnknownBlock(c)) {
|
||||||
fatalerror = true;
|
fatalerror = true;
|
||||||
return;
|
return;
|
||||||
@@ -224,10 +226,10 @@ GIFLoad::ReadGIFCommentBlock()
|
|||||||
if (fInput->Read(comment_data, length) < length)
|
if (fInput->Read(comment_data, length) < length)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
comment_data[length] = 0x00;
|
comment_data[length] = BLOCK_TERMINATOR;
|
||||||
if (debug)
|
if (debug)
|
||||||
syslog(LOG_INFO, "%s", comment_data);
|
syslog(LOG_INFO, "%s", comment_data);
|
||||||
} while (length != 0x00);
|
} while (length != BLOCK_TERMINATOR);
|
||||||
|
|
||||||
if (debug)
|
if (debug)
|
||||||
syslog(LOG_INFO, "\n");
|
syslog(LOG_INFO, "\n");
|
||||||
@@ -248,7 +250,7 @@ GIFLoad::ReadGIFUnknownBlock(unsigned char c)
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
fInput->Seek(length, SEEK_CUR);
|
fInput->Seek(length, SEEK_CUR);
|
||||||
} while (length != 0x00);
|
} while (length != BLOCK_TERMINATOR);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -428,7 +430,7 @@ GIFLoad::ReadGIFImageData()
|
|||||||
fOldCodeLength = fEntrySize[fNewCode];
|
fOldCodeLength = fEntrySize[fNewCode];
|
||||||
fNextCode++;
|
fNextCode++;
|
||||||
|
|
||||||
if (fNextCode > fMaxCode && fBits != 12) {
|
if (fNextCode > fMaxCode && fBits < LZ_MAX_BITS) {
|
||||||
fBits++;
|
fBits++;
|
||||||
fMaxCode = (1 << fBits) - 1;
|
fMaxCode = (1 << fBits) - 1;
|
||||||
}
|
}
|
||||||
@@ -514,6 +516,7 @@ GIFLoad::InitFrame(int size)
|
|||||||
fEndCode = fClearCode + 1;
|
fEndCode = fClearCode + 1;
|
||||||
fNextCode = fClearCode + 2;
|
fNextCode = fClearCode + 2;
|
||||||
fMaxCode = (1 << fBits) - 1;
|
fMaxCode = (1 << fBits) - 1;
|
||||||
|
|
||||||
fPass = 0;
|
fPass = 0;
|
||||||
|
|
||||||
if (fInterlaced)
|
if (fInterlaced)
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#ifndef GIF_PRIVATE_H
|
||||||
|
#define GIF_PRIVATE_H
|
||||||
|
|
||||||
|
|
||||||
|
#define BLOCK_TERMINATOR 0x00
|
||||||
|
|
||||||
|
#define EXTENSION_INTRODUCER 0x21
|
||||||
|
#define DESCRIPTOR_INTRODUCER 0x2c
|
||||||
|
#define TERMINATOR_INTRODUCER 0x3b
|
||||||
|
|
||||||
|
#define GRAPHIC_CONTROL_LABEL 0xf9
|
||||||
|
#define COMMENT_EXTENSION_LABEL 0xfe
|
||||||
|
#define LOOP_BLOCK_LABEL 0xff
|
||||||
|
|
||||||
|
#define LZ_MAX_CODE 4095
|
||||||
|
#define LZ_MAX_BITS 12
|
||||||
|
|
||||||
|
|
||||||
|
#endif // GIF_PRIVATE_H
|
||||||
@@ -24,6 +24,8 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
|
|
||||||
|
#include "GIFPrivate.h"
|
||||||
|
|
||||||
|
|
||||||
const int gs_pass_starts_at[] = {0, 4, 2, 1, 0};
|
const int gs_pass_starts_at[] = {0, 4, 2, 1, 0};
|
||||||
const int gs_increment_pass_by[] = {8, 8, 4, 2, 0};
|
const int gs_increment_pass_by[] = {8, 8, 4, 2, 0};
|
||||||
@@ -173,7 +175,7 @@ GIFSave::GIFSave(BBitmap* bitmap, BPositionIO* output,
|
|||||||
delete hash;
|
delete hash;
|
||||||
|
|
||||||
// Terminating character
|
// Terminating character
|
||||||
char t = 0x3b;
|
char t = TERMINATOR_INTRODUCER;
|
||||||
output->Write(&t, 1);
|
output->Write(&t, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,7 +214,10 @@ GIFSave::WriteGIFHeader()
|
|||||||
void
|
void
|
||||||
GIFSave::WriteGIFControlBlock()
|
GIFSave::WriteGIFControlBlock()
|
||||||
{
|
{
|
||||||
unsigned char b[8] = { 0x21, 0xf9, 0x04, 0, 0, 0, 0, 0x00 };
|
unsigned char b[8] = {
|
||||||
|
EXTENSION_INTRODUCER, GRAPHIC_CONTROL_LABEL, 0x04, 0x00, 0x00, 0x00,
|
||||||
|
0x00, BLOCK_TERMINATOR
|
||||||
|
};
|
||||||
if (palette->UseTransparent()) {
|
if (palette->UseTransparent()) {
|
||||||
b[3] = b[3] | 1;
|
b[3] = b[3] | 1;
|
||||||
b[6] = palette->TransparentIndex();
|
b[6] = palette->TransparentIndex();
|
||||||
@@ -225,7 +230,7 @@ void
|
|||||||
GIFSave::WriteGIFImageHeader()
|
GIFSave::WriteGIFImageHeader()
|
||||||
{
|
{
|
||||||
unsigned char header[10];
|
unsigned char header[10];
|
||||||
header[0] = 0x2c;
|
header[0] = DESCRIPTOR_INTRODUCER;
|
||||||
header[1] = header[2] = 0;
|
header[1] = header[2] = 0;
|
||||||
header[3] = header[4] = 0;
|
header[3] = header[4] = 0;
|
||||||
|
|
||||||
@@ -237,7 +242,7 @@ GIFSave::WriteGIFImageHeader()
|
|||||||
if (fSettings->SetGetBool(GIF_SETTING_INTERLACED))
|
if (fSettings->SetGetBool(GIF_SETTING_INTERLACED))
|
||||||
header[9] = 0x40;
|
header[9] = 0x40;
|
||||||
else
|
else
|
||||||
header[9] = 0x00;
|
header[9] = BLOCK_TERMINATOR;
|
||||||
|
|
||||||
output->Write(header, 10);
|
output->Write(header, 10);
|
||||||
}
|
}
|
||||||
@@ -268,8 +273,8 @@ GIFSave::WriteGIFImageData()
|
|||||||
|
|
||||||
if (next_code > max_code) {
|
if (next_code > max_code) {
|
||||||
BITS++;
|
BITS++;
|
||||||
if (BITS > 12) {
|
if (BITS > LZ_MAX_BITS) {
|
||||||
OutputCode(clear_code, 12);
|
OutputCode(clear_code, LZ_MAX_BITS);
|
||||||
BITS = code_size + 1;
|
BITS = code_size + 1;
|
||||||
ResetHashtable();
|
ResetHashtable();
|
||||||
next_code = clear_code + 1;
|
next_code = clear_code + 1;
|
||||||
@@ -285,7 +290,7 @@ GIFSave::WriteGIFImageData()
|
|||||||
OutputCode(string_code, BITS);
|
OutputCode(string_code, BITS);
|
||||||
OutputCode(end_code, BITS);
|
OutputCode(end_code, BITS);
|
||||||
OutputCode(0, BITS, true);
|
OutputCode(0, BITS, true);
|
||||||
char t = 0x00;
|
char t = BLOCK_TERMINATOR;
|
||||||
output->Write(&t, 1);
|
output->Write(&t, 1);
|
||||||
free(code_value);
|
free(code_value);
|
||||||
free(prefix_code);
|
free(prefix_code);
|
||||||
@@ -531,7 +536,7 @@ GIFSave::InitFrame()
|
|||||||
max_code = (1 << BITS) - 1;
|
max_code = (1 << BITS) - 1;
|
||||||
string_code = 0;
|
string_code = 0;
|
||||||
character = 0;
|
character = 0;
|
||||||
table_size = 1 << 12;
|
table_size = 1 << LZ_MAX_BITS;
|
||||||
|
|
||||||
bit_count = 0;
|
bit_count = 0;
|
||||||
bit_buffer = 0;
|
bit_buffer = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user