qtwainscanner_p.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of QtTWAIN project https://git.semlanik.org/semlanik/QtTWAIN
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "qtwainscanner_p.h"
  26. #include "qtwainscanner.h"
  27. #include "qtwaincontext_p.h"
  28. #include <QDebug>
  29. #include <QIODevice>
  30. #include <QDataStream>
  31. #include <QImage>
  32. using namespace QtTWAIN;
  33. QTWAINScannerPrivate::QTWAINScannerPrivate(QTWAINContext *ctx) : context(ctx)
  34. , status(QtTWAIN::Unknown)//TODO: init with unknown once complete statuses handling
  35. , qPtr(nullptr)
  36. , m_isOpen(false)
  37. , m_filter(nullptr)
  38. {
  39. memset(&dataSource, 0, sizeof(dataSource));
  40. connect(this, &QTWAINScannerPrivate::processEvents, this, [this] {
  41. qDebug() << "ProcessEvents";
  42. while (!m_events.empty()) {
  43. auto event = m_events.front();
  44. qDebug() << "Scanner event received: " << QString("0x%1").arg(QString::number(event.TWMessage, 16));
  45. switch(event.TWMessage)
  46. {
  47. case MSG_CLOSEDSREQ:
  48. context->disableDS(&dataSource);
  49. break;
  50. case MSG_XFERREADY:
  51. transferImage();
  52. break;
  53. case MSG_CLOSEDSOK:
  54. qDebug() << "MSG_CLOSEDSOK";
  55. break;
  56. case MSG_DEVICEEVENT:
  57. qDebug() << "MSG_DEVICEEVENT";
  58. break;
  59. default:
  60. qDebug() << "Unhandled";
  61. break;
  62. }
  63. m_events.pop();
  64. }
  65. qDebug() << "Exit ProcessEvents";
  66. }, Qt::QueuedConnection);
  67. }
  68. bool QTWAINScannerPrivate::scan()
  69. {
  70. if (status != QtTWAIN::Ready) {
  71. qCritical() << "Scanning is already started";
  72. return false;
  73. }
  74. return context->enableDS(&dataSource);
  75. }
  76. bool QTWAINScannerPrivate::open()
  77. {
  78. if (m_isOpen) {
  79. return true;
  80. }
  81. if (context->openDS(&dataSource)) {
  82. m_filter.reset(new QTWAINEventFilter(this));
  83. m_isOpen = true; //TODO: probably might be handled by state
  84. status = QtTWAIN::Ready; //TODO: call status changed notification
  85. return true;
  86. }
  87. status = QtTWAIN::Error;
  88. return false;
  89. }
  90. void QTWAINScannerPrivate::close()
  91. {
  92. if (!m_isOpen) {
  93. qDebug() << "Trying to close scanner that is not opened, skipping";
  94. return;
  95. }
  96. m_filter.reset(nullptr);
  97. context->closeDS(&dataSource);
  98. status = QtTWAIN::Unknown;
  99. }
  100. bool QTWAINScannerPrivate::processEvent(void *message)
  101. {
  102. TW_EVENT event{message, 0};
  103. if (!context->processEvent(&event, &dataSource)) {
  104. return false;
  105. }
  106. m_events.push(event);
  107. processEvents();
  108. qDebug() << "Exit processEvent";
  109. return true;
  110. }
  111. bool QTWAINScannerPrivate::transferImage()
  112. {
  113. //Getting image information
  114. TW_IMAGEINFO info;
  115. memset(&info, 0, sizeof(info));
  116. if (!context->imageInfo(&info, &dataSource)) {
  117. qCritical() << "Unable to receive image tranfer information";
  118. return false;
  119. }
  120. qDebug() << "image size" << info.ImageWidth << "x" << info.ImageLength;
  121. TW_HANDLE imageHandle = nullptr;
  122. //Perform a Native transfer
  123. quint16 result = context->imageNativeTransfer(&imageHandle, &dataSource);//Not sure probably it's blocking call or cause other issues
  124. qDebug() << "Transfer result:" << QString::number(result, 16);
  125. if (TWRC_SUCCESS == result || TWRC_XFERDONE == result) {
  126. PBITMAPINFOHEADER dib = static_cast<PBITMAPINFOHEADER>(QtTWAIN::QDSMInterface::instance()->lock(imageHandle));
  127. if (dib != nullptr) {
  128. QByteArray bmpData;
  129. QDataStream ds(&bmpData, QIODevice::WriteOnly);
  130. int offset = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (sizeof(RGBQUAD)*dib->biClrUsed);
  131. int dibSize = sizeof(BITMAPINFOHEADER) + (sizeof(RGBQUAD)*dib->biClrUsed) + dib->biSizeImage;
  132. if(BI_RGB == dib->biCompression) {
  133. //Note: Calculate the size of the image manually when uncompressed - do not trust the biSizeImage member
  134. dibSize = sizeof(BITMAPINFOHEADER) + (sizeof(RGBQUAD)*dib->biClrUsed) + ((((dib->biWidth * dib->biBitCount + 31)/32) * 4) * dib->biHeight);
  135. }
  136. BITMAPFILEHEADER header;
  137. memset(&header, 0, sizeof(BITMAPFILEHEADER));
  138. header.bfType = ( (WORD) ('M' << 8) | 'B');
  139. header.bfOffBits = offset;
  140. header.bfSize = sizeof(BITMAPFILEHEADER) + dibSize;
  141. ds.writeRawData(reinterpret_cast<char *>(&header), sizeof(BITMAPFILEHEADER));
  142. ds.writeRawData(reinterpret_cast<char *>(dib), dibSize);
  143. QtTWAIN::QDSMInterface::instance()->unlock(imageHandle);
  144. QtTWAIN::QDSMInterface::instance()->free(imageHandle);
  145. #if 1
  146. QImage img;
  147. img.loadFromData(bmpData, "BMP");
  148. if (qPtr != nullptr) {
  149. qPtr->imageReady(img);
  150. }
  151. #endif
  152. }
  153. TW_PENDINGXFERS pendingTransfers;
  154. memset(&pendingTransfers, 0, sizeof(pendingTransfers));
  155. return context->endTransfer(&pendingTransfers, &dataSource);
  156. }
  157. return false;
  158. }