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.

1938 {
1939  unsigned i;
1940  unsigned total_chunk_length = LodePNG_chunk_length(chunk) + 12;
1941  unsigned char *chunk_start, *new_buffer;
1942  size_t new_length = (*outlength) + total_chunk_length;
1943  if(new_length < total_chunk_length || new_length < (*outlength)) return 77; /*integer overflow happened*/
1944 
1945  new_buffer = (unsigned char*)realloc(*out, new_length);
1946  if(!new_buffer) return 9929;
1947  (*out) = new_buffer;
1948  (*outlength) = new_length;
1949  chunk_start = &(*out)[new_length - total_chunk_length];
1950 
1951  for(i = 0; i < total_chunk_length; i++) chunk_start[i] = chunk[i];
1952 
1953  return 0;
1954 }
unsigned LodePNG_chunk_length(const unsigned char *chunk)
Definition: lodepng.cpp:1865
unsigned LodePNG_chunk_check_crc ( const unsigned char *  chunk)

Definition at line 1909 of file lodepng.cpp.

1910 {
1911  unsigned length = LodePNG_chunk_length(chunk);
1912  unsigned CRC = LodePNG_read32bitInt(&chunk[length + 8]);
1913  unsigned checksum = Crc32_crc(&chunk[4], length + 4); /*the CRC is taken of the data and the 4 chunk type letters, not the length*/
1914  if(CRC != checksum) return 1;
1915  else return 0;
1916 }
unsigned LodePNG_chunk_length(const unsigned char *chunk)
Definition: lodepng.cpp:1865
static unsigned LodePNG_read32bitInt(const unsigned char *buffer)
Definition: lodepng.cpp:1840
static unsigned Crc32_crc(const unsigned char *buf, size_t len)
Definition: lodepng.cpp:1799
unsigned char LodePNG_chunk_critical ( const unsigned char *  chunk)

Definition at line 1884 of file lodepng.cpp.

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

Definition at line 1899 of file lodepng.cpp.

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

Definition at line 1904 of file lodepng.cpp.

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

Definition at line 1918 of file lodepng.cpp.

1919 {
1920  unsigned length = LodePNG_chunk_length(chunk);
1921  unsigned CRC = Crc32_crc(&chunk[4], length + 4);
1922  LodePNG_set32bitInt(chunk + 8 + length, CRC);
1923 }
static void LodePNG_set32bitInt(unsigned char *buffer, unsigned value)
Definition: lodepng.cpp:1845
unsigned LodePNG_chunk_length(const unsigned char *chunk)
Definition: lodepng.cpp:1865
static unsigned Crc32_crc(const unsigned char *buf, size_t len)
Definition: lodepng.cpp:1799
unsigned LodePNG_chunk_length ( const unsigned char *  chunk)

Definition at line 1865 of file lodepng.cpp.

1866 {
1867  return LodePNG_read32bitInt(&chunk[0]);
1868 }
static unsigned LodePNG_read32bitInt(const unsigned char *buffer)
Definition: lodepng.cpp:1840
unsigned char* LodePNG_chunk_next ( unsigned char *  chunk)

Definition at line 1925 of file lodepng.cpp.

1926 {
1927  unsigned total_chunk_length = LodePNG_chunk_length(chunk) + 12;
1928  return &chunk[total_chunk_length];
1929 }
unsigned LodePNG_chunk_length(const unsigned char *chunk)
Definition: lodepng.cpp:1865
const unsigned char* LodePNG_chunk_next_const ( const unsigned char *  chunk)

Definition at line 1931 of file lodepng.cpp.

1932 {
1933  unsigned total_chunk_length = LodePNG_chunk_length(chunk) + 12;
1934  return &chunk[total_chunk_length];
1935 }
unsigned LodePNG_chunk_length(const unsigned char *chunk)
Definition: lodepng.cpp:1865
unsigned char LodePNG_chunk_private ( const unsigned char *  chunk)

Definition at line 1889 of file lodepng.cpp.

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

Definition at line 1894 of file lodepng.cpp.

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

Definition at line 1870 of file lodepng.cpp.

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

Definition at line 1877 of file lodepng.cpp.

1878 {
1879  if(strlen(type) != 4) return 0;
1880  return (chunk[4] == type[0] && chunk[5] == type[1] && chunk[6] == type[2] && chunk[7] == type[3]);
1881 }
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.

2362 {
2363  const size_t numpixels = w * h; /*amount of pixels*/
2364  const unsigned OUT_BYTES = LodePNG_InfoColor_getBpp(infoOut) / 8; /*bytes per pixel in the output image*/
2365  const unsigned OUT_ALPHA = LodePNG_InfoColor_isAlphaType(infoOut); /*use 8-bit alpha channel*/
2366  size_t i, c, bp = 0; /*bitpointer, used by less-than-8-bit color types*/
2367 
2368  /*cases where in and out already have the same format*/
2369  if(LodePNG_InfoColor_equal(infoIn, infoOut))
2370  {
2371  size_t i, size = (w * h * LodePNG_InfoColor_getBpp(infoIn) + 7) / 8;
2372  for(i = 0; i < size; i++) out[i] = in[i];
2373  return 0;
2374  }
2375 
2376  if((infoOut->colorType == 2 || infoOut->colorType == 6) && infoOut->bitDepth == 8)
2377  {
2378  if(infoIn->bitDepth == 8)
2379  {
2380  switch(infoIn->colorType)
2381  {
2382  case 0: /*greyscale color*/
2383  for(i = 0; i < numpixels; i++)
2384  {
2385  if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
2386  out[OUT_BYTES * i + 0] = out[OUT_BYTES * i + 1] = out[OUT_BYTES * i + 2] = in[i];
2387  if(OUT_ALPHA && infoIn->key_defined && in[i] == infoIn->key_r) out[OUT_BYTES * i + 3] = 0;
2388  }
2389  break;
2390  case 2: /*RGB color*/
2391  for(i = 0; i < numpixels; i++)
2392  {
2393  if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
2394  for(c = 0; c < 3; c++) out[OUT_BYTES * i + c] = in[3 * i + c];
2395  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;
2396  }
2397  break;
2398  case 3: /*indexed color (palette)*/
2399  for(i = 0; i < numpixels; i++)
2400  {
2401  if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
2402  if(in[i] >= infoIn->palettesize) return 46;
2403  for(c = 0; c < OUT_BYTES; c++) out[OUT_BYTES * i + c] = infoIn->palette[4 * in[i] + c]; /*get rgb colors from the palette*/
2404  }
2405  break;
2406  case 4: /*greyscale with alpha*/
2407  for(i = 0; i < numpixels; i++)
2408  {
2409  out[OUT_BYTES * i + 0] = out[OUT_BYTES * i + 1] = out[OUT_BYTES * i + 2] = in[2 * i + 0];
2410  if(OUT_ALPHA) out[OUT_BYTES * i + 3] = in[2 * i + 1];
2411  }
2412  break;
2413  case 6: /*RGB with alpha*/
2414  for(i = 0; i < numpixels; i++)
2415  {
2416  for(c = 0; c < OUT_BYTES; c++) out[OUT_BYTES * i + c] = in[4 * i + c];
2417  }
2418  break;
2419  default: break;
2420  }
2421  }
2422  else if(infoIn->bitDepth == 16)
2423  {
2424  switch(infoIn->colorType)
2425  {
2426  case 0: /*greyscale color*/
2427  for(i = 0; i < numpixels; i++)
2428  {
2429  if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
2430  out[OUT_BYTES * i + 0] = out[OUT_BYTES * i + 1] = out[OUT_BYTES * i + 2] = in[2 * i];
2431  if(OUT_ALPHA && infoIn->key_defined && 256U * in[i] + in[i + 1] == infoIn->key_r) out[OUT_BYTES * i + 3] = 0;
2432  }
2433  break;
2434  case 2: /*RGB color*/
2435  for(i = 0; i < numpixels; i++)
2436  {
2437  if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
2438  for(c = 0; c < 3; c++) out[OUT_BYTES * i + c] = in[6 * i + 2 * c];
2439  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;
2440  }
2441  break;
2442  case 4: /*greyscale with alpha*/
2443  for(i = 0; i < numpixels; i++)
2444  {
2445  out[OUT_BYTES * i + 0] = out[OUT_BYTES * i + 1] = out[OUT_BYTES * i + 2] = in[4 * i]; /*most significant byte*/
2446  if(OUT_ALPHA) out[OUT_BYTES * i + 3] = in[4 * i + 2];
2447  }
2448  break;
2449  case 6: /*RGB with alpha*/
2450  for(i = 0; i < numpixels; i++)
2451  {
2452  for(c = 0; c < OUT_BYTES; c++) out[OUT_BYTES * i + c] = in[8 * i + 2 * c];
2453  }
2454  break;
2455  default: break;
2456  }
2457  }
2458  else /*infoIn->bitDepth is less than 8 bit per channel*/
2459  {
2460  switch(infoIn->colorType)
2461  {
2462  case 0: /*greyscale color*/
2463  for(i = 0; i < numpixels; i++)
2464  {
2465  unsigned value = readBitsFromReversedStream(&bp, in, infoIn->bitDepth);
2466  if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
2467  if(OUT_ALPHA && infoIn->key_defined && value && ((1U << infoIn->bitDepth) - 1U) == infoIn->key_r && ((1U << infoIn->bitDepth) - 1U)) out[OUT_BYTES * i + 3] = 0;
2468  value = (value * 255) / ((1 << infoIn->bitDepth) - 1); /*scale value from 0 to 255*/
2469  out[OUT_BYTES * i + 0] = out[OUT_BYTES * i + 1] = out[OUT_BYTES * i + 2] = (unsigned char)(value);
2470  }
2471  break;
2472  case 3: /*indexed color (palette)*/
2473  for(i = 0; i < numpixels; i++)
2474  {
2475  unsigned value = readBitsFromReversedStream(&bp, in, infoIn->bitDepth);
2476  if(OUT_ALPHA) out[OUT_BYTES * i + 3] = 255;
2477  if(value >= infoIn->palettesize) return 47;
2478  for(c = 0; c < OUT_BYTES; c++) out[OUT_BYTES * i + c] = infoIn->palette[4 * value + c]; /*get rgb colors from the palette*/
2479  }
2480  break;
2481  default: break;
2482  }
2483  }
2484  }
2485  else if(LodePNG_InfoColor_isGreyscaleType(infoOut) && infoOut->bitDepth == 8) /*conversion from greyscale to greyscale*/
2486  {
2487  if(!LodePNG_InfoColor_isGreyscaleType(infoIn)) return 62;
2488  if(infoIn->bitDepth == 8)
2489  {
2490  switch(infoIn->colorType)
2491  {
2492  case 0: /*greyscale color*/
2493  for(i = 0; i < numpixels; i++)
2494  {
2495  if(OUT_ALPHA) out[OUT_BYTES * i + 1] = 255;
2496  out[OUT_BYTES * i] = in[i];
2497  if(OUT_ALPHA && infoIn->key_defined && in[i] == infoIn->key_r) out[OUT_BYTES * i + 1] = 0;
2498  }
2499  break;
2500  case 4: /*greyscale with alpha*/
2501  for(i = 0; i < numpixels; i++)
2502  {
2503  out[OUT_BYTES * i + 0] = in[2 * i + 0];
2504  if(OUT_ALPHA) out[OUT_BYTES * i + 1] = in[2 * i + 1];
2505  }
2506  break;
2507  default: return 31;
2508  }
2509  }
2510  else if(infoIn->bitDepth == 16)
2511  {
2512  switch(infoIn->colorType)
2513  {
2514  case 0: /*greyscale color*/
2515  for(i = 0; i < numpixels; i++)
2516  {
2517  if(OUT_ALPHA) out[OUT_BYTES * i + 1] = 255;
2518  out[OUT_BYTES * i] = in[2 * i];
2519  if(OUT_ALPHA && infoIn->key_defined && 256U * in[i] + in[i + 1] == infoIn->key_r) out[OUT_BYTES * i + 1] = 0;
2520  }
2521  break;
2522  case 4: /*greyscale with alpha*/
2523  for(i = 0; i < numpixels; i++)
2524  {
2525  out[OUT_BYTES * i] = in[4 * i]; /*most significant byte*/
2526  if(OUT_ALPHA) out[OUT_BYTES * i + 1] = in[4 * i + 2]; /*most significant byte*/
2527  }
2528  break;
2529  default: return 31;
2530  }
2531  }
2532  else /*infoIn->bitDepth is less than 8 bit per channel*/
2533  {
2534  if(infoIn->colorType != 0) return 31; /*colorType 0 is the only greyscale type with < 8 bits per channel*/
2535  for(i = 0; i < numpixels; i++)
2536  {
2537  unsigned value = readBitsFromReversedStream(&bp, in, infoIn->bitDepth);
2538  if(OUT_ALPHA) out[OUT_BYTES * i + 1] = 255;
2539  if(OUT_ALPHA && infoIn->key_defined && value && ((1U << infoIn->bitDepth) - 1U) == infoIn->key_r && ((1U << infoIn->bitDepth) - 1U)) out[OUT_BYTES * i + 1] = 0;
2540  value = (value * 255) / ((1 << infoIn->bitDepth) - 1); /*scale value from 0 to 255*/
2541  out[OUT_BYTES * i] = (unsigned char)(value);
2542  }
2543  }
2544  }
2545  else return 59;
2546 
2547  return 0;
2548 }
size_t palettesize
Definition: lodepng.h:132
unsigned LodePNG_InfoColor_getBpp(const LodePNG_InfoColor *info)
Definition: lodepng.cpp:2066
unsigned colorType
Definition: lodepng.h:127
unsigned bitDepth
Definition: lodepng.h:128
unsigned key_r
Definition: lodepng.h:136
UWORD32 in[16]
Definition: md5.h:44
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
static unsigned readBitsFromReversedStream(size_t *bitpointer, const unsigned char *bitstream, size_t nbits)
Definition: lodepng.cpp:1815
unsigned key_defined
Definition: lodepng.h:135
unsigned key_g
Definition: lodepng.h:137
unsigned LodePNG_InfoColor_isGreyscaleType(const LodePNG_InfoColor *info)
Definition: lodepng.cpp:2068
unsigned LodePNG_InfoColor_equal(const LodePNG_InfoColor *info1, const LodePNG_InfoColor *info2)
Definition: lodepng.cpp:2071
unsigned key_b
Definition: lodepng.h:138
unsigned LodePNG_InfoColor_isAlphaType(const LodePNG_InfoColor *info)
Definition: lodepng.cpp:2069
unsigned char * palette
Definition: lodepng.h:131
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.

1957 {
1958  unsigned i;
1959  unsigned char *chunk, *new_buffer;
1960  size_t new_length = (*outlength) + length + 12;
1961  if(new_length < length + 12 || new_length < (*outlength)) return 77; /*integer overflow happened*/
1962  new_buffer = (unsigned char*)realloc(*out, new_length);
1963  if(!new_buffer) return 9930;
1964  (*out) = new_buffer;
1965  (*outlength) = new_length;
1966  chunk = &(*out)[(*outlength) - length - 12];
1967 
1968  /*1: length*/
1969  LodePNG_set32bitInt(chunk, (unsigned)length);
1970 
1971  /*2: chunk name (4 letters)*/
1972  chunk[4] = type[0];
1973  chunk[5] = type[1];
1974  chunk[6] = type[2];
1975  chunk[7] = type[3];
1976 
1977  /*3: the data*/
1978  for(i = 0; i < length; i++) chunk[8 + i] = data[i];
1979 
1980  /*4: CRC (of the chunkname characters and the data)*/
1982 
1983  return 0;
1984 }
static void LodePNG_set32bitInt(unsigned char *buffer, unsigned value)
Definition: lodepng.cpp:1845
void LodePNG_chunk_generate_crc(unsigned char *chunk)
Definition: lodepng.cpp:1918
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.

3911 {
3913  ucvector outv;
3914  unsigned char* data = 0; /*uncompressed version of the IDAT chunk data*/
3915  size_t datasize = 0;
3916 
3917  /*provide some proper output values if error will happen*/
3918  *out = 0;
3919  *outsize = 0;
3920  encoder->error = 0;
3921 
3922  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*/
3923  info.width = w;
3924  info.height = h;
3925 
3926  if(encoder->settings.autoLeaveOutAlphaChannel && isFullyOpaque(image, w, h, &encoder->infoRaw.color))
3927  {
3928  /*go to a color type without alpha channel*/
3929  if(info.color.colorType == 6) info.color.colorType = 2;
3930  else if(info.color.colorType == 4) info.color.colorType = 0;
3931  }
3932 
3933  if(encoder->settings.zlibsettings.windowSize > 32768) { encoder->error = 60; return; } /*error: windowsize larger than allowed*/
3934  if(encoder->settings.zlibsettings.btype > 2) { encoder->error = 61; return; } /*error: unexisting btype*/
3935  if(encoder->infoPng.interlaceMethod > 1) { encoder->error = 71; return; } /*error: unexisting interlace mode*/
3936  if((encoder->error = checkColorValidity(info.color.colorType, info.color.bitDepth))) return; /*error: unexisting color type given*/
3937  if((encoder->error = checkColorValidity(encoder->infoRaw.color.colorType, encoder->infoRaw.color.bitDepth))) return; /*error: unexisting color type given*/
3938 
3939  if(!LodePNG_InfoColor_equal(&encoder->infoRaw.color, &info.color))
3940  {
3941  unsigned char* converted;
3942  size_t size = (w * h * LodePNG_InfoColor_getBpp(&info.color) + 7) / 8;
3943 
3944  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*/
3945  converted = (unsigned char*)malloc(size);
3946  if(!converted && size) encoder->error = 9955; /*error: malloc failed*/
3947  if(!encoder->error) encoder->error = LodePNG_convert(converted, image, &info.color, &encoder->infoRaw.color, w, h);
3948  if(!encoder->error) preProcessScanlines(&data, &datasize, converted, &info);/*filter(data.data, converted.data, w, h, LodePNG_InfoColor_getBpp(&info.color));*/
3949  free(converted);
3950  }
3951  else preProcessScanlines(&data, &datasize, image, &info);/*filter(data.data, image, w, h, LodePNG_InfoColor_getBpp(&info.color));*/
3952 
3953  ucvector_init(&outv);
3954  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*/
3955  {
3956 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
3957  size_t i;
3958 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
3959  /*write signature and chunks*/
3960  writeSignature(&outv);
3961  /*IHDR*/
3962  addChunk_IHDR(&outv, w, h, info.color.bitDepth, info.color.colorType, info.interlaceMethod);
3963 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
3964  /*unknown chunks between IHDR and PLTE*/
3965  if(info.unknown_chunks.data[0]) { encoder->error = addUnknownChunks(&outv, info.unknown_chunks.data[0], info.unknown_chunks.datasize[0]); if(encoder->error) break; }
3966 #endif /*LODEPNG_COMPILE_UNKNOWN_CHUNKS*/
3967  /*PLTE*/
3968  if(info.color.colorType == 3)
3969  {
3970  if(info.color.palettesize == 0 || info.color.palettesize > 256) { encoder->error = 68; break; }
3971  addChunk_PLTE(&outv, &info.color);
3972  }
3973  if(encoder->settings.force_palette && (info.color.colorType == 2 || info.color.colorType == 6))
3974  {
3975  if(info.color.palettesize == 0 || info.color.palettesize > 256) { encoder->error = 68; break; }
3976  addChunk_PLTE(&outv, &info.color);
3977  }
3978  /*tRNS*/
3979  if(info.color.colorType == 3 && !isPaletteFullyOpaque(info.color.palette, info.color.palettesize)) addChunk_tRNS(&outv, &info.color);
3980  if((info.color.colorType == 0 || info.color.colorType == 2) && info.color.key_defined) addChunk_tRNS(&outv, &info.color);
3981 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
3982  /*bKGD (must come between PLTE and the IDAt chunks*/
3983  if(info.background_defined) addChunk_bKGD(&outv, &info);
3984  /*pHYs (must come before the IDAT chunks)*/
3985  if(info.phys_defined) addChunk_pHYs(&outv, &info);
3986 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
3987 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
3988  /*unknown chunks between PLTE and IDAT*/
3989  if(info.unknown_chunks.data[1]) { encoder->error = addUnknownChunks(&outv, info.unknown_chunks.data[1], info.unknown_chunks.datasize[1]); if(encoder->error) break; }
3990 #endif /*LODEPNG_COMPILE_UNKNOWN_CHUNKS*/
3991  /*IDAT (multiple IDAT chunks must be consecutive)*/
3992  encoder->error = addChunk_IDAT(&outv, data, datasize, &encoder->settings.zlibsettings);
3993  if(encoder->error) break;
3994 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
3995  /*tIME*/
3996  if(info.time_defined) addChunk_tIME(&outv, &info.time);
3997  /*tEXt and/or zTXt*/
3998  for(i = 0; i < info.text.num; i++)
3999  {
4000  if(strlen(info.text.keys[i]) > 79) { encoder->error = 66; break; }
4001  if(strlen(info.text.keys[i]) < 1) { encoder->error = 67; break; }
4002  if(encoder->settings.text_compression)
4003  addChunk_zTXt(&outv, info.text.keys[i], info.text.strings[i], &encoder->settings.zlibsettings);
4004  else
4005  addChunk_tEXt(&outv, info.text.keys[i], info.text.strings[i]);
4006  }
4007  /*LodePNG version id in text chunk*/
4008  if(encoder->settings.add_id)
4009  {
4010  unsigned alread_added_id_text = 0;
4011  for(i = 0; i < info.text.num; i++)
4012  if(!strcmp(info.text.keys[i], "LodePNG")) { alread_added_id_text = 1; break; }
4013  if(alread_added_id_text == 0)
4014  addChunk_tEXt(&outv, "LodePNG", VERSION_STRING); /*it's shorter as tEXt than as zTXt chunk*/
4015  }
4016  /*iTXt*/
4017  for(i = 0; i < info.itext.num; i++)
4018  {
4019  if(strlen(info.itext.keys[i]) > 79) { encoder->error = 66; break; }
4020  if(strlen(info.itext.keys[i]) < 1) { encoder->error = 67; break; }
4021  addChunk_iTXt(&outv, encoder->settings.text_compression,
4022  info.itext.keys[i], info.itext.langtags[i], info.itext.transkeys[i], info.itext.strings[i],
4023  &encoder->settings.zlibsettings);
4024  }
4025 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
4026 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
4027  /*unknown chunks between IDAT and IEND*/
4028  if(info.unknown_chunks.data[2]) { encoder->error = addUnknownChunks(&outv, info.unknown_chunks.data[2], info.unknown_chunks.datasize[2]); if(encoder->error) break; }
4029 #endif /*LODEPNG_COMPILE_UNKNOWN_CHUNKS*/
4030  /*IEND*/
4031  addChunk_IEND(&outv);
4032 
4033  break; /*this isn't really a while loop; no error happened so break out now!*/
4034  }
4035 
4036  free(data);
4037  /*instead of cleaning the vector up, give it to the output*/
4038  *out = outv.data;
4039  *outsize = outv.size;
4040 }
size_t palettesize
Definition: lodepng.h:132
static unsigned isFullyOpaque(const unsigned char *image, unsigned w, unsigned h, const LodePNG_InfoColor *info)
Definition: lodepng.cpp:3857
unsigned LodePNG_InfoColor_getBpp(const LodePNG_InfoColor *info)
Definition: lodepng.cpp:2066
unsigned colorType
Definition: lodepng.h:127
LodePNG_InfoRaw infoRaw
Definition: lodepng.h:342
unsigned bitDepth
Definition: lodepng.h:128
unsigned interlaceMethod
Definition: lodepng.h:224
LodePNG_InfoColor color
Definition: lodepng.h:225
static unsigned isPaletteFullyOpaque(const unsigned char *palette, size_t palettesize)
Definition: lodepng.cpp:3846
LodePNG_EncodeSettings settings
Definition: lodepng.h:340
unsigned force_palette
Definition: lodepng.h:329
static unsigned addChunk_PLTE(ucvector *out, const LodePNG_InfoColor *info)
Definition: lodepng.cpp:3318
static unsigned addChunk_tRNS(ucvector *out, const LodePNG_InfoColor *info)
Definition: lodepng.cpp:3331
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
unsigned char * data
Definition: lodepng.cpp:201
LodeZlib_DeflateSettings zlibsettings
Definition: lodepng.h:326
static unsigned checkColorValidity(unsigned colorType, unsigned bd)
Definition: lodepng.cpp:1991
static void writeSignature(ucvector *out)
Definition: lodepng.cpp:3285
LodePNG_InfoColor color
Definition: lodepng.h:266
static unsigned addChunk_IEND(ucvector *out)
Definition: lodepng.cpp:3382
static void ucvector_init(ucvector *p)
Definition: lodepng.cpp:243
unsigned height
Definition: lodepng.h:221
static unsigned preProcessScanlines(unsigned char **out, size_t *outsize, const unsigned char *in, const LodePNG_InfoPng *infoPng)
Definition: lodepng.cpp:3762
static unsigned addChunk_IDAT(ucvector *out, const unsigned char *data, size_t datasize, LodeZlib_DeflateSettings *zlibsettings)
Definition: lodepng.cpp:3368
LodePNG_InfoPng infoPng
Definition: lodepng.h:341
unsigned key_defined
Definition: lodepng.h:135
unsigned LodePNG_convert(unsigned char *out, const unsigned char *in, LodePNG_InfoColor *infoOut, LodePNG_InfoColor *infoIn, unsigned w, unsigned h)
Definition: lodepng.cpp:2361
unsigned width
Definition: lodepng.h:220
size_t size
Definition: lodepng.cpp:202
int strcmp(const String &s1, const String &s2)
Definition: relates.cpp:14
unsigned LodePNG_InfoColor_equal(const LodePNG_InfoColor *info1, const LodePNG_InfoColor *info2)
Definition: lodepng.cpp:2071
static unsigned addChunk_IHDR(ucvector *out, unsigned w, unsigned h, unsigned bitDepth, unsigned colorType, unsigned interlaceMethod)
Definition: lodepng.cpp:3298
unsigned char * palette
Definition: lodepng.h:131
unsigned autoLeaveOutAlphaChannel
Definition: lodepng.h:328
#define VERSION_STRING
Definition: lodepng.cpp:36
unsigned error
Definition: lodepng.h:343
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.

4043 {
4044  unsigned error;
4046  LodePNG_Encoder_init(&encoder);
4047  LodePNG_encode(&encoder, out, outsize, image, w, h);
4048  error = encoder.error;
4049  LodePNG_Encoder_cleanup(&encoder);
4050  return error;
4051 }
void LodePNG_Encoder_init(LodePNG_Encoder *encoder)
Definition: lodepng.cpp:4076
error
Definition: include.cc:26
void LodePNG_encode(LodePNG_Encoder *encoder, unsigned char **out, size_t *outsize, const unsigned char *image, unsigned w, unsigned h)
Definition: lodepng.cpp:3910
void LodePNG_Encoder_cleanup(LodePNG_Encoder *encoder)
Definition: lodepng.cpp:4084
static QFile::EncoderFn encoder
Definition: qfile.cpp:470
unsigned error
Definition: lodepng.h:343
unsigned LodePNG_encode32f ( const char *  filename,
const unsigned char *  image,
unsigned  w,
unsigned  h 
)

Definition at line 4054 of file lodepng.cpp.

4055 {
4056  unsigned char* buffer;
4057  size_t buffersize;
4058  unsigned error = LodePNG_encode32(&buffer, &buffersize, image, w, h);
4059  LodePNG_saveFile(buffer, buffersize, filename);
4060  free(buffer);
4061  return error;
4062 }
error
Definition: include.cc:26
string filename
Definition: train.py:213
unsigned LodePNG_saveFile(const unsigned char *buffer, size_t buffersize, const char *filename)
Definition: lodepng.cpp:4141
unsigned LodePNG_encode32(unsigned char **out, size_t *outsize, const unsigned char *image, unsigned w, unsigned h)
Definition: lodepng.cpp:4042
void LodePNG_Encoder_cleanup ( LodePNG_Encoder encoder)

Definition at line 4084 of file lodepng.cpp.

4085 {
4086  LodePNG_InfoPng_cleanup(&encoder->infoPng);
4087  LodePNG_InfoRaw_cleanup(&encoder->infoRaw);
4088 }
LodePNG_InfoRaw infoRaw
Definition: lodepng.h:342
void LodePNG_InfoPng_cleanup(LodePNG_InfoPng *info)
Definition: lodepng.cpp:2284
LodePNG_InfoPng infoPng
Definition: lodepng.h:341
void LodePNG_InfoRaw_cleanup(LodePNG_InfoRaw *info)
Definition: lodepng.cpp:2339
void LodePNG_Encoder_copy ( LodePNG_Encoder dest,
const LodePNG_Encoder source 
)

Definition at line 4090 of file lodepng.cpp.

4091 {
4093  *dest = *source;
4094  LodePNG_InfoPng_init(&dest->infoPng);
4095  LodePNG_InfoRaw_init(&dest->infoRaw);
4096  dest->error = LodePNG_InfoPng_copy(&dest->infoPng, &source->infoPng); if(dest->error) return;
4097  dest->error = LodePNG_InfoRaw_copy(&dest->infoRaw, &source->infoRaw); if(dest->error) return;
4098 }
unsigned LodePNG_InfoPng_copy(LodePNG_InfoPng *dest, const LodePNG_InfoPng *source)
Definition: lodepng.cpp:2296
LodePNG_InfoRaw infoRaw
Definition: lodepng.h:342
void LodePNG_Encoder_cleanup(LodePNG_Encoder *encoder)
Definition: lodepng.cpp:4084
void LodePNG_InfoRaw_init(LodePNG_InfoRaw *info)
Definition: lodepng.cpp:2334
LodePNG_InfoPng infoPng
Definition: lodepng.h:341
unsigned LodePNG_InfoRaw_copy(LodePNG_InfoRaw *dest, const LodePNG_InfoRaw *source)
Definition: lodepng.cpp:2344
void LodePNG_InfoPng_init(LodePNG_InfoPng *info)
Definition: lodepng.cpp:2262
unsigned error
Definition: lodepng.h:343
void LodePNG_Encoder_init ( LodePNG_Encoder encoder)

Definition at line 4076 of file lodepng.cpp.

4077 {
4079  LodePNG_InfoPng_init(&encoder->infoPng);
4080  LodePNG_InfoRaw_init(&encoder->infoRaw);
4081  encoder->error = 1;
4082 }
LodePNG_InfoRaw infoRaw
Definition: lodepng.h:342
LodePNG_EncodeSettings settings
Definition: lodepng.h:340
void LodePNG_InfoRaw_init(LodePNG_InfoRaw *info)
Definition: lodepng.cpp:2334
LodePNG_InfoPng infoPng
Definition: lodepng.h:341
void LodePNG_InfoPng_init(LodePNG_InfoPng *info)
Definition: lodepng.cpp:2262
unsigned error
Definition: lodepng.h:343
void LodePNG_EncodeSettings_init(LodePNG_EncodeSettings *settings)
Definition: lodepng.cpp:4065
void LodePNG_EncodeSettings_init ( LodePNG_EncodeSettings settings)

Definition at line 4065 of file lodepng.cpp.

4066 {
4068  settings->autoLeaveOutAlphaChannel = 1;
4069  settings->force_palette = 0;
4070 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
4071  settings->add_id = 1;
4072  settings->text_compression = 0;
4073 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
4074 }
void LodeZlib_DeflateSettings_init(LodeZlib_DeflateSettings *settings)
Definition: lodepng.cpp:1697
unsigned force_palette
Definition: lodepng.h:329
LodeZlib_DeflateSettings zlibsettings
Definition: lodepng.h:326
unsigned autoLeaveOutAlphaChannel
Definition: lodepng.h:328
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.

2047 {
2048  unsigned char* data;
2049  /*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*/
2050  if(!(info->palettesize & (info->palettesize - 1))) /*if palettesize is 0 or a power of two*/
2051  {
2052  /*allocated data must be at least 4* palettesize (for 4 color bytes)*/
2053  size_t alloc_size = info->palettesize == 0 ? 4 : info->palettesize * 4 * 2;
2054  data = (unsigned char*)realloc(info->palette, alloc_size);
2055  if(!data) return 9931;
2056  else info->palette = data;
2057  }
2058  info->palette[4 * info->palettesize + 0] = r;
2059  info->palette[4 * info->palettesize + 1] = g;
2060  info->palette[4 * info->palettesize + 2] = b;
2061  info->palette[4 * info->palettesize + 3] = a;
2062  info->palettesize++;
2063  return 0;
2064 }
size_t palettesize
Definition: lodepng.h:132
static constexpr double g
Definition: Units.h:144
const double a
static bool * b
Definition: config.cpp:1043
unsigned char * palette
Definition: lodepng.h:131
void LodePNG_InfoColor_cleanup ( LodePNG_InfoColor info)

Definition at line 2035 of file lodepng.cpp.

2036 {
2038 }
void LodePNG_InfoColor_clearPalette(LodePNG_InfoColor *info)
Definition: lodepng.cpp:2040
void LodePNG_InfoColor_clearPalette ( LodePNG_InfoColor info)

Definition at line 2040 of file lodepng.cpp.

2041 {
2042  if(info->palette) free(info->palette);
2043  info->palettesize = 0;
2044 }
size_t palettesize
Definition: lodepng.h:132
unsigned char * palette
Definition: lodepng.h:131
unsigned LodePNG_InfoColor_copy ( LodePNG_InfoColor dest,
const LodePNG_InfoColor source 
)

Definition at line 2323 of file lodepng.cpp.

2324 {
2325  size_t i;
2327  *dest = *source;
2328  dest->palette = (unsigned char*)malloc(source->palettesize * 4);
2329  if(!dest->palette && source->palettesize) return 9935;
2330  for(i = 0; i < source->palettesize * 4; i++) dest->palette[i] = source->palette[i];
2331  return 0;
2332 }
size_t palettesize
Definition: lodepng.h:132
void LodePNG_InfoColor_cleanup(LodePNG_InfoColor *info)
Definition: lodepng.cpp:2035
unsigned char * palette
Definition: lodepng.h:131
unsigned LodePNG_InfoColor_getBpp ( const LodePNG_InfoColor info)

Definition at line 2066 of file lodepng.cpp.

2066 { return getBpp(info->colorType, info->bitDepth); } /*calculate bits per pixel out of colorType and bitDepth*/
unsigned colorType
Definition: lodepng.h:127
unsigned bitDepth
Definition: lodepng.h:128
static unsigned getBpp(unsigned colorType, unsigned bitDepth)
Definition: lodepng.cpp:2018
unsigned LodePNG_InfoColor_getChannels ( const LodePNG_InfoColor info)

Definition at line 2067 of file lodepng.cpp.

2067 { return getNumColorChannels(info->colorType); }
unsigned colorType
Definition: lodepng.h:127
static unsigned getNumColorChannels(unsigned colorType)
Definition: lodepng.cpp:2005
void LodePNG_InfoColor_init ( LodePNG_InfoColor info)

Definition at line 2025 of file lodepng.cpp.

2026 {
2027  info->key_defined = 0;
2028  info->key_r = info->key_g = info->key_b = 0;
2029  info->colorType = 6;
2030  info->bitDepth = 8;
2031  info->palette = 0;
2032  info->palettesize = 0;
2033 }
size_t palettesize
Definition: lodepng.h:132
unsigned colorType
Definition: lodepng.h:127
unsigned bitDepth
Definition: lodepng.h:128
unsigned key_r
Definition: lodepng.h:136
unsigned key_defined
Definition: lodepng.h:135
unsigned key_g
Definition: lodepng.h:137
unsigned key_b
Definition: lodepng.h:138
unsigned char * palette
Definition: lodepng.h:131
unsigned LodePNG_InfoColor_isAlphaType ( const LodePNG_InfoColor info)

Definition at line 2069 of file lodepng.cpp.

2069 { return (info->colorType & 4) != 0; }
unsigned colorType
Definition: lodepng.h:127
unsigned LodePNG_InfoColor_isGreyscaleType ( const LodePNG_InfoColor info)

Definition at line 2068 of file lodepng.cpp.

2068 { return info->colorType == 0 || info->colorType == 4; }
unsigned colorType
Definition: lodepng.h:127
void LodePNG_InfoPng_cleanup ( LodePNG_InfoPng info)

Definition at line 2284 of file lodepng.cpp.

2285 {
2287 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
2288  LodePNG_Text_cleanup(&info->text);
2289  LodePNG_IText_cleanup(&info->itext);
2290 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
2291 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
2292  LodePNG_UnknownChunks_cleanup(&info->unknown_chunks);
2293 #endif /*LODEPNG_COMPILE_UNKNOWN_CHUNKS*/
2294 }
LodePNG_InfoColor color
Definition: lodepng.h:225
void LodePNG_InfoColor_cleanup(LodePNG_InfoColor *info)
Definition: lodepng.cpp:2035
unsigned LodePNG_InfoPng_copy ( LodePNG_InfoPng dest,
const LodePNG_InfoPng source 
)

Definition at line 2296 of file lodepng.cpp.

2297 {
2298  unsigned error = 0;
2300  *dest = *source;
2301  LodePNG_InfoColor_init(&dest->color);
2302  error = LodePNG_InfoColor_copy(&dest->color, &source->color); if(error) return error;
2303 
2304 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
2305  error = LodePNG_Text_copy(&dest->text, &source->text); if(error) return error;
2306  error = LodePNG_IText_copy(&dest->itext, &source->itext); if(error) return error;
2307 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
2308 
2309 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
2310  LodePNG_UnknownChunks_init(&dest->unknown_chunks);
2311  error = LodePNG_UnknownChunks_copy(&dest->unknown_chunks, &source->unknown_chunks); if(error) return error;
2312 #endif /*LODEPNG_COMPILE_UNKNOWN_CHUNKS*/
2313  return error;
2314 }
LodePNG_InfoColor color
Definition: lodepng.h:225
void LodePNG_InfoColor_init(LodePNG_InfoColor *info)
Definition: lodepng.cpp:2025
error
Definition: include.cc:26
void LodePNG_InfoPng_cleanup(LodePNG_InfoPng *info)
Definition: lodepng.cpp:2284
unsigned LodePNG_InfoColor_copy(LodePNG_InfoColor *dest, const LodePNG_InfoColor *source)
Definition: lodepng.cpp:2323
void LodePNG_InfoPng_init ( LodePNG_InfoPng info)

Definition at line 2262 of file lodepng.cpp.

2263 {
2264  info->width = info->height = 0;
2265  LodePNG_InfoColor_init(&info->color);
2266  info->interlaceMethod = 0;
2267  info->compressionMethod = 0;
2268  info->filterMethod = 0;
2269 #ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
2270  info->background_defined = 0;
2271  info->background_r = info->background_g = info->background_b = 0;
2272 
2273  LodePNG_Text_init(&info->text);
2274  LodePNG_IText_init(&info->itext);
2275 
2276  info->time_defined = 0;
2277  info->phys_defined = 0;
2278 #endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
2279 #ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
2280  LodePNG_UnknownChunks_init(&info->unknown_chunks);
2281 #endif /*LODEPNG_COMPILE_UNKNOWN_CHUNKS*/
2282 }
unsigned compressionMethod
Definition: lodepng.h:222
unsigned interlaceMethod
Definition: lodepng.h:224
LodePNG_InfoColor color
Definition: lodepng.h:225
void LodePNG_InfoColor_init(LodePNG_InfoColor *info)
Definition: lodepng.cpp:2025
unsigned filterMethod
Definition: lodepng.h:223
unsigned height
Definition: lodepng.h:221
unsigned width
Definition: lodepng.h:220
void LodePNG_InfoRaw_cleanup ( LodePNG_InfoRaw info)

Definition at line 2339 of file lodepng.cpp.

2340 {
2342 }
void LodePNG_InfoColor_cleanup(LodePNG_InfoColor *info)
Definition: lodepng.cpp:2035
LodePNG_InfoColor color
Definition: lodepng.h:266
unsigned LodePNG_InfoRaw_copy ( LodePNG_InfoRaw dest,
const LodePNG_InfoRaw source 
)

Definition at line 2344 of file lodepng.cpp.

2345 {
2346  unsigned error = 0;
2348  *dest = *source;
2349  LodePNG_InfoColor_init(&dest->color);
2350  error = LodePNG_InfoColor_copy(&dest->color, &source->color);
2351  return error;
2352 }
void LodePNG_InfoColor_init(LodePNG_InfoColor *info)
Definition: lodepng.cpp:2025
error
Definition: include.cc:26
LodePNG_InfoColor color
Definition: lodepng.h:266
unsigned LodePNG_InfoColor_copy(LodePNG_InfoColor *dest, const LodePNG_InfoColor *source)
Definition: lodepng.cpp:2323
void LodePNG_InfoRaw_cleanup(LodePNG_InfoRaw *info)
Definition: lodepng.cpp:2339
void LodePNG_InfoRaw_init ( LodePNG_InfoRaw info)

Definition at line 2334 of file lodepng.cpp.

2335 {
2336  LodePNG_InfoColor_init(&info->color);
2337 }
void LodePNG_InfoColor_init(LodePNG_InfoColor *info)
Definition: lodepng.cpp:2025
LodePNG_InfoColor color
Definition: lodepng.h:266
unsigned LodePNG_loadFile ( unsigned char **  out,
size_t *  outsize,
const char *  filename 
)

Definition at line 4110 of file lodepng.cpp.

4111 {
4112  FILE* file;
4113  long size;
4114 
4115  /*provide some proper output values if error will happen*/
4116  *out = 0;
4117  *outsize = 0;
4118 
4119  file = portable_fopen(filename, "rb");
4120  if(!file) return 78;
4121 
4122  /*get filesize:*/
4123  fseek(file , 0 , SEEK_END);
4124  size = ftell(file);
4125  rewind(file);
4126 
4127  /*read contents of the file into the vector*/
4128  if (size>0)
4129  {
4130  *outsize = 0;
4131  *out = (unsigned char*)malloc((size_t)size);
4132  if(size && (*out)) (*outsize) = fread(*out, 1, (size_t)size, file);
4133  }
4134 
4135  fclose(file);
4136  if(!(*out) && size) return 80; /*the above malloc failed*/
4137  return 0;
4138 }
string filename
Definition: train.py:213
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
FILE * portable_fopen(const char *fileName, const char *mode)
Definition: portable.cpp:344
unsigned LodePNG_saveFile ( const unsigned char *  buffer,
size_t  buffersize,
const char *  filename 
)

Definition at line 4141 of file lodepng.cpp.

4142 {
4143  FILE* file;
4144  file = portable_fopen(filename, "wb" );
4145  if(!file) return 79;
4146  fwrite((char*)buffer , 1 , buffersize, file);
4147  fclose(file);
4148  return 0;
4149 }
string filename
Definition: train.py:213
FILE * portable_fopen(const char *fileName, const char *mode)
Definition: portable.cpp:344
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.

1652 {
1653  /*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*/
1654  ucvector deflatedata, outv;
1655  size_t i;
1656  unsigned error;
1657 
1658  unsigned ADLER32;
1659  /*zlib data: 1 byte CMF (CM+CINFO), 1 byte FLG, deflate data, 4 byte ADLER32 checksum of the Decompressed data*/
1660  unsigned CMF = 120; /*0b01111000: CM 8, CINFO 7. With CINFO 7, any window size up to 32768 can be used.*/
1661  unsigned FLEVEL = 0;
1662  unsigned FDICT = 0;
1663  unsigned CMFFLG = 256 * CMF + FDICT * 32 + FLEVEL * 64;
1664  unsigned FCHECK = 31 - CMFFLG % 31;
1665  CMFFLG += FCHECK;
1666 
1667  ucvector_init_buffer(&outv, *out, *outsize); /*ucvector-controlled version of the output buffer, for dynamic array*/
1668 
1669  ucvector_push_back(&outv, (unsigned char)(CMFFLG / 256));
1670  ucvector_push_back(&outv, (unsigned char)(CMFFLG % 256));
1671 
1672  ucvector_init(&deflatedata);
1673  error = LodeFlate_deflate(&deflatedata, in, insize, settings);
1674 
1675  if(!error)
1676  {
1677  ADLER32 = adler32(in, (unsigned)insize);
1678  for(i = 0; i < deflatedata.size; i++) ucvector_push_back(&outv, deflatedata.data[i]);
1679  ucvector_cleanup(&deflatedata);
1680  LodeZlib_add32bitInt(&outv, ADLER32);
1681  }
1682 
1683  *out = outv.data;
1684  *outsize = outv.size;
1685 
1686  return error;
1687 }
void LodeZlib_add32bitInt(ucvector *buffer, unsigned value)
Definition: lodepng.cpp:1592
error
Definition: include.cc:26
static unsigned ucvector_push_back(ucvector *p, unsigned char c)
Definition: lodepng.cpp:258
UWORD32 in[16]
Definition: md5.h:44
unsigned char * data
Definition: lodepng.cpp:201
static void ucvector_cleanup(void *p)
Definition: lodepng.cpp:206
static void ucvector_init(ucvector *p)
Definition: lodepng.cpp:243
unsigned LodeFlate_deflate(ucvector *out, const unsigned char *data, size_t datasize, const LodeZlib_DeflateSettings *settings)
Definition: lodepng.cpp:1542
static void ucvector_init_buffer(ucvector *p, unsigned char *buffer, size_t size)
Definition: lodepng.cpp:251
static unsigned adler32(const unsigned char *data, unsigned len)
Definition: lodepng.cpp:1582
size_t size
Definition: lodepng.cpp:202
void LodeZlib_DeflateSettings_init ( LodeZlib_DeflateSettings settings)

Definition at line 1697 of file lodepng.cpp.

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

Variable Documentation

const LodeZlib_DeflateSettings LodeZlib_defaultDeflateSettings

Definition at line 1704 of file lodepng.cpp.