⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fconvert.cpp

📁 串行通信编程源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
            return(1);                      // ..just return

        case -1:                            // nothing found
            break;                          // ..continue looping

        default:                            // found codeword
            return(0);                      // ..return ok
        }
    }
}



/* ******************************************************************** *
 *
 *  f_get_pels() -- get the next group of PELS from the G3 stream
 *
 *  returns: 0 = converted next word ok
 *           1 = error or end of line
 *
 * ******************************************************************** */

int     f_get_pels(int type,                // type of code
                                            //  0 = white space
                                            //  1 = black space
                   int *len)                // length in bits
{
int     a = 0;                              // accumulator


while (1)
    {
    if (f_get_code(type, len))              // q. get a code word?
        return(1);                          // a. no .. must be out of data

    if (*len > 63)                          // q. get a makeup code?
        a = *len;                           // a. yes .. save for later
     else
        {
        *len += a;                          // else .. add in any makeups
        return(0);                          // ..and return all ok
        }
    }
}



/* ******************************************************************** *
 *
 *  f_get_eol() -- read to end of line in current page
 *
 * ******************************************************************** */

void    f_get_eol(void)
{
int     a,                                  // accumulator
        b;                                  // work bit


for (a = 0xfff; a != 1;)                    // try to find an EOL bit
    {                                       // ..pattern of 000000000001
    if ((b = f_get_bit()) == -1)            // q. get a bit successfully?
        return;                             // a. no .. must be out of data

    a = ((a << 1) | b) & 0xfff;             // move in new bit
    }

}



/* ******************************************************************** *
 *
 *  f_read_g3() -- read a page and convert from G3 format
 *
 *  returns: 0 = page converted sucessfully
 *           1 = page not found or error
 *
 * ******************************************************************** */

int     f_read_g3(int n)                    // page number
{
int     pels,                               // pixels counter
        lines,                              // lines counter
        type,                               // type of codeword
        codes,                              // codewords processed
        eol,                                // consecutive EOLs found
        l = 0;                              // work length
char    huge *p;                            // bitmap line pointer


lseek(f_handle, HDR_LEN, SEEK_SET);         // get to start of 1st page

while(1)                                    // loop to find requested page
    {
    if (read(f_handle, (char *) &f_page,    // q. read in enough to cover
            sizeof(long)) != sizeof(long))  // ..the page length field?
        return(1);                          // a. no .. return not found

    if (--n == 0)                           // q. find target page?
        break;                              // a. yes .. exit loop

    if (lseek(f_handle, f_page,             // q. properly position to the
            SEEK_CUR) == -1L)               // ..start of the next page?
        return(1);                          // a. no .. return not found
    }

f_ptr = 0;                                  // set global pointer
f_get_eol();                                // find end of first line

for (lines = 0, p = page;                   // for the whole bitmap
            lines < LINES;                  // ..run through line
            lines++, p += LINE)             // ..by line
    memset(p, 0, LINE);                     // ..and clear white space

for (lines = 0, p = page, eol = 0;          // fill in each line in the
            lines < LINES;                  // ..bitmap page by processing
            lines++, p += LINE)             // ..one line at a time
    {
    for (pels = 0, type = 0, codes = 0;     // for each bit available
                pels < PELS;                // ..in the bitmap line get
                pels += l, codes++)         // ..codewords and convert
        {
        if (f_get_pels(type, &l))           // q. end of line found?
            break;                          // a. yes .. exit loop

        if (l > (PELS - pels))              // q. too much data?
            l = PELS - pels;                // a. yes .. set to max

        if (type)                           // q. black bits?
            set_bits((char *) p, pels, l);  // a. yes .. set bits on

        type = (type + 1) & 1;              // switch back and forth
        }                                   // ..between black and white

    f_get_eol();                            // find the end of this line

    if (NOT codes)                          // q. empty line?
        {
        eol++;                              // a. yes .. tally empty lines

        if (eol == 6)                       // q. find the end of page?
            break;                          // a. yes .. finish looping
        }

     else
        eol = 0;                            // else .. clear EOL counter
    }

return(0);                                  // finally, return ok

}



/* ******************************************************************** *
 *
 *  f_write_hdr() -- write a fax file header
 *
 * ******************************************************************** */

void    f_write_hdr(void)
{
char    buf[HDR_LEN];                       // header buffer


memset(buf, 0, HDR_LEN);                    // clear buffer to nulls
strcpy(buf, "G3");                          // ..and put in our marker
write(f_handle, buf, HDR_LEN);              // write header

}



/* ******************************************************************** *
 *
 *  f_write_out() -- write bits to output fax file
 *
 * ******************************************************************** */

void    f_write_out(int bits,               // output bits
                    int len)                // ..and number of bits
{
static  unsigned
char    w;                                  // byte wide queue


if (len > 16)                               // q. too many bits?
    return;                                 // a. yes .. just return

bits <<= 16 - len;                          // shift to MSB end of word

while (len--)                               // while there is input
    {
    w >>= 1;                                // shift queue over by one

    if (bits & 0x8000)                      // q. source bit on?
        w |= 0x80;                          // a. yes .. turn on dest

    if (++f_write_flag == 8)                // q. reach limit?
        {
        write(f_handle, (void *) &w, 1);    // a. yes .. write a byte

        if (w == DLE)                       // q. special character?
            write(f_handle, (void *) &w, 1);// a. yes .. write it again

        f_write_flag = 0;                   // clear the flag
        f_write_cnt++;                      // ..and tally the byte
        }

    bits <<= 1;                             // shift source by one bit
    }
}



/* ******************************************************************** *
 *
 *  f_encode() -- encode a string of bits into G3 format
 *
 * ******************************************************************** */

void    f_encode(int cnt,                   // number of bits from bitmap
                 int type)                  // ..and bit color
{
struct  encode_entry *ee;                   // encode entry


if (cnt > 63)                                   // q. big run of bits?
    {
    ee = &encode_table[(cnt / 64) + 63][type];  // a. yes .. get pointer
    f_write_out(ee->code, ee->bits);            // ..write make-up code
    cnt %= 64;                                  // ..and update bit count
    }

ee = &encode_table[cnt][type];              // get element pointer
f_write_out(ee->code, ee->bits);            // write terminating code

}



/* ******************************************************************** *
 *
 *  f_scan_out() -- scan the bitmap and output a codeword
 *
 * ******************************************************************** */

int     f_scan_out(char huge *p,            // bitmap line
                   int  i,                  // starting bit offset
                   int  type)               // type to search
{
int     cnt;                                // work counter


for (cnt = 0; i < PELS; i++, cnt++)         // scan bitmap line
    if (get_bit(                            // q. find a bit which is not
            (unsigned char *) p, i) != type)// ..the same as type?
        break;                              // a. yes .. exit loop

f_encode(cnt, type);                        // build/output codeword
return(cnt);                                // ..then return with count

}



/* ******************************************************************** *
 *
 *  f_write_page() -- write a fax file page
 *
 * ******************************************************************** */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -