1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
111
112
113
114
115
116
117
118
119
120
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
|
/**************************************************************************
*
* Copyright � Microsoft Corporation
*
* Title: FileConv.cpp
*
* Description: This file contains implementation of utility functions for
* image file format conversions used by the sample driver.
*
***************************************************************************/
#include "stdafx.h"
//
// The GDI+ codec name used for the DIB file format:
//
#define GDIPLUS_BMP_ENCODER L"image/bmp"
/**************************************************************************\
*
* Inline function to convert a GDI+ result code to a COM HRESULT,
* similar to the HRESULT_FOM_WIN32 macro.
*
* Parameters:
*
* status - GDI+ return code value
*
* Return Value:
*
* HRESULT describing the GDI+ status, E_FAIL if conversion is possible
*
\**************************************************************************/
inline HRESULT
GDISTATUS_TO_HRESULT(
Gdiplus::Status status)
{
HRESULT hr = E_FAIL;
DWORD dwErr = NO_ERROR;
switch (status)
{
case Gdiplus::Ok:
hr = S_OK;
break;
case Gdiplus::InvalidParameter:
WIAEX_ERROR((g_hInst, "GDI+: Gdiplus::InvalidParameter"));
hr = E_INVALIDARG;
break;
case Gdiplus::OutOfMemory:
WIAEX_ERROR((g_hInst, "GDI+: Gdiplus::OutOfMemory"));
hr = E_OUTOFMEMORY;
break;
case Gdiplus::InsufficientBuffer:
WIAEX_ERROR((g_hInst, "GDI+: Gdiplus::InsufficientBuffer"));
hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
break;
case Gdiplus::Aborted:
WIAEX_ERROR((g_hInst, "GDI+: Gdiplus::Aborted"));
hr = E_ABORT;
break;
case Gdiplus::ObjectBusy:
WIAEX_ERROR((g_hInst, "GDI+: Gdiplus::ObjectBusy"));
hr = E_PENDING;
break;
case Gdiplus::FileNotFound:
WIAEX_ERROR((g_hInst, "GDI+: Gdiplus::FileNotFound"));
hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
break;
case Gdiplus::AccessDenied:
WIAEX_ERROR((g_hInst, "GDI+: Gdiplus::AccessDenied"));
hr = E_ACCESSDENIED;
break;
case Gdiplus::UnknownImageFormat:
WIAEX_ERROR((g_hInst, "GDI+: Gdiplus::UnknownImageFormat"));
hr = HRESULT_FROM_WIN32(ERROR_INVALID_PIXEL_FORMAT);
break;
case Gdiplus::NotImplemented:
WIAEX_ERROR((g_hInst, "GDI+: Gdiplus::NotImplemented"));
hr = E_NOTIMPL;
break;
case Gdiplus::Win32Error:
dwErr = GetLastError();
WIAEX_ERROR((g_hInst, "GDI+: Gdiplus::Win32Error, last error: 0x%08X", dwErr));
hr = HRESULT_FROM_WIN32(dwErr);
break;
case Gdiplus::ValueOverflow:
WIAEX_ERROR((g_hInst, "GDI+: Gdiplus::ValueOverflow"));
hr = E_FAIL;
break;
case Gdiplus::FontFamilyNotFound:
WIAEX_ERROR((g_hInst, "GDI+: Gdiplus::FontFamilyNotFound"));
hr = E_FAIL;
break;
case Gdiplus::FontStyleNotFound:
WIAEX_ERROR((g_hInst, "GDI+: Gdiplus::FontFamilyNotFound"));
hr = E_FAIL;
break;
case Gdiplus::NotTrueTypeFont:
WIAEX_ERROR((g_hInst, "GDI+: Gdiplus::NotTrueTypeFont"));
hr = E_FAIL;
break;
case Gdiplus::UnsupportedGdiplusVersion:
WIAEX_ERROR((g_hInst, "GDI+: Gdiplus::UnsupportedGdiplusVersion"));
hr = E_FAIL;
break;
case Gdiplus::GdiplusNotInitialized:
WIAEX_ERROR((g_hInst, "GDI+: Gdiplus::GdiplusNotInitialized"));
hr = E_FAIL;
break;
case Gdiplus::WrongState:
WIAEX_ERROR((g_hInst, "GDI+: Gdiplus::WrongState"));
hr = E_FAIL;
break;
default:
WIAEX_ERROR((g_hInst, "GDI+: unknown Gdiplus status code (%u)", (ULONG)status));
hr = E_FAIL;
}
return hr;
}
/**************************************************************************\
*
* Executes GdiplusStartup to initialize the GDI+ engine. Must be called
* before calling ConvertImageToDIB. A matching ShutdownGDIPlus call
* must be always made to stop the GDI+ engine started by this function.
*
* Parameters:
*
* ppToken - token returned by GDI+, must be used when calling ShutdownGDIPlus
*
* Return Value:
*
* S_OK if successful, an error HRESULT otherwise
*
\**************************************************************************/
HRESULT
InitializeGDIPlus(
_Out_ ULONG_PTR *ppToken)
{
HRESULT hr = S_OK;
GdiplusStartupInput gdiplusStartupInput;
if (!ppToken)
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Invalid parameter, hr = 0x%08X", hr));
}
//
// Start the GDI+ engine:
//
if (SUCCEEDED(hr))
{
hr = GDISTATUS_TO_HRESULT(GdiplusStartup(ppToken, &gdiplusStartupInput, NULL));
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Gdiplus::GdiplusStartup failed, hr = 0x%08X", hr));
}
}
return hr;
}
/**************************************************************************\
*
* Executes GdiplusShutdown to shutdown the GDI+ engine.
*
* Parameters:
*
* pToken - token obtained from a previous InitializeGDIPlus call
*
* Return Value:
*
* E_INVALIDARG if called with a NULL pToken parameter or S_OK
* (GDI+ does not return a result code for GdiplusShutdown)
*
\**************************************************************************/
HRESULT
ShutdownGDIPlus(
_In_ ULONG_PTR pToken)
{
HRESULT hr = S_OK;
if (!pToken)
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Invalid parameter, hr = 0x%08X", hr));
}
//
// Shutdown the GDI+ engine:
//
if (SUCCEEDED(hr))
{
GdiplusShutdown(pToken);
}
return hr;
}
/**************************************************************************\
*
* Internal helper for ConvertImageToDIB. Enumerates available GDI+ image
* format encoders and returns the CLSID of the specified encoder, if available.
*
* Parameters:
*
* wszFormat - GDI+ format name, e.g. "image/bmp" for DIB
* pClisid - returns the CLSID of the GDI+ encoder
*
* Return Value:
*
* S_OK if successful, an error HRESULT otherwise
*
\**************************************************************************/
HRESULT
_GetEncoderCLSID(
_In_ LPCWSTR wszFormat,
_Out_ CLSID *pClsid)
{
HRESULT hr = S_OK;
UINT nNumEncoders = 0;
UINT cbEncoderSize = 0;
ImageCodecInfo *pImageCodecInfo = NULL;
if ((!wszFormat) || (!pClsid))
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Invalid parameter, hr = 0x%08X", hr));
}
if (SUCCEEDED(hr))
{
hr = GDISTATUS_TO_HRESULT(GetImageEncodersSize(&nNumEncoders, &cbEncoderSize));
if (SUCCEEDED(hr) && (!cbEncoderSize))
{
hr = E_FAIL;
}
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Gdiplus::GetImageEncodersSize(%ws) failed, hr = 0x%08X", wszFormat, hr));
}
}
if (SUCCEEDED(hr))
{
pImageCodecInfo = (ImageCodecInfo*)new BYTE[cbEncoderSize];
if (!pImageCodecInfo)
{
hr = E_OUTOFMEMORY;
WIAEX_ERROR((g_hInst, "Failed to allocate memory for the encoder info, hr = 0x%08X", hr));
}
}
if (SUCCEEDED(hr))
{
hr = GDISTATUS_TO_HRESULT(GetImageEncoders(nNumEncoders, cbEncoderSize, pImageCodecInfo));
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Gdiplus::GetImageEncoders(%u encoders) failed, hr = 0x%08X", nNumEncoders, hr));
}
}
if (SUCCEEDED(hr))
{
hr = E_FAIL;
for (UINT j = 0; j < nNumEncoders; ++j)
{
_Analysis_assume_(cbEncoderSize >= nNumEncoders * sizeof(ImageCodecInfo));
_Analysis_assume_(wcslen(wszFormat) < (cbEncoderSize / sizeof(WCHAR)));
if (!wcscmp(pImageCodecInfo[j].MimeType, wszFormat))
{
*pClsid = pImageCodecInfo[j].Clsid;
hr = S_OK;
break;
}
}
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "No %ws encoder found in %u available GDI+ encoders, hr = 0x%08X", wszFormat, nNumEncoders, hr));
}
}
if (pImageCodecInfo)
{
delete[] pImageCodecInfo;
}
return hr;
}
/**************************************************************************\
*
* Converts a GDI+ compatible image to a Windows DIB.
*
* Parameters:
*
* pInputImage - GDI+ Image object containing the image to be converted
* ppOutputStream - returns a new global memory IStream containing the
* converted DIB image file (must be released by caller)
* plImageWidth - (optional) returns the width of the image, in pixels
* plImageHeight - (optional) returns the height of the image, in pixels
* plOutputImageBPL - (optional) returns the estimated number of bytes
* per line for the converted DIB image
*
* Remarks:
*
* The caller is responsible to release the returned IStream object to
* free the memory after successful execution of this function.
*
* The caller must execute InitializeGDIPlus before calling this function.
*
* Return Value:
*
* S_OK if successful, an error HRESULT otherwise
*
\**************************************************************************/
HRESULT
ConvertImageToDIB(
_In_ Image *pInputImage,
_Outptr_ IStream **ppOutputStream,
_Out_opt_ LONG *plImageWidth,
_Out_opt_ LONG *plImageHeight,
_Out_opt_ LONG *plOutputImageBPL)
{
HRESULT hr = S_OK;
CLSID clsidBmpEncoder = {};
LONG lImageWidth = 0;
LONG lImageHeight = 0;
LONG lOutputBPL = 0;
LONG lBitDepth = 0;
WIAEX_TRACE_BEGIN;
if ((!pInputImage) || (!ppOutputStream))
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Invalid parameter, hr = 0x%08X", hr));
}
//
// Read the dimensions of the input image:
//
if (SUCCEEDED(hr))
{
*ppOutputStream = NULL;
lImageWidth = pInputImage->GetWidth();
lImageHeight = pInputImage->GetHeight();
lBitDepth = GetPixelFormatSize(pInputImage->GetPixelFormat());
if ((lImageWidth > 0) && (lImageHeight > 0) && (lBitDepth > 0))
{
lOutputBPL = BytesPerLine(lImageWidth, lBitDepth);
WIAS_TRACE((g_hInst, "Input image is %u x %u pixels, %u bpp, %u BPL (estimated) on output",
lImageWidth, lImageHeight, lBitDepth, lOutputBPL));
}
else
{
hr = E_FAIL;
WIAEX_ERROR((g_hInst, "Incorrect image dimensions reported by GDI+ (%d x %d pixels, %d bpp), hr = 0x%08X",
lImageWidth, lImageHeight, lBitDepth, hr));
}
}
//
// Retrieve the GDI+ encoder necessary to convert the input image to a DIB:
//
if (SUCCEEDED(hr))
{
hr = _GetEncoderCLSID(GDIPLUS_BMP_ENCODER, &clsidBmpEncoder);
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "The GDI+ %ws encoder appears to be missing or improperly installed, hr = 0x%08X",
GDIPLUS_BMP_ENCODER, hr));
}
}
//
// Create the output stream in global memory:
//
if (SUCCEEDED(hr))
{
hr = CreateStreamOnHGlobal(NULL, TRUE, ppOutputStream);
if (SUCCEEDED(hr) && (!(*ppOutputStream)))
{
hr = E_FAIL;
}
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "CreateStreamOnHGlobal failed, hr = 0x%08X", hr));
}
}
//
// Convert and save the image stored in the Image object to the output stream as a DIB:
//
if (SUCCEEDED(hr))
{
hr = GDISTATUS_TO_HRESULT(pInputImage->Save(*ppOutputStream, &clsidBmpEncoder));
if (FAILED(hr))
{
WIAEX_ERROR((g_hInst, "Image::Save(%ws) failed, hr = 0x%08X", GDIPLUS_BMP_ENCODER, hr));
}
}
if (FAILED(hr))
{
if (ppOutputStream && *ppOutputStream)
{
(*ppOutputStream)->Release();
*ppOutputStream = NULL;
}
}
if (SUCCEEDED(hr))
{
if (plImageWidth)
{
*plImageWidth = lImageWidth;
}
if (plImageHeight)
{
*plImageHeight = lImageHeight;
}
if (plOutputImageBPL)
{
*plOutputImageBPL = lOutputBPL;
}
}
WIAEX_TRACE_FUNC_HR;
return hr;
}
/**************************************************************************\
*
* Converts a 8-bpp grayscale or 24-bpp RGB color DIB to a WIA Raw image.
*
* Parameters:
*
* ppInputStream - input stream containing the DIB to be converted
* ppOutputStream - returns a new global memory IStream containing the
* converted Raw image file (must be released by caller)
*
* Remarks:
*
* The current form of this function for simplicty supports only 8-bpp
* Grayscale and 24-bpp RGB color images. Palettes are not supported.
*
* The caller is responsible to release the returned IStream object to
* free the memory after successful execution of this function.
*
* Return Value:
*
* S_OK if successful, an error HRESULT otherwise
*
\**************************************************************************/
HRESULT
ConvertDibToRaw(
_In_ IStream *pInputStream,
_Out_ IStream **ppOutputStream)
{
HRESULT hr = S_OK;
LARGE_INTEGER liOffset = {};
WIA_RAW_HEADER wiaRawHeader = {};
BYTE bGrayPalette[256] = {};
BITMAPINFOHEADER bih = {};
ULONG ulDataSize = 0;
ULONG ulDataWritten = 0;
ULONG ulPaletteSize = 0;
WIAEX_TRACE_BEGIN;
if ((!pInputStream) || (!ppOutputStream))
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Invalid parameter, hr = 0x%08.8X", hr));
}
else
{
*ppOutputStream = NULL;
}
liOffset.LowPart = sizeof(BITMAPFILEHEADER);
hr = pInputStream->Seek(liOffset, STREAM_SEEK_SET, NULL);
if (S_OK != hr)
{
WIAEX_ERROR((g_hInst, "IStream::Seek(%u, STREAM_SEEK_SET, NULL) failed, hr = 0x%08X", liOffset.LowPart, hr));
}
if (S_OK == hr)
{
//
// Extract the DIB header from the input stream:
//
hr = pInputStream->Read((void *)&bih, sizeof(bih), &ulDataSize);
if ((S_OK == hr) && (ulDataSize != sizeof(bih)))
{
hr = E_FAIL;
WIAEX_ERROR((g_hInst, "Expected to read %u bytes for the DIB header, got %u bytes, hr = 0x%08X",
sizeof(bih), ulDataSize, hr));
}
//
// Validate the input DIB and fill in the output Raw header:
//
if (S_OK == hr)
{
if ((bih.biBitCount != 24) && (bih.biBitCount != 8))
{
hr = E_INVALIDARG;
WIAEX_ERROR((g_hInst, "Test image must be either 8-bpp Grayscale or 24-bpp RGB Color, hr = 0x%08X",
sizeof(bih), ulDataSize, hr));
}
else
{
const char szRawSignature[] = "WRAW";
memcpy(&wiaRawHeader.Tag, szRawSignature, sizeof(DWORD));
wiaRawHeader.Version = 0x00010000;
wiaRawHeader.HeaderSize = sizeof(wiaRawHeader);
wiaRawHeader.XExtent = bih.biWidth;
wiaRawHeader.YExtent = bih.biHeight;
wiaRawHeader.LineOrder = (bih.biHeight < 0) ? WIA_LINE_ORDER_TOP_TO_BOTTOM : WIA_LINE_ORDER_BOTTOM_TO_TOP;
wiaRawHeader.BitsPerPixel = bih.biBitCount;
wiaRawHeader.BytesPerLine = BytesPerLine(bih.biWidth, bih.biBitCount);
wiaRawHeader.ChannelsPerPixel = (8 == bih.biBitCount) ? 1 : 3;
wiaRawHeader.DataType = (8 == bih.biBitCount) ? WIA_DATA_GRAYSCALE : WIA_DATA_RAW_BGR;
wiaRawHeader.BitsPerChannel[0] = 8;
wiaRawHeader.BitsPerChannel[1] = (8 == bih.biBitCount) ? 0 : 8;
wiaRawHeader.BitsPerChannel[2] = (8 == bih.biBitCount) ? 0 : 8;
wiaRawHeader.Compression = WIA_COMPRESSION_NONE;
wiaRawHeader.PhotometricInterp = WIA_PHOTO_WHITE_1;
//
// The scan resolution is fixed for this sample driver:
//
wiaRawHeader.XRes = OPTICAL_RESOLUTION;
wiaRawHeader.YRes = OPTICAL_RESOLUTION;
ulDataSize = wiaRawHeader.BytesPerLine * wiaRawHeader.YExtent;
ulPaletteSize = (8 == bih.biBitCount) ? (256 * sizeof(BYTE)) : 0;
wiaRawHeader.RawDataSize = ulDataSize;
wiaRawHeader.RawDataOffset = ulPaletteSize;
wiaRawHeader.PaletteOffset = 0;
wiaRawHeader.PaletteSize = ulPaletteSize;
//
// For 8-bpp Grayscale data prepare a standard grayscale palette with entries sorted
// in increasing or decreasing order depending on the photometric interpretation:
//
if (8 == bih.biBitCount)
{
for (UINT i = 0; i < 256; i++)
{
bGrayPalette[i] = (WIA_PHOTO_WHITE_1 == wiaRawHeader.PhotometricInterp) ? (BYTE)i : (255 - (BYTE)i);
}
}
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.Version = 0x%08X", wiaRawHeader.Version));
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.HeaderSize = %u bytes", wiaRawHeader.HeaderSize));
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.XExtent = %u pixels", wiaRawHeader.XExtent));
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.YExtent = %u pixels", wiaRawHeader.YExtent));
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.LineOrder = %u", wiaRawHeader.LineOrder));
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.BitsPerPixel = %u bpp", wiaRawHeader.BitsPerPixel));
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.BytesPerLine = %u BPL", wiaRawHeader.BytesPerLine));
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.ChannelsPerPixel = %u", wiaRawHeader.ChannelsPerPixel));
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.DataType = %u", wiaRawHeader.DataType));
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.BitsPerChannel[0] = %u bps", wiaRawHeader.BitsPerChannel[0]));
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.BitsPerChannel[1] = %u bps", wiaRawHeader.BitsPerChannel[1]));
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.BitsPerChannel[2] = %u bps", wiaRawHeader.BitsPerChannel[2]));
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.Compression = %u", wiaRawHeader.Compression));
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.PhotometricInterp = %u", wiaRawHeader.PhotometricInterp));
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.XRes = %u DPI", wiaRawHeader.XRes));
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.YRes = %u DPI", wiaRawHeader.YRes));
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.RawDataOffset = %u bytes", wiaRawHeader.RawDataOffset));
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.RawDataSize = %u bytes", wiaRawHeader.RawDataSize));
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.PaletteOffset = %u bytes", wiaRawHeader.PaletteOffset));
WIAS_TRACE((g_hInst, "WIA_RAW_HEADER.PaletteSize = %u bytes", wiaRawHeader.PaletteSize));
}
}
else
{
WIAEX_ERROR((g_hInst, "Failed to read the DIB header for the test image, hr = 0x%08X", hr));
}
//
// If there is a grayscale palette, jump the input stream pointer past it:
//
if ((S_OK == hr) && (8 == bih.biBitCount))
{
liOffset.LowPart = 256 * sizeof(RGBQUAD);
hr = pInputStream->Seek(liOffset, STREAM_SEEK_CUR, NULL);
if (S_OK != hr)
{
WIAEX_ERROR((g_hInst, "IStream::Seek(%u, STREAM_SEEK_SET, NULL) failed, hr = 0x%08X", liOffset.LowPart, hr));
}
}
//
// Create the output stream in memory:
//
if (S_OK == hr)
{
hr = CreateStreamOnHGlobal(NULL, TRUE, ppOutputStream);
if (SUCCEEDED(hr) && (!(*ppOutputStream)))
{
hr = E_FAIL;
}
if (S_OK != hr)
{
WIAEX_ERROR((g_hInst, "CreateStreamOnHGlobal failed, hr = 0x%08X", hr));
}
}
//
// Write the Raw header to the output stream:
//
if (S_OK == hr)
{
hr = (*ppOutputStream)->Write(&wiaRawHeader, sizeof(wiaRawHeader), &ulDataWritten);
if (SUCCEEDED(hr) && (sizeof(wiaRawHeader) != ulDataWritten))
{
hr = E_FAIL;
WIAEX_ERROR((g_hInst, "Expected to write %u bytes for the raw header, wrote %u bytes, hr = 0x%08X",
sizeof(wiaRawHeader), ulDataWritten, hr));
}
if (S_OK != hr)
{
WIAEX_ERROR((g_hInst, "IStream::Write(%u bytes) failed, hr = 0x%08X", sizeof(wiaRawHeader), hr));
}
}
//
// Write the palette (if one) to the output stream:
//
if ((S_OK == hr) && (8 == bih.biBitCount))
{
hr = (*ppOutputStream)->Write(&bGrayPalette[0], ulPaletteSize, &ulDataWritten);
if (SUCCEEDED(hr) && (ulPaletteSize != ulDataWritten))
{
hr = E_FAIL;
WIAEX_ERROR((g_hInst, "Expected to write %u bytes for the raw palette, wrote %u bytes, hr = 0x%08X",
ulPaletteSize, ulDataWritten, hr));
}
if (S_OK != hr)
{
WIAEX_ERROR((g_hInst, "IStream::Write(%u bytes) failed, hr = 0x%08X", ulPaletteSize, hr));
}
}
//
// Write the Raw image data to the output stream:
//
if (S_OK == hr)
{
ULARGE_INTEGER uliDataSize = {}, uliRead = {}, uliWritten = {};
uliDataSize.LowPart = ulDataSize;
hr = pInputStream->CopyTo(*ppOutputStream, uliDataSize, &uliRead, &uliWritten);
if ((S_OK == hr) && ((ulDataSize != uliRead.LowPart) || (ulDataSize != uliWritten.LowPart)))
{
hr = E_FAIL;
WIAEX_ERROR((g_hInst, "Expected to stream copy %u bytes for the raw image data, copied %u bytes, hr = 0x%08X",
ulDataSize, uliWritten.LowPart, hr));
}
if (S_OK != hr)
{
WIAEX_ERROR((g_hInst, "IStream::CopyTo(%u bytes) failed, hr = 0x%08X", ulDataSize, hr));
}
}
//
// Reset the output stream seek pointer at the beginning of the stream:
//
if (S_OK == hr)
{
liOffset.LowPart = 0;
hr = (*ppOutputStream)->Seek(liOffset, STREAM_SEEK_SET, NULL);
if (S_OK != hr)
{
WIAEX_ERROR((g_hInst, "IStream::Seek(0, STREAM_SEEK_SET, NULL) failed, hr = 0x%08X", hr));
}
}
}
if ((S_OK != hr) && ppOutputStream && *ppOutputStream)
{
(*ppOutputStream)->Release();
*ppOutputStream = NULL;
}
WIAEX_TRACE_FUNC_HR;
return hr;
}
|