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.

144 lines
2.5 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. bmp2tiff.c
  5. Abstract:
  6. This file contains support for converting a
  7. Windows BMP file to a TIFF file.
  8. Environment:
  9. WIN32 User Mode
  10. Author:
  11. Wesley Witt (wesw) 17-Feb-1996
  12. --*/
  13. #include "test.h"
  14. #pragma hdrstop
  15. DWORD
  16. ConvertBmpToTiff(
  17. LPTSTR BmpFile,
  18. LPTSTR TiffFile,
  19. DWORD CompressionType
  20. )
  21. /*++
  22. Routine Description:
  23. Converts a BMP file to a TIFF file.
  24. Arguments:
  25. BmpFile - BMP file name
  26. TiffFile - TIFF file name
  27. CompressionType - Compression method, see tifflib.h
  28. Return Value:
  29. None.
  30. --*/
  31. {
  32. HANDLE hFileIn;
  33. HANDLE hMapIn;
  34. LPVOID FilePtrIn;
  35. HANDLE hTiff;
  36. PBMPINFO BmpInfo;
  37. DWORD LineWidth;
  38. DWORD FileSize;
  39. LPBYTE Bits;
  40. DWORD i,j;
  41. LPBYTE SrcPtr;
  42. DWORD RealWidth;
  43. BYTE BitBuffer[(1728/8)*2];
  44. hFileIn = CreateFile(
  45. BmpFile,
  46. GENERIC_READ,
  47. FILE_SHARE_READ,
  48. NULL,
  49. OPEN_EXISTING,
  50. 0,
  51. NULL
  52. );
  53. if (hFileIn != INVALID_HANDLE_VALUE) {
  54. return GetLastError();
  55. }
  56. hMapIn = CreateFileMapping(
  57. hFileIn,
  58. NULL,
  59. PAGE_READONLY | SEC_COMMIT,
  60. 0,
  61. 0,
  62. NULL
  63. );
  64. if (!hMapIn) {
  65. return GetLastError();
  66. }
  67. FilePtrIn = MapViewOfFile(
  68. hMapIn,
  69. FILE_MAP_READ,
  70. 0,
  71. 0,
  72. 0
  73. );
  74. if (!FilePtrIn) {
  75. return GetLastError();
  76. }
  77. FileSize = GetFileSize( hFileIn, NULL );
  78. BmpInfo = (PBMPINFO) FilePtrIn;
  79. LineWidth = BmpInfo->SizeImage / BmpInfo->Height;
  80. Bits = (LPBYTE) ( (LPBYTE)FilePtrIn + BmpInfo->Offset );
  81. SrcPtr = ((LPBYTE)FilePtrIn + BmpInfo->Offset) + (LineWidth * (BmpInfo->Height - 1));
  82. RealWidth = Align( 8, BmpInfo->Width ) / 8;
  83. hTiff = TiffCreate( TiffFile, CompressionType, LineWidth*8, 1, 1 );
  84. if (!hTiff) {
  85. return GetLastError();
  86. }
  87. TiffStartPage( hTiff );
  88. for (i=0; i<BmpInfo->Height; i++) {
  89. FillMemory( BitBuffer, sizeof(BitBuffer), 0xff );
  90. CopyMemory( BitBuffer, SrcPtr, RealWidth );
  91. if (BmpInfo->Width % 8) {
  92. BitBuffer[BmpInfo->Width/8] |= 0xf;
  93. }
  94. for (j=0; j<sizeof(BitBuffer); j++) {
  95. BitBuffer[j] ^= 0xff;
  96. }
  97. SrcPtr -= LineWidth;
  98. TiffWrite( hTiff, BitBuffer );
  99. }
  100. TiffEndPage( hTiff );
  101. UnmapViewOfFile( FilePtrIn );
  102. CloseHandle( hMapIn );
  103. CloseHandle( hFileIn );
  104. TiffClose( hTiff );
  105. return 0;
  106. }