| 1 |
/****************************************************************************\ |
| 2 |
Part of the XeTeX typesetting system |
| 3 |
Copyright (c) 1994-2006 by SIL International |
| 4 |
|
| 5 |
SIL Author(s): Jonathan Kew |
| 6 |
|
| 7 |
This program is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 2 of the License, or |
| 10 |
(at your option) any later version. |
| 11 |
|
| 12 |
This program is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with this program; if not, write to the Free Software |
| 19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 20 |
\****************************************************************************/ |
| 21 |
|
| 22 |
/* this file is derived from the dvipdfmx project; |
| 23 |
the original header follows... */ |
| 24 |
|
| 25 |
/* $Header: /home/cvsroot/dvipdfmx/src/bmpimage.c,v 1.2 2004/07/27 12:08:46 hirata Exp $ |
| 26 |
|
| 27 |
This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks. |
| 28 |
|
| 29 |
Copyright (C) 2002 by Jin-Hwan Cho and Shunsaku Hirata, |
| 30 |
the dvipdfmx project team <[email protected]> |
| 31 |
|
| 32 |
Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]> |
| 33 |
|
| 34 |
This program is free software; you can redistribute it and/or modify |
| 35 |
it under the terms of the GNU General Public License as published by |
| 36 |
the Free Software Foundation; either version 2 of the License, or |
| 37 |
(at your option) any later version. |
| 38 |
|
| 39 |
This program is distributed in the hope that it will be useful, |
| 40 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 41 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 42 |
GNU General Public License for more details. |
| 43 |
|
| 44 |
You should have received a copy of the GNU General Public License |
| 45 |
along with this program; if not, write to the Free Software |
| 46 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 47 |
*/ |
| 48 |
|
| 49 |
/* |
| 50 |
* BMP SUPPORT: |
| 51 |
*/ |
| 52 |
|
| 53 |
#if HAVE_CONFIG_H |
| 54 |
#include <w2c/config.h> |
| 55 |
#endif |
| 56 |
|
| 57 |
#include "bmpimage.h" |
| 58 |
|
| 59 |
#define DIB_FILE_HEADER_SIZE 14 |
| 60 |
#define DIB_CORE_HEADER_SIZE 12 |
| 61 |
#define DIB_INFO_HEADER_SIZE 40 |
| 62 |
#define DIB_INFO_HEADER_SIZE2 64 |
| 63 |
#define DIB_INFO_HEADER_SIZE4 108 |
| 64 |
#define DIB_INFO_HEADER_SIZE5 124 |
| 65 |
|
| 66 |
#define DIB_COMPRESS_NONE 0 |
| 67 |
#define DIB_COMPRESS_RLE8 1 |
| 68 |
#define DIB_COMPRESS_RLE4 2 |
| 69 |
|
| 70 |
#define DIB_HEADER_SIZE_MAX (DIB_FILE_HEADER_SIZE+DIB_INFO_HEADER_SIZE5) |
| 71 |
|
| 72 |
static void |
| 73 |
WARN(const char *fmt, ...) |
| 74 |
{ |
| 75 |
va_list argp; |
| 76 |
|
| 77 |
fprintf(stderr, "** WARNING ** "); |
| 78 |
va_start(argp, fmt); |
| 79 |
vfprintf(stderr, fmt, argp); |
| 80 |
va_end(argp); |
| 81 |
fprintf(stderr, "\n"); |
| 82 |
} |
| 83 |
|
| 84 |
int |
| 85 |
check_for_bmp (FILE *fp) |
| 86 |
{ |
| 87 |
unsigned char sigbytes[2]; |
| 88 |
|
| 89 |
if (!fp) |
| 90 |
return 0; |
| 91 |
|
| 92 |
rewind(fp); |
| 93 |
if (fread(sigbytes, 1, sizeof(sigbytes), fp) != sizeof(sigbytes) || |
| 94 |
sigbytes[0] != 'B' || sigbytes[1] != 'M') |
| 95 |
return 0; |
| 96 |
else |
| 97 |
return 1; |
| 98 |
|
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
int |
| 103 |
bmp_scan_file(struct bmp_info *info, FILE *fp) |
| 104 |
{ |
| 105 |
unsigned char buf[DIB_HEADER_SIZE_MAX+4]; |
| 106 |
unsigned char *p; |
| 107 |
long offset, hsize; |
| 108 |
long psize; /* Bytes per palette color: 3 for OS2, 4 for Win */ |
| 109 |
unsigned short bit_count; /* Bits per pix */ |
| 110 |
int num_palette; |
| 111 |
unsigned long biXPelsPerMeter, biYPelsPerMeter; |
| 112 |
|
| 113 |
p = buf; |
| 114 |
|
| 115 |
rewind(fp); |
| 116 |
if (fread(buf, 1, DIB_FILE_HEADER_SIZE + 4, fp) |
| 117 |
!= DIB_FILE_HEADER_SIZE + 4) { |
| 118 |
WARN("Could not read BMP file header..."); |
| 119 |
} |
| 120 |
|
| 121 |
if (p[0] != 'B' || p[1] != 'M') { |
| 122 |
WARN("File not starting with \'B\' \'M\'... Not a BMP file?"); |
| 123 |
return -1; |
| 124 |
} |
| 125 |
p += 2; |
| 126 |
|
| 127 |
#define ULONG_LE(b) ((b)[0] + ((b)[1] << 8) +\ |
| 128 |
((b)[2] << 16) + ((b)[3] << 24)) |
| 129 |
#define USHORT_LE(b) ((b)[0] + ((b)[1] << 8)) |
| 130 |
|
| 131 |
/* ignore fsize */ p += 4; |
| 132 |
if (ULONG_LE(p) != 0) { |
| 133 |
WARN("Not a BMP file???"); |
| 134 |
return -1; |
| 135 |
} |
| 136 |
p += 4; |
| 137 |
offset = ULONG_LE(p); p += 4; |
| 138 |
|
| 139 |
/* info header */ |
| 140 |
hsize = ULONG_LE(p); p += 4; |
| 141 |
if (fread(p, sizeof(char), hsize - 4, fp) != hsize - 4) { |
| 142 |
WARN("Could not read BMP file header..."); |
| 143 |
return -1; |
| 144 |
} |
| 145 |
if (hsize == DIB_CORE_HEADER_SIZE) { |
| 146 |
info->width = USHORT_LE(p); p += 2; |
| 147 |
info->height = USHORT_LE(p); p += 2; |
| 148 |
info->xdpi = 72.0; /* assume 72 DPI */ |
| 149 |
info->ydpi = 72.0; /* assume 72 DPI */ |
| 150 |
if (USHORT_LE(p) != 1) { |
| 151 |
WARN("Unknown bcPlanes value in BMP COREHEADER."); |
| 152 |
return -1; |
| 153 |
} |
| 154 |
p += 2; |
| 155 |
bit_count = USHORT_LE(p); p += 2; |
| 156 |
psize = 3; |
| 157 |
} else if (hsize == DIB_INFO_HEADER_SIZE || |
| 158 |
hsize == DIB_INFO_HEADER_SIZE2 || |
| 159 |
hsize == DIB_INFO_HEADER_SIZE4 || |
| 160 |
hsize == DIB_INFO_HEADER_SIZE5) { |
| 161 |
info->width = ULONG_LE(p); p += 4; |
| 162 |
info->height = ULONG_LE(p); p += 4; |
| 163 |
if (USHORT_LE(p) != 1) { |
| 164 |
WARN("Unknown biPlanes value in BMP INFOHEADER."); |
| 165 |
return -1; |
| 166 |
} |
| 167 |
p += 2; |
| 168 |
bit_count = USHORT_LE(p); p += 2; |
| 169 |
/* ignore compression */ p += 4; |
| 170 |
/* ignore biSizeImage */ p += 4; |
| 171 |
biXPelsPerMeter = ULONG_LE(p); p += 4; |
| 172 |
biYPelsPerMeter = ULONG_LE(p); p += 4; |
| 173 |
info->xdpi = biXPelsPerMeter * 0.0254; /* convert pixels per meter to DPI */ |
| 174 |
info->ydpi = biYPelsPerMeter * 0.0254; |
| 175 |
if (info->height < 0) { |
| 176 |
info->height = -info->height; |
| 177 |
} |
| 178 |
psize = 4; |
| 179 |
} else { |
| 180 |
fprintf (stderr, "Unknown BMP header type.\n"); |
| 181 |
exit (1); |
| 182 |
return -1; /* never reaches here */ |
| 183 |
} |
| 184 |
|
| 185 |
if (bit_count < 24) { |
| 186 |
if (bit_count != 1 && |
| 187 |
bit_count != 4 && bit_count != 8) { |
| 188 |
WARN("Unsupported palette size: %ld", bit_count); |
| 189 |
return -1; |
| 190 |
} |
| 191 |
num_palette = (offset - hsize - DIB_FILE_HEADER_SIZE) / psize; |
| 192 |
info->bits_per_component = bit_count; |
| 193 |
info->num_components = 1; |
| 194 |
} else if (bit_count == 24) { /* full color */ |
| 195 |
num_palette = 1; /* dummy */ |
| 196 |
info->bits_per_component = 8; |
| 197 |
info->num_components = 3; |
| 198 |
} else { |
| 199 |
WARN("Unkown BMP bitCount: %ld", bit_count); |
| 200 |
return -1; |
| 201 |
} |
| 202 |
|
| 203 |
if (info->width == 0 || info->height == 0 || num_palette < 1) { |
| 204 |
WARN("Invalid BMP file: width=%ld, height=%ld, #palette=%d", |
| 205 |
info->width, info->height, num_palette); |
| 206 |
return -1; |
| 207 |
} |
| 208 |
|
| 209 |
return 0; |
| 210 |
} |
| 211 |
|