Leptonica 1.68
C Image Processing Library

gifio.c File Reference

Read/write gif format from/to file and memory. More...

#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "allheaders.h"
#include "gif_lib.h"

Go to the source code of this file.


static PIXpixInterlaceGIF (PIX *pixs)
PIXpixReadStreamGif (FILE *fp)
l_int32 pixWriteStreamGif (FILE *fp, PIX *pix)
PIXpixReadMemGif (const l_uint8 *cdata, size_t size)
l_int32 pixWriteMemGif (l_uint8 **pdata, size_t *psize, PIX *pix)


static const l_int32 InterlacedOffset [] = {0, 4, 2, 1}
static const l_int32 InterlacedJumps [] = {8, 8, 4, 2}

Detailed Description

Read/write gif format from/to file and memory.

  Read gif from file
        PIX        *pixReadStreamGif()
        static PIX *pixInterlaceGIF()

  Write gif to file
        l_int32     pixWriteStreamGif()

  Read/write from/to memory (see warning)
        PIX        *pixReadMemGif()
        l_int32     pixWriteMemGif()
  This uses the gif library, version 4.1.6.  Do not use 4.1.4.

  This module was generously contribued by Antony Dovgal.
  He can be contacted at:  tony *AT* daylessday.org

Definition in file gifio.c.

Function Documentation

static PIX * pixInterlaceGIF ( PIX pixs) [static]
PIX* pixReadStreamGif ( FILE *  fp)
l_int32 pixWriteStreamGif ( FILE *  fp,
PIX pix 


Input: stream pix (1, 2, 4, 8, 16 or 32 bpp) Return: 0 if OK, 1 on error

Notes: (1) All output gif have colormaps. If the pix is 32 bpp rgb, this quantizes the colors and writes out 8 bpp. If the pix is 16 bpp grayscale, it converts to 8 bpp first. (2) We can't write to memory using open_memstream() because the gif functions write through a file descriptor, not a file stream.

Definition at line 244 of file gifio.c.

References CALLOC, ERROR_INT, FALSE, FREE, GET_DATA_BIT, GET_DATA_BYTE, GET_DATA_DIBIT, GET_DATA_QBIT, L_WARNING, NULL, pixClone(), pixcmapAddColor(), pixcmapCreate(), pixcmapGetColor(), pixcmapGetCount(), pixConvertRGBToColormap(), pixConvertTo8(), pixDestroy(), pixGetColormap(), pixGetData(), pixGetDepth(), pixGetDimensions(), pixGetText(), pixGetWpl(), pixSetColormap(), PROCNAME, and TRUE.

Referenced by pixWriteStream().

PIX* pixReadMemGif ( const l_uint8 cdata,
size_t  size 


Input: data (const; gif-encoded) size (of data) Return: pix, or null on error

Notes: (1) Of course, we are cheating here -- writing the data out to file and then reading it back in as a gif format. (2) This should not be assumed to be safe from a sophisticated attack, even though we have attempted to make the filename difficult to guess by embedding the process number and the current time in microseconds. The best way to handle temporary files is to use file descriptors (capabilities) or file handles. However, I know of no way to do this for gif files because of the way that libgif handles the file descriptors. The canonical approach would be to do this: char templ[] = "hiddenfilenameXXXXXX"; l_int32 fd = mkstemp(templ); FILE *fp = fdopen(fd, "w+b"); fwrite(data, 1, size, fp); rewind(fp); Pix *pix = pixReadStreamGif(fp); but this fails because fp is in a bad state after writing.

Definition at line 441 of file gifio.c.

References ERROR_PTR, FREE, genTempFilename(), l_binaryWrite(), NULL, pixRead(), and PROCNAME.

Referenced by pixReadHeaderMem(), and pixReadMem().

l_int32 pixWriteMemGif ( l_uint8 **  pdata,
size_t *  psize,
PIX pix 


Input: &data (<return> data of gif compressed image) &size (<return> size of returned data) pix Return: 0 if OK, 1 on error

Notes: (1) See comments in pixReadMemGif()

Definition at line 477 of file gifio.c.

References ERROR_INT, FREE, genTempFilename(), IFF_GIF, l_binaryRead(), pixWrite(), and PROCNAME.

Referenced by pixWriteMem().

Variable Documentation

const l_int32 InterlacedOffset[] = {0, 4, 2, 1} [static]

Definition at line 57 of file gifio.c.

Referenced by pixInterlaceGIF().

const l_int32 InterlacedJumps[] = {8, 8, 4, 2} [static]

Definition at line 58 of file gifio.c.

Referenced by pixInterlaceGIF().

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines