mirror of
https://github.com/rupertwh/bmplib.git
synced 2026-02-04 05:35:48 +00:00
* add dll import/export declarations * add casts to avoid compiler warnings * fix long vs long long error, unearthed by compiling under Windows Compiling with Visual Studio still needs some manual work that is otherwise done by meson: * create config.h * compile and run gen-huffman.c and gen-reversebits.c to create the header files huffman-codes.h and reversebits.h gitignore visual studio
328 lines
10 KiB
C
328 lines
10 KiB
C
/* bmplib - bmplib.h
|
|
*
|
|
* Copyright (c) 2024, Rupert Weber.
|
|
*
|
|
* This file is part of bmplib.
|
|
* bmplib is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as
|
|
* published by the Free Software Foundation, either version 3 of
|
|
* the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library.
|
|
* If not, see <https://www.gnu.org/licenses/>
|
|
*/
|
|
|
|
|
|
#ifndef BMPLIB_H
|
|
#define BMPLIB_H
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#if defined(__GNUC__)
|
|
#define DEPR(m) __attribute__ ((deprecated(m)))
|
|
#else
|
|
#define DEPR(m)
|
|
#endif
|
|
|
|
#ifdef WIN32
|
|
#ifdef BMPLIB_LIB
|
|
#define APIDECL __declspec(dllexport)
|
|
#else
|
|
#define APIDECL __declspec(dllimport)
|
|
#endif
|
|
#else
|
|
#define APIDECL
|
|
#endif
|
|
|
|
|
|
typedef struct Bmphandle *BMPHANDLE;
|
|
|
|
|
|
/*
|
|
* result codes
|
|
*
|
|
* BMP_RESULT_OK all is good, proceed.
|
|
*
|
|
* BMP_RESULT_ERROR something went wrong, image wasn't read
|
|
* from / written to file.
|
|
* Use bmp_errmsg() to get a textual error
|
|
* message.
|
|
*
|
|
* BMP_RESULT_TRUNCATED Some error occurred while loading the
|
|
* image, but image data maybe partially
|
|
* intact.
|
|
* Use bmp_errmsg() to get a textual error
|
|
* message.
|
|
*
|
|
* BMP_RESULT_INVALID Some or all of the pixel values were
|
|
* invalid. This happens when indexed
|
|
* images point to colors beyond the given
|
|
* palette size or to pixels outside of the
|
|
* image dimensions. If the image is also
|
|
* truncated, BMP_RESULT_TRUNCATED will be
|
|
* returned instead.
|
|
*
|
|
* BMP_RESULT_PNG The BMP file contains an embedded PNG
|
|
* BMP_RESULT_JPEG or JPEG file.
|
|
* The file pointer is left in such a state
|
|
* that the PNG/JPEG can be read, e.g. by
|
|
* passing it to libpng/libjpeg.
|
|
* (Obviously, don't use any absolute
|
|
* positioning functions like rewind() or
|
|
* fseek(...,SEEK_SET)), as the file still
|
|
* contains the BMP wrapping.
|
|
*
|
|
* BMP_RESULT_INSANE Header claims that the image is very large.
|
|
* (Default limit: 500MB). You cannot load
|
|
* the image unless you first call
|
|
* bmpread_set_insanity_limit() to set a new
|
|
* sufficiently high limit.
|
|
*/
|
|
enum Bmpresult {
|
|
BMP_RESULT_OK = 0,
|
|
BMP_RESULT_INVALID,
|
|
BMP_RESULT_TRUNCATED,
|
|
BMP_RESULT_INSANE,
|
|
BMP_RESULT_PNG,
|
|
BMP_RESULT_JPEG,
|
|
BMP_RESULT_ERROR,
|
|
};
|
|
typedef enum Bmpresult BMPRESULT;
|
|
|
|
|
|
/*
|
|
* 64-bit BMPs: conversion of RGBA (16bit) values
|
|
*
|
|
* BMP_CONV64_SRGB (default) Assume components are
|
|
* stored in linear light and convert
|
|
* to sRGB gamma.
|
|
*
|
|
* BMP_CONV64_LINEAR No gamma conversion.
|
|
*
|
|
* BMP_CONV64_NONE Leave components as they are. This is
|
|
* a shortcut for the combination
|
|
* - bmpread_set_64bit_conv(BMP_CONV_LINEAR) and
|
|
* - bmp_set_number_format(BMP_FORMAT_S2_13).
|
|
*/
|
|
enum Bmpconv64 {
|
|
BMP_CONV64_SRGB = 0, /* default */
|
|
BMP_CONV64_LINEAR = 1,
|
|
BMP_CONV64_16BIT_SRGB DEPR("use BMP_CONV64_SRGB instead") = 0,
|
|
BMP_CONV64_16BIT DEPR("use BMP_CONV64_LINEAR instead") = 1,
|
|
BMP_CONV64_NONE
|
|
};
|
|
typedef enum Bmpconv64 BMPCONV64;
|
|
|
|
|
|
/*
|
|
* BMP info header versions
|
|
*
|
|
* There doesn't seem to be consensus on whether the
|
|
* BITMAPINFOHEADER is version 1 (with the two Adobe
|
|
* extensions being v2 and v3) or version 3 (with the
|
|
* older BITMAPCIREHEADER and OS22XBITMAPHEADER being
|
|
* v1 and v2).
|
|
* I am going with BITMAPINFOHEADER = v3
|
|
*/
|
|
enum BmpInfoVer {
|
|
BMPINFO_CORE_OS21 = 1, /* 12 bytes */
|
|
BMPINFO_OS22, /* 16 / 40(!) / 64 bytes */
|
|
BMPINFO_V3, /* 40 bytes */
|
|
BMPINFO_V3_ADOBE1, /* 52 bytes, unofficial */
|
|
BMPINFO_V3_ADOBE2, /* 56 bytes, unofficial */
|
|
BMPINFO_V4, /* 108 bytes */
|
|
BMPINFO_V5, /* 124 bytes */
|
|
BMPINFO_FUTURE /* future versions, larger than 124 bytes */
|
|
};
|
|
typedef enum BmpInfoVer BMPINFOVER;
|
|
|
|
|
|
/*
|
|
* RLE type
|
|
*
|
|
* BMP_RLE_NONE no RLE
|
|
*
|
|
* BMP_RLE_AUTO RLE4 for color tables with 16 or fewer
|
|
* colors.
|
|
*
|
|
* BMP_RLE_RLE8 always use RLE8, regardless of color
|
|
* table size.
|
|
*/
|
|
enum BmpRLEtype {
|
|
BMP_RLE_NONE,
|
|
BMP_RLE_AUTO,
|
|
BMP_RLE_RLE8
|
|
};
|
|
typedef enum BmpRLEtype BMPRLETYPE;
|
|
|
|
|
|
/*
|
|
* undefined pixels in RLE images
|
|
*
|
|
* BMP_UNDEFINED_TO_ZERO set undefined pixels to 0 (= first
|
|
* entry in color table).
|
|
*
|
|
* BMP_UNDEFINED_TO_ALPHA (default) make undefined pixels
|
|
* transparent. Always adds an alpha
|
|
* channel to the result.
|
|
*
|
|
*/
|
|
enum BmpUndefined {
|
|
BMP_UNDEFINED_LEAVE,
|
|
BMP_UNDEFINED_TO_ZERO DEPR("use BMP_UNDEFINED_LEAVE instead") = 0,
|
|
BMP_UNDEFINED_TO_ALPHA /* default */
|
|
};
|
|
typedef enum BmpUndefined BMPUNDEFINED;
|
|
|
|
|
|
/*
|
|
* orientation
|
|
*
|
|
* Only relevant when reading the image line-by-line.
|
|
* When reading the image as a whole, it will *always*
|
|
* be returned in top-down orientation. bmpread_orientation()
|
|
* still gives the orientation of the BMP file.
|
|
*/
|
|
enum BmpOrient {
|
|
BMP_ORIENT_BOTTOMUP,
|
|
BMP_ORIENT_TOPDOWN
|
|
};
|
|
typedef enum BmpOrient BMPORIENT;
|
|
|
|
|
|
enum BmpFormat {
|
|
BMP_FORMAT_INT,
|
|
BMP_FORMAT_FLOAT,
|
|
BMP_FORMAT_S2_13
|
|
};
|
|
typedef enum BmpFormat BMPFORMAT;
|
|
|
|
|
|
APIDECL BMPHANDLE bmpread_new(FILE *file);
|
|
|
|
APIDECL BMPRESULT bmpread_load_info(BMPHANDLE h);
|
|
|
|
APIDECL BMPRESULT bmpread_dimensions(BMPHANDLE h,
|
|
int *width,
|
|
int *height,
|
|
int *channels,
|
|
int *bitsperchannel,
|
|
BMPORIENT *orientation);
|
|
|
|
APIDECL int bmpread_width(BMPHANDLE h);
|
|
APIDECL int bmpread_height(BMPHANDLE h);
|
|
APIDECL int bmpread_channels(BMPHANDLE h);
|
|
APIDECL int bmpread_bitsperchannel(BMPHANDLE h);
|
|
APIDECL BMPORIENT bmpread_orientation(BMPHANDLE h);
|
|
|
|
APIDECL int bmpread_resolution_xdpi(BMPHANDLE h);
|
|
APIDECL int bmpread_resolution_ydpi(BMPHANDLE h);
|
|
|
|
APIDECL size_t bmpread_buffersize(BMPHANDLE h);
|
|
|
|
APIDECL BMPRESULT bmpread_load_image(BMPHANDLE h, unsigned char **buffer);
|
|
APIDECL BMPRESULT bmpread_load_line(BMPHANDLE h, unsigned char **buffer);
|
|
|
|
|
|
APIDECL int bmpread_num_palette_colors(BMPHANDLE h);
|
|
APIDECL BMPRESULT bmpread_load_palette(BMPHANDLE h, unsigned char **palette);
|
|
|
|
|
|
APIDECL void bmpread_set_undefined(BMPHANDLE h, BMPUNDEFINED mode);
|
|
APIDECL void bmpread_set_insanity_limit(BMPHANDLE h, size_t limit);
|
|
|
|
APIDECL int bmpread_is_64bit(BMPHANDLE h);
|
|
APIDECL BMPRESULT bmpread_set_64bit_conv(BMPHANDLE h, BMPCONV64 conv);
|
|
|
|
APIDECL BMPINFOVER bmpread_info_header_version(BMPHANDLE h);
|
|
APIDECL const char* bmpread_info_header_name(BMPHANDLE h);
|
|
APIDECL int bmpread_info_header_size(BMPHANDLE h);
|
|
APIDECL int bmpread_info_compression(BMPHANDLE h);
|
|
APIDECL const char* bmpread_info_compression_name(BMPHANDLE h);
|
|
APIDECL int bmpread_info_bitcount(BMPHANDLE h);
|
|
APIDECL BMPRESULT bmpread_info_channel_bits(BMPHANDLE h, int *r, int *g, int *b, int *a);
|
|
|
|
|
|
|
|
|
|
APIDECL BMPHANDLE bmpwrite_new(FILE *file);
|
|
|
|
APIDECL BMPRESULT bmpwrite_set_dimensions(BMPHANDLE h,
|
|
unsigned width,
|
|
unsigned height,
|
|
unsigned channels,
|
|
unsigned bitsperchannel);
|
|
APIDECL BMPRESULT bmpwrite_set_resolution(BMPHANDLE h, int xdpi, int ydpi);
|
|
APIDECL BMPRESULT bmpwrite_set_output_bits(BMPHANDLE h, int red, int green, int blue, int alpha);
|
|
APIDECL BMPRESULT bmpwrite_set_palette(BMPHANDLE h, int numcolors, const unsigned char *palette);
|
|
APIDECL BMPRESULT bmpwrite_allow_2bit(BMPHANDLE h);
|
|
APIDECL BMPRESULT bmpwrite_allow_huffman(BMPHANDLE h);
|
|
APIDECL BMPRESULT bmpwrite_set_rle(BMPHANDLE h, BMPRLETYPE type);
|
|
APIDECL BMPRESULT bmpwrite_set_orientation(BMPHANDLE h, BMPORIENT orientation);
|
|
APIDECL BMPRESULT bmpwrite_set_64bit(BMPHANDLE h);
|
|
|
|
APIDECL BMPRESULT bmpwrite_save_image(BMPHANDLE h, const unsigned char *image);
|
|
APIDECL BMPRESULT bmpwrite_save_line(BMPHANDLE h, const unsigned char *line);
|
|
|
|
|
|
APIDECL BMPRESULT bmp_set_number_format(BMPHANDLE h, BMPFORMAT format);
|
|
|
|
APIDECL void bmp_free(BMPHANDLE h);
|
|
|
|
APIDECL const char* bmp_errmsg(BMPHANDLE h);
|
|
|
|
APIDECL const char* bmp_version(void);
|
|
|
|
|
|
/* these errorcodes aren't part of the API yet, but will be.
|
|
* currently only used internally by bmplib.
|
|
*/
|
|
|
|
#define BMP_ERRTYPE_HARD 0x0000000f
|
|
#define BMP_ERR_FILEIO 0x00000001
|
|
#define BMP_ERR_MEMORY 0x00000002
|
|
#define BMP_ERR_INTERNAL 0x00000004
|
|
|
|
#define BMP_ERRTYPE_DATA 0x0000fff0
|
|
#define BMP_ERR_PIXEL 0x00000010
|
|
#define BMP_ERR_TRUNCATED 0x00000020
|
|
#define BMP_ERR_HEADER 0x00000040
|
|
#define BMP_ERR_INSANE 0x00000080
|
|
#define BMP_ERR_UNSUPPORTED 0x00000100
|
|
#define BMP_ERR_JPEG 0x00000200
|
|
#define BMP_ERR_PNG 0x00000400
|
|
#define BMP_ERR_DIMENSIONS 0x00000800
|
|
#define BMP_ERR_INVALID 0x00001000
|
|
|
|
#define BMP_ERRTYPE_USER 0x0fff0000
|
|
#define BMP_ERR_CONV64 0x00010000
|
|
#define BMP_ERR_FORMAT 0x00020000
|
|
#define BMP_ERR_NULL 0x00040000
|
|
#define BMP_ERR_PALETTE 0x00080000
|
|
#define BMP_ERR_NOINFO 0x00100000
|
|
#define BMP_ERR_UNDEFMODE 0x00200000
|
|
|
|
|
|
|
|
/* these functions are kept for binary compatibility and will be
|
|
* removed from future versions:
|
|
*/
|
|
|
|
APIDECL int DEPR("use bmpread_orientation() instead") bmpread_topdown(BMPHANDLE h);
|
|
APIDECL void DEPR("use bmpread_set_undefined() instead") bmpread_set_undefined_to_alpha(BMPHANDLE h, int mode);
|
|
APIDECL int DEPR("use bmpread_bitsperchannel() instead") bmpread_bits_per_channel(BMPHANDLE h);
|
|
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* BMPLIB_H */
|