summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLode <lvandeve@gmail.com>2015-09-12 23:55:54 +0200
committerLode <lvandeve@gmail.com>2015-09-12 23:55:54 +0200
commit3d1b5d685fca913bcf56753f12c4b538da6878e8 (patch)
tree7609e2cdf424aac7d1bb12528d3cc126bb0a6b0b
parent0bceffb6eca66fe405c59f5c58c6ef2ff1be4add (diff)
add LODEPNG_NO_COMPILE_CRC compile option
-rw-r--r--examples/example_4bit_palette.cpp12
-rw-r--r--examples/example_encode_type.cpp79
-rw-r--r--lodepng.cpp7
-rw-r--r--lodepng.h4
4 files changed, 93 insertions, 9 deletions
diff --git a/examples/example_4bit_palette.cpp b/examples/example_4bit_palette.cpp
index f495cca..a5e610d 100644
--- a/examples/example_4bit_palette.cpp
+++ b/examples/example_4bit_palette.cpp
@@ -55,7 +55,7 @@ int main(int argc, char *argv[])
//create encoder and set settings and info (optional)
lodepng::State state;
-
+
//generate palette
for(int i = 0; i < 16; i++)
{
@@ -63,20 +63,20 @@ int main(int argc, char *argv[])
unsigned char g = 127 * (1 + std::sin(2 * i * 6.28318531 / 16));
unsigned char b = 127 * (1 + std::sin(3 * i * 6.28318531 / 16));
unsigned char a = 63 * (1 + std::sin(8 * i * 6.28318531 / 16)) + 128; /*alpha channel of the palette (tRNS chunk)*/
-
+
//palette must be added both to input and output color mode, because in this
//sample both the raw image and the expected PNG image use that palette.
lodepng_palette_add(&state.info_png.color, r, g, b, a);
lodepng_palette_add(&state.info_raw, r, g, b, a);
}
-
+
//both the raw image and the encoded image must get colorType 3 (palette)
state.info_png.color.colortype = LCT_PALETTE; //if you comment this line, and create the above palette in info_raw instead, then you get the same image in a RGBA PNG.
state.info_png.color.bitdepth = 4;
state.info_raw.colortype = LCT_PALETTE;
state.info_raw.bitdepth = 4;
state.encoder.auto_convert = 0; //we specify ourselves exactly what output PNG color mode we want
-
+
//generate some image
const unsigned w = 511;
const unsigned h = 511;
@@ -87,13 +87,13 @@ int main(int argc, char *argv[])
{
size_t byte_index = (y * w + x) / 2;
bool byte_half = (y * w + x) % 2 == 1;
-
+
int color = (int)(4 * ((1 + std::sin(2.0 * 6.28318531 * x / (double)w))
+ (1 + std::sin(2.0 * 6.28318531 * y / (double)h))) );
image[byte_index] |= (unsigned char)(color << (byte_half ? 0 : 4));
}
-
+
//encode and save
std::vector<unsigned char> buffer;
unsigned error = lodepng::encode(buffer, image.empty() ? 0 : &image[0], w, h, state);
diff --git a/examples/example_encode_type.cpp b/examples/example_encode_type.cpp
new file mode 100644
index 0000000..d1de926
--- /dev/null
+++ b/examples/example_encode_type.cpp
@@ -0,0 +1,79 @@
+/*
+LodePNG Examples
+
+Copyright (c) 2005-2015 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
+arising from the use of this software.
+
+Permission is granted to anyone to use this software for any purpose,
+including commercial applications, and to alter it and redistribute it
+freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+
+ 3. This notice may not be removed or altered from any source
+ distribution.
+*/
+
+//g++ -I ./ lodepng.cpp examples/example_encode_type.cpp -ansi -pedantic -Wall -Wextra -O3
+
+
+
+/*
+This example shows how to enforce a certain color type of the PNG image when
+encoding a PNG (because by default, LodePNG automatically chooses an optimal
+color type, no matter what your raw data's colro type is)
+*/
+
+#include <cmath>
+#include <iostream>
+
+#include "lodepng.h"
+
+int main(int argc, char *argv[])
+{
+ //check if user gave a filename
+ if(argc < 2)
+ {
+ std::cout << "please provide a filename to save to" << std::endl;
+ return 0;
+ }
+
+ //generate some image
+ const unsigned w = 256;
+ const unsigned h = 256;
+ std::vector<unsigned char> image(w * h * 4);
+ for(unsigned y = 0; y < h; y++)
+ for(unsigned x = 0; x < w; x++)
+ {
+ int index = y * w * 4 + x * 4;
+ image[index + 0] = 0;
+ image[index + 1] = 0;
+ image[index + 2] = 0;
+ image[index + 3] = 255;
+ }
+
+ // we're going to encode with a state rather than a convenient function, because enforcing a color type requires setting options
+ lodepng::State state;
+ // input color type
+ state.info_raw.colortype = LCT_RGBA;
+ state.info_raw.bitdepth = 8;
+ // output color type
+ state.info_png.color.colortype = LCT_RGBA;
+ state.info_png.color.bitdepth = 8;
+ state.encoder.auto_convert = 0; // without this, it would ignore the output color type specified above and choose an optimal one instead
+
+ //encode and save
+ std::vector<unsigned char> buffer;
+ unsigned error = lodepng::encode(buffer, &image[0], w, h, state);
+ if(error) std::cout << "encoder error " << error << ": "<< lodepng_error_text(error) << std::endl;
+ else lodepng::save_file(buffer, argv[1]);
+}
diff --git a/lodepng.cpp b/lodepng.cpp
index 326b952..ee87d04 100644
--- a/lodepng.cpp
+++ b/lodepng.cpp
@@ -1,5 +1,5 @@
/*
-LodePNG version 20150418
+LodePNG version 20150912
Copyright (c) 2005-2015 Lode Vandevenne
@@ -42,7 +42,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 = "20150418";
+const char* LODEPNG_VERSION_STRING = "20150912";
/*
This source file is built up in the following large parts. The code sections
@@ -2296,6 +2296,8 @@ const LodePNGDecompressSettings lodepng_default_decompress_settings = {0, 0, 0,
/* / CRC32 / */
/* ////////////////////////////////////////////////////////////////////////// */
+
+#ifndef LODEPNG_NO_COMPILE_CRC
/* CRC polynomial: 0xedb88320 */
static unsigned lodepng_crc32_table[256] = {
0u, 1996959894u, 3993919788u, 2567524794u, 124634137u, 1886057615u, 3915621685u, 2657392035u,
@@ -2344,6 +2346,7 @@ unsigned lodepng_crc32(const unsigned char* buf, size_t len)
}
return c ^ 0xffffffffL;
}
+#endif /* !LODEPNG_NO_COMPILE_CRC */
/* ////////////////////////////////////////////////////////////////////////// */
/* / Reading and writing single bits and bytes from/to stream for LodePNG / */
diff --git a/lodepng.h b/lodepng.h
index 74e4495..59f0dda 100644
--- a/lodepng.h
+++ b/lodepng.h
@@ -1,5 +1,5 @@
/*
-LodePNG version 20150418
+LodePNG version 20150912
Copyright (c) 2005-2015 Lode Vandevenne
@@ -41,6 +41,8 @@ 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.
*/
/*deflate & zlib. If disabled, you must specify alternative zlib functions in
the custom_zlib field of the compress and decompress settings*/