home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap16 / Dibble / DibConv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.7 KB  |  160 lines

  1. /*-------------------------------------------------------
  2.    DIBCONV.C -- Converts DIBs from one format to another
  3.                 (c) Charles Petzold, 1998
  4.   -------------------------------------------------------*/
  5.  
  6. // #define WINVER 0x0500              // eventually remove
  7. #include <windows.h>
  8. #include "dibhelp.h"
  9. #include "dibpal.h"
  10. #include "dibconv.h"
  11.  
  12. HDIB DibConvert (HDIB hdibSrc, int iBitCountDst)
  13. {
  14.      HDIB         hdibDst ;
  15.      HPALETTE     hPalette ;
  16.      int          i, x, y, cx, cy, iBitCountSrc, cColors ;
  17.      PALETTEENTRY pe ;
  18.      RGBQUAD      rgb ;
  19.      WORD         wNumEntries ;
  20.  
  21.      cx = DibWidth (hdibSrc) ;
  22.      cy = DibHeight (hdibSrc) ;
  23.      iBitCountSrc = DibBitCount (hdibSrc) ;
  24.  
  25.      if (iBitCountSrc == iBitCountDst)
  26.           return NULL ;
  27.  
  28.           // DIB with color table to DIB with larger color table:
  29.  
  30.      if ((iBitCountSrc < iBitCountDst) && (iBitCountDst <= 8))
  31.      {
  32.           cColors = DibNumColors (hdibSrc) ;
  33.           hdibDst = DibCreate (cx, cy, iBitCountDst, cColors) ;
  34.  
  35.           for (i = 0 ; i < cColors ; i++)
  36.           {
  37.                DibGetColor (hdibSrc, i, &rgb) ;
  38.                DibSetColor (hdibDst, i, &rgb) ;
  39.           }
  40.  
  41.           for (x = 0 ; x < cx ; x++)
  42.           for (y = 0 ; y < cy ; y++)
  43.           {
  44.                DibSetPixel (hdibDst, x, y, DibGetPixel (hdibSrc, x, y)) ;
  45.           }
  46.      }
  47.           // Any DIB to DIB with no color table
  48.  
  49.      else if (iBitCountDst >= 16)
  50.      {
  51.           hdibDst = DibCreate (cx, cy, iBitCountDst, 0) ;
  52.  
  53.           for (x = 0 ; x < cx ; x++)
  54.           for (y = 0 ; y < cy ; y++)
  55.           {
  56.                DibGetPixelColor (hdibSrc, x, y, &rgb) ;
  57.                DibSetPixelColor (hdibDst, x, y, &rgb) ;
  58.           }
  59.      }
  60.           // DIB with no color table to 8-bit DIB
  61.  
  62.      else if (iBitCountSrc >= 16 && iBitCountDst == 8)
  63.      {
  64.           hPalette = DibPalMedianCut (hdibSrc, 6) ;
  65.  
  66.           GetObject (hPalette, sizeof (WORD), &wNumEntries) ;
  67.  
  68.           hdibDst = DibCreate (cx, cy, 8, wNumEntries) ;
  69.           
  70.           for (i = 0 ; i < (int) wNumEntries ; i++)
  71.           {
  72.                GetPaletteEntries (hPalette, i, 1, &pe) ;
  73.  
  74.                rgb.rgbRed   = pe.peRed ;
  75.                rgb.rgbGreen = pe.peGreen ;
  76.                rgb.rgbBlue  = pe.peBlue ;
  77.                rgb.rgbReserved = 0 ;
  78.  
  79.                DibSetColor (hdibDst, i, &rgb) ;
  80.           }
  81.  
  82.           for (x = 0 ; x < cx ; x++)
  83.           for (y = 0 ; y < cy ; y++)
  84.           {
  85.                DibGetPixelColor (hdibSrc, x, y, &rgb) ;
  86.  
  87.                DibSetPixel (hdibDst, x, y,
  88.                     GetNearestPaletteIndex (hPalette, 
  89.                          RGB (rgb.rgbRed, rgb.rgbGreen, rgb.rgbBlue))) ;
  90.           }
  91.           DeleteObject (hPalette) ;
  92.      }
  93.           // Any DIB to monochrome DIB
  94.  
  95.      else if (iBitCountDst == 1)
  96.      {
  97.           hdibDst = DibCreate (cx, cy, 1, 0) ;
  98.           hPalette = DibPalUniformGrays (2) ;
  99.  
  100.           for (i = 0 ; i < 2 ; i++)
  101.           {
  102.                GetPaletteEntries (hPalette, i, 1, &pe) ;
  103.  
  104.                rgb.rgbRed   = pe.peRed ;
  105.                rgb.rgbGreen = pe.peGreen ;
  106.                rgb.rgbBlue  = pe.peBlue ;
  107.                rgb.rgbReserved = 0 ;
  108.  
  109.                DibSetColor (hdibDst, i, &rgb) ;
  110.           }
  111.  
  112.           for (x = 0 ; x < cx ; x++)
  113.           for (y = 0 ; y < cy ; y++)
  114.           {
  115.                DibGetPixelColor (hdibSrc, x, y, &rgb) ;
  116.  
  117.                DibSetPixel (hdibDst, x, y,
  118.                     GetNearestPaletteIndex (hPalette, 
  119.                          RGB (rgb.rgbRed, rgb.rgbGreen, rgb.rgbBlue))) ;
  120.           }
  121.           DeleteObject (hPalette) ;
  122.      }
  123.           // All non-monochrome DIBs to 4-bit DIB
  124.  
  125.      else if (iBitCountSrc >= 8 && iBitCountDst == 4)
  126.      {
  127.           hdibDst = DibCreate (cx, cy, 4, 0) ;
  128.           hPalette = DibPalVga () ;
  129.  
  130.           for (i = 0 ; i < 16 ; i++)
  131.           {
  132.                GetPaletteEntries (hPalette, i, 1, &pe) ;
  133.  
  134.                rgb.rgbRed   = pe.peRed ;
  135.                rgb.rgbGreen = pe.peGreen ;
  136.                rgb.rgbBlue  = pe.peBlue ;
  137.                rgb.rgbReserved = 0 ;
  138.  
  139.                DibSetColor (hdibDst, i, &rgb) ;
  140.           }
  141.  
  142.           for (x = 0 ; x < cx ; x++)
  143.           for (y = 0 ; y < cy ; y++)
  144.           {
  145.                DibGetPixelColor (hdibSrc, x, y, &rgb) ;
  146.  
  147.                DibSetPixel (hdibDst, x, y,
  148.                     GetNearestPaletteIndex (hPalette, 
  149.                          RGB (rgb.rgbRed, rgb.rgbGreen, rgb.rgbBlue))) ;
  150.           }
  151.           DeleteObject (hPalette) ;
  152.      }
  153.           // Should not be necessary
  154.  
  155.      else
  156.           hdibDst = NULL ;
  157.  
  158.      return hdibDst ;
  159. }
  160.