136l_uint8 *cmapBuf, *fdata, *data, *bmpih_b;
137l_int16 bftype, depth, d;
138l_int32 offset, width, height, height_neg, xres, yres, spp;
139l_int32 compression, imagebytes, fdatabytes, cmapbytes, ncolors, maxcolors;
140l_int32 fdatabpl, extrabytes, filebpp, pixWpl, pixBpl, i, j, k;
142l_uint32 *line, *pixdata, *pword;
150 return (
PIX *)ERROR_PTR(
"cdata not defined", __func__, NULL);
152 return (
PIX *)ERROR_PTR(
"bmf size error", __func__, NULL);
156 bftype = bmpfh->
bfType[0] + ((l_int32)bmpfh->
bfType[1] << 8);
158 return (
PIX *)ERROR_PTR(
"not bmf format", __func__, NULL);
161 if (compression != 0)
162 return (
PIX *)ERROR_PTR(
"cannot read compressed BMP files",
167 offset += (l_int32)bmpfh->
bfOffBits[1] << 8;
168 offset += (l_int32)bmpfh->
bfOffBits[2] << 16;
169 offset += (l_uint32)bmpfh->
bfOffBits[3] << 24;
174 bmpih_b = (l_uint8 *)&bmpih;
175 ihbytes = bmpih_b[0] | ((l_int32)bmpih_b[1] << 8) |
176 ((l_int32)bmpih_b[2] << 16) | ((l_uint32)bmpih_b[3] << 24);
177 width = convertOnBigEnd32(bmpih.
biWidth);
178 height = convertOnBigEnd32(bmpih.
biHeight);
194 return (
PIX *)ERROR_PTR(
"width < 1", __func__, NULL);
195 if (width > L_MAX_ALLOWED_WIDTH)
196 return (
PIX *)ERROR_PTR(
"width too large", __func__, NULL);
197 if (height == 0 || height < -L_MAX_ALLOWED_HEIGHT ||
198 height > L_MAX_ALLOWED_HEIGHT)
199 return (
PIX *)ERROR_PTR(
"invalid height", __func__, NULL);
200 if (xres < 0 || xres > L_MAX_ALLOWED_RES ||
201 yres < 0 || yres > L_MAX_ALLOWED_RES)
202 return (
PIX *)ERROR_PTR(
"invalid resolution", __func__, NULL);
208 if (ihbytes != 40 && ihbytes != 108 && ihbytes != 124) {
209 L_ERROR(
"invalid ihbytes = %d; not in {40, 108, 124}\n",
213 npixels = 1LL * width * height;
214 if (npixels > L_MAX_ALLOWED_PIXELS)
215 return (
PIX *)ERROR_PTR(
"npixels too large", __func__, NULL);
216 if (depth != 1 && depth != 2 && depth != 4 && depth != 8 &&
217 depth != 16 && depth != 24 && depth != 32) {
218 L_ERROR(
"invalid depth = %d; not in {1, 2, 4, 8, 16, 24, 32}\n",
222 fdatabpl = 4 * ((1LL * width * depth + 31)/32);
223 fdatabytes = fdatabpl * height;
224 if (imagebytes != 0 && imagebytes != fdatabytes) {
225 L_ERROR(
"invalid imagebytes = %d; not equal to fdatabytes = %d\n",
226 __func__, imagebytes, fdatabytes);
238 if (ncolors < 0 || ncolors == 1)
239 return (
PIX *)ERROR_PTR(
"invalid: cmap size < 0 or 1", __func__, NULL);
240 if (ncolors > 0 && depth > 8)
241 return (
PIX *)ERROR_PTR(
"can't have cmap for d > 8", __func__, NULL);
242 maxcolors = (depth <= 8) ? 1 << depth : 0;
243 if (ncolors > maxcolors) {
244 L_ERROR(
"cmap too large for depth %d: ncolors = %d > maxcolors = %d\n",
245 __func__, depth, ncolors, maxcolors);
248 if (size != 1LL * offset + 1LL * fdatabytes)
249 return (
PIX *)ERROR_PTR(
"size incommensurate with image data",
255 if ((cmapBuf = (l_uint8 *)LEPT_CALLOC(ncolors,
sizeof(
RGBA_QUAD)))
257 return (
PIX *)ERROR_PTR(
"cmapBuf alloc fail", __func__, NULL );
266 d = (depth == 24) ? 32 : depth;
267 if ((pix = pixCreate(width, height, d)) == NULL) {
269 return (
PIX *)ERROR_PTR(
"pix not made", __func__, NULL);
271 pixSetXRes(pix, (l_int32)((l_float32)xres / 39.37 + 0.5));
272 pixSetYRes(pix, (l_int32)((l_float32)yres / 39.37 + 0.5));
273 pixSetInputFormat(pix, IFF_BMP);
274 pixWpl = pixGetWpl(pix);
278 else if (depth == 24)
287 cmap = pixcmapCreate(L_MIN(d, 8));
288 LEPT_FREE(cmap->
array);
289 cmap->
array = (
void *)cmapBuf;
290 cmap->
n = L_MIN(ncolors, 256);
291 for (i = 0; i < cmap->
n; i++)
292 pixcmapSetAlpha (cmap, i, 255);
294 if (pixSetColormap(pix, cmap)) {
296 return (
PIX *)ERROR_PTR(
"invalid colormap", __func__, NULL);
300 fdata = (l_uint8 *)cdata + offset;
301 pixdata = pixGetData(pix);
302 if (depth != 24 && depth != 32) {
303 data = (l_uint8 *)pixdata + pixBpl * (height - 1);
304 for (i = 0; i < height; i++) {
305 memcpy(data, fdata, fdatabpl);
350 filebpp = (depth == 24) ? 3 : 4;
351 extrabytes = fdatabpl - filebpp * width;
352 line = pixdata + pixWpl * (height - 1);
353 for (i = 0; i < height; i++) {
354 for (j = 0; j < width; j++) {
356 memcpy(&pel, fdata, filebpp);
358 *((l_uint8 *)pword +
COLOR_RED) = pel[2];
368 for (k = 0; k < extrabytes; k++) {
369 memcpy(&pel, fdata, 1);
377 pixEndianByteSwap(pix);
388 if (depth == 1 && cmap) {
389 L_INFO(
"removing opaque cmap from 1 bpp\n", __func__);
461l_uint8 *fdata, *data, *fmdata;
463l_int32 ncolors, val, stepsize, w, h, d, fdepth, xres, yres, valid;
464l_int32 pixWpl, pixBpl, extrabytes, spp, fBpl, fWpl, i, j, k;
466l_uint32 offbytes, fimagebytes;
467l_uint32 *line, *pword;
475 if (pfdata) *pfdata = NULL;
476 if (pfsize) *pfsize = 0;
478 return ERROR_INT(
"&fdata not defined", __func__, 1 );
480 return ERROR_INT(
"&fsize not defined", __func__, 1 );
482 return ERROR_INT(
"pixs not defined", __func__, 1);
485 if ((cmap = pixGetColormap(pixs)) != NULL) {
486 pixcmapIsValid(cmap, pixs, &valid);
488 return ERROR_INT(
"colormap is not valid", __func__, 1);
491 pixGetDimensions(pixs, &w, &h, &d);
492 spp = pixGetSpp(pixs);
493 if (spp != 1 && spp != 3 && spp != 4) {
494 L_ERROR(
"unsupported spp = %d\n", __func__, spp);
498 L_WARNING(
"2 bpp files can't be read; converting to 8 bpp\n",
500 pix = pixConvert2To8(pixs, 0, 85, 170, 255, 1);
502 }
else if (d == 24) {
503 pix = pixConvert24To32(pixs);
506 pix = pixCopy(NULL, pixs);
518 xres = (l_int32)(39.37 * (l_float32)pixGetXRes(pix) + 0.5);
519 yres = (l_int32)(39.37 * (l_float32)pixGetYRes(pix) + 0.5);
521 pixWpl = pixGetWpl(pix);
523 fWpl = (w * fdepth + 31) / 32;
525 fimagebytes = h * fBpl;
526 if (fimagebytes > 4LL * L_MAX_ALLOWED_PIXELS) {
528 return ERROR_INT(
"image data is too large", __func__, 1);
533 if (d == 32 || d == 16) {
536 }
else if ((cmap = pixGetColormap(pix))) {
537 ncolors = pixcmapGetCount(cmap);
539 cta = (l_uint8 *)cmap->
array;
542 cmaplen =
sizeof(bwmap);
544 cta = (l_uint8 *)bwmap;
549 cta = (l_uint8 *)LEPT_CALLOC(cmaplen, 1);
550 stepsize = 255 / (ncolors - 1);
551 for (i = 0, val = 0, pquad = (
RGBA_QUAD *)cta;
553 i++, val += stepsize, pquad++) {
561 if (pixGetColormap(pix) != NULL) {
563 pcmptr = (l_uint8 *)pixGetColormap(pix)->
array;
564 lept_stderr(
"Pix colormap[0] = %c%c%c%d\n",
565 pcmptr[0], pcmptr[1], pcmptr[2], pcmptr[3]);
566 lept_stderr(
"Pix colormap[1] = %c%c%c%d\n",
567 pcmptr[4], pcmptr[5], pcmptr[6], pcmptr[7]);
572 fsize = offbytes + fimagebytes;
573 fdata = (l_uint8 *)LEPT_CALLOC(fsize, 1);
581 bmpfh->
bfSize[0] = (l_uint8)(fsize >> 0);
582 bmpfh->
bfSize[1] = (l_uint8)(fsize >> 8);
583 bmpfh->
bfSize[2] = (l_uint8)(fsize >> 16);
584 bmpfh->
bfSize[3] = (l_uint8)(fsize >> 24);
585 bmpfh->
bfOffBits[0] = (l_uint8)(offbytes >> 0);
586 bmpfh->
bfOffBits[1] = (l_uint8)(offbytes >> 8);
587 bmpfh->
bfOffBits[2] = (l_uint8)(offbytes >> 16);
588 bmpfh->
bfOffBits[3] = (l_uint8)(offbytes >> 24);
592 bmpih.
biWidth = convertOnBigEnd32(w);
593 bmpih.
biHeight = convertOnBigEnd32(h);
594 bmpih.
biPlanes = convertOnBigEnd16(1);
597 bmpih.
biSizeImage = convertOnBigEnd32(fimagebytes);
600 bmpih.
biClrUsed = convertOnBigEnd32(ncolors);
607 if (heapcm) LEPT_FREE(cta);
612 if (fdepth == 1 && cmap && ((l_uint8 *)(cmap->
array))[0] == 0x0) {
617 pixEndianByteSwap(pix);
620 fmdata = fdata + offbytes;
621 if (fdepth != 24 && fdepth != 32) {
622 data = (l_uint8 *)pixGetData(pix) + pixBpl * (h - 1);
623 for (i = 0; i < h; i++) {
624 memcpy(fmdata, data, fBpl);
640 extrabytes = fBpl - spp * w;
641 line = pixGetData(pix) + pixWpl * (h - 1);
642 for (i = 0; i < h; i++) {
643 for (j = 0; j < w; j++) {
645 pel[2] = *((l_uint8 *)pword +
COLOR_RED);
650 memcpy(fmdata, &pel, spp);
654 for (k = 0; k < extrabytes; k++) {
655 memcpy(fmdata, &pel, 1);