Skip to content

Commit

Permalink
Fixed encoder and added sum more images
Browse files Browse the repository at this point in the history
  • Loading branch information
rafa-br34 committed Apr 27, 2024
1 parent 50cc3b7 commit f6afb74
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 32 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ASSETS/RLRRLRRLRRLR_RLC_30720_30720_666666666.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 15 additions & 25 deletions SOURCE/Encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ namespace Encoding {
};

typedef RGBA32(* ColorDescriptor)(size_t);
typedef void(* EncoderCallback)(const uint8_t*, size_t, const Vector2<int>&, unsigned int);

class PaletteManager {
private:
Expand Down Expand Up @@ -120,13 +119,9 @@ namespace Encoding {
~ThreadManager() { WaitJobs(); }
};

enum class PNGFormat : uint8_t {
enum class ImageFormat : uint8_t {
PNG_PALETTE,
PNG_GRAYSCALE,
_PNG_END,

RAW_U8,
_RAW_END
};

class EncoderState {
Expand All @@ -139,8 +134,8 @@ namespace Encoding {
LodePNGColorType ColorType;

switch (Format) {
case DataFormat::PNG_PALETTE: ColorType = LCT_PALETTE; break;
case DataFormat::PNG_GRAYSCALE: ColorType = LCT_GREY; break;
case ImageFormat::PNG_PALETTE: ColorType = LCT_PALETTE; break;
case ImageFormat::PNG_GRAYSCALE: ColorType = LCT_GREY; break;

default: {
DEBUG_PRINT("Unknown data format %d, ColorType will be set to LCT_GREY\n", (int)Format);
Expand All @@ -157,7 +152,7 @@ namespace Encoding {

State.encoder.auto_convert = 0;

if (Format != DataFormat::PNG_PALETTE) return State;
if (Format != ImageFormat::PNG_PALETTE) return State;

// DEBUG_PRINT("Setting up palette with %d colors (%d bits per pixel):\n", (int)StateCount, (int)BitDepth);
Palette.ResizePalette(StateCount);
Expand All @@ -175,34 +170,29 @@ namespace Encoding {

template<typename Function, typename CellType, typename SizeType>
void m_EncodeBuffer(const CellType* Grid, const Vector2<SizeType>& Size, size_t StateCount, Function Callback) {
if (DataFormat::_PNG_END > Format) {
std::vector<uint8_t> Buffer = {};
lodepng::State State = m_SetupEncoderState(StateCount, 8);

unsigned int Result = lodepng::encode(Buffer, (const unsigned char*)Grid, (unsigned int)Size.X, (unsigned int)Size.Y, State);
std::vector<uint8_t> ImageData = {};
lodepng::State State = m_SetupEncoderState(StateCount, 8);

unsigned int Result = lodepng::encode(ImageData, (const unsigned char*)Grid, (unsigned int)Size.X, (unsigned int)Size.Y, State);

ASSERT_MSG(Result == 0, "lodepng::encode -> %d\n", Result);
ASSERT_MSG(Result == 0, "lodepng::encode -> %d\n", Result);

Callback(Buffer.data(), Buffer.size(), Vector2<int>(Size.X, Size.Y), Result);
}
else if (DataFormat::_RAW_END > Format) {
Callback(Grid, (size_t)Size.X * (size_t)Size.Y, Vector2<int>(Size.X, Size.Y), 0);
}
Callback(ImageData, Vector2<int>(Size.X, Size.Y), Result);
}


public:
PaletteManager Palette = {};
ThreadManager Threads = {};

DataFormat Format = DataFormat::PNG_PALETTE;
ImageFormat Format = ImageFormat::PNG_PALETTE;

template<typename Function=EncoderCallback, typename CellType, typename SizeType>
template<typename Function, typename CellType, typename SizeType>
void EncodeSync(const SimulationState<CellType, SizeType>& State, Function Callback) {
m_EncodeBuffer(State.CanvasPointer, State.CanvasSize, State.PossibleStates, Callback);
}

template<typename Function=EncoderCallback, typename CellType, typename SizeType>
template<typename Function, typename CellType, typename SizeType>
void EncodeSync(const SimulationState<CellType, SizeType>& State, const Vector2<SizeType>& SectionPosition, const Vector2<SizeType>& SectionSize, Function Callback) {
std::vector<CellType> Section(SectionSize.X * SectionSize.Y);

Expand All @@ -219,7 +209,7 @@ namespace Encoding {
return m_EncodeBuffer(Section.data(), SectionSize, State.PossibleStates, Callback);
}

template<typename Function=EncoderCallback, typename CellType, typename SizeType>
template<typename Function, typename CellType, typename SizeType>
void EncodeAsync(const SimulationState<CellType, SizeType>& State, Function Callback) {
auto GridCopy = std::make_shared<std::vector<CellType>>();
auto GridSize = State.CanvasSize;
Expand All @@ -233,7 +223,7 @@ namespace Encoding {
}).detach();
}

template<typename Function=EncoderCallback, typename CellType, typename SizeType>
template<typename Function, typename CellType, typename SizeType>
void EncodeAsync(const SimulationState<CellType, SizeType>& State, const Vector2<SizeType>& SectionPosition, const Vector2<SizeType>& SectionSize, Function Callback) {
auto Section = std::make_shared<std::vector<CellType>>(SectionSize.X * SectionSize.Y);

Expand Down
20 changes: 13 additions & 7 deletions SOURCE/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ int main(int ArgCount, const char* ArgValues[]) {
SimulationState<uint8_t, int> Simulation = {};
Encoding::PaletteManager Palette = {};
Encoding::ThreadManager Threads = {};
Encoding::EncoderState Encoder = {};

Threads.ThreadCount = 50;

Expand Down Expand Up @@ -169,22 +170,25 @@ int main(int ArgCount, const char* ArgValues[]) {
// 1ull * 1000000000ull 1b
// 1ull * 1000000ull 1m

size_t Iterations = 3ull * 1000000000ull;
double FrameRate = 30.0; // Video frame rate
double Time = 240.0; // Video time
size_t Iterations = 2ull * 1000000000ull;
double FrameRate = 3.0; // Video frame rate
double Time = 1.0; // Video time
size_t Frames = size_t(Time * FrameRate);

size_t CaptureDelta = size_t(double(Iterations) / double(Frames));

Simulation.Reset();
Palette.ResizePalette(Simulation.PossibleStates);
Encoder.Threads.ThreadCount = 2;
Encoder.Format = Encoding::ImageFormat::PNG_GRAYSCALE;

std::mutex Mutex = {};
size_t CurrentFrame = 0;

for (size_t i = 0; i < Frames; i++) {
Simulation.Simulate(CaptureDelta);//std::cout << "i:" << i << ' ' << Simulation.Simulate(CaptureDelta) << '/' << CaptureDelta << '\n';

/*
auto GridCopy = std::make_shared<std::vector<uint8_t>>();
GridCopy->assign(Simulation.CanvasPointer, Simulation.CanvasPointer + CanvasSize.X * CanvasSize.Y);
Expand All @@ -198,7 +202,7 @@ int main(int ArgCount, const char* ArgValues[]) {
auto Start = std::chrono::high_resolution_clock::now();
for (int X = 0; X < Center.X; X++) {
for (int Y = 0; Y < Center.Y; Y++) {
//*
//
auto C00 = Palette[Canvas[FLATTEN_2D(X * 2 + 0, Y * 2 + 0, CanvasSize.X)]].RGBA;
auto C01 = Palette[Canvas[FLATTEN_2D(X * 2 + 0, Y * 2 + 1, CanvasSize.X)]].RGBA;
auto C10 = Palette[Canvas[FLATTEN_2D(X * 2 + 1, Y * 2 + 0, CanvasSize.X)]].RGBA;
Expand All @@ -209,7 +213,7 @@ int main(int ArgCount, const char* ArgValues[]) {
Image[Index + 0] = uint8_t(((float)C00.R + (float)C01.R + (float)C10.R + (float)C11.R) / 4.f);
Image[Index + 1] = uint8_t(((float)C00.G + (float)C01.G + (float)C10.G + (float)C11.G) / 4.f);
Image[Index + 2] = uint8_t(((float)C00.B + (float)C01.B + (float)C10.B + (float)C11.B) / 4.f);
//*/
///
}
}
auto Elapsed = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::high_resolution_clock::now() - Start);
Expand All @@ -227,8 +231,10 @@ int main(int ArgCount, const char* ArgValues[]) {
Threads.ReleaseThread();
}).detach();
*/


//Encoder.EncodeAsync(Simulation, std::string("Frames/") + std::to_string(i) +".png");
Encoder.EncodeAsync(Simulation, [&, i](const std::vector<uint8_t>& ImageData, const Vector2<int>&, unsigned int) {
lodepng::save_file(ImageData, "Frames/" + std::to_string(i) + ".png");
});
}
}

0 comments on commit f6afb74

Please sign in to comment.