📄 builddec.c
字号:
/* $Id: builddec.c 1.2 90/06/09 18:25:13 marking Exp $
*
NAME
* builddec.c -- build decoding tables for group 3 and group 4 images
*
TYPE
* C source for main program and subroutines
*
SYNOPSIS
* builddec
*
DESCRIPTION
* This program builds tables for decoding group 3 and group 4 encoded
* images. The tables are built as the arrays null_mode [] [],
* null_mode_next_state [] [], horiz_mode [] [], and
* horiz_mode_next_state [] []. The output is C source code which must
* be compiled and linked into a decoding program.
*
RETURNS
* zero if no errors detected, nonzero otherwise
*
LEGAL
* Copyright 1989, 1990 Michael P. Marking, Post Office Box 8039,
* Scottsdale, Arizona 85252-8039. All rights reserved.
*
* License is granted by the copyright holder to distribute and use this
* code without payment of royalties or the necessity of notification as
* long as this notice (all the text under "LEGAL") is included.
*
* Reference: $Id: builddec.c 1.2 90/06/09 18:25:13 marking Exp $
*
* This program is offered without any warranty of any kind. It includes
* no warranty of merchantability or fitness for any purpose. Testing and
* suitability for any use are the sole responsibility of the user.
*
HISTORY
* $Log: builddec.c $ * Revision 1.2 90/06/09 18:25:13 marking * clean up comments for release * * Revision 1.1 89/06/30 17:00:00 marking * Initial revision *
*
NOTES
* 1. Since it is expected that this program will be run
* infrequently, there is no attempt at optimization.
* 2. The tables horiz_mode [] [] and horiz_mode_next_state [] []
* are used in both group 3 and group 4 decoding. The tables
* null_mode [] [] and null_mode_next_state [] [] are used only
* in decoding group 4.
* 3. On an XT, this can take around 15 minutes. The progress
* messages it displays let you know it's still alive, but
* otherwise can be ignored.
* 4. Most of the documentation for the tables themselves is in
* the decode routines g3tdecod.c and g4tdecod.c.
*
FILES
* Creates the file "tables.c", which is to be compiled and linked into
* the table-driven decoding routine.
*
PORTABILITY
* Tested under Microsoft C 5.1. Should be fairly portable.
*
SEE ALSO
* g3tdecod.c -- decode group 3 image using tables
* g4tdecod.c -- decode group 4 image using tables
* "Decoding Group 3 Images", C Users Journal, June 1990
*
INFORMATION
* Although there is no support offered with this program, the author will
* endeavor to correct errors. Updates will also be made available from
* time to time.
*
* Contact: Michael P. Marking, Post Office Box 8039, Scottsdale, Arizona
* 85252-8039 USA. Replies are not guaranteed to be swift. Beginning
* July 1990, e-mail may be sent to uunet!ipel!marking.
*
* Also beginning in July 1990, this code will be archived at the
* ipel!phoenix BBS in file g3g4.zoo. The 24-hour telephone number
* for 300/1200/2400 is (602)274-0462. When logging in, specify user
* "public", system "bbs", and password "public".
*
* This code is also available from the C Users Group in volume 317.
*
*/
/*
* standard headers
*/
#include <stddef.h> /* common types and macros */
#include <stdio.h> /* streams and files */
#include <stdlib.h> /* assorted functions, macros, types */
/*
* application
*/
#include "g3g4.h" /* some #defines */
#define INVALID_CODE -1
#define INCOMPLETE_CODE -2
#define EOL_CODE -3
unsigned long append_0 (unsigned long);
unsigned long append_1 (unsigned long);
short black_run_length (unsigned long);
short search_run_length_table (unsigned long, long *);
short white_run_length (unsigned long);
unsigned long append_0 (unsigned long prefix)
{
return (prefix + 0x10000);
}
unsigned long append_1 (unsigned long prefix)
{
unsigned short prefix_length;
static unsigned short prefix_mask [16] = {0x8000, 0x4000, 0x2000, 0x1000,
0x0800, 0x0400, 0x0200, 0x0100, 0x0080, 0x0040, 0x0020, 0x0010, 0x0008,
0x0004, 0x0002, 0x0001};
prefix_length = 0xFF & (unsigned short) (prefix >> 16);
return (prefix + 0x10000 + prefix_mask [prefix_length]);
}
short search_run_length_table (unsigned long prefix, long *p_table)
{
short table_offset = 0;
long prefix_length, prefix_value;
prefix_length = 0xFF & (prefix >> 16);
prefix_value = 0xFFFF & prefix;
while (p_table [table_offset])
{
if (p_table [table_offset] == prefix_length
&& p_table [table_offset + 1] == prefix_value)
return ((short) p_table [table_offset + 2]);
table_offset += 3; /* move on to next entry */
}
return (INCOMPLETE_CODE); /* no entry found in table */
}
short white_run_length (unsigned long prefix)
{
static long code_table [] =
{
8, 0x3500, 0, /* 0011 0101 */
6, 0x1C00, 1, /* 0001 11 */
4, 0x7000, 2, /* 0111 */
4, 0x8000, 3, /* 1000 */
4, 0xB000, 4, /* 1011 */
4, 0xC000, 5, /* 1100 */
4, 0xE000, 6, /* 1110 */
4, 0xF000, 7, /* 1111 */
5, 0x9800, 8, /* 1001 1 */
5, 0xA000, 9, /* 1010 0 */
5, 0x3800, 10, /* 0011 1 */
5, 0x4000, 11, /* 0100 0 */
6, 0x2000, 12, /* 0010 00 */
6, 0x0C00, 13, /* 0000 11 */
6, 0xD000, 14, /* 1101 00 */
6, 0xD400, 15, /* 1101 01 */
6, 0xA800, 16, /* 1010 10 */
6, 0xAC00, 17, /* 1010 11 */
7, 0x4E00, 18, /* 0100 111 */
7, 0x1800, 19, /* 0001 100 */
7, 0x1000, 20, /* 0001 000 */
7, 0x2E00, 21, /* 0010 111 */
7, 0x0600, 22, /* 0000 011 */
7, 0x0800, 23, /* 0000 100 */
7, 0x5000, 24, /* 0101 000 */
7, 0x5600, 25, /* 0101 011 */
7, 0x2600, 26, /* 0010 011 */
7, 0x4800, 27, /* 0100 100 */
7, 0x3000, 28, /* 0011 000 */
8, 0x0200, 29, /* 0000 0010 */
8, 0x0300, 30, /* 0000 0011 */
8, 0x1A00, 31, /* 0001 1010 */
8, 0x1B00, 32, /* 0001 1011 */
8, 0x1200, 33, /* 0001 0010 */
8, 0x1300, 34, /* 0001 0011 */
8, 0x1400, 35, /* 0001 0100 */
8, 0x1500, 36, /* 0001 0101 */
8, 0x1600, 37, /* 0001 0110 */
8, 0x1700, 38, /* 0001 0111 */
8, 0x2800, 39, /* 0010 1000 */
8, 0x2900, 40, /* 0010 1001 */
8, 0x2A00, 41, /* 0010 1010 */
8, 0x2B00, 42, /* 0010 1011 */
8, 0x2C00, 43, /* 0010 1100 */
8, 0x2D00, 44, /* 0010 1101 */
8, 0x0400, 45, /* 0000 0100 */
8, 0x0500, 46, /* 0000 0101 */
8, 0x0A00, 47, /* 0000 1010 */
8, 0x0B00, 48, /* 0000 1011 */
8, 0x5200, 49, /* 0101 0010 */
8, 0x5300, 50, /* 0101 0011 */
8, 0x5400, 51, /* 0101 0100 */
8, 0x5500, 52, /* 0101 0101 */
8, 0x2400, 53, /* 0010 0100 */
8, 0x2500, 54, /* 0010 0101 */
8, 0x5800, 55, /* 0101 1000 */
8, 0x5900, 56, /* 0101 1001 */
8, 0x5A00, 57, /* 0101 1010 */
8, 0x5B00, 58, /* 0101 1011 */
8, 0x4A00, 59, /* 0100 1010 */
8, 0x4B00, 60, /* 0100 1011 */
8, 0x3200, 61, /* 0011 0010 */
8, 0x3300, 62, /* 0011 0011 */
8, 0x3400, 63, /* 0011 0100 */
5, 0xD800, 64, /* 1101 1 */
5, 0x9000, 128, /* 1001 0 */
6, 0x5C00, 192, /* 0101 11 */
7, 0x6E00, 256, /* 0110 111 */
8, 0x3600, 320, /* 0011 0110 */
8, 0x3700, 384, /* 0011 0111 */
8, 0x6400, 448, /* 0110 0100 */
8, 0x6500, 512, /* 0110 0101 */
8, 0x6800, 576, /* 0110 1000 */
8, 0x6700, 640, /* 0110 0111 */
9, 0x6600, 704, /* 0110 0110 0 */
9, 0x6680, 768, /* 0110 0110 1 */
9, 0x6900, 832, /* 0110 1001 0 */
9, 0x6980, 896, /* 0110 1001 1 */
9, 0x6A00, 960, /* 0110 1010 0 */
9, 0x6A80, 1024, /* 0110 1010 1 */
9, 0x6B00, 1088, /* 0110 1011 0 */
9, 0x6B80, 1152, /* 0110 1011 1 */
9, 0x6C00, 1216, /* 0110 1100 0 */
9, 0x6C80, 1280, /* 0110 1100 1 */
9, 0x6D00, 1344, /* 0110 1101 0 */
9, 0x6D80, 1408, /* 0110 1101 1 */
9, 0x4C00, 1472, /* 0100 1100 0 */
9, 0x4C80, 1536, /* 0100 1100 1 */
9, 0x4D00, 1600, /* 0100 1101 0 */
6, 0x6000, 1664, /* 0110 00 */
9, 0x4D80, 1728, /* 0100 1101 1 */
11, 0x0100, 1792, /* 0000 0001 000 */
11, 0x0180, 1856, /* 0000 0001 100 */
11, 0x01A0, 1920, /* 0000 0001 101 */
12, 0x0120, 1984, /* 0000 0001 0010 */
12, 0x0130, 2048, /* 0000 0001 0011 */
12, 0x0140, 2112, /* 0000 0001 0100 */
12, 0x0150, 2176, /* 0000 0001 0101 */
12, 0x0160, 2240, /* 0000 0001 0110 */
12, 0x0170, 2304, /* 0000 0001 0111 */
12, 0x01C0, 2368, /* 0000 0001 1100 */
12, 0x01D0, 2432, /* 0000 0001 1101 */
12, 0x01E0, 2496, /* 0000 0001 1110 */
12, 0x01F0, 2560, /* 0000 0001 1111 */
12, 0x0010, EOL_CODE, /* 0000 0000 0001 */
9, 0x0080, INVALID_CODE, /* 0000 0000 1 */
10, 0x0040, INVALID_CODE, /* 0000 0000 01 */
11, 0x0020, INVALID_CODE, /* 0000 0000 001 */
12, 0x0000, INVALID_CODE, /* 0000 0000 0000 */
0 /* end-of-table */
};
return (search_run_length_table (prefix, code_table));
}
short black_run_length (unsigned long prefix)
{
static long code_table [] =
{
10, 0x0DC0, 0, /* 0000 1101 11 */
3, 0x4000, 1, /* 010 */
2, 0xC000, 2, /* 11 */
2, 0x8000, 3, /* 10 */
3, 0x6000, 4, /* 011 */
4, 0x3000, 5, /* 0011 */
4, 0x2000, 6, /* 0010 */
5, 0x1800, 7, /* 0001 1 */
6, 0x1400, 8, /* 0001 01 */
6, 0x1000, 9, /* 0001 00 */
7, 0x0800, 10, /* 0000 100 */
7, 0x0A00, 11, /* 0000 101 */
7, 0x0E00, 12, /* 0000 111 */
8, 0x0400, 13, /* 0000 0100 */
8, 0x0700, 14, /* 0000 0111 */
9, 0x0C00, 15, /* 0000 1100 0 */
10, 0x05C0, 16, /* 0000 0101 11 */
10, 0x0600, 17, /* 0000 0110 00 */
10, 0x0200, 18, /* 0000 0010 00 */
11, 0x0CE0, 19, /* 0000 1100 111 */
11, 0x0D00, 20, /* 0000 1101 000 */
11, 0x0D80, 21, /* 0000 1101 100 */
11, 0x06E0, 22, /* 0000 0110 111 */
11, 0x0500, 23, /* 0000 0101 000 */
11, 0x02E0, 24, /* 0000 0010 111 */
11, 0x0300, 25, /* 0000 0011 000 */
12, 0x0CA0, 26, /* 0000 1100 1010 */
12, 0x0CB0, 27, /* 0000 1100 1011 */
12, 0x0CC0, 28, /* 0000 1100 1100 */
12, 0x0CD0, 29, /* 0000 1100 1101 */
12, 0x0680, 30, /* 0000 0110 1000 */
12, 0x0690, 31, /* 0000 0110 1001 */
12, 0x06A0, 32, /* 0000 0110 1010 */
12, 0x06B0, 33, /* 0000 0110 1011 */
12, 0x0D20, 34, /* 0000 1101 0010 */
12, 0x0D30, 35, /* 0000 1101 0011 */
12, 0x0D40, 36, /* 0000 1101 0100 */
12, 0x0D50, 37, /* 0000 1101 0101 */
12, 0x0D60, 38, /* 0000 1101 0110 */
12, 0x0D70, 39, /* 0000 1101 0111 */
12, 0x06C0, 40, /* 0000 0110 1100 */
12, 0x06D0, 41, /* 0000 0110 1101 */
12, 0x0DA0, 42, /* 0000 1101 1010 */
12, 0x0DB0, 43, /* 0000 1101 1011 */
12, 0x0540, 44, /* 0000 0101 0100 */
12, 0x0550, 45, /* 0000 0101 0101 */
12, 0x0560, 46, /* 0000 0101 0110 */
12, 0x0570, 47, /* 0000 0101 0111 */
12, 0x0640, 48, /* 0000 0110 0100 */
12, 0x0650, 49, /* 0000 0110 0101 */
12, 0x0520, 50, /* 0000 0101 0010 */
12, 0x0530, 51, /* 0000 0101 0011 */
12, 0x0240, 52, /* 0000 0010 0100 */
12, 0x0370, 53, /* 0000 0011 0111 */
12, 0x0380, 54, /* 0000 0011 1000 */
12, 0x0270, 55, /* 0000 0010 0111 */
12, 0x0280, 56, /* 0000 0010 1000 */
12, 0x0580, 57, /* 0000 0101 1000 */
12, 0x0590, 58, /* 0000 0101 1001 */
12, 0x02B0, 59, /* 0000 0010 1011 */
12, 0x02C0, 60, /* 0000 0010 1100 */
12, 0x05A0, 61, /* 0000 0101 1010 */
12, 0x0660, 62, /* 0000 0110 0110 */
12, 0x0670, 63, /* 0000 0110 0111 */
10, 0x03C0, 64, /* 0000 0011 11 */
12, 0x0C80, 128, /* 0000 1100 1000 */
12, 0x0C90, 192, /* 0000 1100 1001 */
12, 0x05B0, 256, /* 0000 0101 1011 */
12, 0x0330, 320, /* 0000 0011 0011 */
12, 0x0340, 384, /* 0000 0011 0100 */
12, 0x0350, 448, /* 0000 0011 0101 */
13, 0x0360, 512, /* 0000 0011 0110 0 */
13, 0x0368, 576, /* 0000 0011 0110 1 */
13, 0x0250, 640, /* 0000 0010 0101 0 */
13, 0x0258, 704, /* 0000 0010 0101 1 */
13, 0x0260, 768, /* 0000 0010 0110 0 */
13, 0x0268, 832, /* 0000 0010 0110 1 */
13, 0x0390, 896, /* 0000 0011 1001 0 */
13, 0x0398, 960, /* 0000 0011 1001 1 */
13, 0x03A0, 1024, /* 0000 0011 1010 0 */
13, 0x03A8, 1088, /* 0000 0011 1010 1 */
13, 0x03B0, 1152, /* 0000 0011 1011 0 */
13, 0x03B8, 1216, /* 0000 0011 1011 1 */
13, 0x0290, 1280, /* 0000 0010 1001 0 */
13, 0x0298, 1344, /* 0000 0010 1001 1 */
13, 0x02A0, 1408, /* 0000 0010 1010 0 */
13, 0x02A8, 1472, /* 0000 0010 1010 1 */
13, 0x02D0, 1536, /* 0000 0010 1101 0 */
13, 0x02D8, 1600, /* 0000 0010 1101 1 */
13, 0x0320, 1664, /* 0000 0011 0010 0 */
13, 0x0328, 1728, /* 0000 0011 0010 1 */
11, 0x0100, 1792, /* 0000 0001 000 */
11, 0x0180, 1856, /* 0000 0001 100 */
11, 0x01A0, 1920, /* 0000 0001 101 */
12, 0x0120, 1984, /* 0000 0001 0010 */
12, 0x0130, 2048, /* 0000 0001 0011 */
12, 0x0140, 2112, /* 0000 0001 0100 */
12, 0x0150, 2176, /* 0000 0001 0101 */
12, 0x0160, 2240, /* 0000 0001 0110 */
12, 0x0170, 2304, /* 0000 0001 0111 */
12, 0x01C0, 2368, /* 0000 0001 1100 */
12, 0x01D0, 2432, /* 0000 0001 1101 */
12, 0x01E0, 2496, /* 0000 0001 1110 */
12, 0x01F0, 2560, /* 0000 0001 1111 */
12, 0x0010, EOL_CODE, /* 0000 0000 0001 */
9, 0x0080, INVALID_CODE, /* 0000 0000 1 */
10, 0x0040, INVALID_CODE, /* 0000 0000 01 */
11, 0x0020, INVALID_CODE, /* 0000 0000 001 */
12, 0x0000, INVALID_CODE, /* 0000 0000 0000 */
0 /* end-of-table */
};
return (search_run_length_table (prefix, code_table));
}
#define NULL_MODE_PREFIX_LIMIT 200 /* maximum number of null-mode prefixes */
#define HORIZ_MODE_PREFIX_LIMIT 250 /* maximum number of indigestible
1-dimensional prefixes */
long null_mode_prefix [NULL_MODE_PREFIX_LIMIT];
/* the bit string corresponding to this row of the decoding table */
unsigned char null_mode [NULL_MODE_PREFIX_LIMIT] [256];
/* one of the entries PASS_MODE, HORIZONTAL_MODE, etc, or zero */
unsigned char null_mode_next_state [NULL_MODE_PREFIX_LIMIT] [256];
/* next row of the decoding tables to be used */
short null_mode_prefix_count = 0;
/* number of prefixes or rows in the G4 decoding tables */
/* decoding script values for horiz_mode [] []:
0 - indigestible code
1 - invalid code or error
2..105 - white runs
106..209 - black runs
210 - EOL (valid only for Group 3)
211..255 - (undefined) */
unsigned char horiz_mode [HORIZ_MODE_PREFIX_LIMIT] [256];
unsigned char horiz_mode_next_state [HORIZ_MODE_PREFIX_LIMIT] [256];
/* if the corresponding horiz_mode [] [] entry is zero ("indigestible"),
this entry is a row number for decoding the next byte; otherwise, it
is the bit number to continue coding the next codeword */
long horiz_mode_prefix [HORIZ_MODE_PREFIX_LIMIT];
/* the prefixes corresponding to the rows of the decoding table */
char horiz_mode_color [HORIZ_MODE_PREFIX_LIMIT];
/* color of next run, BLACK or WHITE */
short horiz_mode_prefix_count = 0;
static unsigned char bit_mask [8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04,
0x02, 0x01};
void build_null_mode_tables (void);
short find_horiz_mode_prefix (long, char);
short find_null_mode_prefix (long);
short null_mode_type (long);
void process_horiz_mode_prefixes (void);
short horiz_mode_code_black (short);
short horiz_mode_code_invalid (void);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -