| 47 | } |
| 48 | |
| 49 | bool BMPEncoder::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 uint32 stride_bytes = (width * 3) + (width % 4); |
| 59 | const uint32 size_bytes = (stride_bytes * height); |
| 60 | const BMPHeader header = BMPHeader::Make(width, height, size_bytes); |
| 61 | |
| 62 | std::unique_ptr<uint8[]> line; |
| 63 | try |
| 64 | { |
| 65 | line = std::make_unique<uint8[]>(stride_bytes); |
| 66 | } |
| 67 | catch (const std::bad_alloc&) |
| 68 | { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | writer.write(header); |
| 73 | |
| 74 | const Color* pSrcLine = image[height - 1]; |
| 75 | |
| 76 | for (int32 y = 0; y < height; ++y) |
| 77 | { |
| 78 | uint8* pDst = line.get(); |
| 79 | const Color* pSrc = pSrcLine; |
| 80 | |
| 81 | for (int32 x = 0; x < width; ++x) |
| 82 | { |
| 83 | *pDst++ = pSrc->b; |
| 84 | *pDst++ = pSrc->g; |
| 85 | *pDst++ = pSrc->r; |
| 86 | ++pSrc; |
| 87 | } |
| 88 | |
| 89 | writer.write(line.get(), stride_bytes); |
| 90 | pSrcLine -= width; |
| 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | Blob BMPEncoder::encode(const Image& image) const |
| 97 | { |