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