mirror of
https://github.com/rupertwh/bmplib.git
synced 2026-04-12 17:22:36 +00:00
Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
91066fc509 | ||
|
|
de7004a158 | ||
|
|
7523138fbb | ||
|
|
592605e06f | ||
|
|
eeae2205c5 | ||
|
|
2d7763de7f | ||
|
|
7513352a7b | ||
|
|
ea7b93ce64 | ||
|
|
1b3c8a1081 | ||
|
|
1ede644b71 | ||
|
|
1b9c8af659 | ||
|
|
a4800cb684 | ||
|
|
ed07c4f618 | ||
|
|
9297d88483 | ||
|
|
c0c2513c7d | ||
|
|
352a231632 | ||
|
|
72f8085c41 |
145
API-full.md
145
API-full.md
@@ -1,11 +1,11 @@
|
||||
# Rupert's bmplib -- Full API Description (v1.7.1)
|
||||
# Rupert's bmplib -- Full API Description (v1.7.5)
|
||||
|
||||
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.
|
||||
|
||||
|
||||
## 1. Functions for reading BMP files
|
||||
|
||||
### Get a handle
|
||||
|
||||
```
|
||||
BMPHANDLE bmpread_new(FILE *file)
|
||||
```
|
||||
@@ -16,14 +16,14 @@ handle.
|
||||
|
||||
The handle cannot be reused to read multiple files.
|
||||
|
||||
|
||||
|
||||
### Read the file header
|
||||
|
||||
```
|
||||
BMPRESULT bmpread_load_info(BMPHANDLE h)
|
||||
```
|
||||
|
||||
bmplib reads the file header and checks validity. Possible return values are:
|
||||
|
||||
- `BMP_RESULT_OK`: All is good, you can proceed to read the file.
|
||||
- `BMP_INSANE`: The file is valid, but huge. The default limit is 500MB
|
||||
(relevant is the required buffer size to hold the complete image, not the
|
||||
@@ -42,8 +42,8 @@ bmplib reads the file header and checks validity. Possible return values are:
|
||||
Calling `bmpread_load_info()` is optional when you use `bmpread_dimensions()`
|
||||
(see below).
|
||||
|
||||
|
||||
### Get image dimensions
|
||||
|
||||
```
|
||||
BMPRESULT bmpread_dimensions(BMPHANDLE h,
|
||||
int *width,
|
||||
@@ -77,30 +77,32 @@ BMPORIENT bmpread_orientation(BMPHANDLE h)
|
||||
|
||||
int bmpread_resolution_xdpi(BMPHANDLE h)
|
||||
int bmpread_resolution_ydpi(BMPHANDLE h)
|
||||
|
||||
```
|
||||
|
||||
#### top-down / bottom-up
|
||||
`*orientation` is one of:
|
||||
- `BMP_ORIENT_BOTTOMUP`
|
||||
- `BMP_ORIENT_TOPDOWN`
|
||||
|
||||
`bmpread_orientation()` or the `orientation` value returned by
|
||||
`bmpread_dimensions()` **is only relevant if you load the BMP file
|
||||
line-by-line**. In line-by-line mode (using `bmpread_load_line()`), the
|
||||
image data is always delivered in the order it is in the BMP file. The
|
||||
`orientation` value will tell you if it's top-down or bottom-up. On the
|
||||
other hand, when the whole image is loaded at once (using `bmpread_load_image
|
||||
()`), bmplib will **always** return the image top-down, regardless of how
|
||||
the BMP file is oriented. The `orientation` value will still indicate the
|
||||
orientation of the original BMP.
|
||||
`*orientation` is one of:
|
||||
|
||||
- `BMP_ORIENT_BOTTOMUP`
|
||||
- `BMP_ORIENT_TOPDOWN`
|
||||
|
||||
`bmpread_orientation()` or the `orientation` value returned by
|
||||
`bmpread_dimensions()` **is only relevant if you load the BMP file
|
||||
line-by-line**. In line-by-line mode (using `bmpread_load_line()`), the
|
||||
image data is always delivered in the order it is in the BMP file. The
|
||||
`orientation` value will tell you if it's top-down or bottom-up. On the
|
||||
other hand, when the whole image is loaded at once (using `bmpread_load_image
|
||||
()`), bmplib will **always** return the image top-down, regardless of how
|
||||
the BMP file is oriented. The `orientation` value will still indicate the
|
||||
orientation of the original BMP.
|
||||
|
||||
#### Required size for buffer to receive image
|
||||
|
||||
```
|
||||
size_t bmpread_buffersize(BMPHANDLE h)
|
||||
```
|
||||
Returns the buffer size you have to allocate for the whole image.
|
||||
|
||||
Returns the buffer size you have to allocate for the whole image.
|
||||
|
||||
### Indexed BMPs
|
||||
|
||||
@@ -141,7 +143,6 @@ bmpread_load_palette(h, &palette); /* bmplib will allocate the palette
|
||||
/* or: */
|
||||
palette = malloc(4 * numcolors);
|
||||
bmpread_load_palette(h, &palette); /* bmplib will use the provided buffer */
|
||||
|
||||
```
|
||||
|
||||
Note: Once you have called `bmpread_load_palette()`, both `bmpread_load_image
|
||||
@@ -175,6 +176,7 @@ void bmpread_set_undefined(BMPHANDLE h, BMPUNDEFINED mode)
|
||||
```
|
||||
|
||||
`mode` can be one of:
|
||||
|
||||
- `BMP_UNDEFINED_LEAVE`
|
||||
- `BMP_UNDEFINED_TO_ALPHA` (default)
|
||||
|
||||
@@ -182,7 +184,6 @@ 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)
|
||||
|
||||
|
||||
### Optional settings for 64bit BMPs
|
||||
|
||||
```
|
||||
@@ -207,7 +208,6 @@ Options for `bmpread_set_64bit()` are:
|
||||
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:
|
||||
@@ -218,8 +218,6 @@ BMPRESULT bmp_set_number_format(BMPHANDLE h, BMPFORMAT format)
|
||||
|
||||
(see below, *3. General functions for both reading/writing BMPs*)
|
||||
|
||||
|
||||
|
||||
### Huge files: bmpread_set_insanity_limit()
|
||||
|
||||
bmplib will refuse to load images beyond a certain size (default 500MB) and
|
||||
@@ -232,8 +230,8 @@ void
|
||||
bmpread_set_insanity_limit(BMPHANDLE h, size_t limit)
|
||||
```
|
||||
|
||||
|
||||
### Load the image
|
||||
|
||||
#### bmpread_load_image()
|
||||
|
||||
```
|
||||
@@ -259,7 +257,6 @@ bmpread_load_image(h, &buffer); /* bmplib will allocate the buffer */
|
||||
/* or: */
|
||||
buffer = malloc(bmpread_buffersize(h));
|
||||
bmpread_load_image(h, &buffer); /* bmplib will use the provided buffer */
|
||||
|
||||
```
|
||||
|
||||
The image data is written to the buffer according to the returned dimensions
|
||||
@@ -273,6 +270,7 @@ 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)
|
||||
```
|
||||
@@ -303,7 +301,6 @@ returned in whichever order they are stored in the BMP. Use the value
|
||||
returned by `bmpread_orientation()` to determine if it is top-down or
|
||||
bottom-up. Almost all BMPs will be bottom-up. (see above)
|
||||
|
||||
|
||||
### Invalid pixels
|
||||
|
||||
Invalid pixels may occur in indexed BMPs, both RLE and non-RLE. Invalid pixels
|
||||
@@ -316,7 +313,6 @@ 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.
|
||||
|
||||
|
||||
### Query info about the BMP file
|
||||
|
||||
Note: these functions return information about the original BMP file being
|
||||
@@ -333,7 +329,6 @@ 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
|
||||
|
||||
```
|
||||
@@ -347,17 +342,16 @@ affected**, so you can call bmp_free() immediately after `bmpread_load_image
|
||||
Note: Any error message strings returned by `bmp_errmsg()` are invalidated by
|
||||
`bmp_free()` and must not be used anymore!
|
||||
|
||||
|
||||
|
||||
|
||||
## 2. Functions for writing BMP files
|
||||
|
||||
### Get a handle
|
||||
|
||||
```
|
||||
BMPHANDLE bmpwrite_new(FILE *file)
|
||||
```
|
||||
|
||||
### Set image dimensions
|
||||
|
||||
```
|
||||
BMPRESULT bmpwrite_set_dimensions(BMPHANDLE h,
|
||||
unsigned width,
|
||||
@@ -366,7 +360,6 @@ BMPRESULT bmpwrite_set_dimensions(BMPHANDLE h,
|
||||
unsigned bitsperchannel)
|
||||
|
||||
BMPRESULT bmpwrite_set_resolution(BMPHANDLE h, int xdpi, int ydpi)
|
||||
|
||||
```
|
||||
|
||||
Note: the dimensions set with `bmpwrite_set_dimensions()` describe the source
|
||||
@@ -374,7 +367,6 @@ data that you pass to bmplib, *not* the output BMP format. Use
|
||||
`bmpwrite_set_output_bits()`, `bmpwrite_set_palette()`, and
|
||||
`bmpwrite_set_64bit()` to modify the format written to the BMP file.
|
||||
|
||||
|
||||
### Set the output format
|
||||
|
||||
Optional: set the bit-depth for each output channel. bmplib will otherwise
|
||||
@@ -415,6 +407,7 @@ BMP for 3- or 4-color images, call `bmpwrite_allow_2bit()` before calling
|
||||
```
|
||||
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.
|
||||
@@ -427,16 +420,30 @@ only after explicitly allowing it by calling `bmpwrite_allow_huffman()`
|
||||
|
||||
To activate RLE compression, call `bmpwrite_set_rle()` with `type` set to one
|
||||
of the following values:
|
||||
|
||||
- `BMP_RLE_NONE` no RLE compression, same as not calling `bmpwrite_set_rle()`
|
||||
at all
|
||||
- `BMP_RLE_AUTO` choose RLE4, RLE8, or 1-D Huffman based on number of colors
|
||||
in palette
|
||||
- `BMP_RLE_RLE8` use RLE8, regardless of number of colors in palette
|
||||
|
||||
In order to write 1-D Huffman encoded bitmpas, the provided palette must have
|
||||
2 colors, RLE type must be set to `BMP_RLE_AUTO`, and `bmpwrite_allow_huffman
|
||||
()` must be called. Be aware that *very* few programs will be able to read
|
||||
Huffman encoded BMPs!
|
||||
|
||||
#### 1-D Huffman encoding
|
||||
In order to write 1-D Huffman encoded bitmpas,
|
||||
|
||||
- the provided palette must have 2 colors,
|
||||
- RLE type must be set to `BMP_RLE_AUTO`,
|
||||
- and `bmpwrite_allow_huffman()` must be called.
|
||||
|
||||
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
|
||||
image, but setting it correctly will result in better compression.
|
||||
|
||||
|
||||
#### RLE24
|
||||
|
||||
@@ -449,7 +456,6 @@ In order to save an image as RLE24, the data must be provided as 8 bits per
|
||||
channel RGB (no alpha channel). Call `bmpwrite_set_rle()` with type set to
|
||||
`BMP_RLE_AUTO` and also call `bmpwrite_allow_rle24()` (in any order).
|
||||
|
||||
|
||||
### top-down / bottom-up
|
||||
|
||||
By default, bmplib will write BMP files bottom-up, which is how BMP files are
|
||||
@@ -463,8 +469,9 @@ BMPRESULT bmpwrite_set_orientation(BMPHANDLE h, BMPORIENT orientation)
|
||||
```
|
||||
|
||||
with `orientation` set to one of the following values:
|
||||
- `BMPORIENT_BOTTOMUP`
|
||||
- `BMPORIENT_TOPDOWN`
|
||||
|
||||
- `BMPORIENT_BOTTOMUP`
|
||||
- `BMPORIENT_TOPDOWN`
|
||||
|
||||
Note: When writing the whole image at once using `bmpwrite_save_image()`, the
|
||||
image buffer you provide must **always** be in top-down orientation,
|
||||
@@ -474,9 +481,6 @@ 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.
|
||||
|
||||
|
||||
|
||||
|
||||
### 64-bit RGBA BMPs
|
||||
|
||||
By default, bmplib will not write 64-bit BMPs because they are rather exotic and hardly any
|
||||
@@ -492,9 +496,6 @@ In order to make use of the extended range available in 64-bit BMPs (-4.0 to +3.
|
||||
|
||||
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
|
||||
|
||||
```
|
||||
@@ -517,11 +518,6 @@ Important: When writing the whole image at once using `bmpwrite_save_image
|
||||
line-by-line, the image data must be provided according to the orientation
|
||||
set with `bmpwrite_set_orientation()` (see above).
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## 3. General functions for both reading/writing BMPs
|
||||
|
||||
### bmp_free()
|
||||
@@ -536,7 +532,6 @@ affected, so you can call bmp_free() immediately after bmpread_load_image
|
||||
by `bmp_errmsg()` are invalidated by `bmp_free()` and cannot be used
|
||||
anymore.
|
||||
|
||||
|
||||
### bmp_errmsg()
|
||||
|
||||
```
|
||||
@@ -547,7 +542,6 @@ 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.
|
||||
|
||||
|
||||
### bmp_set_number_format()
|
||||
|
||||
```
|
||||
@@ -562,7 +556,6 @@ sets the number format of the image buffer received from / passed to bmplib. `fo
|
||||
|
||||
For indexed images, `BMP_FORMAT_INT` is the only valid format.
|
||||
|
||||
|
||||
### bmp_version()
|
||||
|
||||
```
|
||||
@@ -571,7 +564,40 @@ const char* bmp_version(void)
|
||||
|
||||
Returns a zero-terminated character string containing the version of bmplib.
|
||||
|
||||
### bmp_set_huffman_t4black_value()
|
||||
|
||||
```
|
||||
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 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
|
||||
@@ -608,10 +634,10 @@ else {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
#### `BMPINFOVER`
|
||||
|
||||
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_V3` BITMAPINFOHEADER (40 bytes)
|
||||
@@ -627,6 +653,7 @@ from `BMPINFO_CORE_OS21` to `BMPINFO_FUTURE`.
|
||||
#### `BMPRLETYPE`
|
||||
|
||||
Used in `bmpwrite_set_rle()`. Possible values are:
|
||||
|
||||
- `BMP_RLE_NONE` No RLE
|
||||
- `BMP_RLE_AUTO` RLE4 or RLE8, chosen based on number of colors in palette
|
||||
- `BMP_RLE_RLE8` Use RLE8 for any number of colors in palette
|
||||
@@ -636,6 +663,7 @@ Can safely be cast from/to int.
|
||||
#### `BMPUNDEFINED`
|
||||
|
||||
Used in `bmpread_set_undefined()`. Possible values are:
|
||||
|
||||
- `BMP_UNDEFINED_TO_ALPHA` (default)
|
||||
- `BMP_UNDEFINED_TO_ZERO`
|
||||
|
||||
@@ -644,6 +672,7 @@ Can safely be cast from/to int.
|
||||
#### `BMPCONV64`
|
||||
|
||||
Used in `bmpread_set_64bit_conv()`. Possible values are:
|
||||
|
||||
- `BMP_CONV64_SRGB` (default)
|
||||
- `BMP_CONV64_LINEAR`
|
||||
- `BMP_CONV64_NONE`
|
||||
@@ -653,12 +682,11 @@ Can safely be cast from/to int.
|
||||
#### `BMPFORMAT`
|
||||
|
||||
Used in `bmp_set_number_format()`. Possible values are:
|
||||
|
||||
- `BMP_FORMAT_INT` (default)
|
||||
- `BMP_FORMAT_FLOAT` 32-bit floating point
|
||||
- `BMP_FORMAT_S2_13` s2.13 fixed point
|
||||
|
||||
|
||||
|
||||
## 5. Sample code
|
||||
|
||||
### Reading BMPs
|
||||
@@ -713,7 +741,6 @@ Used in `bmp_set_number_format()`. Possible values are:
|
||||
*/
|
||||
```
|
||||
|
||||
|
||||
### Writing BMPs
|
||||
|
||||
```
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
Download [bmplib on github](https://github.com/rupertwh/bmplib).
|
||||
|
||||
## Current status (v1.7.1):
|
||||
## Current status (v1.7.5):
|
||||
### Reading BMP files:
|
||||
- 16/24/32 bit RGB(A) with any bits/channel combination
|
||||
(BI_RGB, BI_BITFIELDS, BI_ALPHABITFIELDS).
|
||||
|
||||
62
bmp-common.c
62
bmp-common.c
@@ -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;
|
||||
}
|
||||
|
||||
155
bmp-common.h
155
bmp-common.h
@@ -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;
|
||||
@@ -154,10 +155,7 @@ struct Bmpread {
|
||||
|
||||
|
||||
struct Bmpwrite {
|
||||
struct {
|
||||
uint32_t magic;
|
||||
LOG log;
|
||||
};
|
||||
struct Bmpcommon c;
|
||||
FILE *file;
|
||||
struct Bmpfile *fh;
|
||||
struct Bmpinfo *ih;
|
||||
@@ -167,14 +165,17 @@ struct Bmpwrite {
|
||||
int source_channels;
|
||||
int source_bitsperchannel;
|
||||
int source_bytes_per_pixel;
|
||||
int source_format;
|
||||
enum BmpFormat source_format;
|
||||
struct Palette *palette;
|
||||
int palette_size; /* sizeof palette in bytes */
|
||||
unsigned char *iccprofile;
|
||||
int iccprofile_size;
|
||||
/* output */
|
||||
size_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 */
|
||||
@@ -197,6 +198,18 @@ struct Bmpwrite {
|
||||
};
|
||||
|
||||
|
||||
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, ...);
|
||||
@@ -243,75 +256,73 @@ int16_t s16_from_le(const unsigned char *buf);
|
||||
#define BMPFILE_PT 0x5450
|
||||
|
||||
|
||||
#define BMPFHSIZE 14
|
||||
#define BMPIHSIZE_V3 40
|
||||
#define BMPIHSIZE_V4 108
|
||||
#define BMPIHSIZE_OS22 64
|
||||
|
||||
typedef uint16_t WORD;
|
||||
typedef uint32_t DWORD;
|
||||
typedef int32_t LONG;
|
||||
typedef uint8_t BYTE;
|
||||
#define BMPFHSIZE 14
|
||||
#define BMPIHSIZE_V3 40
|
||||
#define BMPIHSIZE_V4 108
|
||||
#define BMPIHSIZE_OS22 64
|
||||
#define BMPIHSIZE_V5 124
|
||||
|
||||
struct Bmpfile {
|
||||
WORD type; /* "BM" */
|
||||
DWORD size; /* bytes in file */
|
||||
WORD reserved1;
|
||||
WORD reserved2;
|
||||
DWORD offbits;
|
||||
uint16_t type; /* "BM" */
|
||||
uint32_t size; /* bytes in file */
|
||||
uint16_t reserved1;
|
||||
uint16_t reserved2;
|
||||
uint32_t offbits;
|
||||
};
|
||||
|
||||
struct Bmpinfo {
|
||||
/* BITMAPINFOHEADER (40 bytes) */
|
||||
DWORD size; /* sizof struct */
|
||||
LONG width;
|
||||
LONG height;
|
||||
WORD planes;
|
||||
WORD bitcount;
|
||||
DWORD compression;
|
||||
DWORD sizeimage; /* 0 ok for uncompressed */
|
||||
LONG xpelspermeter;
|
||||
LONG ypelspermeter;
|
||||
DWORD clrused;
|
||||
DWORD clrimportant;
|
||||
uint32_t size; /* sizof struct */
|
||||
int32_t width;
|
||||
int32_t height;
|
||||
uint16_t planes;
|
||||
uint16_t bitcount;
|
||||
uint32_t compression;
|
||||
uint32_t sizeimage; /* 0 ok for uncompressed */
|
||||
int32_t xpelspermeter;
|
||||
int32_t ypelspermeter;
|
||||
uint32_t clrused;
|
||||
uint32_t clrimportant;
|
||||
/* BITMAPV4INFOHEADER (108 bytes) */
|
||||
DWORD redmask;
|
||||
DWORD greenmask;
|
||||
DWORD bluemask;
|
||||
DWORD alphamask;
|
||||
DWORD cstype;
|
||||
LONG redX;
|
||||
LONG redY;
|
||||
LONG redZ;
|
||||
LONG greenX;
|
||||
LONG greenY;
|
||||
LONG greenZ;
|
||||
LONG blueX;
|
||||
LONG blueY;
|
||||
LONG blueZ;
|
||||
DWORD gammared;
|
||||
DWORD gammagreen;
|
||||
DWORD gammablue;
|
||||
uint32_t redmask;
|
||||
uint32_t greenmask;
|
||||
uint32_t bluemask;
|
||||
uint32_t alphamask;
|
||||
uint32_t cstype;
|
||||
int32_t redX;
|
||||
int32_t redY;
|
||||
int32_t redZ;
|
||||
int32_t greenX;
|
||||
int32_t greenY;
|
||||
int32_t greenZ;
|
||||
int32_t blueX;
|
||||
int32_t blueY;
|
||||
int32_t blueZ;
|
||||
uint32_t gammared;
|
||||
uint32_t gammagreen;
|
||||
uint32_t gammablue;
|
||||
/* BITMAPV5INFOHEADER (124 bytes) */
|
||||
DWORD intent;
|
||||
DWORD profiledata;
|
||||
DWORD profilesize;
|
||||
DWORD reserved;
|
||||
uint32_t intent;
|
||||
uint32_t profiledata;
|
||||
uint32_t profilesize;
|
||||
uint32_t reserved;
|
||||
|
||||
/* OS22XBITMAPHEADER */
|
||||
WORD resolution; /* = 0 */
|
||||
WORD orientation; /* = 0 */
|
||||
WORD halftone_alg;
|
||||
DWORD halftone_parm1;
|
||||
DWORD halftone_parm2;
|
||||
DWORD color_encoding; /* = 0 (RGB) */
|
||||
DWORD app_id;
|
||||
uint16_t resolution; /* = 0 */
|
||||
uint16_t orientation; /* = 0 */
|
||||
uint16_t halftone_alg;
|
||||
uint32_t halftone_parm1;
|
||||
uint32_t halftone_parm2;
|
||||
uint32_t color_encoding; /* = 0 (RGB) */
|
||||
uint32_t app_id;
|
||||
|
||||
/* internal only, not from file: */
|
||||
enum BmpInfoVer version;
|
||||
/* internal only, not from file: */
|
||||
enum BmpInfoVer version;
|
||||
};
|
||||
|
||||
#define IH_PROFILEDATA_OFFSET (14L + 112L)
|
||||
|
||||
#define MAX_ICCPROFILE_SIZE (1UL << 20)
|
||||
|
||||
|
||||
#define BI_RGB 0
|
||||
@@ -338,3 +349,9 @@ struct Bmpinfo {
|
||||
#define LCS_WINDOWS_COLOR_SPACE 0x57696e20 /* 'Win ' */
|
||||
#define PROFILE_LINKED 0x4c494e4b /* 'LINK' */
|
||||
#define PROFILE_EMBEDDED 0x4d424544 /* 'MBED' */
|
||||
|
||||
|
||||
#define LCS_GM_ABS_COLORIMETRIC 8
|
||||
#define LCS_GM_BUSINESS 1
|
||||
#define LCS_GM_GRAPHICS 2
|
||||
#define LCS_GM_IMAGES 3
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
|
||||
#define BMPLIB_LIB
|
||||
@@ -33,7 +34,6 @@
|
||||
#include "bmp-common.h"
|
||||
#include "huffman.h"
|
||||
#include "bmp-read.h"
|
||||
#include "reversebits.h"
|
||||
|
||||
|
||||
/*
|
||||
@@ -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;
|
||||
}
|
||||
@@ -499,29 +499,58 @@ static inline bool s_read_rgb_pixel(BMPREAD_R rp, union Pixel *restrict px)
|
||||
* s_read_indexed_line
|
||||
* - 1/2/4/8 bits non-RLE indexed
|
||||
*******************************************************/
|
||||
static inline bool s_read_n_bytes(BMPREAD_R rp, int n, unsigned long *restrict buff);
|
||||
static inline unsigned long s_bits_from_buffer(unsigned long buf, int size,
|
||||
int nbits, int used_bits);
|
||||
struct Buffer32 {
|
||||
uint32_t buffer;
|
||||
int n;
|
||||
};
|
||||
|
||||
static inline bool s_buffer32_fill(BMPREAD_R rp, struct Buffer32 *restrict buf)
|
||||
{
|
||||
int byte;
|
||||
|
||||
memset(buf, 0, sizeof *buf);
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (EOF == (byte = s_read_one_byte(rp))) {
|
||||
s_set_file_error(rp);
|
||||
return false;
|
||||
}
|
||||
buf->buffer <<= 8;
|
||||
buf->buffer |= byte;
|
||||
}
|
||||
buf->n = 32;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
static inline uint32_t s_buffer32_bits(struct Buffer32 *restrict buf, int nbits)
|
||||
{
|
||||
uint32_t result;
|
||||
|
||||
assert(nbits < 32);
|
||||
|
||||
result = buf->buffer >> (32 - nbits);
|
||||
buf->buffer = (buf->buffer << nbits) & 0xffffffffUL;
|
||||
buf->n -= nbits;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void s_read_indexed_line(BMPREAD_R rp, unsigned char *restrict line)
|
||||
{
|
||||
int bits_used, buffer_size, x = 0, v;
|
||||
bool done = false;
|
||||
unsigned long buffer;
|
||||
size_t offs;
|
||||
int x = 0, v;
|
||||
bool done = false;
|
||||
struct Buffer32 buffer;
|
||||
size_t offs;
|
||||
|
||||
/* setting the buffer size to the alignment takes care of padding bytes */
|
||||
buffer_size = 32;
|
||||
/* the buffer size of 32 bits takes care of padding bytes */
|
||||
|
||||
while (!done && s_read_n_bytes(rp, buffer_size / 8, &buffer)) {
|
||||
while (!done && s_buffer32_fill(rp, &buffer)) {
|
||||
|
||||
bits_used = 0;
|
||||
while (bits_used < buffer_size) {
|
||||
while (buffer.n >= rp->ih->bitcount) {
|
||||
|
||||
/* mask out the relevant bits for current pixel */
|
||||
v = (int) s_bits_from_buffer(buffer, buffer_size,
|
||||
rp->ih->bitcount, bits_used);
|
||||
bits_used += rp->ih->bitcount;
|
||||
v = (int) s_buffer32_bits(&buffer, rp->ih->bitcount);
|
||||
|
||||
if (v >= rp->palette->numcolors) {
|
||||
v = rp->palette->numcolors - 1;
|
||||
@@ -537,6 +566,7 @@ static void s_read_indexed_line(BMPREAD_R rp, unsigned char *restrict line)
|
||||
line[offs+2] = rp->palette->color[v].blue;
|
||||
s_int_to_result_format(rp, 8, line + offs);
|
||||
}
|
||||
|
||||
if (++x == rp->width) {
|
||||
done = true;
|
||||
break; /* discarding rest of buffer == padding */
|
||||
@@ -547,51 +577,6 @@ static void s_read_indexed_line(BMPREAD_R rp, unsigned char *restrict line)
|
||||
|
||||
|
||||
|
||||
/********************************************************
|
||||
* s_read_n_bytes
|
||||
*******************************************************/
|
||||
|
||||
static inline bool s_read_n_bytes(BMPREAD_R rp, int n, unsigned long *restrict buff)
|
||||
{
|
||||
int byte;
|
||||
|
||||
*buff = 0;
|
||||
while (n--) {
|
||||
if (EOF == (byte = s_read_one_byte(rp))) {
|
||||
s_set_file_error(rp);
|
||||
return false;
|
||||
}
|
||||
*buff <<= 8;
|
||||
*buff |= byte;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/********************************************************
|
||||
* s_bits_from_buffer
|
||||
*******************************************************/
|
||||
|
||||
static inline unsigned long s_bits_from_buffer(unsigned long buf, int size,
|
||||
int nbits, int used_bits)
|
||||
{
|
||||
unsigned long mask;
|
||||
int shift;
|
||||
|
||||
shift = size - (nbits + used_bits);
|
||||
|
||||
mask = (1U << nbits) - 1;
|
||||
mask <<= shift;
|
||||
|
||||
buf &= mask;
|
||||
buf >>= shift;
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/********************************************************
|
||||
* s_read_rle_line
|
||||
* - 4/8/24 bit RLE
|
||||
@@ -762,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;
|
||||
}
|
||||
@@ -787,7 +772,7 @@ static void s_read_huffman_line(BMPREAD_R rp, unsigned char *restrict line)
|
||||
if (rp->hufbuf_len == 0)
|
||||
break;
|
||||
|
||||
if ((rp->hufbuf & 0x00ff) == 0) {
|
||||
if ((rp->hufbuf & 0xff000000UL) == 0) {
|
||||
if (!s_huff_skip_eol(rp)) {
|
||||
rp->truncated = true;
|
||||
break;
|
||||
@@ -815,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);
|
||||
}
|
||||
}
|
||||
@@ -837,11 +822,11 @@ static bool s_huff_skip_eol(BMPREAD_R rp)
|
||||
huff_fillbuf(rp);
|
||||
continue;
|
||||
}
|
||||
while ((rp->hufbuf & 0x0001) == 0) {
|
||||
rp->hufbuf >>= 1;
|
||||
while ((rp->hufbuf & 0x80000000UL) == 0) {
|
||||
rp->hufbuf <<= 1;
|
||||
rp->hufbuf_len--;
|
||||
}
|
||||
rp->hufbuf >>= 1;
|
||||
rp->hufbuf <<= 1;
|
||||
rp->hufbuf_len--;
|
||||
return true;
|
||||
}
|
||||
@@ -858,12 +843,12 @@ static bool s_huff_find_eol(BMPREAD_R rp)
|
||||
huff_fillbuf (rp);
|
||||
while (rp->hufbuf_len > 11)
|
||||
{
|
||||
if ((rp->hufbuf & 0x07ff) == 0) {
|
||||
rp->hufbuf >>= 11;
|
||||
if ((rp->hufbuf & 0xffe00000UL) == 0) {
|
||||
rp->hufbuf <<= 11;
|
||||
rp->hufbuf_len -= 11;
|
||||
return s_huff_skip_eol (rp);
|
||||
}
|
||||
rp->hufbuf >>= 1;
|
||||
rp->hufbuf <<= 1;
|
||||
rp->hufbuf_len -= 1;
|
||||
if (rp->hufbuf_len < 12)
|
||||
huff_fillbuf (rp);
|
||||
@@ -922,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;
|
||||
@@ -953,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.");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
432
bmp-read.c
432
bmp-read.c
@@ -53,13 +53,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)
|
||||
@@ -95,7 +95,6 @@ static bool s_read_info_header(BMPREAD_R rp);
|
||||
static bool s_is_bmptype_supported(BMPREAD_R rp);
|
||||
static struct Palette* s_read_palette(BMPREAD_R rp);
|
||||
static bool s_read_colormasks(BMPREAD_R rp);
|
||||
static bool s_check_dimensions(BMPREAD_R rp);
|
||||
|
||||
API BMPRESULT bmpread_load_info(BMPHANDLE h)
|
||||
{
|
||||
@@ -107,7 +106,6 @@ API BMPRESULT bmpread_load_info(BMPHANDLE h)
|
||||
if (rp->getinfo_called)
|
||||
return rp->getinfo_return;
|
||||
|
||||
|
||||
if (!s_read_file_header(rp))
|
||||
goto abort;
|
||||
|
||||
@@ -121,12 +119,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;
|
||||
}
|
||||
@@ -135,14 +133,21 @@ API BMPRESULT bmpread_load_info(BMPHANDLE h)
|
||||
goto abort;
|
||||
|
||||
rp->width = (int) rp->ih->width;
|
||||
rp->height = (unsigned) rp->ih->height;
|
||||
|
||||
/* 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 = - (int64_t) rp->ih->height;
|
||||
rp->height = -rp->ih->height;
|
||||
} else {
|
||||
rp->height = rp->ih->height;
|
||||
}
|
||||
|
||||
|
||||
if (rp->ih->compression == BI_RLE4 ||
|
||||
rp->ih->compression == BI_RLE8 ||
|
||||
rp->ih->compression == BI_OS2_RLE24)
|
||||
@@ -150,19 +155,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;
|
||||
}
|
||||
@@ -176,7 +181,6 @@ API BMPRESULT bmpread_load_info(BMPHANDLE h)
|
||||
if (!(rp->palette = s_read_palette(rp)))
|
||||
goto abort;
|
||||
} else if (!rp->rle) { /* RGB */
|
||||
memset(&rp->cmask, 0, sizeof rp->cmask);
|
||||
if (!s_read_colormasks(rp))
|
||||
goto abort;
|
||||
|
||||
@@ -192,12 +196,9 @@ API BMPRESULT bmpread_load_info(BMPHANDLE h)
|
||||
if (!br_set_resultbits(rp))
|
||||
goto abort;
|
||||
|
||||
if (!s_check_dimensions(rp))
|
||||
goto abort;
|
||||
|
||||
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 {
|
||||
@@ -234,7 +235,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;
|
||||
@@ -246,7 +247,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;
|
||||
}
|
||||
@@ -266,6 +267,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;
|
||||
@@ -273,6 +281,115 @@ 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) {
|
||||
logerr(rp->c.log, "Must call bmpread_load_info() before loading ICC profile");
|
||||
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, 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)) {
|
||||
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
|
||||
*****************************************************************************/
|
||||
@@ -300,7 +417,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) {
|
||||
@@ -339,7 +456,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;
|
||||
}
|
||||
@@ -352,14 +469,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;
|
||||
}
|
||||
@@ -441,7 +558,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;
|
||||
@@ -533,7 +650,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;
|
||||
}
|
||||
@@ -564,7 +681,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);
|
||||
@@ -572,8 +689,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);
|
||||
}
|
||||
|
||||
@@ -588,7 +705,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;
|
||||
@@ -618,7 +735,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;
|
||||
}
|
||||
@@ -630,20 +747,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;
|
||||
@@ -669,7 +786,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;
|
||||
@@ -683,7 +800,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;
|
||||
@@ -693,7 +810,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;
|
||||
@@ -704,30 +821,6 @@ static bool s_is_bmptype_supported_indexed(BMPREAD_R rp)
|
||||
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* s_check_dimensions
|
||||
*****************************************************************************/
|
||||
|
||||
static bool s_check_dimensions(BMPREAD_R rp)
|
||||
{
|
||||
uint64_t npixels;
|
||||
size_t maxpixels;
|
||||
|
||||
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%d)",
|
||||
(int) rp->ih->width, (int) rp->ih->height);
|
||||
rp->lasterr = BMP_ERR_DIMENSIONS;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* s_read_palette
|
||||
*****************************************************************************/
|
||||
@@ -745,20 +838,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;
|
||||
}
|
||||
@@ -772,7 +865,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;
|
||||
@@ -784,7 +877,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;
|
||||
}
|
||||
@@ -797,10 +890,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);
|
||||
@@ -814,7 +907,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;
|
||||
}
|
||||
@@ -826,6 +919,107 @@ static struct Palette* s_read_palette(BMPREAD_R rp)
|
||||
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* br_set_resultbits
|
||||
*****************************************************************************/
|
||||
static bool s_check_dimensions(BMPREAD_R rp);
|
||||
|
||||
bool br_set_resultbits(BMPREAD_R rp)
|
||||
{
|
||||
int newbits, max_bits = 0, i;
|
||||
|
||||
if (!rp->ih->bitcount)
|
||||
return true;
|
||||
|
||||
switch (rp->result_format) {
|
||||
case BMP_FORMAT_FLOAT:
|
||||
if (rp->result_indexed) {
|
||||
logerr(rp->c.log, "Float is invalid number format for indexed image\n");
|
||||
rp->lasterr = BMP_ERR_FORMAT;
|
||||
return false;
|
||||
}
|
||||
newbits = 8 * sizeof (float);
|
||||
break;
|
||||
|
||||
case BMP_FORMAT_S2_13:
|
||||
if (rp->result_indexed) {
|
||||
logerr(rp->c.log, "s2.13 is invalid number format for indexed image\n");
|
||||
rp->lasterr = BMP_ERR_FORMAT;
|
||||
return false;
|
||||
}
|
||||
newbits = 16;
|
||||
break;
|
||||
|
||||
case BMP_FORMAT_INT:
|
||||
if (rp->ih->bitcount <= 8 || rp->rle)
|
||||
newbits = 8;
|
||||
else { /* RGB */
|
||||
for (i = 0; i < 4; i++) {
|
||||
max_bits = MAX(max_bits, rp->cmask.bits.value[i]);
|
||||
}
|
||||
newbits = 8;
|
||||
while (newbits < max_bits && newbits < 32) {
|
||||
newbits *= 2;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
logerr(rp->c.log, "Invalid number format %d\n", rp->result_format);
|
||||
rp->lasterr = BMP_ERR_FORMAT;
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
if (newbits != rp->result_bitsperchannel) {
|
||||
rp->dim_queried_bitsperchannel = false;
|
||||
rp->dimensions_queried = false;
|
||||
}
|
||||
rp->result_bitsperchannel = newbits;
|
||||
rp->result_bits_per_pixel = rp->result_bitsperchannel * rp->result_channels;
|
||||
rp->result_bytes_per_pixel = rp->result_bits_per_pixel / 8;
|
||||
|
||||
if (!s_check_dimensions(rp))
|
||||
return false;
|
||||
|
||||
rp->result_size = (size_t) rp->width * rp->height * rp->result_bytes_per_pixel;
|
||||
|
||||
if (rp->getinfo_called) {
|
||||
if (rp->insanity_limit && rp->result_size > rp->insanity_limit) {
|
||||
if (rp->getinfo_return == BMP_RESULT_OK) {
|
||||
logerr(rp->c.log, "file is insanely large");
|
||||
rp->lasterr = BMP_ERR_INSANE;
|
||||
rp->getinfo_return = BMP_RESULT_INSANE;
|
||||
}
|
||||
} else if (rp->getinfo_return == BMP_RESULT_INSANE)
|
||||
rp->getinfo_return = BMP_RESULT_OK;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* s_check_dimensions
|
||||
*****************************************************************************/
|
||||
|
||||
static bool s_check_dimensions(BMPREAD_R rp)
|
||||
{
|
||||
uint64_t npixels;
|
||||
size_t maxpixels;
|
||||
|
||||
npixels = (uint64_t) rp->width * rp->height;
|
||||
maxpixels = SIZE_MAX / rp->result_bytes_per_pixel;
|
||||
|
||||
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;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* s_read_colormasks
|
||||
*****************************************************************************/
|
||||
@@ -851,7 +1045,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;
|
||||
@@ -870,19 +1064,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;
|
||||
}
|
||||
@@ -892,80 +1086,6 @@ static bool s_read_colormasks(BMPREAD_R rp)
|
||||
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* br_set_resultbits
|
||||
*****************************************************************************/
|
||||
|
||||
bool br_set_resultbits(BMPREAD_R rp)
|
||||
{
|
||||
int newbits, max_bits = 0, i;
|
||||
|
||||
if (!rp->ih->bitcount)
|
||||
return true;
|
||||
|
||||
switch (rp->result_format) {
|
||||
case BMP_FORMAT_FLOAT:
|
||||
if (rp->result_indexed) {
|
||||
logerr(rp->log, "Float is invalid number format for indexed image\n");
|
||||
rp->lasterr = BMP_ERR_FORMAT;
|
||||
return false;
|
||||
}
|
||||
newbits = 8 * sizeof (float);
|
||||
break;
|
||||
|
||||
case BMP_FORMAT_S2_13:
|
||||
if (rp->result_indexed) {
|
||||
logerr(rp->log, "s2.13 is invalid number format for indexed image\n");
|
||||
rp->lasterr = BMP_ERR_FORMAT;
|
||||
return false;
|
||||
}
|
||||
newbits = 16;
|
||||
break;
|
||||
|
||||
case BMP_FORMAT_INT:
|
||||
if (rp->ih->bitcount <= 8 || rp->rle)
|
||||
newbits = 8;
|
||||
else { /* RGB */
|
||||
for (i = 0; i < 4; i++) {
|
||||
max_bits = MAX(max_bits, rp->cmask.bits.value[i]);
|
||||
}
|
||||
newbits = 8;
|
||||
while (newbits < max_bits && newbits < 32) {
|
||||
newbits *= 2;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
logerr(rp->log, "Invalid number format %d\n", rp->result_format);
|
||||
rp->lasterr = BMP_ERR_FORMAT;
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
if (newbits != rp->result_bitsperchannel) {
|
||||
rp->dim_queried_bitsperchannel = false;
|
||||
rp->dimensions_queried = false;
|
||||
}
|
||||
rp->result_bitsperchannel = newbits;
|
||||
rp->result_bits_per_pixel = rp->result_bitsperchannel * rp->result_channels;
|
||||
rp->result_bytes_per_pixel = rp->result_bits_per_pixel / 8;
|
||||
|
||||
rp->result_size = (size_t) rp->width * rp->height * rp->result_bytes_per_pixel;
|
||||
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");
|
||||
rp->lasterr = BMP_ERR_INSANE;
|
||||
rp->getinfo_return = BMP_RESULT_INSANE;
|
||||
}
|
||||
} else if (rp->getinfo_return == BMP_RESULT_INSANE)
|
||||
rp->getinfo_return = BMP_RESULT_OK;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* s_read_masks_from_bitfields
|
||||
*****************************************************************************/
|
||||
@@ -976,7 +1096,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;
|
||||
@@ -987,10 +1107,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;
|
||||
@@ -1002,10 +1122,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;
|
||||
@@ -1054,7 +1174,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;
|
||||
}
|
||||
@@ -1141,11 +1261,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;
|
||||
}
|
||||
|
||||
@@ -1194,7 +1314,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;
|
||||
@@ -1286,10 +1406,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;
|
||||
|
||||
351
bmp-write.c
351
bmp-write.c
@@ -41,6 +41,7 @@ static void s_decide_outformat(BMPWRITE_R wp);
|
||||
static bool s_write_palette(BMPWRITE_R wp);
|
||||
static bool s_write_bmp_file_header(BMPWRITE_R wp);
|
||||
static bool s_write_bmp_info_header(BMPWRITE_R wp);
|
||||
static bool s_write_iccprofile(BMPWRITE_R wp);
|
||||
static inline int s_write_one_byte(int byte, BMPWRITE_R wp);
|
||||
static bool s_save_header(BMPWRITE_R wp);
|
||||
static bool s_try_saving_image_size(BMPWRITE_R wp);
|
||||
@@ -62,30 +63,31 @@ API BMPHANDLE bmpwrite_new(FILE *file)
|
||||
goto abort;
|
||||
}
|
||||
memset(wp, 0, sizeof *wp);
|
||||
wp->magic = HMAGIC_WRITE;
|
||||
wp->c.magic = HMAGIC_WRITE;
|
||||
|
||||
wp->rle_requested = BMP_RLE_NONE;
|
||||
wp->outorientation = BMP_ORIENT_BOTTOMUP;
|
||||
wp->source_format = BMP_FORMAT_INT;
|
||||
wp->huffman_fg_idx = 1;
|
||||
|
||||
if (!(wp->log = logcreate()))
|
||||
if (!(wp->c.log = logcreate()))
|
||||
goto abort;
|
||||
|
||||
if (!file) {
|
||||
logerr(wp->log, "Must supply file handle");
|
||||
logerr(wp->c.log, "Must supply file handle");
|
||||
goto abort;
|
||||
}
|
||||
|
||||
wp->file = file;
|
||||
|
||||
if (!(wp->fh = malloc(sizeof *wp->fh))) {
|
||||
logsyserr(wp->log, "allocating bmp file header");
|
||||
logsyserr(wp->c.log, "allocating bmp file header");
|
||||
goto abort;
|
||||
}
|
||||
memset(wp->fh, 0, sizeof *wp->fh);
|
||||
|
||||
if (!(wp->ih = malloc(sizeof *wp->ih))) {
|
||||
logsyserr(wp->log, "allocating bmp info header");
|
||||
logsyserr(wp->c.log, "allocating bmp info header");
|
||||
goto abort;
|
||||
}
|
||||
memset(wp->ih, 0, sizeof *wp->ih);
|
||||
@@ -125,13 +127,13 @@ API BMPRESULT bmpwrite_set_dimensions(BMPHANDLE h,
|
||||
return BMP_RESULT_ERROR;
|
||||
|
||||
if (!cm_is_one_of(3, source_bitsperchannel, 8, 16, 32)) {
|
||||
logerr(wp->log, "Invalid number of bits per channel: %d",
|
||||
logerr(wp->c.log, "Invalid number of bits per channel: %d",
|
||||
(int) source_bitsperchannel);
|
||||
return BMP_RESULT_ERROR;
|
||||
}
|
||||
|
||||
if (!cm_is_one_of(4, source_channels, 3, 4, 1, 2)) {
|
||||
logerr(wp->log, "Invalid number of channels: %d", (int) source_channels);
|
||||
logerr(wp->c.log, "Invalid number of channels: %d", (int) source_channels);
|
||||
return BMP_RESULT_ERROR;
|
||||
}
|
||||
|
||||
@@ -140,7 +142,7 @@ API BMPRESULT bmpwrite_set_dimensions(BMPHANDLE h,
|
||||
if (width > INT32_MAX || height > INT32_MAX ||
|
||||
width < 1 || height < 1 ||
|
||||
(uint64_t) width * height > SIZE_MAX / wp->source_bytes_per_pixel) {
|
||||
logerr(wp->log, "Invalid dimensions %ux%ux%u @ %ubits",
|
||||
logerr(wp->c.log, "Invalid dimensions %ux%ux%u @ %ubits",
|
||||
width, height, source_channels,
|
||||
source_bitsperchannel);
|
||||
return BMP_RESULT_ERROR;
|
||||
@@ -199,7 +201,7 @@ API BMPRESULT bmpwrite_set_output_bits(BMPHANDLE h, int red, int green, int blue
|
||||
cm_all_lessoreq_int(32, 4, red, green, blue, alpha) &&
|
||||
red + green + blue > 0 &&
|
||||
red + green + blue + alpha <= 32 )) {
|
||||
logerr(wp->log, "Invalid output bit depths specified: %d-%d-%d - %d",
|
||||
logerr(wp->c.log, "Invalid output bit depths specified: %d-%d-%d - %d",
|
||||
red, green, blue, alpha);
|
||||
wp->outbits_set = false;
|
||||
return BMP_RESULT_ERROR;
|
||||
@@ -235,7 +237,7 @@ API BMPRESULT bmpwrite_set_palette(BMPHANDLE h, int numcolors,
|
||||
return BMP_RESULT_ERROR;
|
||||
|
||||
if (wp->palette) {
|
||||
logerr(wp->log, "Palette already set. Cannot set twice");
|
||||
logerr(wp->c.log, "Palette already set. Cannot set twice");
|
||||
return BMP_RESULT_ERROR;
|
||||
}
|
||||
|
||||
@@ -243,14 +245,14 @@ API BMPRESULT bmpwrite_set_palette(BMPHANDLE h, int numcolors,
|
||||
return BMP_RESULT_ERROR;
|
||||
|
||||
if (numcolors < 2 || numcolors > 256) {
|
||||
logerr(wp->log, "Invalid number of colors for palette (%d)",
|
||||
logerr(wp->c.log, "Invalid number of colors for palette (%d)",
|
||||
numcolors);
|
||||
return BMP_RESULT_ERROR;
|
||||
}
|
||||
|
||||
memsize = sizeof *wp->palette + numcolors * sizeof wp->palette->color[0];
|
||||
if (!(wp->palette = malloc(memsize))) {
|
||||
logsyserr(wp->log, "Allocating palette");
|
||||
logsyserr(wp->c.log, "Allocating palette");
|
||||
return BMP_RESULT_ERROR;
|
||||
}
|
||||
memset(wp->palette, 0, memsize);
|
||||
@@ -267,6 +269,55 @@ API BMPRESULT bmpwrite_set_palette(BMPHANDLE h, int numcolors,
|
||||
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* bmpwrite_set_iccprofile
|
||||
*****************************************************************************/
|
||||
|
||||
API BMPRESULT bmpwrite_set_iccprofile(BMPHANDLE h, size_t size,
|
||||
const unsigned char *iccprofile)
|
||||
{
|
||||
BMPWRITE wp;
|
||||
|
||||
assert(MAX_ICCPROFILE_SIZE < INT_MAX);
|
||||
|
||||
if (!(wp = cm_write_handle(h)))
|
||||
return BMP_RESULT_ERROR;
|
||||
|
||||
if (s_check_already_saved(wp))
|
||||
return BMP_RESULT_ERROR;
|
||||
|
||||
if (!s_is_setting_compatible(wp, "iccprofile"))
|
||||
return BMP_RESULT_ERROR;
|
||||
|
||||
if (wp->iccprofile) {
|
||||
free(wp->iccprofile);
|
||||
wp->iccprofile = NULL;
|
||||
wp->iccprofile_size = 0;
|
||||
wp->ih->profilesize = 0;
|
||||
wp->ih->cstype = LCS_WINDOWS_COLOR_SPACE;
|
||||
}
|
||||
|
||||
if (size > MAX_ICCPROFILE_SIZE) {
|
||||
logerr(wp->c.log, "ICC profile is too large (%zuMB). Max is %luMB.",
|
||||
size >> 20, (unsigned long)(MAX_ICCPROFILE_SIZE >> 20));
|
||||
return BMP_RESULT_ERROR;
|
||||
}
|
||||
|
||||
if (!(wp->iccprofile = malloc(size))) {
|
||||
logsyserr(wp->c.log, "Allocating ICC profile");
|
||||
return BMP_RESULT_ERROR;
|
||||
}
|
||||
memcpy(wp->iccprofile, iccprofile, size);
|
||||
wp->iccprofile_size = (int) size;
|
||||
wp->ih->profilesize = size;
|
||||
wp->ih->cstype = PROFILE_EMBEDDED;
|
||||
wp->ih->intent = LCS_GM_GRAPHICS;
|
||||
|
||||
printf("ICC profile set\n");
|
||||
return BMP_RESULT_OK;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* bmpwrite_set_orientation
|
||||
*****************************************************************************/
|
||||
@@ -284,7 +335,7 @@ API BMPRESULT bmpwrite_set_orientation(BMPHANDLE h, enum BmpOrient orientation)
|
||||
switch (orientation) {
|
||||
case BMP_ORIENT_TOPDOWN:
|
||||
if (wp->rle_requested != BMP_RLE_NONE) {
|
||||
logerr(wp->log, "Topdown is invalid with RLE BMPs");
|
||||
logerr(wp->c.log, "Topdown is invalid with RLE BMPs");
|
||||
return BMP_RESULT_ERROR;
|
||||
}
|
||||
break;
|
||||
@@ -294,7 +345,7 @@ API BMPRESULT bmpwrite_set_orientation(BMPHANDLE h, enum BmpOrient orientation)
|
||||
break;
|
||||
|
||||
default:
|
||||
logerr(wp->log, "Invalid orientation (%d)", (int) orientation);
|
||||
logerr(wp->c.log, "Invalid orientation (%d)", (int) orientation);
|
||||
return BMP_RESULT_ERROR;
|
||||
}
|
||||
|
||||
@@ -322,7 +373,7 @@ API BMPRESULT bmpwrite_set_rle(BMPHANDLE h, enum BmpRLEtype type)
|
||||
return BMP_RESULT_ERROR;
|
||||
|
||||
if (!cm_is_one_of(3, (int) type, (int) BMP_RLE_NONE, (int) BMP_RLE_AUTO, (int) BMP_RLE_RLE8)) {
|
||||
logerr(wp->log, "Invalid RLE type specified (%d)", (int) type);
|
||||
logerr(wp->c.log, "Invalid RLE type specified (%d)", (int) type);
|
||||
return BMP_RESULT_ERROR;
|
||||
}
|
||||
|
||||
@@ -346,8 +397,8 @@ API BMPRESULT bmpwrite_set_resolution(BMPHANDLE h, int xdpi, int ydpi)
|
||||
if (s_check_already_saved(wp))
|
||||
return BMP_RESULT_ERROR;
|
||||
|
||||
wp->ih->xpelspermeter = (LONG) (100.0 / 2.54 * xdpi + 0.5);
|
||||
wp->ih->ypelspermeter = (LONG) (100.0 / 2.54 * ydpi + 0.5);
|
||||
wp->ih->xpelspermeter = (int32_t) (100.0 / 2.54 * xdpi + 0.5);
|
||||
wp->ih->ypelspermeter = (int32_t) (100.0 / 2.54 * ydpi + 0.5);
|
||||
|
||||
return BMP_RESULT_OK;
|
||||
}
|
||||
@@ -439,6 +490,25 @@ API BMPRESULT bmpwrite_set_64bit(BMPHANDLE h)
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* bmpwrite_set_huffman_img_fg_idx
|
||||
*****************************************************************************/
|
||||
|
||||
API BMPRESULT bmpwrite_set_huffman_img_fg_idx(BMPHANDLE h, int idx)
|
||||
{
|
||||
BMPWRITE wp;
|
||||
|
||||
if (!(wp = cm_write_handle(h)))
|
||||
return BMP_RESULT_ERROR;
|
||||
|
||||
if (s_check_already_saved(wp))
|
||||
return BMP_RESULT_ERROR;
|
||||
|
||||
wp->huffman_fg_idx = !!idx;
|
||||
|
||||
return BMP_RESULT_OK;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* s_check_already_saved
|
||||
@@ -447,7 +517,7 @@ API BMPRESULT bmpwrite_set_64bit(BMPHANDLE h)
|
||||
static bool s_check_already_saved(BMPWRITE_R wp)
|
||||
{
|
||||
if (wp->saveimage_done) {
|
||||
logerr(wp->log, "Image already saved.");
|
||||
logerr(wp->c.log, "Image already saved.");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -458,10 +528,42 @@ static bool s_check_already_saved(BMPWRITE_R wp)
|
||||
/*****************************************************************************
|
||||
* s_is_setting_compatible
|
||||
*
|
||||
* setting: "outbits", "srcbits", "srcchannels",
|
||||
* "format", "indexed", "64bit", "rle"
|
||||
* setting: "outbits", "srcbits", "srcchannels", "palette", "allowhuffman"
|
||||
* "format", "indexed", "64bit", "rle", "iccprofile"
|
||||
*****************************************************************************/
|
||||
|
||||
static bool s_is_huffman_activated(BMPWRITE_R wp)
|
||||
{
|
||||
return wp->palette != NULL &&
|
||||
wp->palette_size <= 8 &&
|
||||
wp->rle_requested == BMP_RLE_AUTO &&
|
||||
wp->allow_huffman;
|
||||
}
|
||||
|
||||
static bool s_setting_activates_huffman(BMPWRITE_R wp, const char *setting, int value)
|
||||
{
|
||||
if (!strcmp(setting, "palette")) {
|
||||
if (value <= 2) {
|
||||
if (wp->rle_requested == BMP_RLE_AUTO && wp->allow_huffman)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (!strcmp(setting, "rle")) {
|
||||
if (value == BMP_RLE_AUTO) {
|
||||
if (wp->palette && wp->palette_size <= 8 && wp->allow_huffman)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (!strcmp(setting, "allowhuff")) {
|
||||
if (wp->palette && wp->palette_size <= 8 && wp->rle_requested == BMP_RLE_AUTO)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool s_is_setting_compatible(BMPWRITE_R wp, const char *setting, ...)
|
||||
{
|
||||
int channels, bits;
|
||||
@@ -475,49 +577,49 @@ static bool s_is_setting_compatible(BMPWRITE_R wp, const char *setting, ...)
|
||||
|
||||
if (!strcmp(setting, "outbits")) {
|
||||
if (wp->palette || wp->out64bit || (wp->rle_requested != BMP_RLE_NONE)) {
|
||||
logerr(wp->log, "output bits cannot be set with indexed, RLE, "
|
||||
logerr(wp->c.log, "output bits cannot be set with indexed, RLE, "
|
||||
"or 64bit BMPs");
|
||||
ret = false;
|
||||
}
|
||||
} else if (!strcmp(setting, "srcbits")) {
|
||||
bits = va_arg(args, int);
|
||||
if (wp->palette && bits != 8) {
|
||||
logerr(wp->log, "indexed images must be 8 bits (not %d)", bits);
|
||||
logerr(wp->c.log, "indexed images must be 8 bits (not %d)", bits);
|
||||
ret = false;
|
||||
} else if (wp->source_format == BMP_FORMAT_FLOAT && bits != 32) {
|
||||
logerr(wp->log, "float images must be 32 bits per channel (not %d)", bits);
|
||||
logerr(wp->c.log, "float images must be 32 bits per channel (not %d)", bits);
|
||||
ret = false;
|
||||
} else if (wp->source_format == BMP_FORMAT_S2_13 && bits != 16) {
|
||||
logerr(wp->log, "s2.13 images must be 16 bits per channel (not %d)", bits);
|
||||
logerr(wp->c.log, "s2.13 images must be 16 bits per channel (not %d)", bits);
|
||||
ret = false;
|
||||
}
|
||||
} else if (!strcmp(setting, "srcchannels")) {
|
||||
channels = va_arg(args, int);
|
||||
if (wp->palette && (channels != 1)) {
|
||||
logerr(wp->log, "Indexed images must have 1 channel (not %d)", channels);
|
||||
logerr(wp->c.log, "Indexed images must have 1 channel (not %d)", channels);
|
||||
ret = false;
|
||||
}
|
||||
if (wp->out64bit && (channels != 3 && channels != 4)) {
|
||||
logerr(wp->log, "64bit images must have 3 or 4 channels (not %d)", channels);
|
||||
logerr(wp->c.log, "64bit images must have 3 or 4 channels (not %d)", channels);
|
||||
ret = false;
|
||||
}
|
||||
} else if (!strcmp(setting, "indexed")) {
|
||||
if (wp->out64bit) {
|
||||
logerr(wp->log, "64bit BMPs cannot be indexed");
|
||||
logerr(wp->c.log, "64bit BMPs cannot be indexed");
|
||||
ret = false;
|
||||
}
|
||||
if (wp->outbits_set) {
|
||||
logerr(wp->log, "BMPs with specified channel bits cannot be indexed");
|
||||
logerr(wp->c.log, "BMPs with specified channel bits cannot be indexed");
|
||||
ret = false;
|
||||
}
|
||||
if (wp->source_format != BMP_FORMAT_INT) {
|
||||
logerr(wp->log, "Indexed image must have INT format (not %s)",
|
||||
logerr(wp->c.log, "Indexed image must have INT format (not %s)",
|
||||
cm_format_name(wp->source_format));
|
||||
ret = false;
|
||||
}
|
||||
if (wp->dimensions_set) {
|
||||
if (!(wp->source_channels == 1 && wp->source_bitsperchannel == 8)) {
|
||||
logerr (wp->log, "Indexed images must be 1 channel, 8 bits");
|
||||
logerr (wp->c.log, "Indexed images must be 1 channel, 8 bits");
|
||||
ret = false;
|
||||
}
|
||||
}
|
||||
@@ -526,23 +628,23 @@ static bool s_is_setting_compatible(BMPWRITE_R wp, const char *setting, ...)
|
||||
switch (format) {
|
||||
case BMP_FORMAT_FLOAT:
|
||||
if (wp->dimensions_set && wp->source_bitsperchannel != 32) {
|
||||
logerr(wp->log, "float cannot be %d bits per pixel",
|
||||
logerr(wp->c.log, "float cannot be %d bits per pixel",
|
||||
wp->source_bitsperchannel);
|
||||
ret = false;
|
||||
}
|
||||
if (wp->palette) {
|
||||
logerr(wp->log, "float cannot be used for indexed images");
|
||||
logerr(wp->c.log, "float cannot be used for indexed images");
|
||||
ret = false;
|
||||
}
|
||||
break;
|
||||
case BMP_FORMAT_S2_13:
|
||||
if (wp->dimensions_set && wp->source_bitsperchannel != 16) {
|
||||
logerr(wp->log, "s2.13 cannot be %d bits per pixel",
|
||||
logerr(wp->c.log, "s2.13 cannot be %d bits per pixel",
|
||||
wp->source_bitsperchannel);
|
||||
ret = false;
|
||||
}
|
||||
if (wp->palette) {
|
||||
logerr(wp->log, "s2.13 cannot be used for indexed images");
|
||||
logerr(wp->c.log, "s2.13 cannot be used for indexed images");
|
||||
ret = false;
|
||||
}
|
||||
break;
|
||||
@@ -554,21 +656,42 @@ static bool s_is_setting_compatible(BMPWRITE_R wp, const char *setting, ...)
|
||||
rle = va_arg(args, enum BmpRLEtype);
|
||||
if (rle == BMP_RLE_AUTO || rle == BMP_RLE_RLE8) {
|
||||
if (wp->outorientation != BMP_ORIENT_BOTTOMUP) {
|
||||
logerr(wp->log, "RLE is invalid with top-down BMPs");
|
||||
logerr(wp->c.log, "RLE is invalid with top-down BMPs");
|
||||
ret = false;
|
||||
}
|
||||
if (wp->iccprofile && s_setting_activates_huffman(wp, "rle", rle)) {
|
||||
logerr(wp->c.log, "Huffmann compression cannot be used when storing ICC profile");
|
||||
ret = false;
|
||||
}
|
||||
}
|
||||
|
||||
} else if (!strcmp(setting, "palette")) {
|
||||
int numcolors = va_arg(args, int);
|
||||
if (s_setting_activates_huffman(wp, "palette", numcolors)) {
|
||||
logerr(wp->c.log, "Huffmann compression cannot be used when storing ICC profile");
|
||||
ret = false;
|
||||
}
|
||||
} else if (!strcmp(setting, "allowhuffman")) {
|
||||
if (wp->iccprofile) {
|
||||
logerr(wp->c.log, "Huffmann compression cannot be used when storing ICC profile");
|
||||
ret = false;
|
||||
}
|
||||
} else if (!strcmp(setting, "iccprofile")) {
|
||||
if (s_is_huffman_activated(wp)) {
|
||||
logerr(wp->c.log, "Cannot store ICC profile with Huffmann compressed image");
|
||||
ret = false;
|
||||
}
|
||||
} else if (!strcmp(setting, "orientation")) {
|
||||
orientation = va_arg(args, enum BmpOrient);
|
||||
if (orientation == BMP_ORIENT_TOPDOWN) {
|
||||
if (wp->rle_requested != BMP_RLE_NONE) {
|
||||
logerr(wp->log, "RLE is invalid with top-down BMPs");
|
||||
logerr(wp->c.log, "RLE is invalid with top-down BMPs");
|
||||
ret = false;
|
||||
}
|
||||
}
|
||||
} else if (!strcmp(setting, "64bit")) {
|
||||
if (wp->palette) {
|
||||
logerr(wp->log, "Indexed images cannot be 64bit");
|
||||
logerr(wp->c.log, "Indexed images cannot be 64bit");
|
||||
ret = false;
|
||||
}
|
||||
}
|
||||
@@ -621,7 +744,7 @@ static void s_decide_outformat(BMPWRITE_R wp)
|
||||
wp->ih->compression = BI_RLE8;
|
||||
wp->ih->bitcount = 8;
|
||||
|
||||
} else if (wp->palette->numcolors > 2 || !wp->allow_huffman) {
|
||||
} else if (wp->palette->numcolors > 2 || !wp->allow_huffman || wp->iccprofile) {
|
||||
wp->rle = 4;
|
||||
wp->ih->compression = BI_RLE4;
|
||||
wp->ih->bitcount = 4;
|
||||
@@ -644,7 +767,7 @@ static void s_decide_outformat(BMPWRITE_R wp)
|
||||
}
|
||||
|
||||
} else if (wp->allow_rle24 && wp->source_channels == 3 &&
|
||||
wp->source_bitsperchannel && wp->rle_requested == BMP_RLE_AUTO) {
|
||||
wp->source_bitsperchannel && wp->rle_requested == BMP_RLE_AUTO && !wp->iccprofile) {
|
||||
wp->rle = 24;
|
||||
wp->ih->compression = BI_OS2_RLE24;
|
||||
wp->ih->bitcount = 24;
|
||||
@@ -684,16 +807,22 @@ static void s_decide_outformat(BMPWRITE_R wp)
|
||||
wp->ih->bitcount = (bitsum + 7) / 8 * 8;
|
||||
}
|
||||
|
||||
if (wp->iccprofile) {
|
||||
assert(wp->ih->version >= BMPINFO_V3);
|
||||
wp->ih->version = BMPINFO_V5;
|
||||
wp->ih->size = BMPIHSIZE_V5;
|
||||
}
|
||||
|
||||
if (wp->palette) {
|
||||
wp->ih->clrused = wp->palette->numcolors;
|
||||
} else {
|
||||
wp->outbytes_per_pixel = wp->ih->bitcount / 8;
|
||||
|
||||
if (wp->ih->version >= BMPINFO_V4 && !wp->out64bit) {
|
||||
wp->ih->redmask = (DWORD) (wp->cmask.mask.red << wp->cmask.shift.red);
|
||||
wp->ih->greenmask = (DWORD) (wp->cmask.mask.green << wp->cmask.shift.green);
|
||||
wp->ih->bluemask = (DWORD) (wp->cmask.mask.blue << wp->cmask.shift.blue);
|
||||
wp->ih->alphamask = (DWORD) (wp->cmask.mask.alpha << wp->cmask.shift.alpha);
|
||||
wp->ih->redmask = (uint32_t) (wp->cmask.mask.red << wp->cmask.shift.red);
|
||||
wp->ih->greenmask = (uint32_t) (wp->cmask.mask.green << wp->cmask.shift.green);
|
||||
wp->ih->bluemask = (uint32_t) (wp->cmask.mask.blue << wp->cmask.shift.blue);
|
||||
wp->ih->alphamask = (uint32_t) (wp->cmask.mask.alpha << wp->cmask.shift.alpha);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -703,7 +832,7 @@ static void s_decide_outformat(BMPWRITE_R wp)
|
||||
filesize = bitmapsize + BMPFHSIZE + wp->ih->size + wp->palette_size;
|
||||
|
||||
wp->fh->type = 0x4d42; /* "BM" */
|
||||
wp->fh->size = (DWORD) ((wp->rle || filesize > UINT32_MAX) ? 0 : filesize);
|
||||
wp->fh->size = (uint32_t) ((wp->rle || filesize > UINT32_MAX) ? 0 : filesize);
|
||||
wp->fh->offbits = BMPFHSIZE + wp->ih->size + wp->palette_size;
|
||||
|
||||
wp->ih->width = wp->width;
|
||||
@@ -712,7 +841,7 @@ static void s_decide_outformat(BMPWRITE_R wp)
|
||||
else
|
||||
wp->ih->height = -wp->height;
|
||||
wp->ih->planes = 1;
|
||||
wp->ih->sizeimage = (DWORD) ((wp->rle || bitmapsize > UINT32_MAX) ? 0 : bitmapsize);
|
||||
wp->ih->sizeimage = (uint32_t) ((wp->rle || bitmapsize > UINT32_MAX) ? 0 : bitmapsize);
|
||||
}
|
||||
|
||||
|
||||
@@ -737,7 +866,7 @@ API BMPRESULT bmpwrite_save_image(BMPHANDLE h, const unsigned char *image)
|
||||
return BMP_RESULT_ERROR;
|
||||
|
||||
if (wp->line_by_line) {
|
||||
logerr(wp->log, "Cannot switch from line-by-line to saving full image");
|
||||
logerr(wp->c.log, "Cannot switch from line-by-line to saving full image");
|
||||
return BMP_RESULT_ERROR;
|
||||
}
|
||||
|
||||
@@ -765,7 +894,7 @@ API BMPRESULT bmpwrite_save_image(BMPHANDLE h, const unsigned char *image)
|
||||
break;
|
||||
}
|
||||
if (!res) {
|
||||
logerr(wp->log, "failed saving line %d", y);
|
||||
logerr(wp->c.log, "failed saving line %d", y);
|
||||
return BMP_RESULT_ERROR;
|
||||
}
|
||||
}
|
||||
@@ -773,18 +902,21 @@ API BMPRESULT bmpwrite_save_image(BMPHANDLE h, const unsigned char *image)
|
||||
if (wp->rle > 1) {
|
||||
if (EOF == s_write_one_byte(0, wp) ||
|
||||
EOF == s_write_one_byte(1, wp)) {
|
||||
logsyserr(wp->log, "Writing RLE end-of-file marker");
|
||||
logsyserr(wp->c.log, "Writing RLE end-of-file marker");
|
||||
return BMP_RESULT_ERROR;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!(huff_encode_rtc(wp) && huff_flush(wp))) {
|
||||
logsyserr(wp->log, "Writing RTC end-of-file marker");
|
||||
logsyserr(wp->c.log, "Writing RTC end-of-file marker");
|
||||
return BMP_RESULT_ERROR;
|
||||
}
|
||||
}
|
||||
s_try_saving_image_size(wp);
|
||||
}
|
||||
if (wp->iccprofile)
|
||||
if (!s_write_iccprofile(wp))
|
||||
return BMP_RESULT_ERROR;
|
||||
|
||||
return BMP_RESULT_OK;
|
||||
}
|
||||
@@ -835,17 +967,21 @@ API BMPRESULT bmpwrite_save_line(BMPHANDLE h, const unsigned char *line)
|
||||
if (wp->rle > 1) {
|
||||
if (EOF == s_write_one_byte(0, wp) ||
|
||||
EOF == s_write_one_byte(1, wp)) {
|
||||
logsyserr(wp->log, "Writing RLE end-of-file marker");
|
||||
logsyserr(wp->c.log, "Writing RLE end-of-file marker");
|
||||
goto abort;
|
||||
}
|
||||
} else {
|
||||
if (!(huff_encode_rtc(wp) && huff_flush(wp))) {
|
||||
logsyserr(wp->log, "Writing RTC end-of-file marker");
|
||||
logsyserr(wp->c.log, "Writing RTC end-of-file marker");
|
||||
goto abort;
|
||||
}
|
||||
}
|
||||
s_try_saving_image_size(wp);
|
||||
}
|
||||
if (wp->iccprofile) {
|
||||
if (!s_write_iccprofile(wp))
|
||||
goto abort;
|
||||
}
|
||||
wp->saveimage_done = true;
|
||||
}
|
||||
|
||||
@@ -864,30 +1000,30 @@ abort:
|
||||
static bool s_save_header(BMPWRITE_R wp)
|
||||
{
|
||||
if (wp->saveimage_done || wp->line_by_line) {
|
||||
logerr(wp->log, "Image already saved.");
|
||||
logerr(wp->c.log, "Image already saved.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!wp->dimensions_set) {
|
||||
logerr(wp->log, "Must set dimensions before saving");
|
||||
logerr(wp->c.log, "Must set dimensions before saving");
|
||||
return false;
|
||||
}
|
||||
|
||||
s_decide_outformat(wp);
|
||||
|
||||
if (!s_write_bmp_file_header(wp)) {
|
||||
logsyserr(wp->log, "Writing BMP file header");
|
||||
logsyserr(wp->c.log, "Writing BMP file header");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!s_write_bmp_info_header(wp)) {
|
||||
logsyserr(wp->log, "Writing BMP info header");
|
||||
logsyserr(wp->c.log, "Writing BMP info header");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (wp->palette) {
|
||||
if (!s_write_palette(wp)) {
|
||||
logsyserr(wp->log, "Couldn't write palette");
|
||||
logsyserr(wp->c.log, "Couldn't write palette");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -908,7 +1044,7 @@ static bool s_save_header(BMPWRITE_R wp)
|
||||
* We ignore any errors quietly, as there's nothing we
|
||||
* can do and most (all?) readers ignore those sizes in
|
||||
* the header, anyway. Same goes for file/bitmap sizes
|
||||
* which are too big for the respective fields.
|
||||
* when they are too big for the respective fields.
|
||||
*****************************************************************************/
|
||||
|
||||
static bool s_try_saving_image_size(BMPWRITE_R wp)
|
||||
@@ -951,7 +1087,7 @@ static bool s_save_line_rgb(BMPWRITE_R wp, const unsigned char *line)
|
||||
bits_used += wp->ih->bitcount;
|
||||
if (bits_used == 8) {
|
||||
if (EOF == s_write_one_byte((int)bytes, wp)) {
|
||||
logsyserr(wp->log, "Writing image to BMP file");
|
||||
logsyserr(wp->c.log, "Writing image to BMP file");
|
||||
return false;
|
||||
}
|
||||
bytes = 0;
|
||||
@@ -964,7 +1100,7 @@ static bool s_save_line_rgb(BMPWRITE_R wp, const unsigned char *line)
|
||||
|
||||
for (i = 0; i < wp->outbytes_per_pixel; i++) {
|
||||
if (EOF == s_write_one_byte((bytes >> (8*i)) & 0xff, wp)) {
|
||||
logsyserr(wp->log, "Writing image to BMP file");
|
||||
logsyserr(wp->c.log, "Writing image to BMP file");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -974,7 +1110,7 @@ static bool s_save_line_rgb(BMPWRITE_R wp, const unsigned char *line)
|
||||
if (wp->palette && bits_used != 0) {
|
||||
bytes <<= 8 - bits_used;
|
||||
if (EOF == s_write_one_byte((int)bytes, wp)) {
|
||||
logsyserr(wp->log, "Writing image to BMP file");
|
||||
logsyserr(wp->c.log, "Writing image to BMP file");
|
||||
return false;
|
||||
}
|
||||
bits_used = 0;
|
||||
@@ -982,7 +1118,7 @@ static bool s_save_line_rgb(BMPWRITE_R wp, const unsigned char *line)
|
||||
|
||||
for (i = 0; i < wp->padding; i++) {
|
||||
if (EOF == s_write_one_byte(0, wp)) {
|
||||
logsyserr(wp->log, "Writing padding bytes to BMP file");
|
||||
logsyserr(wp->c.log, "Writing padding bytes to BMP file");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1001,7 +1137,7 @@ static bool s_save_line_rgb(BMPWRITE_R wp, const unsigned char *line)
|
||||
* repeat-run.
|
||||
*****************************************************************************/
|
||||
|
||||
static inline int s_length_of_runs(BMPWRITE_R wp, int x, int group, int minlen)
|
||||
static inline int s_length_of_runs(BMPWRITE_R wp, int group, int minlen)
|
||||
{
|
||||
int i, len = 0;
|
||||
|
||||
@@ -1023,7 +1159,7 @@ static bool s_save_line_rle(BMPWRITE_R wp, const unsigned char *line)
|
||||
{
|
||||
int i, j, k, x, l, dx, outbyte = 0;
|
||||
bool even;
|
||||
int small_number, minlen = 0;
|
||||
int small_number = 0, minlen = 0;
|
||||
|
||||
switch (wp->rle) {
|
||||
case 4:
|
||||
@@ -1045,7 +1181,7 @@ static bool s_save_line_rle(BMPWRITE_R wp, const unsigned char *line)
|
||||
|
||||
if (!wp->group) {
|
||||
if (!(wp->group = malloc(wp->width * sizeof *wp->group))) {
|
||||
logsyserr(wp->log, "allocating RLE buffer");
|
||||
logsyserr(wp->c.log, "allocating RLE buffer");
|
||||
goto abort;
|
||||
}
|
||||
}
|
||||
@@ -1102,7 +1238,7 @@ static bool s_save_line_rle(BMPWRITE_R wp, const unsigned char *line)
|
||||
* run for e.g. two repeated pixels and then restarting the literal
|
||||
* run at a cost of 2-4 bytes (depending on padding)
|
||||
*/
|
||||
if (i+l < wp->group_count && s_length_of_runs(wp, x+dx, i+l, minlen) <= small_number) {
|
||||
if (i+l < wp->group_count && s_length_of_runs(wp, i+l, minlen) <= small_number) {
|
||||
while (i+l < wp->group_count && wp->group[i+l] > (minlen-1) && dx + wp->group[i+l] < 255) {
|
||||
dx += wp->group[i+l];
|
||||
l++;
|
||||
@@ -1200,7 +1336,7 @@ abort:
|
||||
wp->group = NULL;
|
||||
wp->group_count = 0;
|
||||
}
|
||||
logsyserr(wp->log, "Writing RLE data to BMP file");
|
||||
logsyserr(wp->c.log, "Writing RLE data to BMP file");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1213,14 +1349,16 @@ abort:
|
||||
static bool s_save_line_huff(BMPWRITE_R wp, const unsigned char *line)
|
||||
{
|
||||
int x = 0, len;
|
||||
bool black = false;
|
||||
bool black = false, flipbits;
|
||||
|
||||
flipbits = !wp->huffman_fg_idx ^ wp->c.huffman_black_is_zero;
|
||||
|
||||
if (!huff_encode_eol(wp)) /* each line starts with eol */
|
||||
goto abort;
|
||||
|
||||
while (x < wp->width) {
|
||||
len = 0;
|
||||
while ((len < wp->width - x) && ((!!line[x + len]) == black))
|
||||
while ((len < wp->width - x) && ((!!line[x + len]) == (black ^ flipbits)))
|
||||
len++;
|
||||
if (!huff_encode(wp, len, black))
|
||||
goto abort;
|
||||
@@ -1229,7 +1367,7 @@ static bool s_save_line_huff(BMPWRITE_R wp, const unsigned char *line)
|
||||
}
|
||||
return true;
|
||||
abort:
|
||||
logsyserr(wp->log, "Writing 1-D Huffman data to BMP file");
|
||||
logsyserr(wp->c.log, "Writing 1-D Huffman data to BMP file");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1313,7 +1451,7 @@ static inline unsigned long long s_imgrgb_to_outbytes(BMPWRITE_R wp,
|
||||
break;
|
||||
|
||||
default:
|
||||
logerr(wp->log, "Panic! Bitdepth (%d) other than 8/16/32",
|
||||
logerr(wp->c.log, "Panic! Bitdepth (%d) other than 8/16/32",
|
||||
(int) wp->source_bitsperchannel);
|
||||
return (unsigned long long)-1;
|
||||
}
|
||||
@@ -1370,7 +1508,7 @@ static inline unsigned long long s_imgrgb_to_outbytes(BMPWRITE_R wp,
|
||||
break;
|
||||
|
||||
default:
|
||||
logerr(wp->log, "Panic, invalid source number format %d", wp->source_format);
|
||||
logerr(wp->c.log, "Panic, invalid source number format %d", (int) wp->source_format);
|
||||
return (unsigned long long) -1;
|
||||
}
|
||||
|
||||
@@ -1395,9 +1533,9 @@ static inline uint16_t float_to_s2_13(double d)
|
||||
|
||||
d = round(d * 8192.0);
|
||||
|
||||
if (d >= 32768.0)
|
||||
if (d >= 32767.0)
|
||||
s2_13 = 0x7fff; /* max positive value */
|
||||
else if (d < -32768.0)
|
||||
else if (d <= -32768.0)
|
||||
s2_13 = 0x8000; /* min negative value */
|
||||
else
|
||||
s2_13 = (uint16_t) (0xffff & (int)d);
|
||||
@@ -1413,11 +1551,16 @@ static inline uint16_t float_to_s2_13(double d)
|
||||
|
||||
static bool s_write_palette(BMPWRITE_R wp)
|
||||
{
|
||||
int i, c;
|
||||
int i, c;
|
||||
bool reverse = false;
|
||||
|
||||
if (wp->rle == 1)
|
||||
reverse = !wp->huffman_fg_idx;
|
||||
|
||||
for (i = 0; i < wp->palette->numcolors; i++) {
|
||||
int idx = reverse ? wp->palette->numcolors - i - 1 : i;
|
||||
for (c = 0; c < 3; c++) {
|
||||
if (EOF == s_write_one_byte(wp->palette->color[i].value[2-c], wp))
|
||||
if (EOF == s_write_one_byte(wp->palette->color[idx].value[2-c], wp))
|
||||
return false;
|
||||
}
|
||||
if (EOF == s_write_one_byte(0, wp))
|
||||
@@ -1489,11 +1632,11 @@ static bool s_write_bmp_info_header(BMPWRITE_R wp)
|
||||
if (wp->ih->version == BMPINFO_OS22) {
|
||||
#ifdef DEBUG
|
||||
if (wp->ih->size < 40) {
|
||||
logerr(wp->log, "Panic! Invalid header size %d", (int) wp->ih->size);
|
||||
logerr(wp->c.log, "Panic! Invalid header size %d", (int) wp->ih->size);
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
for (int i = 0; (DWORD) i < wp->ih->size - 40; i++) {
|
||||
for (int i = 0; (uint32_t) i < wp->ih->size - 40; i++) {
|
||||
if (EOF == putc(0, wp->file))
|
||||
return false;
|
||||
wp->bytes_written++;
|
||||
@@ -1522,11 +1665,59 @@ static bool s_write_bmp_info_header(BMPWRITE_R wp)
|
||||
}
|
||||
wp->bytes_written += 68;
|
||||
|
||||
if (wp->ih->version == BMPINFO_V4)
|
||||
return true;
|
||||
|
||||
if (!(write_u32_le(wp->file, wp->ih->intent) &&
|
||||
write_u32_le(wp->file, 0) &&
|
||||
write_u32_le(wp->file, wp->iccprofile_size) &&
|
||||
write_u32_le(wp->file, wp->ih->reserved)))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* s_write_iccprofile
|
||||
*****************************************************************************/
|
||||
|
||||
static bool s_write_iccprofile(BMPWRITE_R wp)
|
||||
{
|
||||
uint32_t pos;
|
||||
|
||||
printf("Writing ICC profile\n");
|
||||
if (wp->ih->version < BMPINFO_V5 || !wp->iccprofile)
|
||||
return false;
|
||||
|
||||
pos = wp->bytes_written;
|
||||
|
||||
if (wp->iccprofile_size != fwrite(wp->iccprofile, 1, wp->iccprofile_size, wp->file)) {
|
||||
logsyserr(wp->c.log, "Error writing ICC profile to file");
|
||||
return false;
|
||||
}
|
||||
|
||||
wp->bytes_written += wp->iccprofile_size;
|
||||
|
||||
if (fseek(wp->file, IH_PROFILEDATA_OFFSET, SEEK_SET)) {
|
||||
logsyserr(wp->c.log, "Error writing ICC profile to file");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!write_u32_le(wp->file, pos))
|
||||
return false;
|
||||
|
||||
if (wp->bytes_written < (size_t) LONG_MAX)
|
||||
fseek(wp->file, wp->bytes_written, SEEK_SET);
|
||||
|
||||
printf("Wrote ICC profile\n");
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* s_write_one_byte
|
||||
*****************************************************************************/
|
||||
@@ -1549,7 +1740,7 @@ static inline int s_write_one_byte(int byte, BMPWRITE_R wp)
|
||||
|
||||
void bw_free(BMPWRITE wp)
|
||||
{
|
||||
wp->magic = 0;
|
||||
wp->c.magic = 0;
|
||||
|
||||
if (wp->group)
|
||||
free(wp->group);
|
||||
@@ -1559,8 +1750,8 @@ void bw_free(BMPWRITE wp)
|
||||
free(wp->ih);
|
||||
if (wp->fh)
|
||||
free(wp->fh);
|
||||
if (wp->log)
|
||||
logfree(wp->log);
|
||||
if (wp->c.log)
|
||||
logfree(wp->c.log);
|
||||
|
||||
free(wp);
|
||||
}
|
||||
|
||||
20
bmplib.h
20
bmplib.h
@@ -42,7 +42,7 @@
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct Bmphandle *BMPHANDLE;
|
||||
typedef union Bmphandle *BMPHANDLE;
|
||||
|
||||
|
||||
/*
|
||||
@@ -124,12 +124,10 @@ 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
|
||||
* 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 BITMAPCOREHEADER and OS22XBITMAPHEADER being v1 and v2).
|
||||
* I am going with BITMAPINFOHEADER = v3.
|
||||
*/
|
||||
enum BmpInfoVer {
|
||||
BMPINFO_CORE_OS21 = 1, /* 12 bytes */
|
||||
@@ -241,6 +239,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,12 +269,17 @@ APIDECL BMPRESULT bmpwrite_allow_rle24(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_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_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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
/* bmplib - gen-reversebits.c
|
||||
*
|
||||
* 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/>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
static int reverse(int val, int bits)
|
||||
{
|
||||
int mask;
|
||||
|
||||
bits /= 2;
|
||||
if (bits == 0)
|
||||
return val;
|
||||
mask = (1 << bits) - 1;
|
||||
return (reverse(val & mask, bits) << bits) | reverse(val >> bits, bits);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int reversed, i;
|
||||
FILE *file;
|
||||
const char *src_name = "reversebits.h";
|
||||
const char *this_name = "gen-reversebits.c";
|
||||
|
||||
if (argc == 2) {
|
||||
if (!(file = fopen(argv[1], "w"))) {
|
||||
perror(argv[1]);
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
file = stdout;
|
||||
}
|
||||
|
||||
fprintf(file, "/* bmplib - %s\n", src_name);
|
||||
fprintf(file, " *\n"
|
||||
" * Copyright (c) 2024, Rupert Weber.\n"
|
||||
" *\n"
|
||||
" * This file is part of bmplib.\n"
|
||||
" * bmplib is free software: you can redistribute it and/or modify\n"
|
||||
" * it under the terms of the GNU Lesser General Public License as\n"
|
||||
" * published by the Free Software Foundation, either version 3 of\n"
|
||||
" * the License, or (at your option) any later version.\n"
|
||||
" *\n");
|
||||
fprintf(file, " * This program is distributed in the hope that it will be useful,\n"
|
||||
" * but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
|
||||
" * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
|
||||
" * GNU Lesser General Public License for more details.\n"
|
||||
" *\n"
|
||||
" * You should have received a copy of the GNU Lesser General Public\n"
|
||||
" * License along with this library.\n"
|
||||
" * If not, see <https://www.gnu.org/licenses/>\n"
|
||||
" */\n\n");
|
||||
fprintf(file, "/* This file is auto-generated by %s */\n\n\n", this_name);
|
||||
|
||||
fprintf(file, "static const unsigned char reversebits[] = {\n\t");
|
||||
for (i = 0; i < 256; i++) {
|
||||
reversed = reverse(i, 8);
|
||||
fprintf(file, "0x%02x, ", reversed);
|
||||
if ((i + 1) % 8 == 0 && i < 255)
|
||||
fprintf(file, "\n\t");
|
||||
}
|
||||
fprintf(file, "\n};\n");
|
||||
return 0;
|
||||
}
|
||||
12
huffman.c
12
huffman.c
@@ -29,7 +29,6 @@
|
||||
#include "bmplib.h"
|
||||
#include "logging.h"
|
||||
#include "bmp-common.h"
|
||||
#include "reversebits.h"
|
||||
#include "huffman.h"
|
||||
#include "huffman-codes.h"
|
||||
|
||||
@@ -63,7 +62,7 @@ int huff_decode(BMPREAD_R rp, int black)
|
||||
}
|
||||
|
||||
result += nodebuffer[idx].value;
|
||||
rp->hufbuf >>= bits_used;
|
||||
rp->hufbuf <<= bits_used;
|
||||
rp->hufbuf_len -= bits_used;
|
||||
|
||||
} while (nodebuffer[idx].makeup && result < INT_MAX - 2560);
|
||||
@@ -85,12 +84,12 @@ static int s_findnode(uint32_t bits, int nbits, bool black, int *found)
|
||||
idx = black ? blackroot : whiteroot;
|
||||
|
||||
while (idx != -1 && !nodebuffer[idx].terminal && bits_used < nbits) {
|
||||
if (bits & 1)
|
||||
if (bits & 0x80000000UL)
|
||||
idx = nodebuffer[idx].r;
|
||||
else
|
||||
idx = nodebuffer[idx].l;
|
||||
bits_used++;
|
||||
bits >>= 1;
|
||||
bits <<= 1;
|
||||
}
|
||||
*found = idx;
|
||||
return idx != -1 ? bits_used : 0;
|
||||
@@ -110,8 +109,7 @@ void huff_fillbuf(BMPREAD_R rp)
|
||||
if (EOF == (byte = getc(rp->file)))
|
||||
break;
|
||||
rp->bytes_read++;
|
||||
byte = reversebits[byte];
|
||||
rp->hufbuf |= ((uint32_t)byte) << rp->hufbuf_len;
|
||||
rp->hufbuf |= (uint32_t)byte << (24 - rp->hufbuf_len);
|
||||
rp->hufbuf_len += 8;
|
||||
}
|
||||
}
|
||||
@@ -229,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++;
|
||||
|
||||
11
logging.c
11
logging.c
@@ -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;
|
||||
|
||||
27
meson.build
27
meson.build
@@ -1,12 +1,23 @@
|
||||
project('bmplib', 'c', default_options: ['c_std=c11'], version: '1.7.3')
|
||||
project('bmplib', 'c', default_options: ['c_std=c11', 'warning_level=3'], version: '1.7.5')
|
||||
|
||||
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)
|
||||
@@ -37,22 +48,15 @@ elif cc.sizeof('int') < 4
|
||||
error('sizeof(int) must be at least 32 bit.')
|
||||
endif
|
||||
|
||||
|
||||
gen_huffman = executable('gen-huffman', 'gen-huffman.c')
|
||||
huff_codes = custom_target('huffman-codes.h',
|
||||
output: 'huffman-codes.h',
|
||||
command: [gen_huffman, '@OUTPUT@'],
|
||||
)
|
||||
|
||||
gen_reversebits = executable('gen-reversebits', 'gen-reversebits.c')
|
||||
reversebits = custom_target('reversebits.h',
|
||||
output: 'reversebits.h',
|
||||
command: [gen_reversebits, '@OUTPUT@'],
|
||||
)
|
||||
|
||||
|
||||
bmplib = shared_library('bmp',
|
||||
[bmplib_sources, huff_codes[0], reversebits[0]],
|
||||
[bmplib_sources, huff_codes[0]],
|
||||
version: meson.project_version(),
|
||||
install: true,
|
||||
dependencies: m_dep,
|
||||
@@ -65,4 +69,3 @@ pkg_mod.generate(libraries: bmplib,
|
||||
filebase: 'libbmp',
|
||||
description: 'Library for reading/writing Windows BMP files.',
|
||||
)
|
||||
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
option('insanity_limit_mb', type: 'integer', min: 0, value: 500)
|
||||
option('sanitize', type: 'boolean', value: false)
|
||||
|
||||
Reference in New Issue
Block a user