Debian Codegraph @

podofo/3rdparty/chromium/pdfium/core/fxcodec/fax/faxmodule.cpp

raw
1 // Copyright 2014 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "faxmodule.h"
8
9 #include <stdint.h>
10
11 #include <algorithm>
12 #include <iterator>
13 #include <memory>
14 #include <utility>
15
16 #include <pdfium/pdfium_shim.h>
17 #include <pdfium/core/fxcodec/scanlinedecoder.h>
18
19 namespace chromium {
20
21 namespace {
22
23 const uint8_t OneLeadPos[256] = {
24 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3,
25 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
26 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1,
27 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
28 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
29 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
31 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
33 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
35 };
36
37 // Limit of image dimension. Use the same limit as the JBIG2 codecs.
38 constexpr int kFaxMaxImageDimension = 65535;
39
40 constexpr int kFaxBpc = 1;
41 constexpr int kFaxComps = 1;
42
43 int FindBit(pdfium::span<const uint8_t> data_buf,
44 int max_pos,
45 int start_pos,
46 bool bit) {
47 DCHECK(start_pos >= 0);
48 if (start_pos >= max_pos)
49 return max_pos;
50
51 const uint8_t bit_xor = bit ? 0x00 : 0xff;
52 int bit_offset = start_pos % 8;
53 if (bit_offset) {
54 const int byte_pos = start_pos / 8;
55 uint8_t data = (data_buf[byte_pos] ^ bit_xor) & (0xff >> bit_offset);
56 if (data)
57 return byte_pos * 8 + OneLeadPos[data];
58
59 start_pos += 7;
60 }
61
62 const int max_byte = (max_pos + 7) / 8;
63 int byte_pos = start_pos / 8;
64
65 // Try reading in bigger chunks in case there are long runs to be skipped.
66 static constexpr int kBulkReadSize = 8;
67 if (max_byte >= kBulkReadSize && byte_pos < max_byte - kBulkReadSize) {
68 static constexpr uint8_t skip_block_0[kBulkReadSize] = {
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
70 static constexpr uint8_t skip_block_1[kBulkReadSize] = {
71 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
72 const uint8_t* skip_block = bit ? skip_block_0 : skip_block_1;
73 while (byte_pos < max_byte - kBulkReadSize &&
74 memcmp(data_buf.subspan(byte_pos).data(), skip_block,
75 kBulkReadSize) == 0) {
76 byte_pos += kBulkReadSize;
77 }
78 }
79
80 while (byte_pos < max_byte) {
81 uint8_t data = data_buf[byte_pos] ^ bit_xor;
82 if (data)
83 return std::min(byte_pos * 8 + OneLeadPos[data], max_pos);
84
85 ++byte_pos;
86 }
87 return max_pos;
88 }
89
90 void FaxG4FindB1B2(pdfium::span<const uint8_t> ref_buf,
91 int columns,
92 int a0,
93 bool a0color,
94 int* b1,
95 int* b2) {
96 bool first_bit = a0 < 0 || (ref_buf[a0 / 8] & (1 << (7 - a0 % 8))) != 0;
97 *b1 = FindBit(ref_buf, columns, a0 + 1, !first_bit);
98 if (*b1 >= columns) {
99 *b1 = *b2 = columns;
100 return;
101 }
102 if (first_bit == !a0color) {
103 *b1 = FindBit(ref_buf, columns, *b1 + 1, first_bit);
104 first_bit = !first_bit;
105 }
106 if (*b1 >= columns) {
107 *b1 = *b2 = columns;
108 return;
109 }
110 *b2 = FindBit(ref_buf, columns, *b1 + 1, first_bit);
111 }
112
113 void FaxFillBits(uint8_t* dest_buf, int columns, int startpos, int endpos) {
114 startpos = std::max(startpos, 0);
115 endpos = pdfium::clamp(endpos, 0, columns);
116 if (startpos >= endpos)
117 return;
118
119 int first_byte = startpos / 8;
120 int last_byte = (endpos - 1) / 8;
121 if (first_byte == last_byte) {
122 for (int i = startpos % 8; i <= (endpos - 1) % 8; ++i)
123 dest_buf[first_byte] -= 1 << (7 - i);
124 return;
125 }
126
127 for (int i = startpos % 8; i < 8; ++i)
128 dest_buf[first_byte] -= 1 << (7 - i);
129 for (int i = 0; i <= (endpos - 1) % 8; ++i)
130 dest_buf[last_byte] -= 1 << (7 - i);
131
132 if (last_byte > first_byte + 1)
133 memset(dest_buf + first_byte + 1, 0, last_byte - first_byte - 1);
134 }
135
136 inline bool NextBit(const uint8_t* src_buf, int* bitpos) {
137 int pos = (*bitpos)++;
138 return !!(src_buf[pos / 8] & (1 << (7 - pos % 8)));
139 }
140
141 const uint8_t kFaxBlackRunIns[] = {
142 0, 2, 0x02, 3, 0, 0x03,
143 2, 0, 2, 0x02, 1, 0,
144 0x03, 4, 0, 2, 0x02, 6,
145 0, 0x03, 5, 0, 1, 0x03,
146 7, 0, 2, 0x04, 9, 0,
147 0x05, 8, 0, 3, 0x04, 10,
148 0, 0x05, 11, 0, 0x07, 12,
149 0, 2, 0x04, 13, 0, 0x07,
150 14, 0, 1, 0x18, 15, 0,
151 5, 0x08, 18, 0, 0x0f, 64,
152 0, 0x17, 16, 0, 0x18, 17,
153 0, 0x37, 0, 0, 10, 0x08,
154 0x00, 0x07, 0x0c, 0x40, 0x07, 0x0d,
155 0x80, 0x07, 0x17, 24, 0, 0x18,
156 25, 0, 0x28, 23, 0, 0x37,
157 22, 0, 0x67, 19, 0, 0x68,
158 20, 0, 0x6c, 21, 0, 54,
159 0x12, 1984 % 256, 1984 / 256, 0x13, 2048 % 256, 2048 / 256,
160 0x14, 2112 % 256, 2112 / 256, 0x15, 2176 % 256, 2176 / 256,
161 0x16, 2240 % 256, 2240 / 256, 0x17, 2304 % 256, 2304 / 256,
162 0x1c, 2368 % 256, 2368 / 256, 0x1d, 2432 % 256, 2432 / 256,
163 0x1e, 2496 % 256, 2496 / 256, 0x1f, 2560 % 256, 2560 / 256,
164 0x24, 52, 0, 0x27, 55, 0,
165 0x28, 56, 0, 0x2b, 59, 0,
166 0x2c, 60, 0, 0x33, 320 % 256, 320 / 256,
167 0x34, 384 % 256, 384 / 256, 0x35, 448 % 256, 448 / 256,
168 0x37, 53, 0, 0x38, 54, 0,
169 0x52, 50, 0, 0x53, 51, 0,
170 0x54, 44, 0, 0x55, 45, 0,
171 0x56, 46, 0, 0x57, 47, 0,
172 0x58, 57, 0, 0x59, 58, 0,
173 0x5a, 61, 0, 0x5b, 256 % 256, 256 / 256,
174 0x64, 48, 0, 0x65, 49, 0,
175 0x66, 62, 0, 0x67, 63, 0,
176 0x68, 30, 0, 0x69, 31, 0,
177 0x6a, 32, 0, 0x6b, 33, 0,
178 0x6c, 40, 0, 0x6d, 41, 0,
179 0xc8, 128, 0, 0xc9, 192, 0,
180 0xca, 26, 0, 0xcb, 27, 0,
181 0xcc, 28, 0, 0xcd, 29, 0,
182 0xd2, 34, 0, 0xd3, 35, 0,
183 0xd4, 36, 0, 0xd5, 37, 0,
184 0xd6, 38, 0, 0xd7, 39, 0,
185 0xda, 42, 0, 0xdb, 43, 0,
186 20, 0x4a, 640 % 256, 640 / 256, 0x4b, 704 % 256,
187 704 / 256, 0x4c, 768 % 256, 768 / 256, 0x4d, 832 % 256,
188 832 / 256, 0x52, 1280 % 256, 1280 / 256, 0x53, 1344 % 256,
189 1344 / 256, 0x54, 1408 % 256, 1408 / 256, 0x55, 1472 % 256,
190 1472 / 256, 0x5a, 1536 % 256, 1536 / 256, 0x5b, 1600 % 256,
191 1600 / 256, 0x64, 1664 % 256, 1664 / 256, 0x65, 1728 % 256,
192 1728 / 256, 0x6c, 512 % 256, 512 / 256, 0x6d, 576 % 256,
193 576 / 256, 0x72, 896 % 256, 896 / 256, 0x73, 960 % 256,
194 960 / 256, 0x74, 1024 % 256, 1024 / 256, 0x75, 1088 % 256,
195 1088 / 256, 0x76, 1152 % 256, 1152 / 256, 0x77, 1216 % 256,
196 1216 / 256, 0xff};
197
198 const uint8_t kFaxWhiteRunIns[] = {
199 0, 0, 0, 6, 0x07, 2,
200 0, 0x08, 3, 0, 0x0B, 4,
201 0, 0x0C, 5, 0, 0x0E, 6,
202 0, 0x0F, 7, 0, 6, 0x07,
203 10, 0, 0x08, 11, 0, 0x12,
204 128, 0, 0x13, 8, 0, 0x14,
205 9, 0, 0x1b, 64, 0, 9,
206 0x03, 13, 0, 0x07, 1, 0,
207 0x08, 12, 0, 0x17, 192, 0,
208 0x18, 1664 % 256, 1664 / 256, 0x2a, 16, 0,
209 0x2B, 17, 0, 0x34, 14, 0,
210 0x35, 15, 0, 12, 0x03, 22,
211 0, 0x04, 23, 0, 0x08, 20,
212 0, 0x0c, 19, 0, 0x13, 26,
213 0, 0x17, 21, 0, 0x18, 28,
214 0, 0x24, 27, 0, 0x27, 18,
215 0, 0x28, 24, 0, 0x2B, 25,
216 0, 0x37, 256 % 256, 256 / 256, 42, 0x02,
217 29, 0, 0x03, 30, 0, 0x04,
218 45, 0, 0x05, 46, 0, 0x0a,
219 47, 0, 0x0b, 48, 0, 0x12,
220 33, 0, 0x13, 34, 0, 0x14,
221 35, 0, 0x15, 36, 0, 0x16,
222 37, 0, 0x17, 38, 0, 0x1a,
223 31, 0, 0x1b, 32, 0, 0x24,
224 53, 0, 0x25, 54, 0, 0x28,
225 39, 0, 0x29, 40, 0, 0x2a,
226 41, 0, 0x2b, 42, 0, 0x2c,
227 43, 0, 0x2d, 44, 0, 0x32,
228 61, 0, 0x33, 62, 0, 0x34,
229 63, 0, 0x35, 0, 0, 0x36,
230 320 % 256, 320 / 256, 0x37, 384 % 256, 384 / 256, 0x4a,
231 59, 0, 0x4b, 60, 0, 0x52,
232 49, 0, 0x53, 50, 0, 0x54,
233 51, 0, 0x55, 52, 0, 0x58,
234 55, 0, 0x59, 56, 0, 0x5a,
235 57, 0, 0x5b, 58, 0, 0x64,
236 448 % 256, 448 / 256, 0x65, 512 % 256, 512 / 256, 0x67,
237 640 % 256, 640 / 256, 0x68, 576 % 256, 576 / 256, 16,
238 0x98, 1472 % 256, 1472 / 256, 0x99, 1536 % 256, 1536 / 256,
239 0x9a, 1600 % 256, 1600 / 256, 0x9b, 1728 % 256, 1728 / 256,
240 0xcc, 704 % 256, 704 / 256, 0xcd, 768 % 256, 768 / 256,
241 0xd2, 832 % 256, 832 / 256, 0xd3, 896 % 256, 896 / 256,
242 0xd4, 960 % 256, 960 / 256, 0xd5, 1024 % 256, 1024 / 256,
243 0xd6, 1088 % 256, 1088 / 256, 0xd7, 1152 % 256, 1152 / 256,
244 0xd8, 1216 % 256, 1216 / 256, 0xd9, 1280 % 256, 1280 / 256,
245 0xda, 1344 % 256, 1344 / 256, 0xdb, 1408 % 256, 1408 / 256,
246 0, 3, 0x08, 1792 % 256, 1792 / 256, 0x0c,
247 1856 % 256, 1856 / 256, 0x0d, 1920 % 256, 1920 / 256, 10,
248 0x12, 1984 % 256, 1984 / 256, 0x13, 2048 % 256, 2048 / 256,
249 0x14, 2112 % 256, 2112 / 256, 0x15, 2176 % 256, 2176 / 256,
250 0x16, 2240 % 256, 2240 / 256, 0x17, 2304 % 256, 2304 / 256,
251 0x1c, 2368 % 256, 2368 / 256, 0x1d, 2432 % 256, 2432 / 256,
252 0x1e, 2496 % 256, 2496 / 256, 0x1f, 2560 % 256, 2560 / 256,
253 0xff,
254 };
255
256 int FaxGetRun(pdfium::span<const uint8_t> ins_array,
257 const uint8_t* src_buf,
258 int* bitpos,
259 int bitsize) {
260 uint32_t code = 0;
261 int ins_off = 0;
262 while (true) {
263 uint8_t ins = ins_array[ins_off++];
264 if (ins == 0xff)
265 return -1;
266
267 if (*bitpos >= bitsize)
268 return -1;
269
270 code <<= 1;
271 if (src_buf[*bitpos / 8] & (1 << (7 - *bitpos % 8)))
272 ++code;
273
274 ++(*bitpos);
275 int next_off = ins_off + ins * 3;
276 for (; ins_off < next_off; ins_off += 3) {
277 if (ins_array[ins_off] == code)
278 return ins_array[ins_off + 1] + ins_array[ins_off + 2] * 256;
279 }
280 }
281 }
282
283 void FaxG4GetRow(const uint8_t* src_buf,
284 int bitsize,
285 int* bitpos,
286 uint8_t* dest_buf,
287 pdfium::span<const uint8_t> ref_buf,
288 int columns) {
289 int a0 = -1;
290 bool a0color = true;
291 while (true) {
292 if (*bitpos >= bitsize)
293 return;
294
295 int a1;
296 int a2;
297 int b1;
298 int b2;
299 FaxG4FindB1B2(ref_buf, columns, a0, a0color, &b1, &b2);
300
301 int v_delta = 0;
302 if (!NextBit(src_buf, bitpos)) {
303 if (*bitpos >= bitsize)
304 return;
305
306 bool bit1 = NextBit(src_buf, bitpos);
307 if (*bitpos >= bitsize)
308 return;
309
310 bool bit2 = NextBit(src_buf, bitpos);
311 if (bit1) {
312 v_delta = bit2 ? 1 : -1;
313 } else if (bit2) {
314 int run_len1 = 0;
315 while (true) {
316 int run = FaxGetRun(a0color ? pdfium::make_span(kFaxWhiteRunIns)
317 : pdfium::make_span(kFaxBlackRunIns),
318 src_buf, bitpos, bitsize);
319 run_len1 += run;
320 if (run < 64)
321 break;
322 }
323 if (a0 < 0)
324 ++run_len1;
325 if (run_len1 < 0)
326 return;
327
328 a1 = a0 + run_len1;
329 if (!a0color)
330 FaxFillBits(dest_buf, columns, a0, a1);
331
332 int run_len2 = 0;
333 while (true) {
334 int run = FaxGetRun(a0color ? pdfium::make_span(kFaxBlackRunIns)
335 : pdfium::make_span(kFaxWhiteRunIns),
336 src_buf, bitpos, bitsize);
337 run_len2 += run;
338 if (run < 64)
339 break;
340 }
341 if (run_len2 < 0)
342 return;
343 a2 = a1 + run_len2;
344 if (a0color)
345 FaxFillBits(dest_buf, columns, a1, a2);
346
347 a0 = a2;
348 if (a0 < columns)
349 continue;
350
351 return;
352 } else {
353 if (*bitpos >= bitsize)
354 return;
355
356 if (NextBit(src_buf, bitpos)) {
357 if (!a0color)
358 FaxFillBits(dest_buf, columns, a0, b2);
359
360 if (b2 >= columns)
361 return;
362
363 a0 = b2;
364 continue;
365 }
366
367 if (*bitpos >= bitsize)
368 return;
369
370 bool next_bit1 = NextBit(src_buf, bitpos);
371 if (*bitpos >= bitsize)
372 return;
373
374 bool next_bit2 = NextBit(src_buf, bitpos);
375 if (next_bit1) {
376 v_delta = next_bit2 ? 2 : -2;
377 } else if (next_bit2) {
378 if (*bitpos >= bitsize)
379 return;
380
381 v_delta = NextBit(src_buf, bitpos) ? 3 : -3;
382 } else {
383 if (*bitpos >= bitsize)
384 return;
385
386 if (NextBit(src_buf, bitpos)) {
387 *bitpos += 3;
388 continue;
389 }
390 *bitpos += 5;
391 return;
392 }
393 }
394 }
395 a1 = b1 + v_delta;
396 if (!a0color)
397 FaxFillBits(dest_buf, columns, a0, a1);
398
399 if (a1 >= columns)
400 return;
401
402 // The position of picture element must be monotonic increasing.
403 if (a0 >= a1)
404 return;
405
406 a0 = a1;
407 a0color = !a0color;
408 }
409 }
410
411 void FaxSkipEOL(const uint8_t* src_buf, int bitsize, int* bitpos) {
412 int startbit = *bitpos;
413 while (*bitpos < bitsize) {
414 if (!NextBit(src_buf, bitpos))
415 continue;
416 if (*bitpos - startbit <= 11)
417 *bitpos = startbit;
418 return;
419 }
420 }
421
422 void FaxGet1DLine(const uint8_t* src_buf,
423 int bitsize,
424 int* bitpos,
425 uint8_t* dest_buf,
426 int columns) {
427 bool color = true;
428 int startpos = 0;
429 while (true) {
430 if (*bitpos >= bitsize)
431 return;
432
433 int run_len = 0;
434 while (true) {
435 int run = FaxGetRun(color ? pdfium::make_span(kFaxWhiteRunIns)
436 : pdfium::make_span(kFaxBlackRunIns),
437 src_buf, bitpos, bitsize);
438 if (run < 0) {
439 while (*bitpos < bitsize) {
440 if (NextBit(src_buf, bitpos))
441 return;
442 }
443 return;
444 }
445 run_len += run;
446 if (run < 64)
447 break;
448 }
449 if (!color)
450 FaxFillBits(dest_buf, columns, startpos, startpos + run_len);
451
452 startpos += run_len;
453 if (startpos >= columns)
454 break;
455
456 color = !color;
457 }
458 }
459
460 class FaxDecoder final : public ScanlineDecoder {
461 public:
462 FaxDecoder(pdfium::span<const uint8_t> src_span,
463 int width,
464 int height,
465 int K,
466 bool EndOfLine,
467 bool EncodedByteAlign,
468 bool BlackIs1);
469 ~FaxDecoder() override;
470
471 // ScanlineDecoder:
472 bool Rewind() override;
473 pdfium::span<uint8_t> GetNextLine() override;
474 uint32_t GetSrcOffset() override;
475
476 private:
477 void InvertBuffer();
478
479 const int m_Encoding;
480 int m_bitpos = 0;
481 bool m_bByteAlign = false;
482 const bool m_bEndOfLine;
483 const bool m_bBlack;
484 const pdfium::span<const uint8_t> m_SrcSpan;
485 DataVector<uint8_t> m_ScanlineBuf;
486 DataVector<uint8_t> m_RefBuf;
487 };
488
489 FaxDecoder::FaxDecoder(pdfium::span<const uint8_t> src_span,
490 int width,
491 int height,
492 int K,
493 bool EndOfLine,
494 bool EncodedByteAlign,
495 bool BlackIs1)
496 : ScanlineDecoder(width,
497 height,
498 width,
499 height,
500 kFaxComps,
501 kFaxBpc,
502 fxge::CalculatePitch32OrDie(kFaxBpc, width)),
503 m_Encoding(K),
504 m_bByteAlign(EncodedByteAlign),
505 m_bEndOfLine(EndOfLine),
506 m_bBlack(BlackIs1),
507 m_SrcSpan(src_span),
508 m_ScanlineBuf(m_Pitch),
509 m_RefBuf(m_Pitch) {}
510
511 FaxDecoder::~FaxDecoder() {
512 // Span in superclass can't outlive our buffer.
513 m_pLastScanline = pdfium::span<uint8_t>();
514 }
515
516 bool FaxDecoder::Rewind() {
517 memset(m_RefBuf.data(), 0xff, m_RefBuf.size());
518 m_bitpos = 0;
519 return true;
520 }
521
522 pdfium::span<uint8_t> FaxDecoder::GetNextLine() {
523 int bitsize = pdfium::base::checked_cast<int>(m_SrcSpan.size() * 8);
524 FaxSkipEOL(m_SrcSpan.data(), bitsize, &m_bitpos);
525 if (m_bitpos >= bitsize)
526 return pdfium::span<uint8_t>();
527
528 memset(m_ScanlineBuf.data(), 0xff, m_ScanlineBuf.size());
529 if (m_Encoding < 0) {
530 FaxG4GetRow(m_SrcSpan.data(), bitsize, &m_bitpos, m_ScanlineBuf.data(),
531 m_RefBuf, m_OrigWidth);
532 m_RefBuf = m_ScanlineBuf;
533 } else if (m_Encoding == 0) {
534 FaxGet1DLine(m_SrcSpan.data(), bitsize, &m_bitpos, m_ScanlineBuf.data(),
535 m_OrigWidth);
536 } else {
537 if (NextBit(m_SrcSpan.data(), &m_bitpos)) {
538 FaxGet1DLine(m_SrcSpan.data(), bitsize, &m_bitpos, m_ScanlineBuf.data(),
539 m_OrigWidth);
540 } else {
541 FaxG4GetRow(m_SrcSpan.data(), bitsize, &m_bitpos, m_ScanlineBuf.data(),
542 m_RefBuf, m_OrigWidth);
543 }
544 m_RefBuf = m_ScanlineBuf;
545 }
546 if (m_bEndOfLine)
547 FaxSkipEOL(m_SrcSpan.data(), bitsize, &m_bitpos);
548
549 if (m_bByteAlign && m_bitpos < bitsize) {
550 int bitpos0 = m_bitpos;
551 int bitpos1 = FxAlignToBoundary<8>(m_bitpos);
552 while (m_bByteAlign && bitpos0 < bitpos1) {
553 int bit = m_SrcSpan[bitpos0 / 8] & (1 << (7 - bitpos0 % 8));
554 if (bit != 0)
555 m_bByteAlign = false;
556 else
557 ++bitpos0;
558 }
559 if (m_bByteAlign)
560 m_bitpos = bitpos1;
561 }
562 if (m_bBlack)
563 InvertBuffer();
564 return m_ScanlineBuf;
565 }
566
567 uint32_t FaxDecoder::GetSrcOffset() {
568 return pdfium::base::checked_cast<uint32_t>(
569 std::min<size_t>((m_bitpos + 7) / 8, m_SrcSpan.size()));
570 }
571
572 void FaxDecoder::InvertBuffer() {
573 DCHECK_EQ(m_Pitch, m_ScanlineBuf.size());
574 DCHECK_EQ(m_Pitch % 4, 0);
575 uint32_t* data = reinterpret_cast<uint32_t*>(m_ScanlineBuf.data());
576 for (size_t i = 0; i < m_ScanlineBuf.size() / 4; ++i)
577 data[i] = ~data[i];
578 }
579
580 } // namespace
581
582 // static
583 std::unique_ptr<ScanlineDecoder> FaxModule::CreateDecoder(
584 pdfium::span<const uint8_t> src_span,
585 int width,
586 int height,
587 int K,
588 bool EndOfLine,
589 bool EncodedByteAlign,
590 bool BlackIs1,
591 int Columns,
592 int Rows) {
593 int actual_width = Columns ? Columns : width;
594 int actual_height = Rows ? Rows : height;
595
596 // Reject invalid values.
597 if (actual_width <= 0 || actual_height <= 0)
598 return nullptr;
599
600 // Reject unreasonable large input.
601 if (actual_width > kFaxMaxImageDimension ||
602 actual_height > kFaxMaxImageDimension) {
603 return nullptr;
604 }
605
606 return std::make_unique<FaxDecoder>(src_span, actual_width, actual_height, K,
607 EndOfLine, EncodedByteAlign, BlackIs1);
608 }
609
610 // static
611 int FaxModule::FaxG4Decode(const uint8_t* src_buf,
612 uint32_t src_size,
613 int starting_bitpos,
614 int width,
615 int height,
616 int pitch,
617 uint8_t* dest_buf) {
618 DCHECK(pitch != 0);
619
620 DataVector<uint8_t> ref_buf(pitch, 0xff);
621 int bitpos = starting_bitpos;
622 for (int iRow = 0; iRow < height; ++iRow) {
623 uint8_t* line_buf = dest_buf + iRow * pitch;
624 memset(line_buf, 0xff, pitch);
625 FaxG4GetRow(src_buf, src_size << 3, &bitpos, line_buf, ref_buf, width);
626 memcpy(ref_buf.data(), line_buf, pitch);
627 }
628 return bitpos;
629 }
630
631 } // namespace chromium