| 19 | namespace impl |
| 20 | { |
| 21 | void impl_save_webp ( |
| 22 | const std::string& filename, |
| 23 | const uint8_t* data, |
| 24 | const int width, |
| 25 | const int height, |
| 26 | const int stride, |
| 27 | const float quality, |
| 28 | const webp_type type |
| 29 | ) |
| 30 | { |
| 31 | if (width > WEBP_MAX_DIMENSION || height > WEBP_MAX_DIMENSION) |
| 32 | throw image_save_error("Error while encoding " + filename + ". Bad picture dimensions: " |
| 33 | + std::to_string(width) + "x" + std::to_string(height) |
| 34 | + ". Maximum WebP width and height allowed is " |
| 35 | + std::to_string(WEBP_MAX_DIMENSION) + " pixels"); |
| 36 | |
| 37 | std::ofstream fout(filename, std::ios::binary); |
| 38 | if (!fout.good()) |
| 39 | throw image_save_error("Unable to open " + filename + " for writing."); |
| 40 | |
| 41 | uint8_t* output; |
| 42 | size_t output_size = 0; |
| 43 | switch (type) |
| 44 | { |
| 45 | case webp_type::rgb: |
| 46 | if (quality > 100) |
| 47 | output_size = WebPEncodeLosslessRGB(data, width, height, stride, &output); |
| 48 | else |
| 49 | output_size = WebPEncodeRGB(data, width, height, stride, quality, &output); |
| 50 | break; |
| 51 | case webp_type::rgba: |
| 52 | if (quality > 100) |
| 53 | output_size = WebPEncodeLosslessRGBA(data, width, height, stride, &output); |
| 54 | else |
| 55 | output_size = WebPEncodeRGBA(data, width, height, stride, quality, &output); |
| 56 | break; |
| 57 | case webp_type::bgr: |
| 58 | if (quality > 100) |
| 59 | output_size = WebPEncodeLosslessBGR(data, width, height, stride, &output); |
| 60 | else |
| 61 | output_size = WebPEncodeBGR(data, width, height, stride, quality, &output); |
| 62 | break; |
| 63 | case webp_type::bgra: |
| 64 | if (quality > 100) |
| 65 | output_size = WebPEncodeLosslessBGRA(data, width, height, stride, &output); |
| 66 | else |
| 67 | output_size = WebPEncodeBGRA(data, width, height, stride, quality, &output); |
| 68 | break; |
| 69 | default: |
| 70 | throw image_save_error("Invalid WebP color type"); |
| 71 | } |
| 72 | |
| 73 | if (output_size > 0) |
| 74 | { |
| 75 | fout.write(reinterpret_cast<char*>(output), output_size); |
| 76 | if (!fout.good()) |
| 77 | { |
| 78 | WebPFree(output); |
no test coverage detected