Commit 1457c42181b55de6939cbb384dee47c5d86e503f

Authored by m-holger
1 parent a5ec9dc0

Refactor stream compression logic in `QPDFWriter` to simplify pipeline activatio…

…n and improve separation of concerns.
Showing 1 changed file with 15 additions and 23 deletions
libqpdf/QPDFWriter.cc
... ... @@ -1768,7 +1768,6 @@ QPDFWriter::writeObjectStream(QPDFObjectHandle object)
1768 1768 }
1769 1769 }
1770 1770 {
1771   - auto pp_ostream = m->pipeline_stack.popper();
1772 1771 // Adjust offsets to skip over comment before first object
1773 1772 first = offsets.at(0);
1774 1773 for (auto& iter: offsets) {
... ... @@ -1783,18 +1782,15 @@ QPDFWriter::writeObjectStream(QPDFObjectHandle object)
1783 1782 }
1784 1783  
1785 1784 // Set up a stream to write the stream data into a buffer.
1786   - if (compressed) {
1787   - m->pipeline_stack.activate(
1788   - pp_ostream,
1789   - pl::create<Pl_Flate>(
1790   - pl::create<pl::String>(stream_buffer_pass2), Pl_Flate::a_deflate));
1791   - } else {
1792   - m->pipeline_stack.activate(pp_ostream, stream_buffer_pass2);
1793   - }
  1785 + auto pp_ostream = m->pipeline_stack.activate(stream_buffer_pass2);
  1786 +
1794 1787 writeObjectStreamOffsets(offsets, first_obj);
1795 1788 write(stream_buffer_pass1);
1796 1789 stream_buffer_pass1.clear();
1797 1790 stream_buffer_pass1.shrink_to_fit();
  1791 + if (compressed) {
  1792 + stream_buffer_pass2 = pl::pipe<Pl_Flate>(stream_buffer_pass2, Pl_Flate::a_deflate);
  1793 + }
1798 1794 }
1799 1795  
1800 1796 // Write the object
... ... @@ -2528,20 +2524,7 @@ QPDFWriter::writeXRefStream(
2528 2524 std::string xref_data;
2529 2525 const bool compressed = m->compress_streams && !m->qdf_mode;
2530 2526 {
2531   - auto pp_xref = m->pipeline_stack.popper();
2532   - if (compressed) {
2533   - m->pipeline_stack.clear_buffer();
2534   - auto link = pl::create<pl::String>(xref_data);
2535   - if (!skip_compression) {
2536   - // Write the stream dictionary for compression but don't actually compress. This
2537   - // helps us with computation of padding for pass 1 of linearization.
2538   - link = pl::create<Pl_Flate>(std::move(link), Pl_Flate::a_deflate);
2539   - }
2540   - m->pipeline_stack.activate(
2541   - pp_xref, pl::create<Pl_PNGFilter>(std::move(link), Pl_PNGFilter::a_encode, esize));
2542   - } else {
2543   - m->pipeline_stack.activate(pp_xref, xref_data);
2544   - }
  2527 + auto pp_xref = m->pipeline_stack.activate(xref_data);
2545 2528  
2546 2529 for (int i = first; i <= last; ++i) {
2547 2530 QPDFXRefEntry& e = m->new_obj[i].xref;
... ... @@ -2577,6 +2560,15 @@ QPDFWriter::writeXRefStream(
2577 2560 }
2578 2561 }
2579 2562  
  2563 + if (compressed) {
  2564 + xref_data = pl::pipe<Pl_PNGFilter>(xref_data, Pl_PNGFilter::a_encode, esize);
  2565 + if (!skip_compression) {
  2566 + // Write the stream dictionary for compression but don't actually compress. This
  2567 + // helps us with computation of padding for pass 1 of linearization.
  2568 + xref_data = pl::pipe<Pl_Flate>(xref_data, Pl_Flate::a_deflate);
  2569 + }
  2570 + }
  2571 +
2580 2572 openObject(xref_id);
2581 2573 write("<<").write_qdf("\n ").write(" /Type /XRef").write_qdf("\n ");
2582 2574 write(" /Length ").write(xref_data.size());
... ...