136l_uint8 *cmapBuf, *fdata, *data;
137l_int16 bftype, depth, d;
138l_int32 offset, ihbytes, width, height, height_neg, xres, yres;
139l_int32 compression, imagebytes, fdatabytes, cmapbytes, ncolors, maxcolors;
140l_int32 fdatabpl, extrabytes, pixWpl, pixBpl, i, j, k;
141l_uint32 *line, *pixdata, *pword;
146#define bmpih (&bmph->bmpih)
154 return (
PIX *)ERROR_PTR(
"cdata not defined", __func__, NULL);
155 if (size <
sizeof(BMP_FH) +
sizeof(BMP_IH))
156 return (
PIX *)ERROR_PTR(
"bmf size error", __func__, NULL);
159 bmpfh = (BMP_FH *)cdata;
160 bftype = bmpfh->
bfType[0] + ((l_int32)bmpfh->
bfType[1] << 8);
162 return (
PIX *)ERROR_PTR(
"not bmf format", __func__, NULL);
164 bmph = (BMP_HEADER *)bmpfh;
169 if (compression != 0)
170 return (
PIX *)ERROR_PTR(
"cannot read compressed BMP files",
175 offset += (l_int32)bmpfh->
bfOffBits[1] << 8;
176 offset += (l_int32)bmpfh->
bfOffBits[2] << 16;
177 offset += (l_uint32)bmpfh->
bfOffBits[3] << 24;
181 ihbytes = convertOnBigEnd32(*(l_uint32 *)(bmpih));
182 width = convertOnBigEnd32(bmpih->
biWidth);
183 height = convertOnBigEnd32(bmpih->
biHeight);
185 imagebytes = convertOnBigEnd32(bmpih->
biSizeImage);
199 return (
PIX *)ERROR_PTR(
"width < 1", __func__, NULL);
200 if (width > L_MAX_ALLOWED_WIDTH)
201 return (
PIX *)ERROR_PTR(
"width too large", __func__, NULL);
202 if (height == 0 || height < -L_MAX_ALLOWED_HEIGHT ||
203 height > L_MAX_ALLOWED_HEIGHT)
204 return (
PIX *)ERROR_PTR(
"invalid height", __func__, NULL);
205 if (xres < 0 || xres > L_MAX_ALLOWED_RES ||
206 yres < 0 || yres > L_MAX_ALLOWED_RES)
207 return (
PIX *)ERROR_PTR(
"invalid resolution", __func__, NULL);
213 if (ihbytes != 40 && ihbytes != 108 && ihbytes != 124) {
214 L_ERROR(
"invalid ihbytes = %d; not in {40, 108, 124}\n",
218 npixels = 1LL * width * height;
219 if (npixels > L_MAX_ALLOWED_PIXELS)
220 return (
PIX *)ERROR_PTR(
"npixels too large", __func__, NULL);
222 return (
PIX *)ERROR_PTR(
"32 bpp rgba input data is not supported",
224 if (depth != 1 && depth != 2 && depth != 4 && depth != 8 &&
225 depth != 16 && depth != 24) {
226 L_ERROR(
"invalid depth = %d; not in {1, 2, 4, 8, 16, 24}\n",
230 fdatabpl = 4 * ((1LL * width * depth + 31)/32);
231 fdatabytes = fdatabpl * height;
232 if (imagebytes != 0 && imagebytes != fdatabytes) {
233 L_ERROR(
"invalid imagebytes = %d; not equal to fdatabytes = %d\n",
234 __func__, imagebytes, fdatabytes);
246 if (ncolors < 0 || ncolors == 1)
247 return (
PIX *)ERROR_PTR(
"invalid: cmap size < 0 or 1", __func__, NULL);
248 if (ncolors > 0 && depth > 8)
249 return (
PIX *)ERROR_PTR(
"can't have cmap for d > 8", __func__, NULL);
250 maxcolors = (depth <= 8) ? 1 << depth : 0;
251 if (ncolors > maxcolors) {
252 L_ERROR(
"cmap too large for depth %d: ncolors = %d > maxcolors = %d\n",
253 __func__, depth, ncolors, maxcolors);
256 if (size != 1LL * offset + 1LL * fdatabytes)
257 return (
PIX *)ERROR_PTR(
"size incommensurate with image data",
263 if ((cmapBuf = (l_uint8 *)LEPT_CALLOC(ncolors,
sizeof(
RGBA_QUAD)))
265 return (
PIX *)ERROR_PTR(
"cmapBuf alloc fail", __func__, NULL );
274 d = (depth == 24) ? 32 : depth;
275 if ((pix = pixCreate(width, height, d)) == NULL) {
277 return (
PIX *)ERROR_PTR(
"pix not made", __func__, NULL);
279 pixSetXRes(pix, (l_int32)((l_float32)xres / 39.37 + 0.5));
280 pixSetYRes(pix, (l_int32)((l_float32)yres / 39.37 + 0.5));
281 pixSetInputFormat(pix, IFF_BMP);
282 pixWpl = pixGetWpl(pix);
288 cmap = pixcmapCreate(L_MIN(d, 8));
289 LEPT_FREE(cmap->
array);
290 cmap->
array = (
void *)cmapBuf;
291 cmap->
n = L_MIN(ncolors, 256);
292 for (i = 0; i < cmap->
n; i++)
293 pixcmapSetAlpha (cmap, i, 255);
295 if (pixSetColormap(pix, cmap)) {
297 return (
PIX *)ERROR_PTR(
"invalid colormap", __func__, NULL);
301 fdata = (l_uint8 *)cdata + offset;
302 pixdata = pixGetData(pix);
304 data = (l_uint8 *)pixdata + pixBpl * (height - 1);
305 for (i = 0; i < height; i++) {
306 memcpy(data, fdata, fdatabpl);
347 extrabytes = fdatabpl - 3 * width;
348 line = pixdata + pixWpl * (height - 1);
349 for (i = 0; i < height; i++) {
350 for (j = 0; j < width; j++) {
352 memcpy(&pel, fdata, 3);
354 *((l_uint8 *)pword +
COLOR_RED) = pel[2];
362 for (k = 0; k < extrabytes; k++) {
363 memcpy(&pel, fdata, 1);
371 pixEndianByteSwap(pix);
382 if (depth == 1 && cmap) {
383 L_INFO(
"removing opaque cmap from 1 bpp\n", __func__);
453l_uint8 *fdata, *data, *fmdata;
455l_int32 ncolors, val, stepsize, w, h, d, fdepth, xres, yres, valid;
456l_int32 pixWpl, pixBpl, extrabytes, fBpl, fWpl, i, j, k;
458l_uint32 offbytes, fimagebytes;
459l_uint32 *line, *pword;
464#define bmpih (&bmph->bmpih)
472 if (pfdata) *pfdata = NULL;
473 if (pfsize) *pfsize = 0;
475 return ERROR_INT(
"&fdata not defined", __func__, 1 );
477 return ERROR_INT(
"&fsize not defined", __func__, 1 );
479 return ERROR_INT(
"pixs not defined", __func__, 1);
482 if ((cmap = pixGetColormap(pixs)) != NULL) {
483 pixcmapIsValid(cmap, pixs, &valid);
485 return ERROR_INT(
"colormap is not valid", __func__, 1);
488 pixGetDimensions(pixs, &w, &h, &d);
490 L_WARNING(
"2 bpp files can't be read; converting to 8 bpp\n", __func__);
491 pix = pixConvert2To8(pixs, 0, 85, 170, 255, 1);
494 pix = pixCopy(NULL, pixs);
496 if (pixGetSpp(pixs) == 4)
497 L_WARNING(
"transparency component for rgba pix is ignored; "
498 "assumed opaque\n", __func__);
499 fdepth = (d == 32) ? 24 : d;
502 xres = (l_int32)(39.37 * (l_float32)pixGetXRes(pix) + 0.5);
503 yres = (l_int32)(39.37 * (l_float32)pixGetYRes(pix) + 0.5);
505 pixWpl = pixGetWpl(pix);
507 fWpl = (w * fdepth + 31) / 32;
509 fimagebytes = h * fBpl;
510 if (fimagebytes > 4LL * L_MAX_ALLOWED_PIXELS) {
512 return ERROR_INT(
"image data is too large", __func__, 1);
517 if (d == 32 || d == 16) {
520 }
else if ((cmap = pixGetColormap(pix))) {
521 ncolors = pixcmapGetCount(cmap);
523 cta = (l_uint8 *)cmap->
array;
526 cmaplen =
sizeof(bwmap);
528 cta = (l_uint8 *)bwmap;
530 ncolors = 1 << fdepth;
533 cta = (l_uint8 *)LEPT_CALLOC(cmaplen, 1);
534 stepsize = 255 / (ncolors - 1);
535 for (i = 0, val = 0, pquad = (
RGBA_QUAD *)cta;
537 i++, val += stepsize, pquad++) {
546 pcmptr = (l_uint8 *)pixGetColormap(pix)->array;
547 lept_stderr(
"Pix colormap[0] = %c%c%c%d\n",
548 pcmptr[0], pcmptr[1], pcmptr[2], pcmptr[3]);
549 lept_stderr(
"Pix colormap[1] = %c%c%c%d\n",
550 pcmptr[4], pcmptr[5], pcmptr[6], pcmptr[7]);
555 fsize = offbytes + fimagebytes;
556 fdata = (l_uint8 *)LEPT_CALLOC(fsize, 1);
561 bmpfh = (BMP_FH *)fdata;
564 bmpfh->
bfSize[0] = (l_uint8)(fsize >> 0);
565 bmpfh->
bfSize[1] = (l_uint8)(fsize >> 8);
566 bmpfh->
bfSize[2] = (l_uint8)(fsize >> 16);
567 bmpfh->
bfSize[3] = (l_uint8)(fsize >> 24);
568 bmpfh->
bfOffBits[0] = (l_uint8)(offbytes >> 0);
569 bmpfh->
bfOffBits[1] = (l_uint8)(offbytes >> 8);
570 bmpfh->
bfOffBits[2] = (l_uint8)(offbytes >> 16);
571 bmpfh->
bfOffBits[3] = (l_uint8)(offbytes >> 24);
575 bmph = (BMP_HEADER *)bmpfh;
580 bmpih->
biWidth = convertOnBigEnd32(w);
581 bmpih->
biHeight = convertOnBigEnd32(h);
582 bmpih->
biPlanes = convertOnBigEnd16(1);
583 bmpih->
biBitCount = convertOnBigEnd16(fdepth);
584 bmpih->
biSizeImage = convertOnBigEnd32(fimagebytes);
587 bmpih->
biClrUsed = convertOnBigEnd32(ncolors);
593 if (heapcm) LEPT_FREE(cta);
598 if (fdepth == 1 && cmap && ((l_uint8 *)(cmap->
array))[0] == 0x0) {
603 pixEndianByteSwap(pix);
606 fmdata = fdata + offbytes;
608 data = (l_uint8 *)pixGetData(pix) + pixBpl * (h - 1);
609 for (i = 0; i < h; i++) {
610 memcpy(fmdata, data, fBpl);
623 extrabytes = fBpl - 3 * w;
624 line = pixGetData(pix) + pixWpl * (h - 1);
625 for (i = 0; i < h; i++) {
626 for (j = 0; j < w; j++) {
628 pel[2] = *((l_uint8 *)pword +
COLOR_RED);
631 memcpy(fmdata, &pel, 3);
635 for (k = 0; k < extrabytes; k++) {
636 memcpy(fmdata, &pel, 1);