Linear Methods for Image Interpolation
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
Macros | Functions
imageio.h File Reference

Implements ReadImage and WriteImage functions. More...

#include <stdio.h>
#include "basic.h"
Include dependency graph for imageio.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define MAX_IMAGE_SIZE   10000
 Limit on the maximum allowed image width or height (security). More...
 
#define SUPPORTEDSTRING_JPEG   ""
 
#define SUPPORTEDSTRING_PNG   ""
 
#define SUPPORTEDSTRING_TIFF   ""
 
#define IMAGEIO_U8   0x0000
 
#define IMAGEIO_SINGLE   0x0001
 
#define IMAGEIO_FLOAT   IMAGEIO_SINGLE
 
#define IMAGEIO_DOUBLE   0x0002
 
#define IMAGEIO_STRIP_ALPHA   0x0010
 
#define IMAGEIO_BGRFLIP   0x0020
 
#define IMAGEIO_AFLIP   0x0040
 
#define IMAGEIO_GRAYSCALE   0x0080
 
#define IMAGEIO_GRAY   IMAGEIO_GRAYSCALE
 
#define IMAGEIO_PLANAR   0x0100
 
#define IMAGEIO_COLUMNMAJOR   0x0200
 
#define IMAGEIO_RGB   (IMAGEIO_STRIP_ALPHA)
 
#define IMAGEIO_BGR   (IMAGEIO_STRIP_ALPHA | IMAGEIO_BGRFLIP)
 
#define IMAGEIO_RGBA   0x0000
 
#define IMAGEIO_BGRA   (IMAGEIO_BGRFLIP)
 
#define IMAGEIO_ARGB   (IMAGEIO_AFLIP)
 
#define IMAGEIO_ABGR   (IMAGEIO_BGRFLIP | IMAGEIO_AFLIP)
 
#define READIMAGE_FORMATS_SUPPORTED   "BMP" SUPPORTEDSTRING_JPEG SUPPORTEDSTRING_PNG SUPPORTEDSTRING_TIFF
 String macro listing supported formats for ReadImage. More...
 
#define WRITEIMAGE_FORMATS_SUPPORTED   "BMP" SUPPORTEDSTRING_JPEG SUPPORTEDSTRING_PNG SUPPORTEDSTRING_TIFF
 String macro listing supported formats for WriteImage. More...
 
#define _CRT_SECURE_NO_WARNINGS
 Avoid MSVC warnings on using fopen. More...
 

Functions

int IdentifyImageType (char *Type, const char *FileName)
 Identify the file type of an image file by its magic numbers. More...
 
void * ReadImage (int *Width, int *Height, const char *FileName, unsigned Format)
 Read an image file as 32-bit RGBA data. More...
 
int WriteImage (void *Image, int Width, int Height, const char *FileName, unsigned Format, int Quality)
 Write an image file from 8-bit RGBA image data. More...
 

Detailed Description

Implements ReadImage and WriteImage functions.

Author
Pascal Getreuer getre.nosp@m.uer@.nosp@m.gmail.nosp@m..com

Copyright (c) 2010-2012, Pascal Getreuer All rights reserved.

This program is free software: you can use, modify and/or redistribute it under the terms of the simplified BSD License. You should have received a copy of this license along this program. If not, see http://www.opensource.org/licenses/bsd-license.html.

Definition in file imageio.h.

Macro Definition Documentation

#define _CRT_SECURE_NO_WARNINGS

Avoid MSVC warnings on using fopen.

Definition at line 84 of file imageio.h.

#define IMAGEIO_ABGR   (IMAGEIO_BGRFLIP | IMAGEIO_AFLIP)

Definition at line 62 of file imageio.h.

#define IMAGEIO_AFLIP   0x0040

Definition at line 52 of file imageio.h.

#define IMAGEIO_ARGB   (IMAGEIO_AFLIP)

Definition at line 61 of file imageio.h.

#define IMAGEIO_BGR   (IMAGEIO_STRIP_ALPHA | IMAGEIO_BGRFLIP)

Definition at line 58 of file imageio.h.

#define IMAGEIO_BGRA   (IMAGEIO_BGRFLIP)

Definition at line 60 of file imageio.h.

#define IMAGEIO_BGRFLIP   0x0020

Definition at line 51 of file imageio.h.

#define IMAGEIO_COLUMNMAJOR   0x0200

Definition at line 56 of file imageio.h.

#define IMAGEIO_DOUBLE   0x0002

Definition at line 49 of file imageio.h.

#define IMAGEIO_FLOAT   IMAGEIO_SINGLE

Definition at line 48 of file imageio.h.

#define IMAGEIO_GRAY   IMAGEIO_GRAYSCALE

Definition at line 54 of file imageio.h.

#define IMAGEIO_GRAYSCALE   0x0080

Definition at line 53 of file imageio.h.

#define IMAGEIO_PLANAR   0x0100

Definition at line 55 of file imageio.h.

#define IMAGEIO_RGB   (IMAGEIO_STRIP_ALPHA)

Definition at line 57 of file imageio.h.

#define IMAGEIO_RGBA   0x0000

Definition at line 59 of file imageio.h.

#define IMAGEIO_SINGLE   0x0001

Definition at line 47 of file imageio.h.

#define IMAGEIO_STRIP_ALPHA   0x0010

Definition at line 50 of file imageio.h.

#define IMAGEIO_U8   0x0000

Definition at line 46 of file imageio.h.

#define MAX_IMAGE_SIZE   10000

Limit on the maximum allowed image width or height (security).

Definition at line 23 of file imageio.h.

#define READIMAGE_FORMATS_SUPPORTED   "BMP" SUPPORTEDSTRING_JPEG SUPPORTEDSTRING_PNG SUPPORTEDSTRING_TIFF

String macro listing supported formats for ReadImage.

This macro can be used for example as

printf("Supported formats for reading: " READIMAGE_FORMATS_SUPPORTED ".\n");

Definition at line 75 of file imageio.h.

#define SUPPORTEDSTRING_JPEG   ""

Definition at line 32 of file imageio.h.

#define SUPPORTEDSTRING_PNG   ""

Definition at line 37 of file imageio.h.

#define SUPPORTEDSTRING_TIFF   ""

Definition at line 42 of file imageio.h.

#define WRITEIMAGE_FORMATS_SUPPORTED   "BMP" SUPPORTEDSTRING_JPEG SUPPORTEDSTRING_PNG SUPPORTEDSTRING_TIFF

String macro listing supported formats for WriteImage.

Definition at line 79 of file imageio.h.

Function Documentation

int IdentifyImageType ( char *  Type,
const char *  FileName 
)

Identify the file type of an image file by its magic numbers.

Parameters
Typedestination buffer with space for at least 5 chars
FileNameimage file name
Returns
1 on successful identification, 0 on failure.

The routine fills Type with an identifying string. If there is an error or the file type is unknown, Type is set to a null string.

Definition at line 1844 of file imageio.c.

Here is the caller graph for this function:

void* ReadImage ( int *  Width,
int *  Height,
const char *  FileName,
unsigned  Format 
)

Read an image file as 32-bit RGBA data.

Parameters
Width,Heightpointers to be filled with the image dimensions
FileNameimage file name
Formatspecifies the desired format for the image
Returns
Pointer to the image data, or null on failure

The calling syntax is that the filename is the input and Width, and Height and the returned pointer are outputs. ReadImage allocates memory for the image as one contiguous block of memory and returns a pointer. It is the responsibility of the caller to call Free on this pointer when done to release this memory.

A non-null pointer indicates success. On failure, the returned pointer is null, and Width and Height are set to 0.

The Format argument is used by specifying one of the data type options

  • IMAGEIO_U8: unsigned 8-bit components
  • IMAGEIO_SINGLE: float components
  • IMAGEIO_DOUBLE: double components

and one of the channel options

  • IMAGEIO_GRAYSCALE: grayscale data
  • IMAGEIO_RGB: RGB color data (red is the first channel)
  • IMAGEIO_BGR: BGR color data (blue is the first channel)
  • IMAGEIO_RGBA: RGBA color+alpha data
  • IMAGEIO_BGRA: BGRA color+alpha data
  • IMAGEIO_ARGB: ARGB color+alpha data
  • IMAGEIO_ABGR: ABGR color+alpha data

and optionally either or both of the ordering options

  • IMAGEIO_PLANAR: planar order instead of interleaved components
  • IMAGEIO_COLUMNMAJOR: column major order instead of row major order
uint32_t *Image;
int Width, Height;
if(!(Image = (uint32_t *)ReadImage(&Width, &Height, "myimage.bmp",
return 0;
printf("Read image of size %dx%d\n", Width, Height);
...
Free(Image);

With the default formatting IMAGEIO_U8 | IMAGEIO_RGBA, the image is organized in standard row major top-down 32-bit RGBA order. The image is organized as

    (Top left)                                             (Top right)
    Image[0]                Image[1]        ...  Image[Width-1]
    Image[Width]            Image[Width+1]  ...  Image[2*Width]
    ...                     ...             ...  ...
    Image[Width*(Height-1)] ...             ...  Image[Width*Height-1]
    (Bottom left)                                       (Bottom right)

Each element Image[k] represents one RGBA pixel, which is a 32-bit bitfield. The components of pixel Image[k] can be unpacked as

uint8_t *Component = (uint8_t *)&Image[k];
uint8_t Red = Component[0];
uint8_t Green = Component[1];
uint8_t Blue = Component[2];
uint8_t Alpha = Component[3];

Each component is an unsigned 8-bit integer value with range 0-255. Most images do not have alpha information, in which case the alpha component is set to value 255 (full opacity).

With IMAGEIO_SINGLE or IMAGEIO_DOUBLE, the components are values in the range 0 to 1.

Definition at line 1973 of file imageio.c.

Here is the call graph for this function:

Here is the caller graph for this function:

int WriteImage ( void *  Image,
int  Width,
int  Height,
const char *  FileName,
unsigned  Format,
int  Quality 
)

Write an image file from 8-bit RGBA image data.

Parameters
Imagepointer to the image data
Width,Heightimage dimensions
FileNameimage file name
Formatspecifies how the data is formatted (see ReadImage)
Qualitythe JPEG image quality (between 0 and 100)
Returns
1 on success, 0 on failure

The input Image should be a 32-bit RGBA image stored as in the description of ReadImage. WriteImage writes to FileName in the file format specified by its extension. If saving a JPEG image, the Quality argument specifies the quality factor (between 0 and 100). Quality has no effect on other formats.

The return value indicates success with 1 or failure with 0.

Definition at line 2076 of file imageio.c.

Here is the call graph for this function:

Here is the caller graph for this function: