efros_freeman  0.1
Data Structures | Macros | Typedefs | Functions | Variables
io_png.c File Reference

PNG read/write simplified interface. More...

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <string.h>
#include <png.h>
#include "io_png.h"
Include dependency graph for io_png.c:

Go to the source code of this file.

Data Structures

struct  _io_png_err_s

Macros

#define PNG_SIG_LEN   4
#define IO_PNG_U8   0x0001 /* 8bit unsigned integer */
#define IO_PNG_F32   0x0002 /* 32bit float */
#define CR   6968ul
#define CG   23434ul
#define CB   2366ul

Typedefs

typedef struct _io_png_err_s _io_png_err_t

Functions

char * io_png_info (void)
 helps tracking versions, via the string tag inserted into the library
static void _io_png_err_hdl (png_structp png_ptr, png_const_charp msg)
static void * _io_png_read_abort (FILE *fp, png_structp *png_ptr_p, png_infop *info_ptr_p)
 internal function used to cleanup the memory when png_read_raw() fails
static void * io_png_read_raw (const char *fname, size_t *nxp, size_t *nyp, size_t *ncp, int png_transform, int dtype)
 internal function used to read a PNG file into an array
unsigned char * io_png_read_u8 (const char *fname, size_t *nxp, size_t *nyp, size_t *ncp)
 read a PNG file into a 8bit integer array
unsigned char * io_png_read_u8_rgb (const char *fname, size_t *nxp, size_t *nyp)
 read a PNG file into a 8bit integer array, converted to RGB
unsigned char * io_png_read_u8_gray (const char *fname, size_t *nxp, size_t *nyp)
 read a PNG file into a 8bit integer array, converted to gray
float * io_png_read_f32 (const char *fname, size_t *nxp, size_t *nyp, size_t *ncp)
 read a PNG file into a 32bit float array
float * io_png_read_f32_rgb (const char *fname, size_t *nxp, size_t *nyp)
 read a PNG file into a 32bit float array, converted to RGB
float * io_png_read_f32_gray (const char *fname, size_t *nxp, size_t *nyp)
 read a PNG file into a 32bit float array, converted to gray
static int _io_png_write_abort (FILE *fp, png_byte *idata, png_bytep *row_pointers, png_structp *png_ptr_p, png_infop *info_ptr_p)
 internal function used to cleanup the memory when png_write_raw() fails
static int io_png_write_raw (const char *fname, const void *data, size_t nx, size_t ny, size_t nc, int dtype)
 internal function used to write a byte array as a PNG file
int io_png_write_u8 (const char *fname, const unsigned char *data, size_t nx, size_t ny, size_t nc)
 write a 8bit unsigned integer array into a PNG file
int io_png_write_f32 (const char *fname, const float *data, size_t nx, size_t ny, size_t nc)
 write a float array into a PNG file

Variables

static char _io_png_tag [] = "using io_png " IO_PNG_VERSION

Detailed Description

PNG read/write simplified interface.

This is a front-end to libpng, with routines to:

Multi-channel images are handled: gray, gray+alpha, rgb and rgb+alpha, as well as on-the-fly color model conversion.

Todo:

handle 16bit data

replace rgb/gray with sRGB / Y references

implement sRGB gamma and better RGBY conversion

process the data as float before quantization

output float in [o..1]

Author
Nicolas Limare nicol.nosp@m.as.l.nosp@m.imare.nosp@m.@cml.nosp@m.a.ens.nosp@m.-cac.nosp@m.han.f.nosp@m.r

Definition in file io_png.c.

Macro Definition Documentation

#define CB   2366ul
#define CG   23434ul
#define CR   6968ul
#define IO_PNG_F32   0x0002 /* 32bit float */

Definition at line 56 of file io_png.c.

#define IO_PNG_U8   0x0001 /* 8bit unsigned integer */

Definition at line 55 of file io_png.c.

#define PNG_SIG_LEN   4

Definition at line 52 of file io_png.c.

Typedef Documentation

typedef struct _io_png_err_s _io_png_err_t

Function Documentation

static void _io_png_err_hdl ( png_structp  png_ptr,
png_const_charp  msg 
)
static

local error handler see http://www.libpng.org/pub/png/book/chapter14.htmlpointer

Definition at line 89 of file io_png.c.

Here is the caller graph for this function:

static void* _io_png_read_abort ( FILE *  fp,
png_structp *  png_ptr_p,
png_infop *  info_ptr_p 
)
static

internal function used to cleanup the memory when png_read_raw() fails

Parameters
fpfile pointer to close, ignored if NULL
png_ptr_p,info_ptr_p,pointersto PNG structure pointers, ignored if NULL
Returns
NULL

Definition at line 118 of file io_png.c.

Here is the caller graph for this function:

static int _io_png_write_abort ( FILE *  fp,
png_byte *  idata,
png_bytep *  row_pointers,
png_structp *  png_ptr_p,
png_infop *  info_ptr_p 
)
static

internal function used to cleanup the memory when png_write_raw() fails

Parameters
fpfile pointer to close, ignored if NULL
idata,row_pointersarrays to free, ignored if NULL
png_ptr_p,info_ptr_p,pointersto PNG structure pointers, ignored if NULL
Returns
-1

Definition at line 534 of file io_png.c.

Here is the caller graph for this function:

char* io_png_info ( void  )

helps tracking versions, via the string tag inserted into the library

This function is not expected to be used in real-world programs.

Returns
a pointer to a version info string

Definition at line 72 of file io_png.c.

float* io_png_read_f32 ( const char *  fname,
size_t *  nxp,
size_t *  nyp,
size_t *  ncp 
)

read a PNG file into a 32bit float array

The array contains the deinterlaced channels. 1, 2, 4 and 8bit images are converted to float values between 0. and 1., 3., 15. or 255. 16bit images are also downscaled to 8bit before conversion.

Parameters
fnamePNG file name
nxp,nyp,ncppointers to variables to be filled with the number of columns, lines and channels of the image
Returns
pointer to an allocated unsigned char array of pixels, or NULL if an error happens

Definition at line 424 of file io_png.c.

Here is the call graph for this function:

Here is the caller graph for this function:

float* io_png_read_f32_gray ( const char *  fname,
size_t *  nxp,
size_t *  nyp 
)

read a PNG file into a 32bit float array, converted to gray

See io_png_read_f32() for details.

Definition at line 477 of file io_png.c.

Here is the call graph for this function:

float* io_png_read_f32_rgb ( const char *  fname,
size_t *  nxp,
size_t *  nyp 
)

read a PNG file into a 32bit float array, converted to RGB

See io_png_read_f32() for details.

Definition at line 437 of file io_png.c.

Here is the call graph for this function:

static void* io_png_read_raw ( const char *  fname,
size_t *  nxp,
size_t *  nyp,
size_t *  ncp,
int  png_transform,
int  dtype 
)
static

internal function used to read a PNG file into an array

Todo:
don't loose 16bit info
Parameters
fnamePNG file name, "-" means stdin
nxp,nyp,ncppointers to variables to be filled with the number of columns, lines and channels of the image
png_transforma PNG_TRANSFORM flag to be added to the default libpng read transforms
dtypeidentifier for the data type to be used for output
Returns
pointer to an allocated array of pixels, or NULL if an error happens

Definition at line 142 of file io_png.c.

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned char* io_png_read_u8 ( const char *  fname,
size_t *  nxp,
size_t *  nyp,
size_t *  ncp 
)

read a PNG file into a 8bit integer array

The array contains the de-interlaced channels. 1, 2 and 4bit images are converted to 8bit. 16bit images are previously downscaled to 8bit.

Todo:
don't downscale 16bit images.
Parameters
fnamePNG file name
nxp,nyp,ncppointers to variables to be filled with the number of columns, lines and channels of the image
Returns
pointer to an allocated unsigned char array of pixels, or NULL if an error happens

Definition at line 290 of file io_png.c.

Here is the call graph for this function:

unsigned char* io_png_read_u8_gray ( const char *  fname,
size_t *  nxp,
size_t *  nyp 
)

read a PNG file into a 8bit integer array, converted to gray

See io_png_read_u8() for details.

Definition at line 347 of file io_png.c.

Here is the call graph for this function:

unsigned char* io_png_read_u8_rgb ( const char *  fname,
size_t *  nxp,
size_t *  nyp 
)

read a PNG file into a 8bit integer array, converted to RGB

See io_png_read_u8() for details.

Definition at line 304 of file io_png.c.

Here is the call graph for this function:

int io_png_write_f32 ( const char *  fname,
const float *  data,
size_t  nx,
size_t  ny,
size_t  nc 
)

write a float array into a PNG file

The float values are rounded to 8bit integers, and bounded to [0, 255].

Todo:
handle 16bit images and flexible min/max
Parameters
fnamePNG file name
dataarray to write
nx,ny,ncnumber of columns, lines and channels of the image
Returns
0 if everything OK, -1 if an error occured

Definition at line 745 of file io_png.c.

Here is the call graph for this function:

Here is the caller graph for this function:

static int io_png_write_raw ( const char *  fname,
const void *  data,
size_t  nx,
size_t  ny,
size_t  nc,
int  dtype 
)
static

internal function used to write a byte array as a PNG file

The PNG file is written as a 8bit image file, interlaced, truecolor. Depending on the number of channels, the color model is gray, gray+alpha, rgb, rgb+alpha.

Todo:
handle 16bit
Parameters
fnamePNG file name, "-" means stdout
datadeinterlaced (RRR..GGG..BBB..AAA) image byte array
nx,ny,ncnumber of columns, lines and channels
dtypeidentifier for the data type to be used for output
Returns
0 if everything OK, -1 if an error occured

Definition at line 564 of file io_png.c.

Here is the call graph for this function:

Here is the caller graph for this function:

int io_png_write_u8 ( const char *  fname,
const unsigned char *  data,
size_t  nx,
size_t  ny,
size_t  nc 
)

write a 8bit unsigned integer array into a PNG file

Parameters
fnamePNG file name
dataarray to write
nx,ny,ncnumber of columns, lines and channels of the image
Returns
0 if everything OK, -1 if an error occured

Definition at line 725 of file io_png.c.

Here is the call graph for this function:

Variable Documentation

char _io_png_tag[] = "using io_png " IO_PNG_VERSION
static

Definition at line 63 of file io_png.c.