OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "core/fxcodec/fx_codec.h" | 7 #include "core/fxcodec/fx_codec.h" |
8 | 8 |
| 9 #include <algorithm> |
9 #include <cmath> | 10 #include <cmath> |
10 #include <memory> | 11 #include <memory> |
11 #include <utility> | 12 #include <utility> |
12 | 13 |
13 #include "core/fxcodec/codec/codec_int.h" | 14 #include "core/fxcodec/codec/codec_int.h" |
14 #include "core/fxcrt/fx_ext.h" | 15 #include "core/fxcrt/fx_ext.h" |
15 #include "core/fxcrt/fx_safe_types.h" | 16 #include "core/fxcrt/fx_safe_types.h" |
16 #include "third_party/base/logging.h" | 17 #include "third_party/base/logging.h" |
17 #include "third_party/base/ptr_util.h" | 18 #include "third_party/base/ptr_util.h" |
18 | 19 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 } | 95 } |
95 | 96 |
96 uint8_t* CCodec_ScanlineDecoder::ReadNextLine() { | 97 uint8_t* CCodec_ScanlineDecoder::ReadNextLine() { |
97 return v_GetNextLine(); | 98 return v_GetNextLine(); |
98 } | 99 } |
99 | 100 |
100 bool CCodec_BasicModule::RunLengthEncode(const uint8_t* src_buf, | 101 bool CCodec_BasicModule::RunLengthEncode(const uint8_t* src_buf, |
101 uint32_t src_size, | 102 uint32_t src_size, |
102 uint8_t** dest_buf, | 103 uint8_t** dest_buf, |
103 uint32_t* dest_size) { | 104 uint32_t* dest_size) { |
104 return false; | 105 // Check inputs |
| 106 if (!src_buf || !dest_buf || !dest_size || src_size == 0) |
| 107 return false; |
| 108 |
| 109 // Edge case |
| 110 if (src_size == 1) { |
| 111 *dest_buf = FX_Alloc(uint8_t, 3); |
| 112 (*dest_buf)[0] = 0; |
| 113 (*dest_buf)[1] = src_buf[0]; |
| 114 (*dest_buf)[2] = 128; |
| 115 *dest_size = 3; |
| 116 return true; |
| 117 } |
| 118 |
| 119 // Worst case: 1 nonmatch, 2 match, 1 nonmatch, 2 match, etc. This becomes |
| 120 // 4 output chars for every 3 input, plus up to 4 more for the 1-2 chars |
| 121 // rounded off plus the terminating character. |
| 122 uint32_t est_size = 4 * ((src_size + 2) / 3) + 1; |
| 123 *dest_buf = FX_Alloc(uint8_t, est_size); |
| 124 |
| 125 // Set up pointers. |
| 126 uint8_t* out = *dest_buf; |
| 127 uint32_t run_start = 0; |
| 128 uint32_t run_end = 1; |
| 129 uint8_t x = src_buf[run_start]; |
| 130 uint8_t y = src_buf[run_end]; |
| 131 while (run_end < src_size) { |
| 132 uint32_t max_len = std::min((uint32_t)128, src_size - run_start); |
| 133 while (x == y && (run_end - run_start < max_len - 1)) |
| 134 y = src_buf[++run_end]; |
| 135 |
| 136 // Reached end with matched run. Update variables to expected values. |
| 137 if (x == y) { |
| 138 run_end++; |
| 139 if (run_end < src_size) |
| 140 y = src_buf[run_end]; |
| 141 } |
| 142 if (run_end - run_start > 1) { // Matched run but not at end of input. |
| 143 out[0] = 257 - (run_end - run_start); |
| 144 out[1] = x; |
| 145 x = y; |
| 146 run_start = run_end; |
| 147 run_end++; |
| 148 if (run_end < src_size) |
| 149 y = src_buf[run_end]; |
| 150 out += 2; |
| 151 continue; |
| 152 } |
| 153 // Mismatched run |
| 154 while (x != y && run_end <= run_start + max_len) { |
| 155 out[run_end - run_start] = x; |
| 156 x = y; |
| 157 run_end++; |
| 158 if (run_end == src_size) { |
| 159 if (run_end <= run_start + max_len) { |
| 160 out[run_end - run_start] = x; |
| 161 run_end++; |
| 162 } |
| 163 break; |
| 164 } |
| 165 y = src_buf[run_end]; |
| 166 } |
| 167 out[0] = run_end - run_start - 2; |
| 168 out += run_end - run_start; |
| 169 run_start = run_end - 1; |
| 170 } |
| 171 if (run_start < src_size) { // 1 leftover character |
| 172 out[0] = 0; |
| 173 out[1] = x; |
| 174 out += 2; |
| 175 } |
| 176 *out = 128; |
| 177 *dest_size = out + 1 - *dest_buf; |
| 178 return true; |
105 } | 179 } |
106 | 180 |
107 bool CCodec_BasicModule::A85Encode(const uint8_t* src_buf, | 181 bool CCodec_BasicModule::A85Encode(const uint8_t* src_buf, |
108 uint32_t src_size, | 182 uint32_t src_size, |
109 uint8_t** dest_buf, | 183 uint8_t** dest_buf, |
110 uint32_t* dest_size) { | 184 uint32_t* dest_size) { |
111 return false; | 185 // Check inputs. |
| 186 if (!src_buf || !dest_buf || !dest_size) |
| 187 return false; |
| 188 |
| 189 if (src_size == 0) { |
| 190 *dest_size = 0; |
| 191 return false; |
| 192 } |
| 193 |
| 194 // Worst case: 5 output for each 4 input (plus up to 4 from leftover), plus |
| 195 // 2 character new lines each 75 output chars plus 2 termination chars. May |
| 196 // have fewer if there are special "z" chars. |
| 197 uint32_t est_size = 5 * (src_size / 4) + 4 + src_size / 30 + 2; |
| 198 *dest_buf = FX_Alloc(uint8_t, est_size); |
| 199 |
| 200 // Set up pointers. |
| 201 uint8_t* out = *dest_buf; |
| 202 uint32_t pos = 0; |
| 203 uint32_t line_length = 0; |
| 204 while (src_size >= 4 && pos < src_size - 3) { |
| 205 uint32_t val = ((uint32_t)(src_buf[pos]) << 24) + |
| 206 ((uint32_t)(src_buf[pos + 1]) << 16) + |
| 207 ((uint32_t)(src_buf[pos + 2]) << 8) + |
| 208 (uint32_t)(src_buf[pos + 3]); |
| 209 pos += 4; |
| 210 if (val == 0) { // All zero special case |
| 211 *out = 'z'; |
| 212 out++; |
| 213 line_length++; |
| 214 } else { // Compute base 85 characters and add 33. |
| 215 for (int i = 4; i >= 0; i--) { |
| 216 out[i] = (uint8_t)(val % 85) + 33; |
| 217 val = val / 85; |
| 218 } |
| 219 out += 5; |
| 220 line_length += 5; |
| 221 } |
| 222 if (line_length >= 75) { // Add a return. |
| 223 *out++ = '\r'; |
| 224 *out++ = '\n'; |
| 225 line_length = 0; |
| 226 } |
| 227 } |
| 228 if (pos < src_size) { // Leftover bytes |
| 229 uint32_t val = 0; |
| 230 int count = 0; |
| 231 while (pos < src_size) { |
| 232 val += (uint32_t)(src_buf[pos] << (8 * (3 - pos))); |
| 233 count++; |
| 234 pos++; |
| 235 } |
| 236 for (int i = 4; i >= 0; i--) { |
| 237 if (i <= count) |
| 238 out[i] = (uint8_t)(val % 85) + 33; |
| 239 val = val / 85; |
| 240 } |
| 241 out += count + 1; |
| 242 } |
| 243 |
| 244 // Terminating characters. |
| 245 out[0] = '~'; |
| 246 out[1] = '>'; |
| 247 out += 2; |
| 248 *dest_size = out - *dest_buf; |
| 249 return true; |
112 } | 250 } |
113 | 251 |
114 #ifdef PDF_ENABLE_XFA | 252 #ifdef PDF_ENABLE_XFA |
115 CFX_DIBAttribute::CFX_DIBAttribute() | 253 CFX_DIBAttribute::CFX_DIBAttribute() |
116 : m_nXDPI(-1), | 254 : m_nXDPI(-1), |
117 m_nYDPI(-1), | 255 m_nYDPI(-1), |
118 m_fAspectRatio(-1.0f), | 256 m_fAspectRatio(-1.0f), |
119 m_wDPIUnit(0), | 257 m_wDPIUnit(0), |
120 m_nGifLeft(0), | 258 m_nGifLeft(0), |
121 m_nGifTop(0), | 259 m_nGifTop(0), |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 int width, | 461 int width, |
324 int height, | 462 int height, |
325 int nComps, | 463 int nComps, |
326 int bpc) { | 464 int bpc) { |
327 auto pDecoder = pdfium::MakeUnique<CCodec_RLScanlineDecoder>(); | 465 auto pDecoder = pdfium::MakeUnique<CCodec_RLScanlineDecoder>(); |
328 if (!pDecoder->Create(src_buf, src_size, width, height, nComps, bpc)) | 466 if (!pDecoder->Create(src_buf, src_size, width, height, nComps, bpc)) |
329 return nullptr; | 467 return nullptr; |
330 | 468 |
331 return std::move(pDecoder); | 469 return std::move(pDecoder); |
332 } | 470 } |
OLD | NEW |