From dd1ff089b9640197ed44d7ba23ddf706377c21ab Mon Sep 17 00:00:00 2001 From: A-lex-Ra Date: Tue, 19 Dec 2023 09:26:13 +0600 Subject: [PATCH 1/3] rays efficiency and readability improve --- src/maths/rays.cpp | 126 +++++++++++++++++++----------------------- src/maths/rays.h | 47 +++++++--------- src/voxels/Chunks.cpp | 6 +- 3 files changed, 80 insertions(+), 99 deletions(-) diff --git a/src/maths/rays.cpp b/src/maths/rays.cpp index 3f9c5cc83..2e358bcf1 100644 --- a/src/maths/rays.cpp +++ b/src/maths/rays.cpp @@ -4,9 +4,11 @@ #include "glm/glm.hpp" -std::unordered_map Rays::raysBoxCache_ = {}; +std::unordered_map Ray::raysBoxCache_ = {}; const rayvec3 X_AXIS = rayvec3(1,0,0), Y_AXIS = rayvec3(0,1,0), Z_AXIS = rayvec3(0,0,1); +Ray::Ray(const rayvec3& origin, const rayvec3& dir) : origin(origin), dir(dir) {} + //make faces from AABB AABBFaces::AABBFaces(const rayvec3& parentBoxPos, const AABB& parentBox){ rayvec3 pbMin = parentBox.min(), // every face is min-point and opposite corner point @@ -29,30 +31,28 @@ AABBFaces::AABBFaces(const rayvec3& parentBoxPos, const AABB& parentBox){ } template <> -RayRelation Rays::rayIntersectAAFace( - const rayvec3& rayOrigin, - const rayvec3& rayDir, +RayRelation Ray::intersectAAFace( const rayvec3& faceMin, const rayvec2& faceOppositeCorner, //y and z global coords of opposite corner glm::ivec3& normal_ret, scalar_t& distance_ret //sinonym of rayCoef ){ - if (fabs(glm::dot(rayDir, X_AXIS)) < 1.0E-8){ //precision + if (fabs(glm::dot(dir, X_AXIS)) < 1.0E-8){ //precision return RayRelation::Parallel; } - scalar_t rayCoef = (faceMin.x - rayOrigin.x) / (rayDir.x);// equivalent to distance if raydir normalized + scalar_t rayCoef = (faceMin.x - origin.x) / (dir.x);// equivalent to distance if dir normalized if (rayCoef < 0) return RayRelation::None; rayvec3 intersectPoint = {faceMin.x, - rayCoef*rayDir.y + rayOrigin.y, - rayCoef*rayDir.z + rayOrigin.z}; + rayCoef*dir.y + origin.y, + rayCoef*dir.z + origin.z}; if (intersectPoint.y >= faceMin.y && intersectPoint.y <= faceOppositeCorner[0] && intersectPoint.z >= faceMin.z && intersectPoint.z <= faceOppositeCorner[1]){ - distance_ret = rayCoef; // believe that raydir normalized - if (rayDir.x > 0) normal_ret = -X_AXIS; + distance_ret = rayCoef; // believe that dir normalized + if (dir.x > 0) normal_ret = -X_AXIS; else normal_ret = X_AXIS; return RayRelation::Intersect; } @@ -60,30 +60,28 @@ RayRelation Rays::rayIntersectAAFace( } template <> -RayRelation Rays::rayIntersectAAFace( - const rayvec3& rayOrigin, - const rayvec3& rayDir, +RayRelation Ray::intersectAAFace( const rayvec3& faceMin, const rayvec2& faceOppositeCorner, //x and z global coords of opposite corner glm::ivec3& normal_ret, scalar_t& distance_ret ){ - if (fabs(glm::dot(rayDir, Y_AXIS)) < 1.0E-8){ //precision + if (fabs(glm::dot(dir, Y_AXIS)) < 1.0E-8){ //precision return RayRelation::Parallel; } - scalar_t rayCoef = (faceMin.y - rayOrigin.y) / (rayDir.y);// equivalent to distance if raydir normalized + scalar_t rayCoef = (faceMin.y - origin.y) / (dir.y);// equivalent to distance if dir normalized if (rayCoef < 0) return RayRelation::None; - rayvec3 intersectPoint = { rayCoef *rayDir.x + rayOrigin.x, + rayvec3 intersectPoint = { rayCoef *dir.x + origin.x, faceMin.y, - rayCoef*rayDir.z + rayOrigin.z}; + rayCoef*dir.z + origin.z}; if (intersectPoint.x >= faceMin.x //Face-hit check && intersectPoint.x <= faceOppositeCorner[0] && intersectPoint.z >= faceMin.z && intersectPoint.z <= faceOppositeCorner[1] ){ - distance_ret = rayCoef; // believe that raydir normalized - if (rayDir.y > 0) normal_ret = -Y_AXIS; + distance_ret = rayCoef; // believe that dir normalized + if (dir.y > 0) normal_ret = -Y_AXIS; else normal_ret = Y_AXIS; return RayRelation::Intersect; } @@ -91,30 +89,28 @@ RayRelation Rays::rayIntersectAAFace( } template <> -RayRelation Rays::rayIntersectAAFace( - const rayvec3& rayOrigin, - const rayvec3& rayDir, +RayRelation Ray::intersectAAFace( const rayvec3& faceMin, const rayvec2& faceOppositeCorner, //x and y global coords of opposite corner glm::ivec3& normal_ret, scalar_t& distance_ret ){ - if (fabs(glm::dot(rayDir, Z_AXIS)) < 1.0E-8){ //precision + if (fabs(glm::dot(dir, Z_AXIS)) < 1.0E-8){ //precision return RayRelation::Parallel; } - scalar_t rayCoef = (faceMin.z - rayOrigin.z) / (rayDir.z); // equivalent to distance if raydir normalized + scalar_t rayCoef = (faceMin.z - origin.z) / (dir.z); // equivalent to distance if dir normalized if (rayCoef < 0) return RayRelation::None; - rayvec3 intersectPoint = { rayCoef *rayDir.x + rayOrigin.x, - rayCoef*rayDir.y + rayOrigin.y, + rayvec3 intersectPoint = { rayCoef *dir.x + origin.x, + rayCoef*dir.y + origin.y, faceMin.z}; if (intersectPoint.x >= faceMin.x //Face-hit check && intersectPoint.x <= faceOppositeCorner[0] && intersectPoint.y >= faceMin.y && intersectPoint.y <= faceOppositeCorner[1] ){ - distance_ret = rayCoef; // believe that raydir normalized - if (rayDir.z > 0) normal_ret = -Z_AXIS; + distance_ret = rayCoef; // believe that dir normalized + if (dir.z > 0) normal_ret = -Z_AXIS; else normal_ret = Z_AXIS; return RayRelation::Intersect; } @@ -122,21 +118,19 @@ RayRelation Rays::rayIntersectAAFace( } template <> -RayRelation Rays::isRayIntersectsAAFace( - const rayvec3& rayOrigin, - const rayvec3& rayDir, +RayRelation Ray::isIntersectsAAFace( const rayvec3& faceMin, const rayvec2& faceOppositeCorner ){ - if (fabs(glm::dot(rayDir, X_AXIS)) < 1.0E-8) { //precision of "parallelity" + if (fabs(glm::dot(dir, X_AXIS)) < 1.0E-8) { //precision of "parallelity" return RayRelation::Parallel; } - scalar_t rayCoef = (faceMin.x - rayOrigin.x) / (rayDir.x); + scalar_t rayCoef = (faceMin.x - origin.x) / (dir.x); if (rayCoef < 0) return RayRelation::None; rayvec3 intersectPoint = { faceMin.x, - rayCoef * rayDir.y + rayOrigin.y, - rayCoef * rayDir.z + rayOrigin.z }; + rayCoef * dir.y + origin.y, + rayCoef * dir.z + origin.z }; if (intersectPoint.y >= faceMin.y && intersectPoint.y <= faceOppositeCorner[0] @@ -148,21 +142,19 @@ RayRelation Rays::isRayIntersectsAAFace( } template <> -RayRelation Rays::isRayIntersectsAAFace( - const rayvec3& rayOrigin, - const rayvec3& rayDir, +RayRelation Ray::isIntersectsAAFace( const rayvec3& faceMin, const rayvec2& faceOppositeCorner ) { - if (fabs(glm::dot(rayDir, Y_AXIS)) < 1.0E-8) { //precision of "parallelity" + if (fabs(glm::dot(dir, Y_AXIS)) < 1.0E-8) { //precision of "parallelity" return RayRelation::Parallel; } - scalar_t rayCoef = (faceMin.y - rayOrigin.y) / (rayDir.y); + scalar_t rayCoef = (faceMin.y - origin.y) / (dir.y); if (rayCoef < 0) return RayRelation::None; - rayvec3 intersectPoint = { rayCoef * rayDir.x + rayOrigin.x, + rayvec3 intersectPoint = { rayCoef * dir.x + origin.x, faceMin.y, - rayCoef * rayDir.z + rayOrigin.z }; + rayCoef * dir.z + origin.z }; if (intersectPoint.x >= faceMin.x //Face-hit check && intersectPoint.x <= faceOppositeCorner[0] @@ -174,20 +166,18 @@ RayRelation Rays::isRayIntersectsAAFace( } template <> -RayRelation Rays::isRayIntersectsAAFace( - const rayvec3& rayOrigin, - const rayvec3& rayDir, +RayRelation Ray::isIntersectsAAFace( const rayvec3& faceMin, const rayvec2& faceOppositeCorner ) { - if (fabs(glm::dot(rayDir, Z_AXIS)) < 1.0E-8) { //precision of "parallelity" + if (fabs(glm::dot(dir, Z_AXIS)) < 1.0E-8) { //precision of "parallelity" return RayRelation::Parallel; } - scalar_t rayCoef = (faceMin.z - rayOrigin.z) / (rayDir.z); + scalar_t rayCoef = (faceMin.z - origin.z) / (dir.z); if (rayCoef < 0) return RayRelation::None; - rayvec3 intersectPoint = { rayCoef * rayDir.x + rayOrigin.x, - rayCoef * rayDir.y + rayOrigin.y, + rayvec3 intersectPoint = { rayCoef * dir.x + origin.x, + rayCoef * dir.y + origin.y, faceMin.z }; if (intersectPoint.x >= faceMin.x //Face-hit check @@ -199,9 +189,7 @@ RayRelation Rays::isRayIntersectsAAFace( return RayRelation::None; } -RayRelation Rays::rayIntersectAABB( - const rayvec3& rayOrigin, - const rayvec3& rayDir, +RayRelation Ray::intersectAABB( const rayvec3& boxPos, const AABB& box, float maxDist, @@ -211,23 +199,21 @@ RayRelation Rays::rayIntersectAABB( if (raysBoxCache_.find(boxPos) != raysBoxCache_.end()){ const AABBFaces& boxFaces = raysBoxCache_[boxPos]; - return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, normal_ret, distance_ret); + return intersectAABBFaces(boxFaces, maxDist, normal_ret, distance_ret); } else { const AABBFaces& boxFaces = AABBFaces(boxPos, box); raysBoxCache_[boxPos] = boxFaces; - return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, normal_ret, distance_ret); + return intersectAABBFaces(boxFaces, maxDist, normal_ret, distance_ret); } } else { const AABBFaces& boxFaces = AABBFaces(boxPos, box); - return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, normal_ret, distance_ret); + return intersectAABBFaces(boxFaces, maxDist, normal_ret, distance_ret); } } -RayRelation Rays::rayIntersectAABBFaces( - const rayvec3& rayOrigin, - const rayvec3& rayDir, +RayRelation Ray::intersectAABBFaces( const AABBFaces& boxFaces, float maxDist, glm::ivec3& normal_ret, @@ -239,48 +225,48 @@ RayRelation Rays::rayIntersectAABBFaces( //unsigned char intersectedCount = 0; //this code is very uncomfortable, DONT LEARN IT! bool isIntersect = false; - if (rayIntersectAAFace( - rayOrigin, rayDir, boxFaces.faces[0].first, boxFaces.faces[0].second, bufNormal, faceDist + if (intersectAAFace( + boxFaces.faces[0].first, boxFaces.faces[0].second, bufNormal, faceDist ) > RayRelation::None && faceDist < distance_ret){ isIntersect = true; normal_ret = bufNormal; distance_ret = faceDist; } - if (rayIntersectAAFace( - rayOrigin, rayDir, boxFaces.faces[1].first, boxFaces.faces[1].second, bufNormal, faceDist + if (intersectAAFace( + boxFaces.faces[1].first, boxFaces.faces[1].second, bufNormal, faceDist ) > RayRelation::None && faceDist < distance_ret) { isIntersect = true; normal_ret = bufNormal; distance_ret = faceDist; } - if (rayIntersectAAFace( - rayOrigin, rayDir, boxFaces.faces[2].first, boxFaces.faces[2].second, bufNormal, faceDist + if (intersectAAFace( + boxFaces.faces[2].first, boxFaces.faces[2].second, bufNormal, faceDist ) > RayRelation::None && faceDist < distance_ret) { isIntersect = true; normal_ret = bufNormal; distance_ret = faceDist; } - if (rayIntersectAAFace( - rayOrigin, rayDir, boxFaces.faces[3].first, boxFaces.faces[3].second, bufNormal, faceDist + if (intersectAAFace( + boxFaces.faces[3].first, boxFaces.faces[3].second, bufNormal, faceDist ) > RayRelation::None && faceDist < distance_ret) { isIntersect = true; normal_ret = bufNormal; distance_ret = faceDist; } - if (rayIntersectAAFace( - rayOrigin, rayDir, boxFaces.faces[4].first, boxFaces.faces[4].second, bufNormal, faceDist + if (intersectAAFace( + boxFaces.faces[4].first, boxFaces.faces[4].second, bufNormal, faceDist ) > RayRelation::None && faceDist < distance_ret) { isIntersect = true; normal_ret = bufNormal; distance_ret = faceDist; } - if (rayIntersectAAFace( - rayOrigin, rayDir, boxFaces.faces[5].first, boxFaces.faces[5].second, bufNormal, faceDist + if (intersectAAFace( + boxFaces.faces[5].first, boxFaces.faces[5].second, bufNormal, faceDist ) > RayRelation::None && faceDist < distance_ret) { isIntersect = true; normal_ret = bufNormal; diff --git a/src/maths/rays.h b/src/maths/rays.h index 2a4699d64..17badfdff 100644 --- a/src/maths/rays.h +++ b/src/maths/rays.h @@ -42,67 +42,60 @@ struct std::hash{ } }; -class Rays{ +class Ray{ protected: static const bool IS_RAYS_BOX_CACHE_ON = false; static std::unordered_map raysBoxCache_; //[boxPos]: faces array public: + rayvec3 origin; + rayvec3 dir; + + Ray(const rayvec3& rayOrigin, + const rayvec3& rayDir); //optimized, NOT returns intersectPoint coordinates and normal vector -template -static RayRelation isRayIntersectsAAFace( - const rayvec3& rayOrigin, - const rayvec3& rayDir, + template + RayRelation isIntersectsAAFace( const rayvec3& faceMin, const rayvec2& faceOppositeCorner - ); + ); //returns only normal -template -static RayRelation rayIntersectAAFace( - const rayvec3& rayOrigin, - const rayvec3& rayDir, + template + RayRelation intersectAAFace( const rayvec3& faceMin, const rayvec2& faceOppositeCorner, glm::ivec3& normal_ret -); + ); //returns normal and distance -template -static RayRelation rayIntersectAAFace( - const rayvec3& rayOrigin, - const rayvec3& rayDir, + template + RayRelation intersectAAFace( const rayvec3& faceMin, const rayvec2& faceOppositeCorner, glm::ivec3& normal_ret, scalar_t& distance_ret - ); + ); // returns normal, distance and intersection point -template -static RayRelation rayIntersectAAFace( - const rayvec3& rayOrigin, - const rayvec3& rayDir, + template + RayRelation intersectAAFace( const rayvec3& faceMin, const rayvec2& faceOppositeCorner, glm::ivec3& normal_ret, scalar_t& distance_ret, rayvec3& intersectPoint_ret -); + ); -static RayRelation rayIntersectAABB( - const rayvec3& rayOrigin, - const rayvec3& rayDir, + RayRelation intersectAABB( const rayvec3& boxPos, const AABB& box, float maxDist, glm::ivec3& normal_ret, scalar_t& distance_ret); -static RayRelation rayIntersectAABBFaces( // calculates only normal and distance - const rayvec3& rayOrigin, - const rayvec3& rayDir, + RayRelation intersectAABBFaces( // calculates only normal and distance const AABBFaces& boxFaces, float maxDist, glm::ivec3& normal_ret, diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index d5bdd3afa..f14c52f6a 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -235,7 +235,8 @@ voxel* Chunks::rayCast(vec3 start, ? def->rt.hitboxes[voxel->rotation()] : def->hitbox; scalar_t distance; - if (Rays::rayIntersectAABB(start, dir, iend, box, maxDist, norm, distance) > RayRelation::None){ + Ray ray(start, dir); + if (ray.intersectAABB(iend, box, maxDist, norm, distance) > RayRelation::None){ end = start + (dir * vec3(distance)); return voxel; } @@ -333,8 +334,9 @@ vec3 Chunks::rayCastToObstacle(vec3 start, vec3 dir, float maxDist) { : def->hitbox; scalar_t distance; ivec3 norm; + Ray ray(start, dir); // norm is dummy now, can be inefficient - if (Rays::rayIntersectAABB(start, dir, ivec3(ix, iy, iz), box, maxDist, norm, distance) > RayRelation::None) { + if (ray.intersectAABB(ivec3(ix, iy, iz), box, maxDist, norm, distance) > RayRelation::None) { return start + (dir * vec3(distance)); } } From c17657a8eebf00b7f6a2c34d3aec178549d0a490 Mon Sep 17 00:00:00 2001 From: A-lex-Ra Date: Tue, 19 Dec 2023 13:39:17 +0600 Subject: [PATCH 2/3] refactoring old C for windows --- src/coders/png.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/coders/png.cpp b/src/coders/png.cpp index 81a80b8e3..a20752add 100644 --- a/src/coders/png.cpp +++ b/src/coders/png.cpp @@ -194,10 +194,12 @@ ImageData* _png_load(const char* file){ #include #include +static const int SPNG_SUCCESS = 0; +//returns spng result code int _png_write(const char* filename, uint width, uint height, const ubyte* data, bool alpha) { int fmt; int ret = 0; - spng_ctx* ctx = NULL; + spng_ctx* ctx = nullptr; spng_ihdr ihdr = { 0 }; uint pixsize = alpha ? 4 : 3; @@ -212,7 +214,7 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data, spng_set_ihdr(ctx, &ihdr); fmt = SPNG_FMT_PNG; ret = spng_encode_image(ctx, data, (size_t)width * (size_t)height * pixsize , fmt, SPNG_ENCODE_FINALIZE); - if (ret) { + if (ret != SPNG_SUCCESS) { printf("spng_encode_image() error: %s\n", spng_strerror(ret)); fflush(stdout); spng_ctx_free(ctx); @@ -222,7 +224,7 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data, size_t png_size; void* png_buf = spng_get_png_buffer(ctx, &png_size, &ret); - if (png_buf == NULL) { + if (png_buf == nullptr) { printf("spng_get_png_buffer() error: %s\n", spng_strerror(ret)); } else { @@ -235,7 +237,7 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data, ImageData* _png_load(const char* file){ int r = 0; - FILE *png; + FILE *png = nullptr; char *pngbuf = nullptr; spng_ctx *ctx = nullptr; unsigned char *out = nullptr; @@ -255,7 +257,7 @@ ImageData* _png_load(const char* file){ return nullptr; } pngbuf = new char[siz_pngbuf]; - if(fread(pngbuf, siz_pngbuf, 1, png) != 1){ + if(fread(pngbuf, siz_pngbuf, 1, png) != 1){ //check of read elements count fclose(png); delete[] pngbuf; std::cerr << "fread() failed" << std::endl; @@ -269,14 +271,14 @@ ImageData* _png_load(const char* file){ return nullptr; } r = spng_set_crc_action(ctx, SPNG_CRC_USE, SPNG_CRC_USE); - if (r){ + if (r != SPNG_SUCCESS){ delete[] pngbuf; spng_ctx_free(ctx); std::cerr << "spng_set_crc_action() error: " << spng_strerror(r) << std::endl; return nullptr; } r = spng_set_png_buffer(ctx, pngbuf, siz_pngbuf); - if (r){ + if (r != SPNG_SUCCESS){ delete[] pngbuf; spng_ctx_free(ctx); std::cerr << "spng_set_png_buffer() error: " << spng_strerror(r) << std::endl; @@ -285,7 +287,7 @@ ImageData* _png_load(const char* file){ spng_ihdr ihdr; r = spng_get_ihdr(ctx, &ihdr); - if (r){ + if (r != SPNG_SUCCESS){ delete[] pngbuf; spng_ctx_free(ctx); std::cerr << "spng_get_ihdr() error: " << spng_strerror(r) << std::endl; @@ -306,7 +308,7 @@ ImageData* _png_load(const char* file){ size_t out_size; r = spng_decoded_image_size(ctx, SPNG_FMT_RGBA8, &out_size); - if (r){ + if (r != SPNG_SUCCESS){ delete[] pngbuf; spng_ctx_free(ctx); std::cerr << "spng_decoded_image_size() error: " << spng_strerror(r) << std::endl; @@ -314,7 +316,7 @@ ImageData* _png_load(const char* file){ } out = new unsigned char[out_size]; r = spng_decode_image(ctx, out, out_size, SPNG_FMT_RGBA8, 0); - if (r){ + if (r != SPNG_SUCCESS){ delete[] out; delete[] pngbuf; spng_ctx_free(ctx); From 73abc23b5c55266b05bacbd2328f1bd1cb1b0ae8 Mon Sep 17 00:00:00 2001 From: Ara Date: Tue, 19 Dec 2023 14:14:25 +0600 Subject: [PATCH 3/3] refactoring old C for Linux --- src/coders/png.cpp | 59 ++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/src/coders/png.cpp b/src/coders/png.cpp index a20752add..4794694e2 100644 --- a/src/coders/png.cpp +++ b/src/coders/png.cpp @@ -17,35 +17,36 @@ using std::unique_ptr; #ifdef LIBPNG #include +// returns 0 if all-right, 1 otherwise int _png_write(const char* filename, uint width, uint height, const ubyte* data, bool alpha) { - png_structp png_ptr = NULL; - png_infop info_ptr = NULL; + png_structp png_ptr = nullptr; + png_infop info_ptr = nullptr; uint pixsize = alpha ? 4 : 3; // Open file for writing (binary mode) FILE* fp = fopen(filename, "wb"); - if (fp == NULL) { + if (fp == nullptr) { fprintf(stderr, "Could not open file %s for writing\n", filename); fclose(fp); return 1; } // Initialize write structure - png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - if (png_ptr == NULL) { + png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); + if (png_ptr == nullptr) { fprintf(stderr, "Could not allocate write struct\n"); fclose(fp); - png_destroy_write_struct(&png_ptr, (png_infopp)NULL); + png_destroy_write_struct(&png_ptr, (png_infopp)nullptr); return 1; } // Initialize info structure info_ptr = png_create_info_struct(png_ptr); - if (info_ptr == NULL) { + if (info_ptr == nullptr) { fprintf(stderr, "Could not allocate info struct\n"); fclose(fp); png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); - png_destroy_write_struct(&png_ptr, (png_infopp)NULL); + png_destroy_write_struct(&png_ptr, (png_infopp)nullptr); return 1; } @@ -55,7 +56,7 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data, fprintf(stderr, "Error during png creation\n"); fclose(fp); png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); - png_destroy_write_struct(&png_ptr, (png_infopp)NULL); + png_destroy_write_struct(&png_ptr, (png_infopp)nullptr); return 1; } @@ -85,58 +86,59 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data, } // End write - png_write_end(png_ptr, NULL); + png_write_end(png_ptr, nullptr); fclose(fp); png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); - png_destroy_write_struct(&png_ptr, (png_infopp)NULL); + png_destroy_write_struct(&png_ptr, (png_infopp)nullptr); return 0; } ImageData* _png_load(const char* file){ - FILE *f; - int is_png, bit_depth, color_type, row_bytes; + FILE *f = nullptr; + bool is_png = false; + int bit_depth, color_type, row_bytes; png_infop info_ptr, end_info; png_uint_32 t_width, t_height; png_byte header[8], *image_data; png_bytepp row_pointers; png_structp png_ptr; - if (!(f = fopen(file, "r"))) { + if ((f = fopen(file, "r")) == nullptr) { return nullptr; } - if (fread(header, 1, 8, f) < 8) { + if (fread(header, 1, 8, f) < 8) { // check of read elements count fclose(f); return nullptr; } - is_png = !png_sig_cmp(header, 0, 8); + is_png = (png_sig_cmp(header, 0, 8) == 0); if (!is_png) { fclose(f); return nullptr; } - png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, - NULL, NULL); - if (!png_ptr) { + png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, + nullptr, nullptr); + if (png_ptr == nullptr) { fclose(f); return nullptr; } info_ptr = png_create_info_struct(png_ptr); - if (!info_ptr) { - png_destroy_read_struct( &png_ptr, (png_infopp) NULL, - (png_infopp) NULL ); + if (info_ptr == nullptr) { + png_destroy_read_struct( &png_ptr, (png_infopp) nullptr, + (png_infopp) nullptr ); fclose(f); return nullptr; } end_info = png_create_info_struct(png_ptr); - if (!end_info) { - png_destroy_read_struct(&png_ptr, (png_infopp) NULL, - (png_infopp) NULL); + if (end_info == nullptr) { + png_destroy_read_struct(&png_ptr, (png_infopp) nullptr, + (png_infopp) nullptr); fclose(f); return nullptr; } - if (setjmp(png_jmpbuf(png_ptr))) { + if (setjmp(png_jmpbuf(png_ptr)) != 0) { png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); fclose(f); return nullptr; @@ -146,7 +148,7 @@ ImageData* _png_load(const char* file){ png_set_sig_bytes( png_ptr, 8 ); png_read_info( png_ptr, info_ptr ); png_get_IHDR( png_ptr, info_ptr, &t_width, &t_height, &bit_depth, - &color_type, NULL, NULL, NULL ); + &color_type, nullptr, nullptr, nullptr ); png_read_update_info( png_ptr, info_ptr ); row_bytes = png_get_rowbytes( png_ptr, info_ptr ); image_data = new png_byte[row_bytes * t_height]; @@ -156,7 +158,7 @@ ImageData* _png_load(const char* file){ return nullptr; } row_pointers = (png_bytepp) malloc( t_height * sizeof(png_bytep) ); - if ( !row_pointers ) { + if ( row_pointers == nullptr ) { png_destroy_read_struct( &png_ptr, &info_ptr, &end_info ); delete[] image_data; fclose(f); @@ -180,6 +182,7 @@ ImageData* _png_load(const char* file){ png_get_color_type( png_ptr, info_ptr ) ); png_destroy_read_struct( &png_ptr, &info_ptr, &end_info ); delete[] image_data; + free( row_pointers ); fclose(f); return nullptr; }