view tools/libgfx.h @ 2310:75216bf67fd2

Bump copyright.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 09 Jul 2019 10:27:40 +0300
parents 587b26cecf5b
children 82cb32297ed2
line wrap: on
line source

/*
 * Functions for loading and saving bitmap images
 * Programmed and designed by Matti 'ccr' Hamalainen
 * (C) Copyright 2012-2019 Tecnic Software productions (TNSP)
 *
 * Please read file 'COPYING' for information on license and distribution.
 */
#ifndef LIBMGFX_H
#define LIBMGFX_H 1

#include "dmlib.h"
#include "dmres.h"


#ifdef __cplusplus
extern "C" {
#endif


// Image formats
enum
{
    DM_IMGFMT_PNG,
    DM_IMGFMT_PPM,
    DM_IMGFMT_PCX,
    DM_IMGFMT_IFF_ILBM,
    DM_IMGFMT_IFF_PBM,
    DM_IMGFMT_IFF_ACBM,
    DM_IMGFMT_RAW,
    DM_IMGFMT_ARAW,
    DM_IMGFMT_CDUMP,
};


// Palette formats
enum
{
    DM_PALFMT_ACT,
    DM_PALFMT_RAW,
};


// Color formats (these can be used along with DM_FMT_*)
enum
{
    DM_PIXFMT_PALETTE    = 0x0010,
    DM_PIXFMT_GRAYSCALE  = 0x0020,
    DM_PIXFMT_RGB        = 0x0040,
    DM_PIXFMT_RGBA       = 0x0080,

    DM_PIXFMT_ANY        = 0x00f0,
};


// Error handling modes
enum
{
    DM_ERRMODE_FAIL        = 0,
    DM_ERRMODE_RECOV_1,
    DM_ERRMODE_RECOV_2,
};


// Probe scores
enum
{
    DM_PROBE_SCORE_FALSE   = 0,
    DM_PROBE_SCORE_MAYBE   = 250,
    DM_PROBE_SCORE_AVG     = 500,
    DM_PROBE_SCORE_GOOD    = 750,
    DM_PROBE_SCORE_MAX     = 1000,
};


// Flags for readability/writeability of formats
enum
{
    DM_FMT_WR              = 0x0001, // Format can be written
    DM_FMT_RD              = 0x0002, // Format can be read
    DM_FMT_RDWR            = (DM_FMT_RD | DM_FMT_WR), // Read and write support
    DM_FMT_BROKEN          = 0x1000, // Support is broken/incomplete
};


typedef struct
{
    int ncolors;    // number of colors in palette, if any
    int ctransp;    // transparency color index, -1 if none
    DMColor *colors;// colors
} DMPalette;


// Bitmapped image struct
typedef struct
{
    int pixfmt;     // one of types specified by DM_PIXFMT_*
    int width, height;
    int pitch;      // bytes per scanline
    int bpp;        // bits per pixel

    float aspect;   // aspect ratio (vert / horiz), <= 0 if not set

    BOOL constpal;  // is the palette a const?
    DMPalette *pal; // pointer to palette struct, NULL if no palette

    size_t size;
    Uint8 *data;
} DMImage;


typedef struct
{
    int  fmtid;               // DM_IMGFMT_* of target format (a bit of a kludge here)
    int  pixfmt;              // Target color format DM_PIXFMT_*
    int  scaleX, scaleY;      // Integer scale factors (1..)

    int  nplanes, bpp;        // number of bitplanes to use, bits per PLANE to use
    int  mask;                // masking
    BOOL planar;              // use planar format if the format supports it
    int  compression;         // Use compression/compression level (0 = none, 9 = max)
                              // (not all formats support any, or more than on/off compression)
} DMImageWriteSpec;


typedef struct
{
    char *fext;  // Typical filename extension
    char *name;  // Format name / description
    int  fmtid;  // DM_IMGFMT_*
    int  flags;  // DM_FMT_* and DM_PIXFMT_* flags

    int  (*probe)(const Uint8 *buf, const size_t len);
    int  (*read)(DMResource *fp, DMImage **pimg);
    int  (*write)(DMResource *fp, const DMImage *pimg, const DMImageWriteSpec *spec);
} DMImageFormat;


typedef struct
{
    char *fext;  // Typical filename extension
    char *name;  // Format name / description
    int  fmtid;  // DM_PALFMT_*
    int  flags;  // DM_FMT_*

    int  (*probe)(const Uint8 *buf, const size_t len);
    int  (*read)(DMResource *fp, DMPalette **ppal);
    int  (*write)(DMResource *fp, const DMPalette *ppal);
} DMPaletteFormat;


extern const DMImageFormat dmImageFormatList[];
extern const int ndmImageFormatList;
extern const DMPaletteFormat dmPaletteFormatList[];
extern const int ndmPaletteFormatList;
extern int dmGFXErrorMode;


DMImage * dmImageAlloc(const int width, const int height, const int format, const int bpp);
void      dmImageFree(DMImage *img);
int       dmImageGetBytesPerPixel(const int format);
int       dmImageGetBitsPerPixel(const int format);
int       dmImageProbeGeneric(const Uint8 *buf, const size_t len, const DMImageFormat **fmt, int *index);
int       dmGetNPlanesFromNColors(const int ncolors);

BOOL      dmCompareColor(const DMColor *c1, const DMColor *c2, const BOOL alpha);
int       dmPaletteAlloc(DMPalette **ppal, const int ncolors, const int ctransp);
int       dmPaletteResize(DMPalette **ppal, const int ncolors);
void      dmPaletteFree(DMPalette *pal);
int       dmPaletteCopy(DMPalette **pdst, const DMPalette *src);
int       dmPaletteProbeGeneric(const Uint8 *buf, const size_t len, const DMPaletteFormat **fmt, int *index);


int dmReadACTPalette(DMResource *fp, DMPalette **pdst);
int dmWriteACTPalette(DMResource *fp, const DMPalette *pdst);
int dmReadRAWPalette(DMResource *fp, DMPalette **pdst);
int dmWriteRAWPalette(DMResource *fp, const DMPalette *ppal);


int dmWriteImageData(const DMImage *img, void *cbdata,
    int (*writeRowCB)(void *, const Uint8 *, const size_t), const DMImageWriteSpec *spec);


int dmWriteIFFMasterRAWHeader(DMResource *fp, const char *filename, const char *prefix, const DMImage *img, const DMImageWriteSpec *spec);
int dmWriteRAWImage(DMResource *fp, const DMImage *img, const DMImageWriteSpec *spec);

int dmWritePPMImage(DMResource *fp, const DMImage *img, const DMImageWriteSpec *spec);
int dmReadPPMImage(DMResource *fp, DMImage **pimg);

#ifdef DM_USE_LIBPNG
int dmWritePNGImage(DMResource *fp, const DMImage *img, const DMImageWriteSpec *spec);
int dmReadPNGImage(DMResource *fp, DMImage **pimg);
#endif

int dmWritePCXImage(DMResource *fp, const DMImage *img, const DMImageWriteSpec *spec);
int dmReadPCXImage(DMResource *fp, DMImage **pimg);

int dmReadIFFImage(DMResource *fp, DMImage **pimg);
int dmWriteIFFImage(DMResource *fp, const DMImage *img, const DMImageWriteSpec *spec);


//
// Simplistic bitstream reading and writing
//
typedef struct _DMBitStreamContext
{
    void *handle;

    int (*putByte)(struct _DMBitStreamContext *ctx);
    int (*getByte)(struct _DMBitStreamContext *ctx);

    unsigned int
        outBuf, outBitCount, outByteCount,
        inBuf, inBitCount, inByteCount;
} DMBitStreamContext;


void  dmInitBitStreamContext(DMBitStreamContext *ctx);
int   dmFlushBitStream(DMBitStreamContext *ctx);
int   dmInitBitStreamRes(DMBitStreamContext *ctx, DMResource *fp);

int   dmPutBits(DMBitStreamContext *ctx, const unsigned int val, const unsigned int n);
int   dmGetBits(DMBitStreamContext *ctx, unsigned int *val, const unsigned int n);


#ifdef __cplusplus
}
#endif

#endif // LIBMGFX_H