stream.cc
来自「source code: Covert TXT to PDF」· CC 代码 · 共 2,936 行 · 第 1/5 页
CC
2,936 行
GString *RunLengthStream::getPSFilter(char *indent) { GString *s; if (!(s = str->getPSFilter(indent))) { return NULL; } s->append(indent)->append("/RunLengthDecode filter\n"); return s;}GBool RunLengthStream::isBinary(GBool last) { return str->isBinary(gTrue);}GBool RunLengthStream::fillBuf() { int c; int n, i; if (eof) return gFalse; c = str->getChar(); if (c == 0x80 || c == EOF) { eof = gTrue; return gFalse; } if (c < 0x80) { n = c + 1; for (i = 0; i < n; ++i) buf[i] = (char)str->getChar(); } else { n = 0x101 - c; c = str->getChar(); for (i = 0; i < n; ++i) buf[i] = (char)c; } bufPtr = buf; bufEnd = buf + n; return gTrue;}//------------------------------------------------------------------------// CCITTFaxStream//------------------------------------------------------------------------CCITTFaxStream::CCITTFaxStream(Stream *strA, int encodingA, GBool endOfLineA, GBool byteAlignA, int columnsA, int rowsA, GBool endOfBlockA, GBool blackA): FilterStream(strA) { encoding = encodingA; endOfLine = endOfLineA; byteAlign = byteAlignA; columns = columnsA; rows = rowsA; endOfBlock = endOfBlockA; black = blackA; refLine = (short *)gmalloc((columns + 3) * sizeof(short)); codingLine = (short *)gmalloc((columns + 2) * sizeof(short)); eof = gFalse; row = 0; nextLine2D = encoding < 0; inputBits = 0; codingLine[0] = 0; codingLine[1] = refLine[2] = columns; a0 = 1; buf = EOF;}CCITTFaxStream::~CCITTFaxStream() { delete str; gfree(refLine); gfree(codingLine);}void CCITTFaxStream::reset() { int n; str->reset(); eof = gFalse; row = 0; nextLine2D = encoding < 0; inputBits = 0; codingLine[0] = 0; codingLine[1] = refLine[2] = columns; a0 = 1; buf = EOF; // get initial end-of-line marker and 2D encoding tag if (endOfBlock) { if (lookBits(12) == 0x001) { eatBits(12); } } else { for (n = 0; n < 11 && lookBits(n) == 0; ++n) ; if (n == 11 && lookBits(12) == 0x001) { eatBits(12); } } if (encoding > 0) { nextLine2D = !lookBits(1); eatBits(1); }}int CCITTFaxStream::lookChar() { short code1, code2, code3; int a0New;#if 0 GBool err;#endif GBool gotEOL; int ret; int bits, i; // if at eof just return EOF if (eof && codingLine[a0] >= columns) { return EOF; } // read the next row#if 0 err = gFalse;#endif if (codingLine[a0] >= columns) { // 2-D encoding if (nextLine2D) { for (i = 0; codingLine[i] < columns; ++i) refLine[i] = codingLine[i]; refLine[i] = refLine[i + 1] = columns; b1 = 1; a0New = codingLine[a0 = 0] = 0; do { code1 = getTwoDimCode(); switch (code1) { case twoDimPass: if (refLine[b1] < columns) { a0New = refLine[b1 + 1]; b1 += 2; } break; case twoDimHoriz: if ((a0 & 1) == 0) { code1 = code2 = 0; do { code1 += code3 = getWhiteCode(); } while (code3 >= 64); do { code2 += code3 = getBlackCode(); } while (code3 >= 64); } else { code1 = code2 = 0; do { code1 += code3 = getBlackCode(); } while (code3 >= 64); do { code2 += code3 = getWhiteCode(); } while (code3 >= 64); } codingLine[a0 + 1] = a0New + code1; ++a0; a0New = codingLine[a0 + 1] = codingLine[a0] + code2; ++a0; while (refLine[b1] <= codingLine[a0] && refLine[b1] < columns) b1 += 2; break; case twoDimVert0: a0New = codingLine[++a0] = refLine[b1]; if (refLine[b1] < columns) { ++b1; while (refLine[b1] <= codingLine[a0] && refLine[b1] < columns) b1 += 2; } break; case twoDimVertR1: a0New = codingLine[++a0] = refLine[b1] + 1; if (refLine[b1] < columns) { ++b1; while (refLine[b1] <= codingLine[a0] && refLine[b1] < columns) b1 += 2; } break; case twoDimVertL1: a0New = codingLine[++a0] = refLine[b1] - 1; --b1; while (refLine[b1] <= codingLine[a0] && refLine[b1] < columns) b1 += 2; break; case twoDimVertR2: a0New = codingLine[++a0] = refLine[b1] + 2; if (refLine[b1] < columns) { ++b1; while (refLine[b1] <= codingLine[a0] && refLine[b1] < columns) b1 += 2; } break; case twoDimVertL2: a0New = codingLine[++a0] = refLine[b1] - 2; --b1; while (refLine[b1] <= codingLine[a0] && refLine[b1] < columns) b1 += 2; break; case twoDimVertR3: a0New = codingLine[++a0] = refLine[b1] + 3; if (refLine[b1] < columns) { ++b1; while (refLine[b1] <= codingLine[a0] && refLine[b1] < columns) b1 += 2; } break; case twoDimVertL3: a0New = codingLine[++a0] = refLine[b1] - 3; --b1; while (refLine[b1] <= codingLine[a0] && refLine[b1] < columns) b1 += 2; break; case EOF: eof = gTrue; codingLine[a0 = 0] = columns; return EOF; default: error(getPos(), "Bad 2D code %04x in CCITTFax stream", code1);#if 0 err = gTrue; break;#else eof = gTrue; return EOF;#endif } } while (codingLine[a0] < columns); // 1-D encoding } else { codingLine[a0 = 0] = 0; while (1) { code1 = 0; do { code1 += code3 = getWhiteCode(); } while (code3 >= 64); codingLine[a0+1] = codingLine[a0] + code1; ++a0; if (codingLine[a0] >= columns) break; code2 = 0; do { code2 += code3 = getBlackCode(); } while (code3 >= 64); codingLine[a0+1] = codingLine[a0] + code2; ++a0; if (codingLine[a0] >= columns) break; } } if (codingLine[a0] != columns) { error(getPos(), "CCITTFax row is wrong length (%d)", codingLine[a0]);#if 0 err = gTrue;#endif } // byte-align the row if (byteAlign) { inputBits &= ~7; } // check for end-of-line marker, skipping over any extra zero bits gotEOL = gFalse; if (!endOfBlock && row == rows - 1) { eof = gTrue; } else { code1 = lookBits(12); while (code1 == 0) { eatBits(1); code1 = lookBits(12); } if (code1 == 0x001) { eatBits(12); gotEOL = gTrue; } else if (code1 == EOF) { eof = gTrue; } } // get 2D encoding tag if (!eof && encoding > 0) { nextLine2D = !lookBits(1); eatBits(1); } // check for end-of-block marker if (endOfBlock && gotEOL) { code1 = lookBits(12); if (code1 == 0x001) { eatBits(12); if (encoding > 0) { lookBits(1); eatBits(1); } if (encoding >= 0) { for (i = 0; i < 4; ++i) { code1 = lookBits(12); if (code1 != 0x001) { error(getPos(), "Bad RTC code in CCITTFax stream"); } eatBits(12); if (encoding > 0) { lookBits(1); eatBits(1); } } } eof = gTrue; } }#if 0 // This looks for an end-of-line marker after an error, however // some (most?) CCITT streams in PDF files don't use end-of-line // markers, and the just-plow-on technique works better in those // cases. else if (err) { do { if (code1 == EOF) { eof = gTrue; return EOF; } eatBits(1); code1 = look13Bits(); } while ((code1 >> 1) != 0x001); eatBits(12); codingLine[++a0] = columns; if (encoding > 0) { eatBits(1); nextLine2D = !(code1 & 1); } }#endif a0 = 0; outputBits = codingLine[1] - codingLine[0]; if (outputBits == 0) { a0 = 1; outputBits = codingLine[2] - codingLine[1]; } ++row; } // get a byte if (outputBits >= 8) { ret = ((a0 & 1) == 0) ? 0xff : 0x00; if ((outputBits -= 8) == 0) { ++a0; if (codingLine[a0] < columns) { outputBits = codingLine[a0 + 1] - codingLine[a0]; } } } else { bits = 8; ret = 0; do { if (outputBits > bits) { i = bits; bits = 0; if ((a0 & 1) == 0) { ret |= 0xff >> (8 - i); } outputBits -= i; } else { i = outputBits; bits -= outputBits; if ((a0 & 1) == 0) { ret |= (0xff >> (8 - i)) << bits; } outputBits = 0; ++a0; if (codingLine[a0] < columns) { outputBits = codingLine[a0 + 1] - codingLine[a0]; } } } while (bits > 0 && codingLine[a0] < columns); } buf = black ? (ret ^ 0xff) : ret; return buf;}short CCITTFaxStream::getTwoDimCode() { short code; CCITTCode *p; int n; code = 0; // make gcc happy if (endOfBlock) { code = lookBits(7); p = &twoDimTab1[code]; if (p->bits > 0) { eatBits(p->bits); return p->n; } } else { for (n = 1; n <= 7; ++n) { code = lookBits(n); if (n < 7) { code <<= 7 - n; } p = &twoDimTab1[code]; if (p->bits == n) { eatBits(n); return p->n; } } } error(getPos(), "Bad two dim code (%04x) in CCITTFax stream", code); return EOF;}short CCITTFaxStream::getWhiteCode() { short code; CCITTCode *p; int n; code = 0; // make gcc happy if (endOfBlock) { code = lookBits(12); if ((code >> 5) == 0) { p = &whiteTab1[code]; } else { p = &whiteTab2[code >> 3]; } if (p->bits > 0) { eatBits(p->bits); return p->n; } } else { for (n = 1; n <= 9; ++n) { code = lookBits(n); if (n < 9) { code <<= 9 - n; } p = &whiteTab2[code]; if (p->bits == n) { eatBits(n); return p->n; } } for (n = 11; n <= 12; ++n) { code = lookBits(n); if (n < 12) { code <<= 12 - n; } p = &whiteTab1[code]; if (p->bits == n) { eatBits(n); return p->n; } } } error(getPos(), "Bad white code (%04x) in CCITTFax stream", code); // eat a bit and return a positive number so that the caller doesn't // go into an infinite loop eatBits(1); return 1;}short CCITTFaxStream::getBlackCode() { short code; CCITTCode *p; int n; code = 0; // make gcc happy if (endOfBlock) { code = lookBits(13); if ((code >> 7) == 0) { p = &blackTab1[code]; } else if ((code >> 9) == 0) { p = &blackTab2[(code >> 1) - 64]; } else { p = &blackTab3[code >> 7]; } if (p->bits > 0) { eatBits(p->bits); return p->n; } } else { for (n = 2; n <= 6; ++n) { code = lookBits(n); if (n < 6) { code <<= 6 - n; } p = &blackTab3[code]; if (p->bits == n) { eatBits(n); return p->n; } } for (n = 7; n <= 12; ++n) { code = lookBits(n); if (n < 12) { code <<= 12 - n; } if (code >= 64) { p = &blackTab2[code - 64]; if (p->bits == n) { eatBits(n); return p->n; } } } for (n = 10; n <= 13; ++n) { code = lookBits(n); if (n < 13) { code <<= 13 - n; } p = &blackTab1[code]; if (p->bits == n) { eatBits(n); return p->n; } } } error(getPos(), "Bad black code (%04x) in CCITTFax stream", code); // eat a bit and return a positive number so that the caller doesn't // go into an infinite loop eatBits(1); return 1;}short CCITTFaxStream::lookBits(int n) { int c; while (inputBits < n) { if ((c = str->getChar()) == EOF) { if (inputBits == 0) { return EOF; } // near the end of the stream, the caller may ask for more bits // than are available, but there may still be a valid code in // however many bits are available -- we need to return correct // data in this case return (inputBuf << (n - inputBits)) & (0xffff >> (16 - n)); } inputBuf = (inputBuf << 8) + c; inputBits += 8; } return (inputBuf >> (inputBits - n)) & (0xffff >> (16 - n));}GString *CCITTFaxStream::getPSFilter(char *indent) { GString *s; char s1[50]; if (!(s = str->getPSFilter(indent))) { return NULL; } s->append(indent)->append("<< "); if (encoding != 0) { sprintf(s1, "/K %d ", encoding); s->append(s1); } if (endOfLine) { s->append("/EndOfLine true "); } if (byteAlign) { s->append("/EncodedByteAlign true "); } sprintf(s1, "/Columns %d ", columns); s->append(s1); if (rows != 0) { sprintf(s1, "/Rows %d ", rows); s->append(s1); } if (!endOfBlock) { s->append("/EndOfBlock false "); } if (black) { s->append("/BlackIs1 true "); } s->append(">> /CCITTFaxDecode filter\n"); return s;}GBool CCITTFaxStream::isBinary(GBool last) { return str->isBinary(gTrue);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?