home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / vb_code2 / str2bmp / str2bmp.cpp < prev    next >
C/C++ Source or Header  |  1993-06-13  |  16KB  |  549 lines

  1. // Code for converting BMPs to strings and strings to Bitmaps.
  2. //     (C) 1993, Balboa Technical Services.
  3. //     
  4. //  
  5. //
  6.  
  7. // Header files.
  8. #include <windows.h>
  9. #include "str2bmp.h"
  10. #pragma hdrstop
  11.  
  12. // DESCRIPTION:
  13. //     DLL Initialization code.
  14. // PARAMETERS:
  15. //     Windows stuff.
  16. // RETURN VALUE:
  17. //     1 if succesfully initialization, else 0.
  18. int FAR PASCAL LibMain (HANDLE hInstance, WORD wDataSeg, WORD cbHeapSize, LPSTR lpCmdLine)
  19.  { return 1;
  20.  }
  21.  
  22. // PARAMETERS: Windows stuff.
  23. // RETURN VALUE: 1 if succesfully initialization, else 0.
  24. //int FAR PASCAL WEP (int nParameter)
  25. // { // Dll removal successful.
  26. //   return 1;
  27. // }
  28.  
  29. //    DESCRIPTION:
  30. //        Function for converting VB bitmap to string.
  31. //    PARAMETERS:
  32. //        hdc : Picture.hdc
  33. //           himage : Picture.image
  34. //           string : VB string. Must be large enough to hold data. See
  35. //                         Bmp_GetSize for more info.
  36. //    RETURN VALUE:
  37. //        Number of pixels in the bitmap.
  38. //
  39. DLL_LONG BmpToString( HDC hdc, HBITMAP hImage, STRINGPTR string)
  40.  { // Local variables.
  41.    WORD bw, bh;           // Byte width and byte height.
  42.    HBITMAP hOldBitmap;
  43.  
  44.    // Get information about bitmap size.
  45.    BITMAP bm;
  46.    GetObject( hImage, sizeof( BITMAP), (LPSTR) &bm);
  47.  
  48.    // Select VB's picture...
  49.    hOldBitmap = (HBITMAP) SelectObject( hdc, hImage);
  50.  
  51.    // Store the width and height in a header at beginning of string
  52.    StringBmp_SetDimensions( string, bm.bmWidth, bm.bmHeight, bm.bmWidthBytes);
  53.  
  54.    // Copy bitmap data into string.
  55.    GetBitmapBits( hImage, bm.bmWidthBytes * bm.bmHeight, string+sizeof( SB));
  56.  
  57.    // DeSelect VB's picture.
  58.    SelectObject( hdc, hOldBitmap);
  59.  
  60.    // Return size in pixels of the bitmap.
  61.    return (bw*bh);
  62.  }
  63.  
  64. //    DESCRIPTION:
  65. //       Function for converting string to a bitmap.
  66. //    PARAMETERS:
  67. //          string : VB string. Must contain valid bitmap info created using
  68. //                        BmpToString
  69. //       hdcSrc : VB Source Picture.hdc
  70. //          hbmpSrc : VB Source Picture.image
  71. //    RETURN VALUE:
  72. //       ON SUCCESS: Number of pixels in the image.
  73. //       ON FAILURE: -1 if string does not contain valid bitmap info.
  74. //
  75. DLL_LONG StringToBmp( STRINGPTR string, HDC hdcSrc, HBITMAP hbmpSrc)
  76.  { // Local variables.
  77.    HBITMAP hOldBitmapNew;
  78.    HBITMAP hOldBitmapSrc;
  79.    HBITMAP hNewBitmap;
  80.    HDC hdcNew;
  81.    WORD bw, bh, bwb;
  82.    int ret;
  83.  
  84.    // Make sure parameter is bitmap string.
  85.    //   Return -1 if string does not contain valid bitmap info.
  86.    if( (ret = StringBmp_ValidateStringBmp( string))<0)
  87.     { return ret;                   
  88.     }
  89.  
  90.    // Get width and height from string header.
  91.    bw = StringBmp_GetWidth(string);
  92.    bh = StringBmp_GetHeight(string);
  93.    bwb = StringBmp_GetWidthBytes(string);
  94.       
  95.    // Create a new bitmap.
  96.    hdcNew = CreateCompatibleDC( hdcSrc);
  97.    hOldBitmapSrc = (HBITMAP) SelectObject( hdcSrc, hbmpSrc);
  98.    hNewBitmap = CreateCompatibleBitmap( hdcSrc, bw, bh);
  99.  
  100.    // Select the new bitmap.
  101.    hOldBitmapNew = (HBITMAP) SelectObject( hdcNew, hNewBitmap);
  102.  
  103.    // Copy bitmap data from string.
  104.    SetBitmapBits( hNewBitmap, bwb*bh, string+sizeof( SB));
  105.  
  106.    // Copy the new bitmap to VB's
  107.    BitBlt( hdcSrc, 0,0,bw,bh, hdcNew, 0,0, SRCCOPY);
  108.  
  109.    // DeSelect bitmaps.
  110.    SelectObject( hdcSrc, hOldBitmapSrc);
  111.    SelectObject( hdcNew, hOldBitmapNew);
  112.  
  113.    // Delete the new bitmap.
  114.    DeleteObject( hNewBitmap);
  115.    DeleteDC( hdcNew);
  116.  
  117.    // Return bitmap size in pixels.
  118.    return (bwb*bh);
  119.  }
  120.  
  121. //    DESCRIPTION:
  122. //        Return required size in bytes for a string to hold a bitmap.
  123. //    PARAMETERS:
  124. //           hImage : VB Picture.image
  125. //    RETURN VALUE:
  126. //        ON SUCCESS: Number of bytes to initialize a string size. Use the
  127. //               following code in VB:
  128. //                 Dim bytes as long, a$
  129. //                 bytes = Bmp_GetSize( Picture1.Image)
  130. //                 if bytes = 0 then stop ' Error! Picture too big.
  131. //                 a$ = spaces( bytes) ' Initialize string size.
  132. //        ON FAILURE: 0 if bitmap has more than 32000 pixels.
  133. //
  134. DLL_LONG Bmp_GetSize( HBITMAP hImage)
  135.  { // Local variables.
  136.    HBITMAP hOldBitmap;
  137.    DWORD bwB, hB;
  138.  
  139.    // Get information about bitmap size.
  140.    BITMAP bm;
  141.    GetObject( hImage, sizeof( BITMAP), (LPSTR) &bm);
  142.  
  143.    bwB = bm.bmWidthBytes;
  144.    hB = bm.bmHeight;
  145.  
  146.  
  147.    // Check for error condition
  148.    if( (bwB * hB + sizeof( SB) )>32000L)
  149.     { return 0;
  150.     }
  151.  
  152.    // Calculate size of string
  153.    return( sizeof( SB) + hB * bwB);
  154.  
  155.  }
  156.  
  157. //    DESCRIPTION:
  158. //        Returns the height in pixels of a bitmap.
  159. //    PARAMETERS:
  160. //           hImage : VB Picture.image
  161. //    RETURN VALUE:
  162. //           Height in pixels.
  163. //
  164. DLL_LONG Bmp_GetHeight( HBITMAP hImage)
  165.  { // Get information about bitmap size.
  166.    BITMAP bm;
  167.    GetObject( hImage, sizeof( BITMAP), (LPSTR) &bm);
  168.    return ( bm.bmHeight);
  169.  }
  170.  
  171. //    DESCRIPTION:
  172. //        Returns the width in pixels of a bitmap.
  173. //    PARAMETERS:
  174. //           hImage : VB Picture.image
  175. //    RETURN VALUE:
  176. //           Width in pixels.
  177. //
  178. DLL_LONG Bmp_GetWidth( HBITMAP hImage)
  179.  { // Get information about bitmap size.
  180.    BITMAP bm;
  181.    GetObject( hImage, sizeof( BITMAP), (LPSTR) &bm);
  182.    return( bm.bmWidthBytes);
  183.  }
  184.  
  185.  
  186. //    DESCRIPTION:
  187. //        Store the dimensions of the bitmap in a header at the beginning
  188. //              of the string.
  189. //    PARAMETERS:
  190. //           A : VB string
  191. //        w : width of VB bitmap to be stored in the string.
  192. //        h : height of VB bitmap to be stored in the string.
  193. //        wb : width in bytes (always even) of VB bitmap to be stored.
  194. //    RETURN VALUE:
  195. //        ON SUCCESS: 1
  196. //        ON FAILURE: -1 = Illegal format bitmap string.
  197. DLL_INT StringBmp_SetDimensions( STRINGPTR A, int w, int h, int wb)
  198.  { union StringBmpType temp;
  199.    temp.vbstring = A;
  200.    temp.stringbmp->identifier = StringBmpIdentifier;
  201.    temp.stringbmp->width = w;
  202.    temp.stringbmp->height = h;
  203.    temp.stringbmp->widthbytes = wb;
  204.    return 1;
  205.  }
  206.  
  207.  
  208. //    DESCRIPTION:
  209. //        Examine the header of a string containing bitmap info and
  210. //              determine the bitmap width.
  211. //    PARAMETERS:
  212. //           A : VB string containing bitmap info.
  213. //    RETURN VALUE:
  214. //        width in pixels of the stored bitmap.
  215. DLL_INT StringBmp_GetWidth( STRINGPTR A)
  216.  { union StringBmpType temp;
  217.    temp.vbstring = A;
  218.    return temp.stringbmp->width;
  219.  }
  220.  
  221. //    DESCRIPTION:
  222. //        Examine the header of a string containing bitmap info and
  223. //              determine the bitmap height.
  224. //    PARAMETERS:
  225. //           A : VB string containing bitmap info.
  226. //    RETURN VALUE:
  227. //        Height in pixels of the stored bitmap.
  228. DLL_INT StringBmp_GetHeight( LPSTR A)
  229.  { union StringBmpType temp;
  230.    temp.vbstring = A;
  231.    return temp.stringbmp->height;
  232.  }
  233.  
  234. //    DESCRIPTION:
  235. //        Examine the header of a string containing bitmap info and
  236. //              determine the bitmap width in bytes. (Always even).
  237. //    PARAMETERS:
  238. //           A : VB string containing bitmap info.
  239. //    RETURN VALUE:
  240. //        Width in bytes of the stored bitmap.
  241. DLL_INT StringBmp_GetWidthBytes( LPSTR A)
  242.  { union StringBmpType temp;
  243.    temp.vbstring = A;
  244.    return temp.stringbmp->widthbytes;
  245.   }
  246.  
  247. //    DESCRIPTION:
  248. //        Examine the header of a string containing bitmap info and
  249. //              return the "identifier word". This word is used to check
  250. //              whether the string contains valid bitmap info.
  251. //    PARAMETERS:
  252. //           A : VB string containing bitmap info.
  253. //    RETURN VALUE:
  254. //        Identifier word. Should be 'SB' (0x5342)
  255. DLL_INT StringBmp_GetIdentifier( STRINGPTR A)
  256.  { union StringBmpType temp;
  257.    temp.vbstring = A;
  258.    return temp.stringbmp->identifier;
  259.   }
  260.  
  261. //    DESCRIPTION:
  262. //        Examine the header of a string containing bitmap info and
  263. //              determine whether the bitmap contains valid bitmap info.
  264. //    PARAMETERS:
  265. //           A : VB string containing bitmap info.
  266. //    RETURN VALUE:
  267. //        ON SUCCESS: Return 0.
  268. //        ON FAILURE: -1 = invalid bitmap info.
  269. DLL_LONG StringBmp_ValidateStringBmp( STRINGPTR A)
  270.  { int wA, hA, wbA;
  271.  
  272.    // Check bitmap A:
  273.    if (A != NULL)
  274.     { if (StringBmp_GetIdentifier( A) != StringBmpIdentifier)
  275.        { return -1;
  276.        }
  277.       wA = StringBmp_GetWidth( A);
  278.       hA = StringBmp_GetHeight( A);
  279.       wbA = StringBmp_GetWidthBytes( A);
  280.       if( (wA== -1)||(hA==-1)||(wbA==-1) )
  281.        { return -1; // Illegal format bitmap string.
  282.        }
  283.     }
  284.  
  285.    // Success! The string contains a valid StringBmp.
  286.    return 0;
  287.  }
  288.  
  289. //    DESCRIPTION:
  290. //        Examine the header of two strings containing bitmap info and
  291. //              make sure they are the same size.
  292. //    PARAMETERS:
  293. //           A : VB string containing bitmap info.
  294. //           B : VB string containing bitmap info.
  295. //    RETURN VALUE:
  296. //        ON SUCCESS: 0 = bitmaps are the same size.
  297. //        ON FAILURE: -2 = Bitmaps are not the same size.
  298. DLL_LONG StringBmp_CheckSameSize( STRINGPTR A, STRINGPTR B)
  299.  { int wA, hA, wbA;
  300.    int wB, hB, wbB;
  301.  
  302.    // Check bitmap A:
  303.    wA = StringBmp_GetWidth( A);
  304.    hA = StringBmp_GetHeight( A);
  305.    wbA = StringBmp_GetWidthBytes( A);
  306.  
  307.    // Get size of bitmap B.
  308.    wB = StringBmp_GetWidth( B);
  309.    hB = StringBmp_GetHeight( B);
  310.    wbB = StringBmp_GetWidthBytes( B);
  311.    
  312.  
  313.    // Make sure both bitmaps the same size.
  314.    if ( !( (wbA == wbB) && (hA == hB) && (wA == wB) ) )
  315.     { return -2; // Bitmaps not the same size.
  316.     }
  317.  
  318.    // Everything OK.
  319.    return 0;
  320.  }
  321.  
  322. //    DESCRIPTION:
  323. //        Return number of pixels differing between two equal sized bitmaps.
  324. //    PARAMETERS:
  325. //           A : VB string containing bitmap info.
  326. //           B : VB string containing bitmap info.
  327. //    RETURN VALUE:
  328. //      ON SUCCESS: Return # of pixels differing (zero if no difference).
  329. //      ON FAILURE: -1 = Illegal format bitmap string.
  330. //                  -2 = Bitmaps not the same width/height.
  331. DLL_LONG StringBmp_COMPARE( STRINGPTR A, STRINGPTR B)
  332.  { // Local variables.
  333.    int wA, hA, wbA;
  334.    int h,w, ret;
  335.    DWORD DifferenceCount;
  336.    LPSTR ptrA, ptrB, ptrRESULT, ptrLINEA, ptrLINEB;
  337.  
  338.    // Validate parameters.
  339.    if( (ret = StringBmp_ValidateStringBmp( A))<0)
  340.     { return ret;
  341.     }
  342.    if( (ret = StringBmp_ValidateStringBmp( B))<0)
  343.     { return ret;
  344.     }
  345.  
  346.    // Make sure both are the same size
  347.    if( (ret = StringBmp_CheckSameSize( A, B))<0)
  348.     { return ret;
  349.     }
  350.  
  351.    // Get bitmap dimensions.
  352.    wA = StringBmp_GetWidth( A);
  353.    hA = StringBmp_GetHeight( A);
  354.    wbA = StringBmp_GetWidthBytes( A);
  355.  
  356.  
  357.    // Set Pointers to beginning of data in string.
  358.    ptrLINEA = A + sizeof( SB);
  359.    ptrLINEB = B + sizeof( SB);
  360.  
  361.    // Compare the strings.
  362.    DifferenceCount = 0L; 
  363.    for( h=0; h<hA; h++)
  364.     { ptrA = ptrLINEA;
  365.       ptrB = ptrLINEB;
  366.       ptrLINEA += wbA;
  367.       ptrLINEB += wbA;
  368.       for( w=0; w<wA; w++)
  369.        { if (*(ptrA++) !=  *(ptrB++))
  370.           { DifferenceCount++;   // Count # of deltas.
  371.           }
  372.        }
  373.     }
  374.  
  375.    return DifferenceCount;
  376.  }
  377.  
  378.  
  379. //    DESCRIPTION:
  380. //        For each pixel in strings A and B, perform an OR function and
  381. //           store the result in string RESULT. A and B must be the same size.
  382. //    PARAMETERS:
  383. //           A : VB string containing bitmap info.
  384. //           B : VB string containing bitmap info.
  385. //    RETURN VALUE:
  386. //      ON SUCCESS: Return # of white pixels in the result string.
  387. //      ON FAILURE: -1 = Illegal format bitmap string.
  388. //                  -2 = Bitmaps not the same width/height.
  389. DLL_LONG StringBmp_OR( STRINGPTR A, STRINGPTR B, STRINGPTR RESULT)
  390.  { // Local variables.
  391.    int wbA, hA, wA;
  392.    int i, ret, h, w;
  393.    char d;
  394.    DWORD WhiteCount;
  395.    LPSTR ptrA, ptrB, ptrRESULT;
  396.    LPSTR ptrLINEA, ptrLINEB, ptrLINERESULT;
  397.  
  398.    // Validate parameters.
  399.    if( (ret = StringBmp_ValidateStringBmp( A))<0)
  400.     { return ret;
  401.     }
  402.    if( (ret = StringBmp_ValidateStringBmp( B))<0)
  403.     { return ret;
  404.     }
  405.  
  406.    // Make sure both are the same size
  407.    if( (ret = StringBmp_CheckSameSize( A, B))<0)
  408.     { return ret;
  409.     }
  410.  
  411.    // Set the header for the result.
  412.    StringBmp_SetDimensions( RESULT, StringBmp_GetWidth(A), StringBmp_GetHeight(A), StringBmp_GetWidthBytes(A));
  413.  
  414.    // Get bitmap dimensions.
  415.    wA = StringBmp_GetWidth( A);
  416.    hA = StringBmp_GetHeight( A);
  417.    wbA = StringBmp_GetWidthBytes( A);
  418.  
  419.  
  420.    // Set Pointers to beginning of data in string.
  421.    ptrLINEA = A + sizeof( SB);
  422.    ptrLINEB = B + sizeof( SB);
  423.    ptrLINERESULT = RESULT + sizeof( SB);
  424.  
  425.    // OR the strings.
  426.    WhiteCount = 0; // Count the # of BLACK results.
  427.    for( h=0; h<hA; h++)
  428.     { ptrA = ptrLINEA;
  429.       ptrB = ptrLINEB;
  430.       ptrRESULT = ptrLINERESULT;
  431.       ptrLINEA += wbA;
  432.       ptrLINEB += wbA;
  433.       ptrLINERESULT += wbA;
  434.       for( w=0; w<wA; w++)
  435.        { if ( WHITE ==(*(ptrRESULT++) = (*(ptrA++) & *(ptrB++))))
  436.           { WhiteCount++;
  437.           }
  438.        }
  439.  
  440.     }
  441.    return WhiteCount;
  442.  }
  443.  
  444. //    DESCRIPTION:
  445. //        For each pixel in strings A and B, perform an AND function and
  446. //           store the result in string RESULT. A and B must be the same size.
  447. //    PARAMETERS:
  448. //           A : VB string containing bitmap info.
  449. //           B : VB string containing bitmap info.
  450. //    RETURN VALUE:
  451. //      ON SUCCESS: Return # of black pixels in the result string.
  452. //      ON FAILURE: -1 = Illegal format bitmap string.
  453. //                  -2 = Bitmaps not the same width/height.
  454. DLL_LONG StringBmp_AND( STRINGPTR A, STRINGPTR B, STRINGPTR RESULT)
  455.  { // Local variables.
  456.    int wbA, hA, wA;
  457.    int i, ret, h, w;
  458.    unsigned char d;
  459.    DWORD BlackCount;
  460.    LPSTR ptrA, ptrB, ptrRESULT;
  461.    LPSTR ptrLINEA, ptrLINEB, ptrLINERESULT;
  462.  
  463.    // Validate parameters.
  464.    if( (ret = StringBmp_ValidateStringBmp( A))<0)
  465.     { return ret;
  466.     }
  467.    if( (ret = StringBmp_ValidateStringBmp( B))<0)
  468.     { return ret;
  469.     }
  470.  
  471.    // Make sure both are the same size
  472.    if( (ret = StringBmp_CheckSameSize( A, B))<0)
  473.     { return ret;
  474.     }
  475.  
  476.    // Set the header for the result.
  477.    StringBmp_SetDimensions( RESULT, StringBmp_GetWidth(A), StringBmp_GetHeight(A), StringBmp_GetWidthBytes(A));
  478.  
  479.    // Get bitmap dimensions.
  480.    wA = StringBmp_GetWidth( A);
  481.    hA = StringBmp_GetHeight( A);
  482.    wbA = StringBmp_GetWidthBytes( A);
  483.  
  484.    // Set Pointers to beginning of data in string.
  485.    ptrLINEA = A + sizeof( SB);
  486.    ptrLINEB = B + sizeof( SB);
  487.    ptrLINERESULT = RESULT + sizeof( SB);
  488.  
  489.    // OR the strings.
  490.    BlackCount = 0; // Count the # of BLACK results.
  491.    for( h=0; h<hA; h++)
  492.     { ptrA = ptrLINEA;
  493.       ptrLINEA += wbA;
  494.  
  495.       ptrB = ptrLINEB;
  496.       ptrLINEB += wbA;
  497.  
  498.       ptrRESULT = ptrLINERESULT;
  499.       ptrLINERESULT += wbA;
  500.  
  501.       for( w=0; w<wA; w++)
  502.        { if( BLACK == (*(ptrRESULT++) = (*(ptrA++) | *(ptrB++))))
  503.           { BlackCount++;
  504.           }
  505.        }
  506.     }
  507.    return BlackCount;
  508.  }
  509.  
  510. //    DESCRIPTION:
  511. //        For each pixel in string A, perform a NOT function and
  512. //           store the result in string RESULT.
  513. //    PARAMETERS:
  514. //           A : VB string containing bitmap info.
  515. //    RETURN VALUE:
  516. //      ON SUCCESS: Return # of pixels in A.
  517. //      ON FAILURE: -1 = Illegal format bitmap string.
  518. //                  -2 = Bitmaps not the same width/height.
  519. DLL_LONG StringBmp_INVERT( LPSTR A, LPSTR RESULT)
  520.  { // Local variables.
  521.    int wA, hA;
  522.    int ret;
  523.    LONG i, size;
  524.    LPSTR ptrA, ptrRESULT;
  525.  
  526.    // Validate parameters.
  527.    if( (ret = StringBmp_ValidateStringBmp( A))<0) 
  528.     { return ret;
  529.     }
  530.  
  531.    // Set the header for the result.
  532.    StringBmp_SetDimensions( RESULT, StringBmp_GetWidth(A), StringBmp_GetHeight(A), StringBmp_GetWidthBytes(A));
  533.  
  534.    // Calculate size in bytes.
  535.    size = StringBmp_GetWidthBytes(A)*StringBmp_GetHeight(A); 
  536.  
  537.    // Set Pointers to beginning of data in string.
  538.    ptrA = A + sizeof( SB);
  539.    ptrRESULT = RESULT + sizeof( SB);
  540.  
  541.    // Invert the string.
  542.    for( i=0; i<size; i++)
  543.     { *(ptrRESULT++) = ~*(ptrA++);
  544.     }
  545.  
  546.    return (size);
  547.  }
  548.  
  549.