{"id":1251,"date":"2017-02-20T10:28:21","date_gmt":"2017-02-20T01:28:21","guid":{"rendered":"http:\/\/rageworx.info\/?p=1251"},"modified":"2021-05-17T01:48:22","modified_gmt":"2021-05-16T16:48:22","slug":"fltk-tech-hicon-to-fl_rgb_image","status":"publish","type":"post","link":"https:\/\/rageworx.info\/?p=1251","title":{"rendered":"FLTK tech, HICON to Fl_RGB_Image"},"content":{"rendered":"<p><a href=\"http:\/\/rageworx.info\/wp-content\/uploads\/2017\/02\/fltk_win_hicon_to_fl_rgb_exam.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-1252\" src=\"http:\/\/rageworx.info\/wp-content\/uploads\/2017\/02\/fltk_win_hicon_to_fl_rgb_exam-500x248.jpg\" alt=\"\" width=\"500\" height=\"248\" srcset=\"https:\/\/rageworx.info\/wp-content\/uploads\/2017\/02\/fltk_win_hicon_to_fl_rgb_exam-500x248.jpg 500w, https:\/\/rageworx.info\/wp-content\/uploads\/2017\/02\/fltk_win_hicon_to_fl_rgb_exam.jpg 512w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>This is a simple source code for converting Windows HICON to Fl_RGB_Image for my own customized FLTK window.<\/p>\n<p>Before understanding how it works, It must be requires understanding of some Windows API like LoadImage().<\/p>\n<h3>API, icon_to_flrgb()<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"dracula\">Fl_RGB_Image* icon_to_flrgb(HICON hIcon)\r\n{\r\n    BITMAP bm;\r\n    ICONINFO iconInfo;\r\n\r\n    GetIconInfo(hIcon, &amp;iconInfo);\r\n    GetObject(iconInfo.hbmColor, sizeof(BITMAP),&amp;bm);\r\n\r\n    int width = bm.bmWidth;\r\n    int height = bm.bmHeight;\r\n    int bytesPerScanLine = (width * 3 + 3) &amp; 0xFFFFFFFC;\r\n    int size = bytesPerScanLine * height;\r\n\r\n    BITMAPINFO infoheader = {0};\r\n    infoheader.bmiHeader.biSize     = sizeof(BITMAPINFOHEADER);\r\n    infoheader.bmiHeader.biWidth    = width;\r\n    infoheader.bmiHeader.biHeight   = height;\r\n    infoheader.bmiHeader.biPlanes   = 1;\r\n    infoheader.bmiHeader.biBitCount = 24;\r\n    infoheader.bmiHeader.biCompression = BI_RGB;\r\n    infoheader.bmiHeader.biSizeImage = size;\r\n\r\n    \/\/ allocate Memory for Icon RGB data plus Icon mask plus ARGB buffer for the resulting image\r\n    unsigned char* pixelsIconRGB = new unsigned char[ height * width * 4 ];\r\n\r\n    if ( pixelsIconRGB == NULL )\r\n    {\r\n        return NULL;\r\n    }\r\n\r\n    unsigned char* alphaPixels   = new unsigned char[ height * width * 4 ];\r\n\r\n    if ( alphaPixels == NULL )\r\n    {\r\n        delete[] pixelsIconRGB;\r\n\r\n        return NULL;\r\n    }\r\n\r\n    unsigned char* imagePixels   = new unsigned char[ height * width * 4 ];\r\n\r\n    if ( imagePixels == NULL )\r\n    {\r\n        delete[] pixelsIconRGB;\r\n        delete[] alphaPixels;\r\n\r\n        return NULL;\r\n    }\r\n\r\n    HDC hDC = CreateCompatibleDC(NULL);\r\n\r\n    if ( hDC == NULL )\r\n    {\r\n        delete[] pixelsIconRGB;\r\n        delete[] alphaPixels;\r\n\r\n        return NULL;\r\n    }\r\n\r\n    HBITMAP hBmpOld = (HBITMAP)SelectObject(hDC, (HGDIOBJ)iconInfo.hbmColor);\r\n\r\n    if( GetDIBits(hDC, iconInfo.hbmColor, 0, height, (LPVOID) pixelsIconRGB, &amp;infoheader, DIB_RGB_COLORS) == 0 )\r\n    {\r\n        DeleteDC(hDC);\r\n\r\n        delete[] pixelsIconRGB;\r\n        delete[] alphaPixels;\r\n        delete[] imagePixels;\r\n\r\n        return NULL;\r\n    }\r\n\r\n    SelectObject(hDC, hBmpOld);\r\n   \/\/ now get the mask\r\n    if( GetDIBits(hDC, iconInfo.hbmMask, 0,height,(LPVOID)alphaPixels, &amp;infoheader, DIB_RGB_COLORS) == 0 )\r\n    {\r\n        DeleteDC(hDC);\r\n\r\n        delete[] pixelsIconRGB;\r\n        delete[] alphaPixels;\r\n        delete[] imagePixels;\r\n\r\n        return NULL;\r\n    }\r\n\r\n    DeleteDC(hDC);\r\n\r\n    int x=0;\r\n    int currentSrcPos=0;\r\n    int currentDestPos=0;\r\n    int linePosSrc = 0;\r\n    int linePosDest = 0;\r\n    int vsDest = height-1;\r\n\r\n    for(int y=0; y&lt;height; y++)\r\n    {\r\n        linePosSrc  = ( vsDest - y ) * ( width * 3 );\r\n        linePosDest = y * width * 4;\r\n\r\n        for(x=0; x&lt;width; x++)\r\n        {\r\n            currentDestPos = linePosDest + ( x * 4 );\r\n            currentSrcPos  = linePosSrc + ( x * 3);\r\n\r\n            imagePixels[ currentDestPos + 0 ] = pixelsIconRGB[ currentSrcPos + 2 ];\r\n            imagePixels[ currentDestPos + 1 ] = pixelsIconRGB[ currentSrcPos + 1 ];\r\n            imagePixels[ currentDestPos + 2 ] = pixelsIconRGB[ currentSrcPos + 0 ];\r\n            imagePixels[ currentDestPos + 3 ] = 0xFF - alphaPixels[ currentSrcPos ];\r\n        }\r\n    }\r\n\r\n    Fl_RGB_Image* pImage = new Fl_RGB_Image( imagePixels, width, height, 4 );\r\n\r\n    delete[] pixelsIconRGB;\r\n    delete[] alphaPixels;\r\n\r\n    return pImage;\r\n}<\/pre>\n<p>Plus, to remove Fl_RGB_Image in safe, use this.<\/p>\n<h3>remove_fl_rgb_image()<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"dracula\">void remove_fl_rgb_image( Fl_RGB_Image** img )\r\n{\r\n    if ( img != NULL )\r\n    {\r\n        Fl_RGB_Image* srci = (Fl_RGB_Image*)*img;\r\n\r\n        if ( ( srci-&gt;array != NULL ) &amp;&amp; ( srci-&gt;alloc_array == 0 ) )\r\n        {\r\n            delete[] srci-&gt;array;\r\n        }\r\n\r\n        delete srci;\r\n\r\n        *img = NULL;\r\n    }\r\n}<\/pre>\n<p>And, let know understand how it calls :<\/p>\n<h3>Sequence to convert image and apply, then discard.<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"dracula\">\/\/ Load small ICON in size 16x16.\r\nHICON hIconWindowSmall = (HICON)LoadImage( fl_display,MAKEINTRESOURCE( IDC_ICON_A ),IMAGE_ICON,16,16,LR_SHARED );\r\n\r\n\/\/ Convert HICON to Fl_RGB_Image ..\r\nFl_RGB_Image* convtitleiconimg = icon_to_flrgb( hIconWindowSmall );\r\nif ( convtitleiconimg != NULL )\r\n{\r\n    window_title_box-&gt;image( convtitleiconimg );\r\n}\r\n\r\n.... something do .... and now .\r\n\r\nremove_fl_rgb_image( &amp;convtitleiconimg );<\/pre>\n<p>remove_fl_rgb_image() may called in end of program, when it terminates or class destructor to release memory contains user allocated memory, and it is specification of FLTK 1.3.x.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a simple source code for converting Windows HICON to Fl_RGB_Image for my own customized FLTK window. Before understanding how it works, It must be requires understanding of some Windows API like LoadImage(). API, icon_to_flrgb() Fl_RGB_Image* icon_to_flrgb(HICON hIcon) { BITMAP bm; ICONINFO iconInfo; GetIconInfo(hIcon, &amp;iconInfo); GetObject(iconInfo.hbmColor, sizeof(BITMAP),&amp;bm); int width&#8230; <a href=\"https:\/\/rageworx.info\/?p=1251\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,3],"tags":[10,11,469,71,468,467],"class_list":["post-1251","post","type-post","status-publish","format-standard","hentry","category-development","category-raphs","tag-c","tag-code","tag-converting","tag-fltk","tag-fl_rgb_image","tag-hicon"],"_links":{"self":[{"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/posts\/1251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rageworx.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1251"}],"version-history":[{"count":0,"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/posts\/1251\/revisions"}],"wp:attachment":[{"href":"https:\/\/rageworx.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rageworx.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rageworx.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}