Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/lvandeve/lodepng.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLode <lvandeve@gmail.com>2018-12-30 16:35:48 +0300
committerLode <lvandeve@gmail.com>2018-12-30 16:35:48 +0300
commit472d085f80c8a5a8e46c162b79f5e4e28fd5515b (patch)
tree2fe076b8d7390414c7d2a291a8fd3a1a6f0515c8 /examples
parent2e6b2bac160f153f3890a8a10810c4a7f4ad3aed (diff)
change brace indent style: don't break before opening brace
Diffstat (limited to 'examples')
-rw-r--r--examples/example_4bit_palette.cpp15
-rw-r--r--examples/example_bmp2png.cpp33
-rw-r--r--examples/example_decode.c222
-rw-r--r--examples/example_decode.cpp186
-rw-r--r--examples/example_encode.c227
-rw-r--r--examples/example_encode.cpp189
-rw-r--r--examples/example_encode_type.cpp155
-rw-r--r--examples/example_gzip.cpp26
-rw-r--r--examples/example_opengl.cpp45
-rw-r--r--examples/example_optimize_png.cpp34
-rw-r--r--examples/example_png2bmp.cpp33
-rw-r--r--examples/example_png_info.cpp96
-rw-r--r--examples/example_reencode.cpp26
-rw-r--r--examples/example_sdl.c27
-rw-r--r--examples/example_sdl.cpp30
15 files changed, 616 insertions, 728 deletions
diff --git a/examples/example_4bit_palette.cpp b/examples/example_4bit_palette.cpp
index a5e610d..1490ddf 100644
--- a/examples/example_4bit_palette.cpp
+++ b/examples/example_4bit_palette.cpp
@@ -44,11 +44,9 @@ Gimp 2.8 image editor (until you set mode to RGB).
#include "lodepng.h"
-int main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
//check if user gave a filename
- if(argc < 2)
- {
+ if(argc < 2) {
std::cout << "please provide a filename to save to" << std::endl;
return 0;
}
@@ -57,8 +55,7 @@ int main(int argc, char *argv[])
lodepng::State state;
//generate palette
- for(int i = 0; i < 16; i++)
- {
+ for(int i = 0; i < 16; i++) {
unsigned char r = 127 * (1 + std::sin(5 * i * 6.28318531 / 16));
unsigned char g = 127 * (1 + std::sin(2 * i * 6.28318531 / 16));
unsigned char b = 127 * (1 + std::sin(3 * i * 6.28318531 / 16));
@@ -83,8 +80,7 @@ int main(int argc, char *argv[])
std::vector<unsigned char> image;
image.resize((w * h * 4 + 7) / 8, 0);
for(unsigned y = 0; y < h; y++)
- for(unsigned x = 0; x < w; x++)
- {
+ for(unsigned x = 0; x < w; x++) {
size_t byte_index = (y * w + x) / 2;
bool byte_half = (y * w + x) % 2 == 1;
@@ -97,8 +93,7 @@ int main(int argc, char *argv[])
//encode and save
std::vector<unsigned char> buffer;
unsigned error = lodepng::encode(buffer, image.empty() ? 0 : &image[0], w, h, state);
- if(error)
- {
+ if(error) {
std::cout << "encoder error " << error << ": "<< lodepng_error_text(error) << std::endl;
return 0;
}
diff --git a/examples/example_bmp2png.cpp b/examples/example_bmp2png.cpp
index c6bdd24..9254bde 100644
--- a/examples/example_bmp2png.cpp
+++ b/examples/example_bmp2png.cpp
@@ -44,10 +44,9 @@ NOTE: it overwrites the output file without warning if it exists!
//returns 0 if all went ok, non-0 if error
//output image is always given in RGBA (with alpha channel), even if it's a BMP without alpha channel
-unsigned decodeBMP(std::vector<unsigned char>& image, unsigned& w, unsigned& h, const std::vector<unsigned char>& bmp)
-{
+unsigned decodeBMP(std::vector<unsigned char>& image, unsigned& w, unsigned& h, const std::vector<unsigned char>& bmp) {
static const unsigned MINHEADER = 54; //minimum BMP header size
-
+
if(bmp.size() < MINHEADER) return -1;
if(bmp[0] != 'B' || bmp[1] != 'M') return 1; //It's not a BMP file if it doesn't start with marker 'BM'
unsigned pixeloffset = bmp[10] + 256 * bmp[11]; //where the pixel data starts
@@ -76,21 +75,17 @@ unsigned decodeBMP(std::vector<unsigned char>& image, unsigned& w, unsigned& h,
The 2D for loop below does all these 3 conversions at once.
*/
for(unsigned y = 0; y < h; y++)
- for(unsigned x = 0; x < w; x++)
- {
+ for(unsigned x = 0; x < w; x++) {
//pixel start byte position in the BMP
unsigned bmpos = pixeloffset + (h - y - 1) * scanlineBytes + numChannels * x;
//pixel start byte position in the new raw image
unsigned newpos = 4 * y * w + 4 * x;
- if(numChannels == 3)
- {
+ if(numChannels == 3) {
image[newpos + 0] = bmp[bmpos + 2]; //R
image[newpos + 1] = bmp[bmpos + 1]; //G
image[newpos + 2] = bmp[bmpos + 0]; //B
image[newpos + 3] = 255; //A
- }
- else
- {
+ } else {
image[newpos + 0] = bmp[bmpos + 3]; //R
image[newpos + 1] = bmp[bmpos + 2]; //G
image[newpos + 2] = bmp[bmpos + 1]; //B
@@ -100,10 +95,8 @@ unsigned decodeBMP(std::vector<unsigned char>& image, unsigned& w, unsigned& h,
return 0;
}
-int main(int argc, char *argv[])
-{
- if(argc < 3)
- {
+int main(int argc, char *argv[]) {
+ if(argc < 3) {
std::cout << "Please provice input PNG and output BMP file names" << std::endl;
return 0;
}
@@ -114,21 +107,19 @@ int main(int argc, char *argv[])
unsigned w, h;
unsigned error = decodeBMP(image, w, h, bmp);
- if(error)
- {
+ if(error) {
std::cout << "BMP decoding error " << error << std::endl;
return 0;
}
-
+
std::vector<unsigned char> png;
error = lodepng::encode(png, image, w, h);
- if(error)
- {
+ if(error) {
std::cout << "PNG encoding error " << error << ": " << lodepng_error_text(error) << std::endl;
return 0;
}
-
+
lodepng::save_file(png, argv[2]);
-
+
}
diff --git a/examples/example_decode.c b/examples/example_decode.c
index 8f6ce39..fc8c0f2 100644
--- a/examples/example_decode.c
+++ b/examples/example_decode.c
@@ -1,113 +1,109 @@
-/*
-LodePNG Examples
-
-Copyright (c) 2005-2012 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.
-*/
-
-#include "lodepng.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-
-/*
-3 ways to decode a PNG from a file to RGBA pixel data (and 2 in-memory ways).
-*/
-
-/*
-Example 1
-Decode from disk to raw pixels with a single function call
-*/
-void decodeOneStep(const char* filename)
-{
- unsigned error;
- unsigned char* image;
- unsigned width, height;
-
- error = lodepng_decode32_file(&image, &width, &height, filename);
- if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
-
- /*use image here*/
-
- free(image);
-}
-
-/*
-Example 2
-Load PNG file from disk to memory first, then decode to raw pixels in memory.
-*/
-void decodeTwoSteps(const char* filename)
-{
- unsigned error;
- unsigned char* image;
- unsigned width, height;
- unsigned char* png = 0;
- size_t pngsize;
-
- error = lodepng_load_file(&png, &pngsize, filename);
- if(!error) error = lodepng_decode32(&image, &width, &height, png, pngsize);
- if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
-
- free(png);
-
- /*use image here*/
-
- free(image);
-}
-
-/*
-Example 3
-Load PNG file from disk using a State, normally needed for more advanced usage.
-*/
-void decodeWithState(const char* filename)
-{
- unsigned error;
- unsigned char* image;
- unsigned width, height;
- unsigned char* png = 0;
- size_t pngsize;
- LodePNGState state;
-
- lodepng_state_init(&state);
- /*optionally customize the state*/
-
- error = lodepng_load_file(&png, &pngsize, filename);
- if(!error) error = lodepng_decode(&image, &width, &height, &state, png, pngsize);
- if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
-
- free(png);
-
- /*use image here*/
- /*state contains extra information about the PNG such as text chunks, ...*/
-
- lodepng_state_cleanup(&state);
- free(image);
-}
-
-int main(int argc, char *argv[])
-{
- const char* filename = argc > 1 ? argv[1] : "test.png";
-
- decodeOneStep(filename);
-
- return 0;
-}
-
+/*
+LodePNG Examples
+
+Copyright (c) 2005-2012 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.
+*/
+
+#include "lodepng.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+/*
+3 ways to decode a PNG from a file to RGBA pixel data (and 2 in-memory ways).
+*/
+
+/*
+Example 1
+Decode from disk to raw pixels with a single function call
+*/
+void decodeOneStep(const char* filename) {
+ unsigned error;
+ unsigned char* image;
+ unsigned width, height;
+
+ error = lodepng_decode32_file(&image, &width, &height, filename);
+ if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
+
+ /*use image here*/
+
+ free(image);
+}
+
+/*
+Example 2
+Load PNG file from disk to memory first, then decode to raw pixels in memory.
+*/
+void decodeTwoSteps(const char* filename) {
+ unsigned error;
+ unsigned char* image;
+ unsigned width, height;
+ unsigned char* png = 0;
+ size_t pngsize;
+
+ error = lodepng_load_file(&png, &pngsize, filename);
+ if(!error) error = lodepng_decode32(&image, &width, &height, png, pngsize);
+ if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
+
+ free(png);
+
+ /*use image here*/
+
+ free(image);
+}
+
+/*
+Example 3
+Load PNG file from disk using a State, normally needed for more advanced usage.
+*/
+void decodeWithState(const char* filename) {
+ unsigned error;
+ unsigned char* image;
+ unsigned width, height;
+ unsigned char* png = 0;
+ size_t pngsize;
+ LodePNGState state;
+
+ lodepng_state_init(&state);
+ /*optionally customize the state*/
+
+ error = lodepng_load_file(&png, &pngsize, filename);
+ if(!error) error = lodepng_decode(&image, &width, &height, &state, png, pngsize);
+ if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
+
+ free(png);
+
+ /*use image here*/
+ /*state contains extra information about the PNG such as text chunks, ...*/
+
+ lodepng_state_cleanup(&state);
+ free(image);
+}
+
+int main(int argc, char *argv[]) {
+ const char* filename = argc > 1 ? argv[1] : "test.png";
+
+ decodeOneStep(filename);
+
+ return 0;
+}
+
diff --git a/examples/example_decode.cpp b/examples/example_decode.cpp
index 76cda6e..3a77840 100644
--- a/examples/example_decode.cpp
+++ b/examples/example_decode.cpp
@@ -1,95 +1,91 @@
-/*
-LodePNG Examples
-
-Copyright (c) 2005-2012 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.
-*/
-
-#include "lodepng.h"
-#include <iostream>
-
-/*
-3 ways to decode a PNG from a file to RGBA pixel data (and 2 in-memory ways).
-*/
-
-//g++ lodepng.cpp example_decode.cpp -ansi -pedantic -Wall -Wextra -O3
-
-
-//Example 1
-//Decode from disk to raw pixels with a single function call
-void decodeOneStep(const char* filename)
-{
- std::vector<unsigned char> image; //the raw pixels
- unsigned width, height;
-
- //decode
- unsigned error = lodepng::decode(image, width, height, filename);
-
- //if there's an error, display it
- if(error) std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
-
- //the pixels are now in the vector "image", 4 bytes per pixel, ordered RGBARGBA..., use it as texture, draw it, ...
-}
-
-//Example 2
-//Load PNG file from disk to memory first, then decode to raw pixels in memory.
-void decodeTwoSteps(const char* filename)
-{
- std::vector<unsigned char> png;
- std::vector<unsigned char> image; //the raw pixels
- unsigned width, height;
-
- //load and decode
- unsigned error = lodepng::load_file(png, filename);
- if(!error) error = lodepng::decode(image, width, height, png);
-
- //if there's an error, display it
- if(error) std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
-
- //the pixels are now in the vector "image", 4 bytes per pixel, ordered RGBARGBA..., use it as texture, draw it, ...
-}
-
-//Example 3
-//Load PNG file from disk using a State, normally needed for more advanced usage.
-void decodeWithState(const char* filename)
-{
- std::vector<unsigned char> png;
- std::vector<unsigned char> image; //the raw pixels
- unsigned width, height;
- lodepng::State state; //optionally customize this one
-
- unsigned error = lodepng::load_file(png, filename); //load the image file with given filename
- if(!error) error = lodepng::decode(image, width, height, state, png);
-
- //if there's an error, display it
- if(error) std::cout << "decoder error " << error << ": "<< lodepng_error_text(error) << std::endl;
-
- //the pixels are now in the vector "image", 4 bytes per pixel, ordered RGBARGBA..., use it as texture, draw it, ...
- //State state contains extra information about the PNG such as text chunks, ...
-}
-
-int main(int argc, char *argv[])
-{
- const char* filename = argc > 1 ? argv[1] : "test.png";
-
- decodeOneStep(filename);
-}
-
+/*
+LodePNG Examples
+
+Copyright (c) 2005-2012 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.
+*/
+
+#include "lodepng.h"
+#include <iostream>
+
+/*
+3 ways to decode a PNG from a file to RGBA pixel data (and 2 in-memory ways).
+*/
+
+//g++ lodepng.cpp example_decode.cpp -ansi -pedantic -Wall -Wextra -O3
+
+
+//Example 1
+//Decode from disk to raw pixels with a single function call
+void decodeOneStep(const char* filename) {
+ std::vector<unsigned char> image; //the raw pixels
+ unsigned width, height;
+
+ //decode
+ unsigned error = lodepng::decode(image, width, height, filename);
+
+ //if there's an error, display it
+ if(error) std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
+
+ //the pixels are now in the vector "image", 4 bytes per pixel, ordered RGBARGBA..., use it as texture, draw it, ...
+}
+
+//Example 2
+//Load PNG file from disk to memory first, then decode to raw pixels in memory.
+void decodeTwoSteps(const char* filename) {
+ std::vector<unsigned char> png;
+ std::vector<unsigned char> image; //the raw pixels
+ unsigned width, height;
+
+ //load and decode
+ unsigned error = lodepng::load_file(png, filename);
+ if(!error) error = lodepng::decode(image, width, height, png);
+
+ //if there's an error, display it
+ if(error) std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
+
+ //the pixels are now in the vector "image", 4 bytes per pixel, ordered RGBARGBA..., use it as texture, draw it, ...
+}
+
+//Example 3
+//Load PNG file from disk using a State, normally needed for more advanced usage.
+void decodeWithState(const char* filename) {
+ std::vector<unsigned char> png;
+ std::vector<unsigned char> image; //the raw pixels
+ unsigned width, height;
+ lodepng::State state; //optionally customize this one
+
+ unsigned error = lodepng::load_file(png, filename); //load the image file with given filename
+ if(!error) error = lodepng::decode(image, width, height, state, png);
+
+ //if there's an error, display it
+ if(error) std::cout << "decoder error " << error << ": "<< lodepng_error_text(error) << std::endl;
+
+ //the pixels are now in the vector "image", 4 bytes per pixel, ordered RGBARGBA..., use it as texture, draw it, ...
+ //State state contains extra information about the PNG such as text chunks, ...
+}
+
+int main(int argc, char *argv[]) {
+ const char* filename = argc > 1 ? argv[1] : "test.png";
+
+ decodeOneStep(filename);
+}
+
diff --git a/examples/example_encode.c b/examples/example_encode.c
index 0e0f8bf..ce43aba 100644
--- a/examples/example_encode.c
+++ b/examples/example_encode.c
@@ -1,116 +1,111 @@
-/*
-LodePNG Examples
-
-Copyright (c) 2005-2012 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.
-*/
-
-#include "lodepng.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-
-/*
-3 ways to encode a PNG from RGBA pixel data to a file (and 2 in-memory ways).
-NOTE: this samples overwrite the file or test.png without warning!
-*/
-
-/*
-Example 1
-Encode from raw pixels to disk with a single function call
-The image argument has width * height RGBA pixels or width * height * 4 bytes
-*/
-void encodeOneStep(const char* filename, const unsigned char* image, unsigned width, unsigned height)
-{
- /*Encode the image*/
- unsigned error = lodepng_encode32_file(filename, image, width, height);
-
- /*if there's an error, display it*/
- if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
-}
-
-/*
-Example 2
-Encode from raw pixels to an in-memory PNG file first, then write it to disk
-The image argument has width * height RGBA pixels or width * height * 4 bytes
-*/
-void encodeTwoSteps(const char* filename, const unsigned char* image, unsigned width, unsigned height)
-{
- unsigned char* png;
- size_t pngsize;
-
- unsigned error = lodepng_encode32(&png, &pngsize, image, width, height);
- if(!error) lodepng_save_file(png, pngsize, filename);
-
- /*if there's an error, display it*/
- if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
-
- free(png);
-}
-
-/*
-Example 3
-Save a PNG file to disk using a State, normally needed for more advanced usage.
-The image argument has width * height RGBA pixels or width * height * 4 bytes
-*/
-void encodeWithState(const char* filename, const unsigned char* image, unsigned width, unsigned height)
-{
- unsigned error;
- unsigned char* png;
- size_t pngsize;
- LodePNGState state;
-
- lodepng_state_init(&state);
- /*optionally customize the state*/
-
- error = lodepng_encode(&png, &pngsize, image, width, height, &state);
- if(!error) lodepng_save_file(png, pngsize, filename);
-
- /*if there's an error, display it*/
- if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
-
- lodepng_state_cleanup(&state);
- free(png);
-}
-
-int main(int argc, char *argv[])
-{
- const char* filename = argc > 1 ? argv[1] : "test.png";
-
- /*generate some image*/
- unsigned width = 512, height = 512;
- unsigned char* image = malloc(width * height * 4);
- unsigned x, y;
- for(y = 0; y < height; y++)
- for(x = 0; x < width; x++)
- {
- image[4 * width * y + 4 * x + 0] = 255 * !(x & y);
- image[4 * width * y + 4 * x + 1] = x ^ y;
- image[4 * width * y + 4 * x + 2] = x | y;
- image[4 * width * y + 4 * x + 3] = 255;
- }
-
- /*run an example*/
- encodeOneStep(filename, image, width, height);
-
- free(image);
- return 0;
-}
+/*
+LodePNG Examples
+
+Copyright (c) 2005-2012 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.
+*/
+
+#include "lodepng.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+/*
+3 ways to encode a PNG from RGBA pixel data to a file (and 2 in-memory ways).
+NOTE: this samples overwrite the file or test.png without warning!
+*/
+
+/*
+Example 1
+Encode from raw pixels to disk with a single function call
+The image argument has width * height RGBA pixels or width * height * 4 bytes
+*/
+void encodeOneStep(const char* filename, const unsigned char* image, unsigned width, unsigned height) {
+ /*Encode the image*/
+ unsigned error = lodepng_encode32_file(filename, image, width, height);
+
+ /*if there's an error, display it*/
+ if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
+}
+
+/*
+Example 2
+Encode from raw pixels to an in-memory PNG file first, then write it to disk
+The image argument has width * height RGBA pixels or width * height * 4 bytes
+*/
+void encodeTwoSteps(const char* filename, const unsigned char* image, unsigned width, unsigned height) {
+ unsigned char* png;
+ size_t pngsize;
+
+ unsigned error = lodepng_encode32(&png, &pngsize, image, width, height);
+ if(!error) lodepng_save_file(png, pngsize, filename);
+
+ /*if there's an error, display it*/
+ if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
+
+ free(png);
+}
+
+/*
+Example 3
+Save a PNG file to disk using a State, normally needed for more advanced usage.
+The image argument has width * height RGBA pixels or width * height * 4 bytes
+*/
+void encodeWithState(const char* filename, const unsigned char* image, unsigned width, unsigned height) {
+ unsigned error;
+ unsigned char* png;
+ size_t pngsize;
+ LodePNGState state;
+
+ lodepng_state_init(&state);
+ /*optionally customize the state*/
+
+ error = lodepng_encode(&png, &pngsize, image, width, height, &state);
+ if(!error) lodepng_save_file(png, pngsize, filename);
+
+ /*if there's an error, display it*/
+ if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
+
+ lodepng_state_cleanup(&state);
+ free(png);
+}
+
+int main(int argc, char *argv[]) {
+ const char* filename = argc > 1 ? argv[1] : "test.png";
+
+ /*generate some image*/
+ unsigned width = 512, height = 512;
+ unsigned char* image = malloc(width * height * 4);
+ unsigned x, y;
+ for(y = 0; y < height; y++)
+ for(x = 0; x < width; x++) {
+ image[4 * width * y + 4 * x + 0] = 255 * !(x & y);
+ image[4 * width * y + 4 * x + 1] = x ^ y;
+ image[4 * width * y + 4 * x + 2] = x | y;
+ image[4 * width * y + 4 * x + 3] = 255;
+ }
+
+ /*run an example*/
+ encodeOneStep(filename, image, width, height);
+
+ free(image);
+ return 0;
+}
diff --git a/examples/example_encode.cpp b/examples/example_encode.cpp
index 0258e62..228ac03 100644
--- a/examples/example_encode.cpp
+++ b/examples/example_encode.cpp
@@ -1,97 +1,92 @@
-/*
-LodePNG Examples
-
-Copyright (c) 2005-2012 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.
-*/
-
-#include "lodepng.h"
-#include <iostream>
-
-/*
-3 ways to encode a PNG from RGBA pixel data to a file (and 2 in-memory ways).
-NOTE: this samples overwrite the file or test.png without warning!
-*/
-
-//g++ lodepng.cpp examples/example_encode.cpp -I./ -ansi -pedantic -Wall -Wextra -O3
-
-//Example 1
-//Encode from raw pixels to disk with a single function call
-//The image argument has width * height RGBA pixels or width * height * 4 bytes
-void encodeOneStep(const char* filename, std::vector<unsigned char>& image, unsigned width, unsigned height)
-{
- //Encode the image
- unsigned error = lodepng::encode(filename, image, width, height);
-
- //if there's an error, display it
- if(error) std::cout << "encoder error " << error << ": "<< lodepng_error_text(error) << std::endl;
-}
-
-//Example 2
-//Encode from raw pixels to an in-memory PNG file first, then write it to disk
-//The image argument has width * height RGBA pixels or width * height * 4 bytes
-void encodeTwoSteps(const char* filename, std::vector<unsigned char>& image, unsigned width, unsigned height)
-{
- std::vector<unsigned char> png;
-
- unsigned error = lodepng::encode(png, image, width, height);
- if(!error) lodepng::save_file(png, filename);
-
- //if there's an error, display it
- if(error) std::cout << "encoder error " << error << ": "<< lodepng_error_text(error) << std::endl;
-}
-
-//Example 3
-//Save a PNG file to disk using a State, normally needed for more advanced usage.
-//The image argument has width * height RGBA pixels or width * height * 4 bytes
-void encodeWithState(const char* filename, std::vector<unsigned char>& image, unsigned width, unsigned height)
-{
- std::vector<unsigned char> png;
- lodepng::State state; //optionally customize this one
-
- unsigned error = lodepng::encode(png, image, width, height, state);
- if(!error) lodepng::save_file(png, filename);
-
- //if there's an error, display it
- if(error) std::cout << "encoder error " << error << ": "<< lodepng_error_text(error) << std::endl;
-}
-
-//saves image to filename given as argument. Warning, this overwrites the file without warning!
-int main(int argc, char *argv[])
-{
- //NOTE: this sample will overwrite the file or test.png without warning!
- const char* filename = argc > 1 ? argv[1] : "test.png";
-
- //generate some image
- unsigned width = 512, height = 512;
- std::vector<unsigned char> image;
- image.resize(width * height * 4);
- for(unsigned y = 0; y < height; y++)
- for(unsigned x = 0; x < width; x++)
- {
- image[4 * width * y + 4 * x + 0] = 255 * !(x & y);
- image[4 * width * y + 4 * x + 1] = x ^ y;
- image[4 * width * y + 4 * x + 2] = x | y;
- image[4 * width * y + 4 * x + 3] = 255;
- }
-
- encodeOneStep(filename, image, width, height);
-}
+/*
+LodePNG Examples
+
+Copyright (c) 2005-2012 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.
+*/
+
+#include "lodepng.h"
+#include <iostream>
+
+/*
+3 ways to encode a PNG from RGBA pixel data to a file (and 2 in-memory ways).
+NOTE: this samples overwrite the file or test.png without warning!
+*/
+
+//g++ lodepng.cpp examples/example_encode.cpp -I./ -ansi -pedantic -Wall -Wextra -O3
+
+//Example 1
+//Encode from raw pixels to disk with a single function call
+//The image argument has width * height RGBA pixels or width * height * 4 bytes
+void encodeOneStep(const char* filename, std::vector<unsigned char>& image, unsigned width, unsigned height) {
+ //Encode the image
+ unsigned error = lodepng::encode(filename, image, width, height);
+
+ //if there's an error, display it
+ if(error) std::cout << "encoder error " << error << ": "<< lodepng_error_text(error) << std::endl;
+}
+
+//Example 2
+//Encode from raw pixels to an in-memory PNG file first, then write it to disk
+//The image argument has width * height RGBA pixels or width * height * 4 bytes
+void encodeTwoSteps(const char* filename, std::vector<unsigned char>& image, unsigned width, unsigned height) {
+ std::vector<unsigned char> png;
+
+ unsigned error = lodepng::encode(png, image, width, height);
+ if(!error) lodepng::save_file(png, filename);
+
+ //if there's an error, display it
+ if(error) std::cout << "encoder error " << error << ": "<< lodepng_error_text(error) << std::endl;
+}
+
+//Example 3
+//Save a PNG file to disk using a State, normally needed for more advanced usage.
+//The image argument has width * height RGBA pixels or width * height * 4 bytes
+void encodeWithState(const char* filename, std::vector<unsigned char>& image, unsigned width, unsigned height) {
+ std::vector<unsigned char> png;
+ lodepng::State state; //optionally customize this one
+
+ unsigned error = lodepng::encode(png, image, width, height, state);
+ if(!error) lodepng::save_file(png, filename);
+
+ //if there's an error, display it
+ if(error) std::cout << "encoder error " << error << ": "<< lodepng_error_text(error) << std::endl;
+}
+
+//saves image to filename given as argument. Warning, this overwrites the file without warning!
+int main(int argc, char *argv[]) {
+ //NOTE: this sample will overwrite the file or test.png without warning!
+ const char* filename = argc > 1 ? argv[1] : "test.png";
+
+ //generate some image
+ unsigned width = 512, height = 512;
+ std::vector<unsigned char> image;
+ image.resize(width * height * 4);
+ for(unsigned y = 0; y < height; y++)
+ for(unsigned x = 0; x < width; x++) {
+ image[4 * width * y + 4 * x + 0] = 255 * !(x & y);
+ image[4 * width * y + 4 * x + 1] = x ^ y;
+ image[4 * width * y + 4 * x + 2] = x | y;
+ image[4 * width * y + 4 * x + 3] = 255;
+ }
+
+ encodeOneStep(filename, image, width, height);
+}
diff --git a/examples/example_encode_type.cpp b/examples/example_encode_type.cpp
index a28e38d..c133b24 100644
--- a/examples/example_encode_type.cpp
+++ b/examples/example_encode_type.cpp
@@ -1,79 +1,76 @@
-/*
-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 color 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]);
-}
+/*
+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 color 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/examples/example_gzip.cpp b/examples/example_gzip.cpp
index b2ddf17..60545aa 100644
--- a/examples/example_gzip.cpp
+++ b/examples/example_gzip.cpp
@@ -36,21 +36,19 @@ See also the gzip specification, RFC 1952: http://www.gzip.org/zlib/rfc-gzip.htm
//g++ lodepng.cpp example_gzip.cpp -ansi -pedantic -Wall -Wextra -O3
//saves image to filename given as argument. Warning, this overwrites the file without warning!
-int main(int argc, char *argv[])
-{
- if(argc < 2)
- {
+int main(int argc, char *argv[]) {
+ if(argc < 2) {
std::cout << "Please provide input filename (output is input with .gz)" << std::endl;
return 0;
}
-
+
//NOTE: this sample will overwrite the output file without warning!
std::string infilename = argv[1];
std::string outfilename = infilename + ".gz";
-
+
std::vector<unsigned char> in;
lodepng::load_file(in, infilename);
-
+
size_t outsize = 10;
unsigned char* out = (unsigned char*)malloc(outsize);
out[0] = 31; //ID1
@@ -65,16 +63,16 @@ int main(int argc, char *argv[])
out[8] = 2; //2 = slow, 4 = fast compression
out[9] = 255; //OS unknown
-
+
lodepng_deflate(&out, &outsize, &in[0], in.size(), &lodepng_default_compress_settings);
-
+
unsigned crc = lodepng_crc32(&in[0], in.size());
-
+
size_t footer = outsize;
-
+
outsize += 8;
out = (unsigned char*)realloc(out, outsize);
-
+
//CRC
out[footer + 0] = crc % 256;
out[footer + 1] = (crc >> 8) % 256;
@@ -86,8 +84,8 @@ int main(int argc, char *argv[])
out[footer + 5] = (in.size() >> 8) % 256;
out[footer + 6] = (in.size() >> 16) % 256;
out[footer + 7] = (in.size() >> 24) % 256;
-
+
lodepng_save_file(out, outsize, outfilename.c_str());
-
+
free(out);
}
diff --git a/examples/example_opengl.cpp b/examples/example_opengl.cpp
index 1879864..a4352eb 100644
--- a/examples/example_opengl.cpp
+++ b/examples/example_opengl.cpp
@@ -46,10 +46,8 @@ shows LodePNG can be used to load PNG images as textures in OpenGL.
#include <SDL/SDL.h>
#include <GL/gl.h>
-int main(int argc, char *argv[])
-{
- if(argc < 2)
- {
+int main(int argc, char *argv[]) {
+ if(argc < 2) {
std::cout << "Please provide a filename." << std::endl;
return 1;
}
@@ -61,12 +59,11 @@ int main(int argc, char *argv[])
unsigned error = lodepng::decode(image, width, height, filename);
// If there's an error, display it.
- if(error != 0)
- {
+ if(error != 0) {
std::cout << "error " << error << ": " << lodepng_error_text(error) << std::endl;
return 1;
}
-
+
// Here the PNG is loaded in "image". All the rest of the code is SDL and OpenGL stuff.
int screenw = width;
@@ -74,16 +71,14 @@ int main(int argc, char *argv[])
int screenh = height;
if(screenh > 768) screenw = 768;
- if(SDL_Init(SDL_INIT_VIDEO) < 0)
- {
+ if(SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cout << "Error: Unable to init SDL: " << SDL_GetError() << std::endl;
return 1;
}
-
+
SDL_Surface* scr = SDL_SetVideoMode(screenw, screenh, 32, SDL_OPENGL);
- if(scr == 0)
- {
+ if(scr == 0) {
std::cout << "Error: Unable to set video. SDL error message: " << SDL_GetError() << std::endl;
return 1;
}
@@ -103,12 +98,11 @@ int main(int argc, char *argv[])
glEnable(GL_BLEND);
glDisable(GL_ALPHA_TEST);
- if(glGetError() != GL_NO_ERROR)
- {
+ if(glGetError() != GL_NO_ERROR) {
std::cout << "Error initing GL" << std::endl;
return 1;
}
-
+
// Texture size must be power of two for the primitive OpenGL version this is written for. Find next power of two.
size_t u2 = 1; while(u2 < width) u2 *= 2;
size_t v2 = 1; while(v2 < height) v2 *= 2;
@@ -120,29 +114,26 @@ int main(int argc, char *argv[])
std::vector<unsigned char> image2(u2 * v2 * 4);
for(size_t y = 0; y < height; y++)
for(size_t x = 0; x < width; x++)
- for(size_t c = 0; c < 4; c++)
- {
+ for(size_t c = 0; c < 4; c++) {
image2[4 * u2 * y + 4 * x + c] = image[4 * width * y + 4 * x + c];
}
-
+
// Enable the texture for OpenGL.
glEnable(GL_TEXTURE_2D);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); //GL_NEAREST = no smoothing
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 4, u2, v2, 0, GL_RGBA, GL_UNSIGNED_BYTE, &image2[0]);
-
+
bool done = false;
SDL_Event event = {0};
glColor4ub(255, 255, 255, 255);
-
- while(!done)
- {
+
+ while(!done) {
// Quit the loop when receiving quit event.
- while(SDL_PollEvent(&event))
- {
+ while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) done = 1;
}
-
+
// Draw the texture on a quad, using u3 and v3 to correct non power of two texture size.
glBegin(GL_QUADS);
glTexCoord2d( 0, 0); glVertex2f( 0, 0);
@@ -150,12 +141,12 @@ int main(int argc, char *argv[])
glTexCoord2d(u3, v3); glVertex2f(width, height);
glTexCoord2d( 0, v3); glVertex2f( 0, height);
glEnd();
-
+
// Redraw and clear screen.
SDL_GL_SwapBuffers();
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
+
//Limit frames per second, to not heat up the CPU and GPU too much.
SDL_Delay(16);
}
diff --git a/examples/example_optimize_png.cpp b/examples/example_optimize_png.cpp
index d64c7f2..4fa2ac1 100644
--- a/examples/example_optimize_png.cpp
+++ b/examples/example_optimize_png.cpp
@@ -37,35 +37,32 @@ NOTE: This is not as good as a true PNG optimizer like optipng or pngcrush.
#include <iostream>
-int main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
std::vector<unsigned char> image;
unsigned w, h;
std::vector<unsigned char> buffer;
unsigned error;
-
+
//check if user gave a filename
- if(argc < 3)
- {
+ if(argc < 3) {
std::cout << "please provide in and out filename" << std::endl;
return 0;
}
-
+
lodepng::load_file(buffer, argv[1]);
error = lodepng::decode(image, w, h, buffer);
- if(error)
- {
+ if(error) {
std::cout << "decoding error " << error << ": " << lodepng_error_text(error) << std::endl;
return 0;
}
-
+
size_t origsize = buffer.size();
std::cout << "Original size: " << origsize << " (" << (origsize / 1024) << "K)" << std::endl;
buffer.clear();
-
+
//Now encode as hard as possible with several filter types and window sizes
-
+
lodepng::State state;
state.encoder.filter_palette_zero = 0; //We try several filter types, including zero, allow trying them all on palette images too.
state.encoder.add_id = false; //Don't add LodePNG version chunk to save more bytes
@@ -84,7 +81,7 @@ int main(int argc, char *argv[])
// min match 3 allows all deflate lengths. min match 6 is similar to "Z_FILTERED" of zlib.
int minmatches[2] = { 3, 6 };
int bestminmatch = 0;
-
+
int autoconverts[2] = { 0, 1 };
std::string autoconvertnames[2] = { "0", "1" };
int bestautoconvert = 0;
@@ -92,11 +89,10 @@ int main(int argc, char *argv[])
int bestblocktype = 0;
// Try out all combinations of everything
- for(int i = 0; i < 4; i++) //filter strategy
- for(int j = 0; j < 2; j++) //min match
- for(int k = 0; k < 2; k++) //block type (for small images only)
- for(int l = 0; l < 2; l++) //color convert strategy
- {
+ for(int i = 0; i < 4; i++) //filter strategy
+ for(int j = 0; j < 2; j++) //min match
+ for(int k = 0; k < 2; k++) //block type (for small images only)
+ for(int l = 0; l < 2; l++) { //color convert strategy
if(bestsize > 3000 && (k > 0 || l > 0)) continue; /* these only make sense on small images */
std::vector<unsigned char> temp;
state.encoder.filter_strategy = strategies[i];
@@ -122,12 +118,12 @@ int main(int argc, char *argv[])
inited = true;
}
}
-
+
std::cout << "Chosen filter strategy: " << strategynames[beststrategy] << std::endl;
std::cout << "Chosen min match: " << bestminmatch << std::endl;
std::cout << "Chosen block type: " << bestblocktype << std::endl;
std::cout << "Chosen auto convert: " << autoconvertnames[bestautoconvert] << std::endl;
-
+
lodepng::save_file(buffer, argv[2]);
std::cout << "New size: " << buffer.size() << " (" << (buffer.size() / 1024) << "K)" << std::endl;
}
diff --git a/examples/example_png2bmp.cpp b/examples/example_png2bmp.cpp
index 3fd8328..ae12298 100644
--- a/examples/example_png2bmp.cpp
+++ b/examples/example_png2bmp.cpp
@@ -41,12 +41,11 @@ g++ lodepng.cpp example_png2bmp.cpp -Wall -Wextra -pedantic -ansi -lSDL -O3
//Input image must be RGB buffer (3 bytes per pixel), but you can easily make it
//support RGBA input and output by changing the inputChannels and/or outputChannels
//in the function to 4.
-void encodeBMP(std::vector<unsigned char>& bmp, const unsigned char* image, int w, int h)
-{
+void encodeBMP(std::vector<unsigned char>& bmp, const unsigned char* image, int w, int h) {
//3 bytes per pixel used for both input and output.
int inputChannels = 3;
int outputChannels = 3;
-
+
//bytes 0-13
bmp.push_back('B'); bmp.push_back('M'); //0: bfType
bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); //2: bfSize; size not yet known for now, filled in later.
@@ -66,7 +65,7 @@ void encodeBMP(std::vector<unsigned char>& bmp, const unsigned char* image, int
bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); //42: biYPelsPerMeter
bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); //46: biClrUsed
bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); //50: biClrImportant
-
+
/*
Convert the input RGBRGBRGB pixel buffer to the BMP pixel buffer format. There are 3 differences with the input buffer:
-BMP stores the rows inversed, from bottom to top
@@ -76,14 +75,11 @@ void encodeBMP(std::vector<unsigned char>& bmp, const unsigned char* image, int
int imagerowbytes = outputChannels * w;
imagerowbytes = imagerowbytes % 4 == 0 ? imagerowbytes : imagerowbytes + (4 - imagerowbytes % 4); //must be multiple of 4
-
- for(int y = h - 1; y >= 0; y--) //the rows are stored inversed in bmp
- {
+
+ for(int y = h - 1; y >= 0; y--) { //the rows are stored inversed in bmp
int c = 0;
- for(int x = 0; x < imagerowbytes; x++)
- {
- if(x < w * outputChannels)
- {
+ for(int x = 0; x < imagerowbytes; x++) {
+ if(x < w * outputChannels) {
int inc = c;
//Convert RGB(A) into BGR(A)
if(c == 0) inc = 2;
@@ -103,30 +99,27 @@ void encodeBMP(std::vector<unsigned char>& bmp, const unsigned char* image, int
bmp[5] = bmp.size() / 16777216;
}
-int main(int argc, char *argv[])
-{
- if(argc < 3)
- {
+int main(int argc, char *argv[]) {
+ if(argc < 3) {
std::cout << "Please provice input PNG and output BMP file names" << std::endl;
return 0;
}
const char* infile = argv[1];
const char* outfile = argv[2];
-
-
+
+
std::vector<unsigned char> image; //the raw pixels
unsigned width, height;
unsigned error = lodepng::decode(image, width, height, infile, LCT_RGB, 8);
- if(error)
- {
+ if(error) {
std::cout << "error " << error << ": " << lodepng_error_text(error) << std::endl;
return 0;
}
std::vector<unsigned char> bmp;
encodeBMP(bmp, &image[0], width, height);
-
+
lodepng::save_file(bmp, outfile);
}
diff --git a/examples/example_png_info.cpp b/examples/example_png_info.cpp
index cd2eca6..43427cc 100644
--- a/examples/example_png_info.cpp
+++ b/examples/example_png_info.cpp
@@ -38,8 +38,7 @@ etc...
/*
Display general info about the PNG.
*/
-void displayPNGInfo(const LodePNGInfo& info)
-{
+void displayPNGInfo(const LodePNGInfo& info) {
const LodePNGColorMode& color = info.color;
std::cout << "Compression method: " << info.compression_method << std::endl;
@@ -53,20 +52,17 @@ void displayPNGInfo(const LodePNGInfo& info)
std::cout << "Can have alpha: " << lodepng_can_have_alpha(&color) << std::endl;
std::cout << "Palette size: " << color.palettesize << std::endl;
std::cout << "Has color key: " << color.key_defined << std::endl;
- if(color.key_defined)
- {
+ if(color.key_defined) {
std::cout << "Color key r: " << color.key_r << std::endl;
std::cout << "Color key g: " << color.key_g << std::endl;
std::cout << "Color key b: " << color.key_b << std::endl;
}
std::cout << "Texts: " << info.text_num << std::endl;
- for(size_t i = 0; i < info.text_num; i++)
- {
+ for(size_t i = 0; i < info.text_num; i++) {
std::cout << "Text: " << info.text_keys[i] << ": " << info.text_strings[i] << std::endl << std::endl;
}
std::cout << "International texts: " << info.itext_num << std::endl;
- for(size_t i = 0; i < info.itext_num; i++)
- {
+ for(size_t i = 0; i < info.itext_num; i++) {
std::cout << "Text: "
<< info.itext_keys[i] << ", "
<< info.itext_langtags[i] << ", "
@@ -74,8 +70,7 @@ void displayPNGInfo(const LodePNGInfo& info)
<< info.itext_strings[i] << std::endl << std::endl;
}
std::cout << "Time defined: " << info.time_defined << std::endl;
- if(info.time_defined)
- {
+ if(info.time_defined) {
const LodePNGTime& time = info.time;
std::cout << "year: " << time.year << std::endl;
std::cout << "month: " << time.month << std::endl;
@@ -85,8 +80,7 @@ void displayPNGInfo(const LodePNGInfo& info)
std::cout << "second: " << time.second << std::endl;
}
std::cout << "Physics defined: " << info.phys_defined << std::endl;
- if(info.phys_defined)
- {
+ if(info.phys_defined) {
std::cout << "physics X: " << info.phys_x << std::endl;
std::cout << "physics Y: " << info.phys_y << std::endl;
std::cout << "physics unit: " << info.phys_unit << std::endl;
@@ -97,8 +91,7 @@ void displayPNGInfo(const LodePNGInfo& info)
/*
Display the names and sizes of all chunks in the PNG file.
*/
-void displayChunkNames(const std::vector<unsigned char>& buffer)
-{
+void displayChunkNames(const std::vector<unsigned char>& buffer) {
// Listing chunks is based on the original file, not the decoded png info.
const unsigned char *chunk, *begin, *end, *next;
end = &buffer.back() + 1;
@@ -107,18 +100,15 @@ void displayChunkNames(const std::vector<unsigned char>& buffer)
std::cout << std::endl << "Chunks:" << std::endl;
std::cout << " type: length(s)";
std::string last_type;
- while(chunk + 8 < end && chunk >= begin)
- {
+ while(chunk + 8 < end && chunk >= begin) {
char type[5];
lodepng_chunk_type(type, chunk);
- if(std::string(type).size() != 4)
- {
+ if(std::string(type).size() != 4) {
std::cout << "this is probably not a PNG" << std::endl;
return;
}
- if(last_type != type)
- {
+ if(last_type != type) {
std::cout << std::endl;
std::cout << " " << type << ": ";
}
@@ -137,10 +127,8 @@ void displayChunkNames(const std::vector<unsigned char>& buffer)
/*
Show ASCII art preview of the image
*/
-void displayAsciiArt(const std::vector<unsigned char>& image, unsigned w, unsigned h)
-{
- if(w > 0 && h > 0)
- {
+void displayAsciiArt(const std::vector<unsigned char>& image, unsigned w, unsigned h) {
+ if(w > 0 && h > 0) {
std::cout << std::endl << "ASCII Art Preview: " << std::endl;
unsigned w2 = 48;
if(w < w2) w2 = w;
@@ -151,11 +139,9 @@ void displayAsciiArt(const std::vector<unsigned char>& image, unsigned w, unsign
std::cout << '+';
for(unsigned x = 0; x < w2; x++) std::cout << '-';
std::cout << '+' << std::endl;
- for(unsigned y = 0; y < h2; y++)
- {
+ for(unsigned y = 0; y < h2; y++) {
std::cout << "|";
- for(unsigned x = 0; x < w2; x++)
- {
+ for(unsigned x = 0; x < w2; x++) {
unsigned x2 = x * w / w2;
unsigned y2 = y * h / h2;
int r = image[y2 * w * 4 + x2 * 4 + 0];
@@ -167,8 +153,7 @@ void displayAsciiArt(const std::vector<unsigned char>& image, unsigned w, unsign
int max = (r > g && r > b) ? r : (g > b ? g : b);
int saturation = max - min;
int letter = 'i'; //i for grey, or r,y,g,c,b,m for colors
- if(saturation > 32)
- {
+ if(saturation > 32) {
int h = lightness >= (min + max) / 2;
if(h) letter = (min == r ? 'c' : (min == g ? 'm' : 'y'));
else letter = (max == r ? 'r' : (max == g ? 'g' : 'b'));
@@ -193,12 +178,10 @@ void displayAsciiArt(const std::vector<unsigned char>& image, unsigned w, unsign
/*
Show the filtertypes of each scanline in this PNG image.
*/
-void displayFilterTypes(const std::vector<unsigned char>& buffer, bool ignore_checksums)
-{
+void displayFilterTypes(const std::vector<unsigned char>& buffer, bool ignore_checksums) {
//Get color type and interlace type
lodepng::State state;
- if(ignore_checksums)
- {
+ if(ignore_checksums) {
state.decoder.ignore_crc = 1;
state.decoder.zlibsettings.ignore_adler32 = 1;
}
@@ -206,14 +189,12 @@ void displayFilterTypes(const std::vector<unsigned char>& buffer, bool ignore_ch
unsigned error;
error = lodepng_inspect(&w, &h, &state, &buffer[0], buffer.size());
- if(error)
- {
+ if(error) {
std::cout << "inspect error " << error << ": " << lodepng_error_text(error) << std::endl;
return;
}
- if(state.info_png.interlace_method == 1)
- {
+ if(state.info_png.interlace_method == 1) {
std::cout << "showing filtertypes for interlaced PNG not supported by this example" << std::endl;
return;
}
@@ -225,18 +206,15 @@ void displayFilterTypes(const std::vector<unsigned char>& buffer, bool ignore_ch
std::vector<unsigned char> zdata;
- while(chunk + 8 < end && chunk >= begin)
- {
+ while(chunk + 8 < end && chunk >= begin) {
char type[5];
lodepng_chunk_type(type, chunk);
- if(std::string(type).size() != 4)
- {
+ if(std::string(type).size() != 4) {
std::cout << "this is probably not a PNG" << std::endl;
return;
}
- if(std::string(type) == "IDAT")
- {
+ if(std::string(type) == "IDAT") {
const unsigned char* cdata = lodepng_chunk_data_const(chunk);
unsigned clength = lodepng_chunk_length(chunk);
if(chunk + clength + 12 > end || clength > buffer.size() || chunk + clength + 12 < begin) {
@@ -244,8 +222,7 @@ void displayFilterTypes(const std::vector<unsigned char>& buffer, bool ignore_ch
return;
}
- for(unsigned i = 0; i < clength; i++)
- {
+ for(unsigned i = 0; i < clength; i++) {
zdata.push_back(cdata[i]);
}
}
@@ -259,8 +236,7 @@ void displayFilterTypes(const std::vector<unsigned char>& buffer, bool ignore_ch
std::vector<unsigned char> data;
error = lodepng::decompress(data, &zdata[0], zdata.size());
- if(error)
- {
+ if(error) {
std::cout << "decompress error " << error << ": " << lodepng_error_text(error) << std::endl;
return;
}
@@ -268,15 +244,13 @@ void displayFilterTypes(const std::vector<unsigned char>& buffer, bool ignore_ch
//A line is 1 filter byte + all pixels
size_t linebytes = 1 + lodepng_get_raw_size(w, 1, &state.info_png.color);
- if(linebytes == 0)
- {
+ if(linebytes == 0) {
std::cout << "error: linebytes is 0" << std::endl;
return;
}
std::cout << "Filter types: ";
- for(size_t i = 0; i < data.size(); i += linebytes)
- {
+ for(size_t i = 0; i < data.size(); i += linebytes) {
std::cout << (int)(data[i]) << " ";
}
std::cout << std::endl;
@@ -287,17 +261,14 @@ void displayFilterTypes(const std::vector<unsigned char>& buffer, bool ignore_ch
/*
Main
*/
-int main(int argc, char *argv[]) /*list the chunks*/
-{
+int main(int argc, char *argv[]) /*list the chunks*/ {
bool ignore_checksums = false;
std::string filename = "";
- for (int i = 1; i < argc; i++)
- {
+ for (int i = 1; i < argc; i++) {
if(std::string(argv[i]) == "--ignore_checksums") ignore_checksums = true;
else filename = argv[i];
}
- if(filename == "")
- {
+ if(filename == "") {
std::cout << "Please provide a filename to preview" << std::endl;
return 0;
}
@@ -309,16 +280,14 @@ int main(int argc, char *argv[]) /*list the chunks*/
lodepng::load_file(buffer, filename); //load the image file with given filename
lodepng::State state;
- if(ignore_checksums)
- {
+ if(ignore_checksums) {
state.decoder.ignore_crc = 1;
state.decoder.zlibsettings.ignore_adler32 = 1;
}
unsigned error = lodepng::decode(image, w, h, state, buffer);
- if(error)
- {
+ if(error) {
std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
return 0;
}
@@ -328,8 +297,7 @@ int main(int argc, char *argv[]) /*list the chunks*/
std::cout << "Height: " << h << std::endl;
std::cout << "Num pixels: " << w * h << std::endl;
- if(w > 0 && h > 0)
- {
+ if(w > 0 && h > 0) {
std::cout << "Top left pixel color:"
<< " r: " << (int)image[0]
<< " g: " << (int)image[1]
diff --git a/examples/example_reencode.cpp b/examples/example_reencode.cpp
index d438612..d316bc9 100644
--- a/examples/example_reencode.cpp
+++ b/examples/example_reencode.cpp
@@ -35,42 +35,38 @@ This sample shows how LodePNG can be used for a conforming PNG editor.
#include <iostream>
-int main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
std::vector<unsigned char> image;
unsigned w, h;
std::vector<unsigned char> buffer;
lodepng::State state;
unsigned error;
-
+
//check if user gave a filename
- if(argc < 3)
- {
+ if(argc < 3) {
std::cout << "please provide in and out filename" << std::endl;
return 0;
}
-
+
state.decoder.color_convert = 0;
state.decoder.remember_unknown_chunks = 1; //make it reproduce even unknown chunks in the saved image
-
+
lodepng::load_file(buffer, argv[1]);
error = lodepng::decode(image, w, h, state, buffer);
- if(error)
- {
+ if(error) {
std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
return 0;
}
-
+
buffer.clear();
-
+
state.encoder.text_compression = 1;
-
+
error = lodepng::encode(buffer, image, w, h, state);
- if(error)
- {
+ if(error) {
std::cout << "encoder error " << error << ": " << lodepng_error_text(error) << std::endl;
return 0;
}
-
+
lodepng::save_file(buffer, argv[2]);
}
diff --git a/examples/example_sdl.c b/examples/example_sdl.c
index e4ae3de..874bc4c 100644
--- a/examples/example_sdl.c
+++ b/examples/example_sdl.c
@@ -42,8 +42,7 @@ Press any key to see next image, or esc to quit.
#include <SDL/SDL.h>
/*shows image with SDL. Returns 1 if user wants to fully quit, 0 if user wants to see next image.*/
-int show(const char* filename)
-{
+int show(const char* filename) {
unsigned error;
unsigned char* image;
unsigned w, h, x, y;
@@ -58,8 +57,7 @@ int show(const char* filename)
error = lodepng_decode32_file(&image, &w, &h, filename);
/*stop if there is an error*/
- if(error)
- {
+ if(error) {
printf("decoder error %u: %s\n", error, lodepng_error_text(error));
return 0;
}
@@ -70,14 +68,12 @@ int show(const char* filename)
if(h / 1024 >= jump) jump = h / 1024 + 1;
/*init SDL*/
- if(SDL_Init(SDL_INIT_VIDEO) < 0)
- {
+ if(SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("Error, SDL video init failed\n");
return 0;
}
scr = SDL_SetVideoMode(w / jump, h / jump, 32, SDL_HWSURFACE);
- if(!scr)
- {
+ if(!scr) {
printf("Error, no SDL screen\n");
return 0;
}
@@ -85,8 +81,7 @@ int show(const char* filename)
/*plot the pixels of the PNG file*/
for(y = 0; y + jump - 1 < h; y += jump)
- for(x = 0; x + jump - 1 < w; x += jump)
- {
+ for(x = 0; x + jump - 1 < w; x += jump) {
int checkerColor;
Uint32* bufp;
Uint32 r, g, b, a;
@@ -110,10 +105,8 @@ int show(const char* filename)
/*pause until you press escape and meanwhile redraw screen*/
done = 0;
- while(done == 0)
- {
- while(SDL_PollEvent(&event))
- {
+ while(done == 0) {
+ while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) done = 2;
else if(SDL_GetKeyState(NULL)[SDLK_ESCAPE]) done = 2;
else if(event.type == SDL_KEYDOWN) done = 1; /*press any other key for next image*/
@@ -128,14 +121,12 @@ int show(const char* filename)
return done == 2 ? 1 : 0;
}
-int main(int argc, char* argv[])
-{
+int main(int argc, char* argv[]) {
int i;
if(argc <= 1) printf("Please enter PNG file name(s) to display\n");;
- for(i = 1; i < argc; i++)
- {
+ for(i = 1; i < argc; i++) {
if(show(argv[i])) return 0;
}
return 0;
diff --git a/examples/example_sdl.cpp b/examples/example_sdl.cpp
index 0c68ef5..dc7dee2 100644
--- a/examples/example_sdl.cpp
+++ b/examples/example_sdl.cpp
@@ -39,22 +39,19 @@ Press any key to see next image, or esc to quit.
#include <iostream>
#include <SDL/SDL.h>
-int show(const std::string& caption, const unsigned char* rgba, unsigned w, unsigned h)
-{
+int show(const std::string& caption, const unsigned char* rgba, unsigned w, unsigned h) {
//avoid too large window size by downscaling large image
unsigned jump = 1;
if(w / 1024 >= jump) jump = w / 1024 + 1;
if(h / 1024 >= jump) jump = h / 1024 + 1;
//init SDL
- if(SDL_Init(SDL_INIT_VIDEO) < 0)
- {
+ if(SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cout << "error, SDL video init failed" << std::endl;
return 0;
}
SDL_Surface* scr = SDL_SetVideoMode(w / jump, h / jump, 32, SDL_HWSURFACE);
- if(!scr)
- {
+ if(!scr) {
std::cout << "error, no SDL screen" << std::endl;
return 0;
}
@@ -62,8 +59,7 @@ int show(const std::string& caption, const unsigned char* rgba, unsigned w, unsi
//plot the pixels of the PNG file
for(unsigned y = 0; y + jump - 1 < h; y += jump)
- for(unsigned x = 0; x + jump - 1 < w; x += jump)
- {
+ for(unsigned x = 0; x + jump - 1 < w; x += jump) {
//get RGBA components
Uint32 r = rgba[4 * y * w + 4 * x + 0]; //red
Uint32 g = rgba[4 * y * w + 4 * x + 1]; //green
@@ -85,10 +81,8 @@ int show(const std::string& caption, const unsigned char* rgba, unsigned w, unsi
//pause until you press escape and meanwhile redraw screen
SDL_Event event;
int done = 0;
- while(done == 0)
- {
- while(SDL_PollEvent(&event))
- {
+ while(done == 0) {
+ while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) done = 2;
else if(SDL_GetKeyState(NULL)[SDLK_ESCAPE]) done = 2;
else if(event.type == SDL_KEYDOWN) done = 1; //press any other key for next image
@@ -102,8 +96,7 @@ int show(const std::string& caption, const unsigned char* rgba, unsigned w, unsi
}
/*shows image with SDL. Returns 1 if user wants to fully quit, 0 if user wants to see next image.*/
-int showfile(const char* filename)
-{
+int showfile(const char* filename) {
std::cout << "showing " << filename << std::endl;
std::vector<unsigned char> buffer, image;
@@ -112,8 +105,7 @@ int showfile(const char* filename)
unsigned error = lodepng::decode(image, w, h, buffer); //decode the png
//stop if there is an error
- if(error)
- {
+ if(error) {
std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
return 0;
}
@@ -121,12 +113,10 @@ int showfile(const char* filename)
return show(filename, &image[0], w, h);
}
-int main(int argc, char* argv[])
-{
+int main(int argc, char* argv[]) {
if(argc <= 1) std::cout << "Please enter PNG file name(s) to display" << std::endl;
- for(int i = 1; i < argc; i++)
- {
+ for(int i = 1; i < argc; i++) {
if(showfile(argv[i])) return 0;
}
}