173 lines
6.7 KiB
C++
173 lines
6.7 KiB
C++
#include "files_image_decode.h"
|
|
#include <windows.h>
|
|
#include <wincodec.h>
|
|
#include <webp/encode.h>
|
|
#include <array>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
|
|
namespace
|
|
{
|
|
namespace fs=std::filesystem;
|
|
using namespace ks::files;
|
|
|
|
void expect(bool condition,const char *message)
|
|
{
|
|
if(!condition) throw std::runtime_error(message);
|
|
}
|
|
|
|
template<class T>
|
|
struct Com
|
|
{
|
|
T *value{};
|
|
~Com(){if(value) value->Release();}
|
|
T *operator->() const{return value;}
|
|
};
|
|
|
|
void checked(HRESULT result)
|
|
{
|
|
expect(SUCCEEDED(result),"WIC fixture encoding failed.");
|
|
}
|
|
|
|
void encodeNative(const fs::path &path,REFGUID container)
|
|
{
|
|
Com<IWICImagingFactory> factory;
|
|
checked(CoCreateInstance(CLSID_WICImagingFactory,nullptr,CLSCTX_INPROC_SERVER,IID_PPV_ARGS(&factory.value)));
|
|
Com<IWICStream> stream;
|
|
checked(factory->CreateStream(&stream.value));
|
|
checked(stream->InitializeFromFilename(path.c_str(),GENERIC_WRITE));
|
|
Com<IWICBitmapEncoder> encoder;
|
|
checked(factory->CreateEncoder(container,nullptr,&encoder.value));
|
|
checked(encoder->Initialize(stream.value,WICBitmapEncoderNoCache));
|
|
Com<IWICBitmapFrameEncode> frame;
|
|
checked(encoder->CreateNewFrame(&frame.value,nullptr));
|
|
checked(frame->Initialize(nullptr));
|
|
checked(frame->SetSize(8,6));
|
|
auto pixelFormat=GUID_WICPixelFormat24bppBGR;
|
|
checked(frame->SetPixelFormat(&pixelFormat));
|
|
expect(pixelFormat==GUID_WICPixelFormat24bppBGR,"fixture pixel format");
|
|
std::array<BYTE,8*6*3> pixels{};
|
|
for(size_t i=0;i<pixels.size();i+=3)
|
|
{
|
|
pixels[i]=18;
|
|
pixels[i+1]=90;
|
|
pixels[i+2]=170;
|
|
}
|
|
checked(frame->WritePixels(6,8*3,pixels.size(),pixels.data()));
|
|
checked(frame->Commit());
|
|
checked(encoder->Commit());
|
|
}
|
|
|
|
struct Temp
|
|
{
|
|
fs::path path;
|
|
Temp()
|
|
{
|
|
GUID id{};
|
|
wchar_t name[40]{};
|
|
expect(SUCCEEDED(CoCreateGuid(&id)) && StringFromGUID2(id,name,40),"temporary file prefix");
|
|
path=fs::temp_directory_path()/(L"ks-test-image-"+std::wstring(name));
|
|
expect(fs::create_directory(path),"temporary image directory");
|
|
}
|
|
~Temp(){std::error_code error; fs::remove_all(path,error);}
|
|
};
|
|
|
|
void expectDecodeFailure(const fs::path &file,std::string_view text)
|
|
{
|
|
try {decodeImage(file,file);}
|
|
catch(const std::runtime_error &e)
|
|
{
|
|
expect(std::string_view(e.what()).find(text)!=std::string_view::npos,e.what());
|
|
return;
|
|
}
|
|
throw std::runtime_error("invalid image was decoded");
|
|
}
|
|
|
|
std::vector<uint8_t> animatedWebp(const uint8_t *encoded,size_t size)
|
|
{
|
|
expect(size>12,"WebP fixture RIFF body");
|
|
std::vector<uint8_t> bytes{'R','I','F','F',0,0,0,0,'W','E','B','P',
|
|
'V','P','8','X',10,0,0,0,0x12,0,0,0,0,0,0,0,0,0,
|
|
'A','N','I','M',6,0,0,0,0,0,0,0,0,0,
|
|
'A','N','M','F',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,0,2};
|
|
bytes.insert(bytes.end(),encoded+12,encoded+size);
|
|
const auto storeSize=[&](size_t offset,size_t value) {
|
|
for(unsigned i=0;i<4;++i) bytes[offset+i]=static_cast<uint8_t>(value>>(8*i));
|
|
};
|
|
storeSize(4,bytes.size()-8);
|
|
storeSize(48,16+size-12);
|
|
return bytes;
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
const auto apartment=CoInitializeEx(nullptr,COINIT_MULTITHREADED);
|
|
int result=0;
|
|
try
|
|
{
|
|
expect(SUCCEEDED(apartment),"initialize COM");
|
|
Temp temp;
|
|
for(const auto &[name,container]:std::array{
|
|
std::pair{L"знімок.PNG",&GUID_ContainerFormatPng},
|
|
std::pair{L"picture.jpeg",&GUID_ContainerFormatJpeg},
|
|
std::pair{L"picture.bmp",&GUID_ContainerFormatBmp}})
|
|
{
|
|
const auto path=temp.path/name;
|
|
expect(isImagePath(path),"supported native extension");
|
|
encodeNative(path,*container);
|
|
const auto decoded=decodeImage(path,path);
|
|
expect(decoded.width==8 && decoded.height==6 && decoded.pixels.size()==48,"native image dimensions");
|
|
const auto first=decoded.pixels.front();
|
|
expect((first>>24)==255 && ((first>>16)&255)>150,"native image color and opacity");
|
|
}
|
|
const auto gif=temp.path/L"picture.gif";
|
|
constexpr unsigned char gifData[]{0x47,0x49,0x46,0x38,0x39,0x61,1,0,1,0,0x80,0,0,
|
|
0,0,0,255,255,255,0x2c,0,0,0,0,1,0,1,0,0,2,2,0x44,1,0,0x3b};
|
|
std::ofstream(gif,std::ios::binary).write(reinterpret_cast<const char *>(gifData),sizeof(gifData));
|
|
const auto gifImage=decodeImage(gif,gif);
|
|
expect(gifImage.width==1 && gifImage.height==1 && gifImage.pixels.size()==1,"GIF first frame");
|
|
const auto webp=temp.path/L"picture.webp";
|
|
const uint8_t pixel[]{12,84,200,128};
|
|
uint8_t *encoded=nullptr;
|
|
const auto length=WebPEncodeLosslessBGRA(pixel,1,1,4,&encoded);
|
|
expect(length>0,"WebP fixture");
|
|
std::ofstream(webp,std::ios::binary).write(reinterpret_cast<const char *>(encoded),length);
|
|
auto animation=animatedWebp(encoded,length);
|
|
WebPFree(encoded);
|
|
const auto webpImage=decodeImage(webp,webp);
|
|
expect(webpImage.width==1 && webpImage.height==1 && webpImage.pixels.size()==1,"WebP dimensions");
|
|
const auto color=webpImage.pixels.front();
|
|
expect((color>>24)==128 && ((color>>16)&255)>=99 && ((color>>16)&255)<=101,"WebP premultiplied alpha");
|
|
const auto animated=temp.path/L"animated.webp";
|
|
std::ofstream(animated,std::ios::binary).write(reinterpret_cast<const char *>(animation.data()),animation.size());
|
|
const auto firstFrame=decodeImage(animated,animated);
|
|
expect(firstFrame.width==1 && firstFrame.height==1 && firstFrame.pixels.front()==color,"animated WebP first frame");
|
|
const auto hugeCanvas=temp.path/L"oversized-canvas.webp";
|
|
animation[24]=0x1f;
|
|
animation[25]=0x4e; // 20000 pixels, encoded as width minus one.
|
|
std::ofstream(hugeCanvas,std::ios::binary).write(reinterpret_cast<const char *>(animation.data()),animation.size());
|
|
expectDecodeFailure(hugeCanvas,"too large");
|
|
const auto corrupt=temp.path/L"corrupt.png";
|
|
std::ofstream(corrupt,std::ios::binary)<<"not an image";
|
|
expectDecodeFailure(corrupt,"damaged");
|
|
const auto large=temp.path/L"huge.bmp";
|
|
{
|
|
std::ofstream output(large,std::ios::binary);
|
|
output.seekp(imageSourceLimit);
|
|
output.put('\0');
|
|
}
|
|
expectDecodeFailure(large,"too large");
|
|
std::stop_source canceled;
|
|
canceled.request_stop();
|
|
bool stopped=false;
|
|
try{decodeImage(webp,webp,canceled.get_token());}
|
|
catch(const std::system_error &error){stopped=error.code().value()==ERROR_CANCELLED;}
|
|
expect(stopped,"canceled image load");
|
|
}
|
|
catch(const std::exception &error){std::cerr<<error.what()<<'\n';result=1;}
|
|
if(SUCCEEDED(apartment)) CoUninitialize();
|
|
return result;
|
|
}
|