- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
Tesseract 似乎不能很好地处理位图,它可以处理某些输入但会搞砸其他输入。同时在与之前相同但采用 leptonica 格式 PIX 的输入上表现良好。
如何将内存中的位图转换为 PIX?
想到的一个想法是使用 leptonica 的 pixReadMem() :
00724 /*---------------------------------------------------------------------*
00725 * Read from memory *
00726 *---------------------------------------------------------------------*/
00727 /*!
00728 * pixReadMem()
00729 *
00730 * Input: data (const; encoded)
00731 * datasize (size of data)
00732 * Return: pix, or null on error
00733 *
00734 * Notes:
00735 * (1) This is a variation of pixReadStream(), where the data is read
00736 * from a memory buffer rather than a file.
00737 * (2) On windows, this will only read tiff formatted files from
00738 * memory. For other formats, it requires fmemopen(3).
00739 * Attempts to read those formats will fail at runtime.
00740 * (3) findFileFormatBuffer() requires up to 8 bytes to decide on
00741 * the format. That determines the constraint here.
00742 */
所以现在我需要找到一种方法来从内存中的位图在内存中构造一个 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;
}
关于c++ - 如何将位图转换为内存中的 PIX?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22947929/
我想填充 3D 等高线图 (contour3(X,Y,Z)),就像 2D 等高线填充图 (contourf(X,Y,Z))。但我无法弄清楚如何实现这一目标。 contour3 和 surf 的组合不是
我有一个 c3.js 折线图,表示 2 个值的演变。我需要折线图的工具提示是饼图(工具提示 = 另一个 c3.js 图形)。 这是我成功的: http://jsfiddle.net/owhxgaqm/
我有具有结构的 Pandas 数据框: A B 0 1 1 1 2 1 2 3 4 3 3 7 4 6 8 如何生成 Seaborn Violin 图,每列作为其自己的单独
我正在使用 D3DXSPRITE 方法将我的 map 图 block 绘制到屏幕上,我刚刚添加了一个缩放功能,当您按住向上箭头时会放大,但注意到您现在可以看到图 block 之间的间隙,这是一些屏幕截
今天我们开始学习目前学习到的最难最复杂的数据结构图。 简单回顾一下之前学习的数据结构,数组、单链表、队列等线性表中数据元素是一对一关系,而树结构中数据元素是一对多关系,而图结构中数据元素则是多对
1、系统环境如下图: 2、为该系统添加一块新的虚拟硬盘,添加后需重启虚拟机,否则系统不识别;如下图,/dev/sdc 是新添加的硬盘; 3、fdisk /dev/sdc为新硬盘创建分区:
1、nagios简介 nagios是一款开源的电脑系统和网络监视工具,能有效监控windows、linux和unix的主机状态,交换机路由器等网络设置,打印机等。在系统或服务状态异常时发
越来越多人开始习惯用手机上网,浏览网页、查看邮件···移动化已经成为互联网发展必然趋势,包括facebook在内的很多互联网公司都将移动广告作为下一个淘金地
1.图片处理 1.圆角图片 复制代码 代码如下: /** * 转换成圆角 * &n
Microsoft SQL Server Management Studio是SQL SERVER的客户端工具,相信大家都知道。我不知道大伙使用导入数据的情况怎么样,反正我最近是遇到过。主要是因为没
debian6系统: 首先先安装mysql吧: 打开终端(root)用户登入 apt-get purge mysql-server-5.5 安装完成后: 默认情况下Mysql只允许本地登录
fedora16英文环境下支持中文输入法的方法 fedora16英文环境下支持FCITX的中文输入法: $ im-chooser 就会出现选择界面,选择第二个就行了。
Net预编译命令 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler.exe -? 显示说明 我们需要选择的命令为&n
有的时候电脑出现一些故障有的时候通过将其修改bios设置的方法来解决故障,那么在bios上设置能不能将电脑恢复出厂设置呢?其实也是可以的。方法也很简单的,只要会进入电脑的bios懂的上面英文的意思就
笔者曾介绍过Deepin 将对龙芯进行全面支持,打造最优美龙芯电脑桌面。现在Deepin团队移植工作取得了突破性的成果,Deepin桌面已经在龙芯3A和龙芯3B电脑上成功运行起来了。 以下为龙芯3
在安装一些软件之后,我们的电脑总是会发生一点小变化,不是桌面上多了几个网址图标,就是IE浏览器的默认主页被篡改成乱七八糟的网址。最可气的是,在IE设置中将默认主页改回来后,下次启动Win7后又变了回
“注册表编辑器怎么打开”虽说不是很难的问题,但是对于对电脑常识不是很擅长的网民来说,当电脑出现问题或需要更改设置时,着实还是件头疼的问题。因为需要打开注册表进行操作解决。那么如何打开注册表编辑器呢?
这篇文章重点介绍10个重要的WordPress安全插件和技巧,用来保护WordPress网站或者博客。 1. WP Security 人工帮助你修复被黑客入侵的网站,只要按照他们网站上的联系电话
其实运用object和javascript调用外部文件,也能实现不同栏目调用不同友情链接,即相当于调用不同栏目友情链接文件, {dede:field.typeid/}来获取当前栏目的ID。
我有一个复值矩阵。 如果我发出命令: plot(myMatrix) 然后它在图形设备上显示一种散点图,X 轴标记为 Re(myMatrix),Y 轴标记为 Im(myMatrix)。这显示了我正在寻找
我是一名优秀的程序员,十分优秀!