C++ 如何在内存中将位图转换为PIX?
Tesseract似乎不能很好地处理位图,可以处理某些输入,但会破坏其他输入。同时,对与之前相同但采用leptonica格式PIX的输入执行良好 如何将内存中的位图转换为PIX 我想到的一个想法是使用leptonica的pixReadMem()C++ 如何在内存中将位图转换为PIX?,c++,image-processing,tiff,tesseract,leptonica,C++,Image Processing,Tiff,Tesseract,Leptonica,Tesseract似乎不能很好地处理位图,可以处理某些输入,但会破坏其他输入。同时,对与之前相同但采用leptonica格式PIX的输入执行良好 如何将内存中的位图转换为PIX 我想到的一个想法是使用leptonica的pixReadMem() 所以现在我需要找到一种方法,从内存中的位图构造内存中的TIFF。但是我不知道怎么做。好吧,我决定自己用一个快速脏端口来做。 我只是使用文件加载BMP函数,创建了伪fread/fseek,因为我真的很懒。 工作 如果有人需要,我会把它贴在下面。 lepto
所以现在我需要找到一种方法,从内存中的位图构造内存中的TIFF。但是我不知道怎么做。好吧,我决定自己用一个快速脏端口来做。 我只是使用文件加载BMP函数,创建了伪fread/fseek,因为我真的很懒。 工作 如果有人需要,我会把它贴在下面。 leptonica开发人员为什么没有在windows上正确地使用它,这令人费解
#include <cstring>
#include <leptonica/allheaders.h>
#include "leptonica_hack.h"
#include "bmp.h"
int fake_file_tracker = 0;
size_t fake_fread ( void * ptr, size_t size, size_t count, char * buffer, size_t buflen)
{
if (fake_file_tracker >= buflen)
{
return 0;
}
if (fake_file_tracker + size * count > buflen)
{
(void)memcpy(ptr, (void *)(buffer+fake_file_tracker), (size_t)(buflen - fake_file_tracker));
fake_file_tracker = buflen+1;
return (size_t)((buflen - fake_file_tracker) / size);
}
(void)memcpy(ptr, (void *)(buffer+fake_file_tracker), size * count);
fake_file_tracker += size * count;
return count;
}
int fake_fseek ( char * buffer, long int offset, int origin, size_t buflen)
{
if (origin + offset >= buflen)
return 0;
fake_file_tracker = origin + offset;
return 0;
}
/*!
* pixReadStreamBmp()
*
* Input: stream opened for read
* Return: pix, or null on error
*
* Notes:
* (1) Here are references on the bmp file format:
* http://en.wikipedia.org/wiki/BMP_file_format
* http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html
*/
PIX *
pixReadBmpFromBuffer(char *fp, size_t buflen)
{
fake_file_tracker = 0;
l_uint16 sval;
l_uint32 ival;
l_int16 bfType, bfSize, bfFill1, bfReserved1, bfReserved2;
l_int16 offset, bfFill2, biPlanes, depth, d;
l_int32 biSize, width, height, xres, yres, compression, ignore;
l_int32 imagebytes, biClrUsed, biClrImportant;
l_uint8 *colormapBuf = NULL;
l_int32 colormapEntries;
l_int32 fileBpl, extrabytes, readerror;
l_int32 pixWpl, pixBpl;
l_int32 i, j, k;
l_uint8 pel[4];
l_uint8 *data;
l_uint32 *line, *pword;
PIX *pix, *pixt;
PIXCMAP *cmap;
PROCNAME("pixReadBmpFromBuffer");
if (!fp)
return (PIX *)ERROR_PTR("fp not defined", procName, NULL);
/* Read bitmap file header */
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
bfType = convertOnBigEnd16(sval);
if (bfType != BMP_ID)
return (PIX *)ERROR_PTR("not bmf format", procName, NULL);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
bfSize = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
bfFill1 = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
bfReserved1 = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
bfReserved2 = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
offset = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
bfFill2 = convertOnBigEnd16(sval);
/* Read bitmap info header */
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
biSize = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
width = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
height = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
biPlanes = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
depth = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
compression = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
imagebytes = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
xres = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
yres = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
biClrUsed = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
biClrImportant = convertOnBigEnd32(ival);
if (compression != 0)
return (PIX *)ERROR_PTR("cannot read compressed BMP files",
procName,NULL);
/* A little sanity checking. It would be nice to check
* if the number of bytes in the file equals the offset to
* the data plus the imagedata, but this won't work when
* reading from memory, because fmemopen() doesn't implement
* ftell(). So we can't do that check. The imagebytes for
* uncompressed images is either 0 or the size of the file data.
* (The fact that it can be 0 is perhaps some legacy glitch). */
if (width < 1)
return (PIX *)ERROR_PTR("width < 1", procName,NULL);
if (height < 1)
return (PIX *)ERROR_PTR("height < 1", procName,NULL);
if (depth < 1 || depth > 32)
return (PIX *)ERROR_PTR("depth not in [1 ... 32]", procName,NULL);
fileBpl = 4 * ((width * depth + 31)/32);
if (imagebytes != 0 && imagebytes != fileBpl * height)
return (PIX *)ERROR_PTR("invalid imagebytes", procName,NULL);
if (offset < BMP_FHBYTES + BMP_IHBYTES)
return (PIX *)ERROR_PTR("invalid offset: too small", procName,NULL);
if (offset > BMP_FHBYTES + BMP_IHBYTES + 4 * 256)
return (PIX *)ERROR_PTR("invalid offset: too large", procName,NULL);
/* Handle the colormap */
colormapEntries = (offset - BMP_FHBYTES - BMP_IHBYTES) / sizeof(RGBA_QUAD);
if (colormapEntries > 0) {
if ((colormapBuf = (l_uint8 *)CALLOC(colormapEntries,
sizeof(RGBA_QUAD))) == NULL)
return (PIX *)ERROR_PTR("colormapBuf alloc fail", procName, NULL );
/* Read colormap */
if (fake_fread(colormapBuf, sizeof(RGBA_QUAD), colormapEntries, fp, buflen)
!= colormapEntries) {
FREE(colormapBuf);
return (PIX *)ERROR_PTR( "colormap read fail", procName, NULL);
}
}
/* Make a 32 bpp pix if depth is 24 bpp */
d = depth;
if (depth == 24)
d = 32;
if ((pix = pixCreate(width, height, d)) == NULL)
return (PIX *)ERROR_PTR( "pix not made", procName, NULL);
pixSetXRes(pix, (l_int32)((l_float32)xres / 39.37 + 0.5)); /* to ppi */
pixSetYRes(pix, (l_int32)((l_float32)yres / 39.37 + 0.5)); /* to ppi */
pixWpl = pixGetWpl(pix);
pixBpl = 4 * pixWpl;
cmap = NULL;
if (colormapEntries > 256)
L_WARNING("more than 256 colormap entries!", procName);
if (colormapEntries > 0) { /* import the colormap to the pix cmap */
cmap = pixcmapCreate(L_MIN(d, 8));
FREE(cmap->array); /* remove generated cmap array */
cmap->array = (void *)colormapBuf; /* and replace */
cmap->n = L_MIN(colormapEntries, 256);
}
pixSetColormap(pix, cmap);
/* Seek to the start of the bitmap in the file */
fake_fseek(fp, offset, 0, buflen);
if (depth != 24) { /* typ. 1 or 8 bpp */
data = (l_uint8 *)pixGetData(pix) + pixBpl * (height - 1);
for (i = 0; i < height; i++) {
if (fake_fread(data, 1, fileBpl, fp, buflen) != fileBpl) {
pixDestroy(&pix);
return (PIX *)ERROR_PTR("BMP read fail", procName, NULL);
}
data -= pixBpl;
}
}
else { /* 24 bpp file; 32 bpp pix
* Note: for bmp files, pel[0] is blue, pel[1] is green,
* and pel[2] is red. This is opposite to the storage
* in the pix, which puts the red pixel in the 0 byte,
* the green in the 1 byte and the blue in the 2 byte.
* Note also that all words are endian flipped after
* assignment on L_LITTLE_ENDIAN platforms.
*
* We can then make these assignments for little endians:
* SET_DATA_BYTE(pword, 1, pel[0]); blue
* SET_DATA_BYTE(pword, 2, pel[1]); green
* SET_DATA_BYTE(pword, 3, pel[2]); red
* This looks like:
* 3 (R) 2 (G) 1 (B) 0
* |-----------|------------|-----------|-----------|
* and after byte flipping:
* 3 2 (B) 1 (G) 0 (R)
* |-----------|------------|-----------|-----------|
*
* For big endians we set:
* SET_DATA_BYTE(pword, 2, pel[0]); blue
* SET_DATA_BYTE(pword, 1, pel[1]); green
* SET_DATA_BYTE(pword, 0, pel[2]); red
* This looks like:
* 0 (R) 1 (G) 2 (B) 3
* |-----------|------------|-----------|-----------|
* so in both cases we get the correct assignment in the PIX.
*
* Can we do a platform-independent assignment?
* Yes, set the bytes without using macros:
* *((l_uint8 *)pword) = pel[2]; red
* *((l_uint8 *)pword + 1) = pel[1]; green
* *((l_uint8 *)pword + 2) = pel[0]; blue
* For little endians, before flipping, this looks again like:
* 3 (R) 2 (G) 1 (B) 0
* |-----------|------------|-----------|-----------|
*/
readerror = 0;
extrabytes = fileBpl - 3 * width;
line = pixGetData(pix) + pixWpl * (height - 1);
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
pword = line + j;
if (fake_fread(&pel, 1, 3, fp, buflen) != 3)
readerror = 1;
*((l_uint8 *)pword + COLOR_RED) = pel[2];
*((l_uint8 *)pword + COLOR_GREEN) = pel[1];
*((l_uint8 *)pword + COLOR_BLUE) = pel[0];
}
if (extrabytes) {
for (k = 0; k < extrabytes; k++)
ignore = fake_fread(&pel, 1, 1, fp, buflen);
}
line -= pixWpl;
}
if (readerror) {
pixDestroy(&pix);
return (PIX *)ERROR_PTR("BMP read fail", procName, NULL);
}
}
pixEndianByteSwap(pix);
/* ----------------------------------------------
* The bmp colormap determines the values of black
* and white pixels for binary in the following way:
* if black = 1 (255), white = 0
* 255, 255, 255, 0, 0, 0, 0, 0
* if black = 0, white = 1 (255)
* 0, 0, 0, 0, 255, 255, 255, 0
* We have no need for a 1 bpp pix with a colormap!
* ---------------------------------------------- */
if (depth == 1 && cmap) {
/* L_INFO("Removing colormap", procName); */
pixt = pixRemoveColormap(pix, REMOVE_CMAP_BASED_ON_SRC);
pixDestroy(&pix);
pix = pixt; /* rename */
}
return pix;
}
#包括
#包括
#包括“leptonica_hack.h”
#包括“bmp.h”
int fake_file_tracker=0;
大小\u t伪\u fread(无效*ptr、大小\u t大小、大小\u t计数、字符*缓冲区、大小\u t buflen)
{
如果(假文件跟踪器>=buflen)
{
返回0;
}
如果(假文件跟踪器+大小*计数>buflen)
{
(void)memcpy(ptr,(void*)(缓冲区+伪文件跟踪器),(size_t)(buflen-伪文件跟踪器));
伪文件跟踪器=buflen+1;
返回(大小)(buflen-fake_文件跟踪器)/大小);
}
(void)memcpy(ptr,(void*)(缓冲区+伪文件跟踪器),大小*计数);
伪文件跟踪+=大小*计数;
返回计数;
}
int-fake_-fseek(字符*缓冲区、长整型偏移量、整型原点、大小\u-t buflen)
{
如果(原点+偏移量>=buflen)
返回0;
伪文件跟踪器=原点+偏移量;
返回0;
}
/*!
*pixReadStreamBmp()
*
*输入:为读取而打开的流
*返回:pix,或错误时为null
*
*注:
*(1)以下是bmp文件格式的参考:
* http://en.wikipedia.org/wiki/BMP_file_format
* http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html
*/
皮克斯*
PIXREADBMPFROMPBUFFER(字符*fp,大小\u t buflen)
{
伪文件跟踪器=0;
l_uint16 sval;
l_uint32 ival;
l_int16 bfType、bfSize、bfFill1、bfReserved1、bfReserved2;
l_int16偏移,bfFill2,双平面,深度,d;
l_int32大小、宽度、高度、X分辨率、Y分辨率、压缩、忽略;
l_int32 imagebytes,biClrUsed,biclrucement;
l_uint8*colormapBuf=NULL;
l_int32彩色地图条目;
l_int32 fileBpl,额外字节,readerror;
l_int32 pixWpl,pixBpl;
l_int32 i,j,k;
l_uint8 pel[4];
l_uint8*数据;
l_uint32*行,*pword;
PIX*PIX,*pixt;
PIXCMAP*cmap;
PROCNAME(“PIXREADBMPFROMPBUFFER”);
如果(!fp)
返回(PIX*)错误提示(“未定义fp”,procName,NULL);
/*读取位图文件头*/
忽略=假的(字符*)&sval,1,2,fp,buflen);
bfType=convertOnBigEnd16(sval);
如果(bfType!=BMP\u ID)
返回(PIX*)错误提示(“非bmf格式”,procName,NULL);
忽略=假的(字符*)&sval,1,2,fp,buflen);
bfSize=convertOnBigEnd16(sval);
忽略=假的(字符*)&sval,1,2,fp,buflen);
bfFill1=convertOnBigEnd16(sval);
忽略=假的(字符*)&sval,1,2,fp,buflen);
bfReserved1=convertOnBigEnd16(sval);
忽略=假的(字符*)&sval,1,2,fp,buflen);
bfReserved2=convertOnBigEnd16(sval);
忽略=假的(字符*)&sval,1,2,fp,buflen);
偏移量=convertOnBigEnd16(sval);
忽略=假的(字符*)&sval,1,2,fp,buflen);
bfFill2=convertOnBigEnd16(sval);
/*读取位图信息头*/
忽略=假的(字符*)&ival,1,4,fp,buflen);
biSize=convertOnBigEnd32(ival);
忽略=假的(字符*)&ival,1,4,fp,buflen);
宽度=convertOnBigEnd32(IVL);
忽略=假的(字符*)&ival,1,4,fp,buflen);
高度=convertOnBigEnd32(IVL);
忽略=假的(字符*)&sval,1,2,fp,buflen);
双平面=convertOnBigEnd16(sval);
忽略=假的(字符*)&sval,1,2,fp,buflen);
深度=convertOnBigEnd16(sval);
忽略=假的(字符*)&ival,1,4,fp,buflen);
压缩=convertOnBigEnd32(ival);
忽略=假的(字符*)&ival,1,4,fp,buflen);
imagebytes=convertOnBigEnd32(ival);
忽略=假的(字符*)&ival,1,4,fp,buflen);
xres=convertOnBigEnd32(ival);
忽略=假的(字符*)&ival,1,4,fp,buflen);
yres=convertOnBigEnd32(ival);
忽略=假的(字符*)&ival,1,4,fp,buflen);
BICLRUSE=convertOnBigEnd32(IVL);
忽略=假的(字符*)&ival,1,4,fp,buflen);
BICLR=转换BIGEND32(IVL);
如果(压缩!=0)
返回(PIX*)错误\u PTR(“无法读取压缩的BMP文件”,
procName,空);
/*做一点精神检查。检查一下就好了
*如果文件中的字节数等于
*数据加上imagedata,但当
*从内存中读取,因为fmemopen()无法实现
*ftell()。因此我们无法执行该检查。的imagebytes
*未压缩图像为0或文件数据的大小。
*(它可以是0这一事实可能是一些遗留问题)*/
如果(宽度<1)
返回(PIX*)错误_PTR(“宽度<1”,procName,NULL);
如果(高度<1)
返回(PIX*)错误_PTR(“高度<1”,procName,NULL);
如果(深度<1 | |深度>32)
返回(PIX*)错误_PTR(“深度不在[1…32]”,procName,NULL);
fileBpl=4*((宽度*深度+31)/32);
if(imagebytes!=0&&imagebytes!=fileBpl*高度)
返回(PIX*)错误\u PTR(“无效的imagebytes”,procName,NULL);
如果(偏移量BMP\U FHBYTES+BMP\U IHBYTES+4*256)
返回(PIX*)错误\u PTR(“无效偏移量:太大”,procName,NULL);
/*处理颜色映射*/
colormapEntries=(偏移量-BMP\u FHBYTES-BMP\u IHBYTES)/sizeof(RG)
#include <cstring>
#include <leptonica/allheaders.h>
#include "leptonica_hack.h"
#include "bmp.h"
int fake_file_tracker = 0;
size_t fake_fread ( void * ptr, size_t size, size_t count, char * buffer, size_t buflen)
{
if (fake_file_tracker >= buflen)
{
return 0;
}
if (fake_file_tracker + size * count > buflen)
{
(void)memcpy(ptr, (void *)(buffer+fake_file_tracker), (size_t)(buflen - fake_file_tracker));
fake_file_tracker = buflen+1;
return (size_t)((buflen - fake_file_tracker) / size);
}
(void)memcpy(ptr, (void *)(buffer+fake_file_tracker), size * count);
fake_file_tracker += size * count;
return count;
}
int fake_fseek ( char * buffer, long int offset, int origin, size_t buflen)
{
if (origin + offset >= buflen)
return 0;
fake_file_tracker = origin + offset;
return 0;
}
/*!
* pixReadStreamBmp()
*
* Input: stream opened for read
* Return: pix, or null on error
*
* Notes:
* (1) Here are references on the bmp file format:
* http://en.wikipedia.org/wiki/BMP_file_format
* http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html
*/
PIX *
pixReadBmpFromBuffer(char *fp, size_t buflen)
{
fake_file_tracker = 0;
l_uint16 sval;
l_uint32 ival;
l_int16 bfType, bfSize, bfFill1, bfReserved1, bfReserved2;
l_int16 offset, bfFill2, biPlanes, depth, d;
l_int32 biSize, width, height, xres, yres, compression, ignore;
l_int32 imagebytes, biClrUsed, biClrImportant;
l_uint8 *colormapBuf = NULL;
l_int32 colormapEntries;
l_int32 fileBpl, extrabytes, readerror;
l_int32 pixWpl, pixBpl;
l_int32 i, j, k;
l_uint8 pel[4];
l_uint8 *data;
l_uint32 *line, *pword;
PIX *pix, *pixt;
PIXCMAP *cmap;
PROCNAME("pixReadBmpFromBuffer");
if (!fp)
return (PIX *)ERROR_PTR("fp not defined", procName, NULL);
/* Read bitmap file header */
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
bfType = convertOnBigEnd16(sval);
if (bfType != BMP_ID)
return (PIX *)ERROR_PTR("not bmf format", procName, NULL);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
bfSize = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
bfFill1 = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
bfReserved1 = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
bfReserved2 = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
offset = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
bfFill2 = convertOnBigEnd16(sval);
/* Read bitmap info header */
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
biSize = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
width = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
height = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
biPlanes = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
depth = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
compression = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
imagebytes = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
xres = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
yres = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
biClrUsed = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
biClrImportant = convertOnBigEnd32(ival);
if (compression != 0)
return (PIX *)ERROR_PTR("cannot read compressed BMP files",
procName,NULL);
/* A little sanity checking. It would be nice to check
* if the number of bytes in the file equals the offset to
* the data plus the imagedata, but this won't work when
* reading from memory, because fmemopen() doesn't implement
* ftell(). So we can't do that check. The imagebytes for
* uncompressed images is either 0 or the size of the file data.
* (The fact that it can be 0 is perhaps some legacy glitch). */
if (width < 1)
return (PIX *)ERROR_PTR("width < 1", procName,NULL);
if (height < 1)
return (PIX *)ERROR_PTR("height < 1", procName,NULL);
if (depth < 1 || depth > 32)
return (PIX *)ERROR_PTR("depth not in [1 ... 32]", procName,NULL);
fileBpl = 4 * ((width * depth + 31)/32);
if (imagebytes != 0 && imagebytes != fileBpl * height)
return (PIX *)ERROR_PTR("invalid imagebytes", procName,NULL);
if (offset < BMP_FHBYTES + BMP_IHBYTES)
return (PIX *)ERROR_PTR("invalid offset: too small", procName,NULL);
if (offset > BMP_FHBYTES + BMP_IHBYTES + 4 * 256)
return (PIX *)ERROR_PTR("invalid offset: too large", procName,NULL);
/* Handle the colormap */
colormapEntries = (offset - BMP_FHBYTES - BMP_IHBYTES) / sizeof(RGBA_QUAD);
if (colormapEntries > 0) {
if ((colormapBuf = (l_uint8 *)CALLOC(colormapEntries,
sizeof(RGBA_QUAD))) == NULL)
return (PIX *)ERROR_PTR("colormapBuf alloc fail", procName, NULL );
/* Read colormap */
if (fake_fread(colormapBuf, sizeof(RGBA_QUAD), colormapEntries, fp, buflen)
!= colormapEntries) {
FREE(colormapBuf);
return (PIX *)ERROR_PTR( "colormap read fail", procName, NULL);
}
}
/* Make a 32 bpp pix if depth is 24 bpp */
d = depth;
if (depth == 24)
d = 32;
if ((pix = pixCreate(width, height, d)) == NULL)
return (PIX *)ERROR_PTR( "pix not made", procName, NULL);
pixSetXRes(pix, (l_int32)((l_float32)xres / 39.37 + 0.5)); /* to ppi */
pixSetYRes(pix, (l_int32)((l_float32)yres / 39.37 + 0.5)); /* to ppi */
pixWpl = pixGetWpl(pix);
pixBpl = 4 * pixWpl;
cmap = NULL;
if (colormapEntries > 256)
L_WARNING("more than 256 colormap entries!", procName);
if (colormapEntries > 0) { /* import the colormap to the pix cmap */
cmap = pixcmapCreate(L_MIN(d, 8));
FREE(cmap->array); /* remove generated cmap array */
cmap->array = (void *)colormapBuf; /* and replace */
cmap->n = L_MIN(colormapEntries, 256);
}
pixSetColormap(pix, cmap);
/* Seek to the start of the bitmap in the file */
fake_fseek(fp, offset, 0, buflen);
if (depth != 24) { /* typ. 1 or 8 bpp */
data = (l_uint8 *)pixGetData(pix) + pixBpl * (height - 1);
for (i = 0; i < height; i++) {
if (fake_fread(data, 1, fileBpl, fp, buflen) != fileBpl) {
pixDestroy(&pix);
return (PIX *)ERROR_PTR("BMP read fail", procName, NULL);
}
data -= pixBpl;
}
}
else { /* 24 bpp file; 32 bpp pix
* Note: for bmp files, pel[0] is blue, pel[1] is green,
* and pel[2] is red. This is opposite to the storage
* in the pix, which puts the red pixel in the 0 byte,
* the green in the 1 byte and the blue in the 2 byte.
* Note also that all words are endian flipped after
* assignment on L_LITTLE_ENDIAN platforms.
*
* We can then make these assignments for little endians:
* SET_DATA_BYTE(pword, 1, pel[0]); blue
* SET_DATA_BYTE(pword, 2, pel[1]); green
* SET_DATA_BYTE(pword, 3, pel[2]); red
* This looks like:
* 3 (R) 2 (G) 1 (B) 0
* |-----------|------------|-----------|-----------|
* and after byte flipping:
* 3 2 (B) 1 (G) 0 (R)
* |-----------|------------|-----------|-----------|
*
* For big endians we set:
* SET_DATA_BYTE(pword, 2, pel[0]); blue
* SET_DATA_BYTE(pword, 1, pel[1]); green
* SET_DATA_BYTE(pword, 0, pel[2]); red
* This looks like:
* 0 (R) 1 (G) 2 (B) 3
* |-----------|------------|-----------|-----------|
* so in both cases we get the correct assignment in the PIX.
*
* Can we do a platform-independent assignment?
* Yes, set the bytes without using macros:
* *((l_uint8 *)pword) = pel[2]; red
* *((l_uint8 *)pword + 1) = pel[1]; green
* *((l_uint8 *)pword + 2) = pel[0]; blue
* For little endians, before flipping, this looks again like:
* 3 (R) 2 (G) 1 (B) 0
* |-----------|------------|-----------|-----------|
*/
readerror = 0;
extrabytes = fileBpl - 3 * width;
line = pixGetData(pix) + pixWpl * (height - 1);
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
pword = line + j;
if (fake_fread(&pel, 1, 3, fp, buflen) != 3)
readerror = 1;
*((l_uint8 *)pword + COLOR_RED) = pel[2];
*((l_uint8 *)pword + COLOR_GREEN) = pel[1];
*((l_uint8 *)pword + COLOR_BLUE) = pel[0];
}
if (extrabytes) {
for (k = 0; k < extrabytes; k++)
ignore = fake_fread(&pel, 1, 1, fp, buflen);
}
line -= pixWpl;
}
if (readerror) {
pixDestroy(&pix);
return (PIX *)ERROR_PTR("BMP read fail", procName, NULL);
}
}
pixEndianByteSwap(pix);
/* ----------------------------------------------
* The bmp colormap determines the values of black
* and white pixels for binary in the following way:
* if black = 1 (255), white = 0
* 255, 255, 255, 0, 0, 0, 0, 0
* if black = 0, white = 1 (255)
* 0, 0, 0, 0, 255, 255, 255, 0
* We have no need for a 1 bpp pix with a colormap!
* ---------------------------------------------- */
if (depth == 1 && cmap) {
/* L_INFO("Removing colormap", procName); */
pixt = pixRemoveColormap(pix, REMOVE_CMAP_BASED_ON_SRC);
pixDestroy(&pix);
pix = pixt; /* rename */
}
return pix;
}