Skip to content

Commit

Permalink
change brace indent style: don't break before opening brace
Browse files Browse the repository at this point in the history
  • Loading branch information
lvandeve committed Dec 30, 2018
1 parent 2e6b2ba commit 472d085
Show file tree
Hide file tree
Showing 22 changed files with 1,858 additions and 3,369 deletions.
15 changes: 5 additions & 10 deletions examples/example_4bit_palette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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));
Expand All @@ -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;

Expand All @@ -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;
}
Expand Down
33 changes: 12 additions & 21 deletions examples/example_bmp2png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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]);

}
222 changes: 109 additions & 113 deletions examples/example_decode.c
Original file line number Diff line number Diff line change
@@ -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;
}

Loading

0 comments on commit 472d085

Please sign in to comment.