Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
5.3 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. tiff2bmp.c
  5. Abstract:
  6. This file contains support for converting a
  7. TIFF file to a Windows BMP file.
  8. Environment:
  9. WIN32 User Mode
  10. Author:
  11. Wesley Witt (wesw) 17-Feb-1996
  12. --*/
  13. #include "test.h"
  14. #include "tiff.h"
  15. #include <winfax.h>
  16. #pragma hdrstop
  17. void PrintTheTiff(
  18. LPTSTR TiffFile
  19. )
  20. {
  21. FAX_PRINT_INFO PrintInfo;
  22. FAX_CONTEXT_INFOW ContextInfo;
  23. LPTSTR FullPath = TEXT("c:\\temp\\thetiff.tif");
  24. DWORD TmpFaxJobId;
  25. BOOL Rslt;
  26. ZeroMemory( &PrintInfo, sizeof(FAX_PRINT_INFOW) );
  27. PrintInfo.SizeOfStruct = sizeof(FAX_PRINT_INFOW);
  28. PrintInfo.OutputFileName = FullPath;
  29. ZeroMemory( &ContextInfo, sizeof(FAX_CONTEXT_INFOW) );
  30. ContextInfo.SizeOfStruct = sizeof(FAX_CONTEXT_INFOW);
  31. if (!FaxStartPrintJobW( NULL, &PrintInfo, &TmpFaxJobId, &ContextInfo )) {
  32. DeleteFile( FullPath );
  33. SetLastError( ERROR_INVALID_FUNCTION );
  34. return;
  35. }
  36. Rslt = PrintTiffFile( ContextInfo.hDC, TiffFile );
  37. EndDoc( ContextInfo.hDC );
  38. DeleteDC( ContextInfo.hDC );
  39. }
  40. DWORD
  41. ConvertTiffToBmp(
  42. LPTSTR TiffFile,
  43. LPTSTR BmpFile
  44. )
  45. /*++
  46. Routine Description:
  47. Converts a TIFF file to a BMP file.
  48. Arguments:
  49. TiffFile - TIFF file name
  50. BmpFile - BMP file name
  51. Return Value:
  52. None.
  53. --*/
  54. {
  55. HANDLE hTiff;
  56. HANDLE hBmp;
  57. HANDLE hMap;
  58. LPVOID fPtr;
  59. PBMPINFO BmpInfo;
  60. PWINRGBQUAD Palette;
  61. LPBYTE dPtr;
  62. LPBYTE sPtr;
  63. DWORD i,j;
  64. LPBYTE BmpData;
  65. TIFF_INFO TiffInfo;
  66. hTiff = TiffOpen( TiffFile, &TiffInfo, TRUE, FILLORDER_MSB2LSB );
  67. if (!hTiff) {
  68. _tprintf( TEXT("could not open tiff file\n") );
  69. return 0;
  70. }
  71. _tprintf( TEXT("ImageWidth:\t%d\n"),TiffInfo.ImageWidth);
  72. _tprintf( TEXT("ImageHeight:\t%d\n"),TiffInfo.ImageHeight);
  73. _tprintf( TEXT("PageCount:\t%d\n"),TiffInfo.PageCount);
  74. _tprintf( TEXT("Photometric:\t%d\n"),TiffInfo.PhotometricInterpretation);
  75. _tprintf( TEXT("ImageSize:\t%d\n"),TiffInfo.ImageSize);
  76. _tprintf( TEXT("Compression:\t%d\n"),TiffInfo.CompressionType);
  77. _tprintf( TEXT("FillOrder:\t%d\n"),TiffInfo.FillOrder);
  78. _tprintf( TEXT("YResolution:\t%d\n"),TiffInfo.YResolution);
  79. BmpData = VirtualAlloc(
  80. NULL,
  81. TiffInfo.ImageHeight * (TiffInfo.ImageWidth / 8),
  82. MEM_COMMIT,
  83. PAGE_READWRITE
  84. );
  85. if (!BmpData) {
  86. _tprintf( TEXT("could allocate memory for bmp data\n") );
  87. return 0;
  88. }
  89. if (!TiffRead( hTiff, BmpData,0 )) {
  90. _tprintf( TEXT("could read tiff data\n") );
  91. TiffClose( hTiff );
  92. return 0;
  93. }
  94. hBmp = CreateFile(
  95. BmpFile,
  96. GENERIC_READ | GENERIC_WRITE,
  97. 0,
  98. NULL,
  99. CREATE_ALWAYS,
  100. 0,
  101. NULL
  102. );
  103. if (hBmp == INVALID_HANDLE_VALUE) {
  104. return 0;
  105. }
  106. hMap = CreateFileMapping(
  107. hBmp,
  108. NULL,
  109. PAGE_READWRITE | SEC_COMMIT,
  110. 0,
  111. 1024*1024*3, // 3 meg
  112. NULL
  113. );
  114. if (!hMap) {
  115. return GetLastError();
  116. }
  117. fPtr = MapViewOfFile(
  118. hMap,
  119. FILE_MAP_WRITE,
  120. 0,
  121. 0,
  122. 0
  123. );
  124. if (!fPtr) {
  125. return GetLastError();
  126. }
  127. BmpInfo = (PBMPINFO) fPtr;
  128. Palette = (PWINRGBQUAD) (BmpInfo + 1);
  129. BmpInfo->Type = 0x4d42;
  130. BmpInfo->FileSize = sizeof(BMPINFO) + ((TiffInfo.ImageWidth / 8) * TiffInfo.ImageHeight);
  131. BmpInfo->Reserved1 = 0;
  132. BmpInfo->Reserved2 = 0;
  133. BmpInfo->Offset = sizeof(BMPINFO) + (sizeof(WINRGBQUAD) * 2);
  134. BmpInfo->Size = sizeof(BMPINFO) - FIELD_OFFSET(BMPINFO,Size);
  135. BmpInfo->Width = TiffInfo.ImageWidth;
  136. BmpInfo->Height = TiffInfo.ImageHeight;
  137. BmpInfo->Planes = 1;
  138. BmpInfo->BitCount = 1;
  139. BmpInfo->Compression = 0;
  140. BmpInfo->SizeImage = (TiffInfo.ImageWidth / 8) * TiffInfo.ImageHeight;
  141. BmpInfo->XPelsPerMeter = 0;
  142. BmpInfo->YPelsPerMeter = 0;
  143. BmpInfo->ClrUsed = 0;
  144. BmpInfo->ClrImportant = 0;
  145. if (TiffInfo.PhotometricInterpretation) {
  146. //
  147. // minimum is black.
  148. //
  149. Palette[1].rgbBlue = 0;
  150. Palette[1].rgbGreen = 0;
  151. Palette[1].rgbRed = 0;
  152. Palette[1].rgbReserved = 0;
  153. Palette[0].rgbBlue = 0xff;
  154. Palette[0].rgbGreen = 0xff;
  155. Palette[0].rgbRed = 0xff;
  156. Palette[0].rgbReserved = 0;
  157. } else {
  158. //
  159. // minimum is white
  160. //
  161. Palette[0].rgbBlue = 0;
  162. Palette[0].rgbGreen = 0;
  163. Palette[0].rgbRed = 0;
  164. Palette[0].rgbReserved = 0;
  165. Palette[1].rgbBlue = 0xff;
  166. Palette[1].rgbGreen = 0xff;
  167. Palette[1].rgbRed = 0xff;
  168. Palette[1].rgbReserved = 0;
  169. }
  170. sPtr = (LPBYTE) (BmpData + ((TiffInfo.ImageHeight-1)*(TiffInfo.ImageWidth/8)));
  171. dPtr = (LPBYTE) ((LPBYTE)(Palette + 2));
  172. //
  173. // capture the data
  174. //
  175. for (i=0; i<TiffInfo.ImageHeight; i++) {
  176. CopyMemory( dPtr, sPtr, TiffInfo.ImageWidth/8 );
  177. for (j=0; j<(TiffInfo.ImageWidth/8); j++) {
  178. dPtr[j] ^= 0xff;
  179. }
  180. sPtr -= (TiffInfo.ImageWidth/8);
  181. dPtr += (TiffInfo.ImageWidth/8);
  182. }
  183. UnmapViewOfFile( fPtr );
  184. CloseHandle( hMap );
  185. CloseHandle( hBmp );
  186. TiffClose( hTiff );
  187. PrintTheTiff(TiffFile);
  188. return 0;
  189. }