------------------------------------------------------------------------------
| 579 | |
| 580 | //------------------------------------------------------------------------------ |
| 581 | bool vtkHDRReader::ReadLineRLE(vtkResourceStream* stream, unsigned char* lineBufferPtr) |
| 582 | { |
| 583 | // A line in RLE is sorted by channels, ie. it begins by all the red, then green, blue, and |
| 584 | // exponent It means that we need to read all the line even if we have an smaller extent |
| 585 | int width = this->GetWidth(); |
| 586 | int count; |
| 587 | // The data for each encoded channel |
| 588 | unsigned char buffer[2]; |
| 589 | for (int i = 0; i < 4; ++i) |
| 590 | { |
| 591 | unsigned char* ptrEnd = lineBufferPtr + width; |
| 592 | while (lineBufferPtr < ptrEnd) |
| 593 | { |
| 594 | std::size_t size = sizeof(unsigned char) * 2; |
| 595 | if (stream->Read(buffer, size) != size) |
| 596 | { |
| 597 | vtkErrorMacro("Unable to read as expected"); |
| 598 | return false; |
| 599 | } |
| 600 | |
| 601 | if (buffer[0] > 128) |
| 602 | { |
| 603 | // A run of the same value |
| 604 | count = buffer[0] - 128; |
| 605 | if ((count == 0) || (count > ptrEnd - lineBufferPtr)) |
| 606 | { |
| 607 | return false; |
| 608 | } |
| 609 | |
| 610 | // Copy the same value in the lineBuffer |
| 611 | std::fill(lineBufferPtr, lineBufferPtr + count, buffer[1]); |
| 612 | lineBufferPtr += count; |
| 613 | } |
| 614 | else |
| 615 | { |
| 616 | // A non run |
| 617 | count = buffer[0]; |
| 618 | if ((count == 0) || (count > ptrEnd - lineBufferPtr)) |
| 619 | { |
| 620 | return false; |
| 621 | } |
| 622 | |
| 623 | *lineBufferPtr++ = buffer[1]; |
| 624 | // Read count bytes into output |
| 625 | if (--count > 0) |
| 626 | { |
| 627 | |
| 628 | size = sizeof(unsigned char) * count; |
| 629 | if (stream->Read(lineBufferPtr, size) != size) |
| 630 | { |
| 631 | vtkErrorMacro("Unable to read as expected"); |
| 632 | return false; |
| 633 | } |
| 634 | lineBufferPtr += count; |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | } |
no test coverage detected