43 #include "config_auto.h" 47 #include "allheaders.h" 58 RGBA_QUAD bwmap[2] = { {255,255,255,255}, {0,0,0,255} };
61 static const l_int32 L_MAX_ALLOWED_NUM_COLORS = 256;
64 static const l_int32 L_MAX_ALLOWED_WIDTH = 1000000;
65 static const l_int32 L_MAX_ALLOWED_HEIGHT = 1000000;
66 static const l_int64 L_MAX_ALLOWED_PIXELS = 400000000LL;
67 static const l_int32 L_MAX_ALLOWED_RES = 10000000;
96 PROCNAME(
"pixReadStreamBmp");
99 return (
PIX *)ERROR_PTR(
"fp not defined", procName, NULL);
104 return (
PIX *)ERROR_PTR(
"data not read", procName, NULL);
124 l_uint8 *cmapBuf, *fdata, *data;
125 l_int16 bftype, depth, d;
126 l_int32 offset, width, height, height_neg, xres, yres, compression, imagebytes;
127 l_int32 cmapbytes, cmapEntries;
128 l_int32 fdatabpl, extrabytes, pixWpl, pixBpl, i, j, k;
129 l_uint32 *line, *pixdata, *pword;
132 #if defined(__GNUC__) 134 #define bmpih (&bmph->bmpih) 141 PROCNAME(
"pixReadMemBmp");
144 return (
PIX *)ERROR_PTR(
"cdata not defined", procName, NULL);
146 return (
PIX *)ERROR_PTR(
"bmf size error", procName, NULL);
150 bftype = bmpfh->
bfType[0] + ((l_int32)bmpfh->
bfType[1] << 8);
152 return (
PIX *)ERROR_PTR(
"not bmf format", procName, NULL);
153 #if defined(__GNUC__) 154 bmph = (BMP_HEADER *)bmpfh;
159 if (compression != 0)
160 return (
PIX *)ERROR_PTR(
"cannot read compressed BMP files",
165 offset += (l_int32)bmpfh->
bfOffBits[1] << 8;
166 offset += (l_int32)bmpfh->
bfOffBits[2] << 16;
167 offset += (l_int32)bmpfh->
bfOffBits[3] << 24;
168 width = convertOnBigEnd32(bmpih->
biWidth);
169 height = convertOnBigEnd32(bmpih->
biHeight);
171 imagebytes = convertOnBigEnd32(bmpih->
biSizeImage);
185 return (
PIX *)ERROR_PTR(
"width < 1", procName, NULL);
186 if (width > L_MAX_ALLOWED_WIDTH)
187 return (
PIX *)ERROR_PTR(
"width too large", procName, NULL);
188 if (height == 0 || height < -L_MAX_ALLOWED_HEIGHT ||
189 height > L_MAX_ALLOWED_HEIGHT)
190 return (
PIX *)ERROR_PTR(
"invalid height", procName, NULL);
191 if (xres < 0 || xres > L_MAX_ALLOWED_RES ||
192 yres < 0 || yres > L_MAX_ALLOWED_RES)
193 return (
PIX *)ERROR_PTR(
"invalid resolution", procName, NULL);
199 npixels = 1LL * width * height;
200 if (npixels > L_MAX_ALLOWED_PIXELS)
201 return (
PIX *)ERROR_PTR(
"npixels too large", procName, NULL);
202 if (depth != 1 && depth != 2 && depth != 4 && depth != 8 &&
203 depth != 16 && depth != 24 && depth != 32)
204 return (
PIX *)ERROR_PTR(
"depth not in {1, 2, 4, 8, 16, 24, 32}",
206 fdatabpl = 4 * ((1LL * width * depth + 31)/32);
207 if (imagebytes != 0 && imagebytes != fdatabpl * height)
208 return (
PIX *)ERROR_PTR(
"invalid imagebytes", procName, NULL);
210 cmapEntries = cmapbytes /
sizeof(
RGBA_QUAD);
211 if (cmapEntries < 0 || cmapEntries == 1)
212 return (
PIX *)ERROR_PTR(
"invalid: cmap size < 0 or 1", procName, NULL);
213 if (cmapEntries > L_MAX_ALLOWED_NUM_COLORS)
214 return (
PIX *)ERROR_PTR(
"invalid cmap: too large", procName,NULL);
215 if (size != 1LL * offset + 1LL * fdatabpl * height)
216 return (
PIX *)ERROR_PTR(
"size incommensurate with image data",
221 if (cmapEntries > 0) {
222 if ((cmapBuf = (l_uint8 *)LEPT_CALLOC(cmapEntries,
sizeof(
RGBA_QUAD)))
224 return (
PIX *)ERROR_PTR(
"cmapBuf alloc fail", procName, NULL );
233 d = (depth == 24) ? 32 : depth;
234 if ((pix =
pixCreate(width, height, d)) == NULL) {
236 return (
PIX *)ERROR_PTR(
"pix not made", procName, NULL);
238 pixSetXRes(pix, (l_int32)((l_float32)xres / 39.37 + 0.5));
239 pixSetYRes(pix, (l_int32)((l_float32)yres / 39.37 + 0.5));
240 pixSetInputFormat(pix, IFF_BMP);
241 pixWpl = pixGetWpl(pix);
246 if (cmapEntries > 0) {
248 LEPT_FREE(cmap->
array);
249 cmap->
array = (
void *)cmapBuf;
250 cmap->
n = L_MIN(cmapEntries, 256);
251 for (i = 0; i < cmap->
n; i++)
257 fdata = (l_uint8 *)cdata + offset;
260 data = (l_uint8 *)pixdata + pixBpl * (height - 1);
261 for (i = 0; i < height; i++) {
262 memcpy(data, fdata, fdatabpl);
303 extrabytes = fdatabpl - 3 * width;
304 line = pixdata + pixWpl * (height - 1);
305 for (i = 0; i < height; i++) {
306 for (j = 0; j < width; j++) {
308 memcpy(&pel, fdata, 3);
310 *((l_uint8 *)pword +
COLOR_RED) = pel[2];
315 for (k = 0; k < extrabytes; k++) {
316 memcpy(&pel, fdata, 1);
338 if (depth == 1 && cmap) {
365 PROCNAME(
"pixWriteStreamBmp");
368 return ERROR_INT(
"stream not defined", procName, 1);
370 return ERROR_INT(
"pix not defined", procName, 1);
374 nbytes = fwrite(data, 1, size, fp);
377 return ERROR_INT(
"Write error", procName, 1);
410 l_uint8 *fdata, *data, *fmdata;
412 l_int32 ncolors, val, stepsize;
413 l_int32 w, h, d, fdepth, xres, yres;
414 l_int32 pixWpl, pixBpl, extrabytes, fBpl, fWpl, i, j, k;
416 l_uint32 offbytes, fimagebytes;
417 l_uint32 *line, *pword;
420 #if defined(__GNUC__) 422 #define bmpih (&bmph->bmpih) 430 PROCNAME(
"pixWriteMemBmp");
432 if (pfdata) *pfdata = NULL;
433 if (pfsize) *pfsize = 0;
435 return ERROR_INT(
"&fdata not defined", procName, 1 );
437 return ERROR_INT(
"&fsize not defined", procName, 1 );
439 return ERROR_INT(
"pixs not defined", procName, 1);
443 L_WARNING(
"2 bpp files can't be read; converting to 8 bpp\n", procName);
449 fdepth = (d == 32) ? 24 : d;
452 xres = (l_int32)(39.37 * (l_float32)pixGetXRes(pix) + 0.5);
453 yres = (l_int32)(39.37 * (l_float32)pixGetYRes(pix) + 0.5);
455 pixWpl = pixGetWpl(pix);
457 fWpl = (w * fdepth + 31) / 32;
459 fimagebytes = h * fBpl;
460 if (fimagebytes > 4LL * L_MAX_ALLOWED_PIXELS) {
462 return ERROR_INT(
"image data is too large", procName, 1);
467 if (d == 32 || d == 16) {
470 }
else if ((cmap = pixGetColormap(pix))) {
473 cta = (l_uint8 *)cmap->
array;
476 cmaplen =
sizeof(bwmap);
478 cta = (l_uint8 *)bwmap;
480 ncolors = 1 << fdepth;
483 cta = (l_uint8 *)LEPT_CALLOC(cmaplen, 1);
484 stepsize = 255 / (ncolors - 1);
485 for (i = 0, val = 0, pquad = (
RGBA_QUAD *)cta;
487 i++, val += stepsize, pquad++) {
496 pcmptr = (l_uint8 *)pixGetColormap(pix)->
array;
497 fprintf(stderr,
"Pix colormap[0] = %c%c%c%d\n",
498 pcmptr[0], pcmptr[1], pcmptr[2], pcmptr[3]);
499 fprintf(stderr,
"Pix colormap[1] = %c%c%c%d\n",
500 pcmptr[4], pcmptr[5], pcmptr[6], pcmptr[7]);
505 fsize = offbytes + fimagebytes;
506 fdata = (l_uint8 *)LEPT_CALLOC(fsize, 1);
514 bmpfh->
bfSize[0] = (l_uint8)(fsize >> 0);
515 bmpfh->
bfSize[1] = (l_uint8)(fsize >> 8);
516 bmpfh->
bfSize[2] = (l_uint8)(fsize >> 16);
517 bmpfh->
bfSize[3] = (l_uint8)(fsize >> 24);
518 bmpfh->
bfOffBits[0] = (l_uint8)(offbytes >> 0);
519 bmpfh->
bfOffBits[1] = (l_uint8)(offbytes >> 8);
520 bmpfh->
bfOffBits[2] = (l_uint8)(offbytes >> 16);
521 bmpfh->
bfOffBits[3] = (l_uint8)(offbytes >> 24);
524 #if defined(__GNUC__) 525 bmph = (BMP_HEADER *)bmpfh;
530 bmpih->
biWidth = convertOnBigEnd32(w);
531 bmpih->
biHeight = convertOnBigEnd32(h);
532 bmpih->
biPlanes = convertOnBigEnd16(1);
533 bmpih->
biBitCount = convertOnBigEnd16(fdepth);
534 bmpih->
biSizeImage = convertOnBigEnd32(fimagebytes);
537 bmpih->
biClrUsed = convertOnBigEnd32(ncolors);
543 if (heapcm) LEPT_FREE(cta);
548 if (fdepth == 1 && cmap && ((l_uint8 *)(cmap->
array))[0] == 0x0) {
556 fmdata = fdata + offbytes;
558 data = (l_uint8 *)
pixGetData(pix) + pixBpl * (h - 1);
559 for (i = 0; i < h; i++) {
560 memcpy(fmdata, data, fBpl);
573 extrabytes = fBpl - 3 * w;
575 for (i = 0; i < h; i++) {
576 for (j = 0; j < w; j++) {
578 pel[2] = *((l_uint8 *)pword +
COLOR_RED);
581 memcpy(fmdata, &pel, 3);
585 for (k = 0; k < extrabytes; k++) {
586 memcpy(fmdata, &pel, 1);
PIX * pixRemoveColormap(PIX *pixs, l_int32 type)
pixRemoveColormap()
l_ok pixWriteStreamBmp(FILE *fp, PIX *pix)
pixWriteStreamBmp()
l_ok pixWriteMemBmp(l_uint8 **pfdata, size_t *pfsize, PIX *pixs)
pixWriteMemBmp()
PIX * pixReadStreamBmp(FILE *fp)
pixReadStreamBmp()
PIX * pixReadMemBmp(const l_uint8 *cdata, size_t size)
pixReadMemBmp()
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
PIX * pixInvert(PIX *pixd, PIX *pixs)
pixInvert()
l_uint32 * pixGetData(PIX *pix)
pixGetData()
l_ok pixSetColormap(PIX *pix, PIXCMAP *colormap)
pixSetColormap()
PIXCMAP * pixcmapCreate(l_int32 depth)
pixcmapCreate()
PIX * pixFlipTB(PIX *pixd, PIX *pixs)
pixFlipTB()
l_ok pixEndianByteSwap(PIX *pixs)
pixEndianByteSwap()
void pixDestroy(PIX **ppix)
pixDestroy()
l_ok pixcmapSetAlpha(PIXCMAP *cmap, l_int32 index, l_int32 aval)
pixcmapSetAlpha()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
PIX * pixConvert2To8(PIX *pixs, l_uint8 val0, l_uint8 val1, l_uint8 val2, l_uint8 val3, l_int32 cmapflag)
pixConvert2To8()
l_int32 pixcmapGetCount(PIXCMAP *cmap)
pixcmapGetCount()
PIX * pixCopy(PIX *pixd, PIX *pixs)
pixCopy()