Skip to content

Commit

Permalink
Pti and small png thumbnail renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob1 committed Mar 18, 2024
1 parent 3fb356e commit b073c1c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ std::unique_ptr<std::vector<char>> format::PixelsToPNG(PlaneAdapter<std::vector<
return std::make_unique<std::vector<char>>(std::move(output));
}

std::unique_ptr<std::vector<char>> format::PixelsToPTI(PlaneAdapter<std::vector<pixel>> const &input)
{
return Graphics::ptif_pack(input);
}

const static char hex[] = "0123456789ABCDEF";

ByteString format::URLEncode(ByteString source)
Expand Down
1 change: 1 addition & 0 deletions src/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace format
std::unique_ptr<std::vector<char>> PixelsToPNG(PlaneAdapter<std::vector<pixel>> const &);
std::unique_ptr<PlaneAdapter<std::vector<pixel_rgba>>> PixelsFromPNG(std::vector<char> const &);
std::unique_ptr<PlaneAdapter<std::vector<pixel>>> PixelsFromPNG(std::vector<char> const &, RGB<uint8_t> background);
std::unique_ptr<std::vector<char>> PixelsToPTI(PlaneAdapter<std::vector<pixel>> const &input);
void RenderTemperature(StringBuilder &sb, float temp, int scale);
float StringToTemperature(String str, int defaultScale);
}
24 changes: 21 additions & 3 deletions src/PowderToyRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ int main(int argc, char *argv[])
return 1;
}
auto inputFilename = ByteString(argv[1]);
auto outputFilename = ByteString(argv[2]) + ".png";
auto outputPrefix = ByteString(argv[2]);
ByteString ppmFilename = outputPrefix+".ppm";
ByteString ptiFilename = outputPrefix+".pti";
ByteString ptiSmallFilename = outputPrefix+"-small.pti";
ByteString pngFilename = outputPrefix+".png";
ByteString pngSmallFilename = outputPrefix+"-small.png";

auto simulationData = std::make_unique<SimulationData>();

Expand Down Expand Up @@ -72,6 +77,19 @@ int main(int argc, char *argv[])
ren->RenderBegin();
ren->RenderEnd();

if (auto data = ren->DumpFrame().ToPNG())
Platform::WriteFile(*data, outputFilename);
VideoBuffer screenBuffer = ren->DumpFrame();

if (auto data = screenBuffer.ToPNG())
Platform::WriteFile(*data, pngFilename);

if (auto data = screenBuffer.ToPTI())
Platform::WriteFile(*data, ptiFilename);

screenBuffer.Resize(1.0f/3.0f, true);

if (auto data = screenBuffer.ToPNG())
Platform::WriteFile(*data, pngSmallFilename);

if (auto data = screenBuffer.ToPTI())
Platform::WriteFile(*data, ptiSmallFilename);
}
55 changes: 55 additions & 0 deletions src/graphics/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "resampler/resampler.h"
#include "SimulationConfig.h"
#include "RasterDrawMethodsImpl.h"
#include "bzip2/bz2wrap.h"

VideoBuffer::VideoBuffer(Vec2<int> size):
video(size)
Expand Down Expand Up @@ -165,11 +166,65 @@ std::vector<char> VideoBuffer::ToPPM() const
return format::PixelsToPPM(video);
}

std::unique_ptr<std::vector<char>> VideoBuffer::ToPTI() const
{
return format::PixelsToPTI(video);
}

template struct RasterDrawMethods<VideoBuffer>;

Graphics::Graphics()
{}

std::unique_ptr<std::vector<char>> Graphics::ptif_pack(PlaneAdapter<std::vector<pixel>> const &input)
{
int w = input.Size().X, h = input.Size().Y;
unsigned char *red_chan = (unsigned char*)calloc(1, w*h);
unsigned char *green_chan = (unsigned char*)calloc(1, w*h);
unsigned char *blue_chan = (unsigned char*)calloc(1, w*h);
unsigned char *data = (unsigned char*)malloc(((w*h)*3)+8);

for (int cx = 0; cx < w; cx++)
{
for (int cy = 0; cy < h; cy++)
{
auto rgb = RGB<uint8_t>::Unpack(input[Vec2(cx, cy)]);
red_chan[w*(cy)+(cx)] = rgb.Red;
green_chan[w*(cy)+(cx)] = rgb.Green;
blue_chan[w*(cy)+(cx)] = rgb.Blue;
}
}

memcpy(data, red_chan, w*h);
memcpy(data+(w*h), green_chan, w*h);
memcpy(data+((w*h)*2), blue_chan, w*h);
free(red_chan);
free(green_chan);
free(blue_chan);

unsigned char *header = new unsigned char[8];
header[0] = 'P';
header[1] = 'T';
header[2] = 'i';
header[3] = 1;
header[4] = w;
header[5] = w>>8;
header[6] = h;
header[7] = h>>8;

std::vector<char> result;
if (BZ2WCompress(result, (char *)data, ((w*h)*3)+8, ((w*h)*3)+8) != BZ2WCompressOk)
{
free(data);
delete[] header;
return nullptr;
}
result.insert(result.begin(), header, header + 8);

free(data);
return std::make_unique<std::vector<char>>(std::move(result));
}

void Graphics::draw_icon(int x, int y, Icon icon, unsigned char alpha, bool invert)
{
y--;
Expand Down
3 changes: 3 additions & 0 deletions src/graphics/Graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class VideoBuffer: public RasterDrawMethods<VideoBuffer>
static std::unique_ptr<VideoBuffer> FromPNG(std::vector<char> const &);
std::unique_ptr<std::vector<char>> ToPNG() const;
std::vector<char> ToPPM() const;
std::unique_ptr<std::vector<char>> ToPTI() const;
};

class Graphics: public RasterDrawMethods<Graphics>
Expand Down Expand Up @@ -84,6 +85,8 @@ class Graphics: public RasterDrawMethods<Graphics>
};
static std::vector<RGB<uint8_t>> Gradient(std::vector<GradientStop> stops, int resolution);

static std::unique_ptr<std::vector<char>> ptif_pack(const PlaneAdapter<std::vector<pixel>> &input);

VideoBuffer DumpFrame();

void draw_icon(int x, int y, Icon icon, unsigned char alpha = 255, bool invert = false);
Expand Down

0 comments on commit b073c1c

Please sign in to comment.