Change leptonica/qt conversion functions
This commit is contained in:
		
							parent
							
								
									cc4b0573d8
								
							
						
					
					
						commit
						e7fe13c5f0
					
				@ -6,6 +6,8 @@
 | 
				
			|||||||
#include <leptonica/allheaders.h>
 | 
					#include <leptonica/allheaders.h>
 | 
				
			||||||
#include <tesseract/baseapi.h>
 | 
					#include <tesseract/baseapi.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <QBuffer>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if defined(Q_OS_LINUX)
 | 
					#if defined(Q_OS_LINUX)
 | 
				
			||||||
#include <fstream>
 | 
					#include <fstream>
 | 
				
			||||||
static qint64 getFreeMemory()
 | 
					static qint64 getFreeMemory()
 | 
				
			||||||
@ -38,73 +40,22 @@ static qint64 getFreeMemory()
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
static Pix *convertImage(const QImage &image)
 | 
					static Pix *convertImage(const QImage &image)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  PIX *pix;
 | 
					  QBuffer buffer;
 | 
				
			||||||
 | 
					  buffer.open(QIODevice::WriteOnly);
 | 
				
			||||||
  int width = image.width();
 | 
					  image.save(&buffer, "BMP");
 | 
				
			||||||
  int height = image.height();
 | 
					  const auto &data = buffer.data();
 | 
				
			||||||
  int depth = image.depth();
 | 
					  return pixReadMemBmp(reinterpret_cast<const l_uint8 *>(data.constData()),
 | 
				
			||||||
  int bytesPerLine = image.bytesPerLine();
 | 
					                       data.size());
 | 
				
			||||||
  int wpl = bytesPerLine / 4;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  pix = pixCreate(width, height, depth);
 | 
					 | 
				
			||||||
  pixSetWpl(pix, wpl);
 | 
					 | 
				
			||||||
  pixSetColormap(pix, nullptr);
 | 
					 | 
				
			||||||
  memmove(pix->data, image.bits(), bytesPerLine * height);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  const qreal toDPM = 1.0 / 0.0254;
 | 
					 | 
				
			||||||
  int resolutionX = image.dotsPerMeterX() / toDPM;
 | 
					 | 
				
			||||||
  int resolutionY = image.dotsPerMeterY() / toDPM;
 | 
					 | 
				
			||||||
  pixSetResolution(pix, resolutionX, resolutionY);
 | 
					 | 
				
			||||||
  return pix;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static QImage convertImage(Pix &image)
 | 
					static QImage convertImage(Pix &image)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  int width = pixGetWidth(&image);
 | 
					  l_uint8 *buffer = nullptr;
 | 
				
			||||||
  int height = pixGetHeight(&image);
 | 
					  size_t len = 0;
 | 
				
			||||||
  int depth = pixGetDepth(&image);
 | 
					  pixWriteMemBmp(&buffer, &len, &image);
 | 
				
			||||||
  int bytesPerLine = pixGetWpl(&image) * 4;
 | 
					 | 
				
			||||||
  l_uint32 *datas = pixGetData(&image);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  QImage::Format format;
 | 
					 | 
				
			||||||
  if (depth == 1) {
 | 
					 | 
				
			||||||
    format = QImage::Format_Mono;
 | 
					 | 
				
			||||||
  } else if (depth == 8) {
 | 
					 | 
				
			||||||
    format = QImage::Format_Indexed8;
 | 
					 | 
				
			||||||
  } else {
 | 
					 | 
				
			||||||
    format = QImage::Format_RGB32;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  QImage result((uchar *)datas, width, height, bytesPerLine, format);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  // Set resolution
 | 
					 | 
				
			||||||
  l_int32 xres, yres;
 | 
					 | 
				
			||||||
  pixGetResolution(&image, &xres, &yres);
 | 
					 | 
				
			||||||
  const qreal toDPM = 1.0 / 0.0254;
 | 
					 | 
				
			||||||
  result.setDotsPerMeterX(xres * toDPM);
 | 
					 | 
				
			||||||
  result.setDotsPerMeterY(yres * toDPM);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  // Handle palette
 | 
					 | 
				
			||||||
  QVector<QRgb> _bwCT;
 | 
					 | 
				
			||||||
  _bwCT.append(qRgb(255, 255, 255));
 | 
					 | 
				
			||||||
  _bwCT.append(qRgb(0, 0, 0));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  QVector<QRgb> _grayscaleCT(256);
 | 
					 | 
				
			||||||
  for (int i = 0; i < 256; i++) {
 | 
					 | 
				
			||||||
    _grayscaleCT.append(qRgb(i, i, i));
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  switch (depth) {
 | 
					 | 
				
			||||||
    case 1: result.setColorTable(_bwCT); break;
 | 
					 | 
				
			||||||
    case 8: result.setColorTable(_grayscaleCT); break;
 | 
					 | 
				
			||||||
    default: result.setColorTable(_grayscaleCT);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  if (result.isNull()) {
 | 
					 | 
				
			||||||
    static QImage none(0, 0, QImage::Format_Invalid);
 | 
					 | 
				
			||||||
    qDebug("Invalid format!!!\n");
 | 
					 | 
				
			||||||
    return none;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  QImage result;
 | 
				
			||||||
 | 
					  result.loadFromData(static_cast<uchar *>(buffer), len);
 | 
				
			||||||
  return result;
 | 
					  return result;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user