My Project
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Macros | Typedefs | Functions | Variables
lodepng.h File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Classes

struct  LodeZlib_DeflateSettings
 
struct  LodePNG_InfoColor
 
struct  LodePNG_InfoPng
 
struct  LodePNG_InfoRaw
 
struct  LodePNG_EncodeSettings
 
struct  LodePNG_Encoder
 

Macros

#define LODEPNG_COMPILE_ZLIB   /*deflate&zlib encoder and deflate&zlib decoder*/
 
#define LODEPNG_COMPILE_PNG   /*png encoder and png decoder*/
 
#define LODEPNG_COMPILE_ENCODER   /*deflate&zlib encoder and png encoder*/
 
#define LODEPNG_COMPILE_DISK   /*the optional built in harddisk file loading and saving functions*/
 

Typedefs

typedef struct
LodeZlib_DeflateSettings 
LodeZlib_DeflateSettings
 
typedef struct LodePNG_InfoColor LodePNG_InfoColor
 
typedef struct LodePNG_InfoPng LodePNG_InfoPng
 
typedef struct LodePNG_InfoRaw LodePNG_InfoRaw
 
typedef struct
LodePNG_EncodeSettings 
LodePNG_EncodeSettings
 
typedef struct LodePNG_Encoder LodePNG_Encoder
 

Functions

void LodeZlib_DeflateSettings_init (LodeZlib_DeflateSettings *settings)
 
unsigned LodeZlib_compress (unsigned char **out, size_t *outsize, const unsigned char *in, size_t insize, const LodeZlib_DeflateSettings *settings)
 
unsigned LodePNG_chunk_length (const unsigned char *chunk)
 
void LodePNG_chunk_type (char type[5], const unsigned char *chunk)
 
unsigned char LodePNG_chunk_type_equals (const unsigned char *chunk, const char *type)
 
unsigned char LodePNG_chunk_critical (const unsigned char *chunk)
 
unsigned char LodePNG_chunk_private (const unsigned char *chunk)
 
unsigned char LodePNG_chunk_safetocopy (const unsigned char *chunk)
 
unsigned char * LodePNG_chunk_data (unsigned char *chunk)
 
const unsigned char * LodePNG_chunk_data_const (const unsigned char *chunk)
 
unsigned LodePNG_chunk_check_crc (const unsigned char *chunk)
 
void LodePNG_chunk_generate_crc (unsigned char *chunk)
 
unsigned char * LodePNG_chunk_next (unsigned char *chunk)
 
const unsigned char * LodePNG_chunk_next_const (const unsigned char *chunk)
 
unsigned LodePNG_append_chunk (unsigned char **out, size_t *outlength, const unsigned char *chunk)
 
unsigned LodePNG_create_chunk (unsigned char **out, size_t *outlength, unsigned length, const char *type, const unsigned char *data)
 
void LodePNG_InfoColor_init (LodePNG_InfoColor *info)
 
void LodePNG_InfoColor_cleanup (LodePNG_InfoColor *info)
 
unsigned LodePNG_InfoColor_copy (LodePNG_InfoColor *dest, const LodePNG_InfoColor *source)
 
void LodePNG_InfoColor_clearPalette (LodePNG_InfoColor *info)
 
unsigned LodePNG_InfoColor_addPalette (LodePNG_InfoColor *info, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
 
unsigned LodePNG_InfoColor_getBpp (const LodePNG_InfoColor *info)
 
unsigned LodePNG_InfoColor_getChannels (const LodePNG_InfoColor *info)
 
unsigned LodePNG_InfoColor_isGreyscaleType (const LodePNG_InfoColor *info)
 
unsigned LodePNG_InfoColor_isAlphaType (const LodePNG_InfoColor *info)
 
void LodePNG_InfoPng_init (LodePNG_InfoPng *info)
 
void LodePNG_InfoPng_cleanup (LodePNG_InfoPng *info)
 
unsigned LodePNG_InfoPng_copy (LodePNG_InfoPng *dest, const LodePNG_InfoPng *source)
 
void LodePNG_InfoRaw_init (LodePNG_InfoRaw *info)
 
void LodePNG_InfoRaw_cleanup (LodePNG_InfoRaw *info)
 
unsigned LodePNG_InfoRaw_copy (LodePNG_InfoRaw *dest, const LodePNG_InfoRaw *source)
 
unsigned LodePNG_convert (unsigned char *out, const unsigned char *in, LodePNG_InfoColor *infoOut, LodePNG_InfoColor *infoIn, unsigned w, unsigned h)
 
void LodePNG_EncodeSettings_init (LodePNG_EncodeSettings *settings)
 
void LodePNG_Encoder_init (LodePNG_Encoder *encoder)
 
void LodePNG_Encoder_cleanup (LodePNG_Encoder *encoder)
 
void LodePNG_Encoder_copy (LodePNG_Encoder *dest, const LodePNG_Encoder *source)
 
void LodePNG_encode (LodePNG_Encoder *encoder, unsigned char **out, size_t *outsize, const unsigned char *image, unsigned w, unsigned h)
 
unsigned LodePNG_encode32 (unsigned char **out, size_t *outsize, const unsigned char *image, unsigned w, unsigned h)
 
unsigned LodePNG_encode32f (const char *filename, const unsigned char *image, unsigned w, unsigned h)
 
unsigned LodePNG_loadFile (unsigned char **out, size_t *outsize, const char *filename)
 
unsigned LodePNG_saveFile (const unsigned char *buffer, size_t buffersize, const char *filename)
 

Variables

const LodeZlib_DeflateSettings LodeZlib_defaultDeflateSettings
 

Macro Definition Documentation

#define LODEPNG_COMPILE_DISK   /*the optional built in harddisk file loading and saving functions*/

Definition at line 43 of file lodepng.h.

#define LODEPNG_COMPILE_ENCODER   /*deflate&zlib encoder and png encoder*/

Definition at line 42 of file lodepng.h.

#define LODEPNG_COMPILE_PNG   /*png encoder and png decoder*/

Definition at line 40 of file lodepng.h.

#define LODEPNG_COMPILE_ZLIB   /*deflate&zlib encoder and deflate&zlib decoder*/

Definition at line 39 of file lodepng.h.

Typedef Documentation

Function Documentation

unsigned LodePNG_append_chunk ( unsigned char **  out,
size_t *  outlength,
const unsigned char *  chunk 
)

Definition at line 1937 of file lodepng.cpp.

References LodePNG_chunk_length().

{
unsigned i;
unsigned total_chunk_length = LodePNG_chunk_length(chunk) + 12;
unsigned char *chunk_start, *new_buffer;
size_t new_length = (*outlength) + total_chunk_length;
if(new_length < total_chunk_length || new_length < (*outlength)) return 77; /*integer overflow happened*/
new_buffer = (unsigned char*)realloc(*out, new_length);
if(!new_buffer) return 9929;
(*out) = new_buffer;
(*outlength) = new_length;
chunk_start = &(*out)[new_length - total_chunk_length];
for(i = 0; i < total_chunk_length; i++) chunk_start[i] = chunk[i];
return 0;
}
unsigned LodePNG_chunk_check_crc ( const unsigned char *  chunk)

Definition at line 1909 of file lodepng.cpp.

References Crc32_crc(), LodePNG_chunk_length(), and LodePNG_read32bitInt().

{
unsigned length = LodePNG_chunk_length(chunk);
unsigned CRC = LodePNG_read32bitInt(&chunk[length + 8]);
unsigned checksum = Crc32_crc(&chunk[4], length + 4); /*the CRC is taken of the data and the 4 chunk type letters, not the length*/
if(CRC != checksum) return 1;
else return 0;
}
unsigned char LodePNG_chunk_critical ( const unsigned char *  chunk)

Definition at line 1884 of file lodepng.cpp.

{
return((chunk[4] & 32) == 0);
}
unsigned char* LodePNG_chunk_data ( unsigned char *  chunk)

Definition at line 1899 of file lodepng.cpp.

{
return &chunk[8];
}
const unsigned char* LodePNG_chunk_data_const ( const unsigned char *  chunk)

Definition at line 1904 of file lodepng.cpp.

{
return &chunk[8];
}
void LodePNG_chunk_generate_crc ( unsigned char *  chunk)

Definition at line 1918 of file lodepng.cpp.

References Crc32_crc(), LodePNG_chunk_length(), and LodePNG_set32bitInt().

Referenced by LodePNG_create_chunk().

{
unsigned length = LodePNG_chunk_length(chunk);
unsigned CRC = Crc32_crc(&chunk[4], length + 4);
LodePNG_set32bitInt(chunk + 8 + length, CRC);
}
unsigned LodePNG_chunk_length ( const unsigned char *  chunk)
unsigned char* LodePNG_chunk_next ( unsigned char *  chunk)

Definition at line 1925 of file lodepng.cpp.

References LodePNG_chunk_length().

{
unsigned total_chunk_length = LodePNG_chunk_length(chunk) + 12;
return &chunk[total_chunk_length];
}
const unsigned char* LodePNG_chunk_next_const ( const unsigned char *  chunk)

Definition at line 1931 of file lodepng.cpp.

References LodePNG_chunk_length().

{
unsigned total_chunk_length = LodePNG_chunk_length(chunk) + 12;
return &chunk[total_chunk_length];
}
unsigned char LodePNG_chunk_private ( const unsigned char *  chunk)

Definition at line 1889 of file lodepng.cpp.

{
return((chunk[6] & 32) != 0);
}
unsigned char LodePNG_chunk_safetocopy ( const unsigned char *  chunk)

Definition at line 1894 of file lodepng.cpp.

{
return((chunk[7] & 32) != 0);
}
void LodePNG_chunk_type ( char  type[5],
const unsigned char *  chunk 
)

Definition at line 1870 of file lodepng.cpp.

{
unsigned i;
for(i = 0; i < 4; i++) type[i] = chunk[4 + i];
type[4] = 0; /*null termination char*/
}
unsigned char LodePNG_chunk_type_equals ( const unsigned char *  chunk,
const char *  type 
)

Definition at line 1877 of file lodepng.cpp.

{
if(strlen(type) != 4) return 0;
return (chunk[4] == type[0] && chunk[5] == type[1] && chunk[6] == type[2] && chunk[7] == type[3]);
}
unsigned LodePNG_convert ( unsigned char *  out,
const unsigned char *  in,
LodePNG_InfoColor infoOut,
LodePNG_InfoColor infoIn,
unsigned  w,
unsigned  h 
)

Definition at line 2361 of file lodepng.cpp.

References LodePNG_InfoColor::bitDepth, LodePNG_InfoColor::colorType, LodePNG_InfoColor::key_b, LodePNG_InfoColor::key_defined, LodePNG_InfoColor::key_g, LodePNG_InfoColor::key_r, LodePNG_InfoColor_equal(), LodePNG_InfoColor_getBpp(), LodePNG_InfoColor_isAlphaType(), LodePNG_InfoColor_isGreyscaleType(), LodePNG_InfoColor::palette, LodePNG_InfoColor::palettesize, and readBitsFromReversedStream().

Referenced by LodePNG_encode().

{
const size_t numpixels = w * h; /*amount of pixels*/
const unsigned OUT_BYTES = LodePNG_InfoColor_getBpp(infoOut) / 8; /*bytes per pixel in the output image*/
const unsigned OUT_ALPHA = LodePNG_InfoColor_isAlphaType(infoOut); /*use 8-bit alpha channel*/
size_t i, c, bp = 0; /*bitpointer, used by less-than-8-bit color types*/
/*cases where in and out already have the same format*/
if(LodePNG_InfoColor_equal(infoIn, infoOut))
{
size_t i, size = (w * h * LodePNG_InfoColor_getBpp(infoIn) + 7) / 8;
for(i = 0; i < size; i++) out[i] = in[i];
return 0;
}
if((infoOut->colorType == 2 || infoOut->colorType == 6) && infoOut->bitDepth == 8)
{
if(infoIn->bitDepth == 8)
{
switch(infoIn->colorType)
{
case 0: /*greyscale color*/
for(i = 0; i < numpixels; i++)
{
if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
out[OUT_BYTES * i + 0] = out[OUT_BYTES * i + 1] = out[OUT_BYTES * i + 2] = in[i];
if(OUT_ALPHA && infoIn->key_defined && in[i] == infoIn->key_r) out[OUT_BYTES * i + 3] = 0;
}
break;
case 2: /*RGB color*/
for(i = 0; i < numpixels; i++)
{
if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
for(c = 0; c < 3; c++) out[OUT_BYTES * i + c] = in[3 * i + c];
if(OUT_ALPHA && infoIn->key_defined == 1 && in[3 * i + 0] == infoIn->key_r && in[3 * i + 1] == infoIn->key_g && in[3 * i + 2] == infoIn->key_b) out[OUT_BYTES * i + 3] = 0;
}
break;
case 3: /*indexed color (palette)*/
for(i = 0; i < numpixels; i++)
{
if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
if(in[i] >= infoIn->palettesize) return 46;
for(c = 0; c < OUT_BYTES; c++) out[OUT_BYTES * i + c] = infoIn->palette[4 * in[i] + c]; /*get rgb colors from the palette*/
}
break;
case 4: /*greyscale with alpha*/
for(i = 0; i < numpixels; i++)
{
out[OUT_BYTES * i + 0] = out[OUT_BYTES * i + 1] = out[OUT_BYTES * i + 2] = in[2 * i + 0];
if(OUT_ALPHA) out[OUT_BYTES * i + 3] = in[2 * i + 1];
}
break;
case 6: /*RGB with alpha*/
for(i = 0; i < numpixels; i++)
{
for(c = 0; c < OUT_BYTES; c++) out[OUT_BYTES * i + c] = in[4 * i + c];
}
break;
default: break;
}
}
else if(infoIn->bitDepth == 16)
{
switch(infoIn->colorType)
{
case 0: /*greyscale color*/
for(i = 0; i < numpixels; i++)
{
if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
out[OUT_BYTES * i + 0] = out[OUT_BYTES * i + 1] = out[OUT_BYTES * i + 2] = in[2 * i];
if(OUT_ALPHA && infoIn->key_defined && 256U * in[i] + in[i + 1] == infoIn->key_r) out[OUT_BYTES * i + 3] = 0;
}
break;
case 2: /*RGB color*/
for(i = 0; i < numpixels; i++)
{
if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
for(c = 0; c < 3; c++) out[OUT_BYTES * i + c] = in[6 * i + 2 * c];
if(OUT_ALPHA && infoIn->key_defined && 256U * in[6 * i + 0] + in[6 * i + 1] == infoIn->key_r && 256U * in[6 * i + 2] + in[6 * i + 3] == infoIn->key_g && 256U * in[6 * i + 4] + in[6 * i + 5] == infoIn->key_b) out[OUT_BYTES * i + 3] = 0;
}
break;
case 4: /*greyscale with alpha*/
for(i = 0; i < numpixels; i++)
{
out[OUT_BYTES * i + 0] = out[OUT_BYTES * i + 1] = out[OUT_BYTES * i + 2] = in[4 * i]; /*most significant byte*/
if(OUT_ALPHA) out[OUT_BYTES * i + 3] = in[4 * i + 2];
}
break;
case 6: /*RGB with alpha*/
for(i = 0; i < numpixels; i++)
{
for(c = 0; c < OUT_BYTES; c++) out[OUT_BYTES * i + c] = in[8 * i + 2 * c];
}
break;
default: break;
}
}
else /*infoIn->bitDepth is less than 8 bit per channel*/
{
switch(infoIn->colorType)
{
case 0: /*greyscale color*/
for(i = 0; i < numpixels; i++)
{
unsigned value = readBitsFromReversedStream(&bp, in, infoIn->bitDepth);
if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
if(OUT_ALPHA && infoIn->key_defined && value && ((1U << infoIn->bitDepth) - 1U) == infoIn->key_r && ((1U << infoIn->bitDepth) - 1U)) out[OUT_BYTES * i + 3] = 0;
value = (value * 255) / ((1 << infoIn->bitDepth) - 1); /*scale value from 0 to 255*/
out[OUT_BYTES * i + 0] = out[OUT_BYTES * i + 1] = out[OUT_BYTES * i + 2] = (unsigned char)(value);
}
break;
case 3: /*indexed color (palette)*/
for(i = 0; i < numpixels; i++)
{
unsigned value = readBitsFromReversedStream(&bp, in, infoIn->bitDepth);
if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
if(value >= infoIn->palettesize) return 47;
for(c = 0; c < OUT_BYTES; c++) out[OUT_BYTES * i + c] = infoIn->palette[4 * value + c]; /*get rgb colors from the palette*/
}
break;
default: break;
}
}
}
else if(LodePNG_InfoColor_isGreyscaleType(infoOut) && infoOut->bitDepth == 8) /*conversion from greyscale to greyscale*/
{
if(!LodePNG_InfoColor_isGreyscaleType(infoIn)) return 62;
if(infoIn->bitDepth == 8)
{
switch(infoIn->colorType)
{
case 0: /*greyscale color*/
for(i = 0; i < numpixels; i++)
{
if(OUT_ALPHA) out[OUT_BYTES * i + 1] = 255;
out[OUT_BYTES * i] = in[i];
if(OUT_ALPHA && infoIn->key_defined && in[i] == infoIn->key_r) out[OUT_BYTES * i + 1] = 0;
}
break;
case 4: /*greyscale with alpha*/
for(i = 0; i < numpixels; i++)
{
out[OUT_BYTES * i + 0] = in[2 * i + 0];
if(OUT_ALPHA) out[OUT_BYTES * i + 1] = in[2 * i + 1];
}
break;
default: return 31;
}
}
else if(infoIn->bitDepth == 16)
{
switch(infoIn->colorType)
{
case 0: /*greyscale color*/
for(i = 0; i < numpixels; i++)
{
if(OUT_ALPHA) out[OUT_BYTES * i + 1] = 255;
out[OUT_BYTES * i] = in[2 * i];
if(OUT_ALPHA && infoIn->key_defined && 256U * in[i] + in[i + 1] == infoIn->key_r) out[OUT_BYTES * i + 1] = 0;
}
break;
case 4: /*greyscale with alpha*/
for(i = 0; i < numpixels; i++)
{
out[OUT_BYTES * i] = in[4 * i]; /*most significant byte*/
if(OUT_ALPHA) out[OUT_BYTES * i + 1] = in[4 * i + 2]; /*most significant byte*/
}
break;
default: return 31;
}
}
else /*infoIn->bitDepth is less than 8 bit per channel*/
{
if(infoIn->colorType != 0) return 31; /*colorType 0 is the only greyscale type with < 8 bits per channel*/
for(i = 0; i < numpixels; i++)
{
unsigned value = readBitsFromReversedStream(&bp, in, infoIn->bitDepth);
if(OUT_ALPHA) out[OUT_BYTES * i + 1] = 255;
if(OUT_ALPHA && infoIn->key_defined && value && ((1U << infoIn->bitDepth) - 1U) == infoIn->key_r && ((1U << infoIn->bitDepth) - 1U)) out[OUT_BYTES * i + 1] = 0;
value = (value * 255) / ((1 << infoIn->bitDepth) - 1); /*scale value from 0 to 255*/
out[OUT_BYTES * i] = (unsigned char)(value);
}
}
}
else return 59;
return 0;
}
unsigned LodePNG_create_chunk ( unsigned char **  out,
size_t *  outlength,
unsigned  length,
const char *  type,
const unsigned char *  data 
)

Definition at line 1956 of file lodepng.cpp.

References LodePNG_chunk_generate_crc(), and LodePNG_set32bitInt().

Referenced by addChunk().

{
unsigned i;
unsigned char *chunk, *new_buffer;
size_t new_length = (*outlength) + length + 12;
if(new_length < length + 12 || new_length < (*outlength)) return 77; /*integer overflow happened*/
new_buffer = (unsigned char*)realloc(*out, new_length);
if(!new_buffer) return 9930;
(*out) = new_buffer;
(*outlength) = new_length;
chunk = &(*out)[(*outlength) - length - 12];
/*1: length*/
LodePNG_set32bitInt(chunk, (unsigned)length);
/*2: chunk name (4 letters)*/
chunk[4] = type[0];
chunk[5] = type[1];
chunk[6] = type[2];
chunk[7] = type[3];
/*3: the data*/
for(i = 0; i < length; i++) chunk[8 + i] = data[i];
/*4: CRC (of the chunkname characters and the data)*/
return 0;
}
void LodePNG_encode ( LodePNG_Encoder encoder,
unsigned char **  out,
size_t *  outsize,
const unsigned char *  image,
unsigned  w,
unsigned  h 
)

Definition at line 3910 of file lodepng.cpp.

References addChunk_IDAT(), addChunk_IEND(), addChunk_IHDR(), addChunk_PLTE(), addChunk_tRNS(), LodePNG_EncodeSettings::autoLeaveOutAlphaChannel, LodePNG_InfoColor::bitDepth, LodeZlib_DeflateSettings::btype, checkColorValidity(), LodePNG_InfoPng::color, LodePNG_InfoRaw::color, LodePNG_InfoColor::colorType, ucvector::data, LodePNG_Encoder::error, LodePNG_EncodeSettings::force_palette, LodePNG_InfoPng::height, LodePNG_Encoder::infoPng, LodePNG_Encoder::infoRaw, LodePNG_InfoPng::interlaceMethod, isFullyOpaque(), isPaletteFullyOpaque(), LodePNG_InfoColor::key_defined, LodePNG_convert(), LodePNG_InfoColor_equal(), LodePNG_InfoColor_getBpp(), LodePNG_InfoColor::palette, LodePNG_InfoColor::palettesize, preProcessScanlines(), LodePNG_Encoder::settings, ucvector::size, ucvector_init(), VERSION_STRING, LodePNG_InfoPng::width, LodeZlib_DeflateSettings::windowSize, writeSignature(), and LodePNG_EncodeSettings::zlibsettings.

Referenced by LodePNG_encode32(), Image::save(), and ColoredImage::save().

{
ucvector outv;
unsigned char* data = 0; /*uncompressed version of the IDAT chunk data*/
size_t datasize = 0;
/*provide some proper output values if error will happen*/
*out = 0;
*outsize = 0;
encoder->error = 0;
info = encoder->infoPng; /*UNSAFE copy to avoid having to cleanup! but we will only change primitive parameters, and not invoke the cleanup function nor touch the palette's buffer so we use it safely*/
info.width = w;
info.height = h;
if(encoder->settings.autoLeaveOutAlphaChannel && isFullyOpaque(image, w, h, &encoder->infoRaw.color))
{
/*go to a color type without alpha channel*/
if(info.color.colorType == 6) info.color.colorType = 2;
else if(info.color.colorType == 4) info.color.colorType = 0;
}
if(encoder->settings.zlibsettings.windowSize > 32768) { encoder->error = 60; return; } /*error: windowsize larger than allowed*/
if(encoder->settings.zlibsettings.btype > 2) { encoder->error = 61; return; } /*error: unexisting btype*/
if(encoder->infoPng.interlaceMethod > 1) { encoder->error = 71; return; } /*error: unexisting interlace mode*/
if((encoder->error = checkColorValidity(info.color.colorType, info.color.bitDepth))) return; /*error: unexisting color type given*/
if((encoder->error = checkColorValidity(encoder->infoRaw.color.colorType, encoder->infoRaw.color.bitDepth))) return; /*error: unexisting color type given*/
if(!LodePNG_InfoColor_equal(&encoder->infoRaw.color, &info.color))
{
unsigned char* converted;
size_t size = (w * h * LodePNG_InfoColor_getBpp(&info.color) + 7) / 8;
if((info.color.colorType != 6 && info.color.colorType != 2) || (info.color.bitDepth != 8)) { encoder->error = 59; return; } /*for the output image, only these types are supported*/
converted = (unsigned char*)malloc(size);
if(!converted && size) encoder->error = 9955; /*error: malloc failed*/
if(!encoder->error) encoder->error = LodePNG_convert(converted, image, &info.color, &encoder->infoRaw.color, w, h);
if(!encoder->error) preProcessScanlines(&data, &datasize, converted, &info);/*filter(data.data, converted.data, w, h, LodePNG_InfoColor_getBpp(&info.color));*/
free(converted);
}
else preProcessScanlines(&data, &datasize, image, &info);/*filter(data.data, image, w, h, LodePNG_InfoColor_getBpp(&info.color));*/
ucvector_init(&outv);
while(!encoder->error) /*not really a while loop, this is only used to break out if an error happens to avoid goto's to do the ucvector cleanup*/
{
#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
size_t i;
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
/*write signature and chunks*/
/*IHDR*/
addChunk_IHDR(&outv, w, h, info.color.bitDepth, info.color.colorType, info.interlaceMethod);
#ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
/*unknown chunks between IHDR and PLTE*/
if(info.unknown_chunks.data[0]) { encoder->error = addUnknownChunks(&outv, info.unknown_chunks.data[0], info.unknown_chunks.datasize[0]); if(encoder->error) break; }
#endif /*LODEPNG_COMPILE_UNKNOWN_CHUNKS*/
/*PLTE*/
if(info.color.colorType == 3)
{
if(info.color.palettesize == 0 || info.color.palettesize > 256) { encoder->error = 68; break; }
addChunk_PLTE(&outv, &info.color);
}
if(encoder->settings.force_palette && (info.color.colorType == 2 || info.color.colorType == 6))
{
if(info.color.palettesize == 0 || info.color.palettesize > 256) { encoder->error = 68; break; }
addChunk_PLTE(&outv, &info.color);
}
/*tRNS*/
if(info.color.colorType == 3 && !isPaletteFullyOpaque(info.color.palette, info.color.palettesize)) addChunk_tRNS(&outv, &info.color);
if((info.color.colorType == 0 || info.color.colorType == 2) && info.color.key_defined) addChunk_tRNS(&outv, &info.color);
#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
/*bKGD (must come between PLTE and the IDAt chunks*/
if(info.background_defined) addChunk_bKGD(&outv, &info);
/*pHYs (must come before the IDAT chunks)*/
if(info.phys_defined) addChunk_pHYs(&outv, &info);
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
#ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
/*unknown chunks between PLTE and IDAT*/
if(info.unknown_chunks.data[1]) { encoder->error = addUnknownChunks(&outv, info.unknown_chunks.data[1], info.unknown_chunks.datasize[1]); if(encoder->error) break; }
#endif /*LODEPNG_COMPILE_UNKNOWN_CHUNKS*/
/*IDAT (multiple IDAT chunks must be consecutive)*/
encoder->error = addChunk_IDAT(&outv, data, datasize, &encoder->settings.zlibsettings);
if(encoder->error) break;
#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
/*tIME*/
if(info.time_defined) addChunk_tIME(&outv, &info.time);
/*tEXt and/or zTXt*/
for(i = 0; i < info.text.num; i++)
{
if(strlen(info.text.keys[i]) > 79) { encoder->error = 66; break; }
if(strlen(info.text.keys[i]) < 1) { encoder->error = 67; break; }
if(encoder->settings.text_compression)
addChunk_zTXt(&outv, info.text.keys[i], info.text.strings[i], &encoder->settings.zlibsettings);
else
addChunk_tEXt(&outv, info.text.keys[i], info.text.strings[i]);
}
/*LodePNG version id in text chunk*/
if(encoder->settings.add_id)
{
unsigned alread_added_id_text = 0;
for(i = 0; i < info.text.num; i++)
if(!strcmp(info.text.keys[i], "LodePNG")) { alread_added_id_text = 1; break; }
if(alread_added_id_text == 0)
addChunk_tEXt(&outv, "LodePNG", VERSION_STRING); /*it's shorter as tEXt than as zTXt chunk*/
}
/*iTXt*/
for(i = 0; i < info.itext.num; i++)
{
if(strlen(info.itext.keys[i]) > 79) { encoder->error = 66; break; }
if(strlen(info.itext.keys[i]) < 1) { encoder->error = 67; break; }
addChunk_iTXt(&outv, encoder->settings.text_compression,
info.itext.keys[i], info.itext.langtags[i], info.itext.transkeys[i], info.itext.strings[i],
&encoder->settings.zlibsettings);
}
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
#ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
/*unknown chunks between IDAT and IEND*/
if(info.unknown_chunks.data[2]) { encoder->error = addUnknownChunks(&outv, info.unknown_chunks.data[2], info.unknown_chunks.datasize[2]); if(encoder->error) break; }
#endif /*LODEPNG_COMPILE_UNKNOWN_CHUNKS*/
/*IEND*/
addChunk_IEND(&outv);
break; /*this isn't really a while loop; no error happened so break out now!*/
}
free(data);
/*instead of cleaning the vector up, give it to the output*/
*out = outv.data;
*outsize = outv.size;
}
unsigned LodePNG_encode32 ( unsigned char **  out,
size_t *  outsize,
const unsigned char *  image,
unsigned  w,
unsigned  h 
)

Definition at line 4042 of file lodepng.cpp.

References LodePNG_Encoder::error, LodePNG_encode(), LodePNG_Encoder_cleanup(), and LodePNG_Encoder_init().

Referenced by LodePNG_encode32f().

{
unsigned error;
LodePNG_Encoder encoder;
LodePNG_encode(&encoder, out, outsize, image, w, h);
error = encoder.error;
return error;
}
unsigned LodePNG_encode32f ( const char *  filename,
const unsigned char *  image,
unsigned  w,
unsigned  h 
)

Definition at line 4054 of file lodepng.cpp.

References LodePNG_encode32(), and LodePNG_saveFile().

{
unsigned char* buffer;
size_t buffersize;
unsigned error = LodePNG_encode32(&buffer, &buffersize, image, w, h);
LodePNG_saveFile(buffer, buffersize, filename);
free(buffer);
return error;
}
void LodePNG_Encoder_cleanup ( LodePNG_Encoder encoder)
void LodePNG_Encoder_copy ( LodePNG_Encoder dest,
const LodePNG_Encoder source 
)
void LodePNG_Encoder_init ( LodePNG_Encoder encoder)
void LodePNG_EncodeSettings_init ( LodePNG_EncodeSettings settings)

Definition at line 4065 of file lodepng.cpp.

References LodePNG_EncodeSettings::autoLeaveOutAlphaChannel, LodePNG_EncodeSettings::force_palette, LodeZlib_DeflateSettings_init(), and LodePNG_EncodeSettings::zlibsettings.

Referenced by LodePNG_Encoder_init().

{
settings->autoLeaveOutAlphaChannel = 1;
settings->force_palette = 0;
#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
settings->add_id = 1;
settings->text_compression = 0;
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
}
unsigned LodePNG_InfoColor_addPalette ( LodePNG_InfoColor info,
unsigned char  r,
unsigned char  g,
unsigned char  b,
unsigned char  a 
)

Definition at line 2046 of file lodepng.cpp.

References LodePNG_InfoColor::palette, and LodePNG_InfoColor::palettesize.

Referenced by Image::save().

{
unsigned char* data;
/*the same resize technique as C++ std::vectors is used, and here it's made so that for a palette with the max of 256 colors, it'll have the exact alloc size*/
if(!(info->palettesize & (info->palettesize - 1))) /*if palettesize is 0 or a power of two*/
{
/*allocated data must be at least 4* palettesize (for 4 color bytes)*/
size_t alloc_size = info->palettesize == 0 ? 4 : info->palettesize * 4 * 2;
data = (unsigned char*)realloc(info->palette, alloc_size);
if(!data) return 9931;
else info->palette = data;
}
info->palette[4 * info->palettesize + 0] = r;
info->palette[4 * info->palettesize + 1] = g;
info->palette[4 * info->palettesize + 2] = b;
info->palette[4 * info->palettesize + 3] = a;
info->palettesize++;
return 0;
}
void LodePNG_InfoColor_cleanup ( LodePNG_InfoColor info)
void LodePNG_InfoColor_clearPalette ( LodePNG_InfoColor info)

Definition at line 2040 of file lodepng.cpp.

References LodePNG_InfoColor::palette, and LodePNG_InfoColor::palettesize.

Referenced by LodePNG_InfoColor_cleanup().

{
if(info->palette) free(info->palette);
info->palettesize = 0;
}
unsigned LodePNG_InfoColor_copy ( LodePNG_InfoColor dest,
const LodePNG_InfoColor source 
)

Definition at line 2323 of file lodepng.cpp.

References LodePNG_InfoColor_cleanup(), LodePNG_InfoColor::palette, and LodePNG_InfoColor::palettesize.

Referenced by LodePNG_InfoPng_copy(), and LodePNG_InfoRaw_copy().

{
size_t i;
*dest = *source;
dest->palette = (unsigned char*)malloc(source->palettesize * 4);
if(!dest->palette && source->palettesize) return 9935;
for(i = 0; i < source->palettesize * 4; i++) dest->palette[i] = source->palette[i];
return 0;
}
unsigned LodePNG_InfoColor_getBpp ( const LodePNG_InfoColor info)

Definition at line 2066 of file lodepng.cpp.

References LodePNG_InfoColor::bitDepth, LodePNG_InfoColor::colorType, and getBpp().

Referenced by filter(), LodePNG_convert(), LodePNG_encode(), and preProcessScanlines().

{ return getBpp(info->colorType, info->bitDepth); } /*calculate bits per pixel out of colorType and bitDepth*/
unsigned LodePNG_InfoColor_getChannels ( const LodePNG_InfoColor info)

Definition at line 2067 of file lodepng.cpp.

References LodePNG_InfoColor::colorType, and getNumColorChannels().

{ return getNumColorChannels(info->colorType); }
void LodePNG_InfoColor_init ( LodePNG_InfoColor info)
unsigned LodePNG_InfoColor_isAlphaType ( const LodePNG_InfoColor info)

Definition at line 2069 of file lodepng.cpp.

References LodePNG_InfoColor::colorType.

Referenced by LodePNG_convert().

{ return (info->colorType & 4) != 0; }
unsigned LodePNG_InfoColor_isGreyscaleType ( const LodePNG_InfoColor info)

Definition at line 2068 of file lodepng.cpp.

References LodePNG_InfoColor::colorType.

Referenced by LodePNG_convert().

{ return info->colorType == 0 || info->colorType == 4; }
void LodePNG_InfoPng_cleanup ( LodePNG_InfoPng info)

Definition at line 2284 of file lodepng.cpp.

References LodePNG_InfoPng::color, and LodePNG_InfoColor_cleanup().

Referenced by LodePNG_Encoder_cleanup(), and LodePNG_InfoPng_copy().

{
#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
LodePNG_Text_cleanup(&info->text);
LodePNG_IText_cleanup(&info->itext);
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
#ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
LodePNG_UnknownChunks_cleanup(&info->unknown_chunks);
#endif /*LODEPNG_COMPILE_UNKNOWN_CHUNKS*/
}
unsigned LodePNG_InfoPng_copy ( LodePNG_InfoPng dest,
const LodePNG_InfoPng source 
)

Definition at line 2296 of file lodepng.cpp.

References LodePNG_InfoPng::color, LodePNG_InfoColor_copy(), LodePNG_InfoColor_init(), and LodePNG_InfoPng_cleanup().

Referenced by LodePNG_Encoder_copy().

{
unsigned error = 0;
*dest = *source;
error = LodePNG_InfoColor_copy(&dest->color, &source->color); if(error) return error;
#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
error = LodePNG_Text_copy(&dest->text, &source->text); if(error) return error;
error = LodePNG_IText_copy(&dest->itext, &source->itext); if(error) return error;
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
#ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
LodePNG_UnknownChunks_init(&dest->unknown_chunks);
error = LodePNG_UnknownChunks_copy(&dest->unknown_chunks, &source->unknown_chunks); if(error) return error;
#endif /*LODEPNG_COMPILE_UNKNOWN_CHUNKS*/
return error;
}
void LodePNG_InfoPng_init ( LodePNG_InfoPng info)

Definition at line 2262 of file lodepng.cpp.

References LodePNG_InfoPng::color, LodePNG_InfoPng::compressionMethod, LodePNG_InfoPng::filterMethod, LodePNG_InfoPng::height, LodePNG_InfoPng::interlaceMethod, LodePNG_InfoColor_init(), and LodePNG_InfoPng::width.

Referenced by LodePNG_Encoder_copy(), and LodePNG_Encoder_init().

{
info->width = info->height = 0;
info->interlaceMethod = 0;
info->compressionMethod = 0;
info->filterMethod = 0;
#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
info->background_defined = 0;
info->background_r = info->background_g = info->background_b = 0;
LodePNG_Text_init(&info->text);
LodePNG_IText_init(&info->itext);
info->time_defined = 0;
info->phys_defined = 0;
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
#ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
LodePNG_UnknownChunks_init(&info->unknown_chunks);
#endif /*LODEPNG_COMPILE_UNKNOWN_CHUNKS*/
}
void LodePNG_InfoRaw_cleanup ( LodePNG_InfoRaw info)
unsigned LodePNG_InfoRaw_copy ( LodePNG_InfoRaw dest,
const LodePNG_InfoRaw source 
)

Definition at line 2344 of file lodepng.cpp.

References LodePNG_InfoRaw::color, LodePNG_InfoColor_copy(), LodePNG_InfoColor_init(), and LodePNG_InfoRaw_cleanup().

Referenced by LodePNG_Encoder_copy().

{
unsigned error = 0;
*dest = *source;
error = LodePNG_InfoColor_copy(&dest->color, &source->color);
return error;
}
void LodePNG_InfoRaw_init ( LodePNG_InfoRaw info)
unsigned LodePNG_loadFile ( unsigned char **  out,
size_t *  outsize,
const char *  filename 
)

Definition at line 4110 of file lodepng.cpp.

References portable_fopen().

{
FILE* file;
long size;
/*provide some proper output values if error will happen*/
*out = 0;
*outsize = 0;
file = portable_fopen(filename, "rb");
if(!file) return 78;
/*get filesize:*/
fseek(file , 0 , SEEK_END);
size = ftell(file);
rewind(file);
/*read contents of the file into the vector*/
if (size>0)
{
*outsize = 0;
*out = (unsigned char*)malloc((size_t)size);
if(size && (*out)) (*outsize) = fread(*out, 1, (size_t)size, file);
}
fclose(file);
if(!(*out) && size) return 80; /*the above malloc failed*/
return 0;
}
unsigned LodePNG_saveFile ( const unsigned char *  buffer,
size_t  buffersize,
const char *  filename 
)

Definition at line 4141 of file lodepng.cpp.

References portable_fopen().

Referenced by LodePNG_encode32f(), Image::save(), and ColoredImage::save().

{
FILE* file;
file = portable_fopen(filename, "wb" );
if(!file) return 79;
fwrite((char*)buffer , 1 , buffersize, file);
fclose(file);
return 0;
}
unsigned LodeZlib_compress ( unsigned char **  out,
size_t *  outsize,
const unsigned char *  in,
size_t  insize,
const LodeZlib_DeflateSettings settings 
)

Definition at line 1651 of file lodepng.cpp.

References adler32(), ucvector::data, LodeFlate_deflate(), LodeZlib_add32bitInt(), ucvector::size, ucvector_cleanup(), ucvector_init(), ucvector_init_buffer(), and ucvector_push_back().

Referenced by LodePNG_compress().

{
/*initially, *out must be NULL and outsize 0, if you just give some random *out that's pointing to a non allocated buffer, this'll crash*/
ucvector deflatedata, outv;
size_t i;
unsigned error;
unsigned ADLER32;
/*zlib data: 1 byte CMF (CM+CINFO), 1 byte FLG, deflate data, 4 byte ADLER32 checksum of the Decompressed data*/
unsigned CMF = 120; /*0b01111000: CM 8, CINFO 7. With CINFO 7, any window size up to 32768 can be used.*/
unsigned FLEVEL = 0;
unsigned FDICT = 0;
unsigned CMFFLG = 256 * CMF + FDICT * 32 + FLEVEL * 64;
unsigned FCHECK = 31 - CMFFLG % 31;
CMFFLG += FCHECK;
ucvector_init_buffer(&outv, *out, *outsize); /*ucvector-controlled version of the output buffer, for dynamic array*/
ucvector_push_back(&outv, (unsigned char)(CMFFLG / 256));
ucvector_push_back(&outv, (unsigned char)(CMFFLG % 256));
ucvector_init(&deflatedata);
error = LodeFlate_deflate(&deflatedata, in, insize, settings);
if(!error)
{
ADLER32 = adler32(in, (unsigned)insize);
for(i = 0; i < deflatedata.size; i++) ucvector_push_back(&outv, deflatedata.data[i]);
ucvector_cleanup(&deflatedata);
LodeZlib_add32bitInt(&outv, ADLER32);
}
*out = outv.data;
*outsize = outv.size;
return error;
}
void LodeZlib_DeflateSettings_init ( LodeZlib_DeflateSettings settings)

Definition at line 1697 of file lodepng.cpp.

References LodeZlib_DeflateSettings::btype, LodeZlib_DeflateSettings::useLZ77, and LodeZlib_DeflateSettings::windowSize.

Referenced by LodePNG_EncodeSettings_init().

{
settings->btype = 2; /*compress with dynamic huffman tree (not in the mathematical sense, just not the predefined one)*/
settings->useLZ77 = 1;
settings->windowSize = 2048; /*this is a good tradeoff between speed and compression ratio*/
}

Variable Documentation

const LodeZlib_DeflateSettings LodeZlib_defaultDeflateSettings

Definition at line 1704 of file lodepng.cpp.

Referenced by filter().