From 3d1b5d685fca913bcf56753f12c4b538da6878e8 Mon Sep 17 00:00:00 2001 From: Lode Date: Sat, 12 Sep 2015 23:55:54 +0200 Subject: add LODEPNG_NO_COMPILE_CRC compile option --- examples/example_4bit_palette.cpp | 12 +++--- examples/example_encode_type.cpp | 79 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 examples/example_encode_type.cpp (limited to 'examples') 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 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 +#include + +#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 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 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]); +} -- cgit v1.2.3