view tools/libgfx.h @ 2066:63284e9ad95e

Bump copyright.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 05 Dec 2018 13:33:15 +0200
parents 451980580189
children e4dc8fbaa5ad
line wrap: on
line source

/*
 * Functions for loading and saving bitmap images
 * Programmed and designed by Matti 'ccr' Hamalainen
 * (C) Copyright 2012-2018 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


enum
{
    DM_IMGFMT_PNG,
    DM_IMGFMT_PPM,
    DM_IMGFMT_PCX,
    DM_IMGFMT_IFF_ILBM,
    DM_IMGFMT_IFF_PBM,
    DM_IMGFMT_RAW,
    DM_IMGFMT_ARAW,
    DM_IMGFMT_CDUMP,
};


enum
{
    DM_COLFMT_PALETTE,
    DM_COLFMT_RGB,
    DM_COLFMT_RGBA,
};


// 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
};


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

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

    int ncolors;    // number of colors in palette, if any
    int ctransp;    // transparency color index
    BOOL constpal;  // is the palette a const?
    DMColor *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  format;              // Target color format DM_COLFMT_* 
    int  scaleX, scaleY;      // Scale factors (1..)
    int  nplanes, bpp, mask;
    BOOL planar;
    int  compression;         // Use compression/compression level (0 = none, 9 = max)
} DMImageConvSpec;


typedef struct
{
    char *fext;
    char *name;
    int  fmtid;  // DM_IMGFMT_*
    int  flags;  // DM_FMT_* flags
    int  (*probe)(const Uint8 *buf, const size_t len);
    int  (*read)(DMResource *fp, DMImage **pimg);
    int  (*write)(DMResource *fp, const DMImage *pimg, const DMImageConvSpec *spec);
} DMImageFormat;


extern const DMImageFormat dmImageFormatList[];
extern const int ndmImageFormatList;
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       dmImageProbeGeneric(const Uint8 *buf, const size_t len, const DMImageFormat **fmt, int *index);

BOOL dmCompareColor(const DMColor *c1, const DMColor *c2, BOOL alpha);
BOOL dmPaletteAlloc(DMColor **ppal, int ncolors, int ctransp);
BOOL dmImagePaletteAlloc(DMImage *img, int ncolors, int ctransp);


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


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

int dmWritePPMImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec);

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

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

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


typedef struct _DMBitStreamContext
{
    void *handle;

    BOOL (*putByte)(struct _DMBitStreamContext *ctx, Uint8 val);

    int outBuf, outBitCount, outByteCount;
} DMBitStreamContext;


void  dmInitBitStreamContext(DMBitStreamContext *ctx);
int   dmFlushBitStream(DMBitStreamContext *ctx);
BOOL  dmPutBits(DMBitStreamContext *ctx, const int val, const int n);
int   dmInitBitStreamFILE(DMBitStreamContext *ctx, DMResource *fp);


#ifdef __cplusplus
}
#endif

#endif // LIBMGFX_H