43 #include <config_auto.h>
47 #include "allheaders.h"
59 RGBA_QUAD bwmap[2] = { {255,255,255,255}, {0,0,0,255} };
62 static const l_int32 L_MAX_ALLOWED_WIDTH = 1000000;
63 static const l_int32 L_MAX_ALLOWED_HEIGHT = 1000000;
64 static const l_int64 L_MAX_ALLOWED_PIXELS = 400000000LL;
65 static const l_int32 L_MAX_ALLOWED_RES = 10000000;
95 return (
PIX *)ERROR_PTR(
"fp not defined", __func__, NULL);
100 return (
PIX *)ERROR_PTR(
"data not read", __func__, NULL);
136 l_uint8 *cmapBuf, *fdata, *data;
137 l_int16 bftype, depth, d;
138 l_int32 offset, ihbytes, width, height, height_neg, xres, yres;
139 l_int32 compression, imagebytes, fdatabytes, cmapbytes, ncolors, maxcolors;
140 l_int32 fdatabpl, extrabytes, pixWpl, pixBpl, i, j, k;
141 l_uint32 *line, *pixdata, *pword;
144 #if defined(__GNUC__)
146 #define bmpih (&bmph->bmpih)
154 return (
PIX *)ERROR_PTR(
"cdata not defined", __func__, NULL);
156 return (
PIX *)ERROR_PTR(
"bmf size error", __func__, NULL);
160 bftype = bmpfh->
bfType[0] + ((l_int32)bmpfh->
bfType[1] << 8);
162 return (
PIX *)ERROR_PTR(
"not bmf format", __func__, NULL);
163 #if defined(__GNUC__)
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);
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++)
297 return (
PIX *)ERROR_PTR(
"invalid colormap", __func__, NULL);
301 fdata = (l_uint8 *)cdata + offset;
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);
382 if (depth == 1 && cmap) {
383 L_INFO(
"removing opaque cmap from 1 bpp\n", __func__);
411 return ERROR_INT(
"stream not defined", __func__, 1);
413 return ERROR_INT(
"pix not defined", __func__, 1);
417 nbytes = fwrite(data, 1, size, fp);
420 return ERROR_INT(
"Write error", __func__, 1);
453 l_uint8 *fdata, *data, *fmdata;
455 l_int32 ncolors, val, stepsize, w, h, d, fdepth, xres, yres, valid;
456 l_int32 pixWpl, pixBpl, extrabytes, fBpl, fWpl, i, j, k;
458 l_uint32 offbytes, fimagebytes;
459 l_uint32 *line, *pword;
462 #if defined(__GNUC__)
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) {
485 return ERROR_INT(
"colormap is not valid", __func__, 1);
490 L_WARNING(
"2 bpp files can't be read; converting to 8 bpp\n", __func__);
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))) {
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;
548 pcmptr[0], pcmptr[1], pcmptr[2], pcmptr[3]);
550 pcmptr[4], pcmptr[5], pcmptr[6], pcmptr[7]);
555 fsize = offbytes + fimagebytes;
556 fdata = (l_uint8 *)LEPT_CALLOC(fsize, 1);
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);
574 #if defined(__GNUC__)
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) {
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;
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);
PIX * pixReadMemBmp(const l_uint8 *cdata, size_t size)
pixReadMemBmp()
l_ok pixWriteStreamBmp(FILE *fp, PIX *pix)
pixWriteStreamBmp()
PIX * pixReadStreamBmp(FILE *fp)
pixReadStreamBmp()
l_ok pixWriteMemBmp(l_uint8 **pfdata, size_t *pfsize, PIX *pixs)
pixWriteMemBmp()
l_ok pixcmapSetAlpha(PIXCMAP *cmap, l_int32 index, l_int32 aval)
pixcmapSetAlpha()
l_int32 pixcmapGetCount(const PIXCMAP *cmap)
pixcmapGetCount()
l_ok pixcmapIsValid(const PIXCMAP *cmap, PIX *pix, l_int32 *pvalid)
pixcmapIsValid()
PIXCMAP * pixcmapCreate(l_int32 depth)
pixcmapCreate()
l_uint32 * pixGetData(PIX *pix)
pixGetData()
l_ok pixSetColormap(PIX *pix, PIXCMAP *colormap)
pixSetColormap()
void pixDestroy(PIX **ppix)
pixDestroy()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
PIX * pixCopy(PIX *pixd, const PIX *pixs)
pixCopy()
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
l_ok pixEndianByteSwap(PIX *pixs)
pixEndianByteSwap()
PIX * pixInvert(PIX *pixd, PIX *pixs)
pixInvert()
@ REMOVE_CMAP_BASED_ON_SRC
struct RGBA_Quad RGBA_QUAD
PIX * pixConvert2To8(PIX *pixs, l_uint8 val0, l_uint8 val1, l_uint8 val2, l_uint8 val3, l_int32 cmapflag)
pixConvert2To8()
PIX * pixRemoveColormap(PIX *pixs, l_int32 type)
pixRemoveColormap()
PIX * pixFlipTB(PIX *pixd, PIX *pixs)
pixFlipTB()
void lept_stderr(const char *fmt,...)
lept_stderr()
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()