17 Commits

Author SHA1 Message Date
Rupert
b43fd940e6 bump version to 1.7.7 2025-04-18 15:53:08 +02:00
Rupert
61a4caf6ef fix line-by-line writing bug, refactor write state
bug introduced by 142427e17: line-by-line writing of RLE files resulted
in corrupted files.
Found while refactoring write state
2025-04-18 15:51:09 +02:00
Rupert
4942795e14 don't require explicitly calling load_info() before reading ICC profile 2025-04-17 23:48:19 +02:00
Rupert
52801cd5d5 cleanup s_decide_outformat() 2025-04-17 23:47:24 +02:00
Rupert
0e4e611955 add semicolons in doc for consistent syntax highlighter 2025-04-16 23:50:00 +02:00
Rupert
58ebfc8dc7 doc update, bump version to v1.7.6 2025-04-16 23:43:45 +02:00
Rupert
1e3f4abe16 add support for setting rendering intent
new function: bmpwrite_set_rendering_intent()
2025-04-16 23:08:14 +02:00
Rupert
be9882e04f documentation update
- add documentation for ICC profile functions
- add 'c' to code snippets to enable syntax highlighting
- minor corrections
2025-04-16 13:11:37 +02:00
Rupert
3218cd6e65 small fix in RLE24 preconditions 2025-04-15 22:13:26 +02:00
Rupert
142427e179 add ICC profile writing support
new function bmpwrite_set_iccprofile()
2025-04-15 22:13:26 +02:00
Rupert
0580565bf3 add ICC profile reading support
- new functions:
 - bmpread_iccprofile_size()
 - bmpread_load_iccprofile()
- only pass on profile as is, profile is not interpreted or applied
2025-04-15 22:10:13 +02:00
Rupert
7523138fbb bump version to 1.7.5 2025-04-10 18:29:17 +02:00
Rupert
592605e06f add sanitize meson option 2025-04-10 18:29:17 +02:00
Rupert
eeae2205c5 eliminate all -Wextra warnings
revert height back to int and make INT_MIN an invalid height. Having
a possible height value that's only valid for top-down but not bottom-up
BMPs just creates headaches down the way.
2025-04-10 18:29:17 +02:00
Rupert
2d7763de7f refactor huffman bit-flipping for readability 2025-04-10 11:57:12 +02:00
Rupert
7513352a7b add bmp_set_huffman_t4black_value()
ITU-T T.4 defines 'black' and 'white' (referring to fore- and back-
ground, respectively) pixel sequences, but it doesn't prescribe which
of those is represented by 0 or 1. That would have to be defined by the
BMP specification, but documentation on Huffman BMPs is close to
non-existent.

Current consensus seems to be that 'black' is 1, i.e. indexing the
second color in the palette, and 'white' is 0, i.e. indexing the first
color.

In case that's wrong (in fact it's not even clear if there is a right
and a wrong), bmp_set_huffman_t4black_value() can be used to set the
pixel value of 'black' to either 0 or 1 (and white to the respective
opposite).

Can be used both for reading and writing BMPs.

Changing this value will invert the image colors.
2025-04-09 23:11:58 +02:00
Rupert
ea7b93ce64 read/write handles: move shared portion into struct Bmpcommon
make the generic handle a union of common/read/write
2025-04-09 18:40:06 +02:00
15 changed files with 1073 additions and 511 deletions

View File

@@ -1,4 +1,4 @@
# Rupert's bmplib -- Full API Description (v1.7.1)
# Rupert's bmplib -- Full API Description (v1.7.7)
Refer to the *Quick Start Guide* (API-quick-start.md) for a quick intro to bmplib which describes only the minimal set of functions needed to read/write BMP files.
@@ -6,7 +6,7 @@ Refer to the *Quick Start Guide* (API-quick-start.md) for a quick intro to bmpli
### Get a handle
```
```c
BMPHANDLE bmpread_new(FILE *file)
```
@@ -18,7 +18,7 @@ The handle cannot be reused to read multiple files.
### Read the file header
```
```c
BMPRESULT bmpread_load_info(BMPHANDLE h)
```
@@ -31,7 +31,7 @@ bmplib reads the file header and checks validity. Possible return values are:
`bmpread_set_insanity_limit()` to increase the allowed file size.
Otherwise, `bmpread_load_image()` will refuse to load the image. (You can
build the library with a different default limit, using the meson
option '-Dinsanity_limit_mb=nnn')
option `-Dinsanity_limit_mb=nnn`)
- `BMP_RESULT_PNG` / `BMP_RESULT_JPEG`: It's not really a BMP file, but a
wrapped PNG or JPEG. The file pointer is left in the correct state to be
passed on to e.g. libpng or libjpeg.
@@ -44,13 +44,13 @@ Calling `bmpread_load_info()` is optional when you use `bmpread_dimensions()`
### Get image dimensions
```
```c
BMPRESULT bmpread_dimensions(BMPHANDLE h,
int *width,
int *height,
int *channels,
int *bitsperchannel,
BMPORIENT *orientation)
BMPORIENT *orientation);
```
Use `bmpread_dimensions()` to get all dimensions with one call. It is not
@@ -68,15 +68,15 @@ Note, in order to use these functions, -- unlike with `bmpread_dimensions
()` -- you must first (successfully) call `bmpread_load_info()`, otherwise
they will all return 0!
```
int bmpread_width(BMPHANDLE h)
int bmpread_height(BMPHANDLE h)
int bmpread_channels(BMPHANDLE h)
int bmpread_bitsperchannel(BMPHANDLE h)
BMPORIENT bmpread_orientation(BMPHANDLE h)
```c
int bmpread_width(BMPHANDLE h);
int bmpread_height(BMPHANDLE h);
int bmpread_channels(BMPHANDLE h);
int bmpread_bitsperchannel(BMPHANDLE h);
BMPORIENT bmpread_orientation(BMPHANDLE h);
int bmpread_resolution_xdpi(BMPHANDLE h)
int bmpread_resolution_ydpi(BMPHANDLE h)
int bmpread_resolution_xdpi(BMPHANDLE h);
int bmpread_resolution_ydpi(BMPHANDLE h);
```
#### top-down / bottom-up
@@ -98,8 +98,8 @@ orientation of the original BMP.
#### Required size for buffer to receive image
```
size_t bmpread_buffersize(BMPHANDLE h)
```c
size_t bmpread_buffersize(BMPHANDLE h);
```
Returns the buffer size you have to allocate for the whole image.
@@ -112,9 +112,9 @@ image as 24-bit RGB data, same as non-indexed (RGB) BMPs.
If instead you want to keep the image as indexed, you have the option do so
with these two functions:
```
int bmpread_num_palette_colors(BMPHANDLE h)
BMPRESULT bmpread_load_palette(BMPHANDLE h, unsigned char **palette)
```c
int bmpread_num_palette_colors(BMPHANDLE h);
BMPRESULT bmpread_load_palette(BMPHANDLE h, unsigned char **palette);
```
`bmpread_num_palette_colors()` will return 0 for non-indexed images, otherwise
@@ -130,7 +130,7 @@ the palette buffer will contain "rgb0rgb0rgb0...".
As with the main image buffer, you can either provide one for the palette or
let bmplib allocate it for you (and then `free()` it, once you are done):
```
```c
unsigned char *palette;
int numcolors;
@@ -171,8 +171,8 @@ untouched by undefined pixels. (Note: if you let bmplib allocate the image
buffer, it will always be initialized to zero before loading the image). This
function has no effect on non-RLE BMPs.
```
void bmpread_set_undefined(BMPHANDLE h, BMPUNDEFINED mode)
```c
void bmpread_set_undefined(BMPHANDLE h, BMPUNDEFINED mode);
```
`mode` can be one of:
@@ -184,11 +184,37 @@ Note: If you use `bmpread_load_palette()` to switch to loading the index data
instead of RGB data, this setting will have no effect and undefined pixels
will always be left alone! (see above)
### ICC color profiles
```c
size_t bmpread_iccprofile_size(BMPHANDLE h);
BMPRESULT bmpread_load_iccprofile(BMPHANDLE h, unsigned char **pprofile);
```
Use `bmpread_iccprofile_size()` to query the size (or existence) of an
embedded color profile. If the BMP file doesn't contain a profile, the return
value is 0.
bmplib does not interpret or apply embedded ICC color profiles. The profile is
simply returned 'as is', image data is not afected in any way.
`bmpread_load_iccprofile()` loads the profile into the buffer pointed to by
`*pprofile`. As with loading image and palette data, you can either allocate
the buffer yourself or pass a pointer to a NULL-pointer and let bmplib
allocate an appropriate buffer, e.g.:
```c
unsigned char *profile = NULL;
if (bmpread_iccprofile_size(h) > 0)
bmpread_load_iccprofile(h, &profile);
```
### Optional settings for 64bit BMPs
```
int bmpread_is_64bit(BMPHANDLE h)
BMPRESULT bmpread_set_64bit_conv(BMPHANDLE h, BMPCONV64 conv)
```c
int bmpread_is_64bit(BMPHANDLE h);
BMPRESULT bmpread_set_64bit_conv(BMPHANDLE h, BMPCONV64 conv);
```
If you don't do anything, 64bit BMPs will be read like any other BMP and the
@@ -196,7 +222,11 @@ data will be returned as 16bit/channel sRGB RGBA.
But if you want to access the original s2.13 fixed-point components, or you
don't want the linear-to-sRGB conversion, you can use `bmpread_set_64bit_conv
()` and `bmp_set_number_format()` to control how the image is returned:
()` and `bmp_set_number_format()` to control how the image is returned.
64bit BMP pixel values are in the [-4...4) range, beyond the usual
[0...1]. Unless you load the image as `BMP_FORMAT_S2_13` or `BMP_FORMAT_FLOAT`,
the values will be clipped to [0...1].
Options for `bmpread_set_64bit()` are:
@@ -204,16 +234,18 @@ Options for `bmpread_set_64bit()` are:
fixed-point linear and converted to sRGB-gamma.
- `BMP_CONV64_LINEAR`: no gamma-conversion is applied to the image data.
- `BMP_CONV64_NONE`: this option is just a shorthand for setting
BMP_CONV64_LINEAR *and* BMP_FORMAT_S2_13. Image values are returned exactly
as they are in the BMP file, without any conversion or attempt at
interpretation.
`BMP_CONV64_LINEAR` *and also* calling `bmp_set_number_format()` with
`BMP_FORMAT_S2_13`. Image values are returned exactly as they are in the BMP
file, without any conversion or attempt at interpretation.
### Setting a number format
By default, bmplib will always return the image data as 8-,16-, or 32-bit integer values. You can instead set the number format to floating point or fixed using:
By default, bmplib will always return the image data as 8-,16-, or 32-bit
integer values. You can instead set the number format to floating point or
fixed using:
```
BMPRESULT bmp_set_number_format(BMPHANDLE h, BMPFORMAT format)
```c
BMPRESULT bmp_set_number_format(BMPHANDLE h, BMPFORMAT format);
```
(see below, *3. General functions for both reading/writing BMPs*)
@@ -221,21 +253,20 @@ BMPRESULT bmp_set_number_format(BMPHANDLE h, BMPFORMAT format)
### Huge files: bmpread_set_insanity_limit()
bmplib will refuse to load images beyond a certain size (default 500MB) and
instead return BMP_RESULT_INSANE. If you want to load the image anyway, call
`bmpread_set_insanity_limit()` at any time before calling `bmpread_load_image
()`. `limit` is the new allowed size in bytes. (not MB!)
instead return `BMP_RESULT_INSANE`. If you want to load the image anyway, call
`bmpread_set_insanity_limit()` at any time before calling `bmpread_load_image()`.
`limit` is the new allowed size in bytes (not MB!).
```
void
bmpread_set_insanity_limit(BMPHANDLE h, size_t limit)
```c
void bmpread_set_insanity_limit(BMPHANDLE h, size_t limit);
```
### Load the image
#### bmpread_load_image()
```
BMPRESULT bmpread_load_image(BMPHANDLE h, unsigned char **pbuffer)
```c
BMPRESULT bmpread_load_image(BMPHANDLE h, unsigned char **pbuffer);
```
Loads the complete image from the BMP file into the buffer pointed to by
@@ -247,7 +278,7 @@ with it.
If you allocate the buffer yourself, the buffer must be at least as large as
the size returned by `bmpread_buffersize()`.
```
```c
unsigned char *buffer;
/* either: */
@@ -265,14 +296,14 @@ order R-G-B or R-G-B-A. The returned image is always top-down, i.e. data
starts in the top left corner. Unlike BMPs which are (almost always)
bottom-up. (See above, "Getting information...")
If `bmpread_load_image()` returns BMP_RESULT_TRUNCATED or BMP_RESULT_INVALID,
If `bmpread_load_image()` returns `BMP_RESULT_TRUNCATED` or `BMP_RESULT_INVALID`,
the file may have been damaged or simply contains invalid image data. Image
data is loaded anyway as far as possible and may be partially usable.
#### bmpread_load_line()
```
BMPRESULT bmpread_load_line(BMPHANDLE h, unsigned char **pbuffer)
```c
BMPRESULT bmpread_load_line(BMPHANDLE h, unsigned char **pbuffer);
```
Loads a single scan line from the BMP file into the buffer pointed to by
@@ -286,7 +317,7 @@ To determine the required buffer size, either divide the value from
calculate from the image dimensions returned by bmplib as width * channels *
bitsperchannel / 8.
```
```c
single_line_buffersize = bmpread_buffersize(h) / bmpread_height(h);
/* or */
single_line_buffersize = bmpread_width(h) * bmpread_channels(h) * bmpread_bitsperchannel(h) / 8;
@@ -310,8 +341,8 @@ be set to the maximum allowed value, and attempts to point outside the image
will be ignored.
In both cases, `bmpread_load_image()` and `bmpread_load_line()` will return
BMP_RESULT_INVALID, unless the image is also truncated, then
BMP_RESULT_TRUNCATED is returned.
`BMP_RESULT_INVALID`, unless the image is also truncated, then
`BMP_RESULT_TRUNCATED` is returned.
### Query info about the BMP file
@@ -319,25 +350,25 @@ Note: these functions return information about the original BMP file being
read. They do *not* describe the format of the returned image data, which may
be different!
```
BMPINFOVER bmpread_info_header_version(BMPHANDLE h)
int bmpread_info_header_size(BMPHANDLE h)
int bmpread_info_compression(BMPHANDLE h)
int bmpread_info_bitcount(BMPHANDLE h)
const char* bmpread_info_header_name(BMPHANDLE h)
const char* bmpread_info_compression_name(BMPHANDLE h)
BMPRESULT bmpread_info_channel_bits(BMPHANDLE h, int *r, int *g, int *b, int *a)
```c
BMPINFOVER bmpread_info_header_version(BMPHANDLE h);
int bmpread_info_header_size(BMPHANDLE h);
int bmpread_info_compression(BMPHANDLE h);
int bmpread_info_bitcount(BMPHANDLE h);
const char* bmpread_info_header_name(BMPHANDLE h);
const char* bmpread_info_compression_name(BMPHANDLE h);
BMPRESULT bmpread_info_channel_bits(BMPHANDLE h, int *r, int *g, int *b, int *a);
```
### Release the handle
```
void bmp_free(BMPHANDLE h)
```c
void bmp_free(BMPHANDLE h);
```
Frees all resources associated with the handle `h`. **Image data is not
affected**, so you can call bmp_free() immediately after `bmpread_load_image
()` and still use the returned image data.
affected**, so you can call `bmp_free()` immediately after `bmpread_load_image()`
and still use the returned image data.
Note: Any error message strings returned by `bmp_errmsg()` are invalidated by
`bmp_free()` and must not be used anymore!
@@ -346,20 +377,20 @@ Note: Any error message strings returned by `bmp_errmsg()` are invalidated by
### Get a handle
```
BMPHANDLE bmpwrite_new(FILE *file)
```c
BMPHANDLE bmpwrite_new(FILE *file);
```
### Set image dimensions
```
```c
BMPRESULT bmpwrite_set_dimensions(BMPHANDLE h,
unsigned width,
unsigned height,
unsigned channels,
unsigned bitsperchannel)
unsigned bitsperchannel);
BMPRESULT bmpwrite_set_resolution(BMPHANDLE h, int xdpi, int ydpi)
BMPRESULT bmpwrite_set_resolution(BMPHANDLE h, int xdpi, int ydpi);
```
Note: the dimensions set with `bmpwrite_set_dimensions()` describe the source
@@ -374,15 +405,15 @@ choose appropriate bit-depths for your image. The bit-depth per channel can
be anywhere between 0 and 32, inclusive. In sum, the bits must be at least 1
and must not exceed 32.
```
BMPRESULT bmpwrite_set_output_bits(BMPHANDLE h, int red, int green, int blue, int alpha)
```c
BMPRESULT bmpwrite_set_output_bits(BMPHANDLE h, int red, int green, int blue, int alpha);
```
### Indexed images
```
BMPRESULT bmpwrite_set_palette(BMPHANDLE h, int numcolors, unsigned char *palette)
BMPRESULT bmpwrite_allow_2bit(BMPHANDLE h)
```c
BMPRESULT bmpwrite_set_palette(BMPHANDLE h, int numcolors, unsigned char *palette);
BMPRESULT bmpwrite_allow_2bit(BMPHANDLE h);
```
You can write 1/2/4/8-bit indexed images by providing a color palette with
@@ -404,13 +435,13 @@ BMP for 3- or 4-color images, call `bmpwrite_allow_2bit()` before calling
#### RLE
```
BMPRESULT bmpwrite_set_rle(BMPHANDLE h, BMPRLETYPE type)
BMPRESULT bmpwrite_allow_huffman(BMPHANDLE h)
BMPRESULT bmpwrite_set_huffman_img_fg_idx(BMPHANDLE h, int idx)
```c
BMPRESULT bmpwrite_set_rle(BMPHANDLE h, BMPRLETYPE type);
BMPRESULT bmpwrite_allow_huffman(BMPHANDLE h);
BMPRESULT bmpwrite_set_huffman_img_fg_idx(BMPHANDLE h, int idx);
```
Indexed images may optionally be written run-lenght-encoded (RLE) bitmaps.
Indexed images may optionally be written as run-length-encoded (RLE) bitmaps.
Images with 16 or fewer colors can be written as either RLE4 or RLE8
(default is RLE4), images with more than 16 colors only as RLE8.
@@ -440,8 +471,8 @@ Be aware that *very* few programs will be able to read Huffman encoded BMPs!
In order to get the best compression result, you should also call
`bmpwrite_set_huffman_img_fg_idx()` to specify which color index (0 or 1) in
the image corresponds to the foreground color. Huffman compression is
optimized for scanned text, meaning short runs of foreground color and long
(er) runs of background color. This will not change the appearance of the
optimized for scanned text, meaning short runs of foreground color and long(er)
runs of background color. This will not change the appearance of the
image, but setting it correctly will result in better compression.
@@ -464,8 +495,8 @@ usually orientated.
For non-RLE files, you have the option to change the orientation to top-down.
(RLE files always have to be written in the default bottom-up orientation.)
```
BMPRESULT bmpwrite_set_orientation(BMPHANDLE h, BMPORIENT orientation)
```c
BMPRESULT bmpwrite_set_orientation(BMPHANDLE h, BMPORIENT orientation);
```
with `orientation` set to one of the following values:
@@ -481,26 +512,58 @@ When writing the image line-by-line using `bmpwrite_save_line()`, you must
provide the image lines in the order according to the orientation you have
chosen for the BMP file.
### ICC color profiles
```c
BMPRESULT bmpwrite_set_iccprofile(BMPHANDLE h, size_t size,
const unsigned char *iccprofile);
BMPRESULT bmpwrite_set_rendering_intent(BMPHANDLE h, BMPINTENT intent);
```
Use `bmpwrite_set_iccprofile()` to write an embedded ICC color profile to the
BMP file.
bmplib will not interpret or validate the supplied profile in any way.
Setting a color profile or rendering intent will disable Huffman and RLE24 encodings.
(color profiles require a BITMAPV5HEADER, but those encodings would require
an older OS/2 info header, instead.)
You can optionally specify a rendering intent with `bmpwrite_set_rendering_intent()`,
where `intent` is one of:
- `BMP_INTENT_NONE`
- `BMP_INTENT_BUSINESS` (= saturation)
- `BMP_INTENT_GRAPHICS` (= relative colorimetric)
- `BMP_INTENT_IMAGES` (= perceptive)
- `BMP_INTENT_ABS_COLORIMETRIC` (= absolute colorimetric)
### 64-bit RGBA BMPs
By default, bmplib will not write 64-bit BMPs because they are rather exotic and hardly any
software can open them.
By default, bmplib will not write 64-bit BMPs because they are rather exotic
and hardly any software can open them.
If you do want to write 64-bit BMPs, call
```
BMPRESULT bmpwrite_set_64bit(BMPHANDLE h)
```c
BMPRESULT bmpwrite_set_64bit(BMPHANDLE h);
```
In order to make use of the extended range available in 64-bit BMPs (-4.0 to +3.999...), you will probably want to provide the image buffer either as 32-bit float or as 16-bit s2.13 (and call `bmp_set_number_format()` accordingly).
In order to make use of the extended range available in 64-bit BMPs
(-4.0 to +3.999...), you will probably want to provide the image buffer
either as 32-bit float or as 16-bit s2.13 (and call `bmp_set_number_format()`
accordingly).
Note: 64-bit BMPs store pixel values in *linear light*. Unlike when *reading* 64-bit BMPs, bmplib will not make any gamma/linear conversion while writing BMPs. You have to provide the proper linear values in the image buffer.
Note: 64-bit BMPs store pixel values in *linear light*. Unlike when *reading*
64-bit BMPs, bmplib will not make any gamma/linear conversion while writing
BMPs. You have to provide the proper linear values in the image buffer.
### Write the image
```
BMPRESULT bmpwrite_save_image(BMPHANDLE h, const unsigned char *image)
BMPRESULT bmpwrite_save_line(BMPHANDLE h, const unsigned char *line)
```c
BMPRESULT bmpwrite_save_image(BMPHANDLE h, const unsigned char *image);
BMPRESULT bmpwrite_save_line(BMPHANDLE h, const unsigned char *line);
```
Write either the whole image at once with `bmpwrite_save_image()` or one line
@@ -512,8 +575,8 @@ in host byte order, the channels in the order R-G-B-(A). Indexed data must be
supplied as 8 bit per pixel, even when writing lower bit (1/2/4) BMPs
(see above).
Important: When writing the whole image at once using `bmpwrite_save_image
()`, the image data must be provided top-down (same as is returned by
Important: When writing the whole image at once using `bmpwrite_save_image()`,
the image data must be provided top-down (same as is returned by
`bmpread_load_image()`). When using `bmpwrite_save_line()` to write the image
line-by-line, the image data must be provided according to the orientation
set with `bmpwrite_set_orientation()` (see above).
@@ -522,55 +585,94 @@ set with `bmpwrite_set_orientation()` (see above).
### bmp_free()
```
void bmp_free(BMPHANDLE h)
```c
void bmp_free(BMPHANDLE h);
```
Frees all resources associated with the handle `h`. Image data is not
affected, so you can call bmp_free() immediately after bmpread_load_image
() and still use the returned image data. Note: Any error messages returned
affected, so you can call `bmp_free()` immediately after `bmpread_load_image()`
and still use the returned image data. Note: Any error messages returned
by `bmp_errmsg()` are invalidated by `bmp_free()` and cannot be used
anymore.
anymore!
### bmp_errmsg()
```
const char* bmp_errmsg(BMPHANDLE h)
```c
const char* bmp_errmsg(BMPHANDLE h);
```
Returns a zero-terminated character string containing the last error
description(s). The returned string is safe to use until any other
bmplib-function is called with the same handle.
bmplib-function is called with the same handle or the handle is freed with
`bmp_free()`.
### bmp_set_number_format()
```
BMPRESULT bmp_set_number_format(BMPHANDLE h, BMPFORMAT format)
```c
BMPRESULT bmp_set_number_format(BMPHANDLE h, BMPFORMAT format);
```
sets the number format of the image buffer received from / passed to bmplib. `format` can be one of
- `BMP_FORMAT_INT` image buffer values are expected/returned as 8-, 16-, or 32-bit integers. (this is the default)
- `BMP_FORMAT_INT` image buffer values are expected/returned as 8-, 16-, or
32-bit integers. (this is the default)
- `BMP_FORMAT_FLOAT` image buffer values are expected/returned as 32-bit floating point numbers (C `float`).
- `BMP_FORMAT_S2_13` image buffer values are expected/returned as s2.13 fixed point numbers. s2.13 is a 16-bit format with one sign bit, 2 integer bits, and 13 bits for the fractional part. Range is from -4.0 to +3.999...
- `BMP_FORMAT_S2_13` image buffer values are expected/returned as s2.13 fixed
point numbers. s2.13 is a 16-bit format with one sign bit, 2 integer bits,
and 13 bits for the fractional part. Range is from -4.0 to +3.999...
For indexed images, `BMP_FORMAT_INT` is the only valid format.
### bmp_version()
```
const char* bmp_version(void)
```c
const char* bmp_version(void);
```
Returns a zero-terminated character string containing the version of bmplib.
### bmp_set_huffman_t4black_value()
```c
BMPRESULT bmp_set_huffman_t4black_value(BMPHANDLE h, int blackidx);
```
(not to be confused with `bmpwrite_set_huffman_img_fg_idx()`, which serves an
entirely different purpose, see above.)
ITU-T T.4 defines 'black' and 'white' pixel sequences (referring to fore- and
background, respectively), but it doesn't prescribe which of those is
represented by 0 or 1. That would have to be defined by a BMP specification,
but documentation on Huffman BMPs is close to non-existent.
Current consensus seems to be that 'black' is 1, i.e. indexing the second
color in the palette, and 'white' is 0, i.e. indexing the first color. This
is the default for bmplib.
In case that's wrong (in fact it's not even clear if there is a right and a
wrong), `bmp_set_huffman_t4black_value()` can be used to set the pixel value
of 'black' to either 0 or 1 (and white to the respective opposite).
Can be used both for reading and writing BMPs.
Changing this value will invert the image colors!
Reasons to use this function:
- You know that bmplib's default assumption of 'black'=1 is wrong, and you
want to set it to 0. (In that case, please also drop a note on github.)
- You don't care either way, but you want to be sure to get consistent
behaviour, in case bmplib's default is ever changed in light of new
information/documentation.
- You need to interface with other software that you know assumes 'black'=0.
## 4. Data types and constants
#### `BMPHANDLE`
Returned by `bmpread_new()` and `bmpwrite_new()`.
Identifies the current operation for all subsequent
calls to bmplib-functions.
Returned by `bmpread_new()` and `bmpwrite_new()`. Identifies the current
operation for all subsequent calls to bmplib-functions.
#### `BMPRESULT`
@@ -603,7 +705,7 @@ else {
Returned by `bmpread_info_header_version()`. Possible values are:
- `BMPINFO_CORE_OS21` BITMAPCOREHEADER aka OS21XBITMAPHEADER (12 bytes)
- `BMPINFO_OS22` OS22XBITMAPHEADER (16/40/64 bytes)
- `BMPINFO_OS22` OS22XBITMAPHEADER (16-64 bytes)
- `BMPINFO_V3` BITMAPINFOHEADER (40 bytes)
- `BMPINFO_V3_ADOBE1` BITMAPINFOHEADER with additional RGB masks (52 bytes)
- `BMPINFO_V3_ADOBE2` BITMAPINFOHEADER with additional RGBA masks (56 bytes)
@@ -629,7 +731,7 @@ Can safely be cast from/to int.
Used in `bmpread_set_undefined()`. Possible values are:
- `BMP_UNDEFINED_TO_ALPHA` (default)
- `BMP_UNDEFINED_TO_ZERO`
- `BMP_UNDEFINED_LEAVE`
Can safely be cast from/to int.
@@ -655,7 +757,7 @@ Used in `bmp_set_number_format()`. Possible values are:
### Reading BMPs
```
```c
/* (all error checking left out for clarity) */
BMPHANDLE h;
@@ -707,7 +809,7 @@ Used in `bmp_set_number_format()`. Possible values are:
### Writing BMPs
```
```c
/* (all error checking left out for clarity) */
BMPHANDLE h;

View File

@@ -12,15 +12,16 @@ For the complete API, refer to the *Full API Description* (API-full.md).
## 1. Reading BMP files:
```
bmpread_new()
bmpread_dimensions()
bmpread_load_image()
bmp_free()
```c
bmpread_new();
bmpread_dimensions();
bmpread_load_image();
bmp_free();
```
### Get a handle
```
```c
BMPHANDLE bmpread_new(FILE *file)
```
@@ -32,13 +33,13 @@ The handle cannot be reused to read multiple files.
### Get image dimensions
```
```c
BMPRESULT bmpread_dimensions(BMPHANDLE h,
int *width,
int *height,
int *channels,
int *bitsperchannel,
BMPORIENT *orientation)
BMPORIENT *orientation);
```
Use `bmpread_dimensions()` to get all dimensions with one call. The return
@@ -54,8 +55,8 @@ line-by-line. Can be set to NULL. (see *Full API Description*)
### Load the image
```
BMPRESULT bmpread_load_image(BMPHANDLE h, unsigned char **pbuffer)
```c
BMPRESULT bmpread_load_image(BMPHANDLE h, unsigned char **pbuffer);
```
Loads the complete image from the BMP file into the buffer pointed to by
@@ -67,7 +68,7 @@ with it.
If you allocate the buffer yourself, the buffer must be at least as large as
the size returned by `bmpread_buffersize()` (see *Full API description*).
```
```c
unsigned char *buffer;
/* either: */
@@ -86,7 +87,7 @@ in the order R-G-B or R-G-B-A. The returned image is always top-down, i.e.
data starts in the top left corner. Unlike BMPs which are (almost always)
bottom-up.
If `bmpread_load_image()` returns BMP_RESULT_TRUNCATED or BMP_RESULT_INVALID,
If `bmpread_load_image()` returns `BMP_RESULT_TRUNCATED` or `BMP_RESULT_INVALID`,
the file may have been damaged or simply contains invalid image data. Image
data is loaded anyway as far as possible and may be partially usable.
@@ -94,13 +95,13 @@ data is loaded anyway as far as possible and may be partially usable.
### Release the handle
```
void bmp_free(BMPHANDLE h)
```c
void bmp_free(BMPHANDLE h);
```
Frees all resources associated with the handle `h`. **Image data is not
affected**, so you can call bmp_free() immediately after `bmpread_load_image
()` and still use the returned image data.
affected**, so you can call `bmp_free()` immediately after `bmpread_load_image()`
and still use the returned image data.
Note: Any error message strings returned by `bmp_errmsg()` are invalidated by
`bmp_free()` and must not be used anymore!
@@ -109,25 +110,27 @@ Note: Any error message strings returned by `bmp_errmsg()` are invalidated by
## 2. Writing BMP files:
```
bmpwrite_new()
bmpwrite_set_dimensions()
bmpwrite_save_image()
bmp_free()
```c
bmpwrite_new();
bmpwrite_set_dimensions();
bmpwrite_save_image();
bmp_free();
```
### Get a handle
```
BMPHANDLE bmpwrite_new(FILE *file)
```c
BMPHANDLE bmpwrite_new(FILE *file);
```
### Set image dimensions
```
```c
BMPRESULT bmpwrite_set_dimensions(BMPHANDLE h,
unsigned width,
unsigned height,
unsigned channels,
unsigned bitsperchannel)
unsigned bitsperchannel);
```
Note: the dimensions set with `bmpwrite_set_dimensions()` describe the source
@@ -140,8 +143,8 @@ API description*)
### Write the image
```
BMPRESULT bmpwrite_save_image(BMPHANDLE h, const unsigned char *image)
```c
BMPRESULT bmpwrite_save_image(BMPHANDLE h, const unsigned char *image);
```
Write the whole image at once with `bmpwrite_save_image()`.
@@ -155,8 +158,8 @@ file will be bottom-up.)
### bmp_free()
```
void bmp_free(BMPHANDLE h)
```c
void bmp_free(BMPHANDLE h);
```
Frees all resources associated with the handle `h`.
@@ -182,7 +185,7 @@ Many bmplib functions return the success/failure of an operation as a
- `BMP_RESULT_JPEG`
- `BMP_RESULT_ERROR`
Can safely be cast from/to int. BMP_RESULT_OK is guaranteed to have the value 0.
Can safely be cast from/to int. `BMP_RESULT_OK` is guaranteed to have the value 0.
@@ -190,7 +193,7 @@ Can safely be cast from/to int. BMP_RESULT_OK is guaranteed to have the value 0.
### Reading BMPs
```
```c
/* (all error checking left out for clarity) */
BMPHANDLE h;
@@ -236,7 +239,7 @@ Can safely be cast from/to int. BMP_RESULT_OK is guaranteed to have the value 0.
### Writing BMPs
```
```c
/* (all error checking left out for clarity) */
BMPHANDLE h;

View File

@@ -8,7 +8,7 @@
Download [bmplib on github](https://github.com/rupertwh/bmplib).
## Current status (v1.7.1):
## Current status (v1.7.7):
### Reading BMP files:
- 16/24/32 bit RGB(A) with any bits/channel combination
(BI_RGB, BI_BITFIELDS, BI_ALPHABITFIELDS).
@@ -25,7 +25,7 @@ Download [bmplib on github](https://github.com/rupertwh/bmplib).
- most 'questionable' files (see below)
- some 'bad' files
Questionable files that failed:
Questionable files that fail:
- embedded JPEG and PNG. Not really a fail. We return BMP_RESULT_JPEG or
BMP_RESULT_PNG and leave the file pointer in the correct state to be
passed on to either libpng or libjpeg. Works as designed. Don't want to
@@ -125,8 +125,10 @@ conversion:
- `bmpread_set_64bit_conv()`
- `bmp_set_number_format()`
As to writing BMPs, by default bmplib will not write 64bit BMPs, as they are so exotic that only few applications will read them (other than native Microsoft tools, the new GIMP 3.0 is the only one I am aware of).
Use `bmpwrite_set_64bit()` in order to write 64bit BMPs.
As to writing BMPs, by default bmplib will not write 64bit BMPs, as they are
so exotic that only few applications will read them (other than native
Microsoft tools, the new GIMP 3.0 is the only one I am aware of). Use
`bmpwrite_set_64bit()` in order to write 64bit BMPs.
## TODOs:
@@ -138,7 +140,7 @@ Use `bmpwrite_set_64bit()` in order to write 64bit BMPs.
- [x] read Huffman-encoded BMPs. (Still haven't found any real-life examples)
- [x] line-by-line reading/writing. ~~Right now, the image can only be
passed as a whole to/from bmplib.~~
- [ ] read/write icc-profile and chromaticity/gamma values
- [x] read/write icc-profile and chromaticity/gamma values
- [x] sanity checks for size of of image / palette. Require confirmation
above a certain size (~ 500MB?)
- [x] store undefined pixels (RLE delta and early EOL/EOF) as alpha

View File

@@ -36,13 +36,6 @@
#include "bmp-write.h"
struct Bmphandle {
struct {
uint32_t magic;
LOG log;
};
};
/********************************************************
@@ -62,10 +55,10 @@ API const char* bmp_version(void)
API const char* bmp_errmsg(BMPHANDLE h)
{
if (!(h && (h->magic == HMAGIC_READ || h->magic == HMAGIC_WRITE)))
if (!(h && (h->common.magic == HMAGIC_READ || h->common.magic == HMAGIC_WRITE)))
return "BMPHANDLE is NULL or invalid";
return logmsg(h->log);
return logmsg(h->common.log);
}
@@ -79,17 +72,17 @@ API BMPRESULT bmp_set_number_format(BMPHANDLE h, enum BmpFormat format)
if (!h)
return BMP_RESULT_ERROR;
switch (h->magic) {
switch (h->common.magic) {
case HMAGIC_READ:
return br_set_number_format((BMPREAD)(void*)h, format);
return br_set_number_format(&h->read, format);
case HMAGIC_WRITE:
return bw_set_number_format((BMPWRITE)(void*)h, format);
return bw_set_number_format(&h->write, format);
default:
#ifdef DEBUG
printf("bmp_set_number_format() called with invalid handle (0x%04x)\n",
(unsigned int) h->magic);
(unsigned int) h->common.magic);
#endif
break;
}
@@ -98,6 +91,25 @@ API BMPRESULT bmp_set_number_format(BMPHANDLE h, enum BmpFormat format)
/********************************************************
* bmp_set_huffman_t4black_value
*******************************************************/
API BMPRESULT bmp_set_huffman_t4black_value(BMPHANDLE h, int blackidx)
{
if (!h)
return BMP_RESULT_ERROR;
if (!(h->common.magic == HMAGIC_READ || h->common.magic == HMAGIC_WRITE))
return BMP_RESULT_ERROR;
h->common.huffman_black_is_zero = !blackidx;
return BMP_RESULT_OK;
}
/********************************************************
* bmp_free
*******************************************************/
@@ -107,18 +119,18 @@ API void bmp_free(BMPHANDLE h)
if (!h)
return;
switch (h->magic) {
switch (h->common.magic) {
case HMAGIC_READ:
br_free((BMPREAD)(void*)h);
br_free(&h->read);
break;
case HMAGIC_WRITE:
bw_free((BMPWRITE)(void*)h);
bw_free(&h->write);
break;
default:
#ifdef DEBUG
printf("bmp_free() called with invalid handle (0x%04x)\n",
(unsigned int) h->magic);
(unsigned int) h->common.magic);
#endif
break;
}
@@ -132,10 +144,8 @@ API void bmp_free(BMPHANDLE h)
BMPREAD cm_read_handle(BMPHANDLE h)
{
BMPREAD rp = (BMPREAD)(void*)h;
if (rp && rp->magic == HMAGIC_READ)
return rp;
if (h && h->common.magic == HMAGIC_READ)
return &h->read;
return NULL;
}
@@ -147,10 +157,8 @@ BMPREAD cm_read_handle(BMPHANDLE h)
BMPWRITE cm_write_handle(BMPHANDLE h)
{
BMPWRITE wp = (BMPWRITE)(void*)h;
if (wp && wp->magic == HMAGIC_WRITE)
return wp;
if (h && h->common.magic == HMAGIC_WRITE)
return &h->write;
return NULL;
}
@@ -168,10 +176,10 @@ bool cm_gobble_up(BMPREAD_R rp, int count)
if (EOF == getc(rp->file)) {
if (feof(rp->file)) {
rp->lasterr = BMP_ERR_TRUNCATED;
logerr(rp->log, "unexpected end of file");
logerr(rp->c.log, "unexpected end of file");
} else {
rp->lasterr = BMP_ERR_FILEIO;
logsyserr(rp->log, "error reading from file");
logsyserr(rp->c.log, "error reading from file");
}
return false;
}
@@ -433,3 +441,27 @@ int16_t s16_from_le(const unsigned char *buf)
{
return (int16_t)u16_from_le(buf);
}
/*****************************************************************************
* cm_infoheader_name
*****************************************************************************/
const char* cm_infoheader_name(enum BmpInfoVer infoversion)
{
switch (infoversion) {
case BMPINFO_CORE_OS21 : return "OS21XBITMAPHEADER";
case BMPINFO_OS22 : return "OS22XBITMAPHEADER";
case BMPINFO_V3 : return "BITMAPINFOHEADER";
case BMPINFO_V3_ADOBE1 : return "BITMAPINFOHEADER + RGB mask";
case BMPINFO_V3_ADOBE2 : return "BITMAPINFOHEADER + RGBA mask";
case BMPINFO_V4 : return "BITMAPV4HEADER";
case BMPINFO_V5 : return "BITMAPV5HEADER";
case BMPINFO_FUTURE : return "unknown future version";
default:
return "invalid infoheader version";
}
}

View File

@@ -82,28 +82,29 @@ struct Colormask {
} maxval;
};
typedef struct Bmpread *BMPREAD;
typedef struct Bmpwrite *BMPWRITE;
typedef struct Bmpread *restrict BMPREAD_R;
typedef struct Bmpwrite *restrict BMPWRITE_R;
struct Palette {
int numcolors;
union Pixel color[1];
};
struct Bmpcommon {
uint32_t magic;
LOG log;
bool huffman_black_is_zero; /* defaults to false */
};
struct Bmpread {
struct {
uint32_t magic;
LOG log;
};
struct Bmpcommon c;
FILE *file;
size_t bytes_read; /* number of bytes we have read from the file */
struct Bmpfile *fh;
struct Bmpinfo *ih;
unsigned int insanity_limit;
int width;
unsigned height;
int height;
enum BmpOrient orientation;
bool has_alpha; /* original BMP has alpha channel */
enum BmpUndefined undefined_mode;
@@ -153,11 +154,16 @@ struct Bmpread {
};
enum WriteState {
WS_INIT,
WS_DIMENSIONS_SET,
WS_SAVE_STARTED,
WS_SAVE_DONE,
WS_FATAL,
};
struct Bmpwrite {
struct {
uint32_t magic;
LOG log;
};
struct Bmpcommon c;
FILE *file;
struct Bmpfile *fh;
struct Bmpinfo *ih;
@@ -167,37 +173,49 @@ struct Bmpwrite {
int source_channels;
int source_bitsperchannel;
int source_bytes_per_pixel;
int source_format;
enum BmpFormat source_format;
bool source_has_alpha;
struct Palette *palette;
int palette_size; /* sizeof palette in bytes */
unsigned char *iccprofile;
int iccprofile_size;
/* output */
size_t bytes_written;
uint64_t bytes_written;
size_t bytes_written_before_bitdata;
bool has_alpha;
enum BmpOrient outorientation;
bool huffman_fg_idx;
struct Colormask cmask;
enum BmpRLEtype rle_requested;
int rle; /* 1, 4, or 8 */
bool allow_2bit; /* Windows CE, but many will not read it */
bool allow_huffman;
bool allow_rle24;
int rle; /* 1, 4, 8, or 24 */
bool allow_2bit; /* Windows CE */
bool allow_huffman; /* OS/2 */
bool allow_rle24; /* OS/2 */
bool out64bit;
int outbytes_per_pixel;
int padding;
int *group;
int group_count;
/* state */
enum WriteState write_state;
bool outbits_set;
bool dimensions_set;
bool saveimage_done;
bool line_by_line;
int lbl_y;
uint32_t hufbuf;
int hufbuf_len;
};
union Bmphandle {
struct Bmpcommon common;
struct Bmpread read;
struct Bmpwrite write;
};
typedef struct Bmpread *BMPREAD;
typedef struct Bmpwrite *BMPWRITE;
typedef struct Bmpread *restrict BMPREAD_R;
typedef struct Bmpwrite *restrict BMPWRITE_R;
bool cm_all_lessoreq_int(int limit, int n, ...);
bool cm_all_equal_int(int n, ...);
@@ -232,6 +250,8 @@ int32_t s32_from_le(const unsigned char *buf);
uint16_t u16_from_le(const unsigned char *buf);
int16_t s16_from_le(const unsigned char *buf);
const char* cm_infoheader_name(enum BmpInfoVer infoversion);
#define HMAGIC_READ 0x44414552UL
#define HMAGIC_WRITE 0x54495257UL
@@ -248,6 +268,7 @@ int16_t s16_from_le(const unsigned char *buf);
#define BMPIHSIZE_V3 40
#define BMPIHSIZE_V4 108
#define BMPIHSIZE_OS22 64
#define BMPIHSIZE_V5 124
struct Bmpfile {
uint16_t type; /* "BM" */
@@ -307,7 +328,9 @@ struct Bmpinfo {
enum BmpInfoVer version;
};
#define IH_PROFILEDATA_OFFSET (14L + 112L)
#define MAX_ICCPROFILE_SIZE (1UL << 20)
#define BI_RGB 0
@@ -334,3 +357,9 @@ struct Bmpinfo {
#define LCS_WINDOWS_COLOR_SPACE 0x57696e20 /* 'Win ' */
#define PROFILE_LINKED 0x4c494e4b /* 'LINK' */
#define PROFILE_EMBEDDED 0x4d424544 /* 'MBED' */
#define LCS_GM_BUSINESS 1
#define LCS_GM_GRAPHICS 2
#define LCS_GM_IMAGES 4
#define LCS_GM_ABS_COLORIMETRIC 8

View File

@@ -105,7 +105,7 @@ API BMPRESULT bmpread_load_line(BMPHANDLE h, unsigned char **restrict buffer)
if (!(rp = cm_read_handle(h)))
return BMP_RESULT_ERROR;
logreset(rp->log); /* otherwise we might accumulate thousands */
logreset(rp->c.log); /* otherwise we might accumulate thousands */
/* of log entries with large corrupt images */
return s_load_image_or_line(rp, buffer, true);
@@ -126,31 +126,31 @@ static BMPRESULT s_load_image_or_line(BMPREAD_R rp, unsigned char **restrict buf
if (!(rp->getinfo_called && (rp->getinfo_return == BMP_RESULT_OK))) {
if (rp->getinfo_return == BMP_RESULT_INSANE) {
logerr(rp->log, "trying to load insanley large image");
logerr(rp->c.log, "trying to load insanley large image");
return BMP_RESULT_INSANE;
}
logerr(rp->log, "getinfo had failed, cannot load image");
logerr(rp->c.log, "getinfo had failed, cannot load image");
return BMP_RESULT_ERROR;
}
if (rp->image_loaded) {
logerr(rp->log, "Cannot load image more than once!");
logerr(rp->c.log, "Cannot load image more than once!");
return BMP_RESULT_ERROR;
}
if (rp->line_by_line && !line_by_line) {
logerr(rp->log, "Image is being loaded line-by-line. "
logerr(rp->c.log, "Image is being loaded line-by-line. "
"Cannot switch to full image.");
return BMP_RESULT_ERROR;
}
if (!rp->dimensions_queried) {
logerr(rp->log, "must query dimensions before loading image");
logerr(rp->c.log, "must query dimensions before loading image");
return BMP_RESULT_ERROR;
}
if (!buffer) {
logerr(rp->log, "buffer pointer is NULL");
logerr(rp->c.log, "buffer pointer is NULL");
return BMP_RESULT_ERROR;
}
@@ -160,7 +160,7 @@ static BMPRESULT s_load_image_or_line(BMPREAD_R rp, unsigned char **restrict buf
buffer_size = rp->result_size;
if (!*buffer) { /* no buffer supplied, we will allocate one */
if (!(*buffer = malloc(buffer_size))) {
logsyserr(rp->log, "allocating result buffer");
logsyserr(rp->c.log, "allocating result buffer");
return BMP_RESULT_ERROR;
}
rp->we_allocated_buffer = true;
@@ -176,12 +176,12 @@ static BMPRESULT s_load_image_or_line(BMPREAD_R rp, unsigned char **restrict buf
if (!rp->line_by_line) { /* either whole image or first line */
if (rp->bytes_read > rp->fh->offbits) {
logerr(rp->log, "Corrupt file");
logerr(rp->c.log, "Corrupt file");
goto abort;
}
/* skip to actual bitmap data: */
if (!cm_gobble_up(rp, rp->fh->offbits - rp->bytes_read)) {
logerr(rp->log, "while seeking start of bitmap data");
logerr(rp->c.log, "while seeking start of bitmap data");
goto abort;
}
rp->bytes_read += rp->fh->offbits - rp->bytes_read;
@@ -227,7 +227,7 @@ static void s_read_whole_image(BMPREAD_R rp, unsigned char *restrict image)
linesize = (size_t) rp->width * rp->result_bytes_per_pixel;
for (y = 0; y < (int) rp->height; y += yoff) {
for (y = 0; y < rp->height; y += yoff) {
real_y = (rp->orientation == BMP_ORIENT_TOPDOWN) ? y : rp->height-1-y;
s_read_one_line(rp, image + real_y * linesize);
if (rp->rle_eof || s_stopping_error(rp))
@@ -264,7 +264,7 @@ static void s_read_one_line(BMPREAD_R rp, unsigned char *restrict line)
}
if (!(rp->rle_eof || s_stopping_error(rp))) {
if (yoff > (int) rp->height - rp->lbl_file_y) {
if (yoff > rp->height - rp->lbl_file_y) {
rp->invalid_delta = true;
}
rp->lbl_file_y += yoff;
@@ -278,7 +278,7 @@ static void s_read_one_line(BMPREAD_R rp, unsigned char *restrict line)
}
rp->lbl_y++;
if (rp->lbl_y >= (int) rp->height) {
if (rp->lbl_y >= rp->height) {
rp->image_loaded = true;
}
}
@@ -330,7 +330,7 @@ static void s_read_rgb_line(BMPREAD_R rp, unsigned char *restrict line)
((uint32_t*)line)[offs + i] = pxval;
break;
default:
logerr(rp->log, "Waaaaaaaaaaaaaah!");
logerr(rp->c.log, "Waaaaaaaaaaaaaah!");
rp->panic = true;
return;
}
@@ -383,7 +383,7 @@ static void s_read_rgb_line(BMPREAD_R rp, unsigned char *restrict line)
break;
default:
logerr(rp->log, "Unknown format");
logerr(rp->c.log, "Unknown format");
rp->panic = true;
return;
}
@@ -747,7 +747,7 @@ static void s_read_rle_line(BMPREAD_R rp, unsigned char *restrict line,
continue;
}
logerr(rp->log, "Should never get here! (x=%d, byte=%d)", (int) *x, (int) v);
logerr(rp->c.log, "Should never get here! (x=%d, byte=%d)", (int) *x, (int) v);
rp->panic = true;
break;
}
@@ -800,11 +800,11 @@ static void s_read_huffman_line(BMPREAD_R rp, unsigned char *restrict line)
for (int i = 0; i < runlen; i++, x++) {
offs = (size_t) x * rp->result_bytes_per_pixel;
if (rp->result_indexed) {
line[offs] = black;
line[offs] = black ^ rp->c.huffman_black_is_zero;
} else {
line[offs] = rp->palette->color[black].red;
line[offs+1] = rp->palette->color[black].green;
line[offs+2] = rp->palette->color[black].blue;
line[offs] = rp->palette->color[black ^ rp->c.huffman_black_is_zero].red;
line[offs+1] = rp->palette->color[black ^ rp->c.huffman_black_is_zero].green;
line[offs+2] = rp->palette->color[black ^ rp->c.huffman_black_is_zero].blue;
s_int_to_result_format(rp, 8, line + offs);
}
}
@@ -907,7 +907,7 @@ static inline void s_int_to_result_format(BMPREAD_R rp, int frombits, unsigned c
break;
default:
#ifdef DEBUG
logerr(rp->log, "Unexpected result format %d", rp->result_format);
logerr(rp->c.log, "Unexpected result format %d", rp->result_format);
exit(1);
#endif
break;
@@ -938,19 +938,19 @@ static void s_set_file_error(BMPREAD_R rp)
static void s_log_error_from_state(BMPREAD_R rp)
{
if (rp->panic)
logerr(rp->log, "An internal error occured.");
logerr(rp->c.log, "An internal error occured.");
if (rp->file_eof)
logerr(rp->log, "Unexpected end of file.");
logerr(rp->c.log, "Unexpected end of file.");
if (rp->file_err)
logsyserr(rp->log, "While reading file");
logsyserr(rp->c.log, "While reading file");
if (rp->invalid_index)
logerr(rp->log, "File contained invalid color index.");
logerr(rp->c.log, "File contained invalid color index.");
if (rp->invalid_delta)
logerr(rp->log, "Invalid delta pointing outside image area.");
logerr(rp->c.log, "Invalid delta pointing outside image area.");
if (rp->invalid_overrun)
logerr(rp->log, "RLE data overrunning image area.");
logerr(rp->c.log, "RLE data overrunning image area.");
if (rp->truncated)
logerr(rp->log, "Image was truncated.");
logerr(rp->c.log, "Image was truncated.");
}

View File

@@ -78,23 +78,23 @@ API BMPRESULT bmpread_load_palette(BMPHANDLE h, unsigned char **palette)
return BMP_RESULT_ERROR;
if (!rp->getinfo_called) {
logerr(rp->log, "Must call bmpread_load_info() before loading palette");
logerr(rp->c.log, "Must call bmpread_load_info() before loading palette");
return BMP_RESULT_ERROR;
}
if (!rp->palette) {
logerr(rp->log, "Image has no palette");
logerr(rp->c.log, "Image has no palette");
return BMP_RESULT_ERROR;
}
if (!palette) {
logerr(rp->log, "palette is NULL");
logerr(rp->c.log, "palette is NULL");
return BMP_RESULT_ERROR;
}
memsize = rp->palette->numcolors * 4;
if (!*palette) {
if (!(*palette = malloc(memsize))) {
logsyserr(rp->log, "allocating palette");
logsyserr(rp->c.log, "allocating palette");
return BMP_RESULT_ERROR;
}
}

View File

@@ -35,7 +35,6 @@
#include "bmp-read.h"
const char* s_infoheader_name(int infoversion);
const char* s_compression_name(int compression);
@@ -53,13 +52,13 @@ API BMPHANDLE bmpread_new(FILE *file)
}
memset(rp, 0, sizeof *rp);
rp->magic = HMAGIC_READ;
rp->c.magic = HMAGIC_READ;
rp->undefined_mode = BMP_UNDEFINED_TO_ALPHA;
rp->orientation = BMP_ORIENT_BOTTOMUP;
rp->conv64 = BMP_CONV64_SRGB;
rp->result_format = BMP_FORMAT_INT;
if (!(rp->log = logcreate()))
if (!(rp->c.log = logcreate()))
goto abort;
if (!file)
@@ -119,12 +118,12 @@ API BMPRESULT bmpread_load_info(BMPHANDLE h)
case BMPFILE_IC:
case BMPFILE_PT:
case BMPFILE_BA:
logerr(rp->log, "Bitmap array and icon/pointer files not supported");
logerr(rp->c.log, "Bitmap array and icon/pointer files not supported");
rp->lasterr = BMP_ERR_UNSUPPORTED;
goto abort;
default:
logerr(rp->log, "Unkown BMP type 0x%04x\n", (unsigned int) rp->fh->type);
logerr(rp->c.log, "Unkown BMP type 0x%04x\n", (unsigned int) rp->fh->type);
rp->lasterr = BMP_ERR_UNSUPPORTED;
goto abort;
}
@@ -136,10 +135,15 @@ API BMPRESULT bmpread_load_info(BMPHANDLE h)
/* negative height flips the image vertically */
if (rp->ih->height < 0) {
if (rp->ih->height == INT_MIN) {
logerr(rp->c.log, "Unsupported image height %ld\n", (long) rp->ih->height);
rp->lasterr = BMP_ERR_UNSUPPORTED;
goto abort;
}
rp->orientation = BMP_ORIENT_TOPDOWN;
rp->height = (unsigned) (-(int64_t)rp->ih->height);
rp->height = -rp->ih->height;
} else {
rp->height = (unsigned) rp->ih->height;
rp->height = rp->ih->height;
}
@@ -150,19 +154,19 @@ API BMPRESULT bmpread_load_info(BMPHANDLE h)
if (rp->ih->compression == BI_JPEG || rp->ih->compression == BI_PNG) {
if (!cm_gobble_up(rp, rp->fh->offbits - rp->bytes_read)) {
logerr(rp->log, "while seeking to start of jpeg/png data");
logerr(rp->c.log, "while seeking to start of jpeg/png data");
goto abort;
}
if (rp->ih->compression == BI_JPEG) {
rp->jpeg = true;
rp->getinfo_return = BMP_RESULT_JPEG;
logerr(rp->log, "embedded JPEG data");
logerr(rp->c.log, "embedded JPEG data");
rp->lasterr = BMP_ERR_JPEG;
return BMP_RESULT_JPEG;
} else {
rp->png = true;
rp->getinfo_return = BMP_RESULT_PNG;
logerr(rp->log, "embedded PNG data");
logerr(rp->c.log, "embedded PNG data");
rp->lasterr = BMP_ERR_PNG;
return BMP_RESULT_PNG;
}
@@ -193,7 +197,7 @@ API BMPRESULT bmpread_load_info(BMPHANDLE h)
if (rp->insanity_limit &&
rp->result_size > rp->insanity_limit) {
logerr(rp->log, "file is insanely large");
logerr(rp->c.log, "file is insanely large");
rp->lasterr = BMP_ERR_INSANE;
rp->getinfo_return = BMP_RESULT_INSANE;
} else {
@@ -230,7 +234,7 @@ API BMPRESULT bmpread_set_64bit_conv(BMPHANDLE h, enum Bmpconv64 conv)
case BMP_CONV64_NONE:
if (rp->result_format_explicit && rp->result_format != BMP_FORMAT_S2_13) {
logerr(rp->log, "64-bit conversion %s imcompatible with chosen number format %s.\n",
logerr(rp->c.log, "64-bit conversion %s imcompatible with chosen number format %s.\n",
cm_conv64_name(conv), cm_format_name(rp->result_format));
rp->lasterr = BMP_ERR_CONV64;
return BMP_RESULT_ERROR;
@@ -242,7 +246,7 @@ API BMPRESULT bmpread_set_64bit_conv(BMPHANDLE h, enum Bmpconv64 conv)
rp->conv64_explicit = true;
break;
default:
logerr(rp->log, "Unknown 64-bit conversion %s (%d)", cm_conv64_name(conv), (int) conv);
logerr(rp->c.log, "Unknown 64-bit conversion %s (%d)", cm_conv64_name(conv), (int) conv);
rp->lasterr = BMP_ERR_CONV64;
return BMP_RESULT_ERROR;
}
@@ -262,6 +266,13 @@ API int bmpread_is_64bit(BMPHANDLE h)
if (!(rp = cm_read_handle(h)))
return 0;
if (!rp->getinfo_called)
bmpread_load_info((BMPHANDLE)(void*)rp);
if (rp->getinfo_return != BMP_RESULT_OK && rp->getinfo_return != BMP_RESULT_INSANE) {
return 0;
}
if (rp->ih->bitcount == 64)
return 1;
return 0;
@@ -269,6 +280,121 @@ API int bmpread_is_64bit(BMPHANDLE h)
/*****************************************************************************
* bmpread_iccprofile_size
*****************************************************************************/
API size_t bmpread_iccprofile_size(BMPHANDLE h)
{
BMPREAD rp;
if (!(rp = cm_read_handle(h)))
return 0;
if (!rp->getinfo_called)
return 0;
if (rp->getinfo_return != BMP_RESULT_OK && rp->getinfo_return != BMP_RESULT_INSANE) {
return 0;
}
if (rp->ih->cstype == PROFILE_EMBEDDED && rp->ih->profilesize <= MAX_ICCPROFILE_SIZE)
return (size_t)rp->ih->profilesize;
return 0;
}
/********************************************************
* bmpread_load_iccprofile
*******************************************************/
API BMPRESULT bmpread_load_iccprofile(BMPHANDLE h, unsigned char **profile)
{
BMPREAD rp;
size_t memsize;
long pos;
bool we_allocated = false;
bool file_messed_up = false;
if (!(rp = cm_read_handle(h)))
goto abort;
if (!rp->getinfo_called)
bmpread_load_info((BMPHANDLE)(void*)rp);
if (rp->getinfo_return != BMP_RESULT_OK && rp->getinfo_return != BMP_RESULT_INSANE) {
goto abort;
}
if (rp->ih->cstype != PROFILE_EMBEDDED) {
logerr(rp->c.log, "Image has no ICC profile");
goto abort;
}
if (rp->ih->profilesize > MAX_ICCPROFILE_SIZE) {
logerr(rp->c.log, "ICC profile is too large (%lu). Max is %lu",
(unsigned long) rp->ih->profilesize,
(unsigned long) MAX_ICCPROFILE_SIZE);
goto abort;
}
if (!profile) {
logerr(rp->c.log, "profile is NULL");
goto abort;
}
memsize = rp->ih->profilesize;
if (!*profile) {
if (!(*profile = malloc(memsize))) {
logsyserr(rp->c.log, "allocating ICC profile");
goto abort;
}
we_allocated = true;
}
memset(*profile, 0, memsize);
if (-1 == (pos = ftell(rp->file))) {
logsyserr(rp->c.log, "reading current file position");
goto abort;
}
if (fseek(rp->file, rp->ih->profiledata + 14, SEEK_SET)) {
logsyserr(rp->c.log, "seeking ICC profile in file");
goto abort;
}
/* Any failure from here on out cannot be reasonably recovered from, as
* the file position will be messed up! */
file_messed_up = true;
if (memsize != fread(*profile, 1, memsize, rp->file)) {
if (feof(rp->file))
logerr(rp->c.log, "EOF while reading ICC profile");
else
logsyserr(rp->c.log, "reading ICC profile");
goto abort;
}
if (fseek(rp->file, pos, SEEK_SET)) {
logsyserr(rp->c.log, "failed to reset file position after reading ICC profile");
goto abort;
}
return BMP_RESULT_OK;
abort:
if (profile && *profile && we_allocated) {
free(*profile);
*profile = NULL;
}
if (file_messed_up)
rp->getinfo_return = BMP_RESULT_ERROR;
return BMP_RESULT_ERROR;
}
/*****************************************************************************
* bmpread_dimensions
*****************************************************************************/
@@ -296,7 +422,7 @@ API BMPRESULT bmpread_dimensions(BMPHANDLE h, int* restrict width,
rp->dim_queried_width = true;
}
if (height) {
*height = (int) rp->height;
*height = rp->height;
rp->dim_queried_height = true;
}
if (channels) {
@@ -335,7 +461,7 @@ BMPRESULT br_set_number_format(BMPREAD_R rp, enum BmpFormat format)
if (!(format == BMP_FORMAT_INT ||
format == BMP_FORMAT_FLOAT ||
format == BMP_FORMAT_S2_13)) {
logerr(rp->log, "Invalid number format (%d) specified", (int) format);
logerr(rp->c.log, "Invalid number format (%d) specified", (int) format);
rp->lasterr = BMP_ERR_FORMAT;
return BMP_RESULT_ERROR;
}
@@ -348,14 +474,14 @@ BMPRESULT br_set_number_format(BMPREAD_R rp, enum BmpFormat format)
case BMP_FORMAT_FLOAT:
case BMP_FORMAT_S2_13:
if (rp->getinfo_called && rp->result_indexed) {
logerr(rp->log, "Cannot load color index as float or s2.13");
logerr(rp->c.log, "Cannot load color index as float or s2.13");
rp->lasterr = BMP_ERR_FORMAT;
return BMP_RESULT_ERROR;
}
break;
default:
logerr(rp->log, "Invalid number format (%d) specified", (int) format);
logerr(rp->c.log, "Invalid number format (%d) specified", (int) format);
rp->lasterr = BMP_ERR_FORMAT;
return BMP_RESULT_ERROR;
}
@@ -437,7 +563,7 @@ static int s_single_dim_val(BMPHANDLE h, enum Dimint dim)
break;
case DIM_HEIGHT:
rp->dim_queried_height = true;
ret = (int) rp->height;
ret = rp->height;
break;
case DIM_CHANNELS:
rp->dim_queried_channels = true;
@@ -529,7 +655,7 @@ API void bmpread_set_undefined(BMPHANDLE h, enum BmpUndefined mode)
return;
if (mode != BMP_UNDEFINED_TO_ALPHA && mode != BMP_UNDEFINED_LEAVE) {
logerr(rp->log, "Invalid undefined-mode selected");
logerr(rp->c.log, "Invalid undefined-mode selected");
rp->lasterr = BMP_ERR_UNDEFMODE;
return;
}
@@ -560,7 +686,7 @@ API void bmpread_set_undefined(BMPHANDLE h, enum BmpUndefined mode)
void br_free(BMPREAD rp)
{
rp->magic = 0;
rp->c.magic = 0;
if (rp->palette)
free(rp->palette);
@@ -568,8 +694,8 @@ void br_free(BMPREAD rp)
free(rp->ih);
if (rp->fh)
free(rp->fh);
if (rp->log)
logfree(rp->log);
if (rp->c.log)
logfree(rp->c.log);
free(rp);
}
@@ -584,7 +710,7 @@ static bool s_is_bmptype_supported_indexed(BMPREAD_R rp);
static bool s_is_bmptype_supported(BMPREAD_R rp)
{
if (rp->ih->planes != 1) {
logerr(rp->log, "Unsupported number of planes (%d). "
logerr(rp->c.log, "Unsupported number of planes (%d). "
"Must be 1.", (int) rp->ih->planes);
rp->lasterr = BMP_ERR_HEADER;
return false;
@@ -614,7 +740,7 @@ static bool s_is_bmptype_supported_rgb(BMPREAD_R rp)
/* ok */
break;
default:
logerr(rp->log, "Invalid bitcount %d for RGB image", (int) rp->ih->bitcount);
logerr(rp->c.log, "Invalid bitcount %d for RGB image", (int) rp->ih->bitcount);
rp->lasterr = BMP_ERR_HEADER;
return false;
}
@@ -626,20 +752,20 @@ static bool s_is_bmptype_supported_rgb(BMPREAD_R rp)
case BI_BITFIELDS:
case BI_ALPHABITFIELDS:
if (rp->ih->bitcount == 64) {
logerr(rp->log, "Invalid bitcount %d for BITFIELDS", (int) rp->ih->bitcount);
logerr(rp->c.log, "Invalid bitcount %d for BITFIELDS", (int) rp->ih->bitcount);
rp->lasterr = BMP_ERR_HEADER;
return false;
}
break;
case BI_OS2_RLE24:
if (rp->ih->bitcount != 24) {
logerr(rp->log, "Invalid bitcount %d for RLE24 compression", (int) rp->ih->bitcount);
logerr(rp->c.log, "Invalid bitcount %d for RLE24 compression", (int) rp->ih->bitcount);
rp->lasterr = BMP_ERR_HEADER;
return false;
}
break;
default:
logerr(rp->log, "Unsupported compression %s for RGB image",
logerr(rp->c.log, "Unsupported compression %s for RGB image",
s_compression_name(rp->ih->compression));
rp->lasterr = BMP_ERR_UNSUPPORTED;
return false;
@@ -665,7 +791,7 @@ static bool s_is_bmptype_supported_indexed(BMPREAD_R rp)
break;
default:
logerr(rp->log, "Invalid bitcount %d for indexed image",
logerr(rp->c.log, "Invalid bitcount %d for indexed image",
(int) rp->ih->bitcount);
rp->lasterr = BMP_ERR_HEADER;
return false;
@@ -679,7 +805,7 @@ static bool s_is_bmptype_supported_indexed(BMPREAD_R rp)
if ( (rp->ih->compression == BI_RLE4 && rp->ih->bitcount != 4) ||
(rp->ih->compression == BI_RLE8 && rp->ih->bitcount != 8) ||
(rp->ih->compression == BI_OS2_HUFFMAN && rp->ih->bitcount != 1)) {
logerr(rp->log, "Unsupported compression %s for %d-bit data",
logerr(rp->c.log, "Unsupported compression %s for %d-bit data",
s_compression_name(rp->ih->compression),
(int) rp->ih->bitcount);
rp->lasterr = BMP_ERR_UNSUPPORTED;
@@ -689,7 +815,7 @@ static bool s_is_bmptype_supported_indexed(BMPREAD_R rp)
break;
default:
logerr(rp->log, "Unsupported compression %s for indexed image",
logerr(rp->c.log, "Unsupported compression %s for indexed image",
s_compression_name(rp->ih->compression));
rp->lasterr = BMP_ERR_UNSUPPORTED;
return false;
@@ -717,20 +843,20 @@ static struct Palette* s_read_palette(BMPREAD_R rp)
if (rp->ih->clrused > INT_MAX || rp->ih->clrimportant > rp->ih->clrused) {
logerr(rp->log, "Unreasonable color numbers for palette (%lu/%lu)",
logerr(rp->c.log, "Unreasonable color numbers for palette (%lu/%lu)",
(unsigned long) rp->ih->clrused,
(unsigned long) rp->ih->clrimportant);
rp->lasterr = BMP_ERR_INVALID;
return NULL;
}
if (rp->fh->offbits - rp->bytes_read > INT_MAX) {
logerr(rp->log, "gap to pixeldata too big (%lu)",
logerr(rp->c.log, "gap to pixeldata too big (%lu)",
(unsigned long) rp->fh->offbits - rp->bytes_read);
rp->lasterr = BMP_ERR_INVALID;
return NULL;
}
if (rp->fh->offbits < rp->bytes_read) {
logerr(rp->log, "Invalid offset to pixel data");
logerr(rp->c.log, "Invalid offset to pixel data");
rp->lasterr = BMP_ERR_INVALID;
return NULL;
}
@@ -744,7 +870,7 @@ static struct Palette* s_read_palette(BMPREAD_R rp)
if (0 == (colors_in_file = rp->ih->clrused)) {
colors_in_file = MIN(colors_full_palette, max_colors_in_file);
} else if (colors_in_file > max_colors_in_file) {
logerr(rp->log, "given palette size (%d) too large for available data (%d)",
logerr(rp->c.log, "given palette size (%d) too large for available data (%d)",
colors_in_file, max_colors_in_file);
rp->lasterr = BMP_ERR_INVALID;
return NULL;
@@ -756,7 +882,7 @@ static struct Palette* s_read_palette(BMPREAD_R rp)
memsize = sizeof *palette +
(colors_in_file - colors_ignore) * sizeof palette->color[0];
if (!(palette = malloc(memsize))) {
logsyserr(rp->log, "Allocating mem for palette");
logsyserr(rp->c.log, "Allocating mem for palette");
rp->lasterr = BMP_ERR_MEMORY;
return NULL;
}
@@ -769,10 +895,10 @@ static struct Palette* s_read_palette(BMPREAD_R rp)
EOF == (r = getc(rp->file)) ||
((bytes_per_entry == 4) && (EOF == getc(rp->file))) ) {
if (feof(rp->file)) {
logerr(rp->log, "file ended reading palette entries");
logerr(rp->c.log, "file ended reading palette entries");
rp->lasterr = BMP_ERR_TRUNCATED;
} else {
logsyserr(rp->log, "reading palette entries");
logsyserr(rp->c.log, "reading palette entries");
rp->lasterr = BMP_ERR_FILEIO;
}
free (palette);
@@ -786,7 +912,7 @@ static struct Palette* s_read_palette(BMPREAD_R rp)
for (i = 0; i < colors_ignore; i++) {
if (!cm_gobble_up(rp, bytes_per_entry)) {
logerr(rp->log, "reading superfluous palette entries");
logerr(rp->c.log, "reading superfluous palette entries");
free(palette);
return NULL;
}
@@ -813,7 +939,7 @@ bool br_set_resultbits(BMPREAD_R rp)
switch (rp->result_format) {
case BMP_FORMAT_FLOAT:
if (rp->result_indexed) {
logerr(rp->log, "Float is invalid number format for indexed image\n");
logerr(rp->c.log, "Float is invalid number format for indexed image\n");
rp->lasterr = BMP_ERR_FORMAT;
return false;
}
@@ -822,7 +948,7 @@ bool br_set_resultbits(BMPREAD_R rp)
case BMP_FORMAT_S2_13:
if (rp->result_indexed) {
logerr(rp->log, "s2.13 is invalid number format for indexed image\n");
logerr(rp->c.log, "s2.13 is invalid number format for indexed image\n");
rp->lasterr = BMP_ERR_FORMAT;
return false;
}
@@ -843,7 +969,7 @@ bool br_set_resultbits(BMPREAD_R rp)
}
break;
default:
logerr(rp->log, "Invalid number format %d\n", rp->result_format);
logerr(rp->c.log, "Invalid number format %d\n", rp->result_format);
rp->lasterr = BMP_ERR_FORMAT;
return false;
@@ -865,7 +991,7 @@ bool br_set_resultbits(BMPREAD_R rp)
if (rp->getinfo_called) {
if (rp->insanity_limit && rp->result_size > rp->insanity_limit) {
if (rp->getinfo_return == BMP_RESULT_OK) {
logerr(rp->log, "file is insanely large");
logerr(rp->c.log, "file is insanely large");
rp->lasterr = BMP_ERR_INSANE;
rp->getinfo_return = BMP_RESULT_INSANE;
}
@@ -889,8 +1015,8 @@ static bool s_check_dimensions(BMPREAD_R rp)
npixels = (uint64_t) rp->width * rp->height;
maxpixels = SIZE_MAX / rp->result_bytes_per_pixel;
if (npixels > maxpixels || rp->width < 1 || rp->height < 1 || rp->height > INT32_MAX) {
logerr(rp->log, "Invalid BMP dimensions (%dx%u)", rp->width, rp->height);
if (npixels > maxpixels || rp->width < 1 || rp->height < 1) {
logerr(rp->c.log, "Invalid BMP dimensions (%dx%d)", rp->width, rp->height);
rp->lasterr = BMP_ERR_DIMENSIONS;
return false;
}
@@ -924,7 +1050,7 @@ static bool s_read_colormasks(BMPREAD_R rp)
break;
default:
logerr(rp->log, "Invalid compression (%s)",
logerr(rp->c.log, "Invalid compression (%s)",
s_compression_name(rp->ih->compression));
rp->lasterr = BMP_ERR_INVALID;
return false;
@@ -943,19 +1069,19 @@ static bool s_read_colormasks(BMPREAD_R rp)
sum_bits += rp->cmask.bits.value[i];
}
if (max_bits > MIN(rp->ih->bitcount, 32) || sum_bits > rp->ih->bitcount) {
logerr(rp->log, "Invalid mask bitcount (max=%d, sum=%d)",
logerr(rp->c.log, "Invalid mask bitcount (max=%d, sum=%d)",
max_bits, sum_bits);
rp->lasterr = BMP_ERR_INVALID;
return false;
}
if (!(rp->cmask.mask.red | rp->cmask.mask.green | rp->cmask.mask.blue)) {
logerr(rp->log, "Empty color masks. Corrupt BMP?");
logerr(rp->c.log, "Empty color masks. Corrupt BMP?");
rp->lasterr = BMP_ERR_INVALID;
return false;
}
if (rp->cmask.mask.red & rp->cmask.mask.green &
rp->cmask.mask.blue & rp->cmask.mask.alpha) {
logerr(rp->log, "Overlapping color masks. Corrupt BMP?");
logerr(rp->c.log, "Overlapping color masks. Corrupt BMP?");
rp->lasterr = BMP_ERR_INVALID;
return false;
}
@@ -975,7 +1101,7 @@ static bool s_read_masks_from_bitfields(BMPREAD_R rp)
int i;
if (!(rp->ih->bitcount == 16 || rp->ih->bitcount == 32)) {
logerr(rp->log, "Invalid bitcount (%d) for BI_BITFIELDS."
logerr(rp->c.log, "Invalid bitcount (%d) for BI_BITFIELDS."
"Must be 16 or 32", (int) rp->ih->bitcount);
rp->lasterr = BMP_ERR_INVALID;
return false;
@@ -986,10 +1112,10 @@ static bool s_read_masks_from_bitfields(BMPREAD_R rp)
read_u32_le(rp->file, &g) &&
read_u32_le(rp->file, &b))) {
if (feof(rp->file)) {
logerr(rp->log, "File ended reading color masks");
logerr(rp->c.log, "File ended reading color masks");
rp->lasterr = BMP_ERR_TRUNCATED;
} else {
logsyserr(rp->log, "Reading BMP color masks");
logsyserr(rp->c.log, "Reading BMP color masks");
rp->lasterr = BMP_ERR_FILEIO;
}
return false;
@@ -1001,10 +1127,10 @@ static bool s_read_masks_from_bitfields(BMPREAD_R rp)
if (rp->ih->compression == BI_ALPHABITFIELDS) {
if (!read_u32_le(rp->file, &a)) {
if (feof(rp->file)) {
logerr(rp->log, "File ended reading color masks");
logerr(rp->c.log, "File ended reading color masks");
rp->lasterr = BMP_ERR_TRUNCATED;
} else {
logsyserr(rp->log, "Reading BMP color masks");
logsyserr(rp->c.log, "Reading BMP color masks");
rp->lasterr = BMP_ERR_FILEIO;
}
return false;
@@ -1053,7 +1179,7 @@ static bool s_create_implicit_colormasks(BMPREAD_R rp)
bitsperchannel = 16;
break;
default:
logerr(rp->log, "Invalid bitcount for BMP (%d)", (int) rp->ih->bitcount);
logerr(rp->c.log, "Invalid bitcount for BMP (%d)", (int) rp->ih->bitcount);
rp->lasterr = BMP_ERR_INVALID;
return false;
}
@@ -1140,11 +1266,11 @@ static bool s_read_file_header(BMPREAD_R rp)
}
if (feof(rp->file)) {
logerr(rp->log, "unexpected end-of-file while reading "
logerr(rp->c.log, "unexpected end-of-file while reading "
"file header");
rp->lasterr = BMP_ERR_TRUNCATED;
} else {
logsyserr(rp->log, "error reading file header");
logsyserr(rp->c.log, "error reading file header");
rp->lasterr = BMP_ERR_FILEIO;
}
@@ -1193,7 +1319,7 @@ static bool s_read_info_header(BMPREAD_R rp)
if (rp->ih->size > 124)
rp->ih->version = BMPINFO_FUTURE;
else {
logerr(rp->log, "Invalid info header size (%lu)",
logerr(rp->c.log, "Invalid info header size (%lu)",
(unsigned long) rp->ih->size);
rp->lasterr = BMP_ERR_HEADER;
return false;
@@ -1285,10 +1411,10 @@ header_done:
abort_file_err:
if (feof(rp->file)) {
logerr(rp->log, "Unexpected end of file while reading BMP info header");
logerr(rp->c.log, "Unexpected end of file while reading BMP info header");
rp->lasterr = BMP_ERR_TRUNCATED;
} else {
logsyserr(rp->log, "While reading BMP info header");
logsyserr(rp->c.log, "While reading BMP info header");
rp->lasterr = BMP_ERR_FILEIO;
}
return false;
@@ -1405,7 +1531,7 @@ static const char* s_info_str(BMPHANDLE h, enum Infostr info)
switch (info) {
case INFO_STR_HEADER_NAME:
return s_infoheader_name(rp->ih->version);
return cm_infoheader_name(rp->ih->version);
case INFO_STR_COMPRESSION_NAME:
return s_compression_name(rp->ih->compression);
@@ -1450,28 +1576,6 @@ API BMPRESULT bmpread_info_channel_bits(BMPHANDLE h, int *r, int *g, int *b, int
/*****************************************************************************
* s_infoheader_name
*****************************************************************************/
const char* s_infoheader_name(int infoversion)
{
switch (infoversion) {
case BMPINFO_CORE_OS21 : return "OS21XBITMAPHEADER";
case BMPINFO_OS22 : return "OS22XBITMAPHEADER";
case BMPINFO_V3 : return "BITMAPINFOHEADER";
case BMPINFO_V3_ADOBE1 : return "BITMAPINFOHEADER + RGB mask";
case BMPINFO_V3_ADOBE2 : return "BITMAPINFOHEADER + RGBA mask";
case BMPINFO_V4 : return "BITMAPV4HEADER";
case BMPINFO_V5 : return "BITMAPV5HEADER";
case BMPINFO_FUTURE : return "unknown future version";
default:
return "invalid infoheader version";
}
}
/*****************************************************************************
* s_compression_name
*****************************************************************************/

File diff suppressed because it is too large Load Diff

View File

@@ -42,7 +42,7 @@
#endif
typedef struct Bmphandle *BMPHANDLE;
typedef union Bmphandle *BMPHANDLE;
/*
@@ -203,6 +203,15 @@ enum BmpFormat {
typedef enum BmpFormat BMPFORMAT;
enum BmpIntent {
BMP_INTENT_NONE,
BMP_INTENT_BUSINESS, /* saturation */
BMP_INTENT_GRAPHICS, /* relative colorimetric */
BMP_INTENT_IMAGES, /* perceptive */
BMP_INTENT_ABS_COLORIMETRIC /* absolute colorimetric */
};
typedef enum BmpIntent BMPINTENT;
APIDECL BMPHANDLE bmpread_new(FILE *file);
APIDECL BMPRESULT bmpread_load_info(BMPHANDLE h);
@@ -239,6 +248,9 @@ 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 size_t bmpread_iccprofile_size(BMPHANDLE h);
APIDECL BMPRESULT bmpread_load_iccprofile(BMPHANDLE h, unsigned char **profile);
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);
@@ -268,11 +280,16 @@ APIDECL BMPRESULT bmpwrite_set_orientation(BMPHANDLE h, BMPORIENT orientation);
APIDECL BMPRESULT bmpwrite_set_64bit(BMPHANDLE h);
APIDECL BMPRESULT bmpwrite_set_huffman_img_fg_idx(BMPHANDLE h, int idx);
APIDECL BMPRESULT bmpwrite_set_iccprofile(BMPHANDLE h, size_t size,
const unsigned char *iccprofile);
APIDECL BMPRESULT bmpwrite_set_rendering_intent(BMPHANDLE h, BMPINTENT intent);
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 BMPRESULT bmp_set_huffman_t4black_value(BMPHANDLE h, int blackidx);
APIDECL void bmp_free(BMPHANDLE h);

View File

@@ -116,14 +116,14 @@ int main(int argc, char *argv[])
fprintf(file, "static const int blackroot = %d;\n", black_tree);
fprintf(file, "static const int whiteroot = %d;\n\n\n", white_tree);
fprintf(file, "static const struct Node nodebuffer[] = {\n");
for (i = 0; i < ARR_SIZE(nodebuffer); i++) {
for (i = 0; i < (int) ARR_SIZE(nodebuffer); i++) {
fprintf(file, "\t{ %3d, %3d, %4d, %d, %d },\n",
n[i].l, n[i].r, n[i].value, n[i].terminal, n[i].makeup);
}
fputs("};\n\n", file);
fputs("static const struct Huffcode huff_term_black[] = {\n\t", file);
for (i = 0; i < ARR_SIZE(huff_term_black); i++) {
for (i = 0; i < (int) ARR_SIZE(huff_term_black); i++) {
fprintf(file, "{ 0x%02hx, %2d },",
str2bits(huff_term_black[i].bits),
(int) strlen(huff_term_black[i].bits));
@@ -135,7 +135,7 @@ int main(int argc, char *argv[])
fputs("\n};\n\n", file);
fputs("static const struct Huffcode huff_term_white[] = {\n\t", file);
for (i = 0; i < ARR_SIZE(huff_term_white); i++) {
for (i = 0; i < (int) ARR_SIZE(huff_term_white); i++) {
fprintf(file, "{ 0x%02hx, %2d },",
str2bits(huff_term_white[i].bits),
(int) strlen(huff_term_white[i].bits));
@@ -147,7 +147,7 @@ int main(int argc, char *argv[])
fputs("\n};\n\n", file);
fputs("static const struct Huffcode huff_makeup_black[] = {\n\t", file);
for (i = 0; i < ARR_SIZE(huff_makeup_black); i++) {
for (i = 0; i < (int) ARR_SIZE(huff_makeup_black); i++) {
fprintf(file, "{ 0x%02hx, %2d },",
str2bits(huff_makeup_black[i].bits),
(int) strlen(huff_makeup_black[i].bits));
@@ -159,7 +159,7 @@ int main(int argc, char *argv[])
fputs("\n};\n\n", file);
fputs("static const struct Huffcode huff_makeup_white[] = {\n\t", file);
for (i = 0; i < ARR_SIZE(huff_makeup_white); i++) {
for (i = 0; i < (int) ARR_SIZE(huff_makeup_white); i++) {
fprintf(file, "{ 0x%02hx, %2d },",
str2bits(huff_makeup_white[i].bits),
(int) strlen(huff_makeup_white[i].bits));
@@ -197,20 +197,20 @@ static void s_buildtree(void)
memset(nodebuffer, 0, sizeof nodebuffer);
for (i = 0; i < ARR_SIZE(huff_term_black); i++) {
for (i = 0; i < (int) ARR_SIZE(huff_term_black); i++) {
add_node(&black_tree, huff_term_black[i].bits,
huff_term_black[i].number, false);
}
for (i = 0; i < ARR_SIZE(huff_makeup_black); i++) {
for (i = 0; i < (int) ARR_SIZE(huff_makeup_black); i++) {
add_node(&black_tree, huff_makeup_black[i].bits,
huff_makeup_black[i].number, true);
}
for (i = 0; i < ARR_SIZE(huff_term_white); i++) {
for (i = 0; i < (int) ARR_SIZE(huff_term_white); i++) {
add_node(&white_tree, huff_term_white[i].bits,
huff_term_white[i].number, false);
}
for (i = 0; i < ARR_SIZE(huff_makeup_white); i++) {
for (i = 0; i < (int) ARR_SIZE(huff_makeup_white); i++) {
add_node(&white_tree, huff_makeup_white[i].bits,
huff_makeup_white[i].number, true);
}
@@ -229,7 +229,7 @@ static void add_node(int *nodeidx, const char *bits, int value, bool makeup)
nodebuffer[*nodeidx].l = -1;
nodebuffer[*nodeidx].r = -1;
}
if (nnodes > ARR_SIZE(nodebuffer)) {
if (nnodes > (int) ARR_SIZE(nodebuffer)) {
printf("too many nodes (have %d, max is %d)\n",
nnodes, (int) ARR_SIZE(nodebuffer));
exit(1);

View File

@@ -227,7 +227,7 @@ bool huff_flush(BMPWRITE_R wp)
while (wp->hufbuf_len >= 8) {
byte = 0x00ff & (wp->hufbuf >> (wp->hufbuf_len - 8));
if (EOF == putc(byte, wp->file)) {
logsyserr(wp->log, "writing Huffman bitmap");
logsyserr(wp->c.log, "writing Huffman bitmap");
return false;
}
wp->bytes_written++;

View File

@@ -29,6 +29,12 @@
#include "config.h"
#include "logging.h"
#if defined(__GNUC__)
#define MAY_BE_UNUSED __attribute__((unused))
#else
#define MAY_BE_UNUSED
#endif
struct Log {
int size;
@@ -169,8 +175,9 @@ void logsyserr(LOG log, const char *fmt, ...)
* s_log()
*********************************************************/
static void s_log(LOG log, const char *file, int line, const char *function,
const char *etxt, const char *fmt, va_list args)
static void s_log(LOG log, const char *file MAY_BE_UNUSED, int line MAY_BE_UNUSED,
const char *function MAY_BE_UNUSED,
const char *etxt, const char *fmt, va_list args)
{
va_list argsdup;
int len = 0,addl_len, required_len;

View File

@@ -1,16 +1,25 @@
project('bmplib', 'c', default_options: ['c_std=c11'], version: '1.7.4')
project('bmplib', 'c', default_options: ['c_std=c11', 'warning_level=3'], version: '1.7.7')
cc = meson.get_compiler('c')
add_project_arguments(['-pedantic','-fvisibility=hidden'], language : 'c')
add_project_arguments('-pedantic', language : 'c')
add_project_arguments('-fvisibility=hidden', language: 'c')
if get_option('buildtype') == 'debug'
add_project_arguments('-DDEBUG', language: 'c')
elif get_option('buildtype') == 'release'
add_project_arguments('-DNDEBUG', language: 'c')
endif
if get_option('sanitize')
sanitize = [
'-fsanitize=signed-integer-overflow',
'-fsanitize=undefined',
'-fsanitize=float-divide-by-zero',
]
add_project_arguments(sanitize, language : 'c')
add_project_link_arguments(sanitize, language: 'c')
endif
m_dep = cc.find_library('m', required : false)
conf_data = configuration_data()

View File

@@ -1 +1,2 @@
option('insanity_limit_mb', type: 'integer', min: 0, value: 500)
option('sanitize', type: 'boolean', value: false)