Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <header-wrappers/winapi/windows-h.hpp>
- #include <opencv2/opencv.hpp>
- #include <assert.h>
- namespace win_cv
- {
- inline void display_aligned_bgr( const HDC dc, const cv::Mat& image )
- {
- constexpr int pixels_per_meter = 2835; // 72 DPI × 39.3701 inch/meter. Is ignored.
- assert( image.channels() == 3 ); // BGR format.
- assert( CV_MAT_DEPTH( image.type() ) == CV_8U ); // Of bytes.
- assert( image.step % 4 == 0 ); // With 4 byte row alignment.
- const int logical_width = image.cols;
- const int physical_width = int( image.step / 3 ); assert( image.step % 3 == 0 );
- const int height = image.rows;
- BITMAPINFO bmpinfo = {};
- bmpinfo.bmiHeader.biSize = sizeof (BITMAPINFOHEADER );
- bmpinfo.bmiHeader.biBitCount = 24;
- bmpinfo.bmiHeader.biClrImportant = 0;
- bmpinfo.bmiHeader.biClrUsed = 0;
- bmpinfo.bmiHeader.biCompression = BI_RGB;
- bmpinfo.bmiHeader.biWidth = physical_width;
- bmpinfo.bmiHeader.biHeight = -height;
- bmpinfo.bmiHeader.biPlanes = 1;
- bmpinfo.bmiHeader.biSizeImage = 0;
- bmpinfo.bmiHeader.biXPelsPerMeter = pixels_per_meter;
- bmpinfo.bmiHeader.biYPelsPerMeter = pixels_per_meter;
- //const auto& h = bmpinfo.bmiHeader;
- //const int stride = ((((h.biWidth * h.biBitCount) + 31) & ~31) >> 3);
- //const int cvstep = image.step;
- //assert( stride == cvstep );
- const int n_scan_lines_set = SetDIBitsToDevice(
- dc, // hdc
- 0, // xDest
- 0, // yDest
- logical_width, // w
- height, // h
- 0, // xSrc
- 0, // ySrc
- 0, // StartScan
- height, // cLines
- image.data, // lpvBits
- &bmpinfo, // lpbmi
- DIB_RGB_COLORS // ColorUse
- );
- (void) n_scan_lines_set;
- }
- inline auto compatible_image( const cv::Mat image )
- -> cv::Mat
- {
- const int stepmod = image.step % 4;
- if( stepmod == 0 ) {
- return image;
- } else {
- const int colsmod = image.cols % 4;
- cv::Mat aligned_image_data( image.rows, image.cols + (4 - colsmod), image.type() );
- cv::Mat aligned_image = aligned_image_data( cv::Rect( 0, 0, image.cols, image.rows ) );
- image.copyTo( aligned_image );
- return aligned_image;
- }
- }
- inline void display_bgr( const HDC dc, const cv::Mat& image )
- {
- const int stepmod = image.step % 4;
- if( stepmod == 0 ) {
- display_aligned_bgr( dc, image );
- } else {
- display_aligned_bgr( dc, compatible_image( image ) );
- }
- }
- } // namespace win_cv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement