summaryrefslogtreecommitdiff
path: root/core/fxcodec/codec/fx_codec.cpp
diff options
context:
space:
mode:
authorrbpotter <rbpotter@chromium.org>2017-01-12 10:31:43 -0800
committerCommit bot <commit-bot@chromium.org>2017-01-12 10:31:43 -0800
commitdb7647083d0a5cd2221b94faa15c149214d21725 (patch)
tree427c0beeb82796e8e550e672195061186d7db076 /core/fxcodec/codec/fx_codec.cpp
parente7fb8eb3e9f220a5f0c852f0238ab8d536a8bc9d (diff)
downloadpdfium-db7647083d0a5cd2221b94faa15c149214d21725.tar.xz
Add postscript path
This patch adds the additional functions required to make postscript printing functional. The most significant additions are are two added compression functions and a new API for setting the postscript level. Not currently called from Chromium, Chromium patch to come. BUG= Review-Url: https://codereview.chromium.org/2612243005
Diffstat (limited to 'core/fxcodec/codec/fx_codec.cpp')
-rw-r--r--core/fxcodec/codec/fx_codec.cpp142
1 files changed, 140 insertions, 2 deletions
diff --git a/core/fxcodec/codec/fx_codec.cpp b/core/fxcodec/codec/fx_codec.cpp
index 6b6c723388..23171cf3ea 100644
--- a/core/fxcodec/codec/fx_codec.cpp
+++ b/core/fxcodec/codec/fx_codec.cpp
@@ -6,6 +6,7 @@
#include "core/fxcodec/fx_codec.h"
+#include <algorithm>
#include <cmath>
#include <memory>
#include <utility>
@@ -101,14 +102,151 @@ bool CCodec_BasicModule::RunLengthEncode(const uint8_t* src_buf,
uint32_t src_size,
uint8_t** dest_buf,
uint32_t* dest_size) {
- return false;
+ // Check inputs
+ if (!src_buf || !dest_buf || !dest_size || src_size == 0)
+ return false;
+
+ // Edge case
+ if (src_size == 1) {
+ *dest_buf = FX_Alloc(uint8_t, 3);
+ (*dest_buf)[0] = 0;
+ (*dest_buf)[1] = src_buf[0];
+ (*dest_buf)[2] = 128;
+ *dest_size = 3;
+ return true;
+ }
+
+ // Worst case: 1 nonmatch, 2 match, 1 nonmatch, 2 match, etc. This becomes
+ // 4 output chars for every 3 input, plus up to 4 more for the 1-2 chars
+ // rounded off plus the terminating character.
+ uint32_t est_size = 4 * ((src_size + 2) / 3) + 1;
+ *dest_buf = FX_Alloc(uint8_t, est_size);
+
+ // Set up pointers.
+ uint8_t* out = *dest_buf;
+ uint32_t run_start = 0;
+ uint32_t run_end = 1;
+ uint8_t x = src_buf[run_start];
+ uint8_t y = src_buf[run_end];
+ while (run_end < src_size) {
+ uint32_t max_len = std::min((uint32_t)128, src_size - run_start);
+ while (x == y && (run_end - run_start < max_len - 1))
+ y = src_buf[++run_end];
+
+ // Reached end with matched run. Update variables to expected values.
+ if (x == y) {
+ run_end++;
+ if (run_end < src_size)
+ y = src_buf[run_end];
+ }
+ if (run_end - run_start > 1) { // Matched run but not at end of input.
+ out[0] = 257 - (run_end - run_start);
+ out[1] = x;
+ x = y;
+ run_start = run_end;
+ run_end++;
+ if (run_end < src_size)
+ y = src_buf[run_end];
+ out += 2;
+ continue;
+ }
+ // Mismatched run
+ while (x != y && run_end <= run_start + max_len) {
+ out[run_end - run_start] = x;
+ x = y;
+ run_end++;
+ if (run_end == src_size) {
+ if (run_end <= run_start + max_len) {
+ out[run_end - run_start] = x;
+ run_end++;
+ }
+ break;
+ }
+ y = src_buf[run_end];
+ }
+ out[0] = run_end - run_start - 2;
+ out += run_end - run_start;
+ run_start = run_end - 1;
+ }
+ if (run_start < src_size) { // 1 leftover character
+ out[0] = 0;
+ out[1] = x;
+ out += 2;
+ }
+ *out = 128;
+ *dest_size = out + 1 - *dest_buf;
+ return true;
}
bool CCodec_BasicModule::A85Encode(const uint8_t* src_buf,
uint32_t src_size,
uint8_t** dest_buf,
uint32_t* dest_size) {
- return false;
+ // Check inputs.
+ if (!src_buf || !dest_buf || !dest_size)
+ return false;
+
+ if (src_size == 0) {
+ *dest_size = 0;
+ return false;
+ }
+
+ // Worst case: 5 output for each 4 input (plus up to 4 from leftover), plus
+ // 2 character new lines each 75 output chars plus 2 termination chars. May
+ // have fewer if there are special "z" chars.
+ uint32_t est_size = 5 * (src_size / 4) + 4 + src_size / 30 + 2;
+ *dest_buf = FX_Alloc(uint8_t, est_size);
+
+ // Set up pointers.
+ uint8_t* out = *dest_buf;
+ uint32_t pos = 0;
+ uint32_t line_length = 0;
+ while (src_size >= 4 && pos < src_size - 3) {
+ uint32_t val = ((uint32_t)(src_buf[pos]) << 24) +
+ ((uint32_t)(src_buf[pos + 1]) << 16) +
+ ((uint32_t)(src_buf[pos + 2]) << 8) +
+ (uint32_t)(src_buf[pos + 3]);
+ pos += 4;
+ if (val == 0) { // All zero special case
+ *out = 'z';
+ out++;
+ line_length++;
+ } else { // Compute base 85 characters and add 33.
+ for (int i = 4; i >= 0; i--) {
+ out[i] = (uint8_t)(val % 85) + 33;
+ val = val / 85;
+ }
+ out += 5;
+ line_length += 5;
+ }
+ if (line_length >= 75) { // Add a return.
+ *out++ = '\r';
+ *out++ = '\n';
+ line_length = 0;
+ }
+ }
+ if (pos < src_size) { // Leftover bytes
+ uint32_t val = 0;
+ int count = 0;
+ while (pos < src_size) {
+ val += (uint32_t)(src_buf[pos] << (8 * (3 - pos)));
+ count++;
+ pos++;
+ }
+ for (int i = 4; i >= 0; i--) {
+ if (i <= count)
+ out[i] = (uint8_t)(val % 85) + 33;
+ val = val / 85;
+ }
+ out += count + 1;
+ }
+
+ // Terminating characters.
+ out[0] = '~';
+ out[1] = '>';
+ out += 2;
+ *dest_size = out - *dest_buf;
+ return true;
}
#ifdef PDF_ENABLE_XFA