31#include <webp/decode.h>
34 WebPBitstreamFeatures& features)
39 size_t lastSize = data.size();
40 data.resize(data.size() + 4096);
41 sal_Size nBytesRead =
stream.ReadBytes(data.data() + lastSize, 4096);
44 data.resize(lastSize + nBytesRead);
45 int status = WebPGetFeatures(data.data(), data.size(), &features);
46 if (status == VP8_STATUS_OK)
48 if (status == VP8_STATUS_NOT_ENOUGH_DATA)
58 if (!WebPInitDecoderConfig(&
config))
60 SAL_WARN(
"vcl.filter.webp",
"WebPInitDecoderConfig() failed");
64 std::vector<uint8_t> data;
68 const int& width =
config.input.width;
69 const int& height =
config.input.height;
70 const int& has_alpha =
config.input.has_alpha;
80 if (bSupportsBitmap32 && has_alpha)
95 std::vector<uint8_t> tmpRgbaData;
102 PixelMode pixelMode = PixelMode::SetPixel;
104 config.output.width = width;
105 config.output.height = height;
106 config.output.is_external_memory = 1;
107 if (bSupportsBitmap32 && has_alpha)
113 config.output.colorspace = MODE_rgbA;
114 pixelMode = PixelMode::DirectRead;
117 config.output.colorspace = MODE_bgrA;
118 pixelMode = PixelMode::DirectRead;
121 config.output.colorspace = MODE_Argb;
122 pixelMode = PixelMode::DirectRead;
125 config.output.colorspace = MODE_RGBA;
126 pixelMode = PixelMode::SetPixel;
137 config.output.colorspace = MODE_RGBA;
138 pixelMode = PixelMode::Split;
141 config.output.colorspace = MODE_BGRA;
142 pixelMode = PixelMode::Split;
145 config.output.colorspace = MODE_RGBA;
146 pixelMode = PixelMode::SetPixel;
155 config.output.colorspace = MODE_RGB;
156 pixelMode = PixelMode::DirectRead;
159 config.output.colorspace = MODE_BGR;
160 pixelMode = PixelMode::DirectRead;
163 config.output.colorspace = MODE_RGBA;
164 pixelMode = PixelMode::SetPixel;
169 if (pixelMode == PixelMode::DirectRead)
171 config.output.u.RGBA.rgba = access->GetBuffer();
172 config.output.u.RGBA.stride = access->GetScanlineSize();
173 config.output.u.RGBA.size = access->GetScanlineSize() * access->Height();
177 tmpRgbaData.resize(width * height * 4);
178 config.output.u.RGBA.rgba = tmpRgbaData.data();
179 config.output.u.RGBA.stride = width * 4;
180 config.output.u.RGBA.size = tmpRgbaData.size();
183 std::unique_ptr<WebPIDecoder,
decltype(&WebPIDelete)> decoder(WebPIDecode(
nullptr, 0, &
config),
190 int status = WebPIAppend(decoder.get(), data.data(), data.size());
191 if (status == VP8_STATUS_OK)
193 if (status != VP8_STATUS_SUSPENDED)
202 sal_Size nBytesRead =
stream.ReadBytes(data.data(), 4096);
209 data.resize(nBytesRead);
214 case PixelMode::DirectRead:
217 if (access->IsBottomUp())
219 std::vector<char> tmp;
220 const sal_uInt32 lineSize = access->GetScanlineSize();
221 tmp.resize(lineSize);
225 memcpy(tmp.data(), access->GetScanline(
y), lineSize);
226 memcpy(access->GetScanline(
y), access->GetScanline(otherY), lineSize);
227 memcpy(access->GetScanline(otherY), tmp.data(), lineSize);
232 case PixelMode::Split:
238 const unsigned char* src = tmpRgbaData.data() + width * 4 *
y;
239 unsigned char* dstB = access->GetScanline(
y);
240 unsigned char* dstA = accessAlpha->GetScanline(
y);
243 memcpy(dstB, src, 3);
244 *dstA = 255 - *(src + 3);
252 case PixelMode::SetPixel:
256 const unsigned char* src = tmpRgbaData.data() + width * 4 *
y;
272 const unsigned char* src = tmpRgbaData.data() + width * 4 *
y;
276 accessAlpha->SetPixelIndex(
y,
x, 255 -
a);
286 if (bSupportsBitmap32 && has_alpha)
291 graphic =
BitmapEx(bitmap, bitmapAlpha);
300 bool bRetValue =
readWebp(rStream, rGraphic);
308 std::vector<uint8_t> data;
309 WebPBitstreamFeatures features;
312 pixelSize =
Size(features.width, features.height);
313 bitsPerPixel = features.has_alpha ? 32 : 24;
314 hasAlpha = features.has_alpha;
ScanlineFormat RemoveScanline(ScanlineFormat nFormat)
bool supportsBitmap32() const
void SetError(ErrCode nErrorCode)
This template handles BitmapAccess the RAII way.
Reference< XOutputStream > stream
#define SVSTREAM_FILEFORMAT_ERROR
#define SAL_WARN(area, stream)
static bool readWebpInfo(SvStream &stream, std::vector< uint8_t > &data, WebPBitstreamFeatures &features)
static bool readWebp(SvStream &stream, Graphic &graphic)
bool ReadWebpInfo(SvStream &stream, Size &pixelSize, sal_uInt16 &bitsPerPixel, bool &hasAlpha)
bool ImportWebpGraphic(SvStream &rStream, Graphic &rGraphic)
ImplSVData * ImplGetSVData()