jbig2stream.cc

来自「source code: Covert TXT to PDF」· CC 代码 · 共 2,563 行 · 第 1/5 页

CC
2,563
字号
      readTextRegionSeg(segNum, gFalse, gFalse, segLength, refSegs, nRefSegs);      break;    case 6:      readTextRegionSeg(segNum, gTrue, gFalse, segLength, refSegs, nRefSegs);      break;    case 7:      readTextRegionSeg(segNum, gTrue, gTrue, segLength, refSegs, nRefSegs);      break;    case 16:      readPatternDictSeg(segNum, segLength);      break;    case 20:      readHalftoneRegionSeg(segNum, gFalse, gFalse, segLength,			    refSegs, nRefSegs);      break;    case 22:      readHalftoneRegionSeg(segNum, gTrue, gFalse, segLength,			    refSegs, nRefSegs);      break;    case 23:      readHalftoneRegionSeg(segNum, gTrue, gTrue, segLength,			    refSegs, nRefSegs);      break;    case 36:      readGenericRegionSeg(segNum, gFalse, gFalse, segLength);      break;    case 38:      readGenericRegionSeg(segNum, gTrue, gFalse, segLength);      break;    case 39:      readGenericRegionSeg(segNum, gTrue, gTrue, segLength);      break;    case 40:      readGenericRefinementRegionSeg(segNum, gFalse, gFalse, segLength,				     refSegs, nRefSegs);      break;    case 42:      readGenericRefinementRegionSeg(segNum, gTrue, gFalse, segLength,				     refSegs, nRefSegs);      break;    case 43:      readGenericRefinementRegionSeg(segNum, gTrue, gTrue, segLength,				     refSegs, nRefSegs);      break;    case 48:      readPageInfoSeg(segLength);      break;    case 50:      readEndOfStripeSeg(segLength);      break;    case 52:      readProfilesSeg(segLength);      break;    case 53:      readCodeTableSeg(segNum, segLength);      break;    case 62:      readExtensionSeg(segLength);      break;    default:      error(getPos(), "Unknown segment type in JBIG2 stream");      for (i = 0; i < segLength; ++i) {	if ((c1 = curStr->getChar()) == EOF) {	  goto eofError2;	}      }      break;    }    gfree(refSegs);  }  return; eofError2:  gfree(refSegs); eofError1:  error(getPos(), "Unexpected EOF in JBIG2 stream");}void JBIG2Stream::readSymbolDictSeg(Guint segNum, Guint length,				    Guint *refSegs, Guint nRefSegs) {  JBIG2SymbolDict *symbolDict;  JBIG2HuffmanTable *huffDHTable, *huffDWTable;  JBIG2HuffmanTable *huffBMSizeTable, *huffAggInstTable;  JBIG2Segment *seg;  GList *codeTables;  JBIG2SymbolDict *inputSymbolDict;  Guint flags, sdTemplate, sdrTemplate, huff, refAgg;  Guint huffDH, huffDW, huffBMSize, huffAggInst;  Guint contextUsed, contextRetained;  int sdATX[4], sdATY[4], sdrATX[2], sdrATY[2];  Guint numExSyms, numNewSyms, numInputSyms, symCodeLen;  JBIG2Bitmap **bitmaps;  JBIG2Bitmap *collBitmap, *refBitmap;  Guint *symWidths;  Guint symHeight, symWidth, totalWidth, x, symID;  int dh, dw, refAggNum, refDX, refDY, bmSize;  GBool ex;  int run, cnt;  Guint i, j, k;  Guchar *p;  // symbol dictionary flags  if (!readUWord(&flags)) {    goto eofError;  }  sdTemplate = (flags >> 10) & 3;  sdrTemplate = (flags >> 12) & 1;  huff = flags & 1;  refAgg = (flags >> 1) & 1;  huffDH = (flags >> 2) & 3;  huffDW = (flags >> 4) & 3;  huffBMSize = (flags >> 6) & 1;  huffAggInst = (flags >> 7) & 1;  contextUsed = (flags >> 8) & 1;  contextRetained = (flags >> 9) & 1;  // symbol dictionary AT flags  if (!huff) {    if (sdTemplate == 0) {      if (!readByte(&sdATX[0]) ||	  !readByte(&sdATY[0]) ||	  !readByte(&sdATX[1]) ||	  !readByte(&sdATY[1]) ||	  !readByte(&sdATX[2]) ||	  !readByte(&sdATY[2]) ||	  !readByte(&sdATX[3]) ||	  !readByte(&sdATY[3])) {	goto eofError;      }    } else {      if (!readByte(&sdATX[0]) ||	  !readByte(&sdATY[0])) {	goto eofError;      }    }  }  // symbol dictionary refinement AT flags  if (refAgg && !sdrTemplate) {    if (!readByte(&sdrATX[0]) ||	!readByte(&sdrATY[0]) ||	!readByte(&sdrATX[1]) ||	!readByte(&sdrATY[1])) {      goto eofError;    }  }  // SDNUMEXSYMS and SDNUMNEWSYMS  if (!readULong(&numExSyms) || !readULong(&numNewSyms)) {    goto eofError;  }  // get referenced segments: input symbol dictionaries and code tables  codeTables = new GList();  numInputSyms = 0;  for (i = 0; i < nRefSegs; ++i) {    seg = findSegment(refSegs[i]);    if (seg->getType() == jbig2SegSymbolDict) {      numInputSyms += ((JBIG2SymbolDict *)seg)->getSize();    } else if (seg->getType() == jbig2SegCodeTable) {      codeTables->append(seg);    }  }  // compute symbol code length  symCodeLen = 0;  i = 1;  while (i < numInputSyms + numNewSyms) {    ++symCodeLen;    i <<= 1;  }  // get the input symbol bitmaps  bitmaps = (JBIG2Bitmap **)gmalloc((numInputSyms + numNewSyms) *				    sizeof(JBIG2Bitmap *));  k = 0;  inputSymbolDict = NULL;  for (i = 0; i < nRefSegs; ++i) {    seg = findSegment(refSegs[i]);    if (seg->getType() == jbig2SegSymbolDict) {      inputSymbolDict = (JBIG2SymbolDict *)seg;      for (j = 0; j < inputSymbolDict->getSize(); ++j) {	bitmaps[k++] = inputSymbolDict->getBitmap(j);      }    }  }  // get the Huffman tables  huffDHTable = huffDWTable = NULL; // make gcc happy  huffBMSizeTable = huffAggInstTable = NULL; // make gcc happy  i = 0;  if (huff) {    if (huffDH == 0) {      huffDHTable = huffTableD;    } else if (huffDH == 1) {      huffDHTable = huffTableE;    } else {      huffDHTable = ((JBIG2CodeTable *)codeTables->get(i++))->getHuffTable();    }    if (huffDW == 0) {      huffDWTable = huffTableB;    } else if (huffDW == 1) {      huffDWTable = huffTableC;    } else {      huffDWTable = ((JBIG2CodeTable *)codeTables->get(i++))->getHuffTable();    }    if (huffBMSize == 0) {      huffBMSizeTable = huffTableA;    } else {      huffBMSizeTable =	  ((JBIG2CodeTable *)codeTables->get(i++))->getHuffTable();    }    if (huffAggInst == 0) {      huffAggInstTable = huffTableA;    } else {      huffAggInstTable =	  ((JBIG2CodeTable *)codeTables->get(i++))->getHuffTable();    }  }  delete codeTables;  // set up the Huffman decoder  if (huff) {    huffDecoder->reset();  // set up the arithmetic decoder  } else {    if (contextUsed && inputSymbolDict) {      resetGenericStats(sdTemplate, inputSymbolDict->getGenericRegionStats());      if (refAgg) {	resetRefinementStats(sdrTemplate,			     inputSymbolDict->getRefinementRegionStats());      }    } else {      resetGenericStats(sdTemplate, NULL);      if (refAgg) {	resetRefinementStats(sdrTemplate, NULL);      }    }    resetIntStats(symCodeLen);    arithDecoder->start();  }  // allocate symbol widths storage  symWidths = NULL;  if (huff && !refAgg) {    symWidths = (Guint *)gmalloc(numNewSyms * sizeof(Guint));  }  symHeight = 0;  i = 0;  while (i < numNewSyms) {    // read the height class delta height    if (huff) {      huffDecoder->decodeInt(&dh, huffDHTable);    } else {      arithDecoder->decodeInt(&dh, iadhStats);    }    symHeight += dh;    symWidth = 0;    totalWidth = 0;    j = i;    // read the symbols in this height class    while (1) {      // read the delta width      if (huff) {	if (!huffDecoder->decodeInt(&dw, huffDWTable)) {	  break;	}      } else {	if (!arithDecoder->decodeInt(&dw, iadwStats)) {	  break;	}      }      symWidth += dw;      // using a collective bitmap, so don't read a bitmap here      if (huff && !refAgg) {	symWidths[i] = symWidth;	totalWidth += symWidth;      // refinement/aggregate coding      } else if (refAgg) {	if (huff) {	  if (!huffDecoder->decodeInt(&refAggNum, huffAggInstTable)) {	    break;	  }	} else {	  if (!arithDecoder->decodeInt(&refAggNum, iaaiStats)) {	    break;	  }	}	if (refAggNum == 1) {	  if (huff) {	    symID = huffDecoder->readBits(symCodeLen);	    huffDecoder->decodeInt(&refDX, huffTableO);	    huffDecoder->decodeInt(&refDY, huffTableO);	    huffDecoder->decodeInt(&bmSize, huffTableA);	    huffDecoder->reset();	    arithDecoder->start();	  } else {	    symID = arithDecoder->decodeIAID(symCodeLen, iaidStats);	    arithDecoder->decodeInt(&refDX, iardxStats);	    arithDecoder->decodeInt(&refDY, iardyStats);	  }	  refBitmap = bitmaps[symID];	  bitmaps[numInputSyms + i] =	      readGenericRefinementRegion(symWidth, symHeight,					  sdrTemplate, gFalse,					  refBitmap, refDX, refDY,					  sdrATX, sdrATY);	  //~ do we need to use the bmSize value here (in Huffman mode)?	} else {	  bitmaps[numInputSyms + i] =	      readTextRegion(huff, gTrue, symWidth, symHeight,			     refAggNum, 0, numInputSyms + i, NULL,			     symCodeLen, bitmaps, 0, 0, 0, 1, 0,			     huffTableF, huffTableH, huffTableK, huffTableO,			     huffTableO, huffTableO, huffTableO, huffTableA,			     sdrTemplate, sdrATX, sdrATY);	}      // non-ref/agg coding      } else {	bitmaps[numInputSyms + i] =	    readGenericBitmap(gFalse, symWidth, symHeight,			      sdTemplate, gFalse, gFalse, NULL,			      sdATX, sdATY, 0);      }      ++i;    }    // read the collective bitmap    if (huff && !refAgg) {      huffDecoder->decodeInt(&bmSize, huffBMSizeTable);      if (huff) {	huffDecoder->reset();      }      if (bmSize == 0) {	collBitmap = new JBIG2Bitmap(0, totalWidth, symHeight);	bmSize = symHeight * ((totalWidth + 7) >> 3);	p = collBitmap->getDataPtr();	for (k = 0; k < (Guint)bmSize; ++k) {	  *p++ = str->getChar();	}      } else {	collBitmap = readGenericBitmap(gTrue, totalWidth, symHeight,				       0, gFalse, gFalse, NULL, NULL, NULL,				       bmSize);      }      x = 0;      for (; j < i; ++j) {	bitmaps[numInputSyms + j] =	    collBitmap->getSlice(x, 0, symWidths[j], symHeight);	x += symWidths[j];      }      delete collBitmap;    }  }  // create the symbol dict object  symbolDict = new JBIG2SymbolDict(segNum, numExSyms);  // exported symbol list  i = j = 0;  ex = gFalse;  while (i < numInputSyms + numNewSyms) {    if (huff) {      huffDecoder->decodeInt(&run, huffTableA);    } else {      arithDecoder->decodeInt(&run, iaexStats);    }    if (ex) {      for (cnt = 0; cnt < run; ++cnt) {	symbolDict->setBitmap(j++, bitmaps[i++]->copy());      }    } else {      i += run;    }    ex = !ex;  }    for (i = 0; i < numNewSyms; ++i) {    delete bitmaps[numInputSyms + i];  }  gfree(bitmaps);  if (symWidths) {    gfree(symWidths);  }  // save the arithmetic decoder stats  if (!huff && contextRetained) {    symbolDict->setGenericRegionStats(genericRegionStats->copy());    if (refAgg) {      symbolDict->setRefinementRegionStats(refinementRegionStats->copy());    }  }  // store the new symbol dict  segments->append(symbolDict);  return; eofError:  error(getPos(), "Unexpected EOF in JBIG2 stream");}void JBIG2Stream::readTextRegionSeg(Guint segNum, GBool imm,				    GBool lossless, Guint length,				    Guint *refSegs, Guint nRefSegs) {  JBIG2Bitmap *bitmap;  JBIG2HuffmanTable runLengthTab[36];  JBIG2HuffmanTable *symCodeTab;  JBIG2HuffmanTable *huffFSTable, *huffDSTable, *huffDTTable;  JBIG2HuffmanTable *huffRDWTable, *huffRDHTable;  JBIG2HuffmanTable *huffRDXTable, *huffRDYTable, *huffRSizeTable;  JBIG2Segment *seg;  GList *codeTables;  JBIG2SymbolDict *symbolDict;  JBIG2Bitmap **syms;  Guint w, h, x, y, segInfoFlags, extCombOp;  Guint flags, huff, refine, logStrips, refCorner, transposed;  Guint combOp, defPixel, sOffset, templ;  Guint huffFlags, huffFS, huffDS, huffDT;  Guint huffRDW, huffRDH, huffRDX, huffRDY, huffRSize;  Guint numInstances, numSyms, symCodeLen;  int atx[2], aty[2];  Guint i, k, kk;  int j;  // region segment info field  if (!readULong(&w) || !readULong(&h) ||      !readULong(&x) || !readULong(&y) ||      !readUByte(&segInfoFlags)) {    goto eofError;  }  extCombOp = segInfoFlags & 7;  // rest of the text region header  if (!readUWord(&flags)) {    goto eofError;  }  huff = flags & 1;  refine = (flags >> 1) & 1;  logStrips = (flags >> 2) & 3;  refCorner = (flags >> 4) & 3;  transposed = (flags >> 6) & 1;  combOp = (flags >> 7) & 3;  defPixel = (flags >> 9) & 1;  sOffset = (flags >> 10) & 0x1f;  templ = (flags >> 15) & 1;  huffFS = huffDS = huffDT = 0; // make gcc happy  huffRDW = huffRDH = huffRDX = huffRDY = huffRSize = 0; // make gcc happy  if (huff) {    if (!readUWord(&huffFlags)) {      goto eofError;    }    huffFS = huffFlags & 3;    huffDS = (huffFlags >> 2) & 3;    huffDT = (huffFlags >> 4) & 3;    huffRDW = (huffFlags >> 6) & 3;    huffRDH = (huffFlags >> 8) & 3;    huffRDX = (huffFlags >> 10) & 3;    huffRDY = (huffFlags >> 12) & 3;    huffRSize = (huffFlags >> 14) & 1;  }  if (refine && templ == 0) {    if (!readByte(&atx[0]) || !readByte(&aty[0]) ||	!readByte(&atx[1]) || !readByte(&aty[1])) {      goto eofError;    }  }  if (!readULong(&numInstances)) {    goto eofError;  }  // get symbol dictionaries and tables  codeTables = new GList();  numSyms = 0;  for (i = 0; i < nRefSegs; ++i) {    seg = findSegment(refSegs[i]);    if (seg->getType() == jbig2SegSymbolDict) {      numSyms += ((JBIG2SymbolDict *)seg)->getSize();    } else if (seg->getType() == jbig2SegCodeTable) {      codeTables->append(seg);    }  }  symCodeLen = 0;  i = 1;  while (i < numSyms) {    ++symCodeLen;    i <<= 1;  }  // get the symbol bitmaps  syms = (JBIG2Bitmap **)gmalloc(numSyms * sizeof(JBIG2Bitmap *));  kk = 0;  for (i = 0; i < nRefSegs; ++i) {    seg = findSegment(refSegs[i]);    if (seg->getType() == jbig2SegSymbolDict) {      symbolDict = (JBIG2SymbolDict *)seg;      for (k = 0; k < symbolDict->getSize(); ++k) {	syms[kk++] = symbolDict->getBitmap(k);      }    }  }

⌨️ 快捷键说明

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