ds_image.c 18.8 KB
Newer Older
1 2
/*
 * Copyright 2000 Corel Corporation
3
 * Copyright 2006 CodeWeavers, Aric Stewart
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 19 20 21
 */

#include "config.h"

22 23
#include <stdarg.h>

24
#include "sane_i.h"
25 26 27 28 29 30 31
#include "wingdi.h"
#include "winuser.h"
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(twain);

/* DG_IMAGE/DAT_IMAGEINFO/MSG_GET */
32
TW_UINT16 SANE_ImageInfoGet (pTW_IDENTITY pOrigin, 
33 34
                              TW_MEMREF pData)
{
35
#ifndef SONAME_LIBSANE
36 37 38 39 40
    return TWRC_FAILURE;
#else
    TW_UINT16 twRC = TWRC_SUCCESS;
    pTW_IMAGEINFO pImageInfo = (pTW_IMAGEINFO) pData;
    SANE_Status status;
41
    SANE_Int resolution;
42 43 44

    TRACE("DG_IMAGE/DAT_IMAGEINFO/MSG_GET\n");

45
    if (activeDS.currentState != 6 && activeDS.currentState != 7)
46 47
    {
        twRC = TWRC_FAILURE;
48
        activeDS.twCC = TWCC_SEQERROR;
49 50 51
    }
    else
    {
52
        if (activeDS.currentState == 6)
53
        {
54
            /* return general image description information about the image about to be transferred */
55
            status = psane_get_parameters (activeDS.deviceHandle, &activeDS.sane_param);
56
            TRACE("Getting parameters\n");
57 58 59 60
            if (status != SANE_STATUS_GOOD)
            {
                WARN("psane_get_parameters: %s\n", psane_strstatus (status));
                psane_cancel (activeDS.deviceHandle);
61
                activeDS.sane_started = FALSE;
62 63 64 65
                activeDS.twCC = TWCC_OPERATIONERROR;
                return TWRC_FAILURE;
            }
            activeDS.sane_param_valid = TRUE;
66 67
        }

68 69 70 71
        if (sane_option_get_int(activeDS.deviceHandle, "resolution", &resolution) == SANE_STATUS_GOOD)
            pImageInfo->XResolution.Whole = pImageInfo->YResolution.Whole = resolution;
        else
            pImageInfo->XResolution.Whole = pImageInfo->YResolution.Whole = -1;
72 73
        pImageInfo->XResolution.Frac = 0;
        pImageInfo->YResolution.Frac = 0;
74 75
        pImageInfo->ImageWidth = activeDS.sane_param.pixels_per_line;
        pImageInfo->ImageLength = activeDS.sane_param.lines;
76

77 78
        TRACE("Bits per Sample %i\n",activeDS.sane_param.depth);
        TRACE("Frame Format %i\n",activeDS.sane_param.format);
79

80
        if (activeDS.sane_param.format == SANE_FRAME_RGB )
81
        {
82
            pImageInfo->BitsPerPixel = activeDS.sane_param.depth * 3;
83
            pImageInfo->Compression = TWCP_NONE;
84
            pImageInfo->Planar = TRUE;
85
            pImageInfo->SamplesPerPixel = 3;
86 87 88
            pImageInfo->BitsPerSample[0] = activeDS.sane_param.depth;
            pImageInfo->BitsPerSample[1] = activeDS.sane_param.depth;
            pImageInfo->BitsPerSample[2] = activeDS.sane_param.depth;
89
            pImageInfo->PixelType = TWPT_RGB;
90
        }
91
        else if (activeDS.sane_param.format == SANE_FRAME_GRAY)
92
        {
93
            pImageInfo->BitsPerPixel = activeDS.sane_param.depth;
94 95 96
            pImageInfo->Compression = TWCP_NONE;
            pImageInfo->Planar = TRUE;
            pImageInfo->SamplesPerPixel = 1;
97
            pImageInfo->BitsPerSample[0] = activeDS.sane_param.depth;
98 99 100 101
            if (activeDS.sane_param.depth == 1)
                pImageInfo->PixelType = TWPT_BW;
            else
                pImageInfo->PixelType = TWPT_GRAY;
102
        }
103
        else
104
        {
105
            ERR("Unhandled source frame type %i\n",activeDS.sane_param.format);
106
            twRC = TWRC_FAILURE;
107
            activeDS.twCC = TWCC_SEQERROR;
108 109 110 111 112 113 114 115
        }
    }

    return twRC;
#endif
}

/* DG_IMAGE/DAT_IMAGELAYOUT/MSG_GET */
116
TW_UINT16 SANE_ImageLayoutGet (pTW_IDENTITY pOrigin,
117 118
                                TW_MEMREF pData)
{
119
#ifndef SONAME_LIBSANE
120
    return TWRC_FAILURE;
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
#else
    TW_IMAGELAYOUT *img = (TW_IMAGELAYOUT *) pData;
    SANE_Fixed tlx_current;
    SANE_Fixed tly_current;
    SANE_Fixed brx_current;
    SANE_Fixed bry_current;
    SANE_Status status;

    TRACE("DG_IMAGE/DAT_IMAGELAYOUT/MSG_GET\n");

    status = sane_option_probe_scan_area(activeDS.deviceHandle, "tl-x", &tlx_current, NULL, NULL, NULL, NULL);
    if (status == SANE_STATUS_GOOD)
        status = sane_option_probe_scan_area(activeDS.deviceHandle, "tl-y", &tly_current, NULL, NULL, NULL, NULL);

    if (status == SANE_STATUS_GOOD)
        status = sane_option_probe_scan_area(activeDS.deviceHandle, "br-x", &brx_current, NULL, NULL, NULL, NULL);

    if (status == SANE_STATUS_GOOD)
        status = sane_option_probe_scan_area(activeDS.deviceHandle, "br-y", &bry_current, NULL, NULL, NULL, NULL);

    if (status != SANE_STATUS_GOOD)
    {
        activeDS.twCC = sane_status_to_twcc(status);
        return TWRC_FAILURE;
    }

    convert_sane_res_to_twain(SANE_UNFIX(tlx_current), SANE_UNIT_MM, &img->Frame.Left, TWUN_INCHES);
    convert_sane_res_to_twain(SANE_UNFIX(tly_current), SANE_UNIT_MM, &img->Frame.Top, TWUN_INCHES);
    convert_sane_res_to_twain(SANE_UNFIX(brx_current), SANE_UNIT_MM, &img->Frame.Right, TWUN_INCHES);
    convert_sane_res_to_twain(SANE_UNFIX(bry_current), SANE_UNIT_MM, &img->Frame.Bottom, TWUN_INCHES);

    img->DocumentNumber = 1;
    img->PageNumber = 1;
    img->FrameNumber = 1;

    activeDS.twCC = TWCC_SUCCESS;
    return TWRC_SUCCESS;
#endif
159 160 161
}

/* DG_IMAGE/DAT_IMAGELAYOUT/MSG_GETDEFAULT */
162
TW_UINT16 SANE_ImageLayoutGetDefault (pTW_IDENTITY pOrigin, 
163 164 165 166 167 168 169 170
                                       TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

/* DG_IMAGE/DAT_IMAGELAYOUT/MSG_RESET */
171
TW_UINT16 SANE_ImageLayoutReset (pTW_IDENTITY pOrigin, 
172 173 174 175 176 177 178
                                  TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
#ifdef SONAME_LIBSANE
static TW_UINT16 set_one_imagecoord(const char *option_name, TW_FIX32 val, BOOL *changed)
{
    double d = val.Whole + ((double) val.Frac / 65536.0);
    int set_status = 0;
    SANE_Status status;
    status = sane_option_set_fixed(activeDS.deviceHandle, option_name,
             SANE_FIX((d * 254) / 10), &set_status);
    if (status != SANE_STATUS_GOOD)
    {
        activeDS.twCC = sane_status_to_twcc(status);
        return TWRC_FAILURE;
    }
    if (set_status & SANE_INFO_INEXACT)
        *changed = TRUE;
    return TWRC_SUCCESS;
}
#endif

198
/* DG_IMAGE/DAT_IMAGELAYOUT/MSG_SET */
199
TW_UINT16 SANE_ImageLayoutSet (pTW_IDENTITY pOrigin,
200 201
                                TW_MEMREF pData)
{
202
#ifndef SONAME_LIBSANE
203
    return TWRC_FAILURE;
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
#else
    TW_IMAGELAYOUT *img = (TW_IMAGELAYOUT *) pData;
    BOOL changed = FALSE;
    TW_UINT16 twrc;

    TRACE("DG_IMAGE/DAT_IMAGELAYOUT/MSG_SET\n");
    TRACE("Frame: [Left %x.%x|Top %x.%x|Right %x.%x|Bottom %x.%x]\n",
            img->Frame.Left.Whole, img->Frame.Left.Frac,
            img->Frame.Top.Whole, img->Frame.Top.Frac,
            img->Frame.Right.Whole, img->Frame.Right.Frac,
            img->Frame.Bottom.Whole, img->Frame.Bottom.Frac);

    twrc = set_one_imagecoord("tl-x", img->Frame.Left, &changed);
    if (twrc != TWRC_SUCCESS)
        return (twrc);

    twrc = set_one_imagecoord("tl-y", img->Frame.Top, &changed);
    if (twrc != TWRC_SUCCESS)
        return (twrc);

    twrc = set_one_imagecoord("br-x", img->Frame.Right, &changed);
    if (twrc != TWRC_SUCCESS)
        return (twrc);

    twrc = set_one_imagecoord("br-y", img->Frame.Bottom, &changed);
    if (twrc != TWRC_SUCCESS)
        return (twrc);

    activeDS.twCC = TWCC_SUCCESS;
    return changed ? TWRC_CHECKSTATUS : TWRC_SUCCESS;
#endif
235 236 237
}

/* DG_IMAGE/DAT_IMAGEMEMXFER/MSG_GET */
238
TW_UINT16 SANE_ImageMemXferGet (pTW_IDENTITY pOrigin, 
239 240
                                 TW_MEMREF pData)
{
241
#ifndef SONAME_LIBSANE
242
    return TWRC_FAILURE;
243 244 245 246 247 248 249
#else
    TW_UINT16 twRC = TWRC_SUCCESS;
    pTW_IMAGEMEMXFER pImageMemXfer = (pTW_IMAGEMEMXFER) pData;
    SANE_Status status = SANE_STATUS_GOOD;

    TRACE ("DG_IMAGE/DAT_IMAGEMEMXFER/MSG_GET\n");

250
    if (activeDS.currentState < 6 || activeDS.currentState > 7)
251 252
    {
        twRC = TWRC_FAILURE;
253
        activeDS.twCC = TWCC_SEQERROR;
254 255 256 257 258 259 260 261 262 263
    }
    else
    {
        LPBYTE buffer;
        int buff_len = 0;
        int consumed_len = 0;
        LPBYTE ptr;
        int rows;

        /* Transfer an image from the source to the application */
264
        if (activeDS.currentState == 6)
265
        {
266 267

            /* trigger scanning dialog */
268
            activeDS.progressWnd = ScanningDialogBox(NULL,0);
269

270
            ScanningDialogBox(activeDS.progressWnd,0);
271

272
            if (! activeDS.sane_started)
273
            {
274 275 276 277 278 279 280 281 282
                status = psane_start (activeDS.deviceHandle);
                if (status != SANE_STATUS_GOOD)
                {
                    WARN("psane_start: %s\n", psane_strstatus (status));
                    psane_cancel (activeDS.deviceHandle);
                    activeDS.twCC = TWCC_OPERATIONERROR;
                    return TWRC_FAILURE;
                }
                activeDS.sane_started = TRUE;
283 284
            }

285
            status = psane_get_parameters (activeDS.deviceHandle,
286 287
                    &activeDS.sane_param);
            activeDS.sane_param_valid = TRUE;
288 289 290

            if (status != SANE_STATUS_GOOD)
            {
291 292
                WARN("psane_get_parameters: %s\n", psane_strstatus (status));
                psane_cancel (activeDS.deviceHandle);
293
                activeDS.sane_started = FALSE;
294
                activeDS.twCC = TWCC_OPERATIONERROR;
295 296 297 298
                return TWRC_FAILURE;
            }

            TRACE("Acquiring image %dx%dx%d bits (format=%d last=%d) from sane...\n"
299 300 301
              , activeDS.sane_param.pixels_per_line, activeDS.sane_param.lines,
              activeDS.sane_param.depth, activeDS.sane_param.format,
              activeDS.sane_param.last_frame);
302

303
            activeDS.currentState = 7;
304 305 306
        }

        /* access memory buffer */
307
        if (pImageMemXfer->Memory.Length < activeDS.sane_param.bytes_per_line)
308
        {
309
            psane_cancel (activeDS.deviceHandle);
310
            activeDS.sane_started = FALSE;
311
            activeDS.twCC = TWCC_BADVALUE;
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
            return TWRC_FAILURE;
        }

        if (pImageMemXfer->Memory.Flags & TWMF_HANDLE)
        {
            FIXME("Memory Handle, may not be locked correctly\n");
            buffer = LocalLock(pImageMemXfer->Memory.TheMem);
        }
        else
            buffer = pImageMemXfer->Memory.TheMem;
       
        memset(buffer,0,pImageMemXfer->Memory.Length);

        ptr = buffer;
        consumed_len = 0;
327
        rows = pImageMemXfer->Memory.Length / activeDS.sane_param.bytes_per_line;
328 329

        /* must fill full lines */
330
        while (consumed_len < (activeDS.sane_param.bytes_per_line*rows) && 
331 332
                status == SANE_STATUS_GOOD)
        {
333
            status = psane_read (activeDS.deviceHandle, ptr,
334
                    (activeDS.sane_param.bytes_per_line*rows) - consumed_len ,
335 336 337 338 339 340 341 342
                    &buff_len);
            consumed_len += buff_len;
            ptr += buff_len;
        }

        if (status == SANE_STATUS_GOOD || status == SANE_STATUS_EOF)
        {
            pImageMemXfer->Compression = TWCP_NONE;
343 344
            pImageMemXfer->BytesPerRow = activeDS.sane_param.bytes_per_line;
            pImageMemXfer->Columns = activeDS.sane_param.pixels_per_line;
345 346 347 348 349
            pImageMemXfer->Rows = rows;
            pImageMemXfer->XOffset = 0;
            pImageMemXfer->YOffset = 0;
            pImageMemXfer->BytesWritten = consumed_len;

350
            ScanningDialogBox(activeDS.progressWnd, consumed_len);
351

352 353
            if (status == SANE_STATUS_EOF)
            {
354
                ScanningDialogBox(activeDS.progressWnd, -1);
355 356
                TRACE("psane_read: %s\n", psane_strstatus (status));
                psane_cancel (activeDS.deviceHandle);
357
                activeDS.sane_started = FALSE;
358 359
                twRC = TWRC_XFERDONE;
            }
360
            activeDS.twCC = TWRC_SUCCESS;
361 362 363
        }
        else if (status != SANE_STATUS_EOF)
        {
364
            ScanningDialogBox(activeDS.progressWnd, -1);
365 366
            WARN("psane_read: %s\n", psane_strstatus (status));
            psane_cancel (activeDS.deviceHandle);
367
            activeDS.sane_started = FALSE;
368
            activeDS.twCC = TWCC_OPERATIONERROR;
369 370 371 372 373 374 375 376 377
            twRC = TWRC_FAILURE;
        }
    }

    if (pImageMemXfer->Memory.Flags & TWMF_HANDLE)
        LocalUnlock(pImageMemXfer->Memory.TheMem);
    
    return twRC;
#endif
378 379
}

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
#ifdef SONAME_LIBSANE
static SANE_Status read_one_line(SANE_Handle h, BYTE *line, int len)
{
    int read_len;
    SANE_Status status;

    for (;;)
    {
        read_len = 0;
        status = psane_read (activeDS.deviceHandle, line, len, &read_len);
        if (status != SANE_STATUS_GOOD)
            break;

        if (read_len == len)
            break;

        line += read_len;
        len -= read_len;
    }

    return status;
}
#endif

404
/* DG_IMAGE/DAT_IMAGENATIVEXFER/MSG_GET */
405
TW_UINT16 SANE_ImageNativeXferGet (pTW_IDENTITY pOrigin, 
406 407
                                    TW_MEMREF pData)
{
408
#ifndef SONAME_LIBSANE
409 410 411 412 413
    return TWRC_FAILURE;
#else
    TW_UINT16 twRC = TWRC_SUCCESS;
    pTW_UINT32 pHandle = (pTW_UINT32) pData;
    SANE_Status status;
414 415 416 417 418
    HANDLE hDIB;
    BITMAPINFOHEADER *header = NULL;
    int dib_bytes;
    int dib_bytes_per_line;
    BYTE *line;
419 420
    RGBQUAD *colors;
    int color_size = 0;
421
    int i;
422
    BYTE *p;
423

424 425
    TRACE("DG_IMAGE/DAT_IMAGENATIVEXFER/MSG_GET\n");

426
    if (activeDS.currentState != 6)
427 428
    {
        twRC = TWRC_FAILURE;
429
        activeDS.twCC = TWCC_SEQERROR;
430 431 432 433
    }
    else
    {
        /* Transfer an image from the source to the application */
434
        if (! activeDS.sane_started)
435
        {
436 437 438 439 440 441 442 443 444
            status = psane_start (activeDS.deviceHandle);
            if (status != SANE_STATUS_GOOD)
            {
                WARN("psane_start: %s\n", psane_strstatus (status));
                psane_cancel (activeDS.deviceHandle);
                activeDS.twCC = TWCC_OPERATIONERROR;
                return TWRC_FAILURE;
            }
            activeDS.sane_started = TRUE;
445
        }
446

447
        status = psane_get_parameters (activeDS.deviceHandle, &activeDS.sane_param);
448
        activeDS.sane_param_valid = TRUE;
449 450
        if (status != SANE_STATUS_GOOD)
        {
451 452
            WARN("psane_get_parameters: %s\n", psane_strstatus (status));
            psane_cancel (activeDS.deviceHandle);
453
            activeDS.sane_started = FALSE;
454
            activeDS.twCC = TWCC_OPERATIONERROR;
455 456 457
            return TWRC_FAILURE;
        }

458
        if (activeDS.sane_param.format == SANE_FRAME_GRAY)
459
        {
460 461 462 463 464 465 466 467
            if (activeDS.sane_param.depth == 8)
                color_size = (1 << 8) * sizeof(*colors);
            else if (activeDS.sane_param.depth == 1)
                ;
            else
            {
                FIXME("For NATIVE, we support only 1 bit monochrome and 8 bit Grayscale, not %d\n", activeDS.sane_param.depth);
                psane_cancel (activeDS.deviceHandle);
468
                activeDS.sane_started = FALSE;
469 470 471 472 473 474 475
                activeDS.twCC = TWCC_OPERATIONERROR;
                return TWRC_FAILURE;
            }
        }
        else if (activeDS.sane_param.format != SANE_FRAME_RGB)
        {
            FIXME("For NATIVE, we support only GRAY and RGB, not %d\n", activeDS.sane_param.format);
476
            psane_cancel (activeDS.deviceHandle);
477
            activeDS.sane_started = FALSE;
478 479 480 481 482
            activeDS.twCC = TWCC_OPERATIONERROR;
            return TWRC_FAILURE;
        }

        TRACE("Acquiring image %dx%dx%d bits (format=%d last=%d bpl=%d) from sane...\n"
483 484
              , activeDS.sane_param.pixels_per_line, activeDS.sane_param.lines,
              activeDS.sane_param.depth, activeDS.sane_param.format,
485
              activeDS.sane_param.last_frame, activeDS.sane_param.bytes_per_line);
486

487 488 489
        dib_bytes_per_line = ((activeDS.sane_param.bytes_per_line + 3) / 4) * 4;
        dib_bytes = activeDS.sane_param.lines * dib_bytes_per_line;

490
        hDIB = GlobalAlloc(GMEM_ZEROINIT, dib_bytes + sizeof(*header) + color_size);
491 492 493 494
        if (hDIB)
           header = GlobalLock(hDIB);

        if (!header)
495
        {
496
            psane_cancel (activeDS.deviceHandle);
497
            activeDS.sane_started = FALSE;
498
            activeDS.twCC = TWCC_LOWMEMORY;
499 500
            if (hDIB)
                GlobalFree(hDIB);
501 502
            return TWRC_FAILURE;
        }
503

504 505 506 507 508
        header->biSize = sizeof (*header);
        header->biWidth = activeDS.sane_param.pixels_per_line;
        header->biHeight = activeDS.sane_param.lines;
        header->biPlanes = 1;
        header->biCompression = BI_RGB;
509 510 511 512
        if (activeDS.sane_param.format == SANE_FRAME_RGB)
            header->biBitCount = activeDS.sane_param.depth * 3;
        if (activeDS.sane_param.format == SANE_FRAME_GRAY)
            header->biBitCount = activeDS.sane_param.depth;
513 514 515 516 517 518
        header->biSizeImage = dib_bytes;
        header->biXPelsPerMeter = 0;
        header->biYPelsPerMeter = 0;
        header->biClrUsed = 0;
        header->biClrImportant = 0;

519 520 521 522 523 524 525 526 527 528 529
        p = (BYTE *)(header + 1);

        if (color_size > 0)
        {
            colors = (RGBQUAD *) p;
            p += color_size;
            for (i = 0; i < (color_size / sizeof(*colors)); i++)
                colors[i].rgbBlue = colors[i].rgbRed = colors[i].rgbGreen = i;
        }


530 531
        /* Sane returns data in top down order.  Acrobat does best with
           a bottom up DIB being returned.  */
532
        line = p + (activeDS.sane_param.lines - 1) * dib_bytes_per_line;
533
        for (i = activeDS.sane_param.lines - 1; i >= 0; i--)
534
        {
535 536 537 538
            activeDS.progressWnd = ScanningDialogBox(activeDS.progressWnd,
                    ((activeDS.sane_param.lines - 1 - i) * 100)
                            /
                    (activeDS.sane_param.lines - 1));
539

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
            status = read_one_line(activeDS.deviceHandle, line,
                            activeDS.sane_param.bytes_per_line);
            if (status != SANE_STATUS_GOOD)
                break;

            line -= dib_bytes_per_line;
        }
        activeDS.progressWnd = ScanningDialogBox(activeDS.progressWnd, -1);

        GlobalUnlock(hDIB);

        if (status != SANE_STATUS_GOOD && status != SANE_STATUS_EOF)
        {
            WARN("psane_read: %s, reading line %d\n", psane_strstatus(status), i);
            psane_cancel (activeDS.deviceHandle);
555
            activeDS.sane_started = FALSE;
556 557 558 559
            activeDS.twCC = TWCC_OPERATIONERROR;
            GlobalFree(hDIB);
            return TWRC_FAILURE;
        }
560

561
        psane_cancel (activeDS.deviceHandle);
562
        activeDS.sane_started = FALSE;
563
        *pHandle = (UINT_PTR)hDIB;
564
        twRC = TWRC_XFERDONE;
565 566
        activeDS.twCC = TWCC_SUCCESS;
        activeDS.currentState = 7;
567 568 569 570
    }
    return twRC;
#endif
}