9#include "imagebuilder.h"
13#include <ksanecore_debug.h>
17ImageBuilder::ImageBuilder(
QImage *image,
int *dpi)
18 : m_image(image), m_dpi(dpi)
28void ImageBuilder::start(
const SANE_Parameters ¶ms)
32 if (m_params.format == SANE_FRAME_GRAY) {
33 switch (m_params.depth) {
44 }
else if (m_params.depth > 8) {
48 if ((m_image->height() != m_params.lines) ||
49 (m_image->width() != m_params.pixels_per_line) || m_image->format() != imageFormat) {
52 int pixelLines = m_params.lines;
54 if (m_params.lines <= 0) {
55 pixelLines = m_params.pixels_per_line;
57 *m_image =
QImage(m_params.pixels_per_line, pixelLines, imageFormat);
59 m_image->setColorTable(
QVector<QRgb>({0xFFFFFFFF,0xFF000000}));
61 int dpm = *m_dpi * (1000.0 / 25.4);
62 m_image->setDotsPerMeterX(dpm);
63 m_image->setDotsPerMeterY(dpm);
65 m_image->fill(0xFFFFFFFF);
68void ImageBuilder::beginFrame(
const SANE_Parameters ¶ms)
77bool ImageBuilder::copyToImage(
const SANE_Byte readData[],
int read_bytes)
79 switch (m_params.format) {
81 if (m_params.depth == 1) {
82 for (
int i = 0; i < read_bytes; i++) {
83 if (m_pixelY >= m_image->height()) {
86 uchar *imageBits = m_image->scanLine(m_pixelY);
87 imageBits[m_pixelX / 8] = readData[i];
89 if (m_pixelX >= m_params.pixels_per_line) {
96 }
else if (m_params.depth == 8) {
97 for (
int i = 0; i < read_bytes; i++) {
98 if (m_pixelY >= m_image->height()) {
101 uchar *grayScale = m_image->scanLine(m_pixelY);
102 grayScale[m_pixelX] = readData[i];
103 incrementPixelData();
107 }
else if (m_params.depth == 16) {
108 for (
int i = 0; i < read_bytes; i++) {
109 m_pixelData[m_pixelDataIndex] = readData[i];
111 if (m_pixelDataIndex == 2) {
112 m_pixelDataIndex = 0;
114 if (m_pixelDataIndex == 0) {
115 if (m_pixelY >= m_image->height()) {
118 quint16 *grayScale =
reinterpret_cast<quint16*
>(m_image->scanLine(m_pixelY));
119 grayScale[m_pixelX] = m_pixelData[0] + (m_pixelData[1] << 8);
120 incrementPixelData();
129 if (m_params.depth == 8) {
130 for (
int i = 0; i < read_bytes; i++) {
131 m_pixelData[m_pixelDataIndex] = readData[i];
133 if (m_pixelDataIndex == 3) {
134 m_pixelDataIndex = 0;
136 if (m_pixelDataIndex == 0) {
137 if (m_pixelY >= m_image->height()) {
140 QRgb *rgbData =
reinterpret_cast<QRgb*
>(m_image->scanLine(m_pixelY));
141 rgbData[m_pixelX] = qRgb(m_pixelData[0], m_pixelData[1], m_pixelData[2]);
142 incrementPixelData();
147 }
else if (m_params.depth == 16) {
148 for (
int i = 0; i < read_bytes; i++) {
149 m_pixelData[m_pixelDataIndex] = readData[i];
151 if (m_pixelDataIndex == 6) {
152 m_pixelDataIndex = 0;
154 if (m_pixelDataIndex == 0) {
155 if (m_pixelY >= m_image->height()) {
158 QRgba64 *rgbData =
reinterpret_cast<QRgba64*
>(m_image->scanLine(m_pixelY));
160 (m_pixelData[2] + (m_pixelData[3] << 8)),
161 (m_pixelData[4] + (m_pixelData[5] << 8)),
163 incrementPixelData();
171 case SANE_FRAME_RED: {
173 if (m_params.depth == 8) {
174 for (
int i = 0; i < read_bytes; i++) {
175 index = m_frameRead * 4 + 2;
176 if (index >= m_image->sizeInBytes()) {
179 m_image->bits()[index] = readData[i];
183 }
else if (m_params.depth == 16) {
184 for (
int i = 0; i < read_bytes; i++) {
185 index = (m_frameRead - m_frameRead % 2) * 4 + m_frameRead % 2;
186 if (index >= m_image->sizeInBytes()) {
189 m_image->bits()[index] = readData[i];
196 case SANE_FRAME_GREEN: {
198 if (m_params.depth == 8) {
199 for (
int i = 0; i < read_bytes; i++) {
200 int index = m_frameRead * 4 + 1;
201 if (index >= m_image->sizeInBytes()) {
204 m_image->bits()[index] = readData[i];
208 }
else if (m_params.depth == 16) {
209 for (
int i = 0; i < read_bytes; i++) {
210 index = (m_frameRead - m_frameRead % 2) * 4 + 2 + m_frameRead % 2;
211 if (index >= m_image->sizeInBytes()) {
214 m_image->bits()[index] = readData[i];
221 case SANE_FRAME_BLUE: {
223 if (m_params.depth == 8) {
224 for (
int i = 0; i < read_bytes; i++) {
225 int index = m_frameRead * 4;
226 if (index >= m_image->sizeInBytes()) {
229 m_image->bits()[index] = readData[i];
233 }
else if (m_params.depth == 16) {
234 for (
int i = 0; i < read_bytes; i++) {
235 index = (m_frameRead - m_frameRead % 2) * 4 + 4 + m_frameRead % 2;
236 if (index >= m_image->sizeInBytes()) {
239 m_image->bits()[index] = readData[i];
248 qCWarning(KSANECORE_LOG) <<
"Format" << m_params.format <<
"and depth" << m_params.depth <<
"is not yet supported by libksane!";
252void ImageBuilder::renewImage()
254 int start = m_image->sizeInBytes();
257 *m_image = m_image->copy(0, 0, m_image->width(), m_image->height() + m_image->width());
259 for (
int i =
start; i < m_image->sizeInBytes(); i++) {
260 m_image->bits()[i] = 0xFF;
264void ImageBuilder::cropImagetoSize()
266 int height = m_pixelY ? m_pixelY : m_frameRead / m_params.bytes_per_line;
267 if (m_image->height() == height)
269 *m_image = m_image->copy(0, 0, m_image->width(), height);
272void ImageBuilder::incrementPixelData()
275 if (m_pixelX >= m_params.pixels_per_line) {
Q_SCRIPTABLE Q_NOREPLY void start()
QRgba64 fromRgba64(quint16 r, quint16 g, quint16 b, quint16 a)