{"id":628,"date":"2012-11-27T10:12:00","date_gmt":"2012-11-27T10:12:00","guid":{"rendered":"http:\/\/www.bardecode.com\/en1\/?p=628"},"modified":"2013-02-26T11:46:34","modified_gmt":"2013-02-26T11:46:34","slug":"the-difference-between-a-bitmap-and-a-dib","status":"publish","type":"post","link":"https:\/\/www.bardecode.com\/en1\/the-difference-between-a-bitmap-and-a-dib\/","title":{"rendered":"The difference between a BITMAP and a DIB"},"content":{"rendered":"<p>There is much confusion about bitmap handles (HBITMAP) and device independent bitmaps (DIB). The terms are often interchanged and functions that you would expect to return a DIB, such as CreateDIBitmap, do in fact return a value of type HBITMAP.<\/p>\n<p>HBITMAP&#8217;s are handles to memory objects containing a BITMAP structure followed by palette information and bitmap data and should be processed by ScanBarCodeFromBitmap:<\/p>\n<p>DIB&#8217;s are handles to memory objects that have the same format as a BMP file minus the file header (BITMAPFILEHEADER) and should be processed by ScanBarCodeFromDIB.\u00a0The following MFC C++ code can be used to load a small BMP file into a DIB:<\/p>\n<p>#include\u00a0&lt;limits.h&gt;<\/p>\n<div>\/\/ Simple function to load a BMP file into a DIB. This assumes that the file is smaller than UINT_MAX.<\/div>\n<p>&nbsp;<\/p>\n<p>HGLOBAL LoadDIB( LPCSTR sBMPFile)<\/p>\n<p>{<\/p>\n<p>CFile file;<\/p>\n<p>&nbsp;<\/p>\n<p>if( !file.Open( sBMPFile, CFile::modeRead) )<\/p>\n<p>return\u00a0NULL;<\/p>\n<p>&nbsp;<\/p>\n<p>BITMAPFILEHEADER bmfHeader;<\/p>\n<p>ULONGLONG nFileLen;<\/p>\n<p>&nbsp;<\/p>\n<p>nFileLen = file.GetLength();<\/p>\n<p>&nbsp;<\/p>\n<p>if\u00a0(nFileLen &gt; UINT_MAX)<\/p>\n<p>{<\/p>\n<p>AfxMessageBox(&#8220;File is too big to load with a single read&#8221;);<\/p>\n<p>return\u00a0NULL ;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Read file header and ignore<\/p>\n<p>if\u00a0(file.Read((LPSTR)&amp;bmfHeader,\u00a0sizeof(bmfHeader)) !=\u00a0sizeof(bmfHeader))<\/p>\n<p>return\u00a0NULL;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ File type should be &#8216;BM&#8217;<\/p>\n<p>if\u00a0(bmfHeader.bfType != ((WORD) (&#8216;M&#8217;\u00a0&lt;&lt; 8) |\u00a0&#8216;B&#8217;))\u00a0return\u00a0NULL;<\/p>\n<p>&nbsp;<\/p>\n<p>HGLOBAL hDIB = ::GlobalAlloc(GMEM_FIXED, (SIZE_T) nFileLen);<\/p>\n<p>if\u00a0(hDIB == 0)<\/p>\n<p>return\u00a0NULL;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Read the remainder of the bitmap file.<\/p>\n<p>if\u00a0(file.Read((LPSTR)hDIB, (UINT) nFileLen &#8211;\u00a0sizeof(BITMAPFILEHEADER)) !=<\/p>\n<p>(UINT) nFileLen &#8211;\u00a0sizeof(BITMAPFILEHEADER) )<\/p>\n<p>{<\/p>\n<p>::GlobalFree(hDIB);<\/p>\n<p>return\u00a0NULL;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>return\u00a0hDIB;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>The following wikipedia article contains a more detailed explanation:\u00a0http:\/\/en.wikipedia.org\/wiki\/BMP_file_format<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is much confusion about bitmap handles (HBITMAP) and device independent bitmaps (DIB). The terms are often interchanged and functions that you would expect to return a DIB, such as CreateDIBitmap, do in fact return a value of type HBITMAP. HBITMAP&#8217;s are handles to memory objects containing a BITMAP structure followed by palette information and<\/p>\n<p><a href=\"https:\/\/www.bardecode.com\/en1\/the-difference-between-a-bitmap-and-a-dib\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":337,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[19,151,152,153,51,62],"class_list":["post-628","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-image","tag-barcode","tag-bitmap","tag-dib","tag-file-type","tag-image-2","tag-software"],"_links":{"self":[{"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/posts\/628"}],"collection":[{"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/comments?post=628"}],"version-history":[{"count":6,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/posts\/628\/revisions"}],"predecessor-version":[{"id":1569,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/posts\/628\/revisions\/1569"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/media\/337"}],"wp:attachment":[{"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/media?parent=628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/categories?post=628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bardecode.com\/en1\/wp-json\/wp\/v2\/tags?post=628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}