home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / VISUAL_B / ARQS_ZIP / IKE.ZIP / ICOREAD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-10  |  5.5 KB  |  205 lines

  1. /*=========================================================================*/
  2. /*                                                                         */
  3. /* ICOREAD.C                                                               */
  4. /*                                                                         */
  5. /* Responsible for reading the icon in from an external ICO file.          */
  6. /*                                                                         */
  7. /* (C) Copyright 1990, 1991  Marc Adler/Magma Systems  All Rights Reserved */
  8. /*                                                                         */
  9. /*=========================================================================*/
  10.  
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include <memory.h>
  14. #include <dos.h>
  15. #include <fcntl.h>
  16. #include <io.h>
  17. #include <sys\types.h>
  18. #include <sys\stat.h>
  19. #include <string.h>
  20. #include <windows.h>
  21. #include "ico.h"
  22.  
  23.  
  24. int ICORead(hWnd, pszFile)
  25.   HWND hWnd;
  26.   char *pszFile;
  27. {
  28.   HDC  hDC;
  29.   int  fd;
  30.   int  i;
  31.   char pFilename[65];
  32.  
  33.   HANDLE hANDMask;
  34.   PSTR pANDMask;
  35.   long nPictureSize;
  36.   int  nTotalBytesPerLine;
  37.  
  38.   HANDLE hBitmapBytes;
  39.   LPSTR  lpBitmapBytes;
  40.   HANDLE      hbmi;
  41.   LPBITMAPINFO lpbmi;
  42.  
  43.   extern BYTE rgb[16][3];
  44.   extern HWND hWndColor;
  45.  
  46.  
  47.   /*
  48.     Open the ICO file
  49.   */
  50.   strcpy(pFilename, pszFile);
  51.   if (strchr(pFilename, '.') == NULL)
  52.     strcat(pFilename, ".ico");
  53.   if ((fd = open(pFilename, O_RDONLY | O_BINARY)) < 0)
  54.   {
  55.     MessageBox(hWndMain, "Couldn't open file", NULL, MB_OK);
  56.     return FALSE;
  57.   }
  58.   strcpy(IconInfo.szFilename, pFilename);
  59.  
  60.   /*
  61.     Read the icon header and the icon directory entry
  62.   */
  63.   read(fd, (char *) &IconHeader, sizeof(IconHeader));
  64.   read(fd, (char *) &IconDir, sizeof(IconDir));
  65.  
  66.   /*
  67.     Make sure that we are dealing with 16-color icons...
  68.   */
  69.   if (IconDir.ColorCount != 16)
  70.   {
  71.     MessageBox(hWnd, "IKE can only deal with 16-color icons.", "Read Error",
  72.                MB_OK | MB_ICONEXCLAMATION);
  73.     return FALSE;
  74.   }
  75.  
  76.   /*
  77.     Free the memory taken up by the previous icon bitmap.
  78.   */
  79.   if (IconInfo.hBitmapInfo)
  80.   {
  81.     GlobalUnlock(IconInfo.hBitmapInfo);
  82.     GlobalFree(IconInfo.hBitmapInfo);
  83.     IconInfo.hBitmapInfo = NULL;
  84.   }
  85.  
  86.   hbmi = GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, 
  87.                      (DWORD) sizeof(BITMAPINFOHEADER) + 16*sizeof(RGBQUAD));
  88.   IconInfo.hBitmapInfo = hbmi;
  89.   lpbmi = (LPBITMAPINFO) GlobalLock(hbmi);
  90.   if (lpbmi == NULL)
  91.   {
  92.     MessageBox(hWnd, "Can't lock lpbmi", "Read", MB_OK);
  93.     close(fd);
  94.     return FALSE;
  95.   }
  96.   _lread(fd, (LPSTR) lpbmi, sizeof(BITMAPINFOHEADER) + 16*sizeof(RGBQUAD));
  97.   for (i = 0;  i < 16;  i++)
  98.     _fmemcpy(rgb[i], &lpbmi->bmiColors[i], 3);
  99.   GlobalUnlock(hbmi);
  100.  
  101.   /*
  102.     First, let's read the AND mask in. This is found at the tail end
  103.     of the bitmap data. We move (length*height/8) bytes backwards from
  104.     (icoDIBOffset + icoDIBSize), and at that point, we find the AND
  105.     mask.
  106.   */
  107.   hANDMask = LocalAlloc(LMEM_FIXED, IconDir.Width*IconDir.Height/8);
  108.   pANDMask = LocalLock(hANDMask);
  109.   if (pANDMask == NULL)
  110.   {
  111.     MessageBox(hWndMain, "Can't allocate AND mask buffer", NULL, MB_OK);
  112.     close(fd);
  113.     return FALSE;
  114.   }
  115.   lseek(fd, (long)
  116.         IconDir.icoDIBOffset+IconDir.icoDIBSize-(IconDir.Width*IconDir.Height/8),
  117.         0);
  118.   _lread(fd, (LPSTR) pANDMask, IconDir.Width*IconDir.Height/8);
  119.   LocalUnlock(hANDMask);
  120.   LocalFree(hANDMask);
  121.  
  122.   /*
  123.     Allocate a global buffer to hold the bitmap data. The size of the
  124.     bitmap is the area in the pixel rectangle divided by 8-bits-per-byte,
  125.     times the number of color planes.
  126.   */
  127.   switch (IconDir.ColorCount)
  128.   {
  129.     case 2 : /* 1 byte = 8 pixels */
  130.       nTotalBytesPerLine = IconDir.Width / 8;
  131.       break;
  132.     case 8 : /* 1 byte = 2 pixels??? */
  133.       nTotalBytesPerLine = IconDir.Width / 2;
  134.       break;
  135.     case 16 : /* 1 byte = 2 pixels */
  136.       nTotalBytesPerLine = IconDir.Width / 2;
  137.       break;
  138.   }
  139.   nPictureSize = (long) nTotalBytesPerLine * IconDir.Height;
  140.  
  141.  
  142.   if (IconInfo.hBitmapBits)
  143.   {
  144.     GlobalUnlock(IconInfo.hBitmapBits);
  145.     GlobalFree(IconInfo.hBitmapBits);
  146.     IconInfo.hBitmapBits = NULL;
  147.   }
  148.  
  149.   hBitmapBytes = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, nPictureSize);
  150.   IconInfo.hBitmapBits = hBitmapBytes;
  151.   if (hBitmapBytes == NULL)
  152.   {
  153.     MessageBox(hWndMain, "Can't allocate global bitmap buffer", NULL, MB_OK);
  154.     close(fd);
  155.     return FALSE;
  156.   }
  157.   lpBitmapBytes = GlobalLock(hBitmapBytes);
  158.   if (lpBitmapBytes == NULL)
  159.   {
  160.     MessageBox(hWndMain, "Can't lock global bitmap buffer", NULL, MB_OK);
  161.     close(fd);
  162.     return FALSE;
  163.   }
  164.  
  165.  
  166.   /*
  167.     Now read the bitmap image in.
  168.   */
  169.   lseek(fd, (long)
  170.         IconDir.icoDIBOffset + IconDir.icoDIBSize -
  171.         (IconDir.Width*IconDir.Height/8) - (nTotalBytesPerLine*IconDir.Height),
  172.         0);
  173.   _lread(fd, lpBitmapBytes, (int) (nTotalBytesPerLine*IconDir.Height));
  174.   GlobalUnlock(hBitmapBytes);
  175.  
  176.   /*
  177.     Cause the edit and image windows to be repainted
  178.   */
  179.   InvalidateRect(hWnd, (LPRECT) NULL, TRUE);
  180.   InvalidateRect(hWndActualImage, (LPRECT) NULL, TRUE);
  181.   InvalidateRect(hWndColor, (LPRECT) NULL, TRUE);
  182.   close(fd);
  183.   return TRUE;
  184. }
  185.  
  186.  
  187. #ifdef DEBUG
  188. Dump(char *szFile, char huge *p, int iHeight, int iWidth)
  189. {
  190.   int i, j;
  191.   FILE *fp;
  192.  
  193.   fp = fopen(szFile, "w");
  194.   for (i = 0;  i < iHeight;  i++)
  195.   {
  196.     for (j = 0;  j < iWidth;  j++)
  197.       fprintf(fp, "%02x", (unsigned char) *p++);
  198.     fprintf(fp, "\n");
  199.   }
  200.  
  201.   fflush(fp);
  202.   fclose(fp);
  203. }
  204. #endif
  205.