Skip to content

Commit ba9fc1f

Browse files
committed
relative intent and grayscale ICC support, and minor fixes
1 parent 83ffb87 commit ba9fc1f

File tree

6 files changed

+198
-95
lines changed

6 files changed

+198
-95
lines changed

lodepng.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
LodePNG version 20181230
2+
LodePNG version 20190210
33
4-
Copyright (c) 2005-2018 Lode Vandevenne
4+
Copyright (c) 2005-2019 Lode Vandevenne
55
66
This software is provided 'as-is', without any express or implied
77
warranty. In no event will the authors be held liable for any damages
@@ -30,16 +30,16 @@ Rename this file to lodepng.cpp to use it for C++, or to lodepng.c to use it for
3030

3131
#include "lodepng.h"
3232

33-
#include <limits.h>
34-
#include <stdio.h>
35-
#include <stdlib.h>
33+
#include <limits.h> /* LONG_MAX */
34+
#include <stdio.h> /* file handling */
35+
#include <stdlib.h> /* allocations */
3636

3737
#if defined(_MSC_VER) && (_MSC_VER >= 1310) /*Visual Studio: A few warning types are not desired here.*/
3838
#pragma warning( disable : 4244 ) /*implicit conversions: not warned by gcc -Wall -Wextra and requires too much casts*/
3939
#pragma warning( disable : 4996 ) /*VS does not like fopen, but fopen_s is not standard C so unusable here*/
4040
#endif /*_MSC_VER */
4141

42-
const char* LODEPNG_VERSION_STRING = "20181230";
42+
const char* LODEPNG_VERSION_STRING = "20190210";
4343

4444
/*
4545
This source file is built up in the following large parts. The code sections
@@ -2685,6 +2685,7 @@ static unsigned lodepng_assign_icc(LodePNGInfo* info, const char* name, const un
26852685

26862686
unsigned lodepng_set_icc(LodePNGInfo* info, const char* name, const unsigned char* profile, unsigned profile_size) {
26872687
if(info->iccp_name) lodepng_clear_icc(info);
2688+
info->iccp_defined = 1;
26882689

26892690
return lodepng_assign_icc(info, name, profile, profile_size);
26902691
}
@@ -2694,6 +2695,7 @@ void lodepng_clear_icc(LodePNGInfo* info) {
26942695
lodepng_free(info->iccp_profile);
26952696
info->iccp_profile = NULL;
26962697
info->iccp_profile_size = 0;
2698+
info->iccp_defined = 0;
26972699
}
26982700
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
26992701

@@ -4329,8 +4331,10 @@ static void decodeGeneric(unsigned char** out, unsigned* w, unsigned* h,
43294331
unsigned critical_pos = 1; /*1 = after IHDR, 2 = after PLTE, 3 = after IDAT*/
43304332
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
43314333

4332-
/*provide some proper output values if error will happen*/
4334+
4335+
/* safe output values in case error happens */
43334336
*out = 0;
4337+
*w = *h = 0;
43344338

43354339
state->error = lodepng_inspect(w, h, state, in, insize); /*reads header and resets other parameters in state->info_png*/
43364340
if(state->error) return;
@@ -4562,6 +4566,9 @@ unsigned lodepng_decode_file(unsigned char** out, unsigned* w, unsigned* h, cons
45624566
unsigned char* buffer = 0;
45634567
size_t buffersize;
45644568
unsigned error;
4569+
/* safe output values in case error happens */
4570+
*out = 0;
4571+
*w = *h = 0;
45654572
error = lodepng_load_file(&buffer, &buffersize, filename);
45664573
if(!error) error = lodepng_decode_memory(out, w, h, buffer, buffersize, colortype, bitdepth);
45674574
lodepng_free(buffer);
@@ -5913,6 +5920,8 @@ unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h,
59135920
unsigned decode(std::vector<unsigned char>& out, unsigned& w, unsigned& h, const std::string& filename,
59145921
LodePNGColorType colortype, unsigned bitdepth) {
59155922
std::vector<unsigned char> buffer;
5923+
/* safe output values in case error happens */
5924+
w = h = 0;
59165925
unsigned error = load_file(buffer, filename);
59175926
if(error) return error;
59185927
return decode(out, w, h, buffer, colortype, bitdepth);

lodepng.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
LodePNG version 20181230
2+
LodePNG version 20190210
33
4-
Copyright (c) 2005-2018 Lode Vandevenne
4+
Copyright (c) 2005-2019 Lode Vandevenne
55
66
This software is provided 'as-is', without any express or implied
77
warranty. In no event will the authors be held liable for any damages
@@ -1037,7 +1037,7 @@ unsigned compress(std::vector<unsigned char>& out, const std::vector<unsigned ch
10371037
[ ] make warnings like: oob palette, checksum fail, data after iend, wrong/unknown crit chunk, no null terminator in text, ...
10381038
[ ] error messages with line numbers (and version)
10391039
[ ] errors in state instead of as return code?
1040-
[ ] new errors/warnings like suspiciously big decompressed ztxt or iccp chunk
1040+
[ ] new errors/warnings like suspiciously big decompressed ztxt or iccp chunk
10411041
[ ] let the C++ wrapper catch exceptions coming from the standard library and return LodePNG error codes
10421042
[ ] allow user to provide custom color conversion functions, e.g. for premultiplied alpha, padding bits or not, ...
10431043
[ ] allow user to give data (void*) to custom allocator
@@ -1915,5 +1915,5 @@ Domain: gmail dot com.
19151915
Account: lode dot vandevenne.
19161916
19171917
1918-
Copyright (c) 2005-2018 Lode Vandevenne
1918+
Copyright (c) 2005-2019 Lode Vandevenne
19191919
*/

lodepng_unittest.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ mv lodepng.cpp lodepng.c ; gcc -I ./ lodepng.c examples/example_decode.c -pedant
4242
4343
*) try lodepng_benchmark.cpp
4444
g++ lodepng.cpp lodepng_benchmark.cpp -Wall -Wextra -pedantic -ansi -lSDL -O3 && ./a.out
45-
g++ lodepng.cpp lodepng_benchmark.cpp -Wall -Wextra -pedantic -ansi -lSDL -O3 && ./a.out corpus/''*
45+
g++ lodepng.cpp lodepng_benchmark.cpp -Wall -Wextra -pedantic -ansi -lSDL -O3 && ./a.out testdata/corpus/''*
4646
4747
*) Check if all examples compile without warnings:
4848
g++ -I ./ lodepng.cpp examples/''*.cpp -W -Wall -ansi -pedantic -O3 -c
@@ -82,7 +82,7 @@ g++ -DDISABLE_SLOW lodepng.cpp lodepng_util.cpp lodepng_unittest.cpp -Wall -Wext
8282
clang++ -fsanitize=address lodepng.cpp lodepng_util.cpp lodepng_unittest.cpp -Wall -Wextra -Wshadow -pedantic -ansi -O3 && ASAN_OPTIONS=allocator_may_return_null=1 ./a.out
8383
clang++ -fsanitize=address lodepng.cpp lodepng_util.cpp lodepng_unittest.cpp -Wall -Wextra -Wshadow -pedantic -ansi -g3 && ASAN_OPTIONS=allocator_may_return_null=1 ./a.out
8484
85-
*) remove "#include <iostream>" from lodepng.cpp if it's still in there
85+
*) remove "#include <iostream>" from lodepng.cpp if it's still in there (some are legit)
8686
cat lodepng.cpp lodepng_util.cpp | grep iostream
8787
cat lodepng.cpp lodepng_util.cpp | grep stdio
8888
cat lodepng.cpp lodepng_util.cpp | grep "#include"
@@ -2502,15 +2502,18 @@ void testXYZ() {
25022502

25032503
// Test sRGB -> XYZ -> sRGB roundtrip
25042504

2505+
unsigned rendering_intent = 3; // test with absolute for now
2506+
25052507
// 8-bit
25062508
{
25072509
// Default state, the conversions use 8-bit sRGB
25082510
lodepng::State state;
25092511
std::vector<float> f(w * h * 4);
2510-
assertNoError(lodepng::convertToXYZ(f.data(), v.data(), w, h, &state));
2512+
float whitepoint[3];
2513+
assertNoError(lodepng::convertToXYZ(f.data(), whitepoint, v.data(), w, h, &state));
25112514

25122515
std::vector<unsigned char> v2(w * h * 4);
2513-
assertNoError(lodepng::convertFromXYZ(v2.data(), f.data(), w, h, &state));
2516+
assertNoError(lodepng::convertFromXYZ(v2.data(), f.data(), w, h, whitepoint, rendering_intent, &state));
25142517

25152518
for(size_t i = 0; i < v2.size(); i++) {
25162519
ASSERT_EQUALS(v[i], v2[i]);
@@ -2523,10 +2526,11 @@ void testXYZ() {
25232526
lodepng::State state;
25242527
state.info_raw.bitdepth = 16;
25252528
std::vector<float> f(w * h * 4);
2526-
assertNoError(lodepng::convertToXYZ(f.data(), v.data(), w, h, &state));
2529+
float whitepoint[3];
2530+
assertNoError(lodepng::convertToXYZ(f.data(), whitepoint, v.data(), w, h, &state));
25272531

25282532
std::vector<unsigned char> v2(w * h * 8);
2529-
assertNoError(lodepng::convertFromXYZ(v2.data(), f.data(), w, h, &state));
2533+
assertNoError(lodepng::convertFromXYZ(v2.data(), f.data(), w, h, whitepoint, rendering_intent, &state));
25302534

25312535
for(size_t i = 0; i < v2.size(); i++) {
25322536
ASSERT_EQUALS(v[i], v2[i]);
@@ -2554,10 +2558,11 @@ void testXYZ() {
25542558
lodepng::State state;
25552559
lodepng_info_copy(&state.info_png, &info_custom);
25562560
std::vector<float> f(w * h * 4);
2557-
assertNoError(lodepng::convertToXYZ(f.data(), v.data(), w, h, &state));
2561+
float whitepoint[3];
2562+
assertNoError(lodepng::convertToXYZ(f.data(), whitepoint, v.data(), w, h, &state));
25582563

25592564
std::vector<unsigned char> v2(w * h * 4);
2560-
assertNoError(lodepng::convertFromXYZ(v2.data(), f.data(), w, h, &state));
2565+
assertNoError(lodepng::convertFromXYZ(v2.data(), f.data(), w, h, whitepoint, rendering_intent, &state));
25612566

25622567
for(size_t i = 0; i < v2.size(); i++) {
25632568
// Allow near instead of exact due to numerical issues with low values,
@@ -2576,10 +2581,11 @@ void testXYZ() {
25762581
lodepng_info_copy(&state.info_png, &info_custom);
25772582
state.info_raw.bitdepth = 16;
25782583
std::vector<float> f(w * h * 4);
2579-
assertNoError(lodepng::convertToXYZ(f.data(), v.data(), w, h, &state));
2584+
float whitepoint[3];
2585+
assertNoError(lodepng::convertToXYZ(f.data(), whitepoint, v.data(), w, h, &state));
25802586

25812587
std::vector<unsigned char> v2(w * h * 8);
2582-
assertNoError(lodepng::convertFromXYZ(v2.data(), f.data(), w, h, &state));
2588+
assertNoError(lodepng::convertFromXYZ(v2.data(), f.data(), w, h, whitepoint, rendering_intent, &state));
25832589

25842590
for(size_t i = 0; i < v2.size(); i += 2) {
25852591
unsigned a = v[i + 0] * 256u + v[i + 1];

0 commit comments

Comments
 (0)