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 <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "core/fxcodec/codec/codec_int.h" | 10 #include "core/fxcodec/codec/codec_int.h" |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 ++(*bitpos); | 266 ++(*bitpos); |
267 int next_off = ins_off + ins * 3; | 267 int next_off = ins_off + ins * 3; |
268 for (; ins_off < next_off; ins_off += 3) { | 268 for (; ins_off < next_off; ins_off += 3) { |
269 if (ins_array[ins_off] == code) { | 269 if (ins_array[ins_off] == code) { |
270 return ins_array[ins_off + 1] + ins_array[ins_off + 2] * 256; | 270 return ins_array[ins_off + 1] + ins_array[ins_off + 2] * 256; |
271 } | 271 } |
272 } | 272 } |
273 } | 273 } |
274 } | 274 } |
275 | 275 |
276 FX_BOOL FaxG4GetRow(const uint8_t* src_buf, | 276 bool FaxG4GetRow(const uint8_t* src_buf, |
277 int bitsize, | 277 int bitsize, |
278 int* bitpos, | 278 int* bitpos, |
279 uint8_t* dest_buf, | 279 uint8_t* dest_buf, |
280 const std::vector<uint8_t>& ref_buf, | 280 const std::vector<uint8_t>& ref_buf, |
281 int columns) { | 281 int columns) { |
282 int a0 = -1; | 282 int a0 = -1; |
283 bool a0color = true; | 283 bool a0color = true; |
284 while (1) { | 284 while (1) { |
285 if (*bitpos >= bitsize) | 285 if (*bitpos >= bitsize) |
286 return FALSE; | 286 return false; |
287 | 287 |
288 int a1; | 288 int a1; |
289 int a2; | 289 int a2; |
290 int b1; | 290 int b1; |
291 int b2; | 291 int b2; |
292 FaxG4FindB1B2(ref_buf, columns, a0, a0color, &b1, &b2); | 292 FaxG4FindB1B2(ref_buf, columns, a0, a0color, &b1, &b2); |
293 | 293 |
294 int v_delta = 0; | 294 int v_delta = 0; |
295 if (!NextBit(src_buf, bitpos)) { | 295 if (!NextBit(src_buf, bitpos)) { |
296 if (*bitpos >= bitsize) | 296 if (*bitpos >= bitsize) |
297 return FALSE; | 297 return false; |
298 | 298 |
299 FX_BOOL bit1 = NextBit(src_buf, bitpos); | 299 bool bit1 = NextBit(src_buf, bitpos); |
300 if (*bitpos >= bitsize) | 300 if (*bitpos >= bitsize) |
301 return FALSE; | 301 return false; |
302 | 302 |
303 FX_BOOL bit2 = NextBit(src_buf, bitpos); | 303 bool bit2 = NextBit(src_buf, bitpos); |
304 if (bit1) { | 304 if (bit1) { |
305 v_delta = bit2 ? 1 : -1; | 305 v_delta = bit2 ? 1 : -1; |
306 } else if (bit2) { | 306 } else if (bit2) { |
307 int run_len1 = 0; | 307 int run_len1 = 0; |
308 while (1) { | 308 while (1) { |
309 int run = FaxGetRun(a0color ? FaxWhiteRunIns : FaxBlackRunIns, | 309 int run = FaxGetRun(a0color ? FaxWhiteRunIns : FaxBlackRunIns, |
310 src_buf, bitpos, bitsize); | 310 src_buf, bitpos, bitsize); |
311 run_len1 += run; | 311 run_len1 += run; |
312 if (run < 64) { | 312 if (run < 64) { |
313 break; | 313 break; |
(...skipping 16 matching lines...) Expand all Loading... |
330 } | 330 } |
331 } | 331 } |
332 a2 = a1 + run_len2; | 332 a2 = a1 + run_len2; |
333 if (a0color) | 333 if (a0color) |
334 FaxFillBits(dest_buf, columns, a1, a2); | 334 FaxFillBits(dest_buf, columns, a1, a2); |
335 | 335 |
336 a0 = a2; | 336 a0 = a2; |
337 if (a0 < columns) | 337 if (a0 < columns) |
338 continue; | 338 continue; |
339 | 339 |
340 return TRUE; | 340 return true; |
341 } else { | 341 } else { |
342 if (*bitpos >= bitsize) | 342 if (*bitpos >= bitsize) |
343 return FALSE; | 343 return false; |
344 | 344 |
345 if (NextBit(src_buf, bitpos)) { | 345 if (NextBit(src_buf, bitpos)) { |
346 if (!a0color) | 346 if (!a0color) |
347 FaxFillBits(dest_buf, columns, a0, b2); | 347 FaxFillBits(dest_buf, columns, a0, b2); |
348 | 348 |
349 if (b2 >= columns) | 349 if (b2 >= columns) |
350 return TRUE; | 350 return true; |
351 | 351 |
352 a0 = b2; | 352 a0 = b2; |
353 continue; | 353 continue; |
354 } | 354 } |
355 | 355 |
356 if (*bitpos >= bitsize) | 356 if (*bitpos >= bitsize) |
357 return FALSE; | 357 return false; |
358 | 358 |
359 FX_BOOL next_bit1 = NextBit(src_buf, bitpos); | 359 bool next_bit1 = NextBit(src_buf, bitpos); |
360 if (*bitpos >= bitsize) | 360 if (*bitpos >= bitsize) |
361 return FALSE; | 361 return false; |
362 | 362 |
363 FX_BOOL next_bit2 = NextBit(src_buf, bitpos); | 363 bool next_bit2 = NextBit(src_buf, bitpos); |
364 if (next_bit1) { | 364 if (next_bit1) { |
365 v_delta = next_bit2 ? 2 : -2; | 365 v_delta = next_bit2 ? 2 : -2; |
366 } else if (next_bit2) { | 366 } else if (next_bit2) { |
367 if (*bitpos >= bitsize) | 367 if (*bitpos >= bitsize) |
368 return FALSE; | 368 return false; |
369 | 369 |
370 v_delta = NextBit(src_buf, bitpos) ? 3 : -3; | 370 v_delta = NextBit(src_buf, bitpos) ? 3 : -3; |
371 } else { | 371 } else { |
372 if (*bitpos >= bitsize) | 372 if (*bitpos >= bitsize) |
373 return FALSE; | 373 return false; |
374 | 374 |
375 if (NextBit(src_buf, bitpos)) { | 375 if (NextBit(src_buf, bitpos)) { |
376 *bitpos += 3; | 376 *bitpos += 3; |
377 continue; | 377 continue; |
378 } | 378 } |
379 *bitpos += 5; | 379 *bitpos += 5; |
380 return TRUE; | 380 return true; |
381 } | 381 } |
382 } | 382 } |
383 } | 383 } |
384 a1 = b1 + v_delta; | 384 a1 = b1 + v_delta; |
385 if (!a0color) | 385 if (!a0color) |
386 FaxFillBits(dest_buf, columns, a0, a1); | 386 FaxFillBits(dest_buf, columns, a0, a1); |
387 | 387 |
388 if (a1 >= columns) | 388 if (a1 >= columns) |
389 return TRUE; | 389 return true; |
390 | 390 |
391 // The position of picture element must be monotonic increasing. | 391 // The position of picture element must be monotonic increasing. |
392 if (a0 >= a1) | 392 if (a0 >= a1) |
393 return FALSE; | 393 return false; |
394 | 394 |
395 a0 = a1; | 395 a0 = a1; |
396 a0color = !a0color; | 396 a0color = !a0color; |
397 } | 397 } |
398 } | 398 } |
399 | 399 |
400 FX_BOOL FaxSkipEOL(const uint8_t* src_buf, int bitsize, int* bitpos) { | 400 bool FaxSkipEOL(const uint8_t* src_buf, int bitsize, int* bitpos) { |
401 int startbit = *bitpos; | 401 int startbit = *bitpos; |
402 while (*bitpos < bitsize) { | 402 while (*bitpos < bitsize) { |
403 if (!NextBit(src_buf, bitpos)) | 403 if (!NextBit(src_buf, bitpos)) |
404 continue; | 404 continue; |
405 if (*bitpos - startbit <= 11) | 405 if (*bitpos - startbit <= 11) |
406 *bitpos = startbit; | 406 *bitpos = startbit; |
407 return TRUE; | 407 return true; |
408 } | 408 } |
409 return FALSE; | 409 return false; |
410 } | 410 } |
411 | 411 |
412 FX_BOOL FaxGet1DLine(const uint8_t* src_buf, | 412 bool FaxGet1DLine(const uint8_t* src_buf, |
413 int bitsize, | 413 int bitsize, |
414 int* bitpos, | 414 int* bitpos, |
415 std::vector<uint8_t>* dest_buf, | 415 std::vector<uint8_t>* dest_buf, |
416 int columns) { | 416 int columns) { |
417 bool color = true; | 417 bool color = true; |
418 int startpos = 0; | 418 int startpos = 0; |
419 while (1) { | 419 while (1) { |
420 if (*bitpos >= bitsize) | 420 if (*bitpos >= bitsize) |
421 return FALSE; | 421 return false; |
422 | 422 |
423 int run_len = 0; | 423 int run_len = 0; |
424 while (1) { | 424 while (1) { |
425 int run = FaxGetRun(color ? FaxWhiteRunIns : FaxBlackRunIns, src_buf, | 425 int run = FaxGetRun(color ? FaxWhiteRunIns : FaxBlackRunIns, src_buf, |
426 bitpos, bitsize); | 426 bitpos, bitsize); |
427 if (run < 0) { | 427 if (run < 0) { |
428 while (*bitpos < bitsize) { | 428 while (*bitpos < bitsize) { |
429 if (NextBit(src_buf, bitpos)) | 429 if (NextBit(src_buf, bitpos)) |
430 return TRUE; | 430 return true; |
431 } | 431 } |
432 return FALSE; | 432 return false; |
433 } | 433 } |
434 run_len += run; | 434 run_len += run; |
435 if (run < 64) { | 435 if (run < 64) { |
436 break; | 436 break; |
437 } | 437 } |
438 } | 438 } |
439 if (!color) | 439 if (!color) |
440 FaxFillBits(dest_buf->data(), columns, startpos, startpos + run_len); | 440 FaxFillBits(dest_buf->data(), columns, startpos, startpos + run_len); |
441 | 441 |
442 startpos += run_len; | 442 startpos += run_len; |
443 if (startpos >= columns) | 443 if (startpos >= columns) |
444 break; | 444 break; |
445 | 445 |
446 color = !color; | 446 color = !color; |
447 } | 447 } |
448 return TRUE; | 448 return true; |
449 } | 449 } |
450 | 450 |
451 } // namespace | 451 } // namespace |
452 | 452 |
453 class CCodec_FaxDecoder : public CCodec_ScanlineDecoder { | 453 class CCodec_FaxDecoder : public CCodec_ScanlineDecoder { |
454 public: | 454 public: |
455 CCodec_FaxDecoder(const uint8_t* src_buf, | 455 CCodec_FaxDecoder(const uint8_t* src_buf, |
456 uint32_t src_size, | 456 uint32_t src_size, |
457 int width, | 457 int width, |
458 int height, | 458 int height, |
459 uint32_t pitch, | 459 uint32_t pitch, |
460 int K, | 460 int K, |
461 bool EndOfLine, | 461 bool EndOfLine, |
462 bool EncodedByteAlign, | 462 bool EncodedByteAlign, |
463 bool BlackIs1); | 463 bool BlackIs1); |
464 ~CCodec_FaxDecoder() override; | 464 ~CCodec_FaxDecoder() override; |
465 | 465 |
466 // CCodec_ScanlineDecoder | 466 // CCodec_ScanlineDecoder |
467 FX_BOOL v_Rewind() override; | 467 bool v_Rewind() override; |
468 uint8_t* v_GetNextLine() override; | 468 uint8_t* v_GetNextLine() override; |
469 uint32_t GetSrcOffset() override; | 469 uint32_t GetSrcOffset() override; |
470 | 470 |
471 private: | 471 private: |
472 const int m_Encoding; | 472 const int m_Encoding; |
473 int m_bitpos; | 473 int m_bitpos; |
474 bool m_bByteAlign; | 474 bool m_bByteAlign; |
475 const bool m_bEndOfLine; | 475 const bool m_bEndOfLine; |
476 const bool m_bBlack; | 476 const bool m_bBlack; |
477 const uint32_t m_SrcSize; | 477 const uint32_t m_SrcSize; |
(...skipping 17 matching lines...) Expand all Loading... |
495 m_bByteAlign(EncodedByteAlign), | 495 m_bByteAlign(EncodedByteAlign), |
496 m_bEndOfLine(EndOfLine), | 496 m_bEndOfLine(EndOfLine), |
497 m_bBlack(BlackIs1), | 497 m_bBlack(BlackIs1), |
498 m_SrcSize(src_size), | 498 m_SrcSize(src_size), |
499 m_pSrcBuf(src_buf), | 499 m_pSrcBuf(src_buf), |
500 m_ScanlineBuf(pitch), | 500 m_ScanlineBuf(pitch), |
501 m_RefBuf(pitch) {} | 501 m_RefBuf(pitch) {} |
502 | 502 |
503 CCodec_FaxDecoder::~CCodec_FaxDecoder() {} | 503 CCodec_FaxDecoder::~CCodec_FaxDecoder() {} |
504 | 504 |
505 FX_BOOL CCodec_FaxDecoder::v_Rewind() { | 505 bool CCodec_FaxDecoder::v_Rewind() { |
506 FXSYS_memset(m_RefBuf.data(), 0xff, m_RefBuf.size()); | 506 FXSYS_memset(m_RefBuf.data(), 0xff, m_RefBuf.size()); |
507 m_bitpos = 0; | 507 m_bitpos = 0; |
508 return TRUE; | 508 return true; |
509 } | 509 } |
510 | 510 |
511 uint8_t* CCodec_FaxDecoder::v_GetNextLine() { | 511 uint8_t* CCodec_FaxDecoder::v_GetNextLine() { |
512 int bitsize = m_SrcSize * 8; | 512 int bitsize = m_SrcSize * 8; |
513 FaxSkipEOL(m_pSrcBuf, bitsize, &m_bitpos); | 513 FaxSkipEOL(m_pSrcBuf, bitsize, &m_bitpos); |
514 if (m_bitpos >= bitsize) | 514 if (m_bitpos >= bitsize) |
515 return nullptr; | 515 return nullptr; |
516 | 516 |
517 FXSYS_memset(m_ScanlineBuf.data(), 0xff, m_ScanlineBuf.size()); | 517 FXSYS_memset(m_ScanlineBuf.data(), 0xff, m_ScanlineBuf.size()); |
518 if (m_Encoding < 0) { | 518 if (m_Encoding < 0) { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 return nullptr; | 598 return nullptr; |
599 | 599 |
600 // Reject unreasonable large input. | 600 // Reject unreasonable large input. |
601 if (actual_width > kMaxImageDimension || actual_height > kMaxImageDimension) | 601 if (actual_width > kMaxImageDimension || actual_height > kMaxImageDimension) |
602 return nullptr; | 602 return nullptr; |
603 | 603 |
604 uint32_t pitch = (static_cast<uint32_t>(actual_width) + 31) / 32 * 4; | 604 uint32_t pitch = (static_cast<uint32_t>(actual_width) + 31) / 32 * 4; |
605 return new CCodec_FaxDecoder(src_buf, src_size, actual_width, actual_height, | 605 return new CCodec_FaxDecoder(src_buf, src_size, actual_width, actual_height, |
606 pitch, K, EndOfLine, EncodedByteAlign, BlackIs1); | 606 pitch, K, EndOfLine, EncodedByteAlign, BlackIs1); |
607 } | 607 } |
OLD | NEW |