| 1 |
/* |
| 2 |
* bmp.c - reading and writing Windows BMP files |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "../base/mdjvucfg.h" |
| 6 |
#include <minidjvu/minidjvu.h> |
| 7 |
#include <stdio.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
typedef struct { |
| 12 |
uint32 file_size; /* File size in bytes */ |
| 13 |
uint32 reserved; |
| 14 |
uint32 offset; /* Offset to image data, bytes */ |
| 15 |
uint32 size; /* Header size in bytes */ |
| 16 |
uint32 width,height; /* Width and height of image */ |
| 17 |
uint16 planes; /* Number of color planes */ |
| 18 |
uint16 bits; /* Bits per pixel */ |
| 19 |
uint32 compression; /* Compression type */ |
| 20 |
uint32 imagesize; /* Image size in bytes */ |
| 21 |
uint32 xresolution,yresolution; /* Pixels per meter */ |
| 22 |
uint32 ncolors; /* Number of colors */ |
| 23 |
uint32 importantcolors; /* Important colors */ |
| 24 |
uint32 color_0; /* Color of 0 bits */ |
| 25 |
uint32 color_1; /* Color of 1 bits */ |
| 26 |
} Header; |
| 27 |
|
| 28 |
/* Reads are little-endian. */ |
| 29 |
|
| 30 |
static uint32 read_uint32(FILE *f) |
| 31 |
{ |
| 32 |
uint32 r = fgetc(f); |
| 33 |
r |= fgetc(f) << 8; |
| 34 |
r |= fgetc(f) << 16; |
| 35 |
return r | fgetc(f) << 24;; |
| 36 |
} |
| 37 |
|
| 38 |
static uint16 read_uint16(FILE *f) |
| 39 |
{ |
| 40 |
uint32 r = fgetc(f); |
| 41 |
return (uint16) (r | (fgetc(f) << 8)); |
| 42 |
} |
| 43 |
|
| 44 |
static void write_uint16(FILE *f, uint16 i) |
| 45 |
{ |
| 46 |
fputc(i, f); |
| 47 |
fputc(i >> 8, f); |
| 48 |
} |
| 49 |
|
| 50 |
static void write_uint32(FILE *f, uint32 i) |
| 51 |
{ |
| 52 |
fputc(i, f); |
| 53 |
fputc(i >> 8, f); |
| 54 |
fputc(i >> 16, f); |
| 55 |
fputc(i >> 24, f); |
| 56 |
} |
| 57 |
|
| 58 |
static void write_bmp_header(FILE *f, uint32 w, uint32 h, int resolution) |
| 59 |
{ |
| 60 |
/* Didn't use structures here for portability (concerning endianness) */ |
| 61 |
uint32 row_size = ((w + 31) & ~31) >> 3; /* padding to 32 bit boundary */ |
| 62 |
uint32 data_size = row_size * h; |
| 63 |
uint32 data_offset = 62; /* size of all auxiliary info in monochrome BMP */ |
| 64 |
uint32 dpm = resolution * 5000 / 127; /* per inch -> per meter */ |
| 65 |
fputc('B', f); |
| 66 |
fputc('M', f); |
| 67 |
write_uint32(f, data_size + data_offset); /* file size */ |
| 68 |
write_uint32(f, 0); /* reserved */ |
| 69 |
write_uint32(f, data_offset); |
| 70 |
write_uint32(f, 40); /* size of the rest of header */ |
| 71 |
write_uint32(f, w); |
| 72 |
write_uint32(f, h); |
| 73 |
write_uint16(f, 1); /* number of color planes */ |
| 74 |
write_uint16(f, 1); /* number of bits per pixel */ |
| 75 |
write_uint32(f, 0); /* compression flag */ |
| 76 |
write_uint32(f, data_size); /* data size (0 also possible here) */ |
| 77 |
write_uint32(f, dpm); |
| 78 |
write_uint32(f, dpm); |
| 79 |
write_uint32(f, 2); |
| 80 |
write_uint32(f, 0); |
| 81 |
write_uint32(f, 0); /* black color */ |
| 82 |
write_uint32(f, 0xFFFFFF); /* white color */ |
| 83 |
} |
| 84 |
|
| 85 |
static void read_bmp_header(FILE *f, Header *h) |
| 86 |
{ |
| 87 |
h->file_size = read_uint32(f); |
| 88 |
h->reserved = read_uint32(f); |
| 89 |
h->offset = read_uint32(f); |
| 90 |
h->size = read_uint32(f); |
| 91 |
h->width = read_uint32(f); |
| 92 |
h->height = read_uint32(f); |
| 93 |
h->planes = read_uint16(f); |
| 94 |
h->bits = read_uint16(f); |
| 95 |
h->compression = read_uint32(f); |
| 96 |
h->imagesize = read_uint32(f); |
| 97 |
h->xresolution = read_uint32(f); |
| 98 |
h->yresolution = read_uint32(f); |
| 99 |
h->ncolors = read_uint32(f); |
| 100 |
h->importantcolors = read_uint32(f); |
| 101 |
h->color_0 = read_uint32(f); |
| 102 |
h->color_1 = read_uint32(f); |
| 103 |
} |
| 104 |
|
| 105 |
static void save_DIB_bytes(mdjvu_bitmap_t bmp, FILE *f) |
| 106 |
{ |
| 107 |
int32 w = mdjvu_bitmap_get_width(bmp); |
| 108 |
int32 h = mdjvu_bitmap_get_height(bmp); |
| 109 |
int32 bytes_per_row = mdjvu_bitmap_get_packed_row_size(bmp); |
| 110 |
int32 DIB_row_size = ((w + 31) & ~31) >> 3; /* padding to 32 bit */ |
| 111 |
unsigned char *buf = (unsigned char *) malloc(DIB_row_size); |
| 112 |
uint32 *p; |
| 113 |
uint32 i; |
| 114 |
int mask; |
| 115 |
if (w & 7) |
| 116 |
mask = ~(0xFF >> (w & 7)); /* b % 8 is 1 -> 0x80; 7 - 0xFE */ |
| 117 |
else |
| 118 |
mask = ~0; |
| 119 |
|
| 120 |
for (i = h; i; i--) |
| 121 |
{ |
| 122 |
uint32 j; |
| 123 |
unsigned char *row = mdjvu_bitmap_access_packed_row(bmp, i - 1); |
| 124 |
memcpy(buf, row, bytes_per_row); |
| 125 |
for (j = DIB_row_size >> 2, p = (uint32 *) buf; j; j--, p++) |
| 126 |
{ |
| 127 |
*p = ~*p; /* BMP stores pixels inversely (0 - black, 1 - white) */ |
| 128 |
} |
| 129 |
|
| 130 |
if (bytes_per_row > 1) |
| 131 |
{ |
| 132 |
fwrite(buf, 1, bytes_per_row - 1, f); |
| 133 |
} |
| 134 |
fputc(buf[bytes_per_row - 1] & mask, f); |
| 135 |
for (j = DIB_row_size - bytes_per_row; j; j--) fputc(0, f); |
| 136 |
} |
| 137 |
free(buf); |
| 138 |
} |
| 139 |
|
| 140 |
MDJVU_IMPLEMENT int mdjvu_file_save_bmp(mdjvu_bitmap_t bmp, |
| 141 |
mdjvu_file_t file, |
| 142 |
int32 resolution, |
| 143 |
mdjvu_error_t *perr) |
| 144 |
{ |
| 145 |
FILE *f = (FILE *) file; |
| 146 |
write_bmp_header(f, mdjvu_bitmap_get_width(bmp), |
| 147 |
mdjvu_bitmap_get_height(bmp), resolution); |
| 148 |
save_DIB_bytes(bmp, f); |
| 149 |
return 1; |
| 150 |
} |
| 151 |
|
| 152 |
MDJVU_IMPLEMENT int mdjvu_save_bmp(mdjvu_bitmap_t bmp, |
| 153 |
const char *path, |
| 154 |
int32 resolution, |
| 155 |
mdjvu_error_t *perr) |
| 156 |
{ |
| 157 |
FILE *f = fopen(path, "wb"); |
| 158 |
int result; |
| 159 |
if (perr) *perr = NULL; |
| 160 |
if (!f) |
| 161 |
{ |
| 162 |
*perr = mdjvu_get_error(mdjvu_error_fopen_write); |
| 163 |
return 0; |
| 164 |
} |
| 165 |
result = mdjvu_file_save_bmp(bmp, (mdjvu_file_t) f, resolution, perr); |
| 166 |
fclose(f); |
| 167 |
return result; |
| 168 |
} |
| 169 |
|
| 170 |
static void invert_row(unsigned char *row, int32 bytes_per_row, int32 w) |
| 171 |
{ |
| 172 |
int32 i; |
| 173 |
for (i = 0; i < bytes_per_row; i++) |
| 174 |
row[i] = ~row[i]; |
| 175 |
|
| 176 |
/* Clear margin bits */ |
| 177 |
if (w & 7) |
| 178 |
row[bytes_per_row - 1] &= ~(0xFF >> (w & 7)); |
| 179 |
} |
| 180 |
|
| 181 |
#define CHECK(X) \ |
| 182 |
{ \ |
| 183 |
if (!(X)) \ |
| 184 |
{ \ |
| 185 |
if (perr) *perr = mdjvu_get_error(mdjvu_error_corrupted_bmp); \ |
| 186 |
return NULL; \ |
| 187 |
} \ |
| 188 |
} |
| 189 |
#define FFs 0xFFFFFF |
| 190 |
MDJVU_IMPLEMENT mdjvu_bitmap_t mdjvu_file_load_bmp(mdjvu_file_t file, mdjvu_error_t *perr) |
| 191 |
{ |
| 192 |
FILE *f = (FILE *) file; |
| 193 |
mdjvu_bitmap_t result; |
| 194 |
int32 w, h, y; |
| 195 |
int32 DIB_row_size, bytes_per_row; |
| 196 |
int invert; |
| 197 |
Header header; |
| 198 |
|
| 199 |
if (perr) *perr = NULL; |
| 200 |
|
| 201 |
CHECK(fgetc(f)=='B'); |
| 202 |
CHECK(fgetc(f)=='M'); |
| 203 |
read_bmp_header(f, &header); |
| 204 |
CHECK(header.compression == 0); |
| 205 |
CHECK(header.planes == 1); |
| 206 |
CHECK(header.bits == 1); |
| 207 |
CHECK(((header.color_0 & FFs) == 0 && (header.color_1 & FFs) == FFs) || |
| 208 |
((header.color_1 & FFs) == 0 && (header.color_0 & FFs) == FFs )); |
| 209 |
|
| 210 |
invert = (header.color_0 & FFs) == 0; |
| 211 |
w = header.width; |
| 212 |
h = header.height; |
| 213 |
result = mdjvu_bitmap_create(w, h); |
| 214 |
DIB_row_size = ((w + 31) & ~31) >> 3; /* padding to 32 bit */ |
| 215 |
bytes_per_row = mdjvu_bitmap_get_packed_row_size(result); |
| 216 |
|
| 217 |
for (y = h; y; y--) |
| 218 |
{ |
| 219 |
unsigned char *row = mdjvu_bitmap_access_packed_row(result, y - 1); |
| 220 |
int k; |
| 221 |
if (fread(row, bytes_per_row, 1, f) != 1) |
| 222 |
{ |
| 223 |
if (perr) *perr = mdjvu_get_error(mdjvu_error_io); |
| 224 |
return NULL; |
| 225 |
} |
| 226 |
|
| 227 |
for (k = 0; k < DIB_row_size - bytes_per_row; k++) |
| 228 |
fgetc(f); |
| 229 |
|
| 230 |
if (invert) |
| 231 |
invert_row(row, bytes_per_row, w); |
| 232 |
} |
| 233 |
|
| 234 |
return result; |
| 235 |
} |
| 236 |
|
| 237 |
MDJVU_IMPLEMENT mdjvu_bitmap_t mdjvu_load_bmp(const char *path, mdjvu_error_t *perr) |
| 238 |
{ |
| 239 |
FILE *f = fopen(path, "rb"); |
| 240 |
mdjvu_bitmap_t result; |
| 241 |
if (!f) |
| 242 |
{ |
| 243 |
if (perr) *perr = mdjvu_get_error(mdjvu_error_fopen_read); |
| 244 |
return NULL; |
| 245 |
} |
| 246 |
if (perr) *perr = NULL; |
| 247 |
result = mdjvu_file_load_bmp((mdjvu_file_t) f, perr); |
| 248 |
fclose(f); |
| 249 |
return result; |
| 250 |
} |