io_png/io_png.c File Reference

PNG read/write simplified interface. More...

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

Go to the source code of this file.

Defines

#define PNG_SIG_LEN   4
#define IO_PNG_U8   0x0001
#define IO_PNG_F32   0x0002

Functions

static void * read_png_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 * read_png_raw (const char *fname, size_t *nx, size_t *ny, size_t *nc, int transform, int dtype)
 internal function used to read a PNG file into an array
unsigned char * read_png_u8 (const char *fname, size_t *nx, size_t *ny, size_t *nc)
 read a PNG file into a 8bit integer array
unsigned char * read_png_u8_rgb (const char *fname, size_t *nx, size_t *ny)
 read a PNG file into a 8bit integer array, converted to RGB
unsigned char * read_png_u8_gray (const char *fname, size_t *nx, size_t *ny)
 read a PNG file into a 8bit integer array, converted to gray
float * read_png_f32 (const char *fname, size_t *nx, size_t *ny, size_t *nc)
 read a PNG file into a 32bit float array
float * read_png_f32_rgb (const char *fname, size_t *nx, size_t *ny)
 read a PNG file into a 32bit float array, converted to RGB
float * read_png_f32_gray (const char *fname, size_t *nx, size_t *ny)
 read a PNG file into a 32bit float array, converted to gray
static int write_png_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 write_png_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 write_png_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 write_png_f32 (const char *fname, const float *data, size_t nx, size_t ny, size_t nc)
 write a float array into a PNG file

Detailed Description

PNG read/write simplified interface.

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

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

Todo:

handle lossless 16bit data

add a test suite

internally handle RGB/gray conversion in read_png_raw()

handle deinterlacing as a libpng transform function

Author:
Nicolas Limare <nicolas.limare@cmla.ens-cachan.fr>

Definition in file io_png.c.


Define Documentation

#define IO_PNG_F32   0x0002

Definition at line 79 of file io_png.c.

#define IO_PNG_U8   0x0001

Definition at line 78 of file io_png.c.

#define PNG_SIG_LEN   4

Definition at line 75 of file io_png.c.


Function Documentation

static void* read_png_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:
fp file pointer to close, ignored if NULL
png_ptr_p,info_ptr_p,pointers to PNG structure pointers, ignored if NULL
Returns:
NULL

Definition at line 94 of file io_png.c.

Here is the caller graph for this function:

float* read_png_f32 ( const char *  fname,
size_t *  nx,
size_t *  ny,
size_t *  nc 
)

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:
fname PNG file name
nx,ny,nc pointers 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 379 of file io_png.c.

Here is the call graph for this function:

float* read_png_f32_gray ( const char *  fname,
size_t *  nx,
size_t *  ny 
)

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

See read_png_f32() for details.

Definition at line 432 of file io_png.c.

Here is the call graph for this function:

float* read_png_f32_rgb ( const char *  fname,
size_t *  nx,
size_t *  ny 
)

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

See read_png_f32() for details.

Definition at line 391 of file io_png.c.

Here is the call graph for this function:

static void* read_png_raw ( const char *  fname,
size_t *  nx,
size_t *  ny,
size_t *  nc,
int  transform,
int  dtype 
) [static]

internal function used to read a PNG file into an array

Todo:
don't loose 16bit info
Parameters:
fname PNG file name, "-" means stdin
nx,ny,nc pointers to variables to be filled with the number of columns, lines and channels of the image
transform a PNG_TRANSFORM to be added to the default read transforms
dtype identifier 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 116 of file io_png.c.

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned char* read_png_u8 ( const char *  fname,
size_t *  nx,
size_t *  ny,
size_t *  nc 
)

read a PNG file into a 8bit integer array

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

Todo:
don't downscale 16bit images.
Parameters:
fname PNG file name
nx,ny,nc pointers 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 269 of file io_png.c.

Here is the call graph for this function:

unsigned char* read_png_u8_gray ( const char *  fname,
size_t *  nx,
size_t *  ny 
)

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

See read_png_u8() for details.

Definition at line 324 of file io_png.c.

Here is the call graph for this function:

unsigned char* read_png_u8_rgb ( const char *  fname,
size_t *  nx,
size_t *  ny 
)

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

See read_png_u8() for details.

Definition at line 282 of file io_png.c.

Here is the call graph for this function:

Here is the caller graph for this function:

static int write_png_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:
fp file pointer to close, ignored if NULL
idata,row_pointers arrays to free, ignored if NULL
png_ptr_p,info_ptr_p,pointers to PNG structure pointers, ignored if NULL
Returns:
-1

Definition at line 486 of file io_png.c.

Here is the caller graph for this function:

int write_png_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:
fname PNG file name
data array to write
nx,ny,nc number of columns, lines and channels of the image
Returns:
0 if everything OK, -1 if an error occured

Definition at line 700 of file io_png.c.

Here is the call graph for this function:

static int write_png_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:
fname PNG file name, "-" means stdout
data deinterlaced (RRR..GGG..BBB..AAA) image byte array
nx,ny,nc number of columns, lines and channels
dtype identifier for the data type to be used for output
Returns:
0 if everything OK, -1 if an error occured

Definition at line 515 of file io_png.c.

Here is the call graph for this function:

Here is the caller graph for this function:

int write_png_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:
fname PNG file name
data array to write
nx,ny,nc number of columns, lines and channels of the image
Returns:
0 if everything OK, -1 if an error occured

Definition at line 680 of file io_png.c.

Here is the call graph for this function:

Here is the caller graph for this function:

 All Classes Files Functions Variables Typedefs Defines
Generated on Wed Oct 5 14:39:22 2011 for local color correction by  doxygen 1.6.3