Skip to content

Commit

Permalink
document NO_COMPILE macros better, and cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
lvandeve committed Jun 18, 2022
1 parent 08a7cd1 commit b4ed2cd
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 26 deletions.
1 change: 1 addition & 0 deletions examples/example_sdl.c
Expand Up @@ -115,6 +115,7 @@ int show(const char* filename) {
SDL_RenderPresent(sdl_renderer);

/* pause until you press escape and meanwhile redraw screen */
done = 0;
while(done == 0) {
while(SDL_PollEvent(&sdl_event)) {
if(sdl_event.type == SDL_QUIT) done = 2;
Expand Down
12 changes: 7 additions & 5 deletions lodepng.cpp
@@ -1,5 +1,5 @@
/*
LodePNG version 20220613
LodePNG version 20220618
Copyright (c) 2005-2022 Lode Vandevenne
Expand Down Expand Up @@ -44,7 +44,7 @@ Rename this file to lodepng.cpp to use it for C++, or to lodepng.c to use it for
#pragma warning( disable : 4996 ) /*VS does not like fopen, but fopen_s is not standard C so unusable here*/
#endif /*_MSC_VER */

const char* LODEPNG_VERSION_STRING = "20220613";
const char* LODEPNG_VERSION_STRING = "20220618";

/*
This source file is divided into the following large parts. The code sections
Expand Down Expand Up @@ -2373,7 +2373,7 @@ const LodePNGDecompressSettings lodepng_default_decompress_settings = {0, 0, 0,
/* ////////////////////////////////////////////////////////////////////////// */


#ifndef LODEPNG_NO_COMPILE_CRC
#ifdef LODEPNG_COMPILE_CRC
/* CRC polynomial: 0xedb88320 */
static unsigned lodepng_crc32_table[256] = {
0u, 1996959894u, 3993919788u, 2567524794u, 124634137u, 1886057615u, 3915621685u, 2657392035u,
Expand Down Expand Up @@ -2419,9 +2419,11 @@ unsigned lodepng_crc32(const unsigned char* data, size_t length) {
}
return r ^ 0xffffffffu;
}
#else /* !LODEPNG_NO_COMPILE_CRC */
#else /* LODEPNG_COMPILE_CRC */
/*in this case, the function is only declared here, and must be defined externally
so that it will be linked in*/
unsigned lodepng_crc32(const unsigned char* data, size_t length);
#endif /* !LODEPNG_NO_COMPILE_CRC */
#endif /* LODEPNG_COMPILE_CRC */

/* ////////////////////////////////////////////////////////////////////////// */
/* / Reading and writing PNG color channel bits / */
Expand Down
27 changes: 23 additions & 4 deletions lodepng.h
@@ -1,5 +1,5 @@
/*
LodePNG version 20220613
LodePNG version 20220618
Copyright (c) 2005-2022 Lode Vandevenne
Expand Down Expand Up @@ -35,56 +35,75 @@ The following #defines are used to create code sections. They can be disabled
to disable code sections, which can give faster compile time and smaller binary.
The "NO_COMPILE" defines are designed to be used to pass as defines to the
compiler command to disable them without modifying this header, e.g.
-DLODEPNG_NO_COMPILE_ZLIB for gcc.
In addition to those below, you can also define LODEPNG_NO_COMPILE_CRC to
allow implementing a custom lodepng_crc32.
-DLODEPNG_NO_COMPILE_ZLIB for gcc or clang.
*/
/*deflate & zlib. If disabled, you must specify alternative zlib functions in
the custom_zlib field of the compress and decompress settings*/
#ifndef LODEPNG_NO_COMPILE_ZLIB
/*pass -DLODEPNG_NO_COMPILE_ZLIB to the compiler to disable this, or comment out LODEPNG_COMPILE_ZLIB below*/
#define LODEPNG_COMPILE_ZLIB
#endif

/*png encoder and png decoder*/
#ifndef LODEPNG_NO_COMPILE_PNG
/*pass -DLODEPNG_NO_COMPILE_PNG to the compiler to disable this, or comment out LODEPNG_COMPILE_PNG below*/
#define LODEPNG_COMPILE_PNG
#endif

/*deflate&zlib decoder and png decoder*/
#ifndef LODEPNG_NO_COMPILE_DECODER
/*pass -DLODEPNG_NO_COMPILE_DECODER to the compiler to disable this, or comment out LODEPNG_COMPILE_DECODER below*/
#define LODEPNG_COMPILE_DECODER
#endif

/*deflate&zlib encoder and png encoder*/
#ifndef LODEPNG_NO_COMPILE_ENCODER
/*pass -DLODEPNG_NO_COMPILE_ENCODER to the compiler to disable this, or comment out LODEPNG_COMPILE_ENCODER below*/
#define LODEPNG_COMPILE_ENCODER
#endif

/*the optional built in harddisk file loading and saving functions*/
#ifndef LODEPNG_NO_COMPILE_DISK
/*pass -DLODEPNG_NO_COMPILE_DISK to the compiler to disable this, or comment out LODEPNG_COMPILE_DISK below*/
#define LODEPNG_COMPILE_DISK
#endif

/*support for chunks other than IHDR, IDAT, PLTE, tRNS, IEND: ancillary and unknown chunks*/
#ifndef LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS
/*pass -DLODEPNG_NO_COMPILE_ANCILLARY_CHUNKS to the compiler to disable this,
or comment out LODEPNG_COMPILE_ANCILLARY_CHUNKS below*/
#define LODEPNG_COMPILE_ANCILLARY_CHUNKS
#endif

/*ability to convert error numerical codes to English text string*/
#ifndef LODEPNG_NO_COMPILE_ERROR_TEXT
/*pass -DLODEPNG_NO_COMPILE_ERROR_TEXT to the compiler to disable this,
or comment out LODEPNG_COMPILE_ERROR_TEXT below*/
#define LODEPNG_COMPILE_ERROR_TEXT
#endif

/*Compile the default allocators (C's free, malloc and realloc). If you disable this,
you can define the functions lodepng_free, lodepng_malloc and lodepng_realloc in your
source files with custom allocators.*/
#ifndef LODEPNG_NO_COMPILE_ALLOCATORS
/*pass -DLODEPNG_NO_COMPILE_ALLOCATORS to the compiler to disable the built-in ones,
or comment out LODEPNG_COMPILE_ALLOCATORS below*/
#define LODEPNG_COMPILE_ALLOCATORS
#endif

/*Disable built-in CRC function, in that case a custom implementation of
lodepng_crc32 must be defined externally so that it can be linked in.*/
#ifndef LODEPNG_NO_COMPILE_CRC
/*pass -DLODEPNG_NO_COMPILE_CRC to the compiler to disable the built-in one,
or comment out LODEPNG_COMPILE_CRC below*/
#define LODEPNG_COMPILE_CRC
#endif

/*compile the C++ version (you can disable the C++ wrapper here even when compiling for C++)*/
#ifdef __cplusplus
#ifndef LODEPNG_NO_COMPILE_CPP
/*pass -DLODEPNG_NO_COMPILE_CPP to the compiler to disable C++ (not needed if a C-only compiler),
or comment out LODEPNG_COMPILE_CPP below*/
#define LODEPNG_COMPILE_CPP
#endif
#endif
Expand Down
27 changes: 12 additions & 15 deletions lodepng_util.cpp
@@ -1,7 +1,7 @@
/*
LodePNG Utils
Copyright (c) 2005-2020 Lode Vandevenne
Copyright (c) 2005-2022 Lode Vandevenne
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
Expand All @@ -24,7 +24,6 @@ freely, subject to the following restrictions:
*/

#include "lodepng_util.h"
#include <iostream> // TODO: remove, don't print stuff from here, return errors instead
#include <stdlib.h> /* allocations */

namespace lodepng {
Expand Down Expand Up @@ -489,7 +488,6 @@ static float iccBackwardTRC(const LodePNGICCCurve* curve, float x) {
a = m;
}
}
return 0;
}
if(curve->type == 2) {
/* Gamma compression */
Expand Down Expand Up @@ -539,7 +537,7 @@ static int decodeICCInt32(const unsigned char* data, size_t size, size_t* pos) {
}

static float decodeICC15Fixed16(const unsigned char* data, size_t size, size_t* pos) {
return decodeICCInt32(data, size, pos) / 65536.0;
return decodeICCInt32(data, size, pos) / 65536.0f;
}

static unsigned isICCword(const unsigned char* data, size_t size, size_t pos, const char* word) {
Expand Down Expand Up @@ -717,9 +715,9 @@ static unsigned parseICC(LodePNGICC* icc, const unsigned char* data, size_t size
static void mulMatrix(float* x2, float* y2, float* z2, const float* m, double x, double y, double z) {
/* double used as inputs even though in general the images are float, so the sums happen in
double precision, because float can give numerical problems for nearby values */
*x2 = x * m[0] + y * m[1] + z * m[2];
*y2 = x * m[3] + y * m[4] + z * m[5];
*z2 = x * m[6] + y * m[7] + z * m[8];
*x2 = (float)(x * m[0] + y * m[1] + z * m[2]);
*y2 = (float)(x * m[3] + y * m[4] + z * m[5]);
*z2 = (float)(x * m[6] + y * m[7] + z * m[8]);
}

static void mulMatrixMatrix(float* result, const float* a, const float* b) {
Expand All @@ -741,7 +739,7 @@ static unsigned invMatrix(float* m) {
double e6 = (double)m[3] * m[7] - (double)m[4] * m[6];
/* inverse determinant */
double d = 1.0 / (m[0] * e0 + m[1] * e3 + m[2] * e6);
float result[9];
double result[9];
if((d > 0 ? d : -d) > 1e15) return 1; /* error, likely not invertible */
result[0] = e0 * d;
result[1] = ((double)m[2] * m[7] - (double)m[1] * m[8]) * d;
Expand All @@ -752,7 +750,7 @@ static unsigned invMatrix(float* m) {
result[6] = e6 * d;
result[7] = ((double)m[6] * m[1] - (double)m[0] * m[7]) * d;
result[8] = ((double)m[0] * m[4] - (double)m[3] * m[1]) * d;
for(i = 0; i < 9; i++) m[i] = result[i];
for(i = 0; i < 9; i++) m[i] = (float)result[i];
return 0; /* ok */
}

Expand Down Expand Up @@ -1343,8 +1341,8 @@ unsigned convertFromXYZ(unsigned char* out, const float* in, unsigned w, unsigne
for(c = 0; c < 4; c++) {
size_t j = i * 8 + c * 2;
int i16 = (int)(0.5f + 65535.0f * LODEPNG_MIN(LODEPNG_MAX(0.0f, im[i * 4 + c]), 1.0f));
data[j + 0] = i16 >> 8;
data[j + 1] = i16 & 255;
data[j + 0] = (unsigned char)(i16 >> 8);
data[j + 1] = (unsigned char)(i16 & 255);
}
}
error = lodepng_convert(out, data, mode_out, &mode16, w, h);
Expand All @@ -1353,8 +1351,7 @@ unsigned convertFromXYZ(unsigned char* out, const float* in, unsigned w, unsigne
LodePNGColorMode mode8 = lodepng_color_mode_make(LCT_RGBA, 8);
for(i = 0; i < n; i++) {
for(c = 0; c < 4; c++) {
int i8 = (int)(0.5f + 255.0f * LODEPNG_MIN(LODEPNG_MAX(0.0f, im[i * 4 + c]), 1.0f));
data[i * 4 + c] = i8;
data[i * 4 + c] = (unsigned char)(0.5f + 255.0f * LODEPNG_MIN(LODEPNG_MAX(0.0f, im[i * 4 + c]), 1.0f));
}
}
error = lodepng_convert(out, data, mode_out, &mode8, w, h);
Expand Down Expand Up @@ -1755,11 +1752,11 @@ struct ExtractPNG { //PNG decoding and information extraction
}
};

void extractZlibInfo(std::vector<ZlibBlockInfo>& zlibinfo, const std::vector<unsigned char>& in) {
unsigned extractZlibInfo(std::vector<ZlibBlockInfo>& zlibinfo, const std::vector<unsigned char>& in) {
ExtractPNG decoder(&zlibinfo);
decoder.decode(&in[0], in.size());

if(decoder.error) std::cout << "extract error: " << decoder.error << std::endl;
return decoder.error ? 1 : 0;
}

} // namespace lodepng
5 changes: 3 additions & 2 deletions lodepng_util.h
@@ -1,7 +1,7 @@
/*
LodePNG Utils
Copyright (c) 2005-2020 Lode Vandevenne
Copyright (c) 2005-2022 Lode Vandevenne
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
Expand Down Expand Up @@ -283,7 +283,8 @@ struct ZlibBlockInfo {
};

//Extracts all info needed from a PNG file to reconstruct the zlib compression exactly.
void extractZlibInfo(std::vector<ZlibBlockInfo>& zlibinfo, const std::vector<unsigned char>& in);
// Returns 0 if no error, non-zero value if error
unsigned extractZlibInfo(std::vector<ZlibBlockInfo>& zlibinfo, const std::vector<unsigned char>& in);

} // namespace lodepng

Expand Down

0 comments on commit b4ed2cd

Please sign in to comment.