|
| 1 | +#include "webp_codec.h" |
| 2 | +#include "webp/decode.h" |
| 3 | +#include "webp/encode.h" |
| 4 | +#include <cstdint> |
| 5 | +#include <stdexcept> |
| 6 | + |
| 7 | +/* |
| 8 | + * WEBPEncoder |
| 9 | + */ |
| 10 | + |
| 11 | +libench::WEBPEncoder::WEBPEncoder() { |
| 12 | + WebPMemoryWriterInit(&this->writer_); |
| 13 | +}; |
| 14 | + |
| 15 | +libench::WEBPEncoder::~WEBPEncoder() { |
| 16 | + WebPMemoryWriterClear(&this->writer_); |
| 17 | +}; |
| 18 | + |
| 19 | +libench::CodestreamContext libench::WEBPEncoder::encodeRGB8(const ImageContext &image) { |
| 20 | + return this->encode8(image); |
| 21 | +} |
| 22 | + |
| 23 | +libench::CodestreamContext libench::WEBPEncoder::encodeRGBA8(const ImageContext &image) { |
| 24 | + return this->encode8(image); |
| 25 | +} |
| 26 | + |
| 27 | +libench::CodestreamContext libench::WEBPEncoder::encode8(const ImageContext &image) { |
| 28 | + WebPMemoryWriterClear(&this->writer_); |
| 29 | + |
| 30 | + WebPConfig config; |
| 31 | + WebPPicture pic; |
| 32 | + const int level = 6; // 0 (faster) - 9 (slower) |
| 33 | + if (!WebPConfigInit(&config) || !WebPConfigLosslessPreset(&config, level) || !WebPPictureInit(&pic)) |
| 34 | + throw std::runtime_error("WEBP encode failed"); |
| 35 | + |
| 36 | + const int num_comps = image.format.comps.num_comps; |
| 37 | + const int rgb_stride = image.width * num_comps; |
| 38 | + pic.width = image.width; |
| 39 | + pic.height = image.height; |
| 40 | + pic.use_argb = 1; |
| 41 | + int ret = num_comps == 3 ? |
| 42 | + WebPPictureImportRGB(&pic, image.planes8[0], rgb_stride) : |
| 43 | + WebPPictureImportRGBA(&pic, image.planes8[0], rgb_stride); |
| 44 | + if (!ret) { |
| 45 | + WebPPictureFree(&pic); |
| 46 | + throw std::runtime_error("WEBP encode failed"); |
| 47 | + } |
| 48 | + |
| 49 | + // Retain pixel values in fully transparent areas. The default will modify |
| 50 | + // the (invisible) pixels to improve compression. |
| 51 | + config.exact = 1; |
| 52 | + |
| 53 | + pic.writer = WebPMemoryWrite; |
| 54 | + pic.custom_ptr = &this->writer_; |
| 55 | + |
| 56 | + if (!WebPEncode(&config, &pic)) { |
| 57 | + WebPPictureFree(&pic); |
| 58 | + WebPMemoryWriterClear(&this->writer_); |
| 59 | + throw std::runtime_error("WEBP encode failed"); |
| 60 | + } |
| 61 | + |
| 62 | + CodestreamContext cs; |
| 63 | + cs.codestream = this->writer_.mem; |
| 64 | + cs.size = this->writer_.size; |
| 65 | + return cs; |
| 66 | +} |
| 67 | + |
| 68 | +/* |
| 69 | + * WEBPDecoder |
| 70 | + */ |
| 71 | + |
| 72 | +libench::WEBPDecoder::WEBPDecoder() { |
| 73 | +}; |
| 74 | + |
| 75 | +libench::WEBPDecoder::~WEBPDecoder() { |
| 76 | + WebPFree(this->image_.planes8[0]); |
| 77 | +}; |
| 78 | + |
| 79 | +libench::ImageContext libench::WEBPDecoder::decodeRGB8(const CodestreamContext& cs) { |
| 80 | + return this->decode8(cs, 3); |
| 81 | +} |
| 82 | + |
| 83 | +libench::ImageContext libench::WEBPDecoder::decodeRGBA8(const CodestreamContext& cs) { |
| 84 | + return this->decode8(cs, 4); |
| 85 | +} |
| 86 | + |
| 87 | +libench::ImageContext libench::WEBPDecoder::decode8(const CodestreamContext& cs, uint8_t num_comps) { |
| 88 | + WebPFree(this->image_.planes8[0]); |
| 89 | + |
| 90 | + this->image_.format = num_comps == 3 ? libench::ImageFormat::RGB8 : libench::ImageFormat::RGBA8; |
| 91 | + |
| 92 | + int width, height; |
| 93 | + this->image_.planes8[0] = num_comps == 3 ? |
| 94 | + WebPDecodeRGB(cs.codestream, cs.size, &width, &height) : |
| 95 | + WebPDecodeRGBA(cs.codestream, cs.size, &width, &height); |
| 96 | + |
| 97 | + if (!this->image_.planes8[0]) |
| 98 | + throw std::runtime_error("WEBP decode failed"); |
| 99 | + |
| 100 | + this->image_.width = static_cast<uint32_t>(width); |
| 101 | + this->image_.height = static_cast<uint32_t>(height); |
| 102 | + |
| 103 | + return this->image_; |
| 104 | +} |
0 commit comments