ds_image.c 18.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright 2000 Corel Corporation
 * Copyright 2006 Marcus Meissner
 * Copyright 2006 CodeWeavers, Aric Stewart
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21 22
 */

#include <stdarg.h>
#include <stdio.h>
23
#include <stdlib.h>
24

25
#include "gphoto2_i.h"
26 27
#include "wingdi.h"
#include "winuser.h"
28
#include "unixlib.h"
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(twain);

/* for the jpeg decompressor source manager. */
static void _jpeg_init_source(j_decompress_ptr cinfo) { }

static boolean _jpeg_fill_input_buffer(j_decompress_ptr cinfo) {
    ERR("(), should not get here.\n");
    return FALSE;
}

static void _jpeg_skip_input_data(j_decompress_ptr cinfo,long num_bytes) {
    TRACE("Skipping %ld bytes...\n", num_bytes);
    cinfo->src->next_input_byte += num_bytes;
    cinfo->src->bytes_in_buffer -= num_bytes;
}

static boolean _jpeg_resync_to_restart(j_decompress_ptr cinfo, int desired) {
    ERR("(desired=%d), should not get here.\n",desired);
    return FALSE;
}
static void _jpeg_term_source(j_decompress_ptr cinfo) { }

53
static void close_file( UINT64 handle )
54 55 56 57 58 59 60 61
{
    struct close_file_params params = { handle };
    GPHOTO2_CALL( close_file, &params );
}

static void close_current_file(void)
{
    close_file( activeDS.file_handle );
62
    activeDS.file_handle = 0;
63 64 65
    free( activeDS.file_data );
}

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/* DG_IMAGE/DAT_CIECOLOR/MSG_GET */
TW_UINT16 GPHOTO2_CIEColorGet (pTW_IDENTITY pOrigin, 
                             TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

/* DG_IMAGE/DAT_EXTIMAGEINFO/MSG_GET */
TW_UINT16 GPHOTO2_ExtImageInfoGet (pTW_IDENTITY pOrigin, 
                                 TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

/* DG_IMAGE/DAT_GRAYRESPONSE/MSG_RESET */
TW_UINT16 GPHOTO2_GrayResponseReset (pTW_IDENTITY pOrigin, 
                                   TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

/* DG_IMAGE/DAT_GRAYRESPONSE/MSG_SET */
TW_UINT16 GPHOTO2_GrayResponseSet (pTW_IDENTITY pOrigin, 
                                 TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

/* DG_IMAGE/DAT_IMAGEFILEXFER/MSG_GET */
TW_UINT16 GPHOTO2_ImageFileXferGet (pTW_IDENTITY pOrigin, 
                                  TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

111
static TW_UINT16 _get_image_and_startup_jpeg(void) {
112
    unsigned int i;
113
    int ret;
114 115
    struct open_file_params open_params;
    struct get_file_data_params get_data_params;
116

117
    if (activeDS.file_handle) /* Already loaded. */
118 119
	return TWRC_SUCCESS;

120 121 122 123 124 125
    for (i = 0; i < activeDS.file_count; i++)
    {
        if (activeDS.download_flags[i])
        {
            activeDS.download_flags[i] = FALSE; /* mark as done */
            break;
126 127
	}
    }
128 129 130 131 132 133 134 135 136 137 138 139
    if (i == activeDS.file_count)
    {
	activeDS.twCC = TWCC_SEQERROR;
	return TWRC_FAILURE;
    }

    open_params.idx     = i;
    open_params.preview = FALSE;
    open_params.handle  = &activeDS.file_handle;
    open_params.size    = &activeDS.file_size;
    if (GPHOTO2_CALL( open_file, &open_params ))
    {
140 141 142
	activeDS.twCC = TWCC_SEQERROR;
	return TWRC_FAILURE;
    }
143 144 145 146 147 148 149

    activeDS.file_data = malloc( activeDS.file_size );
    get_data_params.handle = activeDS.file_handle;
    get_data_params.data   = activeDS.file_data;
    get_data_params.size   = activeDS.file_size;
    if (GPHOTO2_CALL( get_file_data, &get_data_params ))
    {
150 151 152 153 154 155 156
	activeDS.twCC = TWCC_SEQERROR;
	return TWRC_FAILURE;
    }

    /* This is basically so we can use in-memory data for jpeg decompression.
     * We need to have all the functions.
     */
157 158
    activeDS.xjsm.next_input_byte	= activeDS.file_data;
    activeDS.xjsm.bytes_in_buffer	= activeDS.file_size;
159 160 161 162 163 164
    activeDS.xjsm.init_source	= _jpeg_init_source;
    activeDS.xjsm.fill_input_buffer	= _jpeg_fill_input_buffer;
    activeDS.xjsm.skip_input_data	= _jpeg_skip_input_data;
    activeDS.xjsm.resync_to_restart	= _jpeg_resync_to_restart;
    activeDS.xjsm.term_source	= _jpeg_term_source;

165
    activeDS.jd.err = jpeg_std_error(&activeDS.jerr);
166 167
    /* jpeg_create_decompress is a macro that expands to jpeg_CreateDecompress - see jpeglib.h
     * jpeg_create_decompress(&jd); */
168
    jpeg_CreateDecompress(&activeDS.jd, JPEG_LIB_VERSION, sizeof(struct jpeg_decompress_struct));
169
    activeDS.jd.src = &activeDS.xjsm;
170
    ret=jpeg_read_header(&activeDS.jd,TRUE);
171
    activeDS.jd.out_color_space = JCS_RGB;
172
    jpeg_start_decompress(&activeDS.jd);
173 174
    if (ret != JPEG_HEADER_OK) {
	ERR("Jpeg image in stream has bad format, read header returned %d.\n",ret);
175
        close_current_file();
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
	return TWRC_FAILURE;
    }
    return TWRC_SUCCESS;
}

/* DG_IMAGE/DAT_IMAGEINFO/MSG_GET */
TW_UINT16 GPHOTO2_ImageInfoGet (pTW_IDENTITY pOrigin, 
                              TW_MEMREF pData)
{
    pTW_IMAGEINFO pImageInfo = (pTW_IMAGEINFO) pData;

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

    if (activeDS.currentState != 6 && activeDS.currentState != 7) {
        activeDS.twCC = TWCC_SEQERROR;
        return TWRC_FAILURE;
    }
    if (TWRC_SUCCESS != _get_image_and_startup_jpeg()) {
	FIXME("Failed to get an image\n");
        activeDS.twCC = TWCC_SEQERROR;
	return TWRC_FAILURE;
    }
    if (activeDS.currentState == 6)
    {
        /* return general image description information about the image about to be transferred */
        TRACE("Getting parameters\n");
    }
    TRACE("activeDS.jd.output_width = %d\n", activeDS.jd.output_width);
    TRACE("activeDS.jd.output_height = %d\n", activeDS.jd.output_height);
    pImageInfo->Compression	= TWCP_NONE;
    pImageInfo->SamplesPerPixel	= 3;
    pImageInfo->BitsPerSample[0]= 8;
    pImageInfo->BitsPerSample[1]= 8;
    pImageInfo->BitsPerSample[2]= 8;
    pImageInfo->PixelType 	= TWPT_RGB;
    pImageInfo->Planar		= FALSE; /* R-G-B is chunky! */
    pImageInfo->XResolution.Whole = -1;
    pImageInfo->XResolution.Frac = 0;
    pImageInfo->YResolution.Whole = -1;
    pImageInfo->YResolution.Frac = 0;
    pImageInfo->ImageWidth 	= activeDS.jd.output_width;
    pImageInfo->ImageLength 	= activeDS.jd.output_height;
    pImageInfo->BitsPerPixel	= 24;
    return TWRC_SUCCESS;
}

/* DG_IMAGE/DAT_IMAGELAYOUT/MSG_GET */
TW_UINT16 GPHOTO2_ImageLayoutGet (pTW_IDENTITY pOrigin, 
                                TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

/* DG_IMAGE/DAT_IMAGELAYOUT/MSG_GETDEFAULT */
TW_UINT16 GPHOTO2_ImageLayoutGetDefault (pTW_IDENTITY pOrigin, 
                                       TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

/* DG_IMAGE/DAT_IMAGELAYOUT/MSG_RESET */
TW_UINT16 GPHOTO2_ImageLayoutReset (pTW_IDENTITY pOrigin, 
                                  TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

/* DG_IMAGE/DAT_IMAGELAYOUT/MSG_SET */
TW_UINT16 GPHOTO2_ImageLayoutSet (pTW_IDENTITY pOrigin, 
                                TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

/* DG_IMAGE/DAT_IMAGEMEMXFER/MSG_GET */
TW_UINT16 GPHOTO2_ImageMemXferGet (pTW_IDENTITY pOrigin, 
                                 TW_MEMREF pData)
{
    TW_UINT16 twRC = TWRC_SUCCESS;
    pTW_IMAGEMEMXFER pImageMemXfer = (pTW_IMAGEMEMXFER) pData;
    LPBYTE buffer;
    int readrows;
    unsigned int curoff;

    TRACE ("DG_IMAGE/DAT_IMAGEMEMXFER/MSG_GET\n");
    if (activeDS.currentState < 6 || activeDS.currentState > 7) {
        activeDS.twCC = TWCC_SEQERROR;
        return TWRC_FAILURE;
    }
    TRACE("pImageMemXfer.Compression is %d\n", pImageMemXfer->Compression);
    if (activeDS.currentState == 6) {
	if (TWRC_SUCCESS != _get_image_and_startup_jpeg()) {
	    FIXME("Failed to get an image\n");
	    activeDS.twCC = TWCC_SEQERROR;
	    return TWRC_FAILURE;
	}

    if (!activeDS.progressWnd)
282 283
        activeDS.progressWnd = TransferringDialogBox(NULL,0);
    TransferringDialogBox(activeDS.progressWnd,0);
284 285 286

        activeDS.currentState = 7;
    } else {
287
	if (!activeDS.file_handle) {
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    	    activeDS.twCC = TWRC_SUCCESS;
	    return TWRC_XFERDONE;
	}
    }

    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);
    curoff = 0; readrows = 0;
    pImageMemXfer->YOffset	= activeDS.jd.output_scanline;
    pImageMemXfer->XOffset	= 0;	/* we do whole strips */
    while ((activeDS.jd.output_scanline<activeDS.jd.output_height) &&
	   ((pImageMemXfer->Memory.Length - curoff) > activeDS.jd.output_width*activeDS.jd.output_components)
    ) {
	JSAMPROW row = buffer+curoff;
307
	int x = jpeg_read_scanlines(&activeDS.jd,&row,1);
308 309 310 311 312 313 314 315 316 317 318 319 320
	if (x != 1) {
		FIXME("failed to read current scanline?\n");
		break;
	}
	readrows++;
	curoff += activeDS.jd.output_width*activeDS.jd.output_components;
    }
    pImageMemXfer->Compression	= TWCP_NONE;
    pImageMemXfer->BytesPerRow	= activeDS.jd.output_components * activeDS.jd.output_width;
    pImageMemXfer->Rows		= readrows;
    pImageMemXfer->Columns	= activeDS.jd.output_width; /* we do whole strips */
    pImageMemXfer->BytesWritten = curoff;

321
    TransferringDialogBox(activeDS.progressWnd,0);
322 323

    if (activeDS.jd.output_scanline == activeDS.jd.output_height) {
324 325
        jpeg_finish_decompress(&activeDS.jd);
        jpeg_destroy_decompress(&activeDS.jd);
326
        close_current_file();
327 328
	TRACE("xfer is done!\n");

329
	/*TransferringDialogBox(activeDS.progressWnd, -1);*/
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
	twRC = TWRC_XFERDONE;
    }
    activeDS.twCC = TWRC_SUCCESS;
    if (pImageMemXfer->Memory.Flags & TWMF_HANDLE)
        LocalUnlock(pImageMemXfer->Memory.TheMem);
    return twRC;
}

/* DG_IMAGE/DAT_IMAGENATIVEXFER/MSG_GET */
TW_UINT16 GPHOTO2_ImageNativeXferGet (pTW_IDENTITY pOrigin, 
                                    TW_MEMREF pData)
{
    pTW_UINT32 pHandle = (pTW_UINT32) pData;
    HBITMAP hDIB;
    BITMAPINFO bmpInfo;
345
    LPBYTE bits;
346 347 348 349 350 351 352 353 354 355 356
    JSAMPROW samprow, oldsamprow;

    FIXME("DG_IMAGE/DAT_IMAGENATIVEXFER/MSG_GET: implemented, but expect program crash due to DIB.\n");

/*  NOTE NOTE NOTE NOTE NOTE NOTE NOTE
 *
 *  While this is a mandatory transfer mode and this function
 *  is correctly implemented and fully works, the calling program
 *  will likely crash after calling.
 *
 *  Reason is that there is a lot of example code that does:
357
 *  bmpinfo = GlobalLock(hBITMAP); ... pointer access to bmpinfo
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
 *
 *  Our current HBITMAP handles do not support getting GlobalLocked -> App Crash
 *
 *  This needs a GDI Handle rewrite, at least for DIB sections.
 *  - Marcus
 */
    if (activeDS.currentState != 6) {
        activeDS.twCC = TWCC_SEQERROR;
        return TWRC_FAILURE;
    }
    if (TWRC_SUCCESS != _get_image_and_startup_jpeg()) {
	FIXME("Failed to get an image\n");
	activeDS.twCC = TWCC_OPERATIONERROR;
	return TWRC_FAILURE;
    }
    TRACE("Acquiring image %dx%dx%d bits from gphoto.\n",
	activeDS.jd.output_width, activeDS.jd.output_height,
	activeDS.jd.output_components*8);
    ZeroMemory (&bmpInfo, sizeof (BITMAPINFO));
    bmpInfo.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
    bmpInfo.bmiHeader.biWidth = activeDS.jd.output_width;
    bmpInfo.bmiHeader.biHeight = -activeDS.jd.output_height;
    bmpInfo.bmiHeader.biPlanes = 1;
    bmpInfo.bmiHeader.biBitCount = activeDS.jd.output_components*8;
    bmpInfo.bmiHeader.biCompression = BI_RGB;
    bmpInfo.bmiHeader.biSizeImage = 0;
    bmpInfo.bmiHeader.biXPelsPerMeter = 0;
    bmpInfo.bmiHeader.biYPelsPerMeter = 0;
    bmpInfo.bmiHeader.biClrUsed = 0;
    bmpInfo.bmiHeader.biClrImportant = 0;
388
    hDIB = CreateDIBSection (0, &bmpInfo, DIB_RGB_COLORS, (LPVOID)&bits, 0, 0);
389 390
    if (!hDIB) {
	FIXME("Failed creating DIB.\n");
391
        close_current_file();
392 393 394 395 396 397
	activeDS.twCC = TWCC_LOWMEMORY;
	return TWRC_FAILURE;
    }
    samprow = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,activeDS.jd.output_width*activeDS.jd.output_components);
    oldsamprow = samprow;
    while ( activeDS.jd.output_scanline<activeDS.jd.output_height ) {
398
        unsigned int i;
399
        int x = jpeg_read_scanlines(&activeDS.jd,&samprow,1);
400 401 402 403 404 405 406 407 408 409 410 411 412 413
	if (x != 1) {
		FIXME("failed to read current scanline?\n");
		break;
	}
	/* We have to convert from RGB to BGR, see MSDN/ BITMAPINFOHEADER */
	for(i=0;i<activeDS.jd.output_width;i++,samprow+=activeDS.jd.output_components) {
	    *(bits++) = *(samprow+2);
	    *(bits++) = *(samprow+1);
	    *(bits++) = *(samprow);
	}
	bits = (LPBYTE)(((UINT_PTR)bits + 3) & ~3);
	samprow = oldsamprow;
    }
    HeapFree (GetProcessHeap(), 0, samprow);
414
    close_current_file();
415
    *pHandle = (UINT_PTR)hDIB;
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
    activeDS.twCC = TWCC_SUCCESS;
    activeDS.currentState = 7;
    return TWRC_XFERDONE;
}

/* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_GET */
TW_UINT16 GPHOTO2_JPEGCompressionGet (pTW_IDENTITY pOrigin, 
                                    TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

/* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_GETDEFAULT */
TW_UINT16 GPHOTO2_JPEGCompressionGetDefault (pTW_IDENTITY pOrigin,
                                           
                                           TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

/* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_RESET */
TW_UINT16 GPHOTO2_JPEGCompressionReset (pTW_IDENTITY pOrigin, 
                                      TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

/* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_SET */
TW_UINT16 GPHOTO2_JPEGCompressionSet (pTW_IDENTITY pOrigin, 
                                    TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

/* DG_IMAGE/DAT_PALETTE8/MSG_GET */
TW_UINT16 GPHOTO2_Palette8Get (pTW_IDENTITY pOrigin, 
                             TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

/* DG_IMAGE/DAT_PALETTE8/MSG_GETDEFAULT */
TW_UINT16 GPHOTO2_Palette8GetDefault (pTW_IDENTITY pOrigin, 
                                    TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

/* DG_IMAGE/DAT_PALETTE8/MSG_RESET */
TW_UINT16 GPHOTO2_Palette8Reset (pTW_IDENTITY pOrigin, 
                               TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

/* DG_IMAGE/DAT_PALETTE8/MSG_SET */
TW_UINT16 GPHOTO2_Palette8Set (pTW_IDENTITY pOrigin, 
                             TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

/* DG_IMAGE/DAT_RGBRESPONSE/MSG_RESET */
TW_UINT16 GPHOTO2_RGBResponseReset (pTW_IDENTITY pOrigin, 
                                  TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

/* DG_IMAGE/DAT_RGBRESPONSE/MSG_SET */
TW_UINT16 GPHOTO2_RGBResponseSet (pTW_IDENTITY pOrigin, 
                                TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

TW_UINT16
513 514 515 516
_get_gphoto2_file_as_DIB( unsigned int idx, BOOL preview, HWND hwnd, HBITMAP *hDIB )
{
    unsigned char *filedata;
    int ret;
517 518 519 520
    struct jpeg_source_mgr		xjsm;
    struct jpeg_decompress_struct	jd;
    struct jpeg_error_mgr		jerr;
    BITMAPINFO 		bmpInfo;
521
    LPBYTE		bits;
522
    JSAMPROW		samprow, oldsamprow;
523 524
    struct open_file_params open_params;
    struct get_file_data_params get_data_params;
525
    UINT64 file_handle;
526
    unsigned int filesize;
527

528 529 530 531 532 533 534
    open_params.idx     = idx;
    open_params.preview = preview;
    open_params.handle  = &file_handle;
    open_params.size    = &filesize;
    if (GPHOTO2_CALL( open_file, &open_params ))
    {
	FIXME( "Failed to get file %u\n", idx);
535 536
	return TWRC_FAILURE;
    }
537 538 539 540 541 542 543 544
    filedata = malloc( filesize );
    get_data_params.handle = file_handle;
    get_data_params.data   = filedata;
    get_data_params.size   = filesize;
    if (GPHOTO2_CALL( get_file_data, &get_data_params ))
    {
        close_file( file_handle );
        free( filedata );
545 546 547 548 549
	return TWRC_FAILURE;
    }

    /* FIXME: Actually we might get other types than JPEG ... But only handle JPEG for now */
    if (filedata[0] != 0xff) {
550
	ERR("File %u might not be JPEG, cannot decode!\n", idx);
551 552 553 554 555 556 557 558 559 560 561 562 563
    }

    /* This is basically so we can use in-memory data for jpeg decompression.
     * We need to have all the functions.
     */
    xjsm.next_input_byte	= filedata;
    xjsm.bytes_in_buffer	= filesize;
    xjsm.init_source	= _jpeg_init_source;
    xjsm.fill_input_buffer	= _jpeg_fill_input_buffer;
    xjsm.skip_input_data	= _jpeg_skip_input_data;
    xjsm.resync_to_restart	= _jpeg_resync_to_restart;
    xjsm.term_source	= _jpeg_term_source;

564
    jd.err = jpeg_std_error(&jerr);
565 566
    /* jpeg_create_decompress is a macro that expands to jpeg_CreateDecompress - see jpeglib.h
     * jpeg_create_decompress(&jd); */
567
    jpeg_CreateDecompress(&jd, JPEG_LIB_VERSION, sizeof(struct jpeg_decompress_struct));
568
    jd.src = &xjsm;
569
    ret=jpeg_read_header(&jd,TRUE);
570
    jd.out_color_space = JCS_RGB;
571
    jpeg_start_decompress(&jd);
572 573
    if (ret != JPEG_HEADER_OK) {
	ERR("Jpeg image in stream has bad format, read header returned %d.\n",ret);
574 575
        close_file( file_handle );
        free( filedata );
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
	return TWRC_FAILURE;
    }

    ZeroMemory (&bmpInfo, sizeof (BITMAPINFO));
    bmpInfo.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
    bmpInfo.bmiHeader.biWidth = jd.output_width;
    bmpInfo.bmiHeader.biHeight = -jd.output_height;
    bmpInfo.bmiHeader.biPlanes = 1;
    bmpInfo.bmiHeader.biBitCount = jd.output_components*8;
    bmpInfo.bmiHeader.biCompression = BI_RGB;
    bmpInfo.bmiHeader.biSizeImage = 0;
    bmpInfo.bmiHeader.biXPelsPerMeter = 0;
    bmpInfo.bmiHeader.biYPelsPerMeter = 0;
    bmpInfo.bmiHeader.biClrUsed = 0;
    bmpInfo.bmiHeader.biClrImportant = 0;
591
    *hDIB = CreateDIBSection(0, &bmpInfo, DIB_RGB_COLORS, (LPVOID)&bits, 0, 0);
592 593
    if (!*hDIB) {
	FIXME("Failed creating DIB.\n");
594 595
        close_file( file_handle );
        free( filedata );
596 597 598 599 600
	return TWRC_FAILURE;
    }
    samprow = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,jd.output_width*jd.output_components);
    oldsamprow = samprow;
    while ( jd.output_scanline<jd.output_height ) {
601
        unsigned int i;
602
        int x = jpeg_read_scanlines(&jd,&samprow,1);
603 604 605 606 607 608 609 610 611 612 613 614 615 616
	if (x != 1) {
	    FIXME("failed to read current scanline?\n");
	    break;
	}
	/* We have to convert from RGB to BGR, see MSDN/ BITMAPINFOHEADER */
	for(i=0;i<jd.output_width;i++,samprow+=jd.output_components) {
	    *(bits++) = *(samprow+2);
	    *(bits++) = *(samprow+1);
	    *(bits++) = *(samprow);
	}
	bits = (LPBYTE)(((UINT_PTR)bits + 3) & ~3);
	samprow = oldsamprow;
    }
    HeapFree (GetProcessHeap(), 0, samprow);
617 618
    close_file( file_handle );
    free( filedata );
619 620
    return TWRC_SUCCESS;
}