Merge branch 'png_output'
This commit is contained in:
commit
962085d475
6 changed files with 85 additions and 14 deletions
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
Checks: 'clang-diagnostic-*,clang-analyzer-*,*,-llvmlibc-*,-fuchsia-*,-readability-magic-numbers,-cppcoreguidelines-avoid-magic-numbers,-modernize-use-trailing-return-type,-llvm-header-guard,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-altera-struct-pack-align,-misc-non-private-member-variables-in-classes,-modernize-use-nodiscard,-cppcoreguidelines-pro-type-union-access,-llvm-else-after-return,-readability-else-after-return'
|
||||
Checks: 'clang-diagnostic-*,clang-analyzer-*,*,-llvmlibc-*,-fuchsia-*,-readability-magic-numbers,-cppcoreguidelines-avoid-magic-numbers,-modernize-use-trailing-return-type,-llvm-header-guard,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-altera-struct-pack-align,-misc-non-private-member-variables-in-classes,-modernize-use-nodiscard,-cppcoreguidelines-pro-type-union-access,-llvm-else-after-return,-readability-else-after-return,-cppcoreguidelines-owning-memory,-android-cloexec-fopen'
|
||||
WarningsAsErrors: ''
|
||||
HeaderFilterRegex: ''
|
||||
AnalyzeTemporaryDtors: false
|
||||
|
@ -8,12 +8,12 @@ User: stephen
|
|||
CheckOptions:
|
||||
- key: cppcoreguidelines-no-malloc.Reallocations
|
||||
value: '::realloc'
|
||||
- key: cppcoreguidelines-owning-memory.LegacyResourceConsumers
|
||||
value: '::free;::realloc;::freopen;::fclose'
|
||||
- key: modernize-use-auto.MinTypeNameLength
|
||||
value: '5'
|
||||
- key: bugprone-reserved-identifier.Invert
|
||||
value: 'false'
|
||||
- key: modernize-replace-disallow-copy-and-assign-macro.MacroName
|
||||
value: DISALLOW_COPY_AND_ASSIGN
|
||||
- key: bugprone-narrowing-conversions.PedanticMode
|
||||
value: 'false'
|
||||
- key: bugprone-unused-return-value.CheckedFunctions
|
||||
|
@ -30,8 +30,6 @@ CheckOptions:
|
|||
value: 'false'
|
||||
- key: cert-dcl37-c.AllowedIdentifiers
|
||||
value: ''
|
||||
- key: modernize-replace-disallow-copy-and-assign-macro.MacroName
|
||||
value: DISALLOW_COPY_AND_ASSIGN
|
||||
- key: hicpp-use-emplace.SmartPointers
|
||||
value: '::std::shared_ptr;::std::unique_ptr;::std::auto_ptr;::std::weak_ptr'
|
||||
- key: hicpp-member-init.IgnoreArrays
|
||||
|
@ -316,8 +314,6 @@ CheckOptions:
|
|||
value: 'false'
|
||||
- key: modernize-use-emplace.TupleMakeFunctions
|
||||
value: '::std::make_pair;::std::make_tuple'
|
||||
- key: cppcoreguidelines-owning-memory.LegacyResourceProducers
|
||||
value: '::malloc;::aligned_alloc;::realloc;::calloc;::fopen;::freopen;::tmpfile'
|
||||
- key: hicpp-uppercase-literal-suffix.IgnoreMacros
|
||||
value: 'true'
|
||||
- key: bugprone-argument-comment.StrictMode
|
||||
|
@ -334,12 +330,12 @@ CheckOptions:
|
|||
value: ''
|
||||
- key: modernize-avoid-bind.PermissiveParameterList
|
||||
value: 'false'
|
||||
- key: hicpp-use-override.OverrideSpelling
|
||||
value: override
|
||||
- key: modernize-use-override.FinalSpelling
|
||||
value: final
|
||||
- key: cert-err09-cpp.MaxSize
|
||||
value: '-1'
|
||||
- key: hicpp-use-override.OverrideSpelling
|
||||
value: override
|
||||
- key: hicpp-use-equals-default.IgnoreMacros
|
||||
value: 'true'
|
||||
- key: modernize-use-noexcept.ReplacementString
|
||||
|
|
|
@ -40,6 +40,10 @@ else()
|
|||
endif()
|
||||
target_include_directories(Example02 PUBLIC ${GLM_INCLUDE_DIRS})
|
||||
|
||||
find_package(PNG REQUIRED)
|
||||
target_include_directories(Example02 PUBLIC ${PNG_INCLUDE_DIRS})
|
||||
target_link_libraries(Example02 PUBLIC ${PNG_LIBRARIES})
|
||||
|
||||
find_program(CLANG_FORMAT "clang-format")
|
||||
if(CLANG_FORMAT)
|
||||
add_custom_target(
|
||||
|
|
|
@ -2,4 +2,4 @@
|
|||
|
||||
set -ev
|
||||
|
||||
clang-tidy src/main.cpp -checks=*,-llvmlibc-*,-fuchsia-*,-readability-magic-numbers,-cppcoreguidelines-avoid-magic-numbers,-modernize-use-trailing-return-type,-llvm-header-guard,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-altera-struct-pack-align,-misc-non-private-member-variables-in-classes,-modernize-use-nodiscard,-cppcoreguidelines-pro-type-union-access,-llvm-else-after-return,-readability-else-after-return -dump-config -- -std=c++17 > .clang-tidy
|
||||
clang-tidy src/main.cpp -checks=*,-llvmlibc-*,-fuchsia-*,-readability-magic-numbers,-cppcoreguidelines-avoid-magic-numbers,-modernize-use-trailing-return-type,-llvm-header-guard,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-altera-struct-pack-align,-misc-non-private-member-variables-in-classes,-modernize-use-nodiscard,-cppcoreguidelines-pro-type-union-access,-llvm-else-after-return,-readability-else-after-return,-cppcoreguidelines-owning-memory,-android-cloexec-fopen -dump-config -- -std=c++17 > .clang-tidy
|
||||
|
|
|
@ -133,7 +133,10 @@ int main(int argc, char **argv) {
|
|||
auto pixels =
|
||||
Ex02::RT::renderColorsWithSpheres(outputWidth, outputHeight, threadCount);
|
||||
|
||||
auto outFilename = pixels.writeToFile(outputFile);
|
||||
// auto outFilename = pixels.writeToFile(outputFile);
|
||||
// std::cout << "Rendered image saved to " << outFilename << std::endl;
|
||||
|
||||
auto outFilename = pixels.writeToPNG(outputFile);
|
||||
std::cout << "Rendered image saved to " << outFilename << std::endl;
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -2,13 +2,18 @@
|
|||
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <csetjmp>
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/matrix.hpp>
|
||||
|
||||
#include <png.h>
|
||||
|
||||
const float PI = std::acos(-1.0F);
|
||||
|
||||
Ex02::RT::Pixel::Pixel() : r(0), g(0), b(0) {}
|
||||
|
@ -43,6 +48,66 @@ std::string Ex02::RT::Image::writeToFile(const std::string &filename) const {
|
|||
return outfilename;
|
||||
}
|
||||
|
||||
std::string Ex02::RT::Image::writeToPNG(const std::string &filename) const {
|
||||
std::string outfilename = filename + ".png";
|
||||
|
||||
FILE *outfile = fopen(outfilename.c_str(), "wb");
|
||||
if (outfile == nullptr) {
|
||||
return "ERROR";
|
||||
}
|
||||
|
||||
const static auto pngErrorLFn = [](png_structp psp, png_const_charp message) {
|
||||
std::cerr << "WARNING [libpng]: " << message << std::endl;
|
||||
};
|
||||
|
||||
const static auto pngWarnLFn = [](png_structp psp, png_const_charp message) {
|
||||
std::cerr << "ERROR [libpng]: " << message << std::endl;
|
||||
};
|
||||
|
||||
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr,
|
||||
pngErrorLFn, pngWarnLFn);
|
||||
if (png_ptr == nullptr) {
|
||||
fclose(outfile);
|
||||
return "ERROR";
|
||||
}
|
||||
|
||||
png_infop info_ptr = png_create_info_struct(png_ptr);
|
||||
if (info_ptr == nullptr) {
|
||||
png_destroy_write_struct(&png_ptr, nullptr);
|
||||
fclose(outfile);
|
||||
return "ERROR";
|
||||
}
|
||||
|
||||
if (setjmp(png_jmpbuf(png_ptr))) {
|
||||
png_destroy_write_struct(&png_ptr, &info_ptr);
|
||||
fclose(outfile);
|
||||
return "ERROR";
|
||||
}
|
||||
|
||||
png_init_io(png_ptr, outfile);
|
||||
|
||||
png_set_IHDR(png_ptr, info_ptr, this->width, this->data.size() / this->width,
|
||||
8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
|
||||
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
|
||||
|
||||
png_write_info(png_ptr, info_ptr);
|
||||
|
||||
png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
|
||||
|
||||
for (unsigned int j = 0; j < this->data.size() / this->width; ++j) {
|
||||
unsigned char *dataPtr =
|
||||
reinterpret_cast<unsigned char *>(&this->data.at(j * this->width));
|
||||
png_write_rows(png_ptr, &dataPtr, 1);
|
||||
}
|
||||
|
||||
png_write_end(png_ptr, nullptr);
|
||||
|
||||
png_destroy_write_struct(&png_ptr, &info_ptr);
|
||||
|
||||
fclose(outfile);
|
||||
return outfilename;
|
||||
}
|
||||
|
||||
/*
|
||||
glm::mat4x4 Ex02::RT::Internal::defaultMVP() {
|
||||
glm::mat4x4 mvp = glm::perspective(
|
||||
|
|
|
@ -18,7 +18,7 @@ constexpr float EX02_RAY_TRACER_GRAY_SPHERE_RADIUS = 1.5F;
|
|||
|
||||
namespace Ex02::RT {
|
||||
|
||||
struct Pixel {
|
||||
struct alignas(4) Pixel {
|
||||
Pixel();
|
||||
|
||||
unsigned char r, g, b;
|
||||
|
@ -31,12 +31,15 @@ public:
|
|||
Pixel &getPixel(unsigned int x, unsigned int y);
|
||||
const Pixel &getPixel(unsigned int x, unsigned int y) const;
|
||||
|
||||
// returns actual output filename
|
||||
// returns actual output filename (it appends the file extension)
|
||||
std::string writeToFile(const std::string &filename) const;
|
||||
|
||||
// returns actual output filename (it appends the file extension)
|
||||
std::string writeToPNG(const std::string &filename) const;
|
||||
|
||||
private:
|
||||
unsigned int width;
|
||||
std::vector<Pixel> data;
|
||||
mutable std::vector<Pixel> data;
|
||||
};
|
||||
|
||||
namespace Internal {
|
||||
|
|
Loading…
Reference in a new issue