Initial WIC image writing.

This commit is contained in:
jmsgrogan 2023-01-11 17:34:40 +00:00
parent 53a9f7bd15
commit 8c814ce89f
6 changed files with 72 additions and 3 deletions

View file

@ -1,6 +1,18 @@
#pragma once #pragma once
#include <filesystem>
using Path = std::filesystem::path;
class Image;
class PlatformImageWriter class PlatformImageWriter
{ {
public:
enum class ImgFormat
{
PNG
};
virtual void write(const Path& path, Image* image, ImgFormat format = ImgFormat::PNG) = 0;
}; };

View file

@ -15,6 +15,10 @@
#include "ByteUtils.h" #include "ByteUtils.h"
#ifdef _WIN32
#include "Win32WicImageWriter.h"
#endif
#include <iostream> #include <iostream>
PngWriter::PngWriter() PngWriter::PngWriter()
@ -157,8 +161,20 @@ void PngWriter::writeDataChunks(const BufferBitStream& buffer)
} }
} }
void PngWriter::writePlatform(Image* image)
{
mPlatformWriter = std::make_unique<Win32WicImageWriter>();
mPlatformWriter->write(mPath, image, PlatformImageWriter::ImgFormat::PNG);
}
void PngWriter::write(Image* image) void PngWriter::write(Image* image)
{ {
if (image->getPlatformImage())
{
writePlatform(image);
return;
}
if (!mPath.empty()) if (!mPath.empty())
{ {
mWorkingFile = std::make_unique<File>(mPath); mWorkingFile = std::make_unique<File>(mPath);

View file

@ -33,6 +33,8 @@ public:
void write(Image* image); void write(Image* image);
private: private:
void writePlatform(Image* image);
void writeSignature(); void writeSignature();
void writeHeader(); void writeHeader();

View file

@ -1,2 +1,40 @@
#include "Win32WicImageWriter.h" #include "Win32WicImageWriter.h"
#include "Image.h"
#include "Win32WicInterface.h"
#include "Win32WicImage.h"
#include "StringUtils.h"
#include <wincodec.h>
#include <wrl.h>
void Win32WicImageWriter::write(const Path& path, Image* image, ImgFormat format )
{
Win32WicInterface wic_interface;
Microsoft::WRL::ComPtr<IWICStream> pStream;
auto hr = wic_interface.getFactory()->CreateStream(&pStream);
hr = pStream->InitializeFromFilename(path.wstring().c_str(), GENERIC_WRITE);
Microsoft::WRL::ComPtr<IWICBitmapEncoder> pEncoder;
hr = wic_interface.getFactory()->CreateEncoder(GUID_ContainerFormatPng, NULL, &pEncoder);
hr = pEncoder->Initialize(pStream.Get(), WICBitmapEncoderNoCache);
Microsoft::WRL::ComPtr<IWICBitmapFrameEncode> pFrameEncode;
hr = pEncoder->CreateNewFrame(&pFrameEncode, NULL);
hr = pFrameEncode->Initialize(NULL);
hr = pFrameEncode->SetSize(image->getWidth(), image->getHeight());
WICPixelFormatGUID out_format = GUID_WICPixelFormatDontCare;
hr = pFrameEncode->SetPixelFormat(&out_format);
hr = pFrameEncode->WriteSource(dynamic_cast<Win32WicImage*>(image->getPlatformImage())->getBitmap(), NULL);
hr = pFrameEncode->Commit();
hr = pEncoder->Commit();
}

View file

@ -2,7 +2,8 @@
#include "PlatformImageWriter.h" #include "PlatformImageWriter.h"
class Win32WicImageWriter class Win32WicImageWriter : public PlatformImageWriter
{ {
public:
void write(const Path& path, Image* image, ImgFormat format = ImgFormat::PNG) override;
}; };

View file

@ -33,5 +33,5 @@ TEST_CASE(TestD2dOffScreenRendering, "graphics")
PngWriter writer; PngWriter writer;
writer.setPath(TestUtils::getTestOutputDir(__FILE__) / "out.png"); writer.setPath(TestUtils::getTestOutputDir(__FILE__) / "out.png");
//writer.write(image); writer.write(image);
}; };