| 47 | } |
| 48 | |
| 49 | bool TGAEncoder::encode(const Image& image, IWriter& writer) const |
| 50 | { |
| 51 | if (not writer.isOpen()) |
| 52 | { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | const int32 width = image.width(); |
| 57 | const int32 height = image.height(); |
| 58 | const size_t stride_bytes = image.stride(); |
| 59 | const TGAHeader header = TGAHeader::Make(width, height); |
| 60 | |
| 61 | std::unique_ptr<uint8[]> line; |
| 62 | try |
| 63 | { |
| 64 | line = std::make_unique<uint8[]>(stride_bytes); |
| 65 | } |
| 66 | catch (const std::bad_alloc&) |
| 67 | { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | writer.write(header); |
| 72 | |
| 73 | const Color* pSrc = image[0]; |
| 74 | |
| 75 | for (int32 y = 0; y < height; ++y) |
| 76 | { |
| 77 | std::memcpy(line.get(), pSrc, stride_bytes); |
| 78 | |
| 79 | size_t index = 0; |
| 80 | |
| 81 | for (int32 x = 0; x < width; ++x) |
| 82 | { |
| 83 | std::swap(line[index], line[index + 2]); |
| 84 | index += 4; |
| 85 | } |
| 86 | |
| 87 | writer.write(line.get(), stride_bytes); |
| 88 | |
| 89 | pSrc += width; |
| 90 | } |
| 91 | |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | Blob TGAEncoder::encode(const Image& image) const |
| 96 | { |