home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / icm20 / icmview / dibinfo.c < prev    next >
C/C++ Source or Header  |  1997-09-07  |  29KB  |  868 lines

  1. //THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. //ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. //THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright  1994-1997  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  FILE:
  9. //    DIBINFO.C
  10. //
  11. //  PURPOSE:
  12. //
  13. //
  14. //  PLATFORMS:
  15. //    Windows 95, Windows NT
  16. //
  17. //  SPECIAL INSTRUCTIONS: N/A
  18. //
  19.  
  20. // Windows Header Files:
  21. #pragma warning(disable:4001)   // Single-line comment warnings
  22. #pragma warning(disable:4115)   // Named type definition in parentheses
  23. #pragma warning(disable:4201)   // Nameless struct/union warning
  24. #pragma warning(disable:4214)   // Bit field types other than int warnings
  25. #pragma warning(disable:4514)   // Unreferenced inline function has been removed
  26.  
  27. // Windows Header Files:
  28. #include <Windows.h>
  29. #include <WindowsX.h>
  30. #include "icm.h"
  31.  
  32. // Restore the warnings--leave the single-line comment warning OFF
  33. #pragma warning(default:4115)   // Named type definition in parentheses
  34. #pragma warning(default:4201)   // Nameless struct/union warning
  35. #pragma warning(default:4214)   // Bit field types other than int warnings
  36.  
  37. // C RunTime Header Files
  38. #include <tchar.h>
  39.  
  40. // Local Header Files
  41. #include "icmview.h"
  42. #include "dibinfo.h"
  43. #include "dibs.h"
  44. #include "regutil.h"
  45. #include "print.h"
  46. #include "debug.h"
  47.  
  48. // local definitions
  49. #ifndef ICM_DONE_OUTSIDEDC
  50.     #define ICM_DONE_OUTSIDEDC  4
  51. #endif
  52.  
  53. // default settings
  54.  
  55. // external functions
  56.  
  57. // external data
  58.  
  59. // public data
  60.  
  61. // private data
  62.  
  63. // public functions
  64.  
  65. // private functions
  66.  
  67. //////////////////////////////////////////////////////////////////////////
  68. //  Function:  fReadDIBInfo
  69. //
  70. //  Description:
  71. //    Will read a file in DIB format and return a global HANDLE
  72. //    to it's BITMAPINFO.  This function will work with both
  73. //    "old" (BITMAPCOREHEADER) and "new" (BITMAPINFOHEADER)
  74. //    bitmap formats, but will always return a "new" BITMAPINFO
  75. //
  76. //  Parameters:
  77. //    LPTSTR     Pointer to string containing the filename of the image.
  78. //    LPDIBINFO Pointer to DIBINFO structure.
  79. //
  80. //  Returns:
  81. //    BOOL
  82. //
  83. //  Comments:
  84. //
  85. //
  86. //////////////////////////////////////////////////////////////////////////
  87. BOOL fReadDIBInfo(LPTSTR lpszFileName, LPDIBINFO lpDIBInfo)
  88. {
  89.     // Local variables
  90.     HANDLE                hDIBFile;
  91.     HANDLE                hDIB = NULL;
  92.     LPBITMAPINFOHEADER    lpBmpInfoHdr = NULL;
  93.  
  94.     //  Initialize variables
  95.     if ( (NULL == lpszFileName) || (NULL == lpDIBInfo) )
  96.     {
  97.         DebugMsg(__TEXT("fReadDIBInfo:  NULL parameter.\r\n"));
  98.         return FALSE;
  99.     }
  100.  
  101.     // Set the filename
  102.     lpDIBInfo->lpszImageFileName = GlobalAlloc(GPTR,(lstrlen(lpszFileName)+1) * sizeof(TCHAR));
  103.     if (lpDIBInfo->lpszImageFileName)
  104.     {
  105.         _tcscpy(lpDIBInfo->lpszImageFileName, lpszFileName);
  106.     }
  107.  
  108.     // Open image file
  109.     hDIBFile = CreateFile(lpszFileName,
  110.                           GENERIC_READ,
  111.                           FILE_SHARE_READ,
  112.                           NULL,
  113.                           OPEN_EXISTING,
  114.                           FILE_ATTRIBUTE_NORMAL,
  115.                           (HANDLE)NULL);
  116.  
  117.     if (INVALID_HANDLE_VALUE == hDIBFile)
  118.     {
  119.         return FALSE;
  120.     }
  121.  
  122.     // Read DIB from file.
  123.     hDIB = ReadDIBFromFile(hDIBFile);
  124.     CloseHandle(hDIBFile);
  125.  
  126.     // Make sure that DIB file read was successful.
  127.     if (NULL == hDIB)
  128.     {
  129.         DebugMsg(__TEXT("fReadDIBInfo:  Failed to read DIB file.\r\n"));
  130.         return FALSE;
  131.     }
  132.  
  133.     // Get pointer to DIB.
  134.     lpBmpInfoHdr = (LPBITMAPINFOHEADER) GlobalLock(hDIB);
  135.     if (NULL == lpBmpInfoHdr)
  136.     {
  137.         GlobalFree(hDIB);
  138.         return FALSE;
  139.     }
  140.  
  141.     // Set values in DIBINFO structure
  142.     if (sizeof(BITMAPCOREHEADER) == lpBmpInfoHdr->biSize)
  143.     {
  144.         LPBITMAPCOREHEADER  lpCoreHdr;
  145.  
  146.         lpCoreHdr = (LPBITMAPCOREHEADER) lpBmpInfoHdr;
  147.         lpDIBInfo->uiDIBWidth   = (DWORD) lpCoreHdr->bcWidth;
  148.         lpDIBInfo->uiDIBHeight  = (DWORD) lpCoreHdr->bcHeight;
  149.         lpDIBInfo->dwDIBBits = lpCoreHdr->bcBitCount;
  150.     }
  151.     else
  152.     {
  153.         lpDIBInfo->dwDIBBits    = (DWORD)lpBmpInfoHdr->biBitCount;
  154.         lpDIBInfo->uiDIBWidth  = abs(lpBmpInfoHdr->biWidth);
  155.         lpDIBInfo->uiDIBHeight = abs(lpBmpInfoHdr->biHeight);
  156.     }
  157.  
  158.     // Set bmFormat.  Since the app only supports RGB's, pixel depth is enough
  159.     lpDIBInfo->bmFormat = (DWORD)-1;
  160.     switch (lpDIBInfo->dwDIBBits)
  161.     {
  162.         case 1:
  163.             //lpDIBInfo->bmFormat = BM_1GRAY;
  164.             lpDIBInfo->bmFormat = 0;
  165.             break;
  166.         case 16:
  167.             // Should either be 555 or 565 bitmap.
  168.             // Check mask if BI_BITFILEDS.
  169.             if ( (BI_BITFIELDS == lpBmpInfoHdr->biCompression)
  170.                  &&
  171.                  (0x7E0 == *((LPDWORD)(lpBmpInfoHdr + 1) +1))
  172.                )
  173.             {
  174.                 lpDIBInfo->bmFormat = BM_565RGB;
  175.             }
  176.             else
  177.             {
  178.                 lpDIBInfo->bmFormat = BM_x555RGB;
  179.             }
  180.             break;
  181.         case 24:
  182.             lpDIBInfo->bmFormat = BM_RGBTRIPLETS;  // RGB Triplets -- most significant byte is R
  183.             break;
  184.         case 32:
  185.             lpDIBInfo->bmFormat = BM_xRGBQUADS;
  186.             break;
  187.         case 4:
  188.         case 8:
  189.             lpDIBInfo->bmFormat = 0;
  190.             break;
  191.         default:
  192.             DebugMsg(__TEXT("fReadDIBInfo : Unknown dwDIBBits value.\r\n"));
  193.             break;
  194.     }
  195.     lpDIBInfo->hDIB = hDIB;
  196.     lpDIBInfo->dwStretchBltMode = ICMV_STRETCH_DEFAULT;
  197.     lpDIBInfo->bStretch = FALSE;
  198.     lpDIBInfo->hPal = CreateDIBPalette(hDIB);
  199.  
  200.     // Unlock bmp info.
  201.     GlobalUnlock(hDIB);
  202.     return TRUE;
  203. }   // End of function fReadDIBInfo
  204.  
  205.  
  206. //
  207. // Functions for DIBINFO structure
  208. //
  209. ///////////////////////////////////////////////////////////////////////
  210. //
  211. // Function:  GetDIBInfoHandle
  212. //
  213. // Purpose:   Encapsulates the getting and setting of the
  214. //            WW_DIB_HINFO value for a window since we are
  215. //            storing a handle, and handles change from 16
  216. //            bits in WIN16 to 32 bits in WIN32.
  217. //
  218. // Parms:     hWnd     == Window to retrieve the DIBINFO handle from.
  219. //
  220. // Returns:   The previous value.
  221. //
  222. ///////////////////////////////////////////////////////////////////////
  223.  
  224. HGLOBAL GetDIBInfoHandle (HWND hWnd)
  225. {
  226.     return (HGLOBAL)GetWindowLong(hWnd, GWL_DIBINFO);
  227. }
  228.  
  229.  
  230. //////////////////////////////////////////////////////////////////////////
  231. //  Function:  GetDIBInfoPtr
  232. //
  233. //  Description:
  234. //    Gets a pointer to the DIBINFO structure of the window.
  235. //
  236. //  Parameters:
  237. //    HWND    Handle to a window
  238. //
  239. //  Returns:
  240. //    LPDIBINFO  Pointer to DIBINFO structure.
  241. //
  242. //  Comments:
  243. //
  244. //
  245. //////////////////////////////////////////////////////////////////////////
  246. LPDIBINFO GetDIBInfoPtr(HWND hWnd)
  247. {
  248.     // Local variables
  249.     HGLOBAL     hDIBInfo;       // Handle to DIBINFO structure
  250.     LPDIBINFO   lpDIBInfo;      // Pointer to DIBINFO structure
  251.  
  252.     //  Initialize variables
  253.     lpDIBInfo = NULL;
  254.  
  255.     hDIBInfo = GetDIBInfoHandle(hWnd);
  256.     if (hDIBInfo != NULL)
  257.     {
  258.         lpDIBInfo = GlobalLock(hDIBInfo);
  259.         //lpDIBInfo = GlobalLock(hDIBInfo);
  260.     }
  261.  
  262.     return(lpDIBInfo);
  263. }   // End of function GetDIBInfoPtr
  264.  
  265.  
  266. //////////////////////////////////////////////////////////////////////////
  267. //  Function:  CreateDIBInfo
  268. //
  269. //  Description:
  270. //    Initializes the window/thread by setting the ICMINFO values to the
  271. //    current global values.  Then, the extra window information is set to
  272. //    point to the ICMINFO structure so that any operation using this
  273. //    window can obtain necesary information to manipulate the image.
  274. //
  275. //  Parameters:
  276. //    none.
  277. //
  278. //  Returns:
  279. //    HANDLE to DIBINFO structure.
  280. //
  281. //  Comments:
  282. //
  283. //////////////////////////////////////////////////////////////////////////
  284.  
  285. HGLOBAL CreateDIBInfo(void)
  286. {
  287.     // Local variables
  288.     HGLOBAL   hDIBInfo;           // Handle to ICMINFO structure
  289.     LPDIBINFO lpDIBInfo;          // Pointer to ICMINFO structure
  290.  
  291.     // Allocate DIBINFO structure and get a pointer to it
  292.     hDIBInfo = GlobalAlloc(GHND, sizeof(DIBINFO));
  293.     if ((HGLOBAL)NULL != hDIBInfo)
  294.     {
  295.         if (NULL != (lpDIBInfo = GlobalLock(hDIBInfo)))
  296.         {
  297.             InitDIBInfo(lpDIBInfo);
  298.         }
  299.         else
  300.         {
  301.             DebugMsg(__TEXT("DIBS.C : CreateDIBInfo : Failed to lock DIBINFO\r\n"));
  302.         }
  303.     }
  304.     else
  305.     {
  306.         DebugMsg(__TEXT("DIBS.C : CreateDIBInfo : Global alloc failed\r\n"));
  307.         return(NULL);
  308.     }
  309.     GlobalUnlock(hDIBInfo);
  310.     if (NULL == lpDIBInfo)
  311.     {
  312.         GlobalFree(hDIBInfo);
  313.         hDIBInfo = NULL;
  314.     }
  315.     return(hDIBInfo);
  316. }
  317.  
  318.  
  319. ///////////////////////////////////////////////////////////////////////////////
  320. //  Function:  fDuplicateDIBInfo
  321. //
  322. //  Description:
  323. //    Copies the source DIBINFO into the target DIBINFO.
  324. //
  325. //  Parameters:
  326. //    LPDIBINFO Target DIBINFO to recieve the contents of the source DIBINFO.
  327. //    LPDIBINFO Source DIBINFO to be copied into the target DIBINFO.
  328. //
  329. //  Returns:
  330. //    void
  331. //
  332. //  Comments:
  333. //
  334. ///////////////////////////////////////////////////////////////////////////////
  335. LPDIBINFO fDuplicateDIBInfo(LPDIBINFO lpDIDest, LPDIBINFO lpDISrc)
  336. {
  337.     // Local variables
  338.  
  339.     // Initialize variables
  340.     if (lpDISrc == NULL)
  341.     {
  342.         return(NULL);
  343.     }
  344.     if (lpDIDest == NULL)
  345.     {
  346.         lpDIDest = (LPDIBINFO)GlobalLock(CreateDIBInfo());
  347.     }
  348.     if (lpDIDest == (LPDIBINFO)NULL)
  349.     {
  350.         return(NULL);
  351.     }
  352.  
  353.     // Now, copy the body of the DIBINFO structure
  354.     lpDIDest->lpszImageFileName = CopyString(lpDISrc->lpszImageFileName);
  355.     CopyRect((LPRECT)&lpDIDest->rcClip, (LPRECT)&lpDISrc->rcClip);
  356.  
  357.     lpDIDest->hWndOwner         = lpDISrc->hWndOwner;
  358.     lpDIDest->hDIB              = lpDISrc->hDIB;
  359.     lpDIDest->hDIBTransformed   = lpDISrc->hDIBTransformed;
  360.     lpDIDest->hPal              = lpDISrc->hPal;
  361.     lpDIDest->dwDIBBits         = lpDISrc->dwDIBBits;
  362.     lpDIDest->uiDIBWidth        = lpDISrc->uiDIBWidth;
  363.     lpDIDest->uiDIBHeight       = lpDISrc->uiDIBHeight;
  364.     lpDIDest->bmFormat          = lpDISrc->bmFormat;
  365.  
  366.     lpDIDest->bStretch          = lpDISrc->bStretch;
  367.     lpDIDest->dwStretchBltMode  = lpDISrc->dwStretchBltMode;
  368.     lpDIDest->dwPrintOption     = lpDISrc->dwPrintOption;
  369.     lpDIDest->dwXScale          = lpDISrc->dwXScale;
  370.     lpDIDest->dwYScale          = lpDISrc->dwYScale;
  371.  
  372.     // Copy DEVMODE.
  373.     if (NULL != lpDISrc->pDevMode)
  374.     {
  375.         HANDLE  hDevMode = GlobalHandle(lpDISrc->pDevMode);
  376.         DWORD   dwSize = GlobalSize(hDevMode);
  377.  
  378.  
  379.         lpDIDest->pDevMode = (PDEVMODE) GlobalLock(GlobalAlloc(GHND, dwSize));
  380.         if (NULL != lpDIDest->pDevMode)
  381.         {
  382.             memcpy(lpDIDest->pDevMode, lpDISrc->pDevMode, dwSize);
  383.         }
  384.     }
  385.  
  386.     // Copy the ICM information now
  387.     fDuplicateICMInfo(lpDIDest, lpDISrc);
  388.  
  389.     // Made it this far--return pointer to DIBINFO
  390.     return(lpDIDest);
  391. } // End of function fDuplicateDIBInfo
  392.  
  393. //////////////////////////////////////////////////////////////////////////
  394. //  Function:  fDuplicateICMInfo
  395. //
  396. //  Description:
  397. //    Safely copies source ICM information in a DIBINFO structure to
  398. //    the target DIBINFO.
  399. //
  400. //  Parameters:
  401. //    @@@
  402. //
  403. //  Returns:
  404. //    BOOL
  405. //
  406. //  Comments:
  407. //
  408. //
  409. //////////////////////////////////////////////////////////////////////////
  410. BOOL fDuplicateICMInfo(LPDIBINFO lpDIDest, LPDIBINFO lpDISrc)
  411. {
  412.     // Local variables
  413.  
  414.     //  Initialize variables
  415.     if (NULL == lpDIDest)
  416.     {
  417.         DebugMsg(__TEXT("DIBS.C : fDuplicateICMInfo : NULL Target\r\n"));
  418.         return(FALSE);
  419.     }
  420.     if (NULL == lpDISrc)
  421.     {
  422.         DebugMsg(__TEXT("DIBS.C : fDuplicateICMInfo : NULL Source\r\n"));
  423.         return(FALSE);
  424.     }
  425.  
  426.     lpDIDest->dwICMFlags = lpDISrc->dwICMFlags;
  427.     lpDIDest->hLCS = lpDISrc->hLCS;
  428.  
  429.     // Copy strings.
  430.     UpdateString(&(lpDIDest->lpszMonitorName)   ,lpDISrc->lpszMonitorName);
  431.     UpdateString(&(lpDIDest->lpszMonitorProfile),lpDISrc->lpszMonitorProfile);
  432.     UpdateString(&(lpDIDest->lpszPrinterName   ),lpDISrc->lpszPrinterName);
  433.     UpdateString(&(lpDIDest->lpszPrinterProfile),lpDISrc->lpszPrinterProfile);
  434.     UpdateString(&(lpDIDest->lpszTargetProfile) ,lpDISrc->lpszTargetProfile);
  435.  
  436.     // Copy intents
  437.     lpDIDest->dwRenderIntent = lpDIDest->dwRenderIntent;
  438.     lpDIDest->dwProofingIntent = lpDIDest->dwProofingIntent;
  439.  
  440. }   // End of function fDuplicateICMInfo
  441.  
  442.  
  443. //////////////////////////////////////////////////////////////////////////
  444. //  Function:  FreeDIBInfo
  445. //
  446. //  Description:
  447. //    Frees the DIBINFO structure and its members.
  448. //
  449. //  Parameters:
  450. //    HGLOBAL   Handle to DIBINFO structure@@@
  451. //
  452. //  Returns:
  453. //    void
  454. //
  455. //  Comments:
  456. //    This function will also deallocate the association ICMINFO
  457. //    structure which is contained within the DIBINFO structure.
  458. //
  459. //////////////////////////////////////////////////////////////////////////
  460. BOOL fFreeDIBInfo(HGLOBAL hDIBInfo, BOOL bFreeDIBHandles)
  461. {
  462.     // Local variables
  463.     LPDIBINFO lpDIBInfo;
  464.     HGLOBAL   hFreed;
  465.     DWORD     dwLastError;
  466.  
  467.     // Initialize variables
  468.     if (hDIBInfo == NULL)
  469.     {
  470.         return(TRUE);
  471.     }
  472.  
  473.     // Obtain DIBINFO pointer
  474.     lpDIBInfo = GlobalLock(hDIBInfo);
  475.     if (lpDIBInfo == NULL)
  476.     {
  477.         return(FALSE);
  478.     }
  479.     // Have the pointer, let's free its members
  480.     if (lpDIBInfo->lpszImageFileName)
  481.         hFreed = GlobalFree(lpDIBInfo->lpszImageFileName);
  482.     if (lpDIBInfo->lpszMonitorName)
  483.         hFreed = GlobalFree(lpDIBInfo->lpszMonitorName);
  484.     if (lpDIBInfo->lpszMonitorProfile)
  485.         hFreed = GlobalFree(lpDIBInfo->lpszMonitorProfile);
  486.     if (lpDIBInfo->lpszPrinterName)
  487.         hFreed = GlobalFree(lpDIBInfo->lpszPrinterName);
  488.     if (lpDIBInfo->lpszPrinterProfile)
  489.         hFreed = GlobalFree(lpDIBInfo->lpszPrinterProfile);
  490.     if (lpDIBInfo->lpszTargetProfile)
  491.         hFreed = GlobalFree(lpDIBInfo->lpszTargetProfile);
  492.  
  493.     // Preserve last error if necessary
  494.     SetLastError(0);
  495.  
  496.     if (bFreeDIBHandles)
  497.     {
  498.         if (NULL != lpDIBInfo->hDIB)
  499.             GlobalFree(lpDIBInfo->hDIB);
  500.         if (NULL != lpDIBInfo->hDIBTransformed)
  501.             GlobalFree(lpDIBInfo->hDIBTransformed);
  502.     }
  503.     GlobalUnlock(hDIBInfo);
  504.     if (NULL != (GlobalFree(hDIBInfo))) // unsuccessful free
  505.     {
  506.         dwLastError = GetLastError();
  507.         DebugMsg(__TEXT("DIBS.C : fFreeDIBInfo : GlobalFree failed, LastError = %ld\r\n"), dwLastError);
  508.         return(FALSE);
  509.     }
  510.     return(TRUE);
  511. } // End of function fFreeDIBInfo
  512.  
  513. //////////////////////////////////////////////////////////////////////////
  514. //  Function:  InitDIBInfo
  515. //
  516. //  Description:
  517. //    Given a pointer to a DIBINFO structure, this function will place
  518. //    default values in all of its members.
  519. //
  520. //  Parameters:
  521. //    @@@
  522. //
  523. //  Returns:
  524. //    BOOL  Success / Failure.
  525. //
  526. //  Comments:
  527. //
  528. //
  529. //////////////////////////////////////////////////////////////////////////
  530. BOOL InitDIBInfo(LPDIBINFO lpDIBInfo)
  531. {
  532.     // Local variables
  533.  
  534.     //  Initialize variables
  535.     if (NULL == lpDIBInfo)
  536.     {
  537.         SetLastError(ERROR_INVALID_PARAMETER);
  538.         DebugMsg(__TEXT("DIBS.C : InitDIBInfo : lpDIBInfo == NULL\r\n"));
  539.         return(FALSE);
  540.     }
  541. #ifdef _DEBUG
  542.     memset(lpDIBInfo, UNINIT_BYTE, sizeof(DIBINFO));
  543. #endif
  544.  
  545.     lpDIBInfo->hWndOwner = NULL;
  546.     lpDIBInfo->lpszImageFileName = NULL;
  547.     lpDIBInfo->hDIB = NULL;
  548.     lpDIBInfo->hDIBTransformed = NULL;
  549.     lpDIBInfo->hPal = NULL;
  550.  
  551.     // Image attributes
  552.     lpDIBInfo->dwDIBBits = 0;
  553.     lpDIBInfo->uiDIBWidth   =   0;
  554.     lpDIBInfo->uiDIBHeight = 0;
  555.     lpDIBInfo->bmFormat = (DWORD)-1;
  556.  
  557.     // Display options
  558.     SetRect((LPRECT)&(lpDIBInfo->rcClip), 0, 0, 0, 0);
  559.     lpDIBInfo->dwStretchBltMode = ICMV_STRETCH_DEFAULT;
  560.     lpDIBInfo->bStretch = FALSE;
  561.  
  562.     // Printing Options
  563.     lpDIBInfo->dwPrintOption = ICMV_PRINT_DEFAULTSIZE;
  564.     lpDIBInfo->dwXScale = 0;
  565.     lpDIBInfo->dwYScale = 0;
  566.     lpDIBInfo->pDevMode = NULL;
  567.  
  568.     // ICM Attributes
  569.     lpDIBInfo->dwICMFlags = ICMVFLAGS_DEFAULT_ICMFLAGS;
  570.     lpDIBInfo->hLCS = NULL;
  571.     lpDIBInfo->lpszMonitorName = NULL;
  572.     lpDIBInfo->lpszMonitorProfile = NULL;
  573.     lpDIBInfo->lpszPrinterName = NULL;
  574.     lpDIBInfo->lpszPrinterProfile = NULL;
  575.     lpDIBInfo->lpszTargetProfile = NULL;
  576.     lpDIBInfo->dwRenderIntent = ICMV_RENDER_INTENT_DEFAULT;
  577.     lpDIBInfo->dwProofingIntent = ICMV_PROOFING_INTENT_DEFAULT;
  578.  
  579.     return(TRUE);
  580. }   // End of function InitDIBInfo
  581.  
  582.  
  583. //////////////////////////////////////////////////////////////////////////
  584. //  Function:  GetDefaultICMInfo
  585. //
  586. //  Description:
  587. //    Initializes the global DIBINFO structure with default profiles.
  588. //
  589. //  Parameters:
  590. //    @@@
  591. //
  592. //  Returns:
  593. //    void
  594. //
  595. //  Comments:
  596. //
  597. //
  598. //////////////////////////////////////////////////////////////////////////
  599. BOOL GetDefaultICMInfo(void)
  600. {
  601.     // Local variables
  602.     LPDIBINFO   lpDIBInfo;
  603.     HDC                 hDC;
  604.     BOOL                bRC;
  605.     LPSTR           lpszDefaultProfile;
  606.  
  607.     //  Initialize variables
  608.     bRC = TRUE;
  609.     lpszDefaultProfile = NULL;
  610.     lpDIBInfo = GetDIBInfoPtr(ghAppWnd); // Lock info for writing
  611.  
  612.     // Get display DC
  613.     hDC = GetDC(ghAppWnd);
  614.     lpDIBInfo->lpszMonitorName = GetRegistryString(HKEY_LOCAL_MACHINE,
  615.                                                    __TEXT("System\\CurrentControlSet\\Services\\Class\\Monitor\\0000"),
  616.                                                    __TEXT("DriverDesc"));
  617.     lpDIBInfo->lpszMonitorProfile = GetDefaultICMProfile(hDC);
  618.  
  619.     if (NULL == lpDIBInfo->lpszMonitorProfile)
  620.     {
  621.         DWORD   dwSize;
  622.  
  623.         GetStandardColorSpaceProfile(NULL, LCS_WINDOWS_COLOR_SPACE, NULL, &dwSize);
  624.         if (0 != dwSize)
  625.         {
  626.             lpszDefaultProfile = GlobalAlloc(GPTR, dwSize);
  627.             if (GetStandardColorSpaceProfile(NULL, LCS_WINDOWS_COLOR_SPACE, lpszDefaultProfile, &dwSize))
  628.             {
  629.                 GetBaseFilename(lpszDefaultProfile, &(lpDIBInfo->lpszMonitorProfile));
  630.             }
  631.             else
  632.             {
  633.                 DISPLAY_LASTERROR(LASTERROR_NOALLOC, GetLastError());
  634.                 bRC = FALSE;
  635.                 lpDIBInfo->lpszMonitorProfile = NULL;
  636.             }
  637.         }
  638.         else
  639.         {
  640.             lpDIBInfo->lpszMonitorProfile = GlobalAlloc(GPTR, (lstrlen(DEFAULT_ICM_PROFILE) +1 ) * sizeof(TCHAR));
  641.             _tcscpy(lpDIBInfo->lpszMonitorProfile, DEFAULT_ICM_PROFILE);
  642.         }
  643.  
  644.         DebugMsg(__TEXT("Display DC didn't get profile.  Using <%s>\r\n"), lpDIBInfo->lpszMonitorProfile);
  645.     }
  646.     DebugMsg(__TEXT("GetDefaultICMInfo:  Monitor profile <%s>\r\n"), lpDIBInfo->lpszMonitorProfile);
  647.     ReleaseDC(ghAppWnd, hDC);
  648.  
  649.  
  650.     if (bRC)
  651.     {
  652.         // Get printer name and DC
  653.         lpDIBInfo->lpszPrinterName = GetDefaultPrinterName();
  654.         if (lpDIBInfo->lpszPrinterName != NULL)
  655.         {
  656.             hDC = GetPrinterDC(lpDIBInfo->lpszPrinterName, lpDIBInfo->pDevMode);
  657.             if (hDC != NULL)
  658.             {
  659.                 lpDIBInfo->lpszPrinterProfile = GetDefaultICMProfile(hDC);
  660.                 lpDIBInfo->lpszTargetProfile= lpDIBInfo->lpszPrinterProfile;
  661.                 DebugMsg(__TEXT("GetDefaultICMInfo:  Printer profile <%s>\r\n"), lpDIBInfo->lpszPrinterProfile ?
  662.                          lpDIBInfo->lpszPrinterProfile : __TEXT("NULL"));
  663.                 DeleteDC(hDC);
  664.                 bRC = TRUE;
  665.             }
  666.         }
  667.         GlobalUnlock(GlobalHandle(lpDIBInfo));
  668.     }
  669.  
  670.     if (NULL != lpszDefaultProfile)
  671.     {
  672.         GlobalFree(lpszDefaultProfile);
  673.     }
  674.     return(bRC);
  675. }   // End of function GetDefaultICMInfo
  676.  
  677. //////////////////////////////////////////////////////////////////////////
  678. //  Function:  DumpDIBINFO
  679. //
  680. //  Description:
  681. //    Dumps DIBInfo structure
  682. //
  683. //  Parameters:
  684. //    @@@
  685. //
  686. //  Returns:
  687. //    void
  688. //
  689. //  Comments:
  690. //
  691. //
  692. //////////////////////////////////////////////////////////////////////////
  693. void DumpDIBINFO(LPTSTR lpszMsg, LPDIBINFO lpDIBInfo)
  694. {
  695.     // Local variables
  696.  
  697.     //  Initialize variables
  698.     DebugMsg(__TEXT("\r\n******************** DumpDIBINFO ********************\r\n"));
  699.     DebugMsg(__TEXT("***** %s \r\n"), lpszMsg);
  700.     if (lpDIBInfo == NULL)
  701.     {
  702.         DebugMsg(__TEXT("lpDIBInfo                    NULL\r\n\r\n"));
  703.         return;
  704.     }
  705.  
  706.     DebugMsg(__TEXT("lpDIBInfo                   0x%08lx\r\n"), lpDIBInfo);
  707.     if (lpDIBInfo->lpszImageFileName != NULL)
  708.     {
  709.         DebugMsg(__TEXT("lpszImageFileName           %s\r\n"), lpDIBInfo-> lpszImageFileName);
  710.     }
  711.     else
  712.     {
  713.         DebugMsg(__TEXT("lpszImageFileName           <NULL PTR>\r\n"));
  714.     }
  715.  
  716.     DebugMsg(__TEXT("hDIB                        0x%08lx\r\n"), lpDIBInfo-> hDIB);
  717.     DebugMsg(__TEXT("hDIBTransformed             0x%08lx\r\n"), lpDIBInfo-> hDIBTransformed);
  718.     DebugMsg(__TEXT("hPal                        0x%08lx\r\n"), lpDIBInfo-> hPal);
  719.     DebugMsg(__TEXT("dwDIBBits                   %lu\r\n"), lpDIBInfo-> dwDIBBits);
  720.     DebugMsg(__TEXT("uiDIBWidth                  %lu\r\n"), lpDIBInfo-> uiDIBWidth);
  721.     DebugMsg(__TEXT("uiDIBHeight                 %lu\r\n"), lpDIBInfo-> uiDIBHeight);
  722.     DebugMsg(__TEXT("bmFormat                    %ld\r\n"), (DWORD)(lpDIBInfo->bmFormat));
  723.     DumpRectangle(__TEXT("rcClip                      "), (LPRECT)&(lpDIBInfo->rcClip));
  724.     DebugMsg(__TEXT("dwStretchBltMode            %lu\r\n"), lpDIBInfo-> dwStretchBltMode);
  725.     DebugMsg(__TEXT("bStretch                    %lu\r\n"), lpDIBInfo-> bStretch);
  726.     DebugMsg(__TEXT("dwPrintOption               %lu\r\n"), lpDIBInfo-> dwPrintOption);
  727.     DebugMsg(__TEXT("dwXScale                    %lu\r\n"), lpDIBInfo-> dwXScale);
  728.     DebugMsg(__TEXT("dwYScale                    %lu\r\n\r\n"), lpDIBInfo-> dwYScale);
  729.     DebugMsg(__TEXT("lpszMonitorName             0x%08lX <%s>\r\n"), lpDIBInfo->lpszMonitorName   , lpDIBInfo->lpszMonitorName    ? lpDIBInfo->lpszMonitorName    : __TEXT("NULL"));
  730.     DebugMsg(__TEXT("lpszMonitorProfile          0x%08lX <%s>\r\n"), lpDIBInfo->lpszMonitorProfile, lpDIBInfo->lpszMonitorProfile ? lpDIBInfo->lpszMonitorProfile : __TEXT("NULL"));
  731.     DebugMsg(__TEXT("lpszPrinterName             0x%08lX <%s>\r\n"), lpDIBInfo->lpszPrinterName   , lpDIBInfo->lpszPrinterName    ? lpDIBInfo->lpszPrinterName    : __TEXT("NULL"));
  732.     DebugMsg(__TEXT("lpszPrinterProfile          0x%08lX <%s>\r\n"), lpDIBInfo->lpszPrinterProfile, lpDIBInfo->lpszPrinterProfile ? lpDIBInfo->lpszPrinterProfile : __TEXT("NULL"));
  733.     DebugMsg(__TEXT("lpszTargetProfile           0x%08lX <%s>\r\n\r\n"), lpDIBInfo->lpszTargetProfile, lpDIBInfo->lpszTargetProfile ? lpDIBInfo->lpszTargetProfile : __TEXT("NULL"));
  734.     DebugMsg(__TEXT("dwICMFlags                  %ld\r\n"), lpDIBInfo->dwICMFlags);
  735.     DebugMsg(__TEXT("dwRenderIntent              %ld\r\n"), lpDIBInfo->dwRenderIntent);
  736.     DebugMsg(__TEXT("dwProofingIntent            %ld\r\n"), lpDIBInfo->dwProofingIntent);
  737.     DebugMsg(__TEXT("^^^^^^^^^^DumpDIBINFO   0x%08lx^^^^^^^^^^^^^^\r\n\r\n\r\n"), lpDIBInfo);
  738. }   // End of function DumpDIBINFO
  739.  
  740.  
  741. //////////////////////////////////////////////////////////////////////////
  742. //  Function:  SetupDC
  743. //
  744. //  Description:
  745. //    Sets up DC for drawing based on DIBINFO.  This consolidates code for both
  746. //    printing and drawing to the screen.
  747. //
  748. //  Parameters:
  749. //    @@@
  750. //
  751. //  Returns:
  752. //    BOOL
  753. //
  754. //  Comments:
  755. //
  756. //
  757. //////////////////////////////////////////////////////////////////////////
  758. BOOL SetupDC(HDC hDC, LPDIBINFO lpDIBInfo, HPALETTE *phOldPalette, HDC *phDCPrinter)
  759. {
  760.     int     iICMMode;
  761.     BOOL    bRC;
  762.     TCHAR   stFullProfile[MAX_PATH];
  763.  
  764.  
  765.     // Initialize variables.
  766.     *phOldPalette = NULL;
  767.     *phDCPrinter = NULL;
  768.  
  769.     // Select/Realize our palette.  Make it a background palette, so that
  770.     // it doesn't mess up foreground palette when background windows repaint.
  771.     if (NULL != lpDIBInfo->hPal)
  772.     {
  773.         *phOldPalette = SelectPalette (hDC, lpDIBInfo->hPal, TRUE);
  774.         if (NULL == *phOldPalette)
  775.         {
  776.             DISPLAY_LASTERROR(LASTERROR_NOALLOC, GetLastError());
  777.         }
  778.     }
  779.  
  780.     //Only do ICM pre-processing if "Inside DC" is selected.
  781.     if (!CHECK_DWFLAG(lpDIBInfo->dwICMFlags, ICMVFLAGS_ICM20))
  782.     {
  783.         if (CHECK_DWFLAG(lpDIBInfo->dwICMFlags, ICMVFLAGS_ENABLE_ICM))
  784.         {
  785.             // build the FULL pathname to the profile.
  786.             wsprintf(stFullProfile,__TEXT("%s\\%s"), gstProfilesDir, lpDIBInfo->lpszMonitorProfile);
  787.  
  788.             if (SetICMProfile(hDC, stFullProfile))
  789.             {
  790.                 iICMMode = SetICMMode(hDC, ICM_ON);
  791.                 if (0 == iICMMode)
  792.                 {
  793.                     DISPLAY_LASTERROR(LASTERROR_NOALLOC,GetLastError());
  794.                     SetDWFlags((LPDWORD)&(lpDIBInfo->dwICMFlags), ICMVFLAGS_ENABLE_ICM, FALSE);
  795.                 }
  796.                 else
  797.                 {
  798.                     if ((CHECK_DWFLAG(lpDIBInfo->dwICMFlags, ICMVFLAGS_PROOFING)) && (NULL != lpDIBInfo->lpszTargetProfile))
  799.                     {
  800.                         *phDCPrinter = GetPrinterDC(lpDIBInfo->lpszPrinterName, lpDIBInfo->pDevMode);
  801.                         if (NULL != *phDCPrinter)
  802.                         {
  803.                             wsprintf(stFullProfile,__TEXT("%s\\%s"), gstProfilesDir, lpDIBInfo->lpszTargetProfile);
  804.                             bRC = SetICMProfile(*phDCPrinter, stFullProfile);
  805.                             if (bRC)
  806.                             {
  807.                                 iICMMode = SetICMMode(*phDCPrinter, ICM_ON);
  808.                                 if (0 != iICMMode)
  809.                                 {
  810.                                     bRC = ColorMatchToTarget(hDC, *phDCPrinter, CS_ENABLE);
  811.                                     if (!bRC)
  812.                                     {
  813.                                         DebugMsg(__TEXT("DIBPaint:  ColorMatchToTarget w/profile %s FAILED\r\n"), stFullProfile);
  814.                                         DISPLAY_LASTERROR(LASTERROR_NOALLOC,GetLastError());
  815.                                     }
  816.                                 }
  817.                                 else
  818.                                 {
  819.                                     DebugMsg(__TEXT("DIBPaint:  SetICMMode (%s, %s) FAILED!\r\n"), lpDIBInfo->lpszPrinterName, stFullProfile);
  820.                                     DISPLAY_LASTERROR(LASTERROR_NOALLOC,GetLastError());
  821.                                 }
  822.                             }
  823.                             else
  824.                             {
  825.                                 DebugMsg(__TEXT("DIBPaint:  SetICMProfile w/profile %s FAILED\r\n"), stFullProfile);
  826.                                 DISPLAY_LASTERROR(LASTERROR_NOALLOC,GetLastError());
  827.                             }
  828.                         }
  829.                         else
  830.                         {
  831.                             DebugMsg(__TEXT("DIBPaint:  GetPrinterDC() w/printer %s FAILED\r\n"), lpDIBInfo->lpszPrinterName);
  832.                             DISPLAY_LASTERROR(LASTERROR_NOALLOC,GetLastError());
  833.                         }
  834.                     }
  835.  
  836.                     if (NULL != lpDIBInfo->hPal)
  837.                     {
  838.                         WORD    wEntries;
  839.  
  840.  
  841.                         GetObject(lpDIBInfo->hPal, sizeof(wEntries), &wEntries);
  842.                         //if(!ColorCorrectPalette(hDC, lpDIBInfo->hPal, 0, (DWORD)wEntries))
  843.                         //{
  844.                         //    DISPLAY_LASTERROR(LASTERROR_NOALLOC, GetLastError());
  845.                         //}
  846.                     }
  847.                 }
  848.             }
  849.             else
  850.             {
  851.                 DebugMsg(__TEXT("DIBPaint : SetICMProfile(%s)FAILED\r\n"), stFullProfile);
  852.                 DISPLAY_LASTERROR(LASTERROR_NOALLOC,GetLastError());
  853.             }
  854.         }
  855.         else
  856.         {
  857.             iICMMode = SetICMMode(hDC, ICM_OFF);
  858.         }
  859.     }
  860.     else
  861.     {
  862.         iICMMode = SetICMMode(hDC, ICM_DONE_OUTSIDEDC);
  863.     }
  864.  
  865.     return TRUE;
  866. }
  867.  
  868.